Skip to content

UI: three more functions at 100%: the scenario description's buttons, the map click handler and the name entry's prepare - #434

Open
kotijekriv wants to merge 4 commits into
sourcehold:mainfrom
kotijekriv:reimpl/UI-B-round23-100
Open

kotijekriv wants to merge 4 commits into
sourcehold:mainfrom
kotijekriv:reimpl/UI-B-round23-100

Conversation

@kotijekriv

Copy link
Copy Markdown
Contributor

Three UI functions at 100%, moved out of our open drafts #328 (two) and #356 (one), all from gynt's drafts. No header changes.

Function Address Before reccmp raw Normalised
ScenarioDescription::MenuItemRenderFunction_ScenarioDescription_Main 0x004D8250 95.0% in the draft PR (a8b3225); gynt's 28.99% (38ce297; his newest commit on the file is e63ae30) 100.0 100.0
InGameMenu::MenuItemActionHandler_InGameMenu_PeasantBuildAndRightClickMenuSelection 0x00434350 90.6% in the draft PR (f1f198c); gynt's newest scored draft 67.13% (b62cbe6; the newest commit on the file is 8ca7861) 100.0 100.0
GameStartEnterName::MenuView_GameStartEnterName_Prepare 0x00440560 98.2% in the draft PR (c5b5fe6); gynt's 82.52% (ac76223; his newest commit on the file is 0295332) 100.0 100.0

Measured.

  • Setup: the bundled MSVC1400-SP1 against the Steam Stronghold Crusader.exe (3bb0a8c1…5387ac5a), and sourcehold's reccmp with --resolve-wrapped-calls --ignore-security-cookie. The struct resolvers and the function resolvers are enabled locally (not committed); in the full build 0x004314E0 (MapEditorLandscaping_GeneralButtons handler) is left out and its resolver stays off, as in main's baseline.
  • Narrow build: each function alone reads 100.0 raw and normalised.
  • Full build: an all-on build of fc31bc6 on main 3458fea: 0 failed of 2,426 compile steps, 99.58 over 2,442 functions, against 99.58 over 2,439 for main alone (built at f489b72; 3458fea only adds files under tools/). All three functions read 100.0 raw and normalised.
    • The head is one commit further (733d5cf): it rewords one comment in the click handler and changes no code. That function alone reads 100.0 raw and normalised on the head; the full build was not repeated for it.
    • One function on main moved, up: Constructor_MenuView_GameStartEnterName (0x0059A700) went from 90.0 to 100.0. It pushes the prepare function's address (0x00440560), which now resolves to the reimplementation. Its status line says "100.0% | Reimplemented (counted: only the pushed callback address is unnamed in the original)" since Reimplement 176 static initializers and GetStringBasedOnHardcodedMaps #405; the remark in brackets no longer applies. This PR leaves that line as it is.
    • Every other main function reads exactly what it read before.
  • Checks: openshc-pr-check gives 0 ERROR and 25 CHECK, all placeholder names (below). openshc-pr-fix finds nothing to change.
  • Not run in game.

Behaviour fixed compared with gynt's drafts

These fixes were made in the draft PRs and checked against the binary. They are listed here because the drafts' texts no longer carry these functions.

  • PeasantBuildAndRightClickMenuSelection: two places where the original function ends and gynt's draft (b62cbe6) carried on.
    • When the mouse is not over the map, the original ends with storeXYAndResetMouseState (tail jump at 0x00434609). The draft called it and went on into the click handling.
    • With a mapper command active and the left button down, the original ends with setupMouseTileXY (tail jump at 0x004347E5). The draft called it and went on into the selection code.
  • GameStartEnterName_Prepare: when a name was entered before, the original returns right after clearing the two trigger flags (stores at 0x00440648 and 0x0044064E, return at 0x00440669). gynt's draft (ac76223) had no return there: it went on to load frontend_combat3.tgx, reset the text and open the name dialog. Its name buffer was 256 bytes where the original's is 250; that changes the frame, not what the function does.

What got them to 100%

MenuItemRenderFunction_ScenarioDescription_Main

  • The last difference was which copy of two identical runs of tests the compiler keeps. The original has the button == 13 test and the economic-mode tests once, behind the else arm's button == 12 test (0x004D8543), and the other arm jumps there.
  • The draft wrote the whole test in each arm as one condition. MSVC then kept the earlier arm's copy, and dropped the re-tests of 12 and 13 that the original makes.
  • Now each arm only does its own tests and falls out, and the economic-mode test stands once behind both. The later copy survives and the re-tests stay.

MenuItemActionHandler_InGameMenu_PeasantBuildAndRightClickMenuSelection

  • The left-click arm with a mapper command (90.6 to 99.7). The original tests the command in line (0x00434699) and has the arm's code, which ends in returns, behind the function's last return (0x004347BE). else if (command != NULL) { ... } moved the test out of line together with the code. Written as else if (command == NULL) { } else { ... }, the code is an else arm that returns: MSVC lays that out behind the next return and leaves the test where it is. The empty arm has a comment saying why. The plain returns then share the earlier return block, as in the original.
  • The second selectUnitAndOpenStatusMenu call (99.7 to 100). The original tests the first call's result with test eax, eax and the second's with cmp eax, ebp, where ebp holds zero. That is how MSVC tests a variable, not a call's result, so the second result goes through a local: BOOLEnum selectedLast = FALSE; if (...) selectedLast = call; if (selectedLast) return;. One local for both calls turns the first test into a cmp too.

