Repository navigation
UI: the text editor's buttons at 100%, and ColorUnion is 2 bytes - #426
Open
kotijekriv wants to merge 2 commits into
Open
kotijekriv wants to merge 2 commits into
kotijekriv wants to merge 2 commits into
Conversation
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>
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.TextEditor::MenuItemRenderFunction_TextEditor_ButtonsMeasured.
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_GeneralButtonshandler) 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.tools/). The text editor reads 100.0 raw and normalised, and no function on main moved, so the header change moves nothing else.openshc-pr-checkgives 1 ERROR and 9 CHECK, all explained below.openshc-pr-fixfinds nothing to change.The header change:
ColorUnionis a shortbe046d5b7removesint intValuefromColorUnion.ColorUnionandColorContainerare now 2 bytes each.ColorContainerholds oneColorUnion, and nothing else in the code uses it.COL_WHITE0x00DF338C toCOL_LIGHT_GREY0x00DF33F0) are only ever accessed 16 bits wide.mov,movzx,movsxorcmp, or a word store. For example,COL_BLACK = 0at 0x004678A3, andCOL_WHITE = 0x7FFFand0xFFFFat 0x004678C9 and 0x0046792E.src/OpenSHC/Globalswith a 4-byte scalar-like type, these 18 are the only ones the exe never accesses 4 bytes wide.shortValueto a dword load. The text editor's case 32 readsCOL_BLACKwithmov ecx, dword ptrwhere the original hasmov cx, word ptr [0xdf33d0](0x0045E5EF)..intValuewith a cast, reading through a pointer, and ashort constlocal.intValueis a union member at offset 0, so no offsets move.intValue, takes the size of either type, or embedsColorUnionorColorContainerin another type.ColorUnion.hpp: it addsushort ushortValueand keepsintValue, so the two PRs conflict on that file. The way to resolve it is to keep both 16-bit members and dropintValue. Reimpl/viewport render state #241 also reads the colours 16 bits wide (COL_MAGENTAthroughshortValueandushortValue, neverintValue), which fits this reading.What got it to 100%
The draft was 91.5%. In order, the changes were:
if/elsefollowed by one return.pop esi; ret.getHelpSectionText's result goes straight intorenderTextToScreen.getHelpSectionTextlast: 0x0045E710, then 0x0045E71B.field_0x23970as aswitchwith the onecase 1.sub eax, 1; jneat 0x0045EBA5 and 0x0045EC39. That is what a one-case switch compiles to, where== 1gives acmp.otherColorUnk_0x0beforetextColor. This gives the original's copy into ax,mov ax, cxbefore thetextColorstore (0x0045E60A), where we hadmovzx eax, cxafter 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_1isbutton, as in 28 of the sibling handlers.textColorternaries of cases 33 to 36 areif/else.(ushort)and(uint)(ushort)casts onDAT_PointerToTemporaryTextMemoryare gone. It is anLPWSTR, which is already unsigned 16-bit.(char*)casts on thes_strings passed torenderTextToScreenare gone, because it takes achar const*. The 18 forrenderTextToScreen2, which takes achar*, stay.COL_000000instead of0.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.
field_0x23970switch. The original goes on into the switch (0x0045E5F8 to 0x0045E617).field_0x23970cases 2, 3, 10 and 12. The original goes on and draws the box and "NEXT" (0x0045EAFA to 0x0045EB0D).Case -4 is an
if/elsewith 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
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
TextAlignmentparameter of the text calls is also a width. The text editor's buttons passDAT_ButtonWin that slot ofrenderText2andrenderTextToScreen2(0x0045E00E pushes the width). That is why this function has 20(TextAlignment)DAT_ButtonW::instancecasts.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.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 76renderText2calls passTTA_LEFT,TTA_CENTERorTTA_RIGHT.int(alignment or centring width) would remove the casts. This has not been measured.The checker's findings
ColorUnion.hppbe046d5b7:228,:304,:393,:433,:473,:493field_0x23970:338field_0x7a16c:36field_0x23970's tag kinds, in the switches at:228,:304,:393,:433,:473and: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 showsname_0x0for 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