Skip to content

UI: LobbyMenu DoEveryFrame at 100%, with the gotos the match needs - #500

Merged
gynt merged 1 commit into
sourcehold:mainfrom
kotijekriv:reimpl/UI-B-lobbydoeveryframe-100
Nov 25, 2026
Merged

gynt merged 1 commit into
sourcehold:mainfrom
kotijekriv:reimpl/UI-B-lobbydoeveryframe-100

Conversation

@kotijekriv

@kotijekriv kotijekriv commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

One more UI function at 100%, out of the draft #422. It is rewritten from the binary, with gynt's draft for the names.

Function Address Before reccmp raw Normalised
LobbyMenu::MenuView_LobbyMenu_DoEveryFrame 0x004482F0 gynt's 54.19% (e9e2437); our draft #422 at 100% with an open question 100.0 100.0

No header changes in this PR.

Proposed, not changed here:

  • GameCommandType has no value for 106, so the code casts the number. I'd propose GCT_UNKNOWN_106 = 106 between 105 and 107, named like the enum's other unnamed commands. The function queues command 106 while it sends the map to the other players (push 0x6a at 0x00448642, into queueCommand 0x00489100, whose parameter is a GameCommandType). gynt's draft writes the number as GCT_START_OR_STOP_SEND_MAP_FILEUnk | GCT_MULTIPLAYER_INITIATE_ANNOUNCE_HOST (104 | 2), which is how Ghidra printed 106. I left the header alone because that one line moves a function on main: with it, our full build read LaunchSinglePlayerGameUnk (0x0042C620, UI: the single-player game launch at 100% #466) at 99.4 raw / 83.3 normalised instead of 100.0, register allocation only (Compiler randomness: adding Enums and Typedefs causes different bytecode because of different code folding #414): one add of the two selection fields has eax and ecx swapped. That file includes GameCommandType.hpp, and a quick compile of it reads 0.9854 with the line and 0.9900 without, with exactly that swap. So it waits for you, if you want it in Ghidra.
  • DAT_00b960dc, DAT_00b960f8 and GameSynchronyState::lobbyMapSortOrder are undefined4, so the function casts each of them once. I'd propose int for all three:
    • DAT_00b960dc is tested < 0 and DAT_00b960f8 >= 66, and the original compares both signed (jge at 0x00448367, jl at 0x0044874D).
    • lobbyMapSortOrder is only negated here (neg at 0x00448977), which is the same for signed and unsigned values; the cast there only keeps MSVC from warning about a minus on an unsigned value. Main's MenuItemActionHandler_LobbyMenu_MapSelectHeader casts it too, to compare it < 5.
    • The main files that use them would need measuring with the new types: LaunchSinglePlayerGameUnk, LobbyscreenStartButtonClick, MapDescScrollbar and MapSelectHeader for DAT_00b960dc, PeasantBuildAndRightClickMenuSelection for DAT_00b960f8, and MapSelectHeader for lobbyMapSortOrder. I haven't measured them, so the headers are unchanged.

Measured.

  • Setup: the bundled MSVC1400-SP1 against the Steam Stronghold Crusader.exe (3bb0a8c1…5387ac5a), both resolver sets enabled locally (not committed), and sourcehold's reccmp with --resolve-wrapped-calls --ignore-security-cookie.
  • Narrow build: a real build of this head (one commit on main 709b429), this file alone with the data and its own function resolver on: 100.0 raw and 100.0 normalised, with no diff line.
  • Full build: not run on this head, which changes only its own .cpp and its status line. The full build of the first cut, f1fcf329c (the same function before the review fixes, with the same cast for 106), was an all-on build with both resolver sets on and main's own /GL list, next to the same build of main 709b429 in the same clone. Seven files that don't compile on main yet were left out of both with their resolvers off: requestGoods (0x004D1F00), MenuItemActionHandler_Allies_Main (0x004B12D0), hasEnoughGoldForMoat (0x0040CAC0), and the four UnitsState files the Reimplement 47 UnitsState functions at 100% and restyle 28 more #378 sync leaves behind (getFirstSelectedUnitID, nonEuroRecruit, queueDisbandAndAttackCommand3Params, setPositionOfUnit). 0 of 2,651 compile steps failed and it linked; 99.6874 raw / 99.6551 normalised over 2,667 functions, against main's 99.6821 / 99.6462 over 2,666, and nothing read lower. Three functions went up to 100.0: Constructor_MenuView_LobbyMenu (90.0), which pushes this function's address and now resolves it, and handleMenuItems (97.9) and Constructor_Menu (98.4), whose 0x11000000 compares main's layout prints as a symbol of our DLL.
  • Checks: my local style checker reports no errors. What it leaves are the placeholder names, the four casts (the three undefined4 reads and command 106), the '' terminator, the gotos with their index, and two notes on truth tests (the BOOLEnum flags are tested bare, the counts and the three-state fade value against 0). Not run in game.