MenuView_GameStartEnterName_Prepare

  • The last difference was the frame: the name buffer sat at [esp + 0xc] where the original has it at [esp + 0x10], under the same security cookie.
  • Two things in the draft cancelled out in the frame's size but not in its layout. The name buffer is 250 bytes, not 256. And the length test is strlen, with which MSVC's frame is four bytes larger; the draft had a hand-written loop instead, because with the 256-byte buffer strlen made the frame four bytes too large.
  • With both, the frame is 0x10c bytes with the buffer four bytes up, as in the original.
  • The frame alone would allow any size from 249 to 252. 250 is the size of the game's own name arrays: UserTextHandler::textArray[16][250], which the name is copied from, and DAT_PlayerNames[9][250].

The first two findings are about which of two identical code tails MSVC keeps, and where it lays out an arm that returns. They may help with other functions whose only gap is block placement.

The checker's 25 CHECKs

All 25 are placeholder names that Ghidra has no better name for.

Count Name What this code shows
3 + 4 DAT_00b960f8, INT_00b960f0 a counter and a time: in multiplayer, command 117 goes out every 1.5 seconds with the counter, until it reaches 66
7 GameCore::field_0x15c set to 1 by four of the mouse's view events (lowered view, rotation, zoom, flat view), cleared on a right click, and tested when the right button is released
1 + 1 MouseState::field_0x198, field_0x19c set to 2 by the mouse events 4 and 5; not known beyond that
1 + 1 ViewportState::field_0x0, field_0x34 non-zero while the mouse is over the map; a time compared with the click's time (200 ms)
3 + 3 + 1 INT_00ed27bc, DAT_00ed2798, INT_00ed27c4 tested by the scenario description's buttons; not known beyond that

🤖 Generated with Claude Code

kotijekriv and others added 4 commits November 20, 2026 04:46
ScenarioDescription::MenuItemRenderFunction_ScenarioDescription_Main, from gynt's draft (38ce297 at 28.99% on his
fork's development; his newest commit on the file is e63ae30 on reimpl/UI-MenuItems-development) by way of my
draft in the open draft PR (a8b3225, 95.0%, "block order of the last tests"). reccmp 100.0 (normalized 100.0) on
main 3458fea with the data on.

The last difference was which copy of the `button == 13` test and the economic-mode tests the compiler keeps. The
original has them once, behind the else arm's `button == 12` test (0x004D8543), and the other arm jumps there.
My draft wrote the whole test in each arm as one condition, and MSVC then kept the earlier arm's copy and dropped
the re-tests of 12 and 13 that the original makes. Written as the original has it, the arms only do their own
tests and fall out, and the economic-mode test stands once behind both: the later copy survives and the re-tests
stay.

Co-authored-by: Gynt <gynt@users.noreply.github.com>
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
InGameMenu::MenuItemActionHandler_InGameMenu_PeasantBuildAndRightClickMenuSelection, from gynt's drafts (b62cbe6
at 67.13% on his fork's reimpl/UI-MenuItems-development is his newest scored one; 8ca7861 on reimpl/Wilps-2026-10-18
is the newest commit on the file) by way of my draft in the open draft PR (f1f198c, 90.6%, "block placement at the
end"). reccmp 100.0 (normalized 100.0) on main 3458fea with the data on.

Two changes took it from 90.6 to 100:
- The left-click arm with a mapper command. The original tests the command in line (0x00434699) and has the arm's
  code, which ends in returns, behind the function's last return (0x004347BE). `else if (command != NULL) { ... }`
  moved the test out of line with the code. Written as `else if (command == NULL) { } else { ... }` the code is an
  else arm that returns, which MSVC lays out behind the next return, and the test stays where it is. That alone
  gave 99.7: the plain returns then share the earlier return block, as in the original.
- The second selectUnitAndOpenStatusMenu call, for the unit that was under the mouse before. The original tests the
  first call's result with `test eax, eax` and the second's with `cmp eax, ebp` (ebp holds zero), so the second
  result went through a variable: `BOOLEnum selectedLast = FALSE; if (...) selectedLast = call; if (selectedLast)
  return;`. One variable for both calls turns the first test into a `cmp` too.

Co-authored-by: Gynt <gynt@users.noreply.github.com>
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
GameStartEnterName::MenuView_GameStartEnterName_Prepare, from gynt's draft (ac76223 at 82.52% on his fork's
reimpl/UI-MenuViews-development) by way of my draft in the open draft PR (c5b5fe6, 98.2%, "stack slots of the name
buffer and the cookie"). reccmp 100.0 (normalized 100.0) on main 3458fea with the data on.

The last difference was the frame: the name buffer sat at [esp + 0xc] where the original has it at [esp + 0x10], under
the same cookie. Two things in the draft cancelled out in the frame's size but not in its layout:
- the name buffer is 250 bytes, not 256 (250 is also the size of the game's player name entries);
- the length test is strlen, with which MSVC's frame is four bytes larger. The draft had a hand-written loop
  instead, because with the 256-byte buffer strlen made the frame four bytes too large.
With both, the frame is 0x10c bytes with the buffer four bytes up, as in the original.

Co-authored-by: Gynt <gynt@users.noreply.github.com>
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
The empty arm for "no mapper command" in MenuItemActionHandler_InGameMenu_PeasantBuildAndRightClickMenuSelection
(0x00434350) is what keeps the command test in line. Its comment said "an else-if on the command loses the match",
which reads oddly on an arm that is itself an else-if on the command. It now names the form that loses the match:
`else if (command != M_MAPPER_NULL) { ... }` without the empty arm. Comment only: reccmp still reads 100.0
(normalized 100.0).

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant