Skip to content

UI menu items: 8 functions, none at 100% yet - #530

Draft
gynt wants to merge 20 commits into
mainfrom
reimpl/ui-menuitems-development
Draft

gynt wants to merge 20 commits into
mainfrom
reimpl/ui-menuitems-development

Conversation

@gynt

@gynt gynt commented Nov 30, 2026 •

Copy link
Copy Markdown
Contributor

8 menu-item render and action handlers, rebuilt from the asm. The functions were ported from gynt's catch-all branches: this branch is origin/main plus the copied files, compile fixes, matching work and status.

Measured with reccmp, with both the struct and the function resolvers enabled and main's /GL callees on.

0 of 8 functions at 100%. The others hit a wall: the blocker named for each was measured, and the source forms tried against it tied or scored worse.

Hit a wall

Function Address Match Ported at Why it stops there
Roundtable::MenuItemRenderFunction_Roundtable_Main 0x004AEF70 97.2% 59.1% Scheduling of the AI name index loads.
BuildingAndStatusMenu::MenuItemRenderFunction_BuildingAndStatusMenu_BuildingWorkStatus 0x004476B0 87.8% 69.0% employees and needed swap eax/edx; the needed arms share the x/y loads.
General::MenuItemRenderFunction_General_EventButtonAndData 0x004BFFA0 87.6% 87.6% The original hoists the constant pushes above the branch and pushes the colour as an immediate per arm, loading the month per arm in different registers. Every two-call form we write hoists the shared month read (68 or worse); the kept colour-variable form is best.
SoundOptions::MenuItemActionHandler_SoundOptions_Buttons 0x004940B0 86.6% 78.0% The original keeps 0 in eax for the toggle; ten source forms tie.
General::MenuItemActionHandler_General_CreateEventCallbackFunction 0x004C1770 86.3% 46.3% The second zero register is ebx in the original and ebp here; tail-merge choices of a few cases.
Unused::MenuItemActionHandler_UnusedCreateMessageEvent_SetTextUnk 0x004BFE20 84.0% 24.1% Ours cross-jumps the 0x13 arm into the 0x25 arm's activate/sort/reset tail; the original keeps its own copy. Four case orders, a guarded body and break vs return all tie or lose.
TextEditor::MenuItemRenderFunction_TextEditor_Scrollbar 0x0045EF90 82.4% 51.6% y and right - 1 swap ebp/edi.
MenuItem::handleMenuElementsCallbacks 0x004F4290 67.0% 42.0% ebx/ebp role swap, and the slider spills the thumb position.

Behaviour fixes

These were wrong in the decompiler output and are fixed against the binary:

  • Roundtable_Main: the selected-seat box and team icon no longer return early, the dragged-seat face no longer gets icon 0x21c drawn over it, and the trailing interacting/blend block runs only for button 0x14.
  • BuildingWorkStatus: with needed workers and some employed only "needed + 4" is shown (it also printed "No labour"), and the function goes on to the building-type switch when no worker is a peasant on his way.
  • CreateEventCallbackFunction: bandits and archers opened the event slider dialog twice.

Type and header changes are in their own types: commits. Resolver flags are not committed.


🤖 Generated with Claude Code

https://claude.ai/code/session_01XmXCmXNCM9MzSpvzeakUrQ

gynt and others added 11 commits November 28, 2026 13:56
…s-development

Their best match lives only on gynt's catch-all branches, which are ~2080 cpp
files ahead of their merge base and 412 commits behind main. The cpp files are
copied unchanged, without the headers of those branches:
- gynt/reimpl/UI-DisplayElements-pr-development@fbc092e15 (1)
- gynt/reimpl/UI-MenuItems-development@405b98871 (7)

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XmXCmXNCM9MzSpvzeakUrQ
Renamed fields, globals and resolvers by address, enum-typed arguments,
array fields indexed directly, clang-format.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XmXCmXNCM9MzSpvzeakUrQ
SHC_3BB0A8C1_0x004476B0 69.0% MenuItemRenderFunction_BuildingAndStatusMenu_BuildingWorkStatus
SHC_3BB0A8C1_0x0045EF90 51.6% MenuItemRenderFunction_TextEditor_Scrollbar
SHC_3BB0A8C1_0x004940B0 78.0% MenuItemActionHandler_SoundOptions_Buttons
SHC_3BB0A8C1_0x004AEF70 59.1% MenuItemRenderFunction_Roundtable_Main
SHC_3BB0A8C1_0x004BFE20 24.1% MenuItemActionHandler_UnusedCreateMessageEvent_SetTextUnk
SHC_3BB0A8C1_0x004BFFA0 87.6% MenuItemRenderFunction_General_EventButtonAndData
SHC_3BB0A8C1_0x004C1770 46.3% MenuItemActionHandler_General_CreateEventCallbackFunction
SHC_3BB0A8C1_0x004F4290 42.0% handleMenuElementsCallbacks

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XmXCmXNCM9MzSpvzeakUrQ
- UnusedCreateMessageEvent_SetTextUnk 24.1 -> 84.0: a switch with the arms in
  the original's layout order (0x25, 0x13, -1), month reset at >= 12. Left: our
  compiler merges the 0x13 arm's trailing calls into the 0x25 arm.