The three gotos

The function searches the map list twice for a map with enough places, and both searches end in the same block. On #422 I asked whether three gotos are acceptable there. gynt said the same of his own gotos on #396 (2026-11-25), about updateGfxLayer: "The goto's are necessary for the match. There was no code form that could remove these goto's!" He marked two more there "Also not removable".

These are the goto-free forms I measured, on an original of 724 instructions. Each changes only the map search, and all were compiled in one run:

Form Instructions quick_diff
gotos, as committed 723 0.9938 (reccmp 100.0)
a found index, set before a break out of each loop 730 0.9464
__forceinline helpers with early returns 740 0.9385
static or inline helpers (not inlined) 688 0.9391

0.9938 is as high as quick_diff goes on this function; reccmp reads 100.0. Each goto has a one-line comment; the style bot will still post its three goto findings.

Kept for the match

  • The step to the next face is written in both arms of the faces loop: one copy after the arms keeps the count on the stack and moves every register in the loop.
  • DAT_MapSelectionRelativeSelected = -1 is stored again inside the test for -1, as the original does (0x00448950).
  • The search start, scroll offset plus selection, is written out in both places: a local for it swaps a mov and a cmp.
  • The fade steps by elapsed / -40: the original multiplies by 0x99999999, the constant of a division by -40 (gynt's draft shows it as * -0x66666667), so it is not written -= elapsed / 40.
  • The map list's index is one lower than in gynt's draft: main's sync 12854bc (2026-11-04) moved DAT_ArrayOfMapIndices from 0x884 to 0x880, so the original's 0x01126E28 is element 0.

What the placeholders hold

These are read from how the code uses them, and the names are unchanged.

  • DAT_00b960dc: the lobby's fade. Below 0 the lobby fades in from dark, and this sets it back to 0 at the end; above 0 it fades to dark and then ends the menu switch delay. 0 means no fade.
  • DWORD_00b95b1c: the time the fade started. The fade's step is the time since then; this function sets it after it has read the map list again, and LaunchSinglePlayerGameUnk sets it just before it sets DAT_00b960dc to 1.
  • INT_00b960f0: the time the last part of the lobby's face bitmap was sent.
  • DAT_00b960f8: the number of the next part to send, from 0 to 65.
  • INT_00b960fc: the time of the last 30-second refresh of the player information.
  • INT_00b95958: a non-host redraws the map description at that refresh only while this is 0. I don't know what it means.
  • INT_00b95b74: probably the map description text's height. It is reset for each map that is read.
  • DAT_00b960f4: the map description's scroll offset, reset the same way.
  • INT_00b95f6c: 1 while the mouse is over the box of the players' faces.

Out of #422

#422 keeps MenuItemRenderFunction_OptionsMenu_Buttons and its #414 question.

🤖 Generated with Claude Code

LobbyMenu::MenuView_LobbyMenu_DoEveryFrame, rewritten from the binary with gynt's draft for the names (d283417 at
50.97% on his fork's development; e9e2437 at 54.19% on his reimpl/UI-MenuViews-development; 08c492b of 2026-11-02 on
his reimpl/UI-MenuItems-development, his newest reimplement commit on the file). reccmp 100.0 (normalized 100.0) on main
709b429 with the data and its own function resolver on; quick_diff reads the draft 0.6142 there.

