diff options
| author | Brian Munro <brian.alexander.munro@gmail.com> | 2017-12-27 10:40:59 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2017-12-27 10:40:59 +0200 |
| commit | 91bd9b1eb6badea4558ba1c4ad0bac9549c5b314 (patch) | |
| tree | e236e2c472188d834bcf9215ed19527332e31e27 /src/main.cpp | |
| parent | 35e0bc22856c8f29e3c29ec27bd7c3c5a882197f (diff) | |
| parent | 548744d746223f0f96b33587ca41d2ed6a314f33 (diff) | |
Merge pull request #168 from erasmux/dev
Various fixes
Diffstat (limited to 'src/main.cpp')
| -rw-r--r-- | src/main.cpp | 81 |
1 files changed, 70 insertions, 11 deletions
diff --git a/src/main.cpp b/src/main.cpp index 3d315f1d..4c5ac75c 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -78,6 +78,7 @@ along with Mod Organizer. If not, see <http://www.gnu.org/licenses/>. #include <cstdarg>
#include <iostream>
+#include <sstream>
#include <stdexcept>
@@ -139,6 +140,60 @@ static LONG WINAPI MyUnhandledExceptionFilter(struct _EXCEPTION_POINTERS *except return EXCEPTION_CONTINUE_SEARCH;
}
+// Parses the first parseArgCount arguments of the current process command line and returns
+// them in parsedArgs, the rest of the command line is returned untouched.
+LPCWSTR UntouchedCommandLineArguments(int parseArgCount, std::vector<std::wstring>& parsedArgs)
+{
+ LPCWSTR cmd = GetCommandLineW();
+ LPCWSTR arg = nullptr; // to skip executable name
+ for (; parseArgCount >= 0 && *cmd; ++cmd)
+ {
+ if (*cmd == '"') {
+ int escaped = 0;
+ for (++cmd; *cmd && (*cmd != '"' || escaped % 2 != 0); ++cmd)
+ escaped = *cmd == '\\' ? escaped + 1 : 0;
+ }
+ if (*cmd == ' ') {
+ if (arg)
+ if (cmd-1 > arg && *arg == '"' && *(cmd-1) == '"')
+ parsedArgs.push_back(std::wstring(arg+1, cmd-1));
+ else
+ parsedArgs.push_back(std::wstring(arg, cmd));
+ arg = cmd + 1;
+ --parseArgCount;
+ }
+ }
+ return cmd;
+}
+
+static int SpawnWaitProcess(LPCWSTR workingDirectory, LPCWSTR commandLine) {
+ PROCESS_INFORMATION pi{ 0 };
+ STARTUPINFO si{ 0 };
+ si.cb = sizeof(si);
+ std::wstring commandLineCopy = commandLine;
+
+ if (!CreateProcessW(NULL, &commandLineCopy[0], NULL, NULL, FALSE, 0, NULL, workingDirectory, &si, &pi)) {
+ // A bit of a problem where to log the error message here, at least this way you can get the message
+ // using a either DebugView or a live debugger:
+ std::wostringstream ost;
+ ost << L"CreateProcess failed: " << commandLine << ", " << GetLastError();
+ OutputDebugStringW(ost.str().c_str());
+ return -1;
+ }
+
+ WaitForSingleObject(pi.hProcess, INFINITE);
+
+ DWORD exitCode = (DWORD)-1;
+ ::GetExitCodeProcess(pi.hProcess, &exitCode);
+ CloseHandle(pi.hThread);
+ CloseHandle(pi.hProcess);
+ return static_cast<int>(exitCode);
+}
+
+static DWORD WaitForProcess() {
+
+}
+
static bool HaveWriteAccess(const std::wstring &path)
{
bool writable = false;
@@ -389,6 +444,14 @@ int runApplication(MOApplication &application, SingleInstance &instance, // global crashDumpType sits in OrganizerCore to make a bit less ugly to update it when the settings are changed during runtime
OrganizerCore::setGlobalCrashDumpsType(settings.value("Settings/crash_dumps_type", static_cast<int>(CrashDumpsType::Mini)).toInt());
+ qDebug("Loaded settings:");
+ settings.beginGroup("Settings");
+ for (auto k : settings.allKeys())
+ if (!k.contains("username") && !k.contains("password"))
+ qDebug(" %s=%s", k.toUtf8().data(), settings.value(k).toString().toUtf8().data());
+ settings.endGroup();
+
+
qDebug("initializing core");
OrganizerCore organizer(settings);
if (!organizer.bootstrap()) {
@@ -525,20 +588,16 @@ int runApplication(MOApplication &application, SingleInstance &instance, int main(int argc, char *argv[])
{
+ if (argc >= 4) {
+ std::vector<std::wstring> arg;
+ auto args = UntouchedCommandLineArguments(2, arg);
+ if (arg[0] == L"launch")
+ return SpawnWaitProcess(arg[1].c_str(), args);
+ }
+
MOApplication application(argc, argv);
QStringList arguments = application.arguments();
- if ((arguments.length() >= 4) && (arguments.at(1) == "launch")) {
- // all we're supposed to do is launch another process
- QProcess process;
- process.setWorkingDirectory(QDir::fromNativeSeparators(arguments.at(2)));
- process.setProgram(QDir::fromNativeSeparators(arguments.at(3)));
- process.setArguments(arguments.mid(4));
- process.start();
- process.waitForFinished(-1);
- return process.exitCode();
- }
-
setupPath();
#if !defined(QT_NO_SSL)
|
