From 5e528fb4cf16ae208944d15d568c9140e3d741e4 Mon Sep 17 00:00:00 2001 From: isanae <14251494+isanae@users.noreply.github.com> Date: Sat, 18 Jul 2020 02:24:44 -0400 Subject: new CommandLine class implemented crashdump as a command, fixed dump_running_process.bat to use it attach to console if present instead of always create one --- src/env.cpp | 42 +++++++++++++++++++++++++++++++++++++++--- 1 file changed, 39 insertions(+), 3 deletions(-) (limited to 'src/env.cpp') diff --git a/src/env.cpp b/src/env.cpp index 2862aa95..bf75c9fa 100644 --- a/src/env.cpp +++ b/src/env.cpp @@ -16,9 +16,15 @@ using namespace MOBase; Console::Console() : m_hasConsole(false), m_in(nullptr), m_out(nullptr), m_err(nullptr) { - // open a console - if (!AllocConsole()) { - // failed, ignore + // try to attach to parent + if (!AttachConsole(ATTACH_PARENT_PROCESS)) { + if (GetLastError() != ERROR_ACCESS_DENIED) { + // parent has no console, create one + if (!AllocConsole()) { + // failed, ignore + return; + } + } } m_hasConsole = true; @@ -1032,6 +1038,36 @@ HandlePtr dumpFile() return {}; } + +CoreDumpTypes coreDumpTypeFromString(const std::string& s) +{ + if (s == "data") + return env::CoreDumpTypes::Data; + else if (s == "full") + return env::CoreDumpTypes::Full; + else + return env::CoreDumpTypes::Mini; +} + +std::string toString(CoreDumpTypes type) +{ + switch (type) + { + case CoreDumpTypes::Mini: + return "mini"; + + case CoreDumpTypes::Data: + return "data"; + + case CoreDumpTypes::Full: + return "full"; + + default: + return "?"; + } +} + + bool createMiniDump(HANDLE process, CoreDumpTypes type) { const DWORD pid = GetProcessId(process); -- cgit v1.3.1 From 9435202034cafb05ffc11aed48ff57536bce73f7 Mon Sep 17 00:00:00 2001 From: isanae <14251494+isanae@users.noreply.github.com> Date: Wed, 22 Jul 2020 23:27:01 -0400 Subject: removed flags from SingleInstance because there's only one left refactoring in main.cpp: - moved stuff to loglist.cpp and moapplication.cpp - split main() into a few functions --- src/env.cpp | 13 ++- src/env.h | 3 +- src/loglist.cpp | 26 ++++++ src/loglist.h | 1 + src/main.cpp | 225 ++++++++++++++++++++++++------------------------- src/moapplication.cpp | 19 ++--- src/moapplication.h | 20 ++--- src/singleinstance.cpp | 4 +- src/singleinstance.h | 18 +--- src/spawn.cpp | 2 +- 10 files changed, 174 insertions(+), 157 deletions(-) (limited to 'src/env.cpp') diff --git a/src/env.cpp b/src/env.cpp index bf75c9fa..9f0acbbd 100644 --- a/src/env.cpp +++ b/src/env.cpp @@ -400,11 +400,18 @@ QString path() return get("PATH"); } -QString addPath(const QString& s) +QString appendToPath(const QString& s) { auto old = path(); - set("PATH", get("PATH") + ";" + s); - return old; + set("PATH", old + ";" + s); + return old; +} + +QString prependToPath(const QString& s) +{ + auto old = path(); + set("PATH", s + ";" + old); + return old; } QString setPath(const QString& s) diff --git a/src/env.h b/src/env.h index 9bec1713..a563f2c3 100644 --- a/src/env.h +++ b/src/env.h @@ -236,7 +236,8 @@ QString get(const QString& name); QString set(const QString& name, const QString& value); QString path(); -QString addPath(const QString& s); +QString appendToPath(const QString& s); +QString prependToPath(const QString& s); QString setPath(const QString& s); diff --git a/src/loglist.cpp b/src/loglist.cpp index 7a64ecad..167b61ef 100644 --- a/src/loglist.cpp +++ b/src/loglist.cpp @@ -327,3 +327,29 @@ void initLogging() qInstallMessageHandler(qtLogCallback); } + +bool createAndMakeWritable(const std::wstring &subPath) { + QString const dataPath = qApp->property("dataPath").toString(); + QString fullPath = dataPath + "/" + QString::fromStdWString(subPath); + + if (!QDir(fullPath).exists() && !QDir().mkdir(fullPath)) { + QMessageBox::critical(nullptr, QObject::tr("Error"), + QObject::tr("Failed to create \"%1\". Your user " + "account probably lacks permission.") + .arg(fullPath)); + return false; + } else { + return true; + } +} + +bool setLogDirectory(const QString& dir) +{ + const auto logFile = dir + "/logs/mo_interface.log"; + + if (!createAndMakeWritable(AppConfig::logPath())) { + return false; + } + + log::getDefault().setFile(MOBase::log::File::single(logFile.toStdWString())); +} diff --git a/src/loglist.h b/src/loglist.h index 7387eb50..0745ed3e 100644 --- a/src/loglist.h +++ b/src/loglist.h @@ -84,5 +84,6 @@ private: void initLogging(); +bool setLogDirectory(const QString& dir); #endif // LOGBUFFER_H diff --git a/src/main.cpp b/src/main.cpp index e52b252b..864d26a1 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -38,7 +38,13 @@ along with Mod Organizer. If not, see . #include #include -#pragma comment(linker, "/manifestDependency:\"name='dlls' processorArchitecture='x86' version='1.0.0.0' type='win32' \"") +// see addDllsToPath() below +#pragma comment(linker, "/manifestDependency:\"" \ + "name='dlls' " \ + "processorArchitecture='x86' " \ + "version='1.0.0.0' " \ + "type='win32' \"") + using namespace MOBase; using namespace MOShared; @@ -47,21 +53,6 @@ void sanityChecks(const env::Environment& env); int checkIncompatibleModule(const env::Module& m); int checkPathsForSanity(MOBase::IPluginGame& game, const Settings& s); -bool createAndMakeWritable(const std::wstring &subPath) { - QString const dataPath = qApp->property("dataPath").toString(); - QString fullPath = dataPath + "/" + QString::fromStdWString(subPath); - - if (!QDir(fullPath).exists() && !QDir().mkdir(fullPath)) { - QMessageBox::critical(nullptr, QObject::tr("Error"), - QObject::tr("Failed to create \"%1\". Your user " - "account probably lacks permission.") - .arg(fullPath)); - return false; - } else { - return true; - } -} - void purgeOldFiles() { // remove the temporary backup directory in case we're restarting after an @@ -305,36 +296,47 @@ MOBase::IPluginGame *determineCurrentGame( } -// extend path to include dll directory so plugins don't need a manifest -// (using AddDllDirectory would be an alternative to this but it seems fairly -// complicated esp. -// since it isn't easily accessible on Windows < 8 -// SetDllDirectory replaces other search directories and this seems to -// propagate to child processes) -void setupPath() +// This adds the `dlls` directory to the path so the dlls can be found. How +// MO is able to find dlls in there is a bit convoluted: +// +// Dependencies on DLLs can be baked into an executable by passing a +// `manifestdependency` option to the linker. This can be done on the command +// line or with a pragma. Typically, the dependency will not be a hardcoded +// filename, but an assembly name, such as Microsoft.Windows.Common-Controls. +// +// When Windows loads the exe, it will look for this assembly in a variety of +// places, such as in the WinSxS folder, but also in the program's folder. It +// will look for `assemblyname.dll` or `assemblyname/assemblyname.dll` and try +// to load that. +// +// If these files don't exist, then the loader gets creative and looks for +// `assemblyname.manifest` and `assemblyname/assemblyname.manifest`. A manifest +// file is just an XML file that can contain a list of DLLs to load for this +// assembly. +// +// In MO's case, there's a `pragma` at the beginning of this file which adds +// `dlls` as an "assembly" dependency. This is a bit of a hack to just force +// the loader to eventually find `dlls/dlls.manifest`, which contains the list +// of all the DLLs MO requires to load. +// +// This file was handwritten in `modorganizer/src/dlls.manifest.qt5` and +// is copied and renamed in CMakeLists.txt into `bin/dlls/dlls.manifest`. Note +// that the useless and incorrect .qt5 extension is removed. +// +void addDllsToPath() { - static const int BUFSIZE = 4096; - - QCoreApplication::setLibraryPaths(QStringList(QCoreApplication::applicationDirPath() + "/dlls") + QCoreApplication::libraryPaths()); + const auto dllsPath = QDir::toNativeSeparators( + QCoreApplication::applicationDirPath() + "/dlls"); - boost::scoped_array oldPath(new TCHAR[BUFSIZE]); - DWORD offset = ::GetEnvironmentVariable(TEXT("PATH"), oldPath.get(), BUFSIZE); - if (offset > BUFSIZE) { - oldPath.reset(new TCHAR[offset]); - ::GetEnvironmentVariable(TEXT("PATH"), oldPath.get(), offset); - } - - std::wstring newPath(ToWString(QDir::toNativeSeparators( - QCoreApplication::applicationDirPath())) + L"\\dlls"); - newPath += L";"; - newPath += oldPath.get(); + QCoreApplication::setLibraryPaths( + QStringList(dllsPath) + QCoreApplication::libraryPaths()); - ::SetEnvironmentVariableW(L"PATH", newPath.c_str()); + env::prependToPath(dllsPath); } int runApplication( MOApplication &application, const cl::CommandLine& cl, - SingleInstance &instance, const QString &splashPath) + SingleInstance &instance, const QString &dataPath) { TimeThis tt("runApplication() to exec()"); @@ -343,7 +345,6 @@ int runApplication( createVersionInfo().displayString(3), GITID, QCoreApplication::applicationDirPath(), MOShared::getUsvfsVersionString()); - const QString dataPath = application.property("dataPath").toString(); log::info("data path: {}", dataPath); if (InstanceManager::isPortablePath(dataPath)) { @@ -429,8 +430,14 @@ int runApplication( checkPathsForSanity(*game, settings); bool useSplash = settings.useSplash(); + QString splashPath; if (useSplash) { + splashPath = dataPath + "/splash.png"; + if (!QFile::exists(dataPath + "/splash.png")) { + splashPath = ":/MO/gui/splash"; + } + if (splashPath.startsWith(':')) { // currently using MO splash, see if the plugin contains one QString pluginSplash @@ -599,111 +606,101 @@ int runApplication( return 1; } -int main(int argc, char *argv[]) +int forwardToPrimary(SingleInstance& instance, const cl::CommandLine& cl) { - cl::CommandLine cl; - - const auto r = cl.run(GetCommandLineW()); - if (r) - return *r; - - TimeThis tt("main to runApplication()"); - - // in loglist.cpp - initLogging(); + if (cl.shortcut().isValid()) { + instance.sendMessage(cl.shortcut().toString()); + } else if (cl.nxmLink()) { + instance.sendMessage(*cl.nxmLink()); + } else { + QMessageBox::information( + nullptr, QObject::tr("Mod Organizer"), + QObject::tr("An instance of Mod Organizer is already running")); + } - //Make sure the configured temp folder exists - QDir tempDir = QDir::temp(); - if (!tempDir.exists()) - tempDir.root().mkpath(tempDir.canonicalPath()); + return 0; +} - //Should allow for better scaling of ui with higher resolution displays - QApplication::setAttribute(Qt::AA_EnableHighDpiScaling); +void resetForRestart(cl::CommandLine& cl) +{ + LogModel::instance().clear(); + ResetExitFlag(); - MOApplication application(argc, argv); - QStringList arguments = application.arguments(); + // make sure the log file isn't locked in case MO was restarted and + // the previous instance gets deleted + log::getDefault().setFile({}); - SetThisThreadName("main"); + // don't reprocess command line + cl.clear(); +} - setupPath(); +QString determineDataPath(const cl::CommandLine& cl) +{ + try + { + InstanceManager& instanceManager = InstanceManager::instance(); + if (cl.instance()) + instanceManager.overrideInstance(*cl.instance()); - SingleInstance::Flags siFlags = SingleInstance::NoFlags; + return instanceManager.determineDataPath(); + } + catch (const std::exception &e) + { + if (strcmp(e.what(),"Canceled")) { + QMessageBox::critical(nullptr, QObject::tr("Failed to set up instance"), e.what()); + } - if (cl.multiple()) { - arguments.removeAll("--multiple"); - siFlags |= SingleInstance::AllowMultiple; + return {}; } +} - SingleInstance instance(siFlags); - if (instance.ephemeral()) { - if (cl.shortcut().isValid()) { - instance.sendMessage(cl.shortcut().toString()); - return 0; - } else if (cl.nxmLink()) { - instance.sendMessage(*cl.nxmLink()); - return 0; - } else if (arguments.size() == 1) { - QMessageBox::information( - nullptr, QObject::tr("Mod Organizer"), - QObject::tr("An instance of Mod Organizer is already running")); - return 0; - } - } // we continue for the primary instance OR if MO was called with parameters +int main(int argc, char *argv[]) +{ + cl::CommandLine cl; - do { - LogModel::instance().clear(); - ResetExitFlag(); + if (auto r=cl.run(GetCommandLineW())) { + return *r; + } - // make sure the log file isn't locked in case MO was restarted and - // the previous instance gets deleted - log::getDefault().setFile({}); + TimeThis tt("main to runApplication()"); + SetThisThreadName("main"); - QString dataPath; + initLogging(); + auto application = MOApplication::create(argc, argv); + addDllsToPath(); - try { - InstanceManager& instanceManager = InstanceManager::instance(); + SingleInstance instance(cl.multiple()); + if (instance.ephemeral()) { + return forwardToPrimary(instance, cl); + } - if (cl.instance()) - instanceManager.overrideInstance(*cl.instance()); + for (;;) + { + // resets things when MO is "restarted" + resetForRestart(cl); - dataPath = instanceManager.determineDataPath(); - } catch (const std::exception &e) { - if (strcmp(e.what(),"Canceled")) - QMessageBox::critical(nullptr, QObject::tr("Failed to set up instance"), e.what()); + const QString dataPath = determineDataPath(cl); + if (dataPath.isEmpty()) { return 1; } - application.setProperty("dataPath", dataPath); - - // initialize dump collection only after "dataPath" since the crashes are stored under it - setUnhandledExceptionHandler(); - const auto logFile = - qApp->property("dataPath").toString() + "/logs/mo_interface.log"; + application.setProperty("dataPath", dataPath); + setExceptionHandler(); - if (!createAndMakeWritable(AppConfig::logPath())) { + if (!setLogDirectory(dataPath)) { reportError("Failed to create log folder"); InstanceManager::instance().clearCurrentInstance(); return 1; } - log::getDefault().setFile(MOBase::log::File::single(logFile.toStdWString())); - log::debug("command line: '{}'", QString::fromWCharArray(GetCommandLineW())); - QString splash = dataPath + "/splash.png"; - if (!QFile::exists(dataPath + "/splash.png")) { - splash = ":/MO/gui/splash"; - } - tt.stop(); - const int result = runApplication(application, cl, instance, splash); + const int result = runApplication(application, cl, instance, dataPath); if (result != RestartExitCode) { return result; } - - argc = 1; - cl.clear(); - } while (true); + } } diff --git a/src/moapplication.cpp b/src/moapplication.cpp index dd49bf53..d95d544a 100644 --- a/src/moapplication.cpp +++ b/src/moapplication.cpp @@ -24,16 +24,10 @@ along with Mod Organizer. If not, see . #include "shared/appconfig.h" #include #include -#if QT_VERSION < QT_VERSION_CHECK(5,0,0) -#include -#include -#endif #include #include #include #include - - #include @@ -77,7 +71,7 @@ public: }; -MOApplication::MOApplication(int &argc, char **argv) +MOApplication::MOApplication(int argc, char** argv) : QApplication(argc, argv) { connect(&m_StyleWatcher, &QFileSystemWatcher::fileChanged, [&](auto&& file){ @@ -89,8 +83,13 @@ MOApplication::MOApplication(int &argc, char **argv) setStyle(new ProxyStyle(style())); } +MOApplication MOApplication::create(int argc, char** argv) +{ + QApplication::setAttribute(Qt::AA_EnableHighDpiScaling); + return MOApplication(argc, argv); +} -bool MOApplication::setStyleFile(const QString &styleName) +bool MOApplication::setStyleFile(const QString& styleName) { // remove all files from watch QStringList currentWatch = m_StyleWatcher.files(); @@ -114,7 +113,7 @@ bool MOApplication::setStyleFile(const QString &styleName) } -bool MOApplication::notify(QObject *receiver, QEvent *event) +bool MOApplication::notify(QObject* receiver, QEvent* event) { try { return QApplication::notify(receiver, event); @@ -134,7 +133,7 @@ bool MOApplication::notify(QObject *receiver, QEvent *event) } -void MOApplication::updateStyle(const QString &fileName) +void MOApplication::updateStyle(const QString& fileName) { if (QStyleFactory::keys().contains(fileName)) { setStyle(QStyleFactory::create(fileName)); diff --git a/src/moapplication.h b/src/moapplication.h index 9db130af..3ed71fb6 100644 --- a/src/moapplication.h +++ b/src/moapplication.h @@ -24,27 +24,25 @@ along with Mod Organizer. If not, see . #include -class MOApplication : public QApplication { -Q_OBJECT -public: - - MOApplication(int &argc, char **argv); +class MOApplication : public QApplication +{ + Q_OBJECT - virtual bool notify (QObject *receiver, QEvent *event); +public: + static MOApplication create(int argc, char** argv); + virtual bool notify (QObject* receiver, QEvent* event); public slots: - - bool setStyleFile(const QString &style); + bool setStyleFile(const QString& style); private slots: - - void updateStyle(const QString &fileName); + void updateStyle(const QString& fileName); private: - QFileSystemWatcher m_StyleWatcher; QString m_DefaultStyle; + MOApplication(int argc, char** argv); }; diff --git a/src/singleinstance.cpp b/src/singleinstance.cpp index e32c8de1..bd7ccc43 100644 --- a/src/singleinstance.cpp +++ b/src/singleinstance.cpp @@ -28,14 +28,14 @@ static const int s_Timeout = 5000; using MOBase::reportError; -SingleInstance::SingleInstance(Flags flags, QObject *parent) : +SingleInstance::SingleInstance(bool allowMultiple, QObject *parent) : QObject(parent), m_Ephemeral(false), m_OwnsSM(false) { m_SharedMem.setKey(s_Key); if (!m_SharedMem.create(1)) { if (m_SharedMem.error() == QSharedMemory::AlreadyExists) { - if (!flags.testFlag(AllowMultiple)) { + if (!allowMultiple) { m_SharedMem.attach(); m_Ephemeral = true; } diff --git a/src/singleinstance.h b/src/singleinstance.h index d500a056..5f6c3633 100644 --- a/src/singleinstance.h +++ b/src/singleinstance.h @@ -36,19 +36,9 @@ class SingleInstance : public QObject Q_OBJECT public: - enum Flag - { - NoFlags = 0x00, - - // if another instance is running, run this one disconnected from the - // shared memory - AllowMultiple = 0x01 - }; - - using Flags = QFlags; - - - explicit SingleInstance(Flags flags, QObject *parent = 0); + // `allowMultiple`: if another instance is running, run this one + // disconnected from the shared memory + explicit SingleInstance(bool allowMultiple, QObject *parent = 0); /** * @return true if this instance's job is to forward data to the primary @@ -97,6 +87,4 @@ private: }; -Q_DECLARE_OPERATORS_FOR_FLAGS(SingleInstance::Flags); - #endif // SINGLEINSTANCE_H diff --git a/src/spawn.cpp b/src/spawn.cpp index 2016bb23..a9ecb61e 100644 --- a/src/spawn.cpp +++ b/src/spawn.cpp @@ -488,7 +488,7 @@ DWORD spawn(const SpawnParameters& sp, HANDLE& processHandle) } const QString moPath = QCoreApplication::applicationDirPath(); - const auto oldPath = env::addPath(QDir::toNativeSeparators(moPath)); + const auto oldPath = env::appendToPath(QDir::toNativeSeparators(moPath)); PROCESS_INFORMATION pi = {}; BOOL success = FALSE; -- cgit v1.3.1 From 3c791b092054192fcff27b599728d3482688aec8 Mon Sep 17 00:00:00 2001 From: isanae <14251494+isanae@users.noreply.github.com> Date: Thu, 23 Jul 2020 03:01:00 -0400 Subject: moved registry key from "Tannin" to "Mod Organizer Team" --- src/env.cpp | 160 ++++++++++++++++++++++++++++++++++++++++++++++++ src/env.h | 9 +++ src/instancemanager.cpp | 31 ++++++++-- src/instancemanager.h | 2 + 4 files changed, 196 insertions(+), 6 deletions(-) (limited to 'src/env.cpp') diff --git a/src/env.cpp b/src/env.cpp index 9f0acbbd..5bed84c1 100644 --- a/src/env.cpp +++ b/src/env.cpp @@ -846,6 +846,166 @@ Association getAssociation(const QFileInfo& targetInfo) } +struct RegistryKeyCloser +{ + using pointer = HKEY; + + void operator()(HKEY key) + { + if (key != 0) { + ::RegCloseKey(key); + } + } +}; + +using RegistryKeyPtr = std::unique_ptr; + +RegistryKeyPtr openRegistryKey(HKEY parent, const wchar_t* name) +{ + HKEY subkey = 0; + + auto r = ::RegOpenKeyExW( + parent, name, + 0, KEY_SET_VALUE|KEY_ENUMERATE_SUB_KEYS|KEY_QUERY_VALUE, + &subkey); + + if (r != ERROR_SUCCESS) { + return {}; + } + + return RegistryKeyPtr(subkey); +} + +bool keyHasValues(HKEY key) +{ + auto name = std::make_unique(1000 + 1); + DWORD nameSize = 1000; + + // note that RegEnumValueW() also enumerates the default value if it exists + auto r = ::RegEnumValueW( + key, 0, name.get(), &nameSize, nullptr, nullptr, nullptr, nullptr); + + if (r != ERROR_NO_MORE_ITEMS) { + return true; + } + + // no values, no default, it's empty + return false; +} + +bool forEachSubKey(HKEY key, std::function f) +{ + auto name = std::make_unique(1000 + 1); + DWORD nameSize = 1000; + + DWORD i = 0; + + // something would be really wrong if it had more than 100 keys + while (i < 100) + { + auto r = ::RegEnumKeyExW( + key, i, name.get(), &nameSize, nullptr, nullptr, nullptr, nullptr); + + if (r == ERROR_NO_MORE_ITEMS) { + // no more subkeys + break; + } + + if (r == ERROR_SUCCESS) { + // a subkey exists + auto subkey = openRegistryKey(key, name.get()); + + if (!subkey) { + // can't open it, stop + return false; + } + + // fire callback + if (!f(name.get())) { + return false; + } + } else { + // something went wrong, stop + return false; + } + + ++i; + } + + return true; +} + +bool isKeyEmpty(HKEY key) +{ + // check for any values + if (keyHasValues(key)) { + return false; + } + + auto r = forEachSubKey(key, [&](const wchar_t* name) { + // a subkey exists, recursively check if it's empty + auto subkey = openRegistryKey(key, name); + + if (!subkey) { + // can't open, stop + return false; + } + + if (!isKeyEmpty(subkey.get())) { + // not empty, stop + return false; + } + + // empty, go on + return true; + }); + + if (!r) { + // something went wrong or some subkey has values + return false; + } + + // key has no values and has either no subkeys or all subkeys are empty + return true; +} + +void deleteRegistryKeyIfEmpty(const QString& name) +{ + if (name.isEmpty()) { + return; + } + + auto key = openRegistryKey(HKEY_CURRENT_USER, name.toStdWString().c_str()); + if (!key) { + return; + } + + if (!isKeyEmpty(key.get())) { + return; + } + + ::RegDeleteTreeW(HKEY_CURRENT_USER, name.toStdWString().c_str()); +} + +bool registryValueExists(const QString& keyName, const QString& valueName) +{ + auto key = openRegistryKey( + HKEY_CURRENT_USER, keyName.toStdWString().c_str()); + + if (!key) { + return false; + } + + DWORD type = 0; + + auto r = ::RegQueryValueExW( + key.get(), valueName.toStdWString().c_str(), + nullptr, &type, nullptr, nullptr); + + return (r == ERROR_SUCCESS); +} + + // returns the filename of the given process or the current one // std::wstring processFilename(HANDLE process=INVALID_HANDLE_VALUE) diff --git a/src/env.h b/src/env.h index a563f2c3..dbbd5cf4 100644 --- a/src/env.h +++ b/src/env.h @@ -301,6 +301,15 @@ struct Association Association getAssociation(const QFileInfo& file); +// returns whether the given value exists +// +bool registryValueExists(const QString& key, const QString& value); + +// deletes a registry key if it's empty or only contains empty keys +// +void deleteRegistryKeyIfEmpty(const QString& name); + + enum class CoreDumpTypes { Mini = 1, diff --git a/src/instancemanager.cpp b/src/instancemanager.cpp index 9bcd0f3c..aa8f1d8c 100644 --- a/src/instancemanager.cpp +++ b/src/instancemanager.cpp @@ -23,6 +23,7 @@ along with Mod Organizer. If not, see . #include "settings.h" #include "shared/appconfig.h" #include "plugincontainer.h" +#include "env.h" #include #include #include @@ -37,14 +38,15 @@ along with Mod Organizer. If not, see . using namespace MOBase; -static const char COMPANY_NAME[] = "Tannin"; -static const char APPLICATION_NAME[] = "Mod Organizer"; -static const char INSTANCE_KEY[] = "CurrentInstance"; +const QString Organization = "Mod Organizer Team"; +const QString Application = "Mod Organizer"; +const QString InstanceValue = "CurrentInstance"; InstanceManager::InstanceManager() - : m_AppSettings(COMPANY_NAME, APPLICATION_NAME) + : m_AppSettings(Organization, Application) { + updateRegistryKey(); } InstanceManager &InstanceManager::instance() @@ -53,6 +55,23 @@ InstanceManager &InstanceManager::instance() return s_Instance; } +void InstanceManager::updateRegistryKey() +{ + const QString OldOrganization = "Tannin"; + const QString OldApplication = "Mod Organizer"; + const QString OldInstanceValue = "CurrentInstance"; + + const QString OldRootKey = "Software\\" + OldOrganization; + + if (env::registryValueExists(OldRootKey + "\\" + OldApplication, OldInstanceValue)) { + QSettings old(OldOrganization, OldApplication); + setCurrentInstance(old.value(OldInstanceValue).toString()); + old.remove(OldInstanceValue); + } + + env::deleteRegistryKeyIfEmpty(OldRootKey); +} + void InstanceManager::overrideInstance(const QString& instanceName) { m_overrideInstanceName = instanceName; @@ -70,7 +89,7 @@ QString InstanceManager::currentInstance() const if (m_overrideInstance) return m_overrideInstanceName; else - return m_AppSettings.value(INSTANCE_KEY, "").toString(); + return m_AppSettings.value(InstanceValue, "").toString(); } void InstanceManager::clearCurrentInstance() @@ -82,7 +101,7 @@ void InstanceManager::clearCurrentInstance() void InstanceManager::setCurrentInstance(const QString &name) { - m_AppSettings.setValue(INSTANCE_KEY, name); + m_AppSettings.setValue(InstanceValue, name); } bool InstanceManager::deleteLocalInstance(const QString &instanceId) const diff --git a/src/instancemanager.h b/src/instancemanager.h index 2331ebfd..7f87045d 100644 --- a/src/instancemanager.h +++ b/src/instancemanager.h @@ -71,6 +71,8 @@ private: bool portableInstall() const; bool portableInstallIsLocked() const; + void updateRegistryKey(); + private: QSettings m_AppSettings; -- cgit v1.3.1