Repository navigation
UI: the unit action buttons' render function at 100% - #440
Open
kotijekriv wants to merge 1 commit into
Open
kotijekriv wants to merge 1 commit into
kotijekriv wants to merge 1 commit into
Conversation
BuildMenu::MenuItemRenderFunction_BuildMenu_UnitActionButtons, rewritten from the original's listing. gynt's drafts: 19176b8 at 39.09% ("bug fix, costs 0.6% match") on his fork's reimpl/UI-MenuItems, whose status line records 39.1%, and 0523265 at 40.73% ("if/else") on his fork's reimpl/UI-MenuItems-development, whose status line records 40.7%; the newest commit on the file, dd1a7c4, is on that branch too. Our draft (72d2bc1, then ae5a920 and 07a33e2; 40.9% in the draft PR sourcehold#328) was his code with a mechanical pass and a fix for button 376. The function decides for each unit action button whether it is drawn: one switch over the button's picture (DAT_CurrentButtonGmDataIndex, a GmDataIndex value), dispatched at 0x00438C35 through the jump table at 0x0043907C and the byte table at 0x004390AC. A hidden button stores TRUE in DAT_ButtonUnknownZero and returns; a shown one is rendered and stores FALSE. The drafts reached the shared render, the shared renderNumber call and the stance buttons' common end by goto (seven gotos to three labels), carried renderNumber's seven arguments there in seven locals, and reached part of the hides by break to a store behind the switch. Written without them, five things decide the match: - One render stands behind the switch and the shown paths leave by break. In the original 374, 216, 211 to 215 with 558, and default all jump to one render block (0x00438EC1); each path that falls into the render has a copy. - Three plain renders are written out all the same: at 218's end (0x00438CFA), in 219's ranged arm (0x00438EC1, the block the breaks land on) and at 219's end (0x00438EF3). With a break at 218's or 219's end the compiler keeps a copy of the hide there and jumps to the render, where the original jumps to the hide. With a break in the ranged arm the render block goes to the function's end and case 217 keeps code of its own (288 instructions against 277). - Every hide is written where it is decided (DAT_ButtonUnknownZero = TRUE; return;). The original keeps the 1 in esi in the cases that also compare with it, and the 0 in edi; its hides share two blocks, 0x00439048 for the register and 0x0043906C for the immediate. - The hides in front of a written-out render are one || test (218, 219's end), and so is the test of default and 217. As separate ifs the match is lost each time. Case 220's six conditions are one test as well; there one test and separate ifs compile the same. - case 217 has a body of its own, the same as default's. The original's jump table has twelve entries, one of them for 217 alone, and it leads to default's code (0x00439051). With `case 217:` in front of `default:` the table has eleven entries and the byte table other values, while the instructions stay the same. The two cases that draw a number each write their renderNumber call; the compiler shares the call's end (0x00438E95), as the original does. hasEngineerSelected is compared with TRUE in 376 and in 211 to 215 with 558, because the original compares with 1 there (0x00438C41, 0x0043902E); a truth test loses the match in both places. The case labels and the stores to DAT_CurrentButtonGmDataIndex use GmDataIndex's names; the siege engine unit types and the two colours use their enum names too. Measured: quick_diff 0.9964 with 277 instructions on both sides; the one difference is how the two listings print 376's tail call. All 45 local jumps land on the same instruction and both switch tables are the same, compared by a script because quick_diff prints every local jump alike. reccmp 100.0% (normalized 100.0%) alone, data resolvers and its own resolver on. Not run in game. 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
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 function at 100%, moved out of our open draft #328, from gynt's draft. No header changes.
MenuItems::BuildMenu::MenuItemRenderFunction_BuildMenu_UnitActionButtonsOur draft was gynt's code with a mechanical pass and a fix for button 376; gynt's best, 40.7%, is 0523265 on his fork's reimpl/UI-MenuItems-development.
Measured.
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); in the full build 0x004314E0 (MapEditorLandscaping_GeneralButtonshandler) is left out and its resolver stays off, as in main's own full build.tools/). The new function reads 100.0 raw and normalised there too, with no diff entry.OpenSHC::prefix dropped in front ofGeneral_Func,Map,GameandCommands, comments and two includes. Each was measured to compile to the same instructions, and the narrow build above is of this head.Menu::handleMenuItems(0x004F6280) reads 97.9 (91.4 normalised) andMenu::Constructor_Menu(0x004F4100) reads 98.4 (95.2), both 100.0 before. Both compare a menu item's type with the constant 0x11000000, five compares in all. In this build's layout a symbol of our DLL lies at exactly the address 0x11000000, so reccmp prints the constant as that symbol's name on our side. The instructions are the same on both sides. UI: three more functions at 100%: the building menu's DoInitial, the credits' prepare and activateModalDialog2 #435's and UI: the credits scroll's fade and the win/defeat window at 100% #437's texts describe the same thing for their builds.What got it to 100%
The function decides for each unit action button of the build menu whether it is drawn: one
switchover the button's picture, dispatched at 0x00438C35 through the jump table at 0x0043907C and the byte table at 0x004390AC. A hidden button stores TRUE inDAT_ButtonUnknownZeroand returns; a shown one is rendered and stores FALSE.The drafts had the decompiler's shape. The shared render, the shared
renderNumbercall and the stance buttons' common end were reached bygoto(seven gotos to three labels). Seven locals carriedrenderNumber's seven arguments to the shared call. Part of the hides were written in place, the rest left bybreakto a store behind the switch. Our draft read 280 instructions against the original's 277 (0.6715 in our listing diff). The function is written from the original's listing now, without gotos. Five things decide the match:break. In the original 374, 216, 211 to 215 with 558, and default all jump to one render block (0x00438EC1), and each path that falls into the render has a copy of it. With the render written out at every path the instructions are the same 277, but the block the jumps land on and the stance buttons' common end sit behind 372 (0.9747 in our listing diff).breakat 218's or at 219's end the compiler keeps a copy of the hide there and jumps to the render, where the original jumps to the hide and keeps the render.breakin the ranged arm the render block goes to the function's end and case 217 keeps code of its own: 288 instructions against 277.DAT_ButtonUnknownZero = TRUE; return;). The original keeps the 1 inesiin the cases that also compare with it, and the 0 inedi. Its hides share two blocks: 0x00439048 stores the register, 0x0043906C the immediate. A variant with every hide as abreakto one store behind the switch does not keep the 1 in a register (241 instructions against 277).||test (218 and 219's end), and so is the test of default and 217. As separateifs the match is lost each time. Case 220's six conditions are one test as well; there one test and separateifs compile the same, and we took the one test so that the file reads the same way throughout.case 217has a body of its own, the same as default's. The original's jump table has twelve entries. One of them is for 217 alone, and it leads to default's code (0x00439051). Withcase 217:written in front ofdefault:the table has eleven entries and the byte table other values, while the instructions stay the same.The two cases that draw a number (220 and one arm of 219) each write their
renderNumbercall; the compiler shares the call's end (0x00438E95), as the original does.Several other choices made no difference, and we took the plainer form: 374's, 211's and 216's two hides as one test each, and the stance cases as
if/else.No behaviour differs from our draft, as far as we can tell: the draft already hid button 376 when no engineer is selected (ae5a920).
Things in the file that look odd and are the original's
hasEngineerSelected == TRUEin 376 and!= TRUEin 211 to 215 with 558. The original compares with 1 there (cmp [hasEngineerSelected], esiwith 1 inesi, at 0x00438C41 and 0x0043902E); every other case tests the flag for truth. A truth test loses the match in both places. A comment at each says so.case 217with default's body again, and the||tests. They are the points above; each has a one-line comment that says which change loses the match. Button 376 and the two number cases have render calls of their own for other reasons: the tail call, and the number drawn behind the render.Header findings (nothing changed here)
DAT_CurrentButtonGmDataIndexisundefined4. It holds aGmDataIndexInt:prepareAndRenderMenuItemsstoresfirstItemTypeData.gmDataIndex & 0x7fffffinto it, and the original compares it signed here (jgat 0x00438C12). As aGmDataIndexIntthe(int)cast in thisswitchwould go. Left for a header sync.UnitsState.hpp:selectionContainsTunnelersOnly()andselectionContainsShieldmenOnly()returnint, but main's implementations return only TRUE or FALSE, like theirBOOLEnumneighboursselectionContainsEngineersOnly(),selectionContainsRangedOnlyUnits()andselectionContainsLaddermenOnly().countSelectedCatapultsAndTrebuchets()returnsundefined4; main's implementation returns 0, 1 or 2.getFirstSelectedUnitOfEitherType(int param_1, int param_2): main's .cpp calls the parametersunitType_1andunitType_2.DAT_ButtonUnknownZero: TRUE means "not drawn" in this function. InRenderCurrentNotActiveButtonWithPossibleAlphaTexOnCurrentSurfaceUnka set flag picks the picture two further on (stateOffset = 2), which looks like a disabled state. A lead for a name; we would not call it "hidden" everywhere.DAT_00ee1090andDAT_00ee1094areundefined4and have no reader on main. This function sets them next to the button's picture (291 or 308, and 202, 198 or 201). As picture numbers they would not fit (291 isGDI_MAPEDIT_BUTTONS_291_PIC_67), so we don't know what they are.The checker's 8 CHECKs
hasEngineerSelected == TRUEand!= TRUE(two findings)DAT_CurrentButtonGmDataIndexin theswitchundefined4and the original compares signed; without the cast ours compares unsigned (jaforjg). See the header findingsDAT_00ee1094andDAT_00ee1090(five findings)🤖 Generated with Claude Code