Skip to content

UI: the map editor's properties buttons handler at 100% - #441

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

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

Conversation

@kotijekriv

Copy link
Copy Markdown
Contributor

One function at 100%, moved out of our open draft #328, from gynt's draft. One header change: MenuItemActionHandler_MapEditorProperties_MapDescriptionBox gets the parameter list (int param_1, ...) (first commit); its definition follows (second commit).

Function Address Before reccmp raw Normalised
MenuItems::MapEditorProperties::MenuItemActionHandler_MapEditorProperties_MainButtons 0x00443070 53.2% (our draft in #328); gynt's best 48.4% 100.0% 100.0%

Our draft was gynt's code with a mechanical pass; gynt's best, 48.4%, is 39d2799 on his fork's reimpl/UI-MenuItems-development.

The header change

For button 9 MainButtons calls the description box's handler (0x0042EDF0, at 100% on main) with one argument and cleans the stack itself (push ebp; call; add esp, 4 from 0x004436D8, with 0 in ebp). The header declared the handler with ().

  • It is a menu item's action handler: its address stands in the menu item at 0x005EE8FC, two dwords in front of that item's render function 0x0042EBC0, and nowhere else in the image. It does not read its argument.
  • MapEditorProperties.hpp and MapEditorProperties.func.hpp: the declaration and the resolver's type get (int param_1, ...), the list that MainButtons' handler and the two render functions of this header have. (The two scrollbar functions take five parameters.)
  • The handler's .cpp: the definition takes (int unused, ...), main's wording for a parameter that is not read. The handler is cdecl, so its code is the same: it stays at 100.0 raw and normalised.

Nothing else on main refers to the handler. The first commit does not build alone, because header changes go in a commit of their own.

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).
  • Narrow build: this head's function and the description box's handler, together with main's RenderLoadAndSaveBar (whose address the function stores), their resolvers on: 100.0 raw and normalised for all three.
    • Without RenderLoadAndSaveBar's file in the build the function reads 99.8 (95.2 normalised): the stored address then shows as a trampoline on our side, one line. Measured on this head.
  • Full build: an all-on build of an earlier form of this head: 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/). The new function reads 100.0 raw and normalised there too, with no diff entry, and the description box's handler stays at 100.0.
    • What this head changed after that build: six (char) casts are gone, our own review renamed two indices and one flag's values, flattened one if and added comments, and the header commit was split in two. Each gives the same listing, and the narrow build above is of this head.
    • Every function on main reads exactly what it read before. Unrounded, the average is 99.5761 here against 99.5759 for main.
  • Jumps and switch tables: all 27 local jumps land on the same instruction as in the original, and the jump table (15 entries, 0x00443778) and the byte table (38 bytes, 0x004437B4) are the same. We compared them with a script.
  • Checks: our local style checker gives 0 ERROR and 22 CHECK (below); our local fixer finds nothing to change.
  • Not run in game.

What got it to 100%

The function handles the buttons of the map editor's properties view: one switch over the button's parameter (-6 to 31). Button 31 starts a game on the edited map and copies the scenario's settings into the game state; that case is two thirds of the function, mostly plain stores.

The drafts had the decompiler's shape, with button 31's copies in the order of the original's instructions. Our draft read 411 instructions against the original's 401 (0.5567 in our listing diff). The function is written from the original's listing now. What decided the match:

  • The cases stand in the original's order: 2, 3, 4, 5, 6, 31, 9, 25, then -1 to -6. The draft had 9 and 25 in front of 31 and the negative ones from -6 to -1.
  • The seven euro and seven mercenary recruitable values are copied by one loop with both copies in it. The compiler unrolls it completely. Pair by pair is the order that gives the original's registers; a loop for each array gives others (0.9900 to 0.9576 in our listing diff).
  • The six weapon flags are copied in front of that loop, in the scenario's order (behind it: 0.9712 to 0.9262, measured with the copies written out).
  • The nine euroRecruitableCopy flags are cleared in front of the loop and set behind it by seven ifs. Cleared behind the loop, the first four copies' stores move in front of the clears (0.9900 to 0.9800). The draft set them by = (int)(x != 0).
  • The starting goods are copied by a loop over the 25 entries. The compiler turns it into the original's rep movsd. With memcpy it takes the next statement's value (startGoods[RT_PARTIALPITCH]) from the scenario's array, before the copy; the original reads the copied entry afterwards (0.9900 to 0.9875).
  • The twenty siege unit counts are copied one by one in ascending order; the compiler schedules them in threes backwards, as the original has them.
  • Button 9 calls the description box's handler with 0 (the header change above).

Several other choices gave the same listing, and we took the plainer form: two ifs for button 6's tab in place of the draft's gotos, the scroll-down written flat as main's SingleplayerMapChoice handler has it, and the six score additions without a cast.

No behaviour differs from our draft, as far as we can tell. The draft called the description box's handler without the argument; the handler does not read it.

Names

  • The landscaping tab's 237 and 239 are Commands::M_MAPPER_SUB_MODE_FEATURE and M_MAPPER_SUB_MODE_FEATURE_MP, written through buildMenuTab. Main's MenuItemActionHandler_MapEditorLandscaping_CategoryButtons does the first half of this swap, on its parameter, with the same names.
  • The starting goods' entries 7 and 8 are Game::Resources::RT_PITCH and RT_PARTIALPITCH, as main indexes SEC_StartingResources.
  • The build menu's tab 10 is UI::Enums::BMTT_CASTLE, as main's hideOrUnhideUI writes it.
  • The text index 9 is OpenSHC::Text::TAIT_NINE__FILTER_B, and INT_00b960e4 takes TRUE and FALSE, both as main writes them.
  • The parameter is called button, and the case labels stay numbers, with a comment for each button or pair of buttons. There is no enum for these buttons; main's MenuItemRenderFunction_MapEditorProperties_MainButtons handles the same ones the same way.

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

  • startGoods[RT_PITCH] = startGoods[RT_PARTIALPITCH]; startGoods[RT_PARTIALPITCH] = 0; behind the copy of the starting goods (0x004433CB and 0x004434B6): the scenario keeps this amount under RT_PARTIALPITCH, and the game moves it to RT_PITCH.
  • The score additions cross: column 2 gets siege engine count 3 and column 3 gets count 2; column 8 gets count 5.
  • Button 6 sets all nine currentPlayerFullIDArray entries to -1 in a loop and then entry 1 to 1. The original has eight stores of -1 and one of 1; the compiler drops the loop's store into entry 1.

Header findings (nothing changed here)

  • ActiveMenuTab (the union behind GameCore's ...MenuTabToSwitchTo fields) has no member for the landscaping menu's values, which are MappersEnum's sub modes (231 to 239). Main writes them through buildMenuTab, and so does this file.
  • MenuTextInputState::DAT_SomeTextArrayIndex is undefined4. It holds a text array index: main stores TAIT_NINE__FILTER_B and UserTextHandlerState::textArrayIndex into it.
  • DAT_00b960f4 and INT_00b95b74: the description's arrows move DAT_00b960f4 by 14 between 0 and INT_00b95b74 - 231. A lead: the description's scroll position and its height.
  • AttackInfo::someSinglePlayerScore (byte[50][10]) is indexed by the attack wave here (inv_count), and its columns get the scenario's siege engine counts. A lead: per wave, not a score.

The checker's 22 CHECKs

19 are Ghidra's placeholder names (four globals and five field_0x.. fields); one says the header change stays in the PR until main has it; one asks about the resolver's type, which has the form of the header's other (int param_1, ...) resolvers; one notes the numeric case labels (see "Names").

🤖 Generated with Claude Code

kotijekriv and others added 3 commits November 21, 2026 04:18
…takes (int param_1, ...)

MenuItemActionHandler_MapEditorProperties_MainButtons (0x00443070) calls the handler at 0x0042EDF0 with one
argument and cleans the stack itself (push of the 0 it keeps in ebp, call, add esp, 4, from 0x004436D8). The
handler is a menu item's action handler: its address stands in the menu item at 0x005EE8FC, two dwords in front of
that item's render function 0x0042EBC0, and nowhere else in the image. So it gets the parameter list that
MainButtons' handler and the two render functions of this header have, (int param_1, ...), and not ().

Changed: the declaration in MapEditorProperties.hpp and the resolver's type in MapEditorProperties.func.hpp. The
definition's signature follows in the next commit; the branch builds from there on.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
…finition follows its header

The header commit before this one gives the handler at 0x0042EDF0 the parameter list (int param_1, ...). The
definition takes (int unused, ...) now. The handler does not read the parameter and is cdecl, so its code is
unchanged: reccmp 100.0 (normalized 100.0) as before.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
MapEditorProperties::MenuItemActionHandler_MapEditorProperties_MainButtons, rewritten from the original's listing.
gynt's draft: 39d2799 at 48.43% ("Style pass, asm differs") on his fork's reimpl/UI-MenuItems-development, whose
status line records 48.4%; the newest commit on the file there is dd1a7c4. Our draft (59372c9, then d79d89d
and 6202bb0; 53.2% in the draft PR sourcehold#328) was his code with a mechanical pass.

The function handles the buttons of the map editor's properties view: two early exits while a dialog is open, then
one switch over the button's parameter (-6 to 31), dispatched through the byte table at 0x004437B4 and the jump
table at 0x00443778. Button 31 starts a game on the edited map and copies the scenario's settings into the game
state; that case is two thirds of the function. What decided the match:

- The cases stand in the original's order: 2, 3, 4, 5, 6, 31, 9, 25, -1 to -6. The draft had 9 and 25 in front of
  31 and the negative ones from -6 to -1.

- The seven euro and seven mercenary recruitable values are copied by one loop over the index with both copies in
  it; the compiler unrolls it completely. Pair by pair is the order that gives the original's registers: the
  first four euro values end in eax, ecx, edx and esi and stay there for the tests behind the loop, the first two
  mercenary values go through edx, the third through esi and everything later through edi. A loop for each array
  gives other registers (quick_diff 0.9900 -> 0.9576). The draft had the twenty short copies in the order of the
  original's instructions, which the compiler does not keep.

- The six weapon flags are copied in front of that loop, in the scenario's order (behind it: 0.9712 -> 0.9262 with
  the copies written out).

- The nine euroRecruitableCopy flags are cleared in front of the loop and set behind it by seven ifs. Cleared
  behind the loop, the first four copies' stores move in front of the clears (0.9900 -> 0.9800). The draft set
  them by `= (int)(x != 0)`.

- The starting goods are copied by a loop over the 25 entries, which the compiler turns into the original's
  rep movsd. With memcpy it reads the next statement's value (startGoods[RT_PARTIALPITCH]) from the scenario's
  array and loads it before the copy, where the original reads the copied entry afterwards (0.9900 -> 0.9875).

- The twenty siege unit counts are copied one by one in ascending order; the original's instructions run in
  threes backwards, which is the compiler's scheduling of exactly that order.

- Button 6's tab test is two ifs: the feature tab becomes its multiplayer version on a multiplayer map and the
  multiplayer version becomes the feature tab on a single-player one. The draft's gotos compile to the same
  listing (measured on this text).

- Button 9 calls the description box's handler with 0, which the two commits before this one allow.

Names: the landscaping tab's 237 and 239 are Commands::M_MAPPER_SUB_MODE_FEATURE and M_MAPPER_SUB_MODE_FEATURE_MP,
written through buildMenuTab (main's MenuItemActionHandler_MapEditorLandscaping_CategoryButtons does the first half
of this swap, on its parameter, with the same names); the build menu's 10 is UI::Enums::BMTT_CASTLE (the draft's
BASMTT_HUNTERSHUT has the same value in the other enum of the union); the text index 9 is
OpenSHC::Text::TAIT_NINE__FILTER_B as main stores it into the same field; startGoods' entries 7 and 8 are
Game::Resources::RT_PITCH and RT_PARTIALPITCH, as main indexes SEC_StartingResources; INT_00b960e4 takes TRUE and
FALSE as main writes it. The parameter is called button and the case labels stay numbers, with a comment for each
button or pair of buttons, as in main's MenuItemRenderFunction_MapEditorProperties_MainButtons for the same
buttons. The description's scroll-down is written flat, as main's SingleplayerMapChoice handler has the same
scroll; nested it compiles to the same code.

quick_diff ends at 0.9900 with four hunks that are its own printing (three stores into TileMapState fields shown
as [G]+G, the operands of rep movsd). Narrow build with those two commits and the two finished callees the
function refers to in the sources list, their resolvers on (the description box's handler, and
RenderLoadAndSaveBar, whose address it stores): reccmp 100.0, normalized 100.0, and both callees stay at 100.0.
Without RenderLoadAndSaveBar's file in the list the stored address shows as a trampoline on our side: one line,
99.8 (95.2 normalized).

Co-authored-by: Gynt <gynt@users.noreply.github.com>
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
@github-actions github-actions Bot added the declaration/type change This requires changes to the generated/exported types or definitions. label Nov 21, 2026

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

declaration/type change This requires changes to the generated/exported types or definitions.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant