| 90 | } |
| 91 | |
| 92 | int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { |
| 93 | std::wstring cmdLine = ::ConvertLPSTRToWString(lpCmdLine); |
| 94 | DebugMode = cmdLine.find(L"--debug") != std::wstring::npos; |
| 95 | |
| 96 | ::DebugLog(L"__________________________________________________"); |
| 97 | ::DebugLog((L"Command line: " + cmdLine).c_str()); |
| 98 | |
| 99 | wchar_t insomniaExecutable[MAX_PATH]; |
| 100 | ::GetModuleFileNameW(NULL, insomniaExecutable, sizeof(insomniaExecutable)); |
| 101 | ::DebugLog((L"Insomnia executable: " + std::wstring(insomniaExecutable)).c_str()); |
| 102 | |
| 103 | std::wstring workDir(insomniaExecutable); |
| 104 | workDir = workDir.substr(0, workDir.find_last_of(L"\\")); |
| 105 | ::DebugLog((L"Current path: " + workDir).c_str()); |
| 106 | |
| 107 | std::wstring updatePath(workDir); |
| 108 | // get one directory above |
| 109 | updatePath = updatePath.substr(0, updatePath.find_last_of(L"\\")) + L"\\Update.exe"; |
| 110 | updatePath = QuotePathIfNeeded(updatePath); |
| 111 | ::DebugLog((L"Update path: " + updatePath).c_str()); |
| 112 | |
| 113 | // preserve the console output from the original executable |
| 114 | ::AttachConsole(-1); |
| 115 | |
| 116 | HANDLE stdHandle = ::GetStdHandle(STD_OUTPUT_HANDLE); |
| 117 | ::WriteConsoleW(stdHandle, L"Insomnia is starting...\n", 24, NULL, NULL); |
| 118 | ::WriteConsoleW(stdHandle, (L"Command line arguments: " + cmdLine + L"\n").c_str(), |
| 119 | cmdLine.size() + 25, NULL, NULL); |
| 120 | |
| 121 | if (cmdLine.find(SQUIRREL_INSTALL) != std::wstring::npos) { |
| 122 | ::DebugLog(L"Squirrel.Windows install"); |
| 123 | |
| 124 | // Squirrel.Windows install |
| 125 | std::wstring shortcut = QuotePathIfNeeded(insomniaExecutable); |
| 126 | std::wstring shortcutTarget = std::filesystem::path(shortcut).filename().wstring(); |
| 127 | ::DebugLog((L"Shortcut target: " + shortcutTarget).c_str()); |
| 128 | std::wstring args = std::wstring(L"--createShortcut=") + shortcutTarget; |
| 129 | ::ShellExecuteW(0, L"open", updatePath.c_str(), args.c_str(), NULL, SW_HIDE); |
| 130 | |
| 131 | return 0; |
| 132 | } else if (cmdLine.find(SQUIRREL_UPDATED) != std::wstring::npos || |
| 133 | cmdLine.find(SQUIRREL_OBSOLETE) != std::wstring::npos) { |
| 134 | ::DebugLog(L"Squirrel.Windows updated or obsoleted"); |
| 135 | // Squirrel.Windows update |
| 136 | return 0; |
| 137 | } else if (cmdLine.find(SQUIRREL_UNINSTALL) != std::wstring::npos) { |
| 138 | // Squirrel.Windows uninstall |
| 139 | std::wstring shortcut = QuotePathIfNeeded(insomniaExecutable); |
| 140 | std::wstring shortcutTarget = std::filesystem::path(shortcut).filename().wstring(); |
| 141 | ::DebugLog((L"Shortcut target: " + shortcutTarget).c_str()); |
| 142 | std::wstring args = std::wstring(L"--removeShortcut=") + shortcutTarget; |
| 143 | ::ShellExecuteW(0, L"open", updatePath.c_str(), args.c_str(), NULL, SW_HIDE); |
| 144 | ::DebugLog(L"Squirrel.Windows uninstall"); |
| 145 | |
| 146 | return 0; |
| 147 | } else if (cmdLine.find(SQUIRREL_FIRST_RUN) != std::wstring::npos) { |
| 148 | // Squirrel.Windows first run |
| 149 | ::DebugLog(L"Squirrel.Windows first run"); |
nothing calls this directly
no test coverage detected