Skip to content

UI: the unit action buttons' render function at 100% - #440

Open
kotijekriv wants to merge 1 commit into
sourcehold:mainfrom
kotijekriv:reimpl/UI-B-round29-100
Open

kotijekriv wants to merge 1 commit into
sourcehold:mainfrom
kotijekriv:reimpl/UI-B-round29-100

Conversation

@kotijekriv

Copy link
Copy Markdown
Contributor

One function at 100%, moved out of our open draft #328, from gynt's draft. No header changes.

Function Address Before reccmp raw Normalised
MenuItems::BuildMenu::MenuItemRenderFunction_BuildMenu_UnitActionButtons 0x00438BE0 40.9% (our draft in #328); gynt's best 40.7% 100.0% 100.0%

Our draft was gynt's code with a mechanical pass and a fix for button 376; gynt's best, 40.7%, is 0523265 on his fork's reimpl/UI-MenuItems-development.

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 own full build.
  • Narrow build: the file alone, as committed in this head, reads 100.0 raw and normalised.
  • Full build: an all-on build of an earlier version of this commit (main 3458fea plus this file, before our own review changes listed below): 0 failed of 2,424 compile steps, 99.57 over 2,440 functions, against 99.58 over 2,439 for main alone (built at f489b72; 3458fea only adds files under tools/). The new function reads 100.0 raw and normalised there too, with no diff entry.
    • What this head changed after that build: enum names for numbers, three truth tests, case 220's hides joined into one test, the OpenSHC:: prefix dropped in front of General_Func, Map, Game and Commands, comments and two includes. Each was measured to compile to the same instructions, and the narrow build above is of this head.
    • Two functions on main 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. UI: three more functions at 100%: the building menu's DoInitial, the credits' prepare and activateModalDialog2 #435's and UI: the credits scroll's fade and the win/defeat window at 100% #437's texts describe the same thing for their builds.
    • Every other function on main reads exactly what it read before.
    • The average: unrounded it is 99.5746 here against 99.5759 for main. The two false readings above take it down by 0.0015; the new function adds 0.0002.
  • Jumps and switch tables: all 45 local jumps land on the same instruction as in the original, and the jump table (12 entries) and the byte table (166 bytes) are the same. We compared them with a script, because our quick listing diff prints every local jump alike and does not look at tables.
  • Checks: our local style checker gives 0 ERROR and 8 CHECK (below); our local fixer finds nothing to change.
  • Not run in game.

What got it to 100%

The function decides for each unit action button of the build menu whether it is drawn: one switch over the button's picture, dispatched at 0x00438C35 through the jump table at 0x0043907C and the byte table at 0x004390AC. A hidden button stores TRUE in DAT_ButtonUnknownZero and returns; a shown one is rendered and stores FALSE.

The drafts had the decompiler's shape. The shared render, the shared renderNumber call and the stance buttons' common end were reached by goto (seven gotos to three labels). Seven locals carried renderNumber's seven arguments to the shared call. Part of the hides were written in place, the rest left by break to a store behind the switch. Our draft read 280 instructions against the original's 277 (0.6715 in our listing diff). The function is written from the original's listing now, without gotos. Five things decide the match:

  • One render stands behind the switch, and the shown paths leave by break. In the original 374, 216, 211 to 215 with 558, and default all jump to one render block (0x00438EC1), and each path that falls into the render has a copy of it. With the render written out at every path the instructions are the same 277, but the block the jumps land on and the stance buttons' common end sit behind 372 (0.9747 in our listing diff).
  • Three plain renders are written out all the same: at 218's end (0x00438CFA), in 219's ranged arm (0x00438EC1, the block the breaks land on) and at 219's end (0x00438EF3).
    • With a break at 218's or at 219's end the compiler keeps a copy of the hide there and jumps to the render, where the original jumps to the hide and keeps the render.
    • With a break in the ranged arm the render block goes to the function's end and case 217 keeps code of its own: 288 instructions against 277.
  • Every hide is written where it is decided (DAT_ButtonUnknownZero = TRUE; return;). The original keeps the 1 in esi in the cases that also compare with it, and the 0 in edi. Its hides share two blocks: 0x00439048 stores the register, 0x0043906C the immediate. A variant with every hide as a break to one store behind the switch does not keep the 1 in a register (241 instructions against 277).
  • The hides in front of a written-out render are one || test (218 and 219's end), and so is the test of default and 217. As separate ifs the match is lost each time. Case 220's six conditions are one test as well; there one test and separate ifs compile the same, and we took the one test so that the file reads the same way throughout.
  • case 217 has a body of its own, the same as default's. The original's jump table has twelve entries. One of them is for 217 alone, and it leads to default's code (0x00439051). With case 217: written in front of default: the table has eleven entries and the byte table other values, while the instructions stay the same.

The two cases that draw a number (220 and one arm of 219) each write their renderNumber call; the compiler shares the call's end (0x00438E95), as the original does.

Several other choices made no difference, and we took the plainer form: 374's, 211's and 216's two hides as one test each, and the stance cases as if / else.

No behaviour differs from our draft, as far as we can tell: the draft already hid button 376 when no engineer is selected (ae5a920).

Things in the file that look odd and are the original's

  • hasEngineerSelected == TRUE in 376 and != TRUE in 211 to 215 with 558. The original compares with 1 there (cmp [hasEngineerSelected], esi with 1 in esi, at 0x00438C41 and 0x0043902E); every other case tests the flag for truth. A truth test loses the match in both places. A comment at each says so.
  • Button 376 renders by a tail call and stores nothing after it (0x00438C4F). Every other shown path stores FALSE again behind its render, although the function already stored FALSE in front of the switch. Both are as in the original.
  • Three plain renders written out beside the shared one, case 217 with default's body again, and the || tests. They are the points above; each has a one-line comment that says which change loses the match. Button 376 and the two number cases have render calls of their own for other reasons: the tail call, and the number drawn behind the render.

Header findings (nothing changed here)

  • DAT_CurrentButtonGmDataIndex is undefined4. It holds a GmDataIndexInt: prepareAndRenderMenuItems stores firstItemTypeData.gmDataIndex & 0x7fffff into it, and the original compares it signed here (jg at 0x00438C12). As a GmDataIndexInt the (int) cast in this switch would go. Left for a header sync.
  • UnitsState.hpp:
    • selectionContainsTunnelersOnly() and selectionContainsShieldmenOnly() return int, but main's implementations return only TRUE or FALSE, like their BOOLEnum neighbours selectionContainsEngineersOnly(), selectionContainsRangedOnlyUnits() and selectionContainsLaddermenOnly().
    • countSelectedCatapultsAndTrebuchets() returns undefined4; main's implementation returns 0, 1 or 2.
    • getFirstSelectedUnitOfEitherType(int param_1, int param_2): main's .cpp calls the parameters unitType_1 and unitType_2.
  • DAT_ButtonUnknownZero: TRUE means "not drawn" in this function. In RenderCurrentNotActiveButtonWithPossibleAlphaTexOnCurrentSurfaceUnk a set flag picks the picture two further on (stateOffset = 2), which looks like a disabled state. A lead for a name; we would not call it "hidden" everywhere.
  • DAT_00ee1090 and DAT_00ee1094 are undefined4 and have no reader on main. This function sets them next to the button's picture (291 or 308, and 202, 198 or 201). As picture numbers they would not fit (291 is GDI_MAPEDIT_BUTTONS_291_PIC_67), so we don't know what they are.

The checker's 8 CHECKs

What Why it stays
hasEngineerSelected == TRUE and != TRUE (two findings) the original compares with 1 there; see the oddities above
a cast on DAT_CurrentButtonGmDataIndex in the switch the header has it as undefined4 and the original compares signed; without the cast ours compares unsigned (ja for jg). See the header findings
the placeholder names DAT_00ee1094 and DAT_00ee1090 (five findings) Ghidra's names; see the header findings

🤖 Generated with Claude Code

BuildMenu::MenuItemRenderFunction_BuildMenu_UnitActionButtons, rewritten from the original's listing. gynt's drafts:
19176b8 at 39.09% ("bug fix, costs 0.6% match") on his fork's reimpl/UI-MenuItems, whose status line records
39.1%, and 0523265 at 40.73% ("if/else") on his fork's reimpl/UI-MenuItems-development, whose status line records
40.7%; the newest commit on the file, dd1a7c4, is on that branch too. Our draft (72d2bc1, then ae5a920 and
07a33e2; 40.9% in the draft PR sourcehold#328) was his code with a mechanical pass and a fix for button 376.

The function decides for each unit action button whether it is drawn: one switch over the button's picture
(DAT_CurrentButtonGmDataIndex, a GmDataIndex value), dispatched at 0x00438C35 through the jump table at 0x0043907C
and the byte table at 0x004390AC. A hidden button stores TRUE in DAT_ButtonUnknownZero and returns; a shown one is
rendered and stores FALSE. The drafts reached the shared render, the shared renderNumber call and the stance
buttons' common end by goto (seven gotos to three labels), carried renderNumber's seven arguments there in seven
locals, and reached part of the hides by break to a store behind the switch. Written without them, five things
decide the match:

- One render stands behind the switch and the shown paths leave by break. In the original 374, 216, 211 to 215 with
  558, and default all jump to one render block (0x00438EC1); each path that falls into the render has a copy.

- Three plain renders are written out all the same: at 218's end (0x00438CFA), in 219's ranged arm (0x00438EC1,
  the block the breaks land on) and at 219's end (0x00438EF3). With a break at 218's or 219's end the compiler keeps
  a copy of the hide there and jumps to the render, where the original jumps to the hide. With a break in the ranged
  arm the render block goes to the function's end and case 217 keeps code of its own (288 instructions against 277).

- Every hide is written where it is decided (DAT_ButtonUnknownZero = TRUE; return;). The original keeps the 1 in
  esi in the cases that also compare with it, and the 0 in edi; its hides share two blocks, 0x00439048 for the
  register and 0x0043906C for the immediate.

- The hides in front of a written-out render are one || test (218, 219's end), and so is the test of default and
  217. As separate ifs the match is lost each time. Case 220's six conditions are one test as well; there one test
  and separate ifs compile the same.

- case 217 has a body of its own, the same as default's. The original's jump table has twelve entries, one of them
  for 217 alone, and it leads to default's code (0x00439051). With `case 217:` in front of `default:` the table has
  eleven entries and the byte table other values, while the instructions stay the same.

The two cases that draw a number each write their renderNumber call; the compiler shares the call's end
(0x00438E95), as the original does.

hasEngineerSelected is compared with TRUE in 376 and in 211 to 215 with 558, because the original compares with 1
there (0x00438C41, 0x0043902E); a truth test loses the match in both places. The case labels and the stores to
DAT_CurrentButtonGmDataIndex use GmDataIndex's names; the siege engine unit types and the two colours use their
enum names too.

Measured: quick_diff 0.9964 with 277 instructions on both sides; the one difference is how the two listings print
376's tail call. All 45 local jumps land on the same instruction and both switch tables are the same, compared by
a script because quick_diff prints every local jump alike. 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