Skip to content

UI: the text editor's buttons at 100%, and ColorUnion is 2 bytes - #426

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

kotijekriv wants to merge 2 commits into
sourcehold:mainfrom
kotijekriv:reimpl/UI-B-round18-100

Conversation

@kotijekriv

Copy link
Copy Markdown
Contributor

One more UI function at 100%, the help text editor's button renderer, from our draft #327. It needed one header change, in a commit of its own: the colour globals' type, ColorUnion, is 2 bytes instead of 4.

Function Address Before reccmp raw Normalised
TextEditor::MenuItemRenderFunction_TextEditor_Buttons 0x0045DE40 our draft #327 (020b8de): 91.5%, built on main's code too; gynt's 75.94% (140055c; newest on the file 0e551f6) 100.0 100.0

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), with one exception in the full build below: 0x004314E0 (MapEditorLandscaping_GeneralButtons handler) is left out of the build and its function resolver stays off. Main's baseline build leaves it out the same way, so it is measured in neither.
  • Full build: an all-on build of this branch on main 3458fea: 0 failed, 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 text editor reads 100.0 raw and normalised, and no function on main moved, so the header change moves nothing else.
  • The build's commit: it ran on 45231deee. The head differs from it only in the readability changes listed below. A narrow build of the head reads 100.0 raw and normalised too.
  • Checks: openshc-pr-check gives 1 ERROR and 9 CHECK, all explained below. openshc-pr-fix finds nothing to change.
  • Not run in game.

The header change: ColorUnion is a short

be046d5b7 removes int intValue from ColorUnion. ColorUnion and ColorContainer are now 2 bytes each. ColorContainer holds one ColorUnion, and nothing else in the code uses it.

  • The evidence: the 18 colour globals (COL_WHITE 0x00DF338C to COL_LIGHT_GREY 0x00DF33F0) are only ever accessed 16 bits wide.
    • Every instruction in the exe that names one of their addresses is a word mov, movzx, movsx or cmp, or a word store. For example, COL_BLACK = 0 at 0x004678A3, and COL_WHITE = 0x7FFF and 0xFFFF at 0x004678C9 and 0x0046792E.
    • None of the addresses is ever pushed or loaded as an immediate, so nothing reaches them through a pointer.
    • Of all 374 globals in src/OpenSHC/Globals with a 4-byte scalar-like type, these 18 are the only ones the exe never accesses 4 bytes wide.
  • Why it matters: with a 4-byte object, MSVC widens a hoisted read of shortValue to a dword load. The text editor's case 32 reads COL_BLACK with mov ecx, dword ptr where the original has mov cx, word ptr [0xdf33d0] (0x0045E5EF).
    • A 4-byte struct in place of the union gives the same dword load.
    • So do reading .intValue with a cast, reading through a pointer, and a short const local.
    • Only a 2-byte object gives the word load.
  • Nothing else changes:
    • intValue is a union member at offset 0, so no offsets move.
    • Nothing on main or in any open PR reads intValue, takes the size of either type, or embeds ColorUnion or ColorContainer in another type.
    • The full build above shows no function moving.
  • The checker's ERROR ("a header field line was removed") is about this line, and it is on purpose.
  • The declaration check will fail on this PR until the admins sync the header.
  • Reimpl/viewport render state #241 (gynt's ViewportRenderState draft) also edits ColorUnion.hpp: it adds ushort ushortValue and keeps intValue, so the two PRs conflict on that file. The way to resolve it is to keep both 16-bit members and drop intValue. Reimpl/viewport render state #241 also reads the colours 16 bits wide (COL_MAGENTA through shortValue and ushortValue, never intValue), which fits this reading.

What got it to 100%

The draft was 91.5%. In order, the changes were:

  • Case -5: the two "Exit" calls are an if / else followed by one return.
    • The original keeps a full copy of the call in each arm, at 0x0045DF3B and 0x0045DF6E, each with its own pop esi; ret.
    • With a return inside the first arm, MSVC merged the second arm into the first copy.
  • Case 32, arm 2: getHelpSectionText's result goes straight into renderTextToScreen.
    • The original pushes the render call's other arguments first and calls getHelpSectionText last: 0x0045E710, then 0x0045E71B.
    • The decompiler's locals for the text and the position are gone.
  • Cases 35 and 36: they test field_0x23970 as a switch with the one case 1.
    • The original has sub eax, 1; jne at 0x0045EBA5 and 0x0045EC39. That is what a one-case switch compiles to, where == 1 gives a cmp.
  • Case 32's colours: the last two instructions, and each change alone leaves the other one.
    • Each arm stores otherColorUnk_0x0 before textColor. This gives the original's copy into ax, mov ax, cx before the textColor store (0x0045E60A), where we had movzx eax, cx after it.
    • COL_BLACK's word load before the branch (0x0045E5EF) needed the header change above.

