Skip to content

Commit

Permalink
Some code refactoring in the "startup" threads.
Browse files Browse the repository at this point in the history
  • Loading branch information
lordmulder committed May 9, 2019
1 parent af87671 commit 3c6316d
Show file tree
Hide file tree
Showing 9 changed files with 162 additions and 174 deletions.
64 changes: 20 additions & 44 deletions src/thread_avisynth.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
#include "thread_avisynth.h"

//Qt
#include <QLibrary>
#include <QEventLoop>
#include <QTimer>
#include <QApplication>
Expand All @@ -43,11 +42,13 @@ static const bool ENABLE_PORTABLE_AVS = true;
QMutex AvisynthCheckThread::m_avsLock;
QScopedPointer<QFile> AvisynthCheckThread::m_avsDllPath[2];

//Helper
//-------------------------------------
// Auxilary functions
//-------------------------------------

#define VALID_DIR(STR) ((!(STR).isEmpty()) && QDir((STR)).exists())
#define BOOLIFY(X) ((X) ? '1' : '0')

//Utility function
QString AVS_CHECK_BINARY(const SysinfoModel *sysinfo, const bool& x64)
{
return QString("%1/toolset/%2/avs_check_%2.exe").arg(sysinfo->getAppPath(), (x64 ? "x64": "x86"));
Expand All @@ -58,7 +59,6 @@ class Wow64RedirectionDisabler
public:
Wow64RedirectionDisabler(void)
{
m_oldValue = NULL;
m_disabled = MUtils::OS::wow64fsredir_disable(m_oldValue);
}
~Wow64RedirectionDisabler(void)
Expand All @@ -73,7 +73,7 @@ class Wow64RedirectionDisabler
}
private:
bool m_disabled;
void* m_oldValue;
uintptr_t m_oldValue;
};

//-------------------------------------
Expand Down Expand Up @@ -140,8 +140,7 @@ AvisynthCheckThread::AvisynthCheckThread(const SysinfoModel *const sysinfo)
:
m_sysinfo(sysinfo)
{
m_success = false;
m_exception = false;
m_basePath.clear();
}

AvisynthCheckThread::~AvisynthCheckThread(void)
Expand All @@ -150,62 +149,33 @@ AvisynthCheckThread::~AvisynthCheckThread(void)

void AvisynthCheckThread::run(void)
{
m_exception = false;
m_success &= 0;
m_basePath.clear();

detectAvisynthVersion1(m_success, m_basePath, m_sysinfo, &m_exception);
StarupThread::run();
}

void AvisynthCheckThread::detectAvisynthVersion1(int &success, QString &basePath, const SysinfoModel *const sysinfo, volatile bool *exception)
int AvisynthCheckThread::threadMain(void)
{
__try
{
detectAvisynthVersion2(success, basePath, sysinfo, exception);
}
__except(1)
{
*exception = true;
qWarning("Unhandled exception error in Avisynth thread !!!");
}
}

void AvisynthCheckThread::detectAvisynthVersion2(int &success, QString &basePath, const SysinfoModel *const sysinfo, volatile bool *exception)
{
try
{
return detectAvisynthVersion3(success, basePath, sysinfo);
}
catch(...)
{
*exception = true;
qWarning("Avisynth initializdation raised an C++ exception!");
}
}

