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): mod buttons take their own keys from hotkeys.ini

Olcay Seygan71870d7b7952
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 69fc41aa6..4543bb2ee 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -32,7 +32,7 @@ found and fixed — EA's own, not port damage.**
## One interface
- The choice between a Classic and a Reforged interface is gone. There is one, laid out the way Zero Hour shipped, and the orders Reforged added now live on the keys you already know: A attack-moves, holding Ctrl fires on whatever is under the cursor as it did in 2003, a guard order can cover a patch of ground instead of one spot, and orders queue up one after another. Everything your base is building stands on the screen as a line of text each, the six closest to done first and "+N more" for the rest. Superweapon countdowns are listed the same way, up to eighteen. Both lists are in bold type, the build list in white and each countdown in its owner's colour. The promotion screen no longer darkens the battlefield behind it. Whether the right button gives orders, or the left as in 1.04, is a setting in the launcher.
-- Every key is yours to move. The launcher's Hotkeys page draws the command bar of every building and unit each general can field, cameos and letters as the game shows them, and a click and a keypress gives any button a new letter or none. Two buttons on one bar that would share a letter are marked before you play. The keyboard commands are there too, from bookmarks and team keys to the camera, along with the ones Zero Hour never bound: select every fighting unit, vehicle or aircraft, every barracks, war factory or airfield, the next idle worker or unit, pause, the general's promotions, the three signals to your allies and the general's powers. Your keys are yours alone; the other players in a match keep theirs, and a match never refuses you for them.
+- Every key is yours to move. The launcher's Hotkeys page draws the command bar of every building and unit each general can field, cameos and letters as the game shows them, and a click and a keypress gives any button a new letter or none. Two buttons on one bar that would share a letter are marked before you play. The keyboard commands are there too, from bookmarks and team keys to the camera, along with the ones Zero Hour never bound: select every fighting unit, vehicle or aircraft, every barracks, war factory or airfield, the next idle worker or unit, pause, the general's promotions, the three signals to your allies and the general's powers. Your keys are yours alone; the other players in a match keep theirs, and a match never refuses you for them. Every mod you install brings its own generals to the page, ShockWave's fifteen next to Zero Hour's thirteen, and a key you give a ShockWave button stays with ShockWave.
- Some keys now mean one thing on every bar: A attack move, S stop, G guard, R rally point, V evacuate or sell, C combat drop, and F for land mines, camo netting and the GLA worker's switch to fake buildings. Where a bunker, fire base, palace or tunnel has both evacuate and sell, V empties it and sell moves to O, so a slipped finger never sells a full bunker. The 182 buttons Zero Hour had put on one of those letters take another from their own name (the Avenger is E now), and no bar gained a clash doing it.
- Grid keys, one switch on the Hotkeys page: a button's key is its place on the bar, so the first button of a Command Center is Q and so is the first of a Barracks, a War Factory or a dozer. The keyboard's Q W E R over A S D F covers the first four columns and Z X over C V the next two, with the fixed keys' places stepping one key along the row to J, K, T, L, N and B. The letter on each button changes to match, and type select, select all, scatter and the rest that sat on those letters step aside, with the page showing where each went.
diff --git a/GeneralsMD/Code/GameEngine/Source/GameClient/MessageStream/HotKey.cpp b/GeneralsMD/Code/GameEngine/Source/GameClient/MessageStream/HotKey.cpp
index 9eaaadcce..a01b0f203 100644
--- a/GeneralsMD/Code/GameEngine/Source/GameClient/MessageStream/HotKey.cpp
+++ b/GeneralsMD/Code/GameEngine/Source/GameClient/MessageStream/HotKey.cpp
@@ -60,6 +60,7 @@
#include "GameClient/Keyboard.h"
#include "GameClient/GameText.h"
#include "Common/AudioEventRTS.h"
+#include "Common/GlobalData.h"
#include "Common/INI.h"
#include "Common/ThingTemplate.h"
#include "GameClient/ControlBar.h"
@@ -348,6 +349,66 @@ static WideChar fixedButtonKey( const AsciiString& lowerName, Int commandType, B
return 0;
}
+/** The scope Hotkeys.ini names mod `mod`'s buttons by, "<scope>:<button>": the -mod folder's name,
+ * lower case and without spaces. The launcher's Hotkeys page uses the same (hotkeys.js, modScope). */
+static std::string modScope( Int mod )
+{
+ std::string path( TheGlobalData->m_mods[ mod ].str() );
+ while( !path.empty() && ( path[ path.size() - 1 ] == '\\' || path[ path.size() - 1 ] == '/' ) )
+ path.erase( path.size() - 1 );
+ const size_t slash = path.find_last_of( "\\/" );
+ if( slash != std::string::npos )
+ path.erase( 0, slash + 1 );
+ std::string scope;
+ for( size_t at = 0; at < path.size(); at++ )
+ if( !isspace( (unsigned char)path[ at ] ) )
+ scope += (char)tolower( (unsigned char)path[ at ] );
+ return scope;
+}
+
+/** A lower case button name without the "m<n>~" -modfactions puts on a mod's own names; *mod gets
+ * that mod's index, or -1 for a name with no prefix. */
+static std::string withoutModPrefix( const char *lowerName, Int *mod )
+{
+ *mod = -1;
+ const char *tilde = strchr( lowerName, '~' );
+ if( lowerName[ 0 ] != 'm' || tilde == NULL || tilde == lowerName + 1 )
+ return lowerName;
+ for( const char *digit = lowerName + 1; digit < tilde; digit++ )
+ if( !isdigit( (unsigned char)*digit ) )
+ return lowerName;
+ *mod = atoi( lowerName + 1 ) - 1;
+ return tilde + 1;
+}
+
+/** The player's key for a button, into *letter; FALSE leaves it alone. A mod's own button (ownMod,
+ * under -modfactions) takes only its mod's "<scope>:<button>" entry. A plain name, which under a
+ * plain -mod may be any loaded mod's, takes the last loaded mod's scoped entry that exists, then
+ * the unscoped one, which is retail's. */
+static Bool findPlayerKey( const std::string& bare, Int ownMod, WideChar *letter )
+{
+ std::map< std::string, WideChar >::const_iterator it;
+ const Int modCount = TheGlobalData ? (Int)TheGlobalData->m_mods.size() : 0;
+ for( Int mod = modCount - 1; mod >= 0; mod-- )
+ {
+ if( ownMod >= 0 && mod != ownMod )
+ continue;
+ it = s_playerButtonKeys.find( modScope( mod ) + ":" + bare );
+ if( it != s_playerButtonKeys.end() )
+ {
+ *letter = it->second;
+ return TRUE;
+ }
+ }
+ if( ownMod >= 0 )
+ return FALSE;
+ it = s_playerButtonKeys.find( bare );
+ if( it == s_playerButtonKeys.end() )
+ return FALSE;
+ *letter = it->second;
+ return TRUE;
+}
+
static Bool isOneOf( WideChar letter, const WideChar *letters, Int count )
{
for( Int each = 0; each < count; each++ )
@@ -360,16 +421,18 @@ static Bool isOneOf( WideChar letter, const WideChar *letters, Int count )
WideChar HotKeyManager::playerButtonKey( const AsciiString& button, WideChar labelKey, Int barSlot,
Int commandType, Bool evacuateOnBar )
{
- AsciiString name = button;
- name.toLower();
- const WideChar fixed = fixedButtonKey( name, commandType, evacuateOnBar );
+ AsciiString lower = button;
+ lower.toLower();
+ Int ownMod = -1;
+ const std::string bare = withoutModPrefix( lower.str(), &ownMod );
+ const WideChar fixed = fixedButtonKey( AsciiString( bare.c_str() ), commandType, evacuateOnBar );
if( fixed != 0 )
return fixed;
if( s_commandGrid && barSlot >= 0 )
return barSlot < GRID_KEY_COUNT ? GRID_KEYS[ barSlot ] : 0;
- std::map< std::string, WideChar >::const_iterator it = s_playerButtonKeys.find( name.str() );
- WideChar letter = it == s_playerButtonKeys.end() ? labelKey : it->second;
+ WideChar letter = labelKey;
+ findPlayerKey( bare, ownMod, &letter );
if( letter >= 'a' && letter <= 'z' )
letter -= 'a' - 'A';
if( isOneOf( letter, FIXED_KEYS, (Int)( sizeof( FIXED_KEYS ) / sizeof( FIXED_KEYS[ 0 ] ) ) ) )