Skip to content

One more UI function at 100%: ScrollingHandler::handleScrolling - #460

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

kotijekriv wants to merge 1 commit into
sourcehold:mainfrom
kotijekriv:reimpl/UI-A-handlescrolling-100

Conversation

@kotijekriv

Copy link
Copy Markdown
Contributor

One more function at 100%, with no header changes and no line for cmake/compiler-flags-gl.txt. One behaviour fix against the draft, one a copy on his fork also has (below). ScrollingHandler::handleScrolling turns the four scroll flags into one of eight directions, or none (while no dialog is open, the four key-down flags set them too), and works out how far a scroll step goes: the longer the scrolling lasts, the further, up to a maximum, with the scroll speed setting changing both the rate and the maximum. It is from gynt's draft (4c73c67 on #256's reimpl/UI-Core, 23.6%; his fork's reimpl/UI-Core-development and reimpl/UI-MenuItems-development carry it at 23.6% too), and the commit credits him as co-author.

Function Address Before After
ScrollingHandler::handleScrolling 0x00468AD0 gynt's draft 23.6% (4c73c67; the same at 10280b1 and 3d17527) 100.0%, off the /GL list and on it

Measured.

  • Setup: the bundled MSVC1400-SP1 against the Steam Stronghold Crusader.exe (3bb0a8c1…5387ac5a), both resolver sets enabled locally (not committed), and sourcehold's reccmp with --resolve-wrapped-calls --ignore-security-cookie.
  • Full build: an all-on build of this branch on main 812d149, with main's own /GL list and with 0x004314E0 (the MapEditorLandscaping_GeneralButtons handler) left out and its resolver off, as in main's baseline: 0 failed of 2,424 compile steps, 99.57 over 2,440 functions, against 99.58 over 2,439 for main alone (built at f489b72; 3458fea only adds files under tools/, and cmake: optional wrapper units built from function-visibility.txt #399 and cmake: function-visibility.txt, which callers must see which callees' bodies #398 add the optional function-visibility units, off by default). handleScrolling reads 100.0 raw and normalised there too, with no diff entry, and its caller Global::WinMain reads what it read before. Two functions on main read lower, and it is how reccmp prints a constant, not their code: Menu::handleMenuItems (0x004F6280) reads 97.9 (91.4 normalised) and Menu::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 and UI: the credits scroll's fade and the win/defeat window at 100% #437 describe the same two readings. Every other main function reads exactly what it read before (unrounded 99.5746 against 99.5759 raw, all of the difference in those two functions).
  • Our own narrower run: the 133 files of main's /GL list that exist on main, and the file as committed, off the list: 144 functions, average 99.56. The function reads 100.0% raw and normalised, with no diff entries. The same build without the file has 143 functions, and each of them reads the same. The sources were main's at 3458fea; main's two commits since (02fab42, 812d149) add cmake files only, with the new option off.
  • Instructions: the original's 130, in its order; reccmp's diff for the function is empty. The function has no switch table (the two-case switch is a compare chain).
  • Please measure with both resolver switches on (the Globals and the function resolvers), as our numbers are.
  • Checks: our local style checker gives 0 ERROR and 0 CHECK; our local fixer finds nothing to change.
  • Not run in game.

The behaviour fix

  • With no direction, the original resets the scroll state and returns (ret at 0x00468BD3): isScrolling is cleared, the base distance goes back to the minimum, the four flags are cleared, and the stop time and the scrolling time are both set to now. In the drafts in the table that block has no return. They went on into the scrolling code: isScrolling was set again, timeScrolling became the time since the stamp just taken (about 0), and the base distance became that time divided by the interval (0, not the minimum). So isScrolling read TRUE after every call, scrolling or not; main has three readers of it (HandleWallTerrainMouseDrag, the in-game menu's place-wall handler and GameCore::viewportAndScrollingRelated). His fork's reimpl/Wilps-2026-10-18 has the return (return; // 0x468BD3 in 8ca7861, a copy with other field names and a 0.0% status line); his later 3d17527 is without it.

Everything else in the draft computes the original's values: the eight directions, the maximum distance of two thirds of the limit, the fast setting's interval less a third and maximum at 133%, and the slow setting's interval plus a half and half the maximum. The draft's divisions are unsigned where the original's are signed (below), which differs only for a limit of 2^30 or more, or an interval of 2^31 or more.

What got it to 100%

The draft is the decompiler's output, with a goto and the division by -3 written as its multiply and shifts. The function is rewritten against the original instructions, in two steps on our dev branch (not public): 86.9%, then 100.0%.

  • The direction is one if / else if chain on up, down, left and right, with left tested before right under up and under down.
  • isScrolling is set in both arms of the direction test, and a second if on it holds the rest of the no-direction block (86.9% to 100.0%). The original places that block in the middle of the speed switch: behind the slow case, in front of the fast one (0x00468BAF). As far as we can tell, the compiler knows the flag on both paths and drops the second test, but only after it has placed the blocks, so the block keeps the place of the second if; the switch's compare chain then moves up to the store, and its slow case with it. That fits the listing: the FALSE store sits in the block (0x00468BB2) and the TRUE store in the switch's dispatch (0x00468B95). Written as one if with a return in front of the store, the block stays in front of the switch (86.9%: the block's 15 instructions in front of the switch, with a jne around them where the original has a je to them). Written as the else arm of the direction test, it goes behind the function's end, and the compiler reorders its stores (78.2%). Testing the direction twice does not do it either (quick_diff 0.9769: the direction is then loaded into a register for the two compares).
  • The speed is a switch, with interval -= interval / 3 for fast and interval += interval / 2 for slow. For the fast case the compiler divides by -3 and adds (imul by 0x55555555, sub edx, ebx, sar edx, 1, the sign added, then add ebx, ecx), which is what the original has (0x00468BD4 to 0x00468BFB). Written interval += interval / -3, or with the third in a local of its own, the listing is the same.
  • (int)scrollDistanceLimit: the original divides it signed (see the header finding below).
  • The elapsed time goes straight into timeScrolling, and the quotient into scrollDistanceBase. With a local for each, the listing is the same, so the fields are used.
  • The four flags are cleared in the order down, up, right, left in both places. Here the compiler emits each run last one first, and the original has left, right, up, down (0x00468BBE, 0x00468C1B).

quick_diff reads 1.0000: 130 instructions against 130.

No line for cmake/compiler-flags-gl.txt

Kept as they are

  • Two tests where one would do: isScrolling is set from the direction and tested right behind. A comment in the code says that the plain form loses the match.
  • The four flag clears are written twice, in the no-direction block and at the end: the original has both sets, each in this order.
  • timeScrolling gets the time stamp itself when scrolling stops (not 0): the original stores timeGetTime() into both fields (0x00468BCA, 0x00468BCD). Nothing on main reads timeScrolling.
  • The interval and the maximum are read at the top, in front of the dialog test: the original reads both fields first (0x00468AD5, 0x00468AD8).

Header finding (nothing changed here)

  • ScrollingHandler::scrollDistanceLimit, scrollAccelerationInterval and scrollDistanceMin are undefined4, but the original uses them as int. It divides the doubled limit by 3 signed (imul by 0x55555556 at 0x00468AE2), halves the interval signed (0x00468B9F) and divides by it with idiv (0x00468C0E), and copies the minimum into the int scrollDistanceBase. As int, the (int) cast here would go. Nothing on main reads the three fields.

Other PRs

🤖 Generated with Claude Code

ScrollingHandler::handleScrolling, from Gynt's draft (4c73c67 at 23.6% on his closed sourcehold#256's reimpl/UI-Core;
the same percentage at 10280b1 on his fork's reimpl/UI-Core-development and at 3d17527 on
reimpl/UI-MenuItems-development). The draft is the decompiler's output. One behaviour fix against it: with no
direction the original resets the scroll state and returns (0x00468BD3). In these drafts the block has no return,
so it went on into the scrolling code, set isScrolling again and overwrote the base distance and timeScrolling
with those of an elapsed time of about 0. His fork's reimpl/Wilps-2026-10-18 (8ca7861), a copy with other
field names, has the return.
Rewritten against the original instructions:
- the direction is one if / else if chain on up, down, left and right, with left tested before right under up
  and under down;
- isScrolling is set in both arms of the direction test, and a second if on it holds the rest of the
  no-direction block. As far as we can tell, the compiler knows the flag on both paths and drops the second test,
  but only after it has placed the blocks: the block keeps the second if's place, and the switch's compare chain
  and slow case then move up in front of it. That is the original, which has the block between the slow and the
  fast case (0x00468BAF). As one if with a return, in front of the store, the block stays in front of the switch
  (86.9%); as the else arm of the direction test it goes behind the function's end, with its stores reordered;
- the speed is a switch, with interval -= interval / 3 for fast and interval += interval / 2 for slow. For the
  fast case the compiler divides by -3 and adds, as the original does (0x00468BD4); interval += interval / -3,
  or the third in a local of its own, gives the same listing;
- (int)scrollDistanceLimit, as the original divides it signed (the field is undefined4 in the header, like the
  interval and the minimum, which it also uses as int);
- the elapsed time goes straight into timeScrolling and the quotient into scrollDistanceBase (locals for them
  give the same listing); the four flags are cleared down, up, right, left in both places, which the compiler
  emits last one first here, as the original has them (left, right, up, down).

No line for cmake/compiler-flags-gl.txt: the file reads 100.0% off the /GL list, and 100.0% on it too. It calls
nothing but timeGetTime.

reccmp in a build of main's whole /GL list (the 133 files that exist) and this file as committed, off the list,
with every resolver of those files on: 144 functions, average 99.56. handleScrolling 100.0% raw and normalised,
no differing line, and the same with the file on the list. The same build without the file has 143 functions,
and each of them reads the same. The sources are main's at 3458fea; main's two commits since (02fab42,
812d149) add cmake files only, with the new option off.

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