OSDN Git Service

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