Skip to content

UI: the GM file loader and the lobby's map sort at 100% - #439

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

kotijekriv wants to merge 2 commits into
sourcehold:mainfrom
kotijekriv:reimpl/UI-B-round27-100

Conversation

@kotijekriv

Copy link
Copy Markdown
Contributor

Two functions at 100%, moved out of our open drafts #363 and #328, both from gynt's drafts. No header changes.

Function Address Before reccmp raw Normalised
Rendering::TextureRenderCore::loadGMFile 0x004559B0 93.1% in the draft PR #363 (d29d51d, written from the original's instructions with gynt's draft for the reads); gynt's own 84.5% (the status line of his fork's reimpl/UI-Rendering-development, 63cc49f; his 071726e at 77.8% is on sourcehold's reimpl/UI-Rendering; the newest commit on the file, 15e5712, is on his fork's reimpl/UI-MenuItems-development) 100.0 100.0
MenuItems::LobbyMenu::MenuItemActionHandler_LobbyMenu_MapSelectHeader 0x00440A50 46.3% in the draft PR #328 (c792435, gynt's code with a mechanical pass); gynt's own 45.9%, "comparison operand order" (the status line of his fork's reimpl/UI-MenuItems-development, from d0d28fd at 45.93%; 7a1fcbd at 32.66% is on his fork's reimpl/UI-MenuItems; the newest commit on the file, dd1a7c4, is on the -development branch) 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 builds: each file alone reads 100.0 raw and normalised, and so do the two files of this head built together.
  • Full build: an all-on build on main 3458fea of 70cc37b9c, the commit before a comment was corrected (this head's tree differs from it in one comment line of loadGMFile.cpp, "bit 2" for "bit 4"; the narrow build above is of this head): 0 failed of 2,425 compile steps, 99.58 over 2,441 functions, against 99.58 over 2,439 for main alone (built at f489b72; 3458fea only adds files under tools/). Both functions read 100.0 raw and normalised, with no diff entry.
    • No function on main moves. Every one reads exactly what it read before.
    • The average: unrounded it is 99.5763 here against 99.5759 for main.
  • Checks: openshc-pr-check gives 0 ERROR and 13 CHECK (below). openshc-pr-fix finds nothing to change.
  • Not run in game.

What got them to 100%

loadGMFile (0x004559B0)

What was left was the loop's test whether a picture shares another one's data.

  • What the original does. It tests the flag first (0x00455B70). With the flag set it reads the colour byte once, into cl, and tests it twice: zero copies the picture, bit 2 (value 4) takes the previous picture's offset, anything else the alternative picture's (0x00455B82 to 0x00455B9A). Without the flag it tests bit 2 in memory and runs on into the same blocks (0x00455B9C).
  • What the draft had. One condition, (flag && colour != 0) || (colour & 4). With it the compiler tested bit 2 again behind the zero test and based the header pointer on the colour byte.
  • The flag's two arms, each with its own sharing and its own copying. The compiler merges the two copies of the copying and lets the second arm run into the first arm's stores, as the original does. With the copying written once the match is lost: behind the draft's one test quick_diff reads 0.9326, behind a flag variable or a continue 0.70 to 0.77. The copying is nine lines with two calls, so the file has them twice; the comment above says why.
  • The colour byte in a local in the flag's arm. Without it the rest is the same, but the copying adds its source address the other way round.

One thing reviewers should know. That last operand order follows the function's locals in a way the source does not show: a second local (a name for the "gm1" string) flipped it back. We checked that it is not the translation unit's symbol count: with 0 to 63 extra declarations in front of the function the code stays the same.

The lobby's map sort (0x00440A50)

The function sorts the lobby's map list by name, by players or by balance: on a click on a column or, with a negative argument, by a stored order again. The draft was the decompiler's text with seven iVar and two uVar locals. It is written from the original's listing now. Six things decide the match:

  • The non-negative arm stands first. The original falls through into the modal checks and jumps away for a negative argument (jl at 0x00440A64).
  • A stored order is compared after it is negated (neg eax at 0x00440B29, then cmp eax, 1 and so on). In each arm the source sets the column in front of the order. The listing loads the order's value first, then the column, and stores the order last (mov ecx, 4; mov eax, 2; mov [order], ecx, from 0x00440B5E); with the order set first in the source the match is lost.
  • In every bubble pass the second index is taken in front of the first, as the name sort's listing has it (0x00440D10). With the first one first the match is lost.
  • The players' sort reads the list's length in its loop condition each time (0x00440C8F); the other two sorts take it once.
  • The players' sort counts its seven tries in the parameter's own slot (mov dword ptr [esp + 0x18], 7 at 0x00440C29), which is why the argument is copied at the top (0x00440A57). A local for the tries loses the match.
  • The search for the chosen map is a plain for loop over the list that handles the hit inside (0x00440DC0 to 0x00440DFD). The draft's while loop with a second test of the length has four instructions more.

The sorts' repeat-until-nothing-swapped loops are for loops (for (int swaps = 1; swaps != 0;)), which compile to the same code as do-while loops.

No behaviour differs from the drafts, as far as we can tell. One test in the search can never be true, if (found < 0) with found counting up from 0, but the original has it (test esi, esi; jge at 0x00440E05), so it stays.

The checker's 13 CHECKs

All are in the map sort's file.

What Why it stays
the parameter's name param_1 (8 findings on 7 lines) it is the header's name. What it holds is in the comment at the function's top: 0, 1 or 2 for a clicked column, or a negated stored order
for (param_1 = 7; param_1 > 0;) has no counter of its own in the header the original counts the seven tries in the parameter's slot; with a local the match is lost. The comment above says so
a cast on lobbyMapSortOrder the header has it as undefined4; one signed compare needs the cast. No header change here
three case labels are numbers the switched value is the clicked column, 0 to 2; there is no enum for it
the placeholder names DAT_00b960dc and INT_00b95ab8 Ghidra's names. The first is non-zero while a menu's background fades, and a click is ignored then; in MenuView_SingleplayerMapChoice_DoEveryFrame its sign picks the fade's direction (the step itself comes from the time). The second is set to 1 before the map table's handler runs, and we don't know more about it

🤖 Generated with Claude Code

kotijekriv and others added 2 commits November 20, 2026 18:09
TextureRenderCore::loadGMFile. gynt's drafts: 071726e at 77.8% on sourcehold's reimpl/UI-Rendering, and 84.5% in
the status line of his fork's reimpl/UI-Rendering-development (63cc49f); the newest commit on the file, 15e5712,
is on his fork's reimpl/UI-MenuItems-development. Our draft d29d51d (93.1%, in the draft PR sourcehold#363) was written from
the original's instructions with his draft for the reads.

What was left was the loop's test whether a picture shares another one's data. The original tests the flag first
(0x00455B70). With the flag set it reads the colour byte once, into cl, and tests it twice: zero copies the picture,
bit 2 (value 4) takes the previous picture's offset, anything else the alternative picture's (0x00455B82 to
0x00455B9A). Without the flag it tests bit 2 in memory and runs on into the same blocks (0x00455B9C). The draft had
one condition, (flag && colour != 0) || (colour & 4): with it the compiler tested bit 2 again behind the zero test
and based the header pointer on the colour byte.

Written as the listing has it:

- The flag's two arms, each with its own sharing and its own copying. The compiler merges the two copies of the
  copying and lets the second arm run into the first arm's stores, as the original does. With the copying written
  once the match is lost: behind the draft's one test quick_diff reads 0.9326, behind a flag variable or a continue
  0.70 to 0.77.

- The colour byte in a local in the flag's arm. Without it the rest is the same, but the copying adds its source
  address the other way round (0.9610 against 0.9714).

Measured: quick_diff 0.9714 with 192 instructions against 193, also with the stack slots kept. What still differs
there is not in the code: four calls through the import table, the jmp over the padding in front of the loop
(0x00455B66), and the previous offset's address, which the original's listing shows folded (0x00D0BA0C). reccmp
100.0% (normalized 100.0%) alone, data resolvers and its own resolver on. Not run in game.

Co-authored-by: Gynt <gynt@users.noreply.github.com>
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
LobbyMenu::MenuItemActionHandler_LobbyMenu_MapSelectHeader, rewritten from the original's listing. gynt's drafts:
7a1fcbd at 32.66% on his fork's reimpl/UI-MenuItems, and d0d28fd at 45.93% ("comparison operand order") on his
fork's reimpl/UI-MenuItems-development, whose status line records 45.9%; the newest commit on the file, dd1a7c4,
is on that branch too. Our draft c792435 (46.3%, in the draft PR sourcehold#328) was his code with a mechanical pass.

The function sorts the lobby's map list by name, by players or by balance: on a click on a column or, with a
negative argument, by a stored order again. The drafts had the decompiler's text, with seven iVar and two uVar
locals. Written out with names, six things decide the match:

- The non-negative arm stands first. The original falls through into the modal checks and jumps away for a
  negative argument (jl at 0x00440A64).

- A stored order is compared after it is negated (neg eax at 0x00440B29, then cmp eax, 1 and so on). In each arm
  the source sets the column in front of the order. The listing loads the order's value first, then the column,
  and stores the order last (mov ecx, 4; mov eax, 2; mov [order], ecx, from 0x00440B5E); with the order set first
  in the source the match is lost.

- In every bubble pass the second index is taken in front of the first, as the name sort's listing has it
  (0x00440D10). With the first one first the match is lost.

- The players' sort reads the list's length in its loop condition each time (0x00440C8F); the other two sorts take
  it once.

- The players' sort counts its seven tries in the parameter's own slot (mov dword ptr [esp + 0x18], 7 at
  0x00440C29), which is why the argument is copied at the top (0x00440A57). A local for the tries loses the match.

- The search for the chosen map is a plain for loop over the list that handles the hit inside (0x00440DC0 to
  0x00440DFD). The drafts' while loop with a second test of the length has four instructions more.

The sorts' repeat-until-nothing-swapped loops are for loops (for (int swaps = 1; swaps != 0;)), which compile to
the same code as do-while loops.

Measured: quick_diff 0.9766 with 253 instructions against 259. The six lines are the original's jmp over the padding
in front of its loops, which our listing shows as padding; the rest is how the two listings print two constants and
the tail call. reccmp 100.0% (normalized 100.0%) alone, data resolvers and its own resolver on. Not run in game.

Co-authored-by: Gynt <gynt@users.noreply.github.com>
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