OSDN Git Service

Some code refactoring in the "startup" threads.
[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 #define BOOLIFY(X) (!!(X))
52
53 //External
54 QString AVS_CHECK_BINARY(const SysinfoModel *sysinfo, const bool& x64);
55
56 //-------------------------------------
57 // External API
58 //-------------------------------------
59
60 bool BinariesCheckThread::check(const SysinfoModel *const sysinfo, QString *const failedPath)
61 {
62         QMutexLocker lock(&m_binLock);
63
64         QEventLoop loop;
65         BinariesCheckThread thread(sysinfo);
66
67         QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
68
69         connect(&thread, SIGNAL(finished()),   &loop, SLOT(quit()));
70         connect(&thread, SIGNAL(terminated()), &loop, SLOT(quit()));
71         
72         thread.start();
73         QTimer::singleShot(30000, &loop, SLOT(quit()));
74         
75         qDebug("Binaries checker thread has been created, please wait...");
76         loop.exec(QEventLoop::ExcludeUserInputEvents);
77         qDebug("Binaries checker thread finished.");
78
79         QApplication::restoreOverrideCursor();
80
81         if(!thread.wait(5000))
82         {
83                 qWarning("Binaries checker thread encountered timeout -> probably deadlock!");
84                 thread.terminate();
85                 thread.wait();
86                 return false;
87         }
88
89         if(thread.getException())
90         {
91                 qWarning("Binaries checker thread encountered an exception !!!");
92                 return false;
93         }
94         
95         const bool success = BOOLIFY(thread.getSuccess());
96         if ((!success) && failedPath)
97         {
98                 *failedPath = thread.getFailedPath();
99         }
100
101         return success;
102 }
103
104 //-------------------------------------
105 // Thread class
106 //-------------------------------------
107
108 BinariesCheckThread::BinariesCheckThread(const SysinfoModel *const sysinfo)
109 :
110         m_sysinfo(sysinfo)
111 {
112         m_failedPath.clear();
113 }
114
115 BinariesCheckThread::~BinariesCheckThread(void)
116 {
117 }
118
119 void BinariesCheckThread::run(void)
120 {
121         m_failedPath.clear();
122         StarupThread::run();
123 }
124
125 int BinariesCheckThread::threadMain(void)
126 {
127         //Create list of all required binary files
128         typedef QPair<QString, bool> FileEntry;
129         QList<FileEntry> binFiles;
130         for(OptionsModel::EncType encdr = OptionsModel::EncType_MIN; encdr <= OptionsModel::EncType_MAX; NEXT(encdr))
131         {
132                 const AbstractEncoderInfo &encInfo = EncoderFactory::getEncoderInfo(encdr);
133                 const quint32 archCount = encInfo.getArchitectures().count();
134                 QSet<QString> filesSet;
135                 for (quint32 archIdx = 0; archIdx < archCount; ++archIdx)
136                 {
137                         const QStringList variants = encInfo.getVariants();
138                         for (quint32 varntIdx = 0; varntIdx < quint32(variants.count()); ++varntIdx)
139                         {
140                                 const QStringList dependencies = encInfo.getDependencies(m_sysinfo, archIdx, varntIdx);
141                                 for (QStringList::ConstIterator iter = dependencies.constBegin(); iter != dependencies.constEnd(); iter++)
142                                 {
143                                         if (!filesSet.contains(*iter))
144                                         {
145                                                 filesSet << (*iter);
146                                                 binFiles << qMakePair(*iter, true);
147                                         }
148                                 }
149                                 const QString binary = encInfo.getBinaryPath(m_sysinfo, archIdx, varntIdx);
150                                 if (!filesSet.contains(binary))
151                                 {
152                                         filesSet << binary;
153                                         binFiles << qMakePair(binary, false);
154                                 }
155                         }
156                 }
157         }
158         for(int i = 0; i < 2; i++)
159         {
160                 binFiles << qMakePair(SourceFactory::getSourceInfo(SourceFactory::SourceType_AVS).getBinaryPath(m_sysinfo, bool(i)), false);
161                 binFiles << qMakePair(AVS_CHECK_BINARY(m_sysinfo, bool(i)), false);
162         }
163         for(size_t i = 0; UpdaterDialog::BINARIES[i].name; i++)
164         {
165                 if(UpdaterDialog::BINARIES[i].exec)
166                 {
167                         binFiles << qMakePair(QString("%1/toolset/common/%2").arg(m_sysinfo->getAppPath(), QString::fromLatin1(UpdaterDialog::BINARIES[i].name)), false);
168                 }
169         }
170
171         //Actually validate the binaries
172         size_t currentFile = 0;
173         for(QList<FileEntry>::ConstIterator iter = binFiles.constBegin(); iter != binFiles.constEnd(); iter++)
174         {
175                 QScopedPointer<QFile> file(new QFile(iter->first));
176                 qDebug("%s", MUTILS_UTF8(file->fileName()));
177
178                 if(file->open(QIODevice::ReadOnly))
179                 {
180                         if(!iter->second)
181                         {
182                                 if (!MUtils::OS::is_executable_file(file->fileName()))
183                                 {
184                                         m_failedPath = file->fileName();
185                                         qWarning("Required tool does NOT look like a valid Win32/Win64 binary:\n%s\n", MUTILS_UTF8(file->fileName()));
186                                         return 0;
187                                 }
188                         }
189                         else
190                         {
191                                 if (!MUtils::OS::is_library_file(file->fileName()))
192                                 {
193                                         m_failedPath = file->fileName();
194                                         qWarning("Required tool does NOT look like a valid Win32/Win64 library:\n%s\n", MUTILS_UTF8(file->fileName()));
195                                         return 0;
196                                 }
197                         }
198                         if(currentFile < MAX_BINARIES)
199                         {
200                                 m_binPath[currentFile++].reset(file.take());
201                                 continue;
202                         }
203                         qFatal("Current binary file exceeds max. number of binaries!");
204                 }
205                 else
206                 {
207                         m_failedPath = file->fileName();
208                         qWarning("Required tool could not be found or access denied:\n%s\n", MUTILS_UTF8(file->fileName()));
209                         return 0;
210                 }
211         }
212
213         return 1;
214 }