Skip to content

feat(system): Add command line option to skip force set of cwd #1445

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
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Generals/Code/GameEngine/Include/Common/GlobalData.h
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,10 @@ class GlobalData : public SubsystemInterface
// Run game without graphics, input or audio.
Bool m_headless;

// TheSuperHackers @feature 11/08/2025
// On startup change the current working directory to the executable's location.
Bool m_changeCurrentWorkingDirectoryToExecutablePath;

Bool m_windowed;
Int m_xResolution;
Int m_yResolution;
Expand Down
10 changes: 10 additions & 0 deletions Generals/Code/GameEngine/Source/Common/CommandLine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -458,6 +458,12 @@ Int parseJobs(char *args[], int num)
return 1;
}

Int parseCwd(char* args[], int num)
{
TheWritableGlobalData->m_changeCurrentWorkingDirectoryToExecutablePath = FALSE;
return 1;
}

Int parseXRes(char *args[], int num)
{
if (num > 1)
Expand Down Expand Up @@ -1163,6 +1169,10 @@ static CommandLineParam paramsForStartup[] =
// (If you have 4 cores, call it with -jobs 4)
// If you do not call this, all replays will be simulated in sequence in the same process.
{ "-jobs", parseJobs },

// TheSuperHackers @feature 11/08/2025
// Use current working directory as provided by the OS.
{ "-cwd", parseCwd },
};

// These Params are parsed during Engine Init before INI data is loaded
Expand Down
1 change: 1 addition & 0 deletions Generals/Code/GameEngine/Source/Common/GlobalData.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -613,6 +613,7 @@ GlobalData::GlobalData()
m_framesPerSecondLimit = 0;
m_chipSetType = 0;
m_headless = FALSE;
m_changeCurrentWorkingDirectoryToExecutablePath = TRUE;
m_windowed = 0;
m_xResolution = 800;
m_yResolution = 600;
Expand Down
28 changes: 16 additions & 12 deletions Generals/Code/Main/WinMain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -764,21 +764,26 @@ 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( NULL, buffer, sizeof( buffer ) );
Char *pEnd = buffer + strlen( buffer );
while( pEnd != buffer )

CommandLine::parseCommandLineForStartup();

if (TheGlobalData->m_changeCurrentWorkingDirectoryToExecutablePath)
{
if( *pEnd == '\\' )
/// @todo remove this force set of working directory later
Char buffer[ _MAX_PATH ];
GetModuleFileName( NULL, buffer, sizeof( buffer ) );
Char *pEnd = buffer + strlen( buffer );
while( pEnd != buffer )
{
*pEnd = 0;
break;
if( *pEnd == '\\' )
{
*pEnd = 0;
break;
}
pEnd--;
}
pEnd--;
::SetCurrentDirectory(buffer);
}
::SetCurrentDirectory(buffer);


#ifdef RTS_DEBUG
Expand All @@ -799,7 +804,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();

// register windows class and create application window
if(!TheGlobalData->m_headless && initializeAppWindows(hInstance, nCmdShow, TheGlobalData->m_windowed) == false)
Expand Down
4 changes: 4 additions & 0 deletions GeneralsMD/Code/GameEngine/Include/Common/GlobalData.h
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,10 @@ class GlobalData : public SubsystemInterface
// Run game without graphics, input or audio.
Bool m_headless;

// TheSuperHackers @feature 11/08/2025
// On startup change the current working directory to the executable's location.
Bool m_changeCurrentWorkingDirectoryToExecutablePath;

Bool m_windowed;
Int m_xResolution;
Int m_yResolution;
Expand Down
10 changes: 10 additions & 0 deletions GeneralsMD/Code/GameEngine/Source/Common/CommandLine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -458,6 +458,12 @@ Int parseJobs(char *args[], int num)
return 1;
}

Int parseCwd(char* args[], int num)
{
TheWritableGlobalData->m_changeCurrentWorkingDirectoryToExecutablePath = FALSE;
return 1;
}

Int parseXRes(char *args[], int num)
{
if (num > 1)
Expand Down Expand Up @@ -1163,6 +1169,10 @@ static CommandLineParam paramsForStartup[] =
// (If you have 4 cores, call it with -jobs 4)
// If you do not call this, all replays will be simulated in sequence in the same process.
{ "-jobs", parseJobs },

// TheSuperHackers @feature 11/08/2025
// Use current working directory as provided by the OS.
{ "-cwd", parseCwd },
};

// These Params are parsed during Engine Init before INI data is loaded
Expand Down
1 change: 1 addition & 0 deletions GeneralsMD/Code/GameEngine/Source/Common/GlobalData.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -618,6 +618,7 @@ GlobalData::GlobalData()
m_framesPerSecondLimit = 0;
m_chipSetType = 0;
m_headless = FALSE;
m_changeCurrentWorkingDirectoryToExecutablePath = TRUE;
m_windowed = 0;
m_xResolution = 800;
m_yResolution = 600;
Expand Down
27 changes: 15 additions & 12 deletions GeneralsMD/Code/Main/WinMain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -792,20 +792,25 @@ 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( NULL, buffer, sizeof( buffer ) );
Char *pEnd = buffer + strlen( buffer );
while( pEnd != buffer )
CommandLine::parseCommandLineForStartup();

if (TheGlobalData->m_changeCurrentWorkingDirectoryToExecutablePath)
{
if( *pEnd == '\\' )
/// @todo remove this force set of working directory later
Char buffer[ _MAX_PATH ];
GetModuleFileName( NULL, buffer, sizeof( buffer ) );
Char *pEnd = buffer + strlen( buffer );
while( pEnd != buffer )
{
*pEnd = 0;
break;
if( *pEnd == '\\' )
{
*pEnd = 0;
break;
}
pEnd--;
}
pEnd--;
::SetCurrentDirectory(buffer);
}
::SetCurrentDirectory(buffer);


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

CommandLine::parseCommandLineForStartup();

// register windows class and create application window
if(!TheGlobalData->m_headless && initializeAppWindows(hInstance, nCmdShow, TheGlobalData->m_windowed) == false)
return exitcode;
Expand Down
Loading