- TextEditor_Scrollbar 51.6 -> 76.5: the right edge is x + 20 kept in a local,
  horizontal lines end at right - 1. Left: register naming.
- SoundOptions_Buttons 78.0 -> 86.6: the sound toggle is a plain if/else with
  tail calls (Ghidra turned it into a fall-through), cases in jump-table target
  order, the slider locals scoped to their cases so the other arms tail-call.
  Left: the original keeps 0 in a register for the compare and the store.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XmXCmXNCM9MzSpvzeakUrQ
76.5 -> 82.4. Declaring `top` ahead of drawScrollbar gives it ebx as in
the original; y and right - 1 still trade ebp and edi.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XmXCmXNCM9MzSpvzeakUrQ
69.0 -> 87.8.

- With needed workers and some employed, only "needed + 4" is shown; the
  old code also printed "No labour" after it.
- When none of the workers is a peasant on his way, the function goes on
  to the building-type switch instead of returning, as the original does.
  The unit it passes there is the last worker the loop read; with no
  workers it is an uninitialised local, which is the stack slot the
  original reads.
- Plain loops and named locals replace the pointer walks and iVar names.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XmXCmXNCM9MzSpvzeakUrQ
59.1 -> 97.2.

- The selected-seat dimBox and the team icon no longer return early; in
  the original both fall through to the name text.
- In the dragged-seat (200) branch, drawing the face now returns instead
  of falling through to draw icon 0x21c on top of it.
- The trailing interacting/blend block is gone from the end of the
  function: the original only runs it for button 0x14.

The AI name lookup is an inline argument of the render call, and the
empty-seat exit shares the final epilogue. What is left is the
scheduling of the AI name index loads.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XmXCmXNCM9MzSpvzeakUrQ
42.0 -> 62.2.

- Buttons come before sliders, and the menu->one slider arm before the
  other, as in the original's fall-through order.
- Each slider arm declares its own min/max/value pointers; the first
  clamp of the dragging arm reads the fields directly.
- The right-click (type 4) and left-click paths share one click block,
  selected by a ternary instead of the decompiler's goto.
- The sound arm tests sound != 1 first, so the 2..7 range is the
  fall-through.

What is left is register allocation: ebx and ebp hold each other's roles
(constant 1 and the timeGetTime pointer), and the slider keeps thumb in a
stack slot where the original uses ebx.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XmXCmXNCM9MzSpvzeakUrQ
46.3 -> 86.3 (measured with the finished clearAnyOtherModalDialogs
resolver enabled, as for the rest of the namespace).

- Bandits and archers (0x92, 0x94) opened the event slider dialog twice;
  the original opens it once.
- Each case calls playEventVideoBik with its own text index, video and
  wav, then clears the dialogs, instead of staging them in shared
  variables for a call after the switch. The original pushes the
  literals per case and jumps into one shared call.
- The 192 in-range no-op values are the switch default, as the index
  table shows.
- unitSelectionIndex is initialised at the top: the original zeroes edi
  in the prologue and the bandits loop starts from it.
- The lion case scopes its spawn x local to its block. With a
  function-scope address-taken local MSVC emits no tail calls anywhere,
  while the original tail-calls clearAnyOtherModalDialogs.

What is left is the second zero register (ebx in the original, ebp
here) and the tail-merge choices of a few cases.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XmXCmXNCM9MzSpvzeakUrQ
The original loads *min before computing the range and reuses that one
value for the `+ min` and the clamp after the store to *value. The
range gets its own local instead of reusing the x-position variable.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XmXCmXNCM9MzSpvzeakUrQ
The original checks for the text/state item type first and returns 0 at
once when its right click is not set, while every other type tests the
left click; the ternary over both flags compiled to a load and test of
a selected flag instead. Spelling out the two arms gives the original's
compare-against-memory form.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XmXCmXNCM9MzSpvzeakUrQ
@github-actions

Copy link
Copy Markdown

Style guide reading (advisory, posted once)

37 possible deviation(s) from REIMPLEMENTATION_STYLE_GUIDE.md across 6 of the 8 reimplemented .cpp file(s) in this PR.

