-
Notifications
You must be signed in to change notification settings - Fork 225
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Game exe path command line argument (#713)
* feat: --exePath commandline argument ex. `SkyrimTogether.exe --exePath C:\Path\To\Game\SkyrimSE.exe` * tweak: add explicit target exe error * tweak: clang format * fix: properly terminate process when no exe path given * tweak: safer args bounds check + more explicit errors * tweak: remove global exePath & titlePath * tweak: clang format * tweak: const naming
- Loading branch information
1 parent
f142880
commit ab18db9
Showing
4 changed files
with
124 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
#include "PathArgument.h" | ||
|
||
#include "TargetConfig.h" | ||
|
||
#include "Utils/Error.h" | ||
#include "utils/Registry.h" | ||
|
||
#include <filesystem> | ||
|
||
namespace oobe | ||
{ | ||
using namespace TiltedPhoques; | ||
|
||
namespace | ||
{ | ||
constexpr wchar_t kTiltedRegistryPath[] = LR"(Software\TiltedPhoques\TiltedEvolution\)" SHORT_NAME; | ||
|
||
#define DIE_NOW(err) \ | ||
{ \ | ||
Die(err, true); \ | ||
return false; \ | ||
} | ||
|
||
bool ValidatePath(const std::wstring& acPath) | ||
{ | ||
const std::wstring cTitlePath = acPath.substr(0, acPath.find_last_of('\\')); | ||
std::wstring errorText{}; | ||
|
||
if (acPath.find_last_of('\\') == std::string::npos || acPath.ends_with(*"\\")) | ||
{ | ||
SetLastError(ERROR_BAD_PATHNAME); | ||
errorText += L"Invalid path\n"; | ||
} | ||
|
||
if (!acPath.ends_with(L".exe")) | ||
{ | ||
SetLastError(ERROR_BAD_ARGUMENTS); | ||
errorText += acPath.substr(acPath.find_last_of('\\') + 1, acPath.back()) + L" is not an executable file\n"; | ||
} | ||
else if (!acPath.ends_with(TARGET_NAME L".exe")) | ||
{ | ||
SetLastError(ERROR_FILE_NOT_FOUND); | ||
errorText += TARGET_NAME L".exe not found\n"; | ||
} | ||
|
||
if (!std::filesystem::exists(acPath) || !std::filesystem::exists(cTitlePath)) | ||
{ | ||
SetLastError(ERROR_BAD_PATHNAME); | ||
errorText += L"Path does not exist\n"; | ||
} | ||
|
||
if (!errorText.empty()) | ||
{ | ||
errorText += L"\nPath: " + acPath; | ||
DIE_NOW(errorText.c_str()) | ||
} | ||
|
||
return true; | ||
} | ||
} // namespace | ||
|
||
bool PathArgument(const std::string& acPath) | ||
{ | ||
const std::wstring cExePath = std::wstring(acPath.begin(), acPath.end()); | ||
const std::wstring cTitlePath = cExePath.substr(0, cExePath.find_last_of('\\')); | ||
|
||
if (!ValidatePath(cExePath)) | ||
{ | ||
DIE_NOW(L"Failed to validate path") | ||
} | ||
|
||
// Write to registry so oobe::SelectInstall can handle the rest | ||
const bool result = Registry::WriteString<wchar_t>(HKEY_CURRENT_USER, kTiltedRegistryPath, L"TitlePath", cTitlePath) && Registry::WriteString<wchar_t>(HKEY_CURRENT_USER, kTiltedRegistryPath, L"TitleExe", cExePath); | ||
|
||
if (!result) | ||
{ | ||
DIE_NOW(L"Failed to write to registry") | ||
} | ||
|
||
return true; | ||
} | ||
} // namespace oobe |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
#pragma once | ||
|
||
#include <string> | ||
|
||
namespace oobe | ||
{ | ||
bool PathArgument(const std::string& acPath); | ||
} |