Adapt for latest MUtils changes.
[LameXP.git] / src / Thread_Initialization.cpp
blobf29a4cc53b31f312c87101f41856fea455e07793
1 ///////////////////////////////////////////////////////////////////////////////
2 // LameXP - Audio Encoder Front-End
3 // Copyright (C) 2004-2016 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, but always including the *additional*
9 // restrictions defined in the "License.txt" file.
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License along
17 // with this program; if not, write to the Free Software Foundation, Inc.,
18 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 // http://www.gnu.org/licenses/gpl-2.0.txt
21 ///////////////////////////////////////////////////////////////////////////////
23 #include "Thread_Initialization.h"
25 //Internal
26 #define LAMEXP_INC_TOOLS 1
27 #include "Tools.h"
28 #include "LockedFile.h"
29 #include "FileHash.h"
30 #include "Tool_Abstract.h"
32 //MUtils
33 #include <MUtils/Global.h>
34 #include <MUtils/OSSupport.h>
35 #include <MUtils/Translation.h>
36 #include <MUtils/Exception.h>
38 //Qt
39 #include <QFileInfo>
40 #include <QCoreApplication>
41 #include <QProcess>
42 #include <QMap>
43 #include <QDir>
44 #include <QResource>
45 #include <QTextStream>
46 #include <QRunnable>
47 #include <QThreadPool>
48 #include <QMutex>
49 #include <QQueue>
50 #include <QElapsedTimer>
51 #include <QVector>
53 /* enable custom tools? */
54 static const bool ENABLE_CUSTOM_TOOLS = true;
56 /* helper macros */
57 #define PRINT_CPU_TYPE(X) case X: qDebug("Selected CPU is: " #X)
58 #define MAKE_REGEXP(STR) (((STR) && ((STR)[0])) ? QRegExp((STR)) : QRegExp())
60 /* constants */
61 static const double g_allowedExtractDelay = 12.0;
62 static const size_t BUFF_SIZE = 512;
63 static const size_t EXPECTED_TOOL_COUNT = 29;
65 /* number of CPU cores -> number of threads */
66 static unsigned int cores2threads(const unsigned int cores)
68 static const size_t LUT_LEN = 4;
70 static const struct
72 const unsigned int upperBound;
73 const double coeffs[4];
75 LUT[LUT_LEN] =
77 { 4, { -0.052695810565, 0.158087431694, 4.982841530055, -1.088233151184 } },
78 { 8, { 0.042431693989, -0.983442622951, 9.548961748634, -7.176393442623 } },
79 { 12, { -0.006277322404, 0.185573770492, 0.196830601093, 17.762622950820 } },
80 { 32, { 0.000673497268, -0.064655737705, 3.199584699454, 5.751606557377 } }
83 size_t index = 0;
84 while((cores > LUT[index].upperBound) && (index < (LUT_LEN-1))) index++;
86 const double x = qBound(1.0, double(cores), double(LUT[LUT_LEN-1].upperBound));
87 const double y = (LUT[index].coeffs[0] * pow(x, 3.0)) + (LUT[index].coeffs[1] * pow(x, 2.0)) + (LUT[index].coeffs[2] * x) + LUT[index].coeffs[3];
89 return qRound(abs(y));
92 ////////////////////////////////////////////////////////////
93 // BaseTask class
94 ////////////////////////////////////////////////////////////
96 class BaseTask : public QRunnable
98 public:
99 BaseTask(void)
103 ~BaseTask(void)
107 static void clearFlags(QMutexLocker &lock = QMutexLocker(&s_mutex))
109 s_bExcept = false;
110 s_errMsg[0] = char(0);
113 static bool getExcept(void)
115 bool ret;
116 QMutexLocker lock(&s_mutex);
117 ret = s_bExcept;
118 return ret;
121 static bool getErrMsg(char *buffer, const size_t buffSize)
123 QMutexLocker lock(&s_mutex);
124 if(s_errMsg[0])
126 strncpy_s(buffer, BUFF_SIZE, s_errMsg, _TRUNCATE);
127 return true;
129 return false;
132 protected:
133 virtual void taskMain(void) = 0;
135 void run(void)
139 if(!getExcept()) taskMain();
141 catch(const std::exception &e)
143 QMutexLocker lock(&s_mutex);
144 if(!s_bExcept)
146 s_bExcept = true;
147 strncpy_s(s_errMsg, BUFF_SIZE, e.what(), _TRUNCATE);
149 lock.unlock();
150 qWarning("OptionalInitTask exception error:\n%s\n\n", e.what());
152 catch(...)
154 QMutexLocker lock(&s_mutex);
155 if(!s_bExcept)
157 s_bExcept = true;
158 strncpy_s(s_errMsg, BUFF_SIZE, "Unknown exception error!", _TRUNCATE);
160 lock.unlock();
161 qWarning("OptionalInitTask encountered an unknown exception!");
165 static volatile bool s_bExcept;
166 static QMutex s_mutex;
167 static char s_errMsg[BUFF_SIZE];
170 QMutex BaseTask::s_mutex;
171 char BaseTask::s_errMsg[BUFF_SIZE] = {'\0'};
172 volatile bool BaseTask::s_bExcept = false;
174 ////////////////////////////////////////////////////////////
175 // ExtractorTask class
176 ////////////////////////////////////////////////////////////
178 class ExtractorTask : public BaseTask
180 public:
181 ExtractorTask(QResource *const toolResource, const QDir &appDir, const QString &toolName, const QByteArray &toolHash, const unsigned int toolVersion, const QString &toolTag)
183 m_appDir(appDir),
184 m_tempPath(MUtils::temp_folder()),
185 m_toolName(toolName),
186 m_toolHash(toolHash),
187 m_toolVersion(toolVersion),
188 m_toolTag(toolTag),
189 m_toolResource(toolResource)
191 /* Nothing to do */
194 ~ExtractorTask(void)
198 static bool getCustom(void)
200 bool ret;
201 QMutexLocker lock(&s_mutex);
202 ret = s_bCustom;
203 return ret;
206 static void clearFlags(void)
208 QMutexLocker lock(&s_mutex);
209 s_bCustom = false;
210 BaseTask::clearFlags(lock);
213 protected:
214 void taskMain(void)
216 QScopedPointer<LockedFile> lockedFile;
217 unsigned int version = m_toolVersion;
219 const QFileInfo toolFileInfo(m_toolName);
220 const QString toolShrtName = QString("%1.%2").arg(toolFileInfo.baseName().toLower(), toolFileInfo.suffix().toLower());
222 //Try to load a "custom" tool first
223 if(ENABLE_CUSTOM_TOOLS)
225 const QFileInfo customTool(QString("%1/tools/%2/%3").arg(m_appDir.canonicalPath(), QString::number(lamexp_version_build()), toolShrtName));
226 if(customTool.exists() && customTool.isFile())
228 qDebug("Setting up file: %s <- %s", toolShrtName.toLatin1().constData(), m_appDir.relativeFilePath(customTool.canonicalFilePath()).toLatin1().constData());
231 lockedFile.reset(new LockedFile(customTool.canonicalFilePath()));
232 version = UINT_MAX; s_bCustom = true;
234 catch(std::runtime_error&)
236 lockedFile.reset();
241 //Try to load the tool from the "cache" next
242 if(lockedFile.isNull())
244 const QFileInfo chachedTool(QString("%1/cache/%2").arg(m_appDir.canonicalPath(), toolFileInfo.fileName()));
245 if(chachedTool.exists() && chachedTool.isFile())
247 qDebug("Validating file: %s <- %s", toolShrtName.toLatin1().constData(), m_appDir.relativeFilePath(chachedTool.canonicalFilePath()).toLatin1().constData());
250 lockedFile.reset(new LockedFile(chachedTool.canonicalFilePath(), m_toolHash));
252 catch(std::runtime_error&)
254 lockedFile.reset();
259 //If still not initialized, extract tool now!
260 if(lockedFile.isNull())
262 qDebug("Extracting file: %s -> %s", m_toolName.toLatin1().constData(), toolShrtName.toLatin1().constData());
263 lockedFile.reset(new LockedFile(m_toolResource.data(), QString("%1/lxp_%2").arg(m_tempPath, toolShrtName), m_toolHash));
266 //Register tool
267 lamexp_tools_register(toolShrtName, lockedFile.take(), version, m_toolTag);
270 private:
271 static volatile bool s_bCustom;
272 QScopedPointer<QResource> m_toolResource;
273 const QDir m_appDir;
274 const QString m_tempPath;
275 const QString m_toolName;
276 const QByteArray m_toolHash;
277 const unsigned int m_toolVersion;
278 const QString m_toolTag;
281 volatile bool ExtractorTask::s_bCustom = false;
283 ////////////////////////////////////////////////////////////
284 // InitAacEncTask class
285 ////////////////////////////////////////////////////////////
287 class InitAacEncTask : public BaseTask
289 public:
290 InitAacEncTask(const aac_encoder_t *const encoder_info)
292 m_encoder_info(encoder_info)
296 ~InitAacEncTask(void)
300 protected:
301 void taskMain(void)
303 initAacEncImpl(m_encoder_info->toolName, m_encoder_info->fileNames, m_encoder_info->checkArgs ? (QStringList() << QString::fromLatin1(m_encoder_info->checkArgs)) : QStringList(), m_encoder_info->toolMinVersion, m_encoder_info->verDigits, m_encoder_info->verShift, m_encoder_info->verStr, MAKE_REGEXP(m_encoder_info->regExpVer), MAKE_REGEXP(m_encoder_info->regExpSig));
306 static void initAacEncImpl(const char *const toolName, const char *const fileNames[], const QStringList &checkArgs, const quint32 &toolMinVersion, const quint32 &verDigits, const quint32 &verShift, const char *const verStr, QRegExp &regExpVer, QRegExp &regExpSig = QRegExp());
308 private:
309 const aac_encoder_t *const m_encoder_info;
312 ////////////////////////////////////////////////////////////
313 // Constructor
314 ////////////////////////////////////////////////////////////
316 InitializationThread::InitializationThread(const MUtils::CPUFetaures::cpu_info_t &cpuFeatures)
318 m_bSuccess(false),
319 m_slowIndicator(false)
322 memcpy(&m_cpuFeatures, &cpuFeatures, sizeof(MUtils::CPUFetaures::cpu_info_t));
325 ////////////////////////////////////////////////////////////
326 // Thread Main
327 ////////////////////////////////////////////////////////////
329 void InitializationThread::run(void)
333 doInit();
335 catch(const std::exception &error)
337 MUTILS_PRINT_ERROR("\nGURU MEDITATION !!!\n\nException error:\n%s\n", error.what());
338 MUtils::OS::fatal_exit(L"Unhandeled C++ exception error, application will exit!");
340 catch(...)
342 MUTILS_PRINT_ERROR("\nGURU MEDITATION !!!\n\nUnknown exception error!\n");
343 MUtils::OS::fatal_exit(L"Unhandeled C++ exception error, application will exit!");
347 double InitializationThread::doInit(const size_t threadCount)
349 m_bSuccess = false;
350 delay();
352 //CPU type selection
353 unsigned int cpuSupport = 0;
354 const bool haveSSE2 = (m_cpuFeatures.features & MUtils::CPUFetaures::FLAG_SSE) && (m_cpuFeatures.features & MUtils::CPUFetaures::FLAG_SSE2);
355 if(haveSSE2 && (m_cpuFeatures.vendor & MUtils::CPUFetaures::VENDOR_INTEL))
357 if (m_cpuFeatures.features & MUtils::CPUFetaures::FLAG_AVX)
359 cpuSupport = m_cpuFeatures.x64 ? CPU_TYPE_X64_AVX : CPU_TYPE_X86_AVX;
361 else
363 cpuSupport = m_cpuFeatures.x64 ? CPU_TYPE_X64_SSE : CPU_TYPE_X86_SSE;
366 else
368 cpuSupport = m_cpuFeatures.x64 ? CPU_TYPE_X64_GEN : CPU_TYPE_X86_GEN;
371 //Hack to disable x64 on Wine, as x64 binaries won't run under Wine (tested with Wine 1.4 under Ubuntu 12.04 x64)
372 if(cpuSupport & CPU_TYPE_X64_ALL)
374 if(MUtils::OS::running_on_wine())
376 qWarning("Running under Wine on a 64-Bit system. Going to disable all x64 support!\n");
377 cpuSupport = (cpuSupport == CPU_TYPE_X64_SSE) ? CPU_TYPE_X86_SSE : CPU_TYPE_X86_GEN;
381 //Print selected CPU type
382 switch(cpuSupport)
384 PRINT_CPU_TYPE(CPU_TYPE_X86_GEN); break;
385 PRINT_CPU_TYPE(CPU_TYPE_X86_SSE); break;
386 PRINT_CPU_TYPE(CPU_TYPE_X86_AVX); break;
387 PRINT_CPU_TYPE(CPU_TYPE_X64_GEN); break;
388 PRINT_CPU_TYPE(CPU_TYPE_X64_SSE); break;
389 PRINT_CPU_TYPE(CPU_TYPE_X64_AVX); break;
390 default: MUTILS_THROW("CPU support undefined!");
393 //Allocate queues
394 QQueue<QString> queueToolName;
395 QQueue<QString> queueChecksum;
396 QQueue<QString> queueVersInfo;
397 QQueue<unsigned int> queueVersions;
398 QQueue<unsigned int> queueCpuTypes;
400 //Init properties
401 for(int i = 0; true; i++)
403 if(!(g_lamexp_tools[i].pcName || g_lamexp_tools[i].pcHash || g_lamexp_tools[i].uiVersion))
405 break;
407 else if(g_lamexp_tools[i].pcName && g_lamexp_tools[i].pcHash && g_lamexp_tools[i].uiVersion)
409 queueToolName.enqueue(QString::fromLatin1(g_lamexp_tools[i].pcName));
410 queueChecksum.enqueue(QString::fromLatin1(g_lamexp_tools[i].pcHash));
411 queueVersInfo.enqueue(QString::fromLatin1(g_lamexp_tools[i].pcVersTag));
412 queueCpuTypes.enqueue(g_lamexp_tools[i].uiCpuType);
413 queueVersions.enqueue(g_lamexp_tools[i].uiVersion);
415 else
417 qFatal("Inconsistent checksum data detected. Take care!");
421 QDir appDir = QDir(QCoreApplication::applicationDirPath()).canonicalPath();
423 QScopedPointer<QThreadPool> pool(new QThreadPool());
424 pool->setMaxThreadCount((threadCount > 0) ? threadCount : qBound(2U, cores2threads(m_cpuFeatures.count), EXPECTED_TOOL_COUNT));
425 ExtractorTask::clearFlags();
427 //Start the timer
428 QElapsedTimer timeExtractStart;
429 timeExtractStart.start();
431 //Extract all files
432 while(!(queueToolName.isEmpty() || queueChecksum.isEmpty() || queueVersInfo.isEmpty() || queueCpuTypes.isEmpty() || queueVersions.isEmpty()))
434 const QString toolName = queueToolName.dequeue();
435 const QString checksum = queueChecksum.dequeue();
436 const QString versInfo = queueVersInfo.dequeue();
437 const unsigned int cpuType = queueCpuTypes.dequeue();
438 const unsigned int version = queueVersions.dequeue();
440 const QByteArray toolHash(checksum.toLatin1());
441 if(toolHash.size() != 96)
443 qFatal("The checksum for \"%s\" has an invalid size!", MUTILS_UTF8(toolName));
444 return -1.0;
447 QScopedPointer<QResource> resource(new QResource(QString(":/tools/%1").arg(toolName)));
448 if(!(resource->isValid() && resource->data()))
450 qFatal("The resource for \"%s\" could not be found!", MUTILS_UTF8(toolName));
451 return -1.0;
454 if(cpuType & cpuSupport)
456 pool->start(new ExtractorTask(resource.take(), appDir, toolName, toolHash, version, versInfo));
457 continue;
461 //Sanity Check
462 if(!(queueToolName.isEmpty() && queueChecksum.isEmpty() && queueVersInfo.isEmpty() && queueCpuTypes.isEmpty() && queueVersions.isEmpty()))
464 qFatal("Checksum queues *not* empty fater verification completed. Take care!");
467 //Wait for extrator threads to finish
468 pool->waitForDone();
470 //Performance measure
471 const double delayExtract = double(timeExtractStart.elapsed()) / 1000.0;
472 timeExtractStart.invalidate();
474 //Make sure all files were extracted correctly
475 if(ExtractorTask::getExcept())
477 char errorMsg[BUFF_SIZE];
478 if(ExtractorTask::getErrMsg(errorMsg, BUFF_SIZE))
480 qFatal("At least one of the required tools could not be initialized:\n%s", errorMsg);
481 return -1.0;
483 qFatal("At least one of the required tools could not be initialized!");
484 return -1.0;
487 qDebug("All extracted.\n");
489 //Using any custom tools?
490 if(ExtractorTask::getCustom())
492 qWarning("Warning: Using custom tools, you might encounter unexpected problems!\n");
495 //Check delay
496 if(delayExtract > g_allowedExtractDelay)
498 m_slowIndicator = true;
499 qWarning("Extracting tools took %.3f seconds -> probably slow realtime virus scanner.", delayExtract);
500 qWarning("Please report performance problems to your anti-virus developer !!!\n");
502 else
504 qDebug("Extracting the tools took %.3f seconds (OK).\n", delayExtract);
507 //Register all translations
508 initTranslations();
510 //Look for AAC encoders
511 InitAacEncTask::clearFlags();
512 for(size_t i = 0; g_lamexp_aacenc[i].toolName; i++)
514 pool->start(new InitAacEncTask(&(g_lamexp_aacenc[i])));
516 pool->waitForDone();
518 //Make sure initialization finished correctly
519 if(InitAacEncTask::getExcept())
521 char errorMsg[BUFF_SIZE];
522 if(InitAacEncTask::getErrMsg(errorMsg, BUFF_SIZE))
524 qFatal("At least one optional component failed to initialize:\n%s", errorMsg);
525 return -1.0;
527 qFatal("At least one optional component failed to initialize!");
528 return -1.0;
531 m_bSuccess = true;
532 delay();
534 return delayExtract;
537 ////////////////////////////////////////////////////////////
538 // INTERNAL FUNCTIONS
539 ////////////////////////////////////////////////////////////
541 void InitializationThread::delay(void)
543 MUtils::OS::sleep_ms(333);
546 ////////////////////////////////////////////////////////////
547 // Translation Support
548 ////////////////////////////////////////////////////////////
550 void InitializationThread::initTranslations(void)
552 //Search for language files
553 const QDir qmDirectory(":/localization");
554 const QStringList qmFiles = qmDirectory.entryList(QStringList() << "LameXP_??.qm", QDir::Files, QDir::Name);
556 //Make sure we found at least one translation
557 if(qmFiles.count() < 1)
559 qFatal("Could not find any translation files!");
560 return;
563 //Initialize variables
564 const QString langResTemplate(":/localization/%1.txt");
565 QRegExp langIdExp("^LameXP_(\\w\\w)\\.qm$", Qt::CaseInsensitive);
567 //Add all available translations
568 for(QStringList::ConstIterator iter = qmFiles.constBegin(); iter != qmFiles.constEnd(); iter++)
570 const QString langFile = qmDirectory.absoluteFilePath(*iter);
571 QString langId, langName;
572 unsigned int systemId = 0, country = 0;
574 if(QFileInfo(langFile).isFile() && (langIdExp.indexIn(*iter) >= 0))
576 langId = langIdExp.cap(1).toLower();
577 QScopedPointer<QResource> langRes(new QResource(langResTemplate.arg(*iter)));
578 if(langRes->isValid() && langRes->size() > 0)
580 QByteArray data = QByteArray::fromRawData(reinterpret_cast<const char*>(langRes->data()), langRes->size());
581 QTextStream stream(&data, QIODevice::ReadOnly);
582 stream.setAutoDetectUnicode(false); stream.setCodec("UTF-8");
584 while(!(stream.atEnd() || (stream.status() != QTextStream::Ok)))
586 QStringList langInfo = stream.readLine().simplified().split(",", QString::SkipEmptyParts);
587 if(langInfo.count() >= 3)
589 systemId = langInfo.at(0).trimmed().toUInt();
590 country = langInfo.at(1).trimmed().toUInt();
591 langName = langInfo.at(2).trimmed();
592 break;
598 if(!(langId.isEmpty() || langName.isEmpty() || (systemId == 0)))
600 if(MUtils::Translation::insert(langId, langFile, langName, systemId, country))
602 qDebug("Registering translation: %s = %s (%u) [%u]", MUTILS_UTF8(*iter), MUTILS_UTF8(langName), systemId, country);
604 else
606 qWarning("Failed to register: %s", langFile.toLatin1().constData());
611 qDebug("All registered.\n");
614 ////////////////////////////////////////////////////////////
615 // AAC Encoder Detection
616 ////////////////////////////////////////////////////////////
618 void InitAacEncTask::initAacEncImpl(const char *const toolName, const char *const fileNames[], const QStringList &checkArgs, const quint32 &toolMinVersion, const quint32 &verDigits, const quint32 &verShift, const char *const verStr, QRegExp &regExpVer, QRegExp &regExpSig)
620 static const size_t MAX_FILES = 8;
621 const QString appPath = QDir(QCoreApplication::applicationDirPath()).canonicalPath();
623 QFileInfoList fileInfo;
624 for(size_t i = 0; fileNames[i] && (fileInfo.count() < MAX_FILES); i++)
626 fileInfo.append(QFileInfo(QString("%1/%2").arg(appPath, QString::fromLatin1(fileNames[i]))));
629 for(QFileInfoList::ConstIterator iter = fileInfo.constBegin(); iter != fileInfo.constEnd(); iter++)
631 if(!(iter->exists() && iter->isFile()))
633 qDebug("%s encoder binaries not found -> Encoding support will be disabled!\n", toolName);
634 return;
636 if((iter->suffix().compare("exe", Qt::CaseInsensitive) == 0) && (!MUtils::OS::is_executable_file(iter->canonicalFilePath())))
638 qDebug("%s executable is invalid -> %s support will be disabled!\n", MUTILS_UTF8(iter->fileName()), toolName);
639 return;
643 qDebug("Found %s encoder binary:\n%s\n", toolName, MUTILS_UTF8(fileInfo.first().canonicalFilePath()));
645 //Lock the encoder binaries
646 QScopedPointer<LockedFile> binaries[MAX_FILES];
649 size_t index = 0;
650 for(QFileInfoList::ConstIterator iter = fileInfo.constBegin(); iter != fileInfo.constEnd(); iter++)
652 binaries[index++].reset(new LockedFile(iter->canonicalFilePath()));
655 catch(...)
657 qWarning("Failed to get excluive lock to encoder binary -> %s support will be disabled!", toolName);
658 return;
661 QProcess process;
662 MUtils::init_process(process, fileInfo.first().absolutePath());
663 process.start(fileInfo.first().canonicalFilePath(), checkArgs);
665 if(!process.waitForStarted())
667 qWarning("%s process failed to create!", toolName);
668 qWarning("Error message: \"%s\"\n", process.errorString().toLatin1().constData());
669 process.kill();
670 process.waitForFinished(-1);
671 return;
674 quint32 toolVersion = 0;
675 bool sigFound = regExpSig.isEmpty() ? true : false;
677 while(process.state() != QProcess::NotRunning)
679 if(!process.waitForReadyRead())
681 if(process.state() == QProcess::Running)
683 qWarning("%s process time out -> killing!", toolName);
684 process.kill();
685 process.waitForFinished(-1);
686 return;
689 while(process.canReadLine())
691 QString line = QString::fromUtf8(process.readLine().constData()).simplified();
692 if((!sigFound) && regExpSig.lastIndexIn(line) >= 0)
694 sigFound = true;
695 continue;
697 if(sigFound && (regExpVer.lastIndexIn(line) >= 0))
699 quint32 tmp[8];
700 if(MUtils::regexp_parse_uint32(regExpVer, tmp, qMin(verDigits, 8U)))
702 toolVersion = 0;
703 for(quint32 i = 0; i < verDigits; i++)
705 toolVersion = (verShift > 0) ? ((toolVersion * verShift) + qBound(0U, tmp[i], (verShift - 1))) : tmp[i];
712 if(toolVersion <= 0)
714 qWarning("%s version could not be determined -> Encoding support will be disabled!", toolName);
715 return;
717 else if(toolVersion < toolMinVersion)
719 qWarning("%s version is too much outdated (%s) -> Encoding support will be disabled!", toolName, MUTILS_UTF8(lamexp_version2string(verStr, toolVersion, "N/A")));
720 qWarning("Minimum required %s version currently is: %s\n", toolName, MUTILS_UTF8(lamexp_version2string(verStr, toolMinVersion, "N/A")));
721 return;
724 qDebug("Enabled %s encoder %s.\n", toolName, MUTILS_UTF8(lamexp_version2string(verStr, toolVersion, "N/A")));
726 size_t index = 0;
727 for(QFileInfoList::ConstIterator iter = fileInfo.constBegin(); iter != fileInfo.constEnd(); iter++)
729 lamexp_tools_register(iter->fileName(), binaries[index++].take(), toolVersion);
733 ////////////////////////////////////////////////////////////
734 // Self-Test Function
735 ////////////////////////////////////////////////////////////
737 void InitializationThread::selfTest(void)
739 const unsigned int cpu[7] = {CPU_TYPE_X86_GEN, CPU_TYPE_X86_SSE, CPU_TYPE_X86_AVX, CPU_TYPE_X64_GEN, CPU_TYPE_X64_SSE, CPU_TYPE_X64_AVX, 0 };
741 for(size_t k = 0; cpu[k]; k++)
743 qDebug("[TEST]");
744 switch(cpu[k])
746 PRINT_CPU_TYPE(CPU_TYPE_X86_GEN); break;
747 PRINT_CPU_TYPE(CPU_TYPE_X86_SSE); break;
748 PRINT_CPU_TYPE(CPU_TYPE_X86_AVX); break;
749 PRINT_CPU_TYPE(CPU_TYPE_X64_GEN); break;
750 PRINT_CPU_TYPE(CPU_TYPE_X64_SSE); break;
751 PRINT_CPU_TYPE(CPU_TYPE_X64_AVX); break;
752 default:
753 MUTILS_THROW("CPU support undefined!");
755 unsigned int n = 0;
756 for(int i = 0; true; i++)
758 if(!(g_lamexp_tools[i].pcName || g_lamexp_tools[i].pcHash || g_lamexp_tools[i].uiVersion))
760 break;
762 else if(g_lamexp_tools[i].pcName && g_lamexp_tools[i].pcHash && g_lamexp_tools[i].uiVersion)
764 const QString toolName = QString::fromLatin1(g_lamexp_tools[i].pcName);
765 const QByteArray expectedHash = QByteArray(g_lamexp_tools[i].pcHash);
766 if(g_lamexp_tools[i].uiCpuType & cpu[k])
768 qDebug("%02i -> %s", ++n, MUTILS_UTF8(toolName));
769 QFile resource(QString(":/tools/%1").arg(toolName));
770 if(!resource.open(QIODevice::ReadOnly))
772 qFatal("The resource for \"%s\" could not be opened!", MUTILS_UTF8(toolName));
773 break;
775 QByteArray hash = FileHash::computeHash(resource);
776 if(hash.isNull() || _stricmp(hash.constData(), expectedHash.constData()))
778 qFatal("Hash check for tool \"%s\" has failed!", MUTILS_UTF8(toolName));
779 break;
781 resource.close();
784 else
786 qFatal("Inconsistent checksum data detected. Take care!");
789 if(n != EXPECTED_TOOL_COUNT)
791 qFatal("Tool count mismatch for CPU type %u. Should be %u, but got %u !!!", cpu[k], EXPECTED_TOOL_COUNT, n);
793 qDebug("Done.\n");
797 ////////////////////////////////////////////////////////////
798 // EVENTS
799 ////////////////////////////////////////////////////////////
801 /*NONE*/