OSDN Git Service

Improved installer error messages on unsupported platforms.
[lamexp/LameXP.git] / src / Thread_Initialization.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // LameXP - Audio Encoder Front-End
3 // Copyright (C) 2004-2011 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_Initialization.h"
23
24 #include "LockedFile.h"
25
26 #include <QFileInfo>
27 #include <QCoreApplication>
28 #include <QProcess>
29 #include <QMap>
30 #include <QDir>
31 #include <QLibrary>
32 #include <QResource>
33 #include <QTime>
34
35 ////////////////////////////////////////////////////////////
36 // TOOLS
37 ////////////////////////////////////////////////////////////
38
39 #define CPU_TYPE_X86 0x000001 //x86-32
40 #define CPU_TYPE_SSE 0x000002 //x86-32 + SSE2 (Intel only!)
41 #define CPU_TYPE_X64 0x000004 //x86-64
42
43 #define CPU_TYPE_GEN (CPU_TYPE_X86|CPU_TYPE_SSE)              //Use for all CPU's, except for x86-64
44 #define CPU_TYPE_ALL (CPU_TYPE_X86|CPU_TYPE_SSE|CPU_TYPE_X64) //Use for all CPU's, x86-32 and x86-64
45
46 static const struct
47 {
48         char *pcHash;
49         unsigned int uiCpuType;
50         char *pcName;
51         unsigned int uiVersion;
52 }
53 g_lamexp_tools[] =
54 {
55         {"3b41f85dde8d4a5a0f4cd5f461099d0db24610ba", CPU_TYPE_ALL, "alac.exe", UINT_MAX},
56         {"fb74ac8b73ad8cba2c3b4e6e61f23401d630dc22", CPU_TYPE_ALL, "elevator.exe", UINT_MAX},
57         {"3c647950bccfcc75d0746c0772e7115684be4dc5", CPU_TYPE_ALL, "faad.exe", 27},
58         {"d33cd86f04bd4067e244d2804466583c7b90a4e2", CPU_TYPE_ALL, "flac.exe", 121},
59         {"9328a50e89b54ec065637496d9681a7e3eebf915", CPU_TYPE_ALL, "gpgv.exe", 1411},
60         {"d837bf6ee4dab557d8b02d46c75a24e58980fffa", CPU_TYPE_ALL, "gpgv.gpg", UINT_MAX},
61         {"b8800842893e2900eea7cda5c2556661333bc5db", CPU_TYPE_ALL, "lame.exe", 39916},
62         {"a4e929cfaa42fa2e61a3d0c6434c77a06d45aef3", CPU_TYPE_ALL, "mac.exe", 406},
63         {"9ee3074dc3232c61c0ffd6ae6755f65974695c51", CPU_TYPE_GEN, "mediainfo.i386.exe", 743},
64         {"3691da33a4efd7625d1bd01cbf5ce6ffd8a12d19", CPU_TYPE_X64, "mediainfo.x64.exe", 743},
65         {"aa89763a5ba4d1a5986549b9ee53e005c51940c1", CPU_TYPE_ALL, "mpcdec.exe", 435},
66         {"38f81efca6c1eeab0b9dc39d06c2ac750267217f", CPU_TYPE_ALL, "mpg123.exe", 1132},
67         {"8dd7138714c3bcb39f5a3213413addba13d06f1e", CPU_TYPE_ALL, "oggdec.exe", UINT_MAX},
68         {"14a99d3b1f0b166dbd68db45196da871e58e14ec", CPU_TYPE_X86, "oggenc2.i386.exe", 287602},
69         {"36f8d93ef3df6a420a73a9b5cf02dafdaf4321f0", CPU_TYPE_SSE, "oggenc2.sse2.exe", 287602},
70         {"87ad1af73e9b9db3da3db645e5c2253cb0c2a2ea", CPU_TYPE_X64, "oggenc2.x64.exe", 287602},
71         {"0d9035bb62bdf46a2785261f8be5a4a0972abd15", CPU_TYPE_ALL, "shorten.exe", 361},
72         {"50ead3b852cbfc067a402e6c2d0d0d8879663dec", CPU_TYPE_ALL, "sox.exe", 1432},
73         {"8671e16497a2d217d3707d4aa418678d02b16bcc", CPU_TYPE_ALL, "speexdec.exe", 12},
74         {"093bfdec22872ca99e40183937c88785468be989", CPU_TYPE_ALL, "tta.exe", 21},
75         {"8c842eef65248b46fa6cb9a9e5714f575672d999", CPU_TYPE_ALL, "valdec.exe", 31},
76         {"62e2805d1b2eb2a4d86a5ca6e6ea58010d05d2a7", CPU_TYPE_ALL, "wget.exe", 1114},
77         {"a7e8aad52213e339ad985829722f35eab62be182", CPU_TYPE_ALL, "wupdate.exe", UINT_MAX},
78         {"b7d14b3540d24df13119a55d97623a61412de6e3", CPU_TYPE_ALL, "wvunpack.exe", 4601},
79         {NULL, NULL, NULL, NULL}
80 };
81
82 ////////////////////////////////////////////////////////////
83 // Constructor
84 ////////////////////////////////////////////////////////////
85
86 InitializationThread::InitializationThread(const lamexp_cpu_t *cpuFeatures)
87 {
88         m_bSuccess = false;
89         memset(&m_cpuFeatures, 0, sizeof(lamexp_cpu_t));
90         
91         if(cpuFeatures)
92         {
93                 memcpy(&m_cpuFeatures, cpuFeatures, sizeof(lamexp_cpu_t));
94         }
95 }
96
97 ////////////////////////////////////////////////////////////
98 // Thread Main
99 ////////////////////////////////////////////////////////////
100
101 void InitializationThread::run()
102 {
103         m_bSuccess = false;
104         delay();
105
106         //CPU type selection
107         unsigned int cpuSupport = m_cpuFeatures.x64 ? CPU_TYPE_X64 : ((m_cpuFeatures.intel && m_cpuFeatures.sse && m_cpuFeatures.sse2) ? CPU_TYPE_SSE : CPU_TYPE_X86);
108         
109         //Allocate maps
110         QMap<QString, QString> mapChecksum;
111         QMap<QString, unsigned int> mapVersion;
112         QMap<QString, unsigned int> mapCpuType;
113
114         //Init properties
115         for(int i = 0; i < INT_MAX; i++)
116         {
117                 if(!g_lamexp_tools[i].pcName && !g_lamexp_tools[i].pcHash && !g_lamexp_tools[i].uiVersion)
118                 {
119                         break;
120                 }
121                 else if(g_lamexp_tools[i].pcName && g_lamexp_tools[i].pcHash && g_lamexp_tools[i].uiVersion)
122                 {
123                         const QString currentTool = QString::fromLatin1(g_lamexp_tools[i].pcName);
124                         mapChecksum.insert(currentTool, QString::fromLatin1(g_lamexp_tools[i].pcHash));
125                         mapCpuType.insert(currentTool, g_lamexp_tools[i].uiCpuType);
126                         mapVersion.insert(currentTool, g_lamexp_tools[i].uiVersion);
127                 }
128                 else
129                 {
130                         qFatal("Inconsistent checksum data detected. Take care!");
131                 }
132         }
133
134         QDir toolsDir(":/tools/");
135         QList<QFileInfo> toolsList = toolsDir.entryInfoList(QStringList("*.*"), QDir::Files, QDir::Name);
136         
137         QTime timer;
138         timer.start();
139
140         //Extract all files
141         while(!toolsList.isEmpty())
142         {
143                 try
144                 {
145                         QFileInfo currentTool = toolsList.takeFirst();
146                         QString toolName = currentTool.fileName().toLower();
147                         QString toolShortName = QString("%1.%2").arg(currentTool.baseName().toLower(), currentTool.suffix().toLower());
148                         
149                         QByteArray toolHash = mapChecksum.take(toolName).toLatin1();
150                         unsigned int toolCpuType = mapCpuType.take(toolName);
151                         unsigned int toolVersion = mapVersion.take(toolName);
152                         
153                         if(toolHash.size() != 40)
154                         {
155                                 throw "The required checksum is missing, take care!";
156                         }
157                         
158                         if(toolCpuType & cpuSupport)
159                         {
160                                 qDebug("Extracting file: %s -> %s", toolName.toLatin1().constData(),  toolShortName.toLatin1().constData());
161                                 LockedFile *lockedFile = new LockedFile(QString(":/tools/%1").arg(toolName), QString("%1/tool_%2").arg(lamexp_temp_folder2(), toolShortName), toolHash);
162                                 lamexp_register_tool(toolShortName, lockedFile, toolVersion);
163                         }
164                 }
165                 catch(char *errorMsg)
166                 {
167                         qFatal("At least one of the required tools could not be extracted:\n%s", errorMsg);
168                         return;
169                 }
170         }
171         
172         //Make sure all files were extracted
173         if(!mapChecksum.isEmpty())
174         {
175                 qFatal("At least one required tool could not be found:\n%s", toolsDir.filePath(mapChecksum.keys().first()).toLatin1().constData());
176                 return;
177         }
178         
179         //Clean-up
180         mapChecksum.clear();
181         mapVersion.clear();
182         mapCpuType.clear();
183
184         qDebug("All extracted.\n");
185
186         //Check delay
187         double delayExtract = static_cast<double>(timer.elapsed()) / 1000.0;
188         if(delayExtract > 8.0)
189         {
190                 qWarning("Extracting tools took %.3f seconds -> probably slow realtime virus scanner.", delayExtract);
191                 qWarning("Please report performance problems to your anti-virus developer !!!\n");
192         }
193
194         //Register all translations
195         initTranslations();
196
197         //Look for Nero encoder
198         initNeroAac();
199         
200         //Look for WMA File decoder
201         initWmaDec();
202
203         delay();
204         m_bSuccess = true;
205 }
206
207 ////////////////////////////////////////////////////////////
208 // PUBLIC FUNCTIONS
209 ////////////////////////////////////////////////////////////
210
211 void InitializationThread::delay(void)
212 {
213         const char *temp = "|/-\\";
214         printf("Thread is doing something important... ?\b", temp[4]);
215
216         for(int i = 0; i < 20; i++)
217         {
218                 printf("%c\b", temp[i%4]);
219                 msleep(25);
220         }
221
222         printf("Done\n\n");
223 }
224
225 void InitializationThread::initTranslations(void)
226 {
227         //Search for language files
228         QStringList qmFiles = QDir(":/localization").entryList(QStringList() << "LameXP_??.qm", QDir::Files, QDir::Name);
229
230         //Make sure we found at least one translation
231         if(qmFiles.count() < 1)
232         {
233                 qFatal("Could not find any translation files!");
234                 return;
235         }
236
237         //Add all available translations
238         while(!qmFiles.isEmpty())
239         {
240                 QString langId, langName;
241                 unsigned int systemId = 0;
242                 QString qmFile = qmFiles.takeFirst();
243                 
244                 QRegExp langIdExp("LameXP_(\\w\\w)\\.qm", Qt::CaseInsensitive);
245                 if(langIdExp.indexIn(qmFile) >= 0)
246                 {
247                         langId = langIdExp.cap(1).toLower();
248                 }
249
250                 QResource langRes = (QString(":/localization/%1.txt").arg(qmFile));
251                 if(langRes.isValid() && langRes.size() > 0)
252                 {
253                         QStringList langInfo = QString::fromUtf8(reinterpret_cast<const char*>(langRes.data()), langRes.size()).simplified().split(",", QString::SkipEmptyParts);
254                         if(langInfo.count() == 2)
255                         {
256                                 systemId = langInfo.at(0).toUInt();
257                                 langName = langInfo.at(1);
258                         }
259                 }
260                 
261                 if(lamexp_translation_register(langId, qmFile, langName, systemId))
262                 {
263                         qDebug64("Registering translation: %1 = %2 (%3)", qmFile, langName, QString::number(systemId));
264                 }
265                 else
266                 {
267                         qWarning("Failed to register: %s", qmFile.toLatin1().constData());
268                 }
269         }
270
271         qDebug("All registered.\n");
272 }
273
274 void InitializationThread::initNeroAac(void)
275 {
276         QFileInfo neroFileInfo[3];
277         neroFileInfo[0] = QFileInfo(QString("%1/neroAacEnc.exe").arg(QCoreApplication::applicationDirPath()));
278         neroFileInfo[1] = QFileInfo(QString("%1/neroAacDec.exe").arg(QCoreApplication::applicationDirPath()));
279         neroFileInfo[2] = QFileInfo(QString("%1/neroAacTag.exe").arg(QCoreApplication::applicationDirPath()));
280         
281         bool neroFilesFound = true;
282         for(int i = 0; i < 3; i++)      { if(!neroFileInfo[i].exists()) neroFilesFound = false; }
283
284         //Lock the Nero binaries
285         if(!neroFilesFound)
286         {
287                 qDebug("Nero encoder binaries not found -> AAC encoding support will be disabled!\n");
288                 return;
289         }
290
291         qDebug("Found Nero AAC encoder binary:\n%s\n", neroFileInfo[0].canonicalFilePath().toUtf8().constData());
292
293         LockedFile *neroBin[3];
294         for(int i = 0; i < 3; i++) neroBin[i] = NULL;
295
296         try
297         {
298                 for(int i = 0; i < 3; i++)
299                 {
300                         neroBin[i] = new LockedFile(neroFileInfo[i].canonicalFilePath());
301                 }
302         }
303         catch(...)
304         {
305                 for(int i = 0; i < 3; i++) LAMEXP_DELETE(neroBin[i]);
306                 qWarning("Failed to get excluive lock to Nero encoder binary -> AAC encoding support will be disabled!");
307                 return;
308         }
309
310         QProcess process;
311         process.setProcessChannelMode(QProcess::MergedChannels);
312         process.setReadChannel(QProcess::StandardOutput);
313         process.start(neroFileInfo[0].canonicalFilePath(), QStringList() << "-help");
314
315         if(!process.waitForStarted())
316         {
317                 qWarning("Nero process failed to create!");
318                 qWarning("Error message: \"%s\"\n", process.errorString().toLatin1().constData());
319                 process.kill();
320                 process.waitForFinished(-1);
321                 for(int i = 0; i < 3; i++) LAMEXP_DELETE(neroBin[i]);
322                 return;
323         }
324
325         unsigned int neroVersion = 0;
326
327         while(process.state() != QProcess::NotRunning)
328         {
329                 if(!process.waitForReadyRead())
330                 {
331                         if(process.state() == QProcess::Running)
332                         {
333                                 qWarning("Nero process time out -> killing!");
334                                 process.kill();
335                                 process.waitForFinished(-1);
336                                 for(int i = 0; i < 3; i++) LAMEXP_DELETE(neroBin[i]);
337                                 return;
338                         }
339                 }
340
341                 while(process.canReadLine())
342                 {
343                         QString line = QString::fromUtf8(process.readLine().constData()).simplified();
344                         QStringList tokens = line.split(" ", QString::SkipEmptyParts, Qt::CaseInsensitive);
345                         int index1 = tokens.indexOf("Package");
346                         int index2 = tokens.indexOf("version:");
347                         if(index1 >= 0 && index2 >= 0 && index1 + 1 == index2 && index2 < tokens.count() - 1)
348                         {
349                                 QStringList versionTokens = tokens.at(index2 + 1).split(".", QString::SkipEmptyParts, Qt::CaseInsensitive);
350                                 if(versionTokens.count() == 4)
351                                 {
352                                         neroVersion = 0;
353                                         neroVersion += min(9, max(0, versionTokens.at(3).toInt()));
354                                         neroVersion += min(9, max(0, versionTokens.at(2).toInt())) * 10;
355                                         neroVersion += min(9, max(0, versionTokens.at(1).toInt())) * 100;
356                                         neroVersion += min(9, max(0, versionTokens.at(0).toInt())) * 1000;
357                                 }
358                         }
359                 }
360         }
361
362         if(!(neroVersion > 0))
363         {
364                 qWarning("Nero AAC version could not be determined -> AAC encoding support will be disabled!");
365                 for(int i = 0; i < 3; i++) LAMEXP_DELETE(neroBin[i]);
366                 return;
367         }
368         
369         for(int i = 0; i < 3; i++)
370         {
371                 lamexp_register_tool(neroFileInfo[i].fileName(), neroBin[i], neroVersion);
372         }
373 }
374
375
376 void InitializationThread::initWmaDec(void)
377 {
378         static const char* wmaDecoderComponentPath = "NCH Software/Components/wmawav/wmawav.exe";
379
380         LockedFile *wmaFileBin = NULL;
381         QFileInfo wmaFileInfo = QFileInfo(QString("%1/%2").arg(lamexp_known_folder(lamexp_folder_programfiles), wmaDecoderComponentPath));
382
383         if(!wmaFileInfo.exists())
384         {
385                 qDebug("WMA File Decoder not found -> WMA decoding support will be disabled!\n");
386                 return;
387         }
388
389         try
390         {
391                 wmaFileBin = new LockedFile(wmaFileInfo.canonicalFilePath());
392         }
393         catch(...)
394         {
395                 qWarning("Failed to get excluive lock to WMA File Decoder binary -> WMA decoding support will be disabled!");
396                 return;
397         }
398
399         QProcess process;
400         process.setProcessChannelMode(QProcess::MergedChannels);
401         process.setReadChannel(QProcess::StandardOutput);
402         process.start(wmaFileInfo.canonicalFilePath(), QStringList());
403
404         if(!process.waitForStarted())
405         {
406                 qWarning("WmaWav process failed to create!");
407                 qWarning("Error message: \"%s\"\n", process.errorString().toLatin1().constData());
408                 process.kill();
409                 process.waitForFinished(-1);
410                 return;
411         }
412
413         bool b_wmaWavFound = false;
414
415         while(process.state() != QProcess::NotRunning)
416         {
417                 if(!process.waitForReadyRead())
418                 {
419                         if(process.state() == QProcess::Running)
420                         {
421                                 qWarning("WmaWav process time out -> killing!");
422                                 process.kill();
423                                 process.waitForFinished(-1);
424                                 return;
425                         }
426                 }
427                 while(process.canReadLine())
428                 {
429                         QString line = QString::fromUtf8(process.readLine().constData()).simplified();
430                         if(line.contains("Usage: wmatowav.exe WMAFileSpec WAVFileSpec", Qt::CaseInsensitive))
431                         {
432                                 b_wmaWavFound = true;
433                         }
434                 }
435         }
436
437         if(!b_wmaWavFound)
438         {
439                 qWarning("WmaWav could not be identified -> WMA decoding support will be disabled!\n");
440                 LAMEXP_DELETE(wmaFileBin);
441                 return;
442         }
443
444         qDebug("Found WMA File Decoder binary:\n%s\n", wmaFileInfo.canonicalFilePath().toUtf8().constData());
445
446         if(wmaFileBin)
447         {
448                 lamexp_register_tool(wmaFileInfo.fileName(), wmaFileBin);
449         }
450 }
451
452 ////////////////////////////////////////////////////////////
453 // EVENTS
454 ////////////////////////////////////////////////////////////
455
456 /*NONE*/