Skip to content

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.

feat(gameclient): console get and set over the options catalog

Olcay Seygan6e1ca34cc545
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 3bab8bfce..42ec55b0f 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -1037,6 +1037,7 @@ A third pass went through the generals' powers, promotions and unit abilities.
- Press the key above Tab and a black panel drops over the top of the screen with a prompt waiting at the bottom of it. Type into it and the game ignores the keys it would otherwise have read as orders, so a word starting with Q does not queue a tank. Escape or the same key again puts it away.
- `help` lists what it takes, `clear` empties it, `echo` says a line back. The up and down arrows walk back through the last thirty-two things you typed.
+- `get` lists every setting the options screen has, under the name it is saved as, with its value and what it takes, and `set` changes one and saves it the way the menu's Accept does: `set ShowNetBox no` takes the info box out of the top right corner, `set ShowSuperweaponStrip no` the superweapon timers. A setting that shows at once changes at once; the rest, such as the window mode or the language, wait for the next launch and the console says so. A typo is refused and named, rather than read as "no" or pushed to the nearest number in range.
- It opens in the main menu as well as in a match.
- Cheats for campaign and skirmish, listed by `cheats`: `money` and `points` hand over cash and general's points, `rankup` raises the general's rank, `heroic` makes every unit you own heroic and `reveal` lifts the shroud for good. `power`, `nocooldown`, `god`, `instantbuild` and `onehitkill` switch on and off: power that never runs short, general's powers and superweapons always ready, your side taking no damage, anything built or trained in an instant, and every hit you land a kill. `money 50000` takes an amount. They are recorded in the replay like any order. In a LAN or online game, a replay or the main menu the console does not offer them at all.
- `trainer` puts every cheat on a steel panel down the left of the screen, the way a trainer would. The on-and-off ones light up green while they are on, cash comes in 1,000, 10,000 and 100,000 at a click, general's points in 1, 5 and 10. A click counts exactly like typing the cheat, replay included. `trainer` again, the X in its corner or Escape puts it away.
diff --git a/GeneralsMD/Code/GameEngine/Include/Common/OptionsCatalog.h b/GeneralsMD/Code/GameEngine/Include/Common/OptionsCatalog.h
index be6fc35dd..4896148f6 100644
--- a/GeneralsMD/Code/GameEngine/Include/Common/OptionsCatalog.h
+++ b/GeneralsMD/Code/GameEngine/Include/Common/OptionsCatalog.h
@@ -51,6 +51,7 @@
// base class keeps it clear of OptionsMenu.cpp - which is where OptionPreferences' constructor
// lives, and which drags in the shell, the audio manager and GameSpy behind it.
class UserPreferences;
+class AsciiString;
//-----------------------------------------------------------------------------
/** How a setting is stored in Options.ini and what kind of control shows it. */
@@ -107,6 +108,14 @@ extern const OptionDef *findOptionDef( const char *iniKey );
/** Clamp a raw value the way the row says. */
extern Int clampOptionValue( const OptionDef& def, Int value );
+/** Typed text -> a value for this row, for the console's "set". Stricter than reading Options.ini:
+ * a bool takes yes/no, true/false, on/off, y/n, t/f or 1/0, a number has to be whole and inside
+ * [lo,hi], and anything else is FALSE with `value` untouched rather than clamped or read as "no". */
+extern Bool parseOptionText( const OptionDef& def, const char *text, Int *value );
+
+/** A value as Options.ini stores it: "yes"/"no" for a bool, the clamped decimal otherwise. */
+extern AsciiString formatOptionValue( const OptionDef& def, Int value );
+
//-----------------------------------------------------------------------------
// Multisampling is stored as an index rather than a sample count, because that is what a combo box
// hands back and because the counts are not a range: 3x and 5x do not exist.
diff --git a/GeneralsMD/Code/GameEngine/Source/Common/OptionsCatalog.cpp b/GeneralsMD/Code/GameEngine/Source/Common/OptionsCatalog.cpp
index 4943faa6b..7d87f4e64 100644
--- a/GeneralsMD/Code/GameEngine/Source/Common/OptionsCatalog.cpp
+++ b/GeneralsMD/Code/GameEngine/Source/Common/OptionsCatalog.cpp
@@ -554,7 +554,34 @@ static Int parseOptionValue( const OptionDef& def, const AsciiString& stored )
}
//-----------------------------------------------------------------------------
-static AsciiString formatOptionValue( const OptionDef& def, Int value )
+Bool parseOptionText( const OptionDef& def, const char *text, Int *value )
+{
+ if( def.kind == OPTION_BOOL )
+ {
+ // off and on in pairs, so a word's index modulo two is its value
+ static const char *const TheBoolWords[] = { "no", "yes", "false", "true", "off", "on", "n", "y", "f", "t", "0", "1" };
+ for( Int i = 0; i < (Int)( sizeof( TheBoolWords ) / sizeof( TheBoolWords[ 0 ] ) ); ++i )
+ {
+ if( strcasecmp( text, TheBoolWords[ i ] ) == 0 )
+ {
+ *value = i % 2;
+ return TRUE;
+ }
+ }
+ return FALSE;
+ }
+
+ char *end;
+ const long number = strtol( text, &end, 10 );
+ if( end == text || *end != '\0' || number < def.lo || number > def.hi )
+ return FALSE;
+
+ *value = (Int)number;
+ return TRUE;
+}
+
+//-----------------------------------------------------------------------------
+AsciiString formatOptionValue( const OptionDef& def, Int value )
{
if( def.kind == OPTION_BOOL )
return AsciiString( value ? "yes" : "no" );
diff --git a/GeneralsMD/Code/GameEngine/Source/GameClient/GameConsole.cpp b/GeneralsMD/Code/GameEngine/Source/GameClient/GameConsole.cpp
index 2e24133b1..be5e5384f 100644
--- a/GeneralsMD/Code/GameEngine/Source/GameClient/GameConsole.cpp
+++ b/GeneralsMD/Code/GameEngine/Source/GameClient/GameConsole.cpp
@@ -30,9 +30,11 @@
#include "Common/FileSystem.h"
#include "Common/GameEngine.h"
#include "Common/MessageStream.h"
+#include "Common/OptionsCatalog.h"
#include "Common/Player.h"
#include "Common/PlayerList.h"
#include "Common/Recorder.h"
+#include "Common/UserPreferences.h"
#include "GameLogic/GameLogic.h"
#include "GameNetwork/GameSpy/ThreadUtils.h"
@@ -168,6 +170,67 @@ static AsciiString runSpeed( AsciiString arguments )
return result;
}
+//-------------------------------------------------------------------------------------------------
+/** "Key = value (range)" for a row of TheOptionCatalog, the value this run is using. */
+//-------------------------------------------------------------------------------------------------
+static AsciiString describeOption( const OptionDef &def )
+{
+ AsciiString result;
+ if( def.kind == OPTION_BOOL )
+ result.format( "%s = %s (yes/no)", def.iniKey, formatOptionValue( def, def.get() ).str() );
+ else
+ result.format( "%s = %s (%d..%d)", def.iniKey, formatOptionValue( def, def.get() ).str(), def.lo, def.hi );
+ return result;
+}
+
+static AsciiString unknownOption( const char *command, AsciiString key )
+{
+ AsciiString result;
+ result.format( "%s: no setting called '%s'; 'get' lists them", command, key.str() );
+ return result;
+}
+
+//-------------------------------------------------------------------------------------------------
+/** Any setting in the options catalog by its Options.ini key, written to Options.ini the way the
+ * options menu's Accept writes it. A row that shows the moment its GlobalData field changes is
+ * changed now too; the rest wait for the next launch, because the device reset or shell rebuild
+ * the menu runs after them is not the console's to start. */
+//-------------------------------------------------------------------------------------------------
+static AsciiString runSetOption( AsciiString arguments )
+{
+ AsciiString key;
+ arguments.nextToken( &key );
+ arguments.trim();
+
+ const OptionDef *def = findOptionDef( key.str() );
+ if( def == NULL )
+ return unknownOption( "set", key );
+
+ AsciiString result;
+ Int value;
+ if( !parseOptionText( *def, arguments.str(), &value ) )
+ {
+ if( def->kind == OPTION_BOOL )
+ result.format( "set: %s takes yes or no, not '%s'", def->iniKey, arguments.str() );
+ else
+ result.format( "set: %s takes a whole number from %d to %d, not '%s'", def->iniKey, def->lo, def->hi, arguments.str() );
+ return result;
+ }
+
+ OptionPreferences pref;
+ pref[ AsciiString( def->iniKey ) ] = formatOptionValue( *def, value );
+ pref.write();
+
+ if( def->apply != APPLY_LIVE )
+ {
+ result.format( "%s = %s saved, from the next launch", def->iniKey, formatOptionValue( *def, value ).str() );
+ return result;
+ }
+
+ def->set( value );
+ return describeOption( *def );
+}
+
//-------------------------------------------------------------------------------------------------
GameConsole::GameConsole()
: m_isOpen( FALSE ),
@@ -338,6 +401,9 @@ void GameConsole::runCommand( AsciiString commandLine )
printLine( AsciiString( "clear empty the scrollback" ) );
printLine( AsciiString( "echo <text> print the text back" ) );
printLine( AsciiString( "speed [n] game speed in percent, 100 is normal, 'reset' goes back" ) );
+ printLine( AsciiString( "get [key] a setting by its Options.ini key; no key lists them all" ) );
+ printLine( AsciiString( "set <key> <v> change a setting and save it, e.g. 'set ShowNetBox no' hides the" ) );
+ printLine( AsciiString( " top right info box, 'set ShowSuperweaponStrip no' the superweapon timers" ) );
if( areCheatsAvailable() )
{
printLine( AsciiString( "cheats single-player cheats" ) );
@@ -392,6 +458,25 @@ void GameConsole::runCommand( AsciiString commandLine )
return;
}
+ if( command == "get" )
+ {
+ if( arguments.isEmpty() )
+ {
+ for( Int i = 0; i < TheOptionCatalogCount; ++i )
+ printLine( describeOption( TheOptionCatalog[ i ] ) );
+ return;
+ }
+ const OptionDef *def = findOptionDef( arguments.str() );
+ printLine( def == NULL ? unknownOption( "get", arguments ) : describeOption( *def ) );
+ return;
+ }
+
+ if( command == "set" )
+ {
+ printLine( runSetOption( arguments ) );
+ return;
+ }
+
UnicodeString unknown( u"unknown command: " );
UnicodeString name;
name.translate( command );
diff --git a/GeneralsMD/Code/Tests/test_gameengine.cpp b/GeneralsMD/Code/Tests/test_gameengine.cpp
index c42888c2f..00ef3fd81 100644
--- a/GeneralsMD/Code/Tests/test_gameengine.cpp
+++ b/GeneralsMD/Code/Tests/test_gameengine.cpp
@@ -12924,6 +12924,42 @@ TEST(net_box_is_a_check_box_that_starts_on_under_its_own_key)
delete scratch;
}
+/* The console's "set" types a value rather than reading one back from Options.ini, so a typo has to
+ be refused instead of turning a box off ("set ShowNetBox maybe" read leniently is "no") or being
+ clamped into some other number. */
+TEST(console_set_parses_only_what_the_row_takes)
+{
+ const OptionDef *box = findOptionDef( "ShowNetBox" );
+ const OptionDef *drag = findOptionDef( "DragTolerance" );
+ CHECK( box != NULL && drag != NULL );
+ if( box == NULL || drag == NULL )
+ return;
+
+ Int value = -1;
+ CHECK( parseOptionText( *box, "no", &value ) && value == 0 );
+ CHECK( parseOptionText( *box, "ON", &value ) && value == 1 );
+ CHECK( parseOptionText( *box, "0", &value ) && value == 0 );
+ CHECK( parseOptionText( *box, "true", &value ) && value == 1 );
+ value = 7;
+ CHECK( !parseOptionText( *box, "maybe", &value ) );
+ CHECK( !parseOptionText( *box, "", &value ) );
+ CHECK( !parseOptionText( *box, "2", &value ) );
+ CHECK_EQ( value, 7 );
+
+ CHECK( parseOptionText( *drag, "2", &value ) && value == 2 );
+ CHECK( parseOptionText( *drag, "50", &value ) && value == 50 );
+ value = 7;
+ CHECK( !parseOptionText( *drag, "1", &value ) );
+ CHECK( !parseOptionText( *drag, "51", &value ) );
+ CHECK( !parseOptionText( *drag, "10px", &value ) );
+ CHECK( !parseOptionText( *drag, "", &value ) );
+ CHECK_EQ( value, 7 );
+
+ CHECK( formatOptionValue( *box, 0 ) == "no" );
+ CHECK( formatOptionValue( *drag, 12 ) == "12" );
+ CHECK( findOptionDef( "showsuperweaponstrip" ) != NULL );
+}
+
/* The income beside the money, per second as it always was until the player picks otherwise.
Automatic is the one with a rule in it: under ten dollars a second the whole number rounds most
of the income away, $135 a minute reading "+2/s", so that is where it goes per minute. */