Skip to content

UI: three more functions at 100%: the building menu's DoInitial, the credits' prepare and activateModalDialog2 - #435

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

kotijekriv wants to merge 3 commits into
sourcehold:mainfrom
kotijekriv:reimpl/UI-B-round24-100

Conversation

@kotijekriv

Copy link
Copy Markdown
Contributor

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

Function Address Before reccmp raw Normalised
BuildingAndStatusMenu::MenuView_BuildingAndStatusMenu_DoInitial 0x004398B0 96.3% in the draft PR (bef74ae); gynt's 96.23% (24a37e8, on his fork's reimpl/UI-MenuViews-development; the newest commit on the file, e9bc49c, is on his reimpl/UI-MenuItems-development) 100.0 100.0
Credits::MenuView_Credits_Prepare 0x004261E0 82.1% in the draft PR (3480be0, his code in main's style); gynt's own 82.1% (the status line on his fork's reimpl/UI-MenuViews-development since 71e725a; his scored commit on the file is 458bcd5 at 80.00%; the newest commit on the file, 0295332, is on his reimpl/UI-MenuItems-development) 100.0 100.0
MenuModalComposition::activateModalDialog2 0x004B2820 82.7% in the draft PR (950e83e); gynt's 53.7% (the status line of 10280b1 on his fork's reimpl/UI-Core-development; 06829d8 at 53.6% is on sourcehold's reimpl/UI-Core; the newest commit on the file, 1eaeddd, is on his reimpl/UI-MenuItems-development) 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: the three functions together read 100.0 raw and normalised on this head.
  • Full build: an all-on build of this head's tree 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, with no diff entry.
    • Two functions on main moved up: Constructor_MenuView_BuildingAndStatusMenu (0x0059A430) from 70.0 to 80.0 and Constructor_MenuView_Credits (0x0059A7F0) from 80.0 to 90.0. Each pushes the address of one of the functions here, which now resolves to the reimplementation.
    • Two read lower, and it is how reccmp prints a constant, not their code: Menu::handleMenuItems (0x004F6280) reads 97.9 (91.4 normalised) and Menu::Constructor_Menu (0x004F4100) reads 98.4 (95.2), both 100.0 before. Both compare a menu item's type with the constant 0x11000000, five compares in all. In this build's layout a symbol of our DLL lies at exactly the address 0x11000000, so reccmp prints the constant as that symbol's name on our side. The instructions are the same on both sides. In an all-on build with only the first two functions, where no symbol lies at that address, both read 100.0.
    • Every other main function reads exactly what it read before.
  • Checks: openshc-pr-check gives 0 ERROR and 18 CHECK (below). openshc-pr-fix finds nothing to change.
  • Not run in game.

What got them to 100%

All three had the same kind of gap: the instructions were right, but a constant sat in a register on one side and was an immediate on the other. In all three the answer was where a statement that both arms of an if / else share is written.

MSVC merges identical code at the start or the end of two arms into one copy, so the instructions come out the same whether the source has the statement once outside the arms or once in each arm. What differs is what the compiler decides before that merge: how often a constant is used, which decides whether it gets a register, and where the registers are saved.

MenuView_BuildingAndStatusMenu_DoInitial

  • The constant 99. The function stores 99 sixteen times, so the compiler holds it in ecx (lea ecx, [ebx + 99] at 0x00439986). The original uses that register only up to the first call and stores the immediate in every later block. Ours loaded 99 into ecx again at the end of each block, 13 times.
  • The last store of the 1024x768 and the 1024x600 blocks (drawBufferChoiceValue = RT_MAP_GAME) stood once behind the two arms. Written in each arm, the 13 reloads are gone.
  • The save of ebp. DAT_MenuHandlerState::instance.y stood once behind the first if / else. Written in each arm, ebp is saved behind the merged store and the load of the tab type (0x004398FF), as in the original, and not in front of the store.
  • Tried without effect on the 99: where the 99 stores sit, a local for the 99, the second store reading the index back, and the four callees in the same translation unit.

MenuView_Credits_Prepare

  • The original keeps 0 in edi for the whole function (xor edi, edi at 0x004261E2, six uses) and saves esi and edi at the start. Ours used the immediate 0 everywhere.
  • totalLoadedGfx = 0 stood once in front of the test. Written in each arm it is one more use of 0, and the compiler keeps 0 in edi. The two stores still come out as one, between the compare and the jump (0x004261EA).

activateModalDialog2

  • Here it was the other way round: ours kept a zero in esi for the whole function, where the original compares with and stores the immediate 0 and saves esi only around the copy of the modal.
  • The last block had the result of getAreWeInAInGameMenu and the dialog's x and y in locals in front of the test. Now the call is tested directly and each arm has its own x and y. The original loads y and x between the call's test and the jump (0x004B29F2 to 0x004B29FA), where the compiler puts loads that both arms start with.

This may help with other functions whose only gap is a constant's register or the place of a push.

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.

  • MenuView_BuildingAndStatusMenu_DoInitial: at 640x480 gynt's draft (24a37e8) never set DAT_MenuHandlerState::instance.y. The original stores it for both branches (the shared store at 0x004398F5). His e9bc49c sets it in front of the test, so it does not have this.
  • activateModalDialog2: two places.
    • When the dialog being closed is an AI lord's message and the next stored one starts playing, the original returns (jne at 0x004B2854 to the function's end). Two of gynt's versions, 10280b1 and 1eaeddd, have an empty if where that return is; 06829d8 and 8ca7861 have the return.
    • In game, the original blits only the map surface's rectangle: the two blits are the arms of one test, each followed by its own end (0x004B2A2A and 0x004B2A4D). All four of gynt's versions (06829d8, 8ca7861, 10280b1, 1eaeddd) went on from the in-game blit to the menu surface's blit.

Style changes against the drafts

  • activateModalDialog2: the border style masks are in decimal, (32 | 512) for the two tiled styles and 1024, because the bits have no names yet. Main's activateModalDialog still has them as 0x220 and 0x400. The imported note on the copy of the modal is gone, as in main's activateModalDialog.
  • MenuView_BuildingAndStatusMenu_DoInitial: pendingMenuRedraw is a BOOLEnum, so it is tested by truthiness and stored FALSE. In the first two blocks leftGfx is declared in each arm with its value.
  • MenuView_Credits_Prepare: the string pointers have their texts as comments.

The checker's 18 CHECKs

Count What Why it stays
12 int leftGfx; declared without a value, in the twelve later resolution blocks of DoInitial both arms of the tab test assign it and the code behind the arms uses it, so there is no single first assignment to declare it at
1 INT_00b95954 tested by the credits' prepare: 0 loads the four credits pictures, anything else the end credit picture
1 + 1 INT_00b960d0, INT_00b960d4 two times taken here: one before LoadTGX_shc_back, one after it
1 + 1 + 1 DAT_00b95b3c, DAT_00b95b70, DAT_00b9610c set here to 0, 0 and 1; not known beyond that

🤖 Generated with Claude Code

BuildingAndStatusMenu::MenuView_BuildingAndStatusMenu_DoInitial, from gynt's draft on his fork's
reimpl/UI-MenuViews-development (his newest scored commit on the file is 24a37e8 at 96.23%; the newest commit on
the file, e9bc49c, is on his reimpl/UI-MenuItems-development), through our draft bef74ae (96.3%, in the draft
PR sourcehold#356), which restored the y store at 640x480.

Two differences were left, and both come from one thing: a store that both arms of an if / else share was written
once behind the arms. The compiler merges the arms' identical ends, so the instructions come out the same either
way, but what it decides before that merge does not:

- DAT_MenuHandlerState::instance.y at the function's start. Written in each arm, ebp is saved behind the merged
  store and the load of the tab type (push ebp at 0x004398FF), as in the original. Written once behind the arms,
  ebp is saved in front of the store.

- drawBufferChoiceValue = RT_MAP_GAME at the end of the 1024x768 and the 1024x600 blocks. The function stores 99
  sixteen times, so the compiler holds 99 in ecx (lea ecx, [ebx + 99] at 0x00439986). The original uses that
  register only up to the first call and stores the immediate in every later block. Written once behind the arms,
  our compiler loaded 99 into ecx again at each block's end (13 times). Written in each arm of those two blocks, it
  does not.

Measured: quick_diff 1.0000 with 1069 instructions on both sides; reccmp 100.0% (normalized 100.0%) alone, data
resolvers and its own resolver on. Not run in game.

Tried on the way, all without effect on the 99: where the 99 stores sit in the first two blocks, a local for the
99, the second store reading the index back, and the four callees in the same translation unit (quick_diff 0.7860).

The text against the draft: pendingMenuRedraw is a BOOLEnum, so it is tested by truthiness and stored FALSE; in the
first two blocks, where each arm is now complete by itself, leftGfx is declared in the arm with its value.

Co-authored-by: Gynt <gynt@users.noreply.github.com>
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Credits::MenuView_Credits_Prepare, from gynt's draft on his fork's reimpl/UI-MenuViews-development (his scored commit
on the file is 458bcd5 at 80.00%, and the branch's status line records 82.1% since 71e725a; the newest commit
on the file, 0295332, is on his reimpl/UI-MenuItems-development), through our draft 3480be0 (82.1%, his code in
main's style, in the draft PR sourcehold#356).

One change: each arm of the test empties the list of loaded pictures itself (totalLoadedGfx = 0), where the draft
had one store in front of the test. The original keeps 0 in edi for the whole function (xor edi, edi at 0x004261E2,
six uses) and saves esi and edi at the start. With one store our compiler used the immediate 0 everywhere and saved
esi only in front of its first use. With the store in each arm it keeps 0 in edi, and the two stores still come out
as one, between the compare and the jump (0x004261EA), where the single store stood too. Presumably the compiler
counts the uses of 0 before it merges the two stores.

Measured: reccmp 100.0% (normalized 100.0%) alone, data resolvers and its own resolver on. Not run in game.

The text against the draft: the string pointers have their texts as comments.

Co-authored-by: Gynt <gynt@users.noreply.github.com>
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
MenuModalComposition::activateModalDialog2, from gynt's drafts: 06829d8 at 53.6% on sourcehold's reimpl/UI-Core;
b1c48ad at 50.64% and 10280b1, whose status line records 53.7%, on his fork's reimpl/UI-Core-development; the
newest commit on the file, 1eaeddd, is on his reimpl/UI-MenuItems-development. Through our draft 950e83e
(82.7%, in the draft PR sourcehold#313).

What was left: our compiler kept a zero in esi for the whole function (xor esi, esi at the start and again behind
the copy of the modal), where the original compares with and stores the immediate 0 and saves esi only around that
copy. The last block decided it. It had the result of getAreWeInAInGameMenu and the dialog's x and y in locals in
front of the test. Now the call is tested directly and each arm has its own x and y. The original loads y and x
between the call's test and the jump (0x004B29F2 to 0x004B29FA), which is where the compiler puts loads that both
arms start with, and tests the result with test eax, eax. With that form no zero register is left.

Checked against the binary: the function returns when the next stored AI lord message starts playing (jne at
0x004B2854), and in game it blits only the map surface's rectangle (the two blits are the arms of one test, each
followed by its own end, 0x004B2A2A and 0x004B2A4D). All four of gynt's versions of the file (06829d8, 8ca7861,
10280b1, 1eaeddd) go on from the in-game blit to the menu surface's blit, and 10280b1 and 1eaeddd have an
empty if where the first return is. Our draft 950e83e had both right already.

Measured: quick_diff 0.9895 with 191 instructions on both sides (what differs is how the two listings print a
rep movsd and an import call); reccmp 100.0% (normalized 100.0%) alone, data resolvers and its own resolver on. Not
run in game.

The text against the draft: the unused BOOLEnum using and include are gone; the border style masks are written in
decimal, (32 | 512) for the two tiled styles and 1024, as the bits have no names yet; the imported note on the copy
is gone, as in main's activateModalDialog.

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