void AvisynthCheckThread::detectAvisynthVersion3(int &success, QString &basePath, const SysinfoModel *const sysinfo)
{
success &= 0;
int flags = 0;

QFile *avsPath32;
if(checkAvisynth(basePath, sysinfo, avsPath32, false))
if(checkAvisynth(m_basePath, m_sysinfo, avsPath32, false))
{
m_avsDllPath[0].reset(avsPath32);
success |= AVISYNTH_X86;
flags |= AVISYNTH_X86;
qDebug("Avisynth 32-Bit edition found!");
}
else
{
qDebug("Avisynth 32-Bit edition *not* found!");
}

if(sysinfo->getCPUFeatures(SysinfoModel::CPUFeatures_X64))
if(m_sysinfo->getCPUFeatures(SysinfoModel::CPUFeatures_X64))
{
QFile *avsPath64;
if(checkAvisynth(basePath, sysinfo, avsPath64, true))
if(checkAvisynth(m_basePath, m_sysinfo, avsPath64, true))
{
m_avsDllPath[1].reset(avsPath64);
success |= AVISYNTH_X64;
flags |= AVISYNTH_X64;
qDebug("Avisynth 64-Bit edition found!");
}
else
Expand All @@ -217,8 +187,14 @@ void AvisynthCheckThread::detectAvisynthVersion3(int &success, QString &basePath
{
qWarning("Skipping 64-Bit Avisynth check on non-x64 system!");
}

return flags;
}

//-------------------------------------
// Internal functions
//-------------------------------------

bool AvisynthCheckThread::checkAvisynth(QString &basePath, const SysinfoModel *const sysinfo, QFile *&path, const bool &x64)
{
qDebug("Avisynth %s-Bit support is being tested.", x64 ? "64" : "32");
Expand Down
10 changes: 2 additions & 8 deletions src/thread_avisynth.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,6 @@ class AvisynthCheckThread : public StarupThread
AvisynthCheckThread(const SysinfoModel *const sysinfo);
~AvisynthCheckThread(void);

int getSuccess(void) { return m_success; }
bool getException(void) { return m_exception; }
QString getPath(void) { return m_basePath; }

typedef enum _AvisynthFlags
Expand All @@ -54,8 +52,6 @@ private slots:
void start(Priority priority = InheritPriority) { QThread::start(priority); }

private:
volatile bool m_exception;
int m_success;
QString m_basePath;
const SysinfoModel *const m_sysinfo;

Expand All @@ -65,10 +61,8 @@ private slots:
//Entry point
virtual void run(void);

//Functions
static void detectAvisynthVersion1(int &success, QString &basePath, const SysinfoModel *const sysinfo, volatile bool *exception);
static void detectAvisynthVersion2(int &success, QString &basePath, const SysinfoModel *const sysinfo, volatile bool *exception);
static void detectAvisynthVersion3(int &success, QString &basePath, const SysinfoModel *const sysinfo);
//Thread main
virtual int threadMain(void);

//Internal functions
static bool checkAvisynth(QString &basePath, const SysinfoModel *const sysinfo, QFile *&path, const bool &x64);
Expand Down
67 changes: 19 additions & 48 deletions src/thread_binaries.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ QScopedPointer<QFile> BinariesCheckThread::m_binPath[MAX_BINARIES];
//Whatever
#define NEXT(X) ((*reinterpret_cast<int*>(&(X)))++)
#define SHFL(X) ((*reinterpret_cast<int*>(&(X))) <<= 1)
#define BOOLIFY(X) (!!(X))

//External
QString AVS_CHECK_BINARY(const SysinfoModel *sysinfo, const bool& x64);
Expand Down Expand Up @@ -91,7 +92,7 @@ bool BinariesCheckThread::check(const SysinfoModel *const sysinfo, QString *cons
return false;
}

const bool success = thread.getSuccess();
const bool success = BOOLIFY(thread.getSuccess());
if ((!success) && failedPath)
{
*failedPath = thread.getFailedPath();
Expand All @@ -108,7 +109,7 @@ BinariesCheckThread::BinariesCheckThread(const SysinfoModel *const sysinfo)
:
m_sysinfo(sysinfo)
{
m_success = m_exception = false;
m_failedPath.clear();
}

BinariesCheckThread::~BinariesCheckThread(void)
Expand All @@ -117,41 +118,12 @@ BinariesCheckThread::~BinariesCheckThread(void)

void BinariesCheckThread::run(void)
{
m_success = m_exception = false;
m_failedPath = QString();
checkBinaries1(m_success, m_failedPath, m_sysinfo, &m_exception);
m_failedPath.clear();
StarupThread::run();
}

void BinariesCheckThread::checkBinaries1(volatile bool &success, QString &failedPath, const SysinfoModel *const sysinfo, volatile bool *exception)
int BinariesCheckThread::threadMain(void)
{
__try
{
checkBinaries2(success, failedPath, sysinfo, exception);
}
__except(1)
{
*exception = true;
qWarning("Unhandled exception error in binaries checker thread !!!");
}
}

void BinariesCheckThread::checkBinaries2(volatile bool &success, QString &failedPath, const SysinfoModel *const sysinfo, volatile bool *exception)
{
try
{
return checkBinaries3(success, failedPath, sysinfo);
}
catch(...)
{
*exception = true;
qWarning("Binaries checker initializdation raised an C++ exception!");
}
}

void BinariesCheckThread::checkBinaries3(volatile bool &success, QString &failedPath, const SysinfoModel *const sysinfo)
{
success = true;

//Create list of all required binary files
typedef QPair<QString, bool> FileEntry;
QList<FileEntry> binFiles;
Expand All @@ -165,7 +137,7 @@ void BinariesCheckThread::checkBinaries3(volatile bool &success, QString &failed
const QStringList variants = encInfo.getVariants();
for (quint32 varntIdx = 0; varntIdx < quint32(variants.count()); ++varntIdx)
{
const QStringList dependencies = encInfo.getDependencies(sysinfo, archIdx, varntIdx);
const QStringList dependencies = encInfo.getDependencies(m_sysinfo, archIdx, varntIdx);
for (QStringList::ConstIterator iter = dependencies.constBegin(); iter != dependencies.constEnd(); iter++)
{
if (!filesSet.contains(*iter))
Expand All @@ -174,7 +146,7 @@ void BinariesCheckThread::checkBinaries3(volatile bool &success, QString &failed
binFiles << qMakePair(*iter, true);
}
}
const QString binary = encInfo.getBinaryPath(sysinfo, archIdx, varntIdx);
const QString binary = encInfo.getBinaryPath(m_sysinfo, archIdx, varntIdx);
if (!filesSet.contains(binary))
{
filesSet << binary;
Expand All @@ -185,14 +157,14 @@ void BinariesCheckThread::checkBinaries3(volatile bool &success, QString &failed
}
for(int i = 0; i < 2; i++)
{
binFiles << qMakePair(SourceFactory::getSourceInfo(SourceFactory::SourceType_AVS).getBinaryPath(sysinfo, bool(i)), false);
binFiles << qMakePair(AVS_CHECK_BINARY(sysinfo, bool(i)), false);
binFiles << qMakePair(SourceFactory::getSourceInfo(SourceFactory::SourceType_AVS).getBinaryPath(m_sysinfo, bool(i)), false);
binFiles << qMakePair(AVS_CHECK_BINARY(m_sysinfo, bool(i)), false);
}
for(size_t i = 0; UpdaterDialog::BINARIES[i].name; i++)
{
if(UpdaterDialog::BINARIES[i].exec)
{
binFiles << qMakePair(QString("%1/toolset/common/%2").arg(sysinfo->getAppPath(), QString::fromLatin1(UpdaterDialog::BINARIES[i].name)), false);
binFiles << qMakePair(QString("%1/toolset/common/%2").arg(m_sysinfo->getAppPath(), QString::fromLatin1(UpdaterDialog::BINARIES[i].name)), false);
}
}

Expand All @@ -209,20 +181,18 @@ void BinariesCheckThread::checkBinaries3(volatile bool &success, QString &failed
{
if (!MUtils::OS::is_executable_file(file->fileName()))
{
failedPath = file->fileName();
success = false;
m_failedPath = file->fileName();
qWarning("Required tool does NOT look like a valid Win32/Win64 binary:\n%s\n", MUTILS_UTF8(file->fileName()));
return;
return 0;
}
}
else
{
if (!MUtils::OS::is_library_file(file->fileName()))
{
failedPath = file->fileName();
success = false;
m_failedPath = file->fileName();
qWarning("Required tool does NOT look like a valid Win32/Win64 library:\n%s\n", MUTILS_UTF8(file->fileName()));
return;
return 0;
}
}
if(currentFile < MAX_BINARIES)
Expand All @@ -234,10 +204,11 @@ void BinariesCheckThread::checkBinaries3(volatile bool &success, QString &failed
}
else
{
failedPath = file->fileName();
success = false;
m_failedPath = file->fileName();
qWarning("Required tool could not be found or access denied:\n%s\n", MUTILS_UTF8(file->fileName()));
return;
return 0;
}
}

return 1;
}
19 changes: 6 additions & 13 deletions src/thread_binaries.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,16 @@

#pragma once

#include <QThread>
#include "thread_startup.h"

//Qt
#include <QMutex>

class QLibrary;
class SysinfoModel;
class QFile;

class BinariesCheckThread : public QThread
class BinariesCheckThread : public StarupThread
{
Q_OBJECT

Expand All @@ -38,17 +40,10 @@ class BinariesCheckThread : public QThread
protected:
BinariesCheckThread(const SysinfoModel *const sysinfo);
~BinariesCheckThread(void);

int getSuccess(void) { return m_success; }
bool getException(void) { return m_exception; }

const QString& getFailedPath(void) { return m_failedPath; }

private slots:
void start(Priority priority = InheritPriority) { QThread::start(priority); }

private:
volatile bool m_exception, m_success;
QString m_failedPath;
const SysinfoModel *const m_sysinfo;

Expand All @@ -59,8 +54,6 @@ private slots:
//Entry point
virtual void run(void);

//Functions
static void checkBinaries1(volatile bool &success, QString &failedPath, const SysinfoModel *const sysinfo, volatile bool *exception);
static void checkBinaries2(volatile bool &success, QString &failedPath, const SysinfoModel *const sysinfo, volatile bool *exception);
static void checkBinaries3(volatile bool &success, QString &failedPath, const SysinfoModel *const sysinfo);
//Thread main
virtual int threadMain(void);
};
Loading

0 comments on commit 3c6316d

Please sign in to comment.