The match comes first. Where the ugly form is what matched, it stays -- that is the guide's own overriding rule, and rule 21 asks for a one-line comment saying what was measured so the next reader does not retry it. This report is posted once, when the PR opens, and is not repeated on later pushes.

rule what it asks hits
9 Prefer for over while/do 4
11 No magic addresses 2
16 Give variables a name that says what they hold 7
18 Use the name, not the number 23
22 Prefer descriptive code 1
Where

Rule 9 — Prefer for over while/do

  • src/OpenSHC/UI/MenuItems/General/MenuItemActionHandler_General_CreateEventCallbackFunction.cpp:334 — do-loop; the guide asks for a for
  • src/OpenSHC/UI/MenuItems/General/MenuItemActionHandler_General_CreateEventCallbackFunction.cpp:390 — do-loop; the guide asks for a for
  • src/OpenSHC/UI/MenuItems/General/MenuItemActionHandler_General_CreateEventCallbackFunction.cpp:436 — do-loop; the guide asks for a for
  • src/OpenSHC/UI/MenuItems/General/MenuItemActionHandler_General_CreateEventCallbackFunction.cpp:447 — do-loop; the guide asks for a for

Rule 11 — No magic addresses

  • src/OpenSHC/UI/MenuItems/Unused/MenuItemActionHandler_UnusedCreateMessageEvent_SetTextUnk.cpp:30 — literal address 0xFFFFFFFF
  • src/OpenSHC/UI/MenuItems/Unused/MenuItemActionHandler_UnusedCreateMessageEvent_SetTextUnk.cpp:45 — literal address 0xFFFFFFFF

Rule 16 — Give variables a name that says what they hold

  • src/OpenSHC/UI/MenuItems/General/MenuItemActionHandler_General_CreateEventCallbackFunction.cpp:65 — decompiler name sVar1
  • src/OpenSHC/UI/MenuItems/General/MenuItemActionHandler_General_CreateEventCallbackFunction.cpp:67 — decompiler name dVar5
  • src/OpenSHC/UI/MenuItems/General/MenuItemActionHandler_General_CreateEventCallbackFunction.cpp:68 — decompiler name dVar6
  • src/OpenSHC/UI/MenuItems/General/MenuItemActionHandler_General_CreateEventCallbackFunction.cpp:69 — decompiler name iVar7
  • src/OpenSHC/UI/MenuItems/General/MenuItemActionHandler_General_CreateEventCallbackFunction.cpp:73 — decompiler name iVar8
  • src/OpenSHC/UI/MenuItems/SoundOptions/MenuItemActionHandler_SoundOptions_Buttons.cpp:46 — decompiler name local_8
  • src/OpenSHC/UI/MenuItems/SoundOptions/MenuItemActionHandler_SoundOptions_Buttons.cpp:47 — decompiler name local_4

Rule 18 — Use the name, not the number

  • src/OpenSHC/UI/MenuItem/handleMenuElementsCallbacks.cpp:72 — hex constant 0x7FFFFF; use a name, or decimal
  • src/OpenSHC/UI/MenuItems/General/MenuItemActionHandler_General_CreateEventCallbackFunction.cpp:341 — hex constant 0x1D32C38; use a name, or decimal
  • src/OpenSHC/UI/MenuItems/General/MenuItemRenderFunction_General_EventButtonAndData.cpp:46 — hex constant 0xC2F0EB; use a name, or decimal
  • src/OpenSHC/UI/MenuItems/General/MenuItemRenderFunction_General_EventButtonAndData.cpp:50 — hex constant 0xCCFAFF; use a name, or decimal
  • src/OpenSHC/UI/MenuItems/Roundtable/MenuItemRenderFunction_Roundtable_Main.cpp:67 — hex constant 0x222; use a name, or decimal
  • src/OpenSHC/UI/MenuItems/Roundtable/MenuItemRenderFunction_Roundtable_Main.cpp:72 — hex constant 0x21B; use a name, or decimal
  • src/OpenSHC/UI/MenuItems/Roundtable/MenuItemRenderFunction_Roundtable_Main.cpp:76 — hex constant 0x21C; use a name, or decimal
  • src/OpenSHC/UI/MenuItems/Roundtable/MenuItemRenderFunction_Roundtable_Main.cpp:80 — hex constant 0x21D; use a name, or decimal
  • src/OpenSHC/UI/MenuItems/Roundtable/MenuItemRenderFunction_Roundtable_Main.cpp:88 — hex constant 0x20A; use a name, or decimal
  • src/OpenSHC/UI/MenuItems/Roundtable/MenuItemRenderFunction_Roundtable_Main.cpp:99 — hex constant 0x202; use a name, or decimal
  • ... and 13 more

