OSDN Git Service

0df2e84260b05d56b93e049c1bae8dfed5357075
[x264-launcher/x264-launcher.git] / src / thread_binaries.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Simple x264 Launcher
3 // Copyright (C) 2004-2019 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 <QSet>
28 #include <QMutexLocker>
29 #include <QApplication>
30 #include <QProcess>
31 #include <QDir>
32
33 //Internal
34 #include "global.h"
35 #include "model_sysinfo.h"
36 #include "win_updater.h"
37 #include "encoder_factory.h"
38 #include "source_factory.h"
39
40 //MUtils
41 #include <MUtils/Global.h>
42 #include <MUtils/OSSupport.h>
43
44 //Static
45 QMutex BinariesCheckThread::m_binLock;
46 QScopedPointer<QFile> BinariesCheckThread::m_binPath[MAX_BINARIES];
47
48 //Whatever
49 #define NEXT(X) ((*reinterpret_cast<int*>(&(X)))++)
50 #define SHFL(X) ((*reinterpret_cast<int*>(&(X))) <<= 1)
51
52 //External
53 QString AVS_CHECK_BINARY(const SysinfoModel *sysinfo, const bool& x64);
54
55 //-------------------------------------
56 // External API
57 //-------------------------------------
58
59 bool BinariesCheckThread::check(const SysinfoModel *const sysinfo, QString *const failedPath)
60 {
61         QMutexLocker lock(&m_binLock);
62
63         QEventLoop loop;
64         BinariesCheckThread thread(sysinfo);
65
66         QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
67
68         connect(&thread, SIGNAL(finished()),   &loop, SLOT(quit()));
69         connect(&thread, SIGNAL(terminated()), &loop, SLOT(quit()));
70         
71         thread.start();
72         QTimer::singleShot(30000, &loop, SLOT(quit()));
73         
74         qDebug("Binaries checker thread has been created, please wait...");
75         loop.exec(QEventLoop::ExcludeUserInputEvents);
76         qDebug("Binaries checker thread finished.");
77
78         QApplication::restoreOverrideCursor();
79
80         if(!thread.wait(5000))
81         {
82                 qWarning("Binaries checker thread encountered timeout -> probably deadlock!");
83                 thread.terminate();
84                 thread.wait();
85                 return false;
86         }
87
88         if(thread.getException())
89         {
90                 qWarning("Binaries checker thread encountered an exception !!!");
91                 return false;
92         }
93         
94         const bool success = thread.getSuccess();
95         if ((!success) && failedPath)
96         {
97                 *failedPath = thread.getFailedPath();
98         }
99
100         return success;
101 }
102
103 //-------------------------------------
104 // Thread class
105 //-------------------------------------
106
107 BinariesCheckThread::BinariesCheckThread(const SysinfoModel *const sysinfo)
108 :
109         m_sysinfo(sysinfo)
110 {
111         m_success = m_exception = false;
112 }
113
114 BinariesCheckThread::~BinariesCheckThread(void)
115 {
116 }
117
118 void BinariesCheckThread::run(void)
119 {
120         m_success = m_exception = false;
121         m_failedPath = QString();
122         checkBinaries1(m_success, m_failedPath, m_sysinfo, &m_exception);
123 }
124
125 void BinariesCheckThread::checkBinaries1(volatile bool &success, QString &failedPath, const SysinfoModel *const sysinfo, volatile bool *exception)
126 {
127         __try
128         {
129                 checkBinaries2(success, failedPath, sysinfo, exception);
130         }
131         __except(1)
132         {
133                 *exception = true;
134                 qWarning("Unhandled exception error in binaries checker thread !!!");
135         }
136 }
137
138 void BinariesCheckThread::checkBinaries2(volatile bool &success, QString &failedPath, const SysinfoModel *const sysinfo, volatile bool *exception)
139 {
140         try
141         {
142                 return checkBinaries3(success, failedPath, sysinfo);
143         }
144         catch(...)
145         {
146                 *exception = true;
147                 qWarning("Binaries checker initializdation raised an C++ exception!");
148         }
149 }
150
151 void BinariesCheckThread::checkBinaries3(volatile bool &success, QString &failedPath, const SysinfoModel *const sysinfo)
152 {
153         success = true;
154
155         //Create list of all required binary files
156         typedef QPair<QString, bool> FileEntry;
157         QList<FileEntry> binFiles;
158         for(OptionsModel::EncType encdr = OptionsModel::EncType_MIN; encdr <= OptionsModel::EncType_MAX; NEXT(encdr))
159         {
160                 const AbstractEncoderInfo &encInfo = EncoderFactory::getEncoderInfo(encdr);
161                 const quint32 archCount = encInfo.getArchitectures().count();
162                 QSet<QString> filesSet;
163                 for (quint32 archIdx = 0; archIdx < archCount; ++archIdx)
164                 {
165                         const QStringList variants = encInfo.getVariants();
166                         for (quint32 varntIdx = 0; varntIdx < quint32(variants.count()); ++varntIdx)
167                         {
168                                 const QStringList dependencies = encInfo.getDependencies(sysinfo, archIdx, varntIdx);
169                                 for (QStringList::ConstIterator iter = dependencies.constBegin(); iter != dependencies.constEnd(); iter++)
170                                 {
171                                         if (!filesSet.contains(*iter))
172                                         {
173                                                 filesSet << (*iter);
174                                                 binFiles << qMakePair(*iter, true);
175                                         }
176                                 }
177                                 const QString binary = encInfo.getBinaryPath(sysinfo, archIdx, varntIdx);
178                                 if (!filesSet.contains(binary))
179                                 {
180                                         filesSet << binary;
181                                         binFiles << qMakePair(binary, false);
182                                 }
183                         }
184                 }
185         }
186         for(int i = 0; i < 2; i++)
187         {
188                 binFiles << qMakePair(SourceFactory::getSourceInfo(SourceFactory::SourceType_AVS).getBinaryPath(sysinfo, bool(i)), false);
189                 binFiles << qMakePair(AVS_CHECK_BINARY(sysinfo, bool(i)), false);
190         }
191         for(size_t i = 0; UpdaterDialog::BINARIES[i].name; i++)
192         {
193                 if(UpdaterDialog::BINARIES[i].exec)
194                 {
195                         binFiles << qMakePair(QString("%1/toolset/common/%2").arg(sysinfo->getAppPath(), QString::fromLatin1(UpdaterDialog::BINARIES[i].name)), false);
196                 }
197         }
198
199         //Actually validate the binaries
200         size_t currentFile = 0;
201         for(QList<FileEntry>::ConstIterator iter = binFiles.constBegin(); iter != binFiles.constEnd(); iter++)
202         {
203                 QScopedPointer<QFile> file(new QFile(iter->first));
204                 qDebug("%s", MUTILS_UTF8(file->fileName()));
205
206                 if(file->open(QIODevice::ReadOnly))
207                 {
208                         if(!iter->second)
209                         {
210                                 if (!MUtils::OS::is_executable_file(file->fileName()))
211                                 {
212                                         failedPath = file->fileName();
213                                         success = false;
214                                         qWarning("Required tool does NOT look like a valid Win32/Win64 binary:\n%s\n", MUTILS_UTF8(file->fileName()));
215                                         return;
216                                 }
217                         }
218                         else
219                         {
220                                 if (!MUtils::OS::is_library_file(file->fileName()))
221                                 {
222                                         failedPath = file->fileName();
223                                         success = false;
224                                         qWarning("Required tool does NOT look like a valid Win32/Win64 library:\n%s\n", MUTILS_UTF8(file->fileName()));
225                                         return;
226                                 }
227                         }
228                         if(currentFile < MAX_BINARIES)
229                         {
230                                 m_binPath[currentFile++].reset(file.take());
231                                 continue;
232                         }
233                         qFatal("Current binary file exceeds max. number of binaries!");
234                 }
235                 else
236                 {
237                         failedPath = file->fileName();
238                         success = false;
239                         qWarning("Required tool could not be found or access denied:\n%s\n", MUTILS_UTF8(file->fileName()));
240                         return;
241                 }
242         }
243 }