You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Proposal: write the MenuItem tables with inline constructors (byte-identical, verified)
#492
The MenuItem handlers (MenuItemActionHandler_*, MenuItemRenderFunction_*) are long switches over magic numbers, and the tables in RenderingDefinedData are 2,198 raw structs. This proposes a source form for the tables that is more likely to be what the original looked like, and that also makes the handlers readable. Background notes are on the branch docs/static-init-cheat-sheet (IMPLEMENTATION_CHEAT_SHEET.md, Static Initialization of Global Data).
Why the handlers are switches
The tables explain the switches. Of the 2,198 items, 246 are group heads (0x01000000) that carry an action and a render callback. 1,445 are group members (0x02000003) with NULL callbacks and only an ID in callbackParameter. Menu::Constructor_Menu then copies each head's callbacks into members that have none. So the original wrote one handler per group and told the buttons apart by ID, the same idiom as a Win32 dialog proc with switch (LOWORD(wParam)). The switches are probably genuine. What is missing is names for the IDs, and the original probably had them:
The main menu uses its own IDs, 1..9.
In the options dialogs, the ID is a text index.GameplayOptions render calls getTextStringInGroupAtOffset(TEXT_GAME_OPTIONS, button), and 17 ("Back") recurs across dialogs. Negative IDs are value displays.
Proposal: inline MenuItem constructors
VS2005 at /O2 constant-folds inline constructors into static .data, so a table written with constructors produces no initializer code. I wrote the MainMenu, GameplayOptions and SoundOptions tables this way and compiled them with the bundled compiler at the project's flags. All 37 items are byte-identical to the exe (function pointers compared by address), and no ??__E dynamic initializer is emitted.
classMenuItem {
public:// ... the existing 0x50-byte layout, callbacks as unions ...// an item with only an action (MIT 0, the click handler item)MenuItem(MenuItemType type, SimpleActionHandler* action);
// the head of a group: the callbacks its members shareMenuItem(SimpleActionHandler* action, SimpleRenderFunction* render);
// a button with its own callbacksMenuItem(int x, int y, int width, int height, SimpleActionHandler* action, int id, SimpleRenderFunction* render);
// a button in a group, drawn by the group's render functionMenuItem(int x, int y, int width, int height, int id);
// an image button in a group: its size comes from the imageMenuItem(int x, int y, int id, GmDataIndex image, int helpText, int imageFlags = 0);
// a slider or scrollbar: the union member is assigned in the body, no cast neededMenuItem(MenuItemType type, int x, int y, int width, int height, SliderActionHandler* action, int id,
SliderRenderFunction* render, int minThumbLength);
// a marker, such as MIT_LAST_ENTRYexplicitMenuItem(MenuItemType type);
};
MenuItem MenuItems_GameplayOptions[] = {
MenuItem(MIT_SLIDER, 260, 130, 200, 27, GameplayOptions_SpeedSlider, 0, GameplayOptions_SpeedSliderRender, 20),
MenuItem(GameplayOptions_Buttons, GameplayOptions_ButtonsRender),
MenuItem(40, 130, 200, 27, GOB_GAME_SPEED),
MenuItem(40, 165, 200, 27, GOB_BUBBLE_HELP),
MenuItem(260, 165, 200, 27, GOB_BUBBLE_HELP_VALUE),
MenuItem(40, 255, 200, 27, GOB_APPLY),
MenuItem(260, 255, 200, 27, GOB_BACK),
MenuItem(MIT_LAST_ENTRY),
};
The handler then reads switch (button) { case GOB_APPLY: ... case GOB_BACK: ... }. An enum constant compiles to the same immediate, so the match does not move.
What has to be inline and what cannot be
Every MenuItem constructor must be defined inline in the class. The folding depends on it. An inline factory returning MenuItem by value also folds.
Derived classes must not be sliced into the array.struct GmImageButton : MenuItem emits a dynamic initializer (a temporary plus rep movsd) that the original does not have.
Menu, MenuView and MenuModal constructors stay out of line. The original calls them (0x4F4100, 0x4F4020, 0x4A9E00) from the static-initializer stubs, and they cannot fold anyway: Menu's walks the table, and MenuView's and MenuModal's push this onto a global list. Only their globals change form, for example Menu Menu_MainMenu(MenuItems_MainMenu);.
Why constructors rather than macros
Brace aggregates behind macros produce the same bytes, so the binary cannot decide between the two. Two things in the source point to constructors:
The callback field is a union. A C++98 brace initializer can only set a union's first member, so the slider and scrollbar entries (32 of them, plus their render functions) would each need a cast. A constructor assigns the right member directly.
Fields that follow from the item kind come out naturally as constructor defaults:
renderType == 3 always comes with w = h = 0.
renderType == 4 appears if and only if the type is 5 or 6.
testNotEqualZero is 0 in all 2,198 items.
Every field from ucID onward is 0.
One wrinkle: a text button (x, y, w, h, id) and an image button (x, y, id, gm, help) are both five ints. Taking the image as GmDataIndex makes them distinct overloads, which is also the natural type for that parameter.
Open questions
RenderingDefinedData is one generated struct over a contiguous .data region. Writing the tables in source means they become separate globals, linked in the same order, instead of array members of that struct.
The generated MenuItem.hpp has a private copy constructor. That rules out the factory form, though plain constructors don't need a copy.
The 0x40000000 bit on gmDataIndex is unexplained. It is set on 223 image items and clear on 433 others. I passed it through as imageFlags for now.
Naming the IDs is independent of the constructor form. Per-menu ID enums are cheap and could go first.
The static-initializer addresses come in blocks of all 47 MenuViews, then all 108 Menus (in table order), then all 74 MenuModals. That is consistent with one menu-data source file declaring them kind by kind.
Feedback welcome, particularly on whether the tables should move out of RenderingDefinedData at all.
Addendum: Menu, MenuView and MenuModal
These three are the counterpart to MenuItem: their constructors are not inline, and the globals built from them are what produce the Constructor_Menu* / Constructor_MenuView* / Constructor_MenuModal* stubs in Global/Init. I compiled the global definitions below with the bundled VS2005 compiler at /O2. The dynamic initializers it emits match the original stubs (0x0059AAA0, 0x0059A730, 0x0059B620) push for push, including the movsx eax, word ptr [COL_WHITE] that the MenuModal stub starts with. The constructor bodies are the decompiled ones and have not been compared on their own yet.
// a table of items; the constructor fills in what the table leaves outclassMenu {
public:
MenuItem* menuItemArray;
// ... the rest of the 0x44-byte layout ...explicitMenu(MenuItem* items);
// no destructor: the original registers no atexit for any Menu
};
// a screen; it registers itself on a list of all viewsclassMenuView {
public:
dword unknown_0x0;
MenuViewTypeInt menuID;
MenuView* nextMenuViewPtr;
cdeclVoidFunc* prepare;
cdeclVoidFunc* doInitial;
cdeclVoidFunc* doEveryFrame;
MenuView(MenuViewType menuID, cdeclVoidFunc* prepare, cdeclVoidFunc* doInitial, cdeclVoidFunc* doEveryFrame);
~MenuView();
};
// a dialog over a view; it registers itself on a list of all modalsclassMenuModal {
public:
MenuModalTypeInt menuModalID;
int x, y, width, height;
int borderStyle;
int backgroundColourIndex;
MenuModalRenderFunction* menuModalRenderFunction;
Menu* pointerToMenu;
MenuModal* pointerToNextModalMenu;
MenuModal(MenuModalType menuModalID, int x, int y, int width, int height, int borderStyle,
int backgroundColourIndex, MenuModalRenderFunction* render, Menu* menu);
~MenuModal();
};
The definitions go in .cpp files. Menu::Menu (0x004F4100) and MenuView::MenuView (0x004F4020) sit next to each other, so they probably shared a file. MenuModal::MenuModal (0x004A9E00) lives elsewhere.
// the body of Constructor_Menu.cpp: point every item at its menu, compute the skip counts,// copy each group head's callbacks into members that have none, mark the heads of alternativesMenu::Menu(MenuItem* items) { /* ... */ }
MenuView::MenuView(MenuViewType menuID, cdeclVoidFunc* prepare, cdeclVoidFunc* doInitial, cdeclVoidFunc* doEveryFrame)
{
this->menuID = menuID;
this->prepare = prepare;
this->doInitial = doInitial;
this->doEveryFrame = doEveryFrame;
this->nextMenuViewPtr = DAT_MenuViewStackTop;
DAT_MenuViewStackTop = this;
}
// out of line and empty: the linker folded it with every other empty function (0x00469F10, DoNothing)MenuView::~MenuView() {}
MenuModal::MenuModal(MenuModalType menuModalID, int x, int y, int width, int height, int borderStyle,
int backgroundColourIndex, MenuModalRenderFunction* render, Menu* menu)
{
this->menuModalID = menuModalID;
this->x = x;
this->y = y;
this->width = width;
this->height = height;
this->borderStyle = borderStyle;
this->backgroundColourIndex = backgroundColourIndex;
this->menuModalRenderFunction = render;
this->pointerToMenu = menu;
this->pointerToNextModalMenu = DAT_ModalMenuArrayPointerToStackTop;
DAT_ModalMenuArrayPointerToStackTop = this;
}
MenuModal::~MenuModal() {}
The globals then read as one declaration per screen. In the original the three kinds come in separate blocks, all 47 views, then all 108 menus, then all 74 modals, so the file probably declared them kind by kind:
MenuView MenuView_MainMenu(MVT_MAIN_MENU, MenuView_MainMenu_Prepare, MenuView_MainMenu_DoInitial,
MenuView_MainMenu_DoEveryFrame);
// ... the other 46 views ...
Menu Menu_MainMenu(MenuItems_MainMenu);
Menu Menu_GameplayOptions(MenuItems_GameplayOptions);
// ... the other 106 menus, in table order ...// COL_WHITE is a 16-bit colour set up at run time, hence the movsx in the stub
MenuModal MenuModal_GameplayOptions(MMT_GAMEPLAY_OPTIONS, -1, -1, 500, 357, 512, COL_WHITE,
MenuModalRenderFunction_GameplayOptions, &Menu_GameplayOptions);
// ... the other 73 modals ...
Two details pin down the destructors:
MenuView and MenuModal have out-of-line empty destructors. Each of their stubs registers an atexit handler of the form mov ecx, &object; jmp ~T, which is what an out-of-line destructor produces. An empty destructor defined inline in the class still gets the atexit registration, but its handler compiles to a bare ret, and the original's does not.
Menu has no user-declared destructor at all, since its stub registers nothing.
Together with the inline MenuItem constructors above, the source would hold the tables and these globals and nothing else: no hand-written initializer functions, no atexit calls and no fix-ups. The 229 Global/Init stubs and their Meta destructor stubs would all be compiler-generated from the declarations.
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
The
MenuItemhandlers (MenuItemActionHandler_*,MenuItemRenderFunction_*) are longswitches over magic numbers, and the tables inRenderingDefinedDataare 2,198 raw structs. This proposes a source form for the tables that is more likely to be what the original looked like, and that also makes the handlers readable. Background notes are on the branchdocs/static-init-cheat-sheet(IMPLEMENTATION_CHEAT_SHEET.md, Static Initialization of Global Data).Why the handlers are switches
The tables explain the switches. Of the 2,198 items, 246 are group heads (
0x01000000) that carry an action and a render callback. 1,445 are group members (0x02000003) withNULLcallbacks and only an ID incallbackParameter.Menu::Constructor_Menuthen copies each head's callbacks into members that have none. So the original wrote one handler per group and told the buttons apart by ID, the same idiom as a Win32 dialog proc withswitch (LOWORD(wParam)). The switches are probably genuine. What is missing is names for the IDs, and the original probably had them:GameplayOptionsrender callsgetTextStringInGroupAtOffset(TEXT_GAME_OPTIONS, button), and 17 ("Back") recurs across dialogs. Negative IDs are value displays.Proposal: inline
MenuItemconstructorsVS2005 at
/O2constant-folds inline constructors into static.data, so a table written with constructors produces no initializer code. I wrote theMainMenu,GameplayOptionsandSoundOptionstables this way and compiled them with the bundled compiler at the project's flags. All 37 items are byte-identical to the exe (function pointers compared by address), and no??__Edynamic initializer is emitted.The handler then reads
switch (button) { case GOB_APPLY: ... case GOB_BACK: ... }. An enum constant compiles to the same immediate, so the match does not move.What has to be inline and what cannot be
MenuItemconstructor must be defined inline in the class. The folding depends on it. An inline factory returningMenuItemby value also folds.struct GmImageButton : MenuItememits a dynamic initializer (a temporary plusrep movsd) that the original does not have.Menu,MenuViewandMenuModalconstructors stay out of line. The original calls them (0x4F4100,0x4F4020,0x4A9E00) from the static-initializer stubs, and they cannot fold anyway:Menu's walks the table, andMenuView's andMenuModal's pushthisonto a global list. Only their globals change form, for exampleMenu Menu_MainMenu(MenuItems_MainMenu);.Why constructors rather than macros
Brace aggregates behind macros produce the same bytes, so the binary cannot decide between the two. Two things in the source point to constructors:
renderType == 3always comes withw = h = 0.renderType == 4appears if and only if the type is 5 or 6.testNotEqualZerois 0 in all 2,198 items.ucIDonward is 0.One wrinkle: a text button
(x, y, w, h, id)and an image button(x, y, id, gm, help)are both fiveints. Taking the image asGmDataIndexmakes them distinct overloads, which is also the natural type for that parameter.Open questions
RenderingDefinedDatais one generated struct over a contiguous.dataregion. Writing the tables in source means they become separate globals, linked in the same order, instead of array members of that struct.MenuItem.hpphas a private copy constructor. That rules out the factory form, though plain constructors don't need a copy.0x40000000bit ongmDataIndexis unexplained. It is set on 223 image items and clear on 433 others. I passed it through asimageFlagsfor now.The static-initializer addresses come in blocks of all 47
MenuViews, then all 108Menus (in table order), then all 74MenuModals. That is consistent with one menu-data source file declaring them kind by kind.Feedback welcome, particularly on whether the tables should move out of
RenderingDefinedDataat all.Addendum:
Menu,MenuViewandMenuModalThese three are the counterpart to
MenuItem: their constructors are not inline, and the globals built from them are what produce theConstructor_Menu*/Constructor_MenuView*/Constructor_MenuModal*stubs inGlobal/Init. I compiled the global definitions below with the bundled VS2005 compiler at/O2. The dynamic initializers it emits match the original stubs (0x0059AAA0,0x0059A730,0x0059B620) push for push, including themovsx eax, word ptr [COL_WHITE]that theMenuModalstub starts with. The constructor bodies are the decompiled ones and have not been compared on their own yet.The definitions go in .cpp files.
Menu::Menu(0x004F4100) andMenuView::MenuView(0x004F4020) sit next to each other, so they probably shared a file.MenuModal::MenuModal(0x004A9E00) lives elsewhere.The globals then read as one declaration per screen. In the original the three kinds come in separate blocks, all 47 views, then all 108 menus, then all 74 modals, so the file probably declared them kind by kind:
Two details pin down the destructors:
MenuViewandMenuModalhave out-of-line empty destructors. Each of their stubs registers anatexithandler of the formmov ecx, &object; jmp ~T, which is what an out-of-line destructor produces. An empty destructor defined inline in the class still gets theatexitregistration, but its handler compiles to a bareret, and the original's does not.Menuhas no user-declared destructor at all, since its stub registers nothing.Together with the inline
MenuItemconstructors above, the source would hold the tables and these globals and nothing else: no hand-written initializer functions, noatexitcalls and no fix-ups. The 229Global/Initstubs and theirMetadestructor stubs would all be compiler-generated from the declarations.🤖 Generated with Claude Code
All reactions