Then these readability changes, each compiling to the same instructions:

  • param_1 is button, as in 28 of the sibling handlers.
  • The four textColor ternaries of cases 33 to 36 are if / else.
  • The (ushort) and (uint)(ushort) casts on DAT_PointerToTemporaryTextMemory are gone. It is an LPWSTR, which is already unsigned 16-bit.
  • The 15 (char*) casts on the s_ strings passed to renderTextToScreen are gone, because it takes a char const*. The 18 for renderTextToScreen2, which takes a char*, stay.
  • The unpressed text colour of the 1..15 and 20 buttons is COL_000000 instead of 0.

Behaviour compared with gynt's draft

These three fixes were already in #327's draft, and they are kept here. Each is read from the original's asm.

  • Button -5: it drew its label twice when not pressed. The original returns after the first text call (0x0045DF3B).
  • Pressed button 32: the draft returned from its pressed-colour arm before the field_0x23970 switch. The original goes on into the switch (0x0045E5F8 to 0x0045E617).
  • Pressed button 34: the draft returned from the same arm inside its field_0x23970 cases 2, 3, 10 and 12. The original goes on and draws the box and "NEXT" (0x0045EAFA to 0x0045EB0D).

Case -4 is an if / else with one call per arm. This is a change of shape only: gynt's draft renders once per path with the same arguments.

The original's oddities, kept

  • The arm for buttons 1 to 15 and 20 tests button == -4 (:98), and its inner switch has cases -4 and -5. That arm is only reached for buttons 1 to 15 and 20, so neither ever matches. The original has both, and a comment says so at :97.

Proposed, not changed here

  • The TextAlignment parameter of the text calls is also a width. The text editor's buttons pass DAT_ButtonW in that slot of renderText2 and renderTextToScreen2 (0x0045E00E pushes the width). That is why this function has 20 (TextAlignment)DAT_ButtonW::instance casts.
    • Both functions hand the parameter unchanged to renderTextToScreen (0x00474250), which reads it as a signed number. Below 0, the text ends at x. At 0, it starts at x. At 1 or more, it is centred in a box that wide starting at x: x + (alignment - textWidth) / 2.
    • So TTA_CENTER (1) is the one-pixel box, which centres the text on x. The editor passes the button's width to centre each label on its button. Main's 76 renderText2 calls pass TTA_LEFT, TTA_CENTER or TTA_RIGHT.
    • Typing the parameter as an int (alignment or centring width) would remove the casts. This has not been measured.

The checker's findings

Count Where Finding Why it stays
1 ColorUnion.hpp ERROR: a header field line was removed on purpose: see the header change above
1 (branch) header changes in commits of their own they are: be046d5b7
6 :228, :304, :393, :433, :473, :493 placeholder field_0x23970 see below
1 :338 placeholder field_0x7a16c see below
1 :36 75 numeric case labels 44 are the editor's button numbers (-5 to 36), and 31 are field_0x23970's tag kinds, in the switches at :228, :304, :393, :433, :473 and :493. Neither has an enum.

What the placeholders hold

These are read from how the code uses them. The names are unchanged.

  • TextEditorState::field_0x23970: the kind of tag under the cursor in the help text. Button 31 labels it 1 PIC, 2 FONT, 3 COLOUR, 4 LINK, 5 \LINK, 7 CENTRE, 8 \CENTRE, 10 LINKCLR, 11 SOUND, 12 STRING, 14 INCLUDE.
  • UserHelpDefinedData::field_0x7a16c: a table of named entries indexed by the tag's value. The editor shows name_0x0 for colour tags (3) and link-colour tags (10), so it is probably the list of colour names.

One function leaves #327

The text editor's button renderer moves here from the draft #327. 2ab3c7d on #327's branch takes it out of that draft.

🤖 Generated with Claude Code

kotijekriv and others added 2 commits November 19, 2026 08:56
The 18 colour globals (COL_BLACK 0x00DF33D0, COL_WHITE 0x00DF338C and the rest, 0x00DF338C..0x00DF33F0) are only
ever accessed 16 bits wide in the original: every instruction that names one of their addresses is a word mov,
movzx, movsx or cmp, or a word store (COL_BLACK = 0 at 0x004678A3, COL_WHITE = 0x7FFF / 0xFFFF at 0x004678C9 and
0x0046792E), and none of the addresses is ever taken. The int member had no reader, and with it the union was
4 bytes, which lets MSVC widen a hoisted read of shortValue to a dword load: MenuItemRenderFunction_TextEditor_Buttons
reads COL_BLACK with `mov ecx, dword ptr` where the original has `mov cx, word ptr [0xdf33d0]` (0x0045E5EF).
ColorContainer, which holds one ColorUnion and is not used anywhere else, shrinks with it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
TextEditor::MenuItemRenderFunction_TextEditor_Buttons, from gynt's draft (d7366c2 at 62.8% on upstream's
reimpl/UI-MenuItems; be19b2c at 74.93% on his fork's reimpl/UI-MenuItems; 140055c at 75.94% on his
reimpl/UI-MenuItems-development, his newest reimplement commit on the file, with the string literals of his
0e551f6) by way of the sourcehold#327 draft (020b8de, 91.5% on main 3458fea). reccmp 100.0 (normalized 100.0) on main
3458fea plus the previous commit, with the data and its own function resolver on.

The sourcehold#327 draft's work is kept: the switches are in the original's table order (-1, -2, -4, -5, the 1..15/20 buttons,
31..36), the button labels are a switch on the button number itself, and the first field_0x23970 switch has case 10
after case 3. Case -4 is an if/else with one call per arm, a change of shape only: gynt's draft renders once per path
with the same arguments. So are its three behaviour fixes against gynt's draft:
- button -5 drew its label twice when not pressed (the original returns after the first text call, 0x0045DF3B);
- pressed button 32 returned from its pressed-colour arm before the field_0x23970 switch; the original goes on into
  it (0x0045E5F8 -> 0x0045E617);
- pressed button 34 returned from the same arm inside its field_0x23970 cases 2, 3, 10 and 12; the original goes on
  and draws the box and "NEXT" (0x0045EAFA -> 0x0045EB0D).

From 91.5 to 100:
- Case -5 writes the two "Exit" calls as an if/else followed by one return. The original keeps a full copy of the
  call in each arm (0x0045DF3B and 0x0045DF6E, each with its own `pop esi; ret`); with a return inside the first arm
  MSVC merged the second arm into the first copy.
- Case 32's arm 2 passes getHelpSectionText's result straight to renderTextToScreen. The original pushes the render
  call's other arguments first and calls getHelpSectionText last (0x0045E710, then 0x0045E71B); the locals the
  decompiler had for the text and the position are gone.
- Cases 35 and 36 test field_0x23970 as a switch with the one case 1: the original has `sub eax, 1; jne`
  (0x0045EBA5 and 0x0045EC39), which is what a one-case switch compiles to, where `== 1` gives a cmp.
- Case 32's colour block was the last two instructions. Each arm stores otherColorUnk_0x0 before textColor, which
  gives the original's copy into ax (`mov ax, cx` before the textColor store at 0x0045E60A, where we had
  `movzx eax, cx` after it). COL_BLACK's word load before the branch (`mov cx, word ptr`, 0x0045E5EF, where we had
  `mov ecx, dword ptr`) needed the previous commit: with a 4-byte ColorUnion MSVC loads it as a dword. Each change
  alone leaves the other instruction.

Readability, with identical code: param_1 is `button`, as in 28 of the sibling handlers; the four textColor ternaries
of cases 33 to 36 are if/else; the (ushort) and (uint)(ushort) casts on DAT_PointerToTemporaryTextMemory are gone
(it is an LPWSTR, already unsigned 16-bit); the 15 (char*) casts on the s_ strings passed to renderTextToScreen are
gone (it takes a char const*; the 18 for renderTextToScreen2, which takes a char*, stay); and the unpressed text
colour of the 1..15/20 buttons is COL_000000.

Co-authored-by: Gynt <gynt@users.noreply.github.com>
Co-Authored-By: Claude Opus 5.5 <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 19, 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