OSDN Git Service

More code refactoring: Got rid of the "binaries" class + added a SourceFactory class...
[x264-launcher/x264-launcher.git] / src / thread_binaries.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Simple x264 Launcher
3 // Copyright (C) 2004-2015 LoRd_MuldeR <MuldeR2@GMX.de>
4 //
5 // This program is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation; either version 2 of the License, or
8 // (at your option) any later version.
9 //
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 // GNU General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License along
16 // with this program; if not, write to the Free Software Foundation, Inc.,
17 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 //
19 // http://www.gnu.org/licenses/gpl-2.0.txt
20 ///////////////////////////////////////////////////////////////////////////////
21
22 #include "thread_binaries.h"
23
24 #include <QLibrary>
25 #include <QEventLoop>
26 #include <QTimer>
27 #include <QMutexLocker>
28 #include <QApplication>
29 #include <QProcess>
30 #include <QDir>
31
32 //Internal
33 #include "global.h"
34 #include "model_sysinfo.h"
35 #include "win_updater.h"
36 #include "encoder_factory.h"
37 #include "source_factory.h"
38
39 //MUtils
40 #include <MUtils/Global.h>
41 #include <MUtils/OSSupport.h>
42
43 //Static
44 QMutex BinariesCheckThread::m_binLock;
45 QScopedPointer<QFile> BinariesCheckThread::m_binPath[MAX_BINARIES];
46
47 //Whatever
48 #define NEXT(X) ((*reinterpret_cast<int*>(&(X)))++)
49 #define SHFL(X) ((*reinterpret_cast<int*>(&(X))) <<= 1)
50
51 //External
52 QString AVS_CHECK_BINARY(const SysinfoModel *sysinfo, const bool& x64);
53
54 //-------------------------------------
55 // External API
56 //-------------------------------------
57
58 bool BinariesCheckThread::check(SysinfoModel *sysinfo)
59 {
60         QMutexLocker lock(&m_binLock);
61
62         QEventLoop loop;
63         BinariesCheckThread thread(sysinfo);
64
65         QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
66
67         connect(&thread, SIGNAL(finished()), &loop, SLOT(quit()));
68         connect(&thread, SIGNAL(terminated()), &loop, SLOT(quit()));
69         
70         thread.start();
71         QTimer::singleShot(30000, &loop, SLOT(quit()));
72         
73         qDebug("Binaries checker thread has been created, please wait...");
74         loop.exec(QEventLoop::ExcludeUserInputEvents);
75         qDebug("Binaries checker thread finished.");
76
77         QApplication::restoreOverrideCursor();
78
79         if(!thread.wait(1000))
80         {
81                 qWarning("Binaries checker thread encountered timeout -> probably deadlock!");
82                 thread.terminate();
83                 thread.wait();
84                 return false;
85         }
86
87         if(thread.getException())
88         {
89                 qWarning("Binaries checker thread encountered an exception !!!");
90                 return false;
91         }
92         
93         return thread.getSuccess();
94 }
95
96 //-------------------------------------
97 // Thread class
98 //-------------------------------------
99
100 BinariesCheckThread::BinariesCheckThread(const SysinfoModel *const sysinfo)
101 :
102         m_sysinfo(sysinfo)
103 {
104         m_success = m_exception = false;
105 }
106
107 BinariesCheckThread::~BinariesCheckThread(void)
108 {
109 }
110
111 void BinariesCheckThread::run(void)
112 {
113         m_success = m_exception = false;
114         checkBinaries1(m_success, m_sysinfo, &m_exception);
115 }
116
117 void BinariesCheckThread::checkBinaries1(volatile bool &success, const SysinfoModel *const sysinfo, volatile bool *exception)
118 {
119         __try
120         {
121                 checkBinaries2(success, sysinfo, exception);
122         }
123         __except(1)
124         {
125                 *exception = true;
126                 qWarning("Unhandled exception error in binaries checker thread !!!");
127         }
128 }
129
130 void BinariesCheckThread::checkBinaries2(volatile bool &success, const SysinfoModel *const sysinfo, volatile bool *exception)
131 {
132         try
133         {
134                 return checkBinaries3(success, sysinfo);
135         }
136         catch(...)
137         {
138                 *exception = true;
139                 qWarning("Binaries checker initializdation raised an C++ exception!");
140         }
141 }
142
143 void BinariesCheckThread::checkBinaries3(volatile bool &success, const SysinfoModel *const sysinfo)
144 {
145         success = true;
146
147         //Create list of all required binary files
148         QStringList binFiles;
149         for(OptionsModel::EncType encdr = OptionsModel::EncType_X264; encdr <= OptionsModel::EncType_X265; NEXT(encdr))
150         {
151                 const AbstractEncoderInfo &encInfo = EncoderFactory::getEncoderInfo(encdr);
152                 const QFlags<OptionsModel::EncVariant> variants = encInfo.getVariants();
153                 for(OptionsModel::EncArch arch = OptionsModel::EncArch_x86_32; arch <= OptionsModel::EncArch_x86_64; NEXT(arch))
154                 {
155                         for(OptionsModel::EncVariant varnt = OptionsModel::EncVariant_MIN; varnt <= OptionsModel::EncVariant_MAX; SHFL(varnt))
156                         {
157                                 if(variants.testFlag(varnt))
158                                 {
159                                         binFiles << encInfo.getBinaryPath(sysinfo, arch, varnt);
160                                 }
161                         }
162                 }
163         }
164         for(int i = 0; i < 2; i++)
165         {
166                 binFiles << SourceFactory::getSourceInfo(SourceFactory::SourceType_AVS).getBinaryPath(sysinfo, bool(i));
167                 binFiles << AVS_CHECK_BINARY(sysinfo, bool(i));
168         }
169         for(size_t i = 0; UpdaterDialog::BINARIES[i].name; i++)
170         {
171                 if(UpdaterDialog::BINARIES[i].exec)
172                 {
173                         binFiles << QString("%1/toolset/common/%2").arg(sysinfo->getAppPath(), QString::fromLatin1(UpdaterDialog::BINARIES[i].name));
174                 }
175         }
176
177         //Actually validate the binaries
178         size_t currentFile = 0;
179         for(QStringList::ConstIterator iter = binFiles.constBegin(); iter != binFiles.constEnd(); iter++)
180         {
181                 QScopedPointer<QFile> file(new QFile(*iter));
182                 qDebug("%s", MUTILS_UTF8(file->fileName()));
183
184                 if(file->open(QIODevice::ReadOnly))
185                 {
186                         if(!MUtils::OS::is_executable_file(file->fileName()))
187                         {
188                                 success = false;
189                                 qWarning("Required tool does NOT look like a valid Win32/Win64 binary:\n%s\n", MUTILS_UTF8(file->fileName()));
190                                 return;
191                         }
192                         if(currentFile < MAX_BINARIES)
193                         {
194                                 m_binPath[currentFile++].reset(file.take());
195                                 continue;
196                         }
197                         qFatal("Current binary file exceeds max. number of binaries!");
198                 }
199                 else
200                 {
201                         success = false;
202                         qWarning("Required tool could not be found or access denied:\n%s\n", MUTILS_UTF8(file->fileName()));
203                         return;
204                 }
205         }
206 }