From ea6292168a6acd4c263913f0ccd7dd64daf4f5cf Mon Sep 17 00:00:00 2001 From: Sandro Jäckel Date: Fri, 23 Feb 2018 01:45:58 +0100 Subject: Revert "Applied clang-format on source" This reverts commit 5e5c9c07291f6b09623d31c92b1fb61c4ede576e. --- src/loadmechanism.cpp | 454 ++++++++++++++++++++++++++------------------------ 1 file changed, 240 insertions(+), 214 deletions(-) (limited to 'src/loadmechanism.cpp') diff --git a/src/loadmechanism.cpp b/src/loadmechanism.cpp index 1aef11dd..7ad36ac7 100644 --- a/src/loadmechanism.cpp +++ b/src/loadmechanism.cpp @@ -18,273 +18,299 @@ along with Mod Organizer. If not, see . */ #include "loadmechanism.h" -#include "util.h" #include "utility.h" -#include -#include -#include +#include "util.h" +#include +#include +#include #include #include -#include +#include #include -#include -#include -#include +#include +#include +#include + using namespace MOBase; using namespace MOShared; -LoadMechanism::LoadMechanism() : m_SelectedMechanism(LOAD_MODORGANIZER) {} -void LoadMechanism::writeHintFile(const QDir& targetDirectory) { - QString hintFilePath = targetDirectory.absoluteFilePath("mo_path.txt"); - QFile hintFile(hintFilePath); - if (hintFile.exists()) { - hintFile.remove(); - } - if (!hintFile.open(QIODevice::WriteOnly)) { - throw MyException(QObject::tr("failed to open %1: %2").arg(hintFilePath).arg(hintFile.errorString())); - } - hintFile.write(qApp->applicationDirPath().toUtf8().constData()); - hintFile.close(); +LoadMechanism::LoadMechanism() + : m_SelectedMechanism(LOAD_MODORGANIZER) +{ } -void LoadMechanism::removeHintFile(QDir targetDirectory) { targetDirectory.remove("mo_path.txt"); } +void LoadMechanism::writeHintFile(const QDir &targetDirectory) +{ + QString hintFilePath = targetDirectory.absoluteFilePath("mo_path.txt"); + QFile hintFile(hintFilePath); + if (hintFile.exists()) { + hintFile.remove(); + } + if (!hintFile.open(QIODevice::WriteOnly)) { + throw MyException(QObject::tr("failed to open %1: %2").arg(hintFilePath).arg(hintFile.errorString())); + } + hintFile.write(qApp->applicationDirPath().toUtf8().constData()); + hintFile.close(); +} -bool LoadMechanism::isDirectLoadingSupported() { - // FIXME: Seriously? isn't there a 'do i need steam' thing? - IPluginGame const* game = qApp->property("managed_game").value(); - if (game->gameName().compare("oblivion", Qt::CaseInsensitive) == 0) { - // oblivion can be loaded directly if it's not the steam variant - return !game->gameDirectory().exists("steam_api.dll"); - } else { - // all other games work afaik - return true; - } + +void LoadMechanism::removeHintFile(QDir targetDirectory) +{ + targetDirectory.remove("mo_path.txt"); } -bool LoadMechanism::isScriptExtenderSupported() { - IPluginGame const* game = qApp->property("managed_game").value(); - ScriptExtender* extender = game->feature(); - // test if there even is an extender for the managed game and if so whether it's installed - return extender != nullptr && extender->isInstalled(); +bool LoadMechanism::isDirectLoadingSupported() +{ + //FIXME: Seriously? isn't there a 'do i need steam' thing? + IPluginGame const *game = qApp->property("managed_game").value(); + if (game->gameName().compare("oblivion", Qt::CaseInsensitive) == 0) { + // oblivion can be loaded directly if it's not the steam variant + return !game->gameDirectory().exists("steam_api.dll"); + } else { + // all other games work afaik + return true; + } } -bool LoadMechanism::isProxyDLLSupported() { - // using steam_api.dll as the proxy is way too game specific as many games will have different - // versions of that dll. - // plus: the proxy dll hasn't been working for at least the whole 1.12.x versions of MO and - // noone reported it so why maintain an unused feature? - return false; - /* IPluginGame const *game = qApp->property("managed_game").value(); - return game->gameDirectory().exists(QString::fromStdWString(AppConfig::proxyDLLTarget()));*/ +bool LoadMechanism::isScriptExtenderSupported() +{ + IPluginGame const *game = qApp->property("managed_game").value(); + ScriptExtender *extender = game->feature(); + + // test if there even is an extender for the managed game and if so whether it's installed + return extender != nullptr && extender->isInstalled(); } -bool LoadMechanism::hashIdentical(const QString& fileNameLHS, const QString& fileNameRHS) { - QFile fileLHS(fileNameLHS); - if (!fileLHS.open(QIODevice::ReadOnly)) { - throw MyException(QObject::tr("%1 not found").arg(fileNameLHS)); - } - QByteArray dataLHS = fileLHS.readAll(); - QByteArray hashLHS = QCryptographicHash::hash(dataLHS, QCryptographicHash::Md5); +bool LoadMechanism::isProxyDLLSupported() +{ + // using steam_api.dll as the proxy is way too game specific as many games will have different + // versions of that dll. + // plus: the proxy dll hasn't been working for at least the whole 1.12.x versions of MO and + // noone reported it so why maintain an unused feature? + return false; +/* IPluginGame const *game = qApp->property("managed_game").value(); + return game->gameDirectory().exists(QString::fromStdWString(AppConfig::proxyDLLTarget()));*/ +} - fileLHS.close(); - QFile fileRHS(fileNameRHS); - if (!fileRHS.open(QIODevice::ReadOnly)) { - throw MyException(QObject::tr("%1 not found").arg(fileNameRHS)); - } - QByteArray dataRHS = fileRHS.readAll(); - QByteArray hashRHS = QCryptographicHash::hash(dataRHS, QCryptographicHash::Md5); +bool LoadMechanism::hashIdentical(const QString &fileNameLHS, const QString &fileNameRHS) +{ + QFile fileLHS(fileNameLHS); + if (!fileLHS.open(QIODevice::ReadOnly)) { + throw MyException(QObject::tr("%1 not found").arg(fileNameLHS)); + } + QByteArray dataLHS = fileLHS.readAll(); + QByteArray hashLHS = QCryptographicHash::hash(dataLHS, QCryptographicHash::Md5); + + fileLHS.close(); + + QFile fileRHS(fileNameRHS); + if (!fileRHS.open(QIODevice::ReadOnly)) { + throw MyException(QObject::tr("%1 not found").arg(fileNameRHS)); + } + QByteArray dataRHS = fileRHS.readAll(); + QByteArray hashRHS = QCryptographicHash::hash(dataRHS, QCryptographicHash::Md5); - fileRHS.close(); + fileRHS.close(); - return hashLHS == hashRHS; + return hashLHS == hashRHS; } -void LoadMechanism::deactivateScriptExtender() { - try { - IPluginGame const* game = qApp->property("managed_game").value(); - ScriptExtender* extender = game->feature(); - if (extender == nullptr) { - throw MyException(QObject::tr("game doesn't support a script extender")); - } - QDir pluginsDir(game->gameDirectory().absolutePath() + "/data/" + extender->PluginPath()); +void LoadMechanism::deactivateScriptExtender() +{ + try { + IPluginGame const *game = qApp->property("managed_game").value(); + ScriptExtender *extender = game->feature(); + if (extender == nullptr) { + throw MyException(QObject::tr("game doesn't support a script extender")); + } -#pragma message("implement this for usvfs") + QDir pluginsDir(game->gameDirectory().absolutePath() + "/data/" + extender->PluginPath()); - QString vfsDLLName = ""; - if (extender->getArch() == IMAGE_FILE_MACHINE_I386) { - vfsDLLName = ToQString(AppConfig::vfs32DLLName()); - } else if (extender->getArch() == IMAGE_FILE_MACHINE_AMD64) { - vfsDLLName = ToQString(AppConfig::vfs64DLLName()); - } - qDebug("USVFS DLL Name: " + vfsDLLName.toLatin1()); - if (vfsDLLName != "") { - if (QFile(pluginsDir.absoluteFilePath(vfsDLLName)).exists()) { - // remove dll from SE plugins directory - if (!pluginsDir.remove(vfsDLLName)) { - throw MyException(QObject::tr("Failed to delete %1").arg(pluginsDir.absoluteFilePath(vfsDLLName))); - } - } - } +#pragma message("implement this for usvfs") - removeHintFile(pluginsDir); - } catch (const std::exception& e) { - QMessageBox::critical(nullptr, QObject::tr("Failed to deactivate script extender loading"), e.what()); - } + QString vfsDLLName = ""; + if (extender->getArch() == IMAGE_FILE_MACHINE_I386) { + vfsDLLName = ToQString(AppConfig::vfs32DLLName()); + } + else if (extender->getArch() == IMAGE_FILE_MACHINE_AMD64) + { + vfsDLLName = ToQString(AppConfig::vfs64DLLName()); + } + qDebug("USVFS DLL Name: " + vfsDLLName.toLatin1()); + if (vfsDLLName != "") { + if (QFile(pluginsDir.absoluteFilePath(vfsDLLName)).exists()) { + // remove dll from SE plugins directory + if (!pluginsDir.remove(vfsDLLName)) { + throw MyException(QObject::tr("Failed to delete %1").arg(pluginsDir.absoluteFilePath(vfsDLLName))); + } + } + } + + removeHintFile(pluginsDir); + } catch (const std::exception &e) { + QMessageBox::critical(nullptr, QObject::tr("Failed to deactivate script extender loading"), e.what()); + } } -void LoadMechanism::deactivateProxyDLL() { - try { - IPluginGame const* game = qApp->property("managed_game").value(); - - QString targetPath = - game->gameDirectory().absoluteFilePath(QString::fromStdWString(AppConfig::proxyDLLTarget())); - - QFile targetDLL(targetPath); - if (targetDLL.exists()) { - QString origFile = - game->gameDirectory().absoluteFilePath(QString::fromStdWString(AppConfig::proxyDLLOrig())); - // determine if a proxy-dll is installed - // this is a very crude way of making this decision but it should be good enough - if ((targetDLL.size() < 24576) && (QFile(origFile).exists())) { - // remove proxy-dll - if (!targetDLL.remove()) { - throw MyException( - QObject::tr("Failed to remove %1: %2").arg(targetPath).arg(targetDLL.errorString())); - } else if (!QFile::rename(origFile, targetPath)) { - throw MyException(QObject::tr("Failed to rename %1 to %2").arg(origFile, targetPath)); - } - } - } - removeHintFile(game->gameDirectory()); - } catch (const std::exception& e) { - QMessageBox::critical(nullptr, QObject::tr("Failed to deactivate proxy-dll loading"), e.what()); +void LoadMechanism::deactivateProxyDLL() +{ + try { + IPluginGame const *game = qApp->property("managed_game").value(); + + QString targetPath = game->gameDirectory().absoluteFilePath(QString::fromStdWString(AppConfig::proxyDLLTarget())); + + QFile targetDLL(targetPath); + if (targetDLL.exists()) { + QString origFile = game->gameDirectory().absoluteFilePath(QString::fromStdWString(AppConfig::proxyDLLOrig())); + // determine if a proxy-dll is installed + // this is a very crude way of making this decision but it should be good enough + if ((targetDLL.size() < 24576) && (QFile(origFile).exists())) { + // remove proxy-dll + if (!targetDLL.remove()) { + throw MyException(QObject::tr("Failed to remove %1: %2").arg(targetPath).arg(targetDLL.errorString())); + } else if (!QFile::rename(origFile, targetPath)) { + throw MyException(QObject::tr("Failed to rename %1 to %2").arg(origFile, targetPath)); + } + } } + + removeHintFile(game->gameDirectory()); + } catch (const std::exception &e) { + QMessageBox::critical(nullptr, QObject::tr("Failed to deactivate proxy-dll loading"), e.what()); + } } -void LoadMechanism::activateScriptExtender() { - try { - IPluginGame const* game = qApp->property("managed_game").value(); - ScriptExtender* extender = game->feature(); - if (extender == nullptr) { - throw MyException(QObject::tr("game doesn't support a script extender")); - } - QDir pluginsDir(game->gameDirectory().absolutePath() + "/data/" + extender->PluginPath()); +void LoadMechanism::activateScriptExtender() +{ + try { + IPluginGame const *game = qApp->property("managed_game").value(); + ScriptExtender *extender = game->feature(); + if (extender == nullptr) { + throw MyException(QObject::tr("game doesn't support a script extender")); + } - if (!pluginsDir.exists()) { - pluginsDir.mkpath("."); - } + QDir pluginsDir(game->gameDirectory().absolutePath() + "/data/" + extender->PluginPath()); -#pragma message("implement this for usvfs") - std::wstring vfsDLL = L""; - if (extender->getArch() == IMAGE_FILE_MACHINE_I386) { - vfsDLL = AppConfig::vfs32DLLName(); - } else if (extender->getArch() == IMAGE_FILE_MACHINE_AMD64) { - vfsDLL = AppConfig::vfs64DLLName(); - } - if (vfsDLL != L"") { - QString targetPath = pluginsDir.absoluteFilePath(ToQString(vfsDLL)); - QString vfsDLLPath = qApp->applicationDirPath() + "/" + QString::fromStdWString(vfsDLL); - - qDebug("DLL USVFS Target Path: " + targetPath.toLatin1()); - qDebug("DLL USVFS VFS DLL Path: " + vfsDLLPath.toLatin1()); - - QFile dllFile(targetPath); - - if (dllFile.exists()) { - // may be outdated - if (!hashIdentical(targetPath, vfsDLLPath)) { - dllFile.remove(); - } - } - - if (!dllFile.exists()) { - // install dll to SE plugins - if (!QFile::copy(vfsDLLPath, targetPath)) { - throw MyException(QObject::tr("Failed to copy %1 to %2").arg(vfsDLLPath, targetPath)); - } - } - } - writeHintFile(pluginsDir); - } catch (const std::exception& e) { - QMessageBox::critical(nullptr, QObject::tr("Failed to set up script extender loading"), e.what()); + if (!pluginsDir.exists()) { + pluginsDir.mkpath("."); } + +#pragma message("implement this for usvfs") + std::wstring vfsDLL = L""; + if (extender->getArch() == IMAGE_FILE_MACHINE_I386) { + vfsDLL = AppConfig::vfs32DLLName(); + } + else if (extender->getArch() == IMAGE_FILE_MACHINE_AMD64) + { + vfsDLL = AppConfig::vfs64DLLName(); + } + if (vfsDLL != L"") { + QString targetPath = pluginsDir.absoluteFilePath(ToQString(vfsDLL)); + QString vfsDLLPath = qApp->applicationDirPath() + "/" + QString::fromStdWString(vfsDLL); + + qDebug("DLL USVFS Target Path: " + targetPath.toLatin1()); + qDebug("DLL USVFS VFS DLL Path: " + vfsDLLPath.toLatin1()); + + QFile dllFile(targetPath); + + if (dllFile.exists()) { + // may be outdated + if (!hashIdentical(targetPath, vfsDLLPath)) { + dllFile.remove(); + } + } + + if (!dllFile.exists()) { + // install dll to SE plugins + if (!QFile::copy(vfsDLLPath, targetPath)) { + throw MyException(QObject::tr("Failed to copy %1 to %2").arg(vfsDLLPath, targetPath)); + } + } + } + writeHintFile(pluginsDir); + } catch (const std::exception &e) { + QMessageBox::critical(nullptr, QObject::tr("Failed to set up script extender loading"), e.what()); + } } -void LoadMechanism::activateProxyDLL() { - try { - IPluginGame const* game = qApp->property("managed_game").value(); - QString targetPath = - game->gameDirectory().absoluteFilePath(QString::fromStdWString(AppConfig::proxyDLLTarget())); +void LoadMechanism::activateProxyDLL() +{ + try { + IPluginGame const *game = qApp->property("managed_game").value(); + + QString targetPath = game->gameDirectory().absoluteFilePath(QString::fromStdWString(AppConfig::proxyDLLTarget())); + + QFile targetDLL(targetPath); + if (!targetDLL.exists()) { + return; + } + + QString sourcePath = qApp->applicationDirPath() + "/" + ToQString(AppConfig::proxyDLLSource()); - QFile targetDLL(targetPath); - if (!targetDLL.exists()) { - return; + // this is a very crude way of making this decision but it should be good enough + if (targetDLL.size() < 24576) { + // determine if a proxy-dll is already installed and if so, if it's the right one + if (!hashIdentical(targetPath, sourcePath)) { + // wrong proxy dll, probably outdated. delete and install the new one + if (!QFile::remove(targetPath)) { + throw MyException(QObject::tr("Failed to delete old proxy-dll %1").arg(targetPath)); + } + if (!QFile::copy(sourcePath, targetPath)) { + throw MyException(QObject::tr("Failed to copy %1 to %2").arg(sourcePath).arg(targetPath)); } + } // otherwise the proxy-dll is already the right one + } else { + // no proxy dll installed yet. move the original and insert proxy-dll + + QString origFile = game->gameDirectory().absoluteFilePath(QString::fromStdWString(AppConfig::proxyDLLOrig())); - QString sourcePath = qApp->applicationDirPath() + "/" + ToQString(AppConfig::proxyDLLSource()); - - // this is a very crude way of making this decision but it should be good enough - if (targetDLL.size() < 24576) { - // determine if a proxy-dll is already installed and if so, if it's the right one - if (!hashIdentical(targetPath, sourcePath)) { - // wrong proxy dll, probably outdated. delete and install the new one - if (!QFile::remove(targetPath)) { - throw MyException(QObject::tr("Failed to delete old proxy-dll %1").arg(targetPath)); - } - if (!QFile::copy(sourcePath, targetPath)) { - throw MyException(QObject::tr("Failed to copy %1 to %2").arg(sourcePath).arg(targetPath)); - } - } // otherwise the proxy-dll is already the right one - } else { - // no proxy dll installed yet. move the original and insert proxy-dll - - QString origFile = - game->gameDirectory().absoluteFilePath(QString::fromStdWString(AppConfig::proxyDLLOrig())); - - if (QFile(origFile).exists()) { - // orig-file exists. this may happen if the steam-api was updated or the user messed with the - // dlls. - if (!QFile::remove(origFile)) { - throw MyException(QObject::tr("Failed to overwrite %1").arg(origFile)); - } - } - if (!QFile::rename(targetPath, origFile)) { - throw MyException(QObject::tr("Failed to rename %1 to %2").arg(targetPath).arg(origFile)); - } - if (!QFile::copy(sourcePath, targetPath)) { - throw MyException(QObject::tr("Failed to copy %1 to %2").arg(sourcePath).arg(targetPath)); - } + if (QFile(origFile).exists()) { + // orig-file exists. this may happen if the steam-api was updated or the user messed with the + // dlls. + if (!QFile::remove(origFile)) { + throw MyException(QObject::tr("Failed to overwrite %1").arg(origFile)); } - writeHintFile(game->gameDirectory()); - } catch (const std::exception& e) { - QMessageBox::critical(nullptr, QObject::tr("Failed to set up proxy-dll loading"), e.what()); + } + if (!QFile::rename(targetPath, origFile)) { + throw MyException(QObject::tr("Failed to rename %1 to %2").arg(targetPath).arg(origFile)); + } + if (!QFile::copy(sourcePath, targetPath)) { + throw MyException(QObject::tr("Failed to copy %1 to %2").arg(sourcePath).arg(targetPath)); + } } + writeHintFile(game->gameDirectory()); + } catch (const std::exception &e) { + QMessageBox::critical(nullptr, QObject::tr("Failed to set up proxy-dll loading"), e.what()); + } } -void LoadMechanism::activate(EMechanism mechanism) { - switch (mechanism) { + +void LoadMechanism::activate(EMechanism mechanism) +{ + switch (mechanism) { case LOAD_MODORGANIZER: { - qDebug("Load Mechanism: Mod Organizer"); - deactivateProxyDLL(); - deactivateScriptExtender(); + qDebug("Load Mechanism: Mod Organizer"); + deactivateProxyDLL(); + deactivateScriptExtender(); } break; case LOAD_SCRIPTEXTENDER: { - qDebug("Load Mechanism: ScriptExtender"); - deactivateProxyDLL(); - activateScriptExtender(); + qDebug("Load Mechanism: ScriptExtender"); + deactivateProxyDLL(); + activateScriptExtender(); } break; case LOAD_PROXYDLL: { - qDebug("Load Mechanism: Proxy DLL"); - deactivateScriptExtender(); - activateProxyDLL(); + qDebug("Load Mechanism: Proxy DLL"); + deactivateScriptExtender(); + activateProxyDLL(); } break; - } + } } + -- cgit v1.3.1