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(-)
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