forked from electronicarts/CnC_Generals_Zero_Hour
-
Notifications
You must be signed in to change notification settings - Fork 245
feat(system): Add -cwd option to keep or override the startup working directory #3149
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
CryoTheRenegade
wants to merge
3
commits into
TheSuperHackers:main
Choose a base branch
from
CryoTheRenegade:feat/startup-working-directory
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
adae3f0
feat(system): Add -cwd option to keep or override the startup working…
CryoTheRenegade d92c83e
fix(system): Restore WorldBuilder buf and harden -cwd directory changes
CryoTheRenegade 04d6eaa
refactor(system): Move startup working directory logic out of Command…
CryoTheRenegade File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,36 @@ | ||
| /* | ||
| ** Command & Conquer Generals Zero Hour(tm) | ||
| ** Copyright 2025 TheSuperHackers | ||
| ** | ||
| ** This program is free software: you can redistribute it and/or modify | ||
| ** it under the terms of the GNU General Public License as published by | ||
| ** the Free Software Foundation, either version 3 of the License, or | ||
| ** (at your option) any later version. | ||
| ** | ||
| ** This program is distributed in the hope that it will be useful, | ||
| ** but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| ** GNU General Public License for more details. | ||
| ** | ||
| ** You should have received a copy of the GNU General Public License | ||
| ** along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
| */ | ||
|
|
||
| #pragma once | ||
|
|
||
| #include "Lib/BaseType.h" | ||
|
|
||
| namespace rts | ||
| { | ||
|
|
||
| // TheSuperHackers @feature 14/08/2026 | ||
| // Startup working directory helpers. By default the process working directory is | ||
| // the executable directory. -cwd keeps the OS directory. -cwd <path> uses that path. | ||
|
|
||
| Bool setCurrentDirectoryToExecutablePath(); | ||
| Bool setCurrentDirectoryToPath(const char *path); | ||
|
|
||
| // For tools that do not parse CommandLine startup flags. | ||
| void applyStartupWorkingDirectory(); | ||
|
|
||
| } // namespace rts |
This file contains hidden or 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 hidden or 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,142 @@ | ||
| /* | ||
| ** Command & Conquer Generals Zero Hour(tm) | ||
| ** Copyright 2025 TheSuperHackers | ||
| ** | ||
| ** This program is free software: you can redistribute it and/or modify | ||
| ** it under the terms of the GNU General Public License as published by | ||
| ** the Free Software Foundation, either version 3 of the License, or | ||
| ** (at your option) any later version. | ||
| ** | ||
| ** This program is distributed in the hope that it will be useful, | ||
| ** but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| ** GNU General Public License for more details. | ||
| ** | ||
| ** You should have received a copy of the GNU General Public License | ||
| ** along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
| */ | ||
|
|
||
| #include "PreRTS.h" // This must go first in EVERY cpp file in the GameEngine | ||
|
|
||
| #include "Common/WorkingDirectory.h" | ||
| #include "WWLib/trim.h" | ||
|
|
||
| namespace rts | ||
| { | ||
|
|
||
| Bool setCurrentDirectoryToExecutablePath() | ||
| { | ||
| Char buffer[_MAX_PATH]; | ||
| const DWORD len = GetModuleFileName(nullptr, buffer, ARRAY_SIZE(buffer)); | ||
| if (len == 0 || len >= ARRAY_SIZE(buffer)) | ||
| { | ||
| DEBUG_LOG(("Failed to get executable path for working directory (error %d)", GetLastError())); | ||
| return FALSE; | ||
| } | ||
|
|
||
| if (Char *pEnd = strrchr(buffer, '\\')) | ||
| { | ||
| *pEnd = 0; | ||
| } | ||
|
|
||
| if (::SetCurrentDirectory(buffer) == 0) | ||
| { | ||
| DEBUG_LOG(("Failed to set working directory to executable path '%s' (error %d)", buffer, GetLastError())); | ||
| return FALSE; | ||
| } | ||
|
|
||
| return TRUE; | ||
| } | ||
|
|
||
| Bool setCurrentDirectoryToPath(const char *path) | ||
| { | ||
| if (path == nullptr || path[0] == '\0') | ||
| return FALSE; | ||
|
|
||
| if (::SetCurrentDirectory(path) == 0) | ||
| { | ||
| DEBUG_LOG(("Failed to set working directory to '%s' (error %d)", path, GetLastError())); | ||
| return FALSE; | ||
| } | ||
|
|
||
| return TRUE; | ||
| } | ||
|
|
||
| static char *nextWorkingDirectoryParam(char *newSource, const char *seps) | ||
| { | ||
| static char *source = nullptr; | ||
| if (newSource) | ||
| { | ||
| source = newSource; | ||
| } | ||
| if (!source) | ||
| { | ||
| return nullptr; | ||
| } | ||
|
|
||
| char *first = source; | ||
| if (first) | ||
| { | ||
| char *firstSep = strpbrk(first, seps); | ||
| char firstChar[2] = {0,0}; | ||
| if (firstSep == first) | ||
| { | ||
| firstChar[0] = *first; | ||
| while (*first == firstChar[0]) first++; | ||
| } | ||
|
|
||
| char *end; | ||
| if (firstChar[0]) | ||
| end = strpbrk(first, firstChar); | ||
| else | ||
| end = strpbrk(first, seps); | ||
|
|
||
| if (end) | ||
| { | ||
| source = end+1; | ||
| *end = 0; | ||
|
|
||
| if (!*source) | ||
| source = nullptr; | ||
| } | ||
| else | ||
| { | ||
| source = nullptr; | ||
| } | ||
|
|
||
| if (first && !*first) | ||
| first = nullptr; | ||
| } | ||
|
|
||
| return first; | ||
| } | ||
|
|
||
| void applyStartupWorkingDirectory() | ||
| { | ||
| std::vector<char*> argv; | ||
| std::string cmdLine = GetCommandLineA(); | ||
| char *token = nextWorkingDirectoryParam(&cmdLine[0], "\" "); | ||
| while (token != nullptr) | ||
| { | ||
| argv.push_back(strtrim(token)); | ||
| token = nextWorkingDirectoryParam(nullptr, "\" "); | ||
| } | ||
|
|
||
| const int argc = (int)argv.size(); | ||
| for (int arg = 1; arg < argc; ++arg) | ||
| { | ||
| if (stricmp(argv[arg], "-cwd") != 0) | ||
| continue; | ||
|
|
||
| if (arg + 1 < argc && argv[arg + 1] != nullptr && argv[arg + 1][0] != '-' && argv[arg + 1][0] != '\0') | ||
| { | ||
| if (!setCurrentDirectoryToPath(argv[arg + 1])) | ||
| setCurrentDirectoryToExecutablePath(); | ||
| } | ||
| return; | ||
| } | ||
|
|
||
| setCurrentDirectoryToExecutablePath(); | ||
| } | ||
|
|
||
| } // namespace rts | ||
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -43,6 +43,7 @@ | |
| #include "WinMain.h" | ||
| #include "Lib/BaseType.h" | ||
| #include "Common/CommandLine.h" | ||
| #include "Common/WorkingDirectory.h" | ||
| #include "Common/CriticalSection.h" | ||
| #include "Common/GlobalData.h" | ||
| #include "Common/GameEngine.h" | ||
|
|
@@ -817,14 +818,9 @@ Int APIENTRY WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, | |
| // initialize the memory manager early | ||
| initMemoryManager(); | ||
|
|
||
| /// @todo remove this force set of working directory later | ||
| Char buffer[ _MAX_PATH ]; | ||
| GetModuleFileName( nullptr, buffer, sizeof( buffer ) ); | ||
| if (Char *pEnd = strrchr(buffer, '\\')) | ||
| { | ||
| *pEnd = 0; | ||
| } | ||
| ::SetCurrentDirectory(buffer); | ||
| CommandLine::parseCommandLineForStartup(); | ||
| if (TheGlobalData->m_changeCurrentWorkingDirectoryToExecutablePath) | ||
| rts::setCurrentDirectoryToExecutablePath(); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why is the call delegated to this location through the flag above? To avoid calling it twice after Can we simplify it and cut the |
||
|
|
||
|
|
||
| #ifdef RTS_DEBUG | ||
|
|
@@ -845,8 +841,6 @@ Int APIENTRY WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, | |
| // Force to be loaded from a file, not a resource so same exe can be used in germany and retail. | ||
| gLoadScreenBitmap = (HBITMAP)LoadImage(hInstance, "Install_Final.bmp", IMAGE_BITMAP, 0, 0, LR_SHARED|LR_LOADFROMFILE); | ||
|
|
||
| CommandLine::parseCommandLineForStartup(); | ||
|
|
||
| #ifdef RTS_ENABLE_CRASHDUMP | ||
| // Initialize minidump facilities - requires TheGlobalData so performed after parseCommandLineForStartup | ||
| MiniDumper::initMiniDumper(TheGlobalData->getPath_UserData()); | ||
|
|
||
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nextWorkingDirectoryParamis identical tonextParamatCommandLine.cpp:1333Both are in Core in the same library, so this can just use the existing one.Also
-cwdnow gets parsed twice by two different paths: the game viaparseCwd+m_changeCurrentWorkingDirectoryToExecutablePath, the tools by re-parsing the raw command line here. Any quoting fix would have to land in both. Can these share one entry point?