The array index changed with the header. gynt's draft uses DAT_ArrayOfMapIndices[i - 1], which was right on his branch,
where the array is declared at offset 0x884 and his loop's counter starts at 0. Main's sync 12854bc (2026-11-04)
moved the declared start to 0x880, so the original's 0x01126E28 (0x004487E9, 0x004488B0) is element 0, and the index
here ([mapCount], [index]) is one lower than in the draft.

- The step to the next face is written in both arms of the loop. As one copy after the arms the count is kept on the
  stack and every register in the loop moves.
- The two searches for a map with enough places keep three gotos, because both end in the same block. gynt on
  sourcehold#396 (2026-11-25): "The goto's are necessary for the match. There was no code form that could remove these
  goto's!" The goto-free forms measured in quick_diff, on an original of 724 instructions (0.9938 is quick_diff's
  ceiling on this function):

    gotos, as committed                     723 instructions   0.9938   reccmp 100.0
    a found index set before a break        730 instructions   0.9464
    __forceinline helpers, early returns    740 instructions   0.9385
    static or inline helpers, not inlined   688 instructions   0.9391

- The search starts from the sum of scroll offset and selection written out; a local for it swaps a mov and a cmp.
- The box's positions are declared with left before right and top before bottom, after the flag is cleared: those six
  orders tie at 0.9938 in quick_diff and the others read lower.
- RelativeSelected = -1 is stored again inside the test for -1, which the original does (0x00448950).

Co-authored-by: Gynt <gynt@users.noreply.github.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@github-actions

Copy link
Copy Markdown

Style guide reading (advisory, posted once)

3 possible deviation(s) from REIMPLEMENTATION_STYLE_GUIDE.md across 1 of the 1 reimplemented .cpp file(s) in this PR.

The match comes first. Where the ugly form is what matched, it stays -- that is the guide's own overriding rule, and rule 21 asks for a one-line comment saying what was measured so the next reader does not retry it. This report is posted once, when the PR opens, and is not repeated on later pushes.

rule what it asks hits
20 Keep nesting low; no goto 3
Where

Rule 20 — Keep nesting low; no goto

  • src/OpenSHC/UI/MenuViews/LobbyMenu/MenuView_LobbyMenu_DoEveryFrame.cpp:310 — goto; check how many predecessors the target has first
  • src/OpenSHC/UI/MenuViews/LobbyMenu/MenuView_LobbyMenu_DoEveryFrame.cpp:320 — goto; check how many predecessors the target has first
  • src/OpenSHC/UI/MenuViews/LobbyMenu/MenuView_LobbyMenu_DoEveryFrame.cpp:324 — goto; check how many predecessors the target has first

@kotijekriv kotijekriv changed the title reimplement: SHC_3BB0A8C1_0x004482F0 100.0% Reimplemented UI: LobbyMenu DoEveryFrame at 100%, with the gotos the match needs Nov 25, 2026
@gynt

gynt commented Nov 25, 2026

Copy link
Copy Markdown
Contributor

It would be amazing if we find a solution for the goto's!

@gynt
gynt added this pull request to the merge queue Nov 25, 2026
@kotijekriv

Copy link
Copy Markdown
Contributor Author

Thanks for the review! We'd like that too. Here's what we tried so far: two goto-free forms of the two searches, a found index set before a break and the searches as small inlined helpers. In quick compiles they came out at 0.9464 and 0.9385, against 0.9938 for the goto form, which is the only one that reaches 100% in the real build. So for now the gotos stay. If a form turns up elsewhere that removes this kind of shared-block goto and still matches, we'll come back to this function with it.

Merged via the queue into sourcehold:main with commit fa2ed63 Nov 25, 2026
5 checks passed
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.

2 participants