Skip to content

Commit ea73fb3

Browse files
committed
feature: Add command line option to skip force set of current directory to executable path
1 parent 0d850ca commit ea73fb3

File tree

8 files changed

+61
-24
lines changed

8 files changed

+61
-24
lines changed

Generals/Code/GameEngine/Include/Common/GlobalData.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,10 @@ class GlobalData : public SubsystemInterface
121121
// Run game without graphics, input or audio.
122122
Bool m_headless;
123123

124+
// TheSuperHackers @feature 11/08/2025
125+
// On startup change the current working directory to the executable's location.
126+
Bool m_changeCurrentWorkingDirectoryToExecutablePath;
127+
124128
Bool m_windowed;
125129
Int m_xResolution;
126130
Int m_yResolution;

Generals/Code/GameEngine/Source/Common/CommandLine.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -458,6 +458,12 @@ Int parseJobs(char *args[], int num)
458458
return 1;
459459
}
460460

461+
Int parseCwd(char* args[], int num)
462+
{
463+
TheWritableGlobalData->m_changeCurrentWorkingDirectoryToExecutablePath = FALSE;
464+
return 1;
465+
}
466+
461467
Int parseXRes(char *args[], int num)
462468
{
463469
if (num > 1)
@@ -1163,6 +1169,10 @@ static CommandLineParam paramsForStartup[] =
11631169
// (If you have 4 cores, call it with -jobs 4)
11641170
// If you do not call this, all replays will be simulated in sequence in the same process.
11651171
{ "-jobs", parseJobs },
1172+
1173+
// TheSuperHackers @feature 11/08/2025
1174+
// Use current working directory as provided by the OS.
1175+
{ "-cwd", parseCwd },
11661176
};
11671177

11681178
// These Params are parsed during Engine Init before INI data is loaded

Generals/Code/GameEngine/Source/Common/GlobalData.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -613,6 +613,7 @@ GlobalData::GlobalData()
613613
m_framesPerSecondLimit = 0;
614614
m_chipSetType = 0;
615615
m_headless = FALSE;
616+
m_changeCurrentWorkingDirectoryToExecutablePath = TRUE;
616617
m_windowed = 0;
617618
m_xResolution = 800;
618619
m_yResolution = 600;

Generals/Code/Main/WinMain.cpp

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -764,21 +764,26 @@ Int APIENTRY WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
764764

765765
// initialize the memory manager early
766766
initMemoryManager();
767-
768-
/// @todo remove this force set of working directory later
769-
Char buffer[ _MAX_PATH ];
770-
GetModuleFileName( NULL, buffer, sizeof( buffer ) );
771-
Char *pEnd = buffer + strlen( buffer );
772-
while( pEnd != buffer )
767+
768+
CommandLine::parseCommandLineForStartup();
769+
770+
if (TheGlobalData->m_changeCurrentWorkingDirectoryToExecutablePath)
773771
{
774-
if( *pEnd == '\\' )
772+
/// @todo remove this force set of working directory later
773+
Char buffer[ _MAX_PATH ];
774+
GetModuleFileName( NULL, buffer, sizeof( buffer ) );
775+
Char *pEnd = buffer + strlen( buffer );
776+
while( pEnd != buffer )
775777
{
776-
*pEnd = 0;
777-
break;
778+
if( *pEnd == '\\' )
779+
{
780+
*pEnd = 0;
781+
break;
782+
}
783+
pEnd--;
778784
}
779-
pEnd--;
785+
::SetCurrentDirectory(buffer);
780786
}
781-
::SetCurrentDirectory(buffer);
782787

783788

784789
#ifdef RTS_DEBUG
@@ -799,7 +804,6 @@ Int APIENTRY WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
799804
// Force to be loaded from a file, not a resource so same exe can be used in germany and retail.
800805
gLoadScreenBitmap = (HBITMAP)LoadImage(hInstance, "Install_Final.bmp", IMAGE_BITMAP, 0, 0, LR_SHARED|LR_LOADFROMFILE);
801806

802-
CommandLine::parseCommandLineForStartup();
803807