Rule 22 — Prefer descriptive code

  • src/OpenSHC/UI/MenuItem/handleMenuElementsCallbacks.cpp:87 — unsigned range trick; spell the two bounds out

gynt and others added 6 commits December 3, 2026 23:00
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XmXCmXCVBvESeaU1djQFjp
The .cpp is unchanged; the full build plus reccmp after merging origin/main
(both resolver sets enabled) measures 86.0 instead of the recorded 86.3.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XmXCmXCVBvESeaU1djQFjp
… two

BuildingWorkStatus: compare the building type straight from the building
(no BuildingTypeShort local: that cost a second movzx per compare) and
pick the "No labour"/"needed + 4" text index into a local before a single
renderText2 call. reg_scores.py shows why: with two calls the `needed`
local (benefit 18) outranks `employees` (16) and takes eax, while the
original has employees in eax; with the text index in a local the
original's register assignment comes out. The original still has two
calls there (only the four constant pushes hoisted), so the shared call
is the remaining difference. quick_diff 0.9032 -> 0.9538, reccmp 87.8% ->
89.0%.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XmXCmXCVBvESeaU1djQFjp
…0x24

handleMenuElementsCallbacks: declare the slider's offset and insideThumb
(both start at 0) before the "no click" early return, as the original
does: the zero then lives in eax for the two flag compares and the
return value. reccmp 67.0% -> 70.7%. The rest is register allocation in
the slider/scrollbar half: the original keeps size in ebp and the item x
in ebx with a 0x24 frame, ours spills size and needs 0x28.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XmXCmXCVBvESeaU1djQFjp
CreateEventCallbackFunction: write the stone keep and the stronghold as
two else-if arms with identical bodies (the original's `cmp 0x29; je;
cmp 0x2a; jne; body` is the cross-jumped pair, not an `||`), and test
xyAreValid's result directly instead of through Ghidra's comma
expression (`test eax, eax` instead of `cmp eax, <zero reg>`). reccmp
86.0% -> 87.0%. Remaining: the function-wide zero lives in ebp, the
original's in ebx. reg_scores.py: the zero is served late with ebx at
cost 0 and ebp/esi/edi at -1 (copy affinities), so it takes ebp; that
also changes the push order, the [esp+N] offsets inside the cases and
which copy of the shared playEventVideoBik tail survives cross-jumping.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XmXCmXCVBvESeaU1djQFjp
…/tribe local

The original keeps case 0x91's spawn x and case 0x94's second (monk) tribe
in the same stack slot ([esp + 0x10]). So declare one function-scope local
for both instead of a case-local spawnX plus a dStack_4.

Because the local is address-taken, the monk tribe now stays on the stack
as in the original, instead of taking ebx. That frees ebx for the
function-wide zero, which reg_scores had shown going to ebp. The constant
2 (ULS_NORMAL) again takes ebx in the case 0x92/0x94 loops.

Remaining: dVar5/dVar6 and the first loop counter in case 0x94 sit in
edi/ebp swapped, and some playEventVideoBik/clearAnyOtherModalDialogs
tails are laid out differently.

reccmp 87.0% -> 90.0% (full build, both resolver sets enabled).

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XmXCmXCVBvESeaU1djQFjp
gynt added a commit to gynt/OpenSHC that referenced this pull request Dec 4, 2026
- SoundOptions_Buttons: the reset-volume cases pass their own
  currentValue local instead of reusing the click-type parameter's slot
  through an enum cast, the comment explaining that form is gone, and the
  genie toggle tests the flag by truthiness.

Re-measured on main 592a17d with a full build and reccmp, unchanged:
87.6 / 97.2 / 86.6 / 82.4, all still register allocation only.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XmXCmXCVBvESeaU1djQFjp
gynt and others added 3 commits December 5, 2026 04:59
…reak (90.0 -> 95.9)

Case 0x91 now takes the address of its own block-scoped spawnX/spawnY
instead of a shared function local and param_1. With both addresses
taken on function-wide variables MSVC dropped every tail call; the
original tail-calls clearAnyOtherModalDialogs four times.

The event cases of the inner switch end with break, so they share the
clearAnyOtherModalDialogs call after the switch as in the original.
Legacy iVarN/dVarN names replaced by names per use.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XmXCmXCVBvESeaU1djQFjp
Record the blockers found for BuildingWorkStatus, EventButtonAndData,
UnusedCreateMessageEvent_SetTextUnk and handleMenuElementsCallbacks.
Scores unchanged.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XmXCmXCVBvESeaU1djQFjp

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