OSDN Git Service

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