Bump version + updated changelog.
[simple-x264-launcher.git] / src / thread_binaries.cpp
blob3bd6a197e5ac77f60108c4463ca3f553ce8b76c3
1 ///////////////////////////////////////////////////////////////////////////////
2 // Simple x264 Launcher
3 // Copyright (C) 2004-2018 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.
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.
19 // http://www.gnu.org/licenses/gpl-2.0.txt
20 ///////////////////////////////////////////////////////////////////////////////
22 #include "thread_binaries.h"
24 #include <QLibrary>
25 #include <QEventLoop>
26 #include <QTimer>
27 #include <QSet>
28 #include <QMutexLocker>
29 #include <QApplication>
30 #include <QProcess>
31 #include <QDir>
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"
40 //MUtils
41 #include <MUtils/Global.h>
42 #include <MUtils/OSSupport.h>
44 //Static
45 QMutex BinariesCheckThread::m_binLock;
46 QScopedPointer<QFile> BinariesCheckThread::m_binPath[MAX_BINARIES];
48 //Whatever
49 #define NEXT(X) ((*reinterpret_cast<int*>(&(X)))++)
50 #define SHFL(X) ((*reinterpret_cast<int*>(&(X))) <<= 1)
52 //External
53 QString AVS_CHECK_BINARY(const SysinfoModel *sysinfo, const bool& x64);
55 //-------------------------------------
56 // External API
57 //-------------------------------------
59 bool BinariesCheckThread::check(SysinfoModel *sysinfo)
61 QMutexLocker lock(&m_binLock);
63 QEventLoop loop;
64 BinariesCheckThread thread(sysinfo);
66 QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
68 connect(&thread, SIGNAL(finished()), &loop, SLOT(quit()));
69 connect(&thread, SIGNAL(terminated()), &loop, SLOT(quit()));
71 thread.start();
72 QTimer::singleShot(30000, &loop, SLOT(quit()));
74 qDebug("Binaries checker thread has been created, please wait...");
75 loop.exec(QEventLoop::ExcludeUserInputEvents);
76 qDebug("Binaries checker thread finished.");
78 QApplication::restoreOverrideCursor();
80 if(!thread.wait(1000))
82 qWarning("Binaries checker thread encountered timeout -> probably deadlock!");
83 thread.terminate();
84 thread.wait();
85 return false;
88 if(thread.getException())
90 qWarning("Binaries checker thread encountered an exception !!!");
91 return false;
94 return thread.getSuccess();
97 //-------------------------------------
98 // Thread class
99 //-------------------------------------
101 BinariesCheckThread::BinariesCheckThread(const SysinfoModel *const sysinfo)
103 m_sysinfo(sysinfo)
105 m_success = m_exception = false;
108 BinariesCheckThread::~BinariesCheckThread(void)
112 void BinariesCheckThread::run(void)
114 m_success = m_exception = false;
115 checkBinaries1(m_success, m_sysinfo, &m_exception);
118 void BinariesCheckThread::checkBinaries1(volatile bool &success, const SysinfoModel *const sysinfo, volatile bool *exception)
120 __try
122 checkBinaries2(success, sysinfo, exception);
124 __except(1)
126 *exception = true;
127 qWarning("Unhandled exception error in binaries checker thread !!!");
131 void BinariesCheckThread::checkBinaries2(volatile bool &success, const SysinfoModel *const sysinfo, volatile bool *exception)
135 return checkBinaries3(success, sysinfo);
137 catch(...)
139 *exception = true;
140 qWarning("Binaries checker initializdation raised an C++ exception!");
144 void BinariesCheckThread::checkBinaries3(volatile bool &success, const SysinfoModel *const sysinfo)
146 success = true;
148 //Create list of all required binary files
149 typedef QPair<QString, bool> FileEntry;
150 QList<FileEntry> binFiles;
151 for(OptionsModel::EncType encdr = OptionsModel::EncType_MIN; encdr <= OptionsModel::EncType_MAX; NEXT(encdr))
153 const AbstractEncoderInfo &encInfo = EncoderFactory::getEncoderInfo(encdr);
154 const quint32 archCount = encInfo.getArchitectures().count();
155 QSet<QString> filesSet;
156 for (quint32 archIdx = 0; archIdx < archCount; ++archIdx)
158 const QStringList variants = encInfo.getVariants();
159 for (quint32 varntIdx = 0; varntIdx < quint32(variants.count()); ++varntIdx)
161 const QStringList dependencies = encInfo.getDependencies(sysinfo, archIdx, varntIdx);
162 for (QStringList::ConstIterator iter = dependencies.constBegin(); iter != dependencies.constEnd(); iter++)
164 if (!filesSet.contains(*iter))
166 filesSet << (*iter);
167 binFiles << qMakePair(*iter, true);
170 const QString binary = encInfo.getBinaryPath(sysinfo, archIdx, varntIdx);
171 if (!filesSet.contains(binary))
173 filesSet << binary;
174 binFiles << qMakePair(binary, false);
179 for(int i = 0; i < 2; i++)
181 binFiles << qMakePair(SourceFactory::getSourceInfo(SourceFactory::SourceType_AVS).getBinaryPath(sysinfo, bool(i)), false);
182 binFiles << qMakePair(AVS_CHECK_BINARY(sysinfo, bool(i)), false);
184 for(size_t i = 0; UpdaterDialog::BINARIES[i].name; i++)
186 if(UpdaterDialog::BINARIES[i].exec)
188 binFiles << qMakePair(QString("%1/toolset/common/%2").arg(sysinfo->getAppPath(), QString::fromLatin1(UpdaterDialog::BINARIES[i].name)), false);
192 //Actually validate the binaries
193 size_t currentFile = 0;
194 for(QList<FileEntry>::ConstIterator iter = binFiles.constBegin(); iter != binFiles.constEnd(); iter++)
196 QScopedPointer<QFile> file(new QFile(iter->first));
197 qDebug("%s", MUTILS_UTF8(file->fileName()));
199 if(file->open(QIODevice::ReadOnly))
201 if(!iter->second)
203 if (!MUtils::OS::is_executable_file(file->fileName()))
205 success = false;
206 qWarning("Required tool does NOT look like a valid Win32/Win64 binary:\n%s\n", MUTILS_UTF8(file->fileName()));
207 return;
210 else
212 if (!MUtils::OS::is_library_file(file->fileName()))
214 success = false;
215 qWarning("Required tool does NOT look like a valid Win32/Win64 library:\n%s\n", MUTILS_UTF8(file->fileName()));
216 return;
219 if(currentFile < MAX_BINARIES)
221 m_binPath[currentFile++].reset(file.take());
222 continue;
224 qFatal("Current binary file exceeds max. number of binaries!");
226 else
228 success = false;
229 qWarning("Required tool could not be found or access denied:\n%s\n", MUTILS_UTF8(file->fileName()));
230 return;