804808
// register windows class and create application window
805809
if(!TheGlobalData->m_headless && initializeAppWindows(hInstance, nCmdShow, TheGlobalData->m_windowed) == false)

GeneralsMD/Code/GameEngine/Include/Common/GlobalData.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,10 @@ class GlobalData : public SubsystemInterface
124124
// Run game without graphics, input or audio.
125125
Bool m_headless;
126126

127+
// TheSuperHackers @feature 11/08/2025
128+
// On startup change the current working directory to the executable's location.
129+
Bool m_changeCurrentWorkingDirectoryToExecutablePath;
130+
127131
Bool m_windowed;
128132
Int m_xResolution;
129133
Int m_yResolution;

GeneralsMD/Code/GameEngine/Source/Common/CommandLine.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -458,6 +458,12 @@ Int parseJobs(char *args[], int num)
458458
return 1;
459459
}
460460

461+
Int parseCwd(char* args[], int num)
462+
{
463+
TheWritableGlobalData->m_changeCurrentWorkingDirectoryToExecutablePath = FALSE;
464+
return 1;
465+
}
466+
461467
Int parseXRes(char *args[], int num)
462468
{
463469
if (num > 1)
@@ -1163,6 +1169,10 @@ static CommandLineParam paramsForStartup[] =
11631169
// (If you have 4 cores, call it with -jobs 4)
11641170
// If you do not call this, all replays will be simulated in sequence in the same process.
11651171
{ "-jobs", parseJobs },
1172+
1173+
// TheSuperHackers @feature 11/08/2025
1174+
// Use current working directory as provided by the OS.
1175+
{ "-cwd", parseCwd },
11661176
};
11671177

11681178
// These Params are parsed during Engine Init before INI data is loaded

GeneralsMD/Code/GameEngine/Source/Common/GlobalData.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -618,6 +618,7 @@ GlobalData::GlobalData()
618618
m_framesPerSecondLimit = 0;
619619
m_chipSetType = 0;
620620
m_headless = FALSE;
621+
m_changeCurrentWorkingDirectoryToExecutablePath = TRUE;
621622
m_windowed = 0;
622623
m_xResolution = 800;
623624
m_yResolution = 600;

GeneralsMD/Code/Main/WinMain.cpp

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -792,20 +792,25 @@ Int APIENTRY WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
792792
// initialize the memory manager early
793793
initMemoryManager();
794794

795-
/// @todo remove this force set of working directory later
796-
Char buffer[ _MAX_PATH ];
797-
GetModuleFileName( NULL, buffer, sizeof( buffer ) );
798-
Char *pEnd = buffer + strlen( buffer );
799-
while( pEnd != buffer )
795+
CommandLine::parseCommandLineForStartup();
796+
797+
if (TheGlobalData->m_changeCurrentWorkingDirectoryToExecutablePath)
800798
{
801-
if( *pEnd == '\\' )
799+
/// @todo remove this force set of working directory later
800+
Char buffer[ _MAX_PATH ];
801+
GetModuleFileName( NULL, buffer, sizeof( buffer ) );
802+
Char *pEnd = buffer + strlen( buffer );
803+
while( pEnd != buffer )
802804
{
803-
*pEnd = 0;
804-
break;
805+
if( *pEnd == '\\' )
806+
{
807+
*pEnd = 0;
808+
break;
809+
}
810+
pEnd--;
805811
}
806-
pEnd--;
812+
::SetCurrentDirectory(buffer);
807813
}
808-
::SetCurrentDirectory(buffer);
809814

810815

811816
#ifdef RTS_DEBUG
@@ -846,8 +851,6 @@ Int APIENTRY WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
846851
gLoadScreenBitmap = (HBITMAP)LoadImage(hInstance, "Install_Final.bmp", IMAGE_BITMAP, 0, 0, LR_SHARED|LR_LOADFROMFILE);
847852
#endif
848853

849-
CommandLine::parseCommandLineForStartup();
850-
851854
// register windows class and create application window
852855
if(!TheGlobalData->m_headless && initializeAppWindows(hInstance, nCmdShow, TheGlobalData->m_windowed) == false)
853856
return exitcode;

0 commit comments

Comments
 (0)