Skip to content

One more UI function at 100%: StatusMenus::RenderStatusMenu_Chimp - #463

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

kotijekriv wants to merge 1 commit into
sourcehold:mainfrom
kotijekriv:reimpl/UI-A-chimp-100

Conversation

@kotijekriv

Copy link
Copy Markdown
Contributor

One more function at 100%, with no header changes and no line for cmake/compiler-flags-gl.txt. No behaviour change against gynt's 56.7% draft, which has a return that his older copies miss (below). StatusMenus::RenderStatusMenu_Chimp draws the panel of the selected unit: its picture, its first name, surname and unit type (a ghost has the type only, the five animals no type), what it is doing, and for most unit types a comment, which a newly selected unit also speaks. It is from gynt's draft on his fork's reimpl/UI-StatusMenus-development, and the commit credits him as co-author.

Function Address Before After
StatusMenus::RenderStatusMenu_Chimp 0x00447A50 gynt's draft 56.7% (67318e1; earlier 47.57% at c8bfbb7, 49.6% at 9ec9106, 51.5% at 109e64f) 100.0%, off the /GL list and on it

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.
  • Full build: an all-on build of the previous cut of this branch on main 812d149 (the same code: the last cut changes comments and renames two locals and the inline copy, which compiles to the same listing), with main's own /GL list and with 0x004314E0 (the MapEditorLandscaping_GeneralButtons handler) left out and its resolver off, as in main's baseline: 0 failed of 2,424 compile steps, 99.58 over 2,440 functions, against 99.58 over 2,439 for main alone (built at f489b72; 3458fea only adds files under tools/, and cmake: optional wrapper units built from function-visibility.txt #399 and cmake: function-visibility.txt, which callers must see which callees' bodies #398 add the optional function-visibility units, off by default). RenderStatusMenu_Chimp reads 100.0 raw and normalised there too, with no diff entry. Every main function reads exactly what it read before (unrounded 99.5761 against 99.5759 raw, 99.5494 against 99.5492 normalised; 2,323 against 2,322 functions at 100.0 raw).
  • Our own narrower run: the 133 files of main's /GL list that exist on main, this file, and one file that this PR does not contain (our own copy of TextManager::renderTextFromTextGroup, which this file does not call; see below), all off the list: 145 functions, average 99.57. The function reads 100.0% raw and normalised, with no diff entries, and the same with both files on the list. The other 143 functions of main's list read the same as without the two files. Both builds had an earlier text of this file (one unused include more, other comments, and other names for the copy and for two locals); quick_diff gives the same 195 instructions, line for line, for that text and for this one. The sources were main's at 3458fea; main's two commits since (02fab42, 812d149) add cmake files only, with the new option off.
  • Instructions: the original's 195, in its order; reccmp's diff for the function is empty. The function has no switch table.
  • Please measure with both resolver switches on (the Globals and the function resolvers), as our numbers are.
  • Checks: our local style checker gives 0 ERROR and 5 CHECK (the three placeholder names, at five lines; below); our local fixer finds nothing to change.
  • Not run in game.

Against the drafts

gynt's 56.7% draft (67318e1) and the copy with other global names on his reimpl/Wilps-2026-10-18 (8ca7861) are the decompiler's output and compute what the original computes: the same tests on the unit type, the same texts at the same places, the comment chosen once and spoken for a newly selected unit. Nothing in their control flow is changed here.

His earlier copies do not return when the selected slot no longer holds a unit. They switch back to the build menu, then go on to draw the panel for the empty slot. These are c8bfbb7 (47.57%, carried with his style passes on most of his fork's UI branches, where the status line reads 47.6%) and 9ec9106 (49.6%, #251's head). He recovered the return himself in 109e64f ("Return recovered", 51.5%, on sourcehold's reimpl/UI-StatusMenus), as the original has it (0x00447A84). It is here too.

What got it to 100%

The function is rewritten against the original instructions, in two steps on our dev branch (not public): 87.5%, then 100.0%.

  • The flag for a newly selected unit is cleared in front of the getPeasantGmID call (the original zeroes its register there, 0x00447A8C), and set behind it, as in gynt's 56.7% draft (our 87.5% text had it behind the call).
  • The name text is most likely TextManager::renderTextFromTextGroup (0x00424470), inlined (87.5% to 100.0%, together with the other two points). That function's whole body is renderTextToScreen(getTextStringInGroupAtOffset(group, index), x, y, ...), and the original has exactly these two calls in both arms of the ghost test, with three signs that they come from one inlined call each: the seven outer arguments are pushed once, in front of the branch (0x00447AD2 to 0x00447AF0); the lookup's this is loaded in front of the branch too (0x00447AF1); and the first name's index is in edx, the third scratch register, with the coordinates in eax and ecx (0x00447AC7, 0x00447ACC, 0x00447AFC). Written out by hand as the two nested calls, the instructions are the same, but the compiler gives the three temporaries their registers in another order (the coordinates in edx and eax, the index in ecx), so it cannot load this in front of the branch, and everything behind follows (87.5%). As far as we can tell, an inlined call binds its arguments as the callee's parameters first, and that is what changes the order. The argument temporaries of Reimplement 68 UI::BuildingMenus functions that are 100% #302's BuildingMenus files (the seven outer arguments as locals in push order, here in front of the ghost test, and the two nested calls in each arm) do not give it either: quick_diff 0.8359, the same 195 instructions with other registers.
    • Calling the function through its resolver does not inline it in our builds: with a copy of the function in the build and both files on the /GL list, the call stays a call (75.7%).
    • So the file has a small inline static copy of that function, with its name, its parameter types and its body, and the text manager global in place of this (the object both calls here are made on). The two name texts call it, and a comment says what it is. This is the cheat sheet's "Detecting inlined functions" pattern, and gynt's open draft Reimpl/path finding state development #373 has the same case: its findOwnedGatehouse, findPathUsingClimbingWithHeightMargin16 and setupBabyTreeLocationInfo each have an inline static copy of ViewportRenderState::xyAreValid ("inlined copy of ViewportRenderState::xyAreValid"), a function that is on main at 100%. inline, static inline and __forceinline give the same listing.
  • The current-action call is written at the end of both arms of the ghost test, not once behind it. The original evaluates its two coordinates in each arm, with different registers (0x00447B98 and 0x00447BC5), and shares only the push of the unit and the call (0x00447BDF). Written once, the coordinates are evaluated once.

quick_diff reads 1.0000: 195 instructions against 195.

No line for cmake/compiler-flags-gl.txt

Kept as they are

  • The inline static copy of renderTextFromTextGroup: see above. It makes 0x00424470's two calls, with its arguments, as reimplement OpenSHC::Text::TextManager and OpenSHC::Text::UserTextHandler #265's file does. When reimplement OpenSHC::Text::TextManager and OpenSHC::Text::UserTextHandler #265 lands the copy stays, as Reimpl/path finding state development #373's copies of xyAreValid stay with that function on main: through the resolver the call is not inlined (see the /GL section above). If a resolver call can be inlined one day, the two name texts become calls of TextManager::renderTextFromTextGroup.
  • The current-action call is written twice, with a comment that says why.
  • The ghost test reads the unit's type from the unit again, although the function has it in a local: the original compares the 16-bit field in memory there (0x00447AE7), behind the getPeasantGmID and picture calls. A comment in the code says so.
  • The getPeasantGmID call whose result is dropped: the function only returns a GM ID, and the original calls it and drops the result (0x00447A8E). A comment in the code says so.
  • 1 for the loaded picture, and the positions and font sizes, are plain numbers. Picture 1 is the unit type's sketch: the building and status menu's Prepare (0x00446C00) loads the unit type's chimp picture first and its _sketch.tgx second (0x00447098). A comment in the code says so.

What some placeholders hold (nothing changed here)

  • INT_00b96124: the unit this panel showed last; when it differs from the selected unit, the unit counts as newly selected. The building and status menu's Prepare (0x00446C00) clears it (0x00447139). gynt's reimpl/Wilps-2026-10-18 has it as g_statusMenuLastUnitId.
  • DAT_00b95b6c: the number of the comment text, -1 while none is chosen; the same Prepare sets it to -1 (0x00447009). gynt's Wilps branch: g_statusMenuChimpCommentId. It is undefined4 in the header, compared with -1 here, and holds an int.
  • INT_00b98458: set to the comment's number when the comment is spoken. Nothing in the exe reads it (this store, 0x00447CE1, is its only reference). gynt's Wilps branch: g_lastPlayedChimpCommentId.
  • Unit::rng1_to_70: the surname's number in the surname texts, 0 for a unit without one (gynt's Wilps branch: surnameIndex, the local's name here).
  • The texts (read from the game's text file): TEXT_PEASANT_NAMES are first names ("Harry", "Bob"), TEXT_PEASANT_SURNAMES surnames, TEXT_CHIMP_NAMES the unit types ("Peasant", "Woodcutter"), TEXT_CHIMP_COMMENT the comments ("Morning, your lordship.").

Other PRs

🤖 Generated with Claude Code

StatusMenus::RenderStatusMenu_Chimp, from Gynt's draft (67318e1 at 56.7% on his fork's
reimpl/UI-StatusMenus-development). That draft is the decompiler's output and computes what the original computes;
no behaviour is changed against it. His earlier copies (47.57% at c8bfbb7; 49.6% at 9ec9106, his closed sourcehold#251)
go on after switching back to the build menu for an empty slot; he recovered that return in 109e64f (51.5%,
sourcehold's reimpl/UI-StatusMenus), and it is here as in the original (0x00447A84).
Rewritten against the original instructions:
- the flag for a newly selected unit is cleared in front of the getPeasantGmID call (the original zeroes its
  register there, 0x00447A8C) and set behind it, as in his 56.7% draft;
- the name text is most likely TextManager::renderTextFromTextGroup (0x00424470), inlined. Its body is
  renderTextToScreen(getTextStringInGroupAtOffset(group, index), x, y, ...), and the original has these two calls
  in both arms of the ghost test with the outer arguments pushed once in front of the branch (from 0x00447AD2), the
  lookup's this loaded in front of it too (0x00447AF1) and the first name's index in edx. Written out as the two
  nested calls the instructions are the same, but the three temporaries get their registers in another order and
  this cannot be loaded in front of the branch (87.5%). Called through its resolver the function is not inlined in
  our builds (75.7% with a copy of it in the build, both files on the /GL list; quick_diff 0.8593 with its body in
  the same unit). So the file has an inline static copy of it, with its name, its parameter types and its body and
  the text manager global in place of this, as Gynt's draft sourcehold#373 has copies of ViewportRenderState::xyAreValid in
  three files; inline, static inline and __forceinline give the same listing;
- the current-action call is written at the end of both arms: the original evaluates its coordinates in each arm
  (0x00447B98, 0x00447BC5) and shares the push of the unit and the call (0x00447BDF).

No line for cmake/compiler-flags-gl.txt: the file reads 100.0% off the /GL list, and 100.0% on it too.

reccmp in a build of main's whole /GL list (the 133 files that exist), this file and our own copy of
TextManager::renderTextFromTextGroup (not part of this commit; this file does not call it), all off the list, with
every resolver of those files on: 145 functions, average 99.57. The function 100.0% raw and normalised, no
differing line, and the same with both files on the list. The other 143 functions of main's list read the same as
without the two files. Both builds had an earlier text of this file (one unused include more, other comments, and
other names for the copy and for two locals); quick_diff gives the same 195 instructions, line for line, for that
text and for this one. The sources are main's at 3458fea; main's two commits since (02fab42, 812d149) add
cmake files only, with the new option off.

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