Skip to content

UI: the confirmation dialog's answer handler at 100% - #442

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

kotijekriv wants to merge 1 commit into
sourcehold:mainfrom
kotijekriv:reimpl/UI-B-round32-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. One behaviour fix against the draft (below).

Function Address Before reccmp raw Normalised
MenuItems::General::MenuItemActionHandler_General_LaunchOrQuitMultiplayerGameUnk 0x00494950 64.2% (our draft in #328) 100.0% 100.0%

Our draft was gynt's code with a mechanical pass; his file is on his fork's reimpl/UI-MenuItems-development, and his status line has no number for it. In the narrow build described below (without resetMouseState2 in the build) the draft read 58.3% (65.7% normalised).

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 file as committed, together with two of main's files it refers to, their resolvers on: RenderLoadAndSaveBar (0x00493690), whose address it stores, and resetMouseState2 (0x004689D0), which every path but the multiplayer exit tail-calls. 100.0 raw and normalised for all three.
    • Without resetMouseState2's file in the build the function reads 97.2 (100.0 normalised): each tail jump then shows as a trampoline on our side.
  • 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.
    • What this head changed after that build: comments, and our own review's edits: the inner cases in ascending order, no braces around one case, the local's type name, FALSE for one 0, and the backup map's name taken from string-macros.hpp. 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 49 local jumps land on the same instruction as in the original, and the jump table (5 entries, 0x00495070) and the byte table (38 bytes, 0x00495084) are the same. We compared them with a script.
  • Checks: our local style checker gives 0 ERROR and 5 CHECK (below); our local fixer finds nothing to change.
  • Not run in game.

The behaviour fix

The function switches over the open dialog's question, DAT_MenuOptionsActionParameter. Question -7 is the scenario description's "back to the map choice?" in the builder, asked when a game is suspended (MenuItemActionHandler_ScenarioDescription_Main, buttons 12 and 13). With no suspended game that handler resets the colours and opens the single-player map choice itself, which is what "yes" now does too. The draft, like gynt's text, first tested the question with < 44 on the field as the header types it (undefined4, unsigned), and that compiled to an unsigned compare. So -7 never reached its case -7: inside that branch: it took the other side and ended in the default, and "yes" only closed the dialog.

The original compares signed (jg at 0x004949E2) and has -7 in its jump table: it puts the player colours back to their defaults and then runs 7's and 1000's code, which for -7 ends in the single-player map choice. The fix comes from having one switch over all questions; the (int) makes its dispatch the original's.

What got it to 100%

The function answers the two buttons of a confirmation dialog: 22 carries out what the dialog asks, 23 declines it. Every path ends in resetMouseState2, except a multiplayer game's return to its lobby. Button 22 is one switch over the question (-7, 7, 9, 30, 43, 44, 47, 49, 1000, 2000 and a default); its largest part leaves the running game (7, 1000 and, after the colours, -7).

The drafts had the decompiler's shape: range tests around a partial switch, nine gotos to five labels, and resetMouseState2(); return; written out in every path. The function is written from the original's listing now, without gotos. What decided the match, each measured with one thing changed (the variants were built before the last edits, which do not change the listing):

  • The switch is on (int) of the field. On the field as it is, the dispatch is unsigned and has no table entry for -7: 71.4% (26.0% normalised).
  • resetMouseState2 is called once, after the outer switch, and every path leaves by break. The original has a copy of the call at every path's end, and only its conditional jumps go to the last copy. With resetMouseState2(); return; written out in every path the compiler merges equal ends across cases: 83.7% (71.5%). That variant changes two things: the leaving code then has to stand after the inner switch.
  • In the leaving code (inside case 7: case 1000:) the multiplayer exit tests the field, and the local for the game mode is read after the test. Read before it, the game mode and the constant 1 swap esi and edi through the whole function: 95.8% (89.7%). Without a local: 85.4% (85.1%).
  • Case 30 is one if / else with one break after it. The currentModalDialog test at the end of the leaving code shares the call of clearAnyOtherModalDialogs with case 43 (jne at 0x00495056); with a break in 30's first arm it shares 30's call: 99.8% (95.2%).
  • Case 49 closes the dialog itself, and default does the same in its own two lines. With 49 falling into a default written right after it, the compiler folds default's code into 2000's equal end, and default's three jumps and its table entry land after 2000's code instead of after 49's: 99.2% (95.2%).
  • In 44 the trail part is if (isSkirmishTrail == TRUE) { the three trail tests } with the lobby restore after it. The draft had != TRUE with the restore first: 87.0% (73.5%).
  • The menu to return to is chosen by nested if / else in the original's order, with switchToMenuView written in every arm. The compiler shares the call itself. The two mission select views as one call with ?:: 92.8% (79.3%).

Several other choices gave the same listing, and we took the plainer form: the three trail tests as one if / else if chain with one break, case 22 before case 23, gameMode_2 read from the field in 44, the inner cases in ascending order with default last, and no braces around case 22.

One string rule: the file copies ".map" inline with strcat, which needs the macro from string-macros.hpp. So it does not include string-literals.hpp, and the backup map's name is the macro STR_AUTOBACKUPMAP too (the draft mixed the two headers). Same listing.

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

  • isSkirmishTrail == TRUE in 44. The original compares with 1 there (cmp with the 1 in edi at 0x00494C2C); the same field is tested for truth in the leaving code (0x00494F6F). As a truth test in 44: 99.6% (90.9%). A comment says so.
  • resetMouseState2 is called twice on two paths: 44 calls it first (0x00494BAA) and the leaving code calls it after disconnectDPlay (0x00494E0F), and both end in it like every path.
  • A multiplayer game's exit returns without resetMouseState2: it tail-calls initMultiplayerLobbyState (0x00494A89).

Header findings (nothing changed here)

  • MenuTextInputState::DAT_MenuOptionsActionParameter is undefined4, and one of its values is -7. Main already has the same (int) cast on it in MenuModalRenderFunction_YesNoDialog (0x00493390). Retyping the field to int would drop both casts; none of its other uses on main depends on the sign. This PR keeps the cast because it changes no headers.
  • The status list has 0x00494DB0 as a pending function. It is inside this function (0x00494950 to 0x0049506F): the aligned head of the inlined strcat's scan loop. It can never be reimplemented on its own.
  • GameCore::field_0x6c is 0 or 1 only, a BOOLEnum candidate. When it is set, this function loads auto_backup_map.map and returns to the map editor's properties view, then clears it. The map editor's play test sets it (button 31 of the properties view). A lead for a name: the game is a test of the map being edited.

The checker's 5 CHECKs

What Why it stays
the cast on DAT_MenuOptionsActionParameter in the switch without it the dispatch is unsigned (71.4%); main's YesNoDialog has the same cast, and a retype to int would drop both, but this PR changes no headers
isSkirmishTrail == TRUE the original compares with 1 there; see the oddities above
12 case labels are numbers the dialog's questions have no enum; main writes them as numbers where it stores them
the placeholder name field_0x6c (two findings) Ghidra's name; see the header findings

🤖 Generated with Claude Code

General::MenuItemActionHandler_General_LaunchOrQuitMultiplayerGameUnk, rewritten from the original's listing.
gynt's draft is the file on his fork's reimpl/UI-MenuItems-development (first in 4e57118, newest commit on it
b3069fd); his status line has no number for it. Our draft (791f92c, then d79d89d; 64.2% in the draft PR sourcehold#328)
was his code with a mechanical pass and strcat for the map name's extension.

The function answers the two buttons of a confirmation dialog: 22 carries out what the dialog asks
(DAT_MenuOptionsActionParameter), 23 declines it. Every path ends in resetMouseState2 except a multiplayer game's
return to its lobby. 22 is one switch over the question (-7, 7, 9, 30, 43, 44, 47, 49, 1000, 2000 and a default),
dispatched through a compare tree, the byte table at 0x00495084 and the jump table at 0x00495070.

One behaviour fix. Question -7 is asked by the scenario description's handler (it stores -7 into
DAT_MenuOptionsActionParameter). The draft, like gynt's text, first tested the question with `< 44` on the field as
the header types it (undefined4, unsigned) and compiled to an unsigned compare. So -7 never reached its case: it
took the other side and ended in the default, and "yes" only closed the dialog. The original compares signed (jg at
0x004949E2) and has -7 in the jump table: it puts the player colours back to their defaults and then runs 7's and
1000's code, which for -7 ends in the single-player map choice. The fix comes from having one switch over all
questions; the (int) makes its dispatch the original's.

What decided the match, each measured with one thing changed (reccmp in the narrow build described below; the
variants were built on the text before its last edits, which do not change the listing):

- The switch is on (int) of the field. On the field as it is: 71.4% (26.0% normalized). Main has the same cast on
  the same field in MenuModalRenderFunction_YesNoDialog (0x00493390).

- resetMouseState2 is called once, after the outer switch, and every path leaves by break. The original has a copy
  of it at every path's end and only the conditional jumps (button neither 22 nor 23, three trail tests) go to the
  last one. With `reset; return;` written out in every path, which also means the leaving code has to stand after
  the inner switch (two changes in this variant): 83.7% (71.5%).

- The leaving code (7, 1000 and, after the colours, -7) stands inside `case 7: case 1000:`. The compiler puts its
  first test at 7's place and the rest after the last case, as in the original.

- In that code the multiplayer exit tests the field and the local for the game mode is read after the test. Read
  before it, the game mode and the constant 1 swap esi and edi in the whole function: 95.8% (89.7%). Without a
  local: 85.4% (85.1%).

- Case 30 is one if / else with one break after it. The currentModalDialog test at the end of the leaving code
  shares the call of clearAnyOtherModalDialogs with 43 (jne at 0x00495056 to 0x00494B4A); with a break in 30's
  first arm it shares 30's: 99.8% (95.2%).

- Case 49 closes the dialog itself and default does the same in its own two lines. With 49 falling into a default
  written right after it, the compiler folds default's code into 2000's equal end, and default's three jumps and its
  table entry land after 2000's code (the original has them after 49's, 0x00494D78): 99.2% (95.2%).

- In 44 the trail part is `if (isSkirmishTrail == TRUE) { the three trail tests }` with the lobby restore after it;
  the draft had `!= TRUE` with the restore first: 87.0% (73.5%). The original compares with 1 there (cmp with edi
  at 0x00494C2C); as a truth test: 99.6% (90.9%).

- The menu to return to is chosen by nested if / else in the original's order, with switchToMenuView written in
  every arm; the compiler shares the call itself (pushes in the arms, one call at 0x00495046). The two mission
  select views as one call with ?: read 92.8% (79.3%).

These compile to the same listing, and the plainer form stands: the three trail tests as one if / else-if chain
with one break; `case 22` before `case 23`; gameMode_2 read from the field in 44; the inner cases in ascending
order with default last (the compare tree decides where their code goes); no braces around case 22;
Game::GameModeInt for the local; FALSE for field_0x6c; and the backup map's name as the string-macros.hpp macro,
because the file uses a macro for strcat's inline copy and must not include string-literals.hpp beside it.

Measured in a narrow build on main 3458fea of this file with the two finished functions it refers to in the
sources list, their resolvers on: RenderLoadAndSaveBar (0x00493690), whose address it stores, and resetMouseState2
(0x004689D0), which every path but the multiplayer exit tail-calls. reccmp 100.0, normalized 100.0, and both of them
stay at 100.0. Without resetMouseState2's file in the list the function reads 97.2% (100.0% normalized): each tail
jump then shows as a trampoline. The draft read 58.3 (65.7 normalized) in the same build without resetMouseState2.

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