Zero Hour rebuilt from EA's source: x64, Direct3D 11, VS2022/CMake, ~60 original bugs fixed, an AI that builds a base. Bring your own game.
C++ cmakecnccommand-and-conquercppdirectx11game-enginegame-preservationgeneralsrtssage-enginex64zero-hour
feat(gameengine): run two copies of the game at once (-multiInstance)
Olcay Seygan0d4181f75225
diff --git a/GeneralsMD/Code/GameEngine/Include/Common/EarlyCommandLine.h b/GeneralsMD/Code/GameEngine/Include/Common/EarlyCommandLine.hnew file mode 100644index 000000000..1c7a77b55--- /dev/null+++ b/GeneralsMD/Code/GameEngine/Include/Common/EarlyCommandLine.h@@ -0,0 +1,80 @@+/*+** Command & Conquer Generals Zero Hour(tm)+** Copyright 2025 Electronic Arts Inc.+**+** 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++// EarlyCommandLine.h+//+// A couple of options have to be read before CommandLine.cpp's parser exists: the debug log's file+// name is chosen by a static constructor (preMainInitMemoryManager -> DEBUG_INIT), and the+// one-copy-at-a-time mutex is taken in WinMain long before TheGlobalData is around to hold a flag.+//+// Both read the *wide* command line on purpose. WinMain tokenizes lpCmdLine in place and that+// buffer is the process's ANSI command line, so GetCommandLineA is truncated to the exe name and+// the first switch by the time anything asks. GetCommandLineW is untouched.++#include <windows.h>+#include <wchar.h>+#include <wctype.h>++/** The text just past an option in a command line, or NULL if it does not carry it. An option is+ only an option on a word boundary, so "-win" does not match inside "-window". */+inline const wchar_t *findCommandLineOptionIn( const wchar_t *cmdLine, const wchar_t *option )+{+ if (cmdLine == NULL)+ return NULL;++ const size_t len = wcslen( option );+ for (const wchar_t *at = cmdLine; *at != 0; ++at)+ {+ if ((at == cmdLine || iswspace( at[-1] )) &&+ _wcsnicmp( at, option, len ) == 0 &&+ (at[len] == 0 || iswspace( at[len] )))+ return at + len;+ }+ return NULL;+}++/** The word after an option, if there is one and it is not itself an option. */+inline bool findCommandLineValueIn( const wchar_t *cmdLine, const wchar_t *option, char *out, size_t outSize )+{+ const wchar_t *at = findCommandLineOptionIn( cmdLine, option );+ if (at == NULL || outSize == 0)+ return false;++ while (iswspace( *at ))+ ++at;+ if (*at == 0 || *at == L'-')+ return false;++ size_t i = 0;+ while (*at != 0 && !iswspace( *at ) && i + 1 < outSize)+ out[i++] = (char)*at++;+ out[i] = 0;+ return i > 0;+}++inline const wchar_t *findEarlyCommandLineOption( const wchar_t *option )+{+ return findCommandLineOptionIn( GetCommandLineW(), option );+}++inline bool findEarlyCommandLineValue( const wchar_t *option, char *out, size_t outSize )+{+ return findCommandLineValueIn( GetCommandLineW(), option, out, outSize );+}diff --git a/GeneralsMD/Code/GameEngine/Source/Common/System/Debug.cpp b/GeneralsMD/Code/GameEngine/Source/Common/System/Debug.cppindex 106a60a19..5fe2f6486 100644--- a/GeneralsMD/Code/GameEngine/Source/Common/System/Debug.cpp+++ b/GeneralsMD/Code/GameEngine/Source/Common/System/Debug.cpp@@ -52,6 +52,7 @@#include "Common/CriticalSection.h"#endif#include "Common/Debug.h"+#include "Common/EarlyCommandLine.h"#include "Common/SystemInfo.h"#include "Common/UnicodeString.h"#include "GameClient/GameText.h"@@ -354,11 +355,22 @@ void DebugInit(int flags)char prevbuf[ _MAX_PATH ];char curbuf[ _MAX_PATH ];+ /* -logPrefix <tag> names this run's log. Two copies of the game on one machine - which is+ what testing a network game here comes down to, MULTIPLAYER.md section 6 - otherwise open+ the same file with "w" and only one of them keeps it. It is read here rather than in+ WinMain because a static constructor gets here first: preMainInitMemoryManager() calls+ DEBUG_INIT before main, and by the time WinMain could set gAppPrefix the file is open. */+ char logPrefix[ 32 ];+ if (!findEarlyCommandLineValue( L"-logPrefix", logPrefix, sizeof( logPrefix ) ))+ logPrefix[0] = 0;+strcpy(prevbuf, dirbuf);strcat(prevbuf, gAppPrefix);+ strcat(prevbuf, logPrefix);strcat(prevbuf, DEBUG_FILE_NAME_PREV);strcpy(curbuf, dirbuf);strcat(curbuf, gAppPrefix);+ strcat(curbuf, logPrefix);strcat(curbuf, DEBUG_FILE_NAME);remove(prevbuf);diff --git a/GeneralsMD/Code/Main/WinMain.cpp b/GeneralsMD/Code/Main/WinMain.cppindex 0db321505..f7073962e 100644--- a/GeneralsMD/Code/Main/WinMain.cpp+++ b/GeneralsMD/Code/Main/WinMain.cpp@@ -48,6 +48,7 @@#include "Common/GameEngine.h"#include "Common/GameSounds.h"#include "Common/Debug.h"+#include "Common/EarlyCommandLine.h"#include "Common/GameMemory.h"#include "Common/SafeDisc/CdaPfn.h"#include "Common/StackDump.h"@@ -1056,8 +1057,13 @@ Int APIENTRY WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,//Create a mutex with a unique name to Generals in order to determine if//our app is already running.//WARNING: DO NOT use this number for any other application except Generals.+ /* -multiInstance lets a second copy start. One copy at a time is right for a player and+ wrong for a test: a network game needs two machines, and on this one that means two+ processes, each bound to its own address out of 127.0.0.0/8. */+ const Bool oneCopyIsEnough = (findEarlyCommandLineOption( L"-multiInstance" ) == NULL);+GeneralsMutex = CreateMutex(NULL, FALSE, GENERALS_GUID);- if (GetLastError() == ERROR_ALREADY_EXISTS)+ if (oneCopyIsEnough && GetLastError() == ERROR_ALREADY_EXISTS){HWND ccwindow = FindWindow(GENERALS_GUID, NULL);if (ccwindow)diff --git a/GeneralsMD/Code/Tests/test_gameengine.cpp b/GeneralsMD/Code/Tests/test_gameengine.cppindex 7fb38942c..30f539bfb 100644--- a/GeneralsMD/Code/Tests/test_gameengine.cpp+++ b/GeneralsMD/Code/Tests/test_gameengine.cpp@@ -49,6 +49,7 @@#include "Common/AudioRandomValue.h"#include "GameNetwork/CrcAgreement.h"#include "GameLogic/GameLogic.h"+#include "Common/EarlyCommandLine.h"#include "Common/RadarShroudCache.h"#include "GameClient/Gadget.h"#include "GameNetwork/NetworkUtil.h"@@ -5325,3 +5326,38 @@ TEST(a_replay_game_is_never_itself_recorded)CHECK( !isRecordableGameMode( GAME_NONE + 1 ) );CHECK( !isRecordableGameMode( -1 ) );}++TEST(an_option_read_before_the_parser_exists_is_read_on_word_boundaries)+{+ /* Two options are read straight off the wide command line, before CommandLine.cpp's parser is+ alive: -logPrefix (the debug log's name is chosen by a static constructor) and -multiInstance+ (the one-copy mutex is taken in WinMain). The reader is the whole of that parser. */+ const wchar_t *line = L"\"D:\Run\generals.exe\" -win -multiInstance -logPrefix b_ -maxframes 600";++ CHECK( findCommandLineOptionIn( line, L"-win" ) != NULL );+ CHECK( findCommandLineOptionIn( line, L"-multiInstance" ) != NULL );+ CHECK( findCommandLineOptionIn( line, L"-headless" ) == NULL );+ CHECK( findCommandLineOptionIn( NULL, L"-win" ) == NULL );++ // case does not matter, and an option is only an option on a word boundary+ CHECK( findCommandLineOptionIn( line, L"-LOGPREFIX" ) != NULL );+ CHECK( findCommandLineOptionIn( line, L"-log" ) == NULL ); // not a prefix of a longer word+ CHECK( findCommandLineOptionIn( L"x-win", L"-win" ) == NULL ); // nor a tail of one++ char value[ 32 ];+ CHECK( findCommandLineValueIn( line, L"-logPrefix", value, sizeof( value ) ) );+ CHECK_STR( value, "b_" );+ CHECK( findCommandLineValueIn( line, L"-maxframes", value, sizeof( value ) ) );+ CHECK_STR( value, "600" );++ // an option with nothing after it, or with another option after it, has no value+ CHECK( !findCommandLineValueIn( line, L"-multiInstance", value, sizeof( value ) ) );+ CHECK( !findCommandLineValueIn( L"-logPrefix", L"-logPrefix", value, sizeof( value ) ) );+ CHECK( !findCommandLineValueIn( line, L"-nothere", value, sizeof( value ) ) );++ // and a value longer than the buffer is cut to fit, with room kept for the terminator+ char tiny[ 3 ]; // not "small": rpcndr.h has that as a #define for char+ CHECK( findCommandLineValueIn( L"-logPrefix abcdef", L"-logPrefix", tiny, sizeof( tiny ) ) );+ CHECK_STR( tiny, "ab" );+ CHECK( !findCommandLineValueIn( L"-logPrefix abcdef", L"-logPrefix", tiny, 0 ) );+}