From 1992acac2ace6074fc8a06f3dfaf8d7a9956ea59 Mon Sep 17 00:00:00 2001 From: Brues <5278969+brues-code@users.noreply.github.com> Date: Sat, 7 Nov 2026 10:06:05 -0600 Subject: [PATCH] refactor(frame): share the frame-class plumbing in Frame::Subclass Cooldown and ModelScene each carried their own copy of the parts every frame class this DLL adds needs: a type id, IsA, IsObjectType, GetObjectType, GetScriptMethod, the deleting destructor and its layout thunk, the vtable build, and a factory body that allocates the block with SMemAlloc (FUN_STORM_SMEM_ALLOC, zero-filled), runs the engine class's constructor and installs the vtables. The copies had already drifted: Cooldown compared type names with _stricmp, ModelScene with Storm's SStrCmpI. Frame::Subclass::Class now holds a class's name, the engine class it extends (vtable, layout vtable, constructor), its size and a function that releases its own fields, plus its type id, method hash and vtable copies. Templates on that object give the shared overrides, BuildVTables, New and RegisterFactory (FUN_FRAMEXML_REGISTER_FACTORY). Both classes now answer IsObjectType through Game::StrCmpI and take CSimpleFrame's script resolver (+0x0C), which Cooldown's copied vtable already held. --- src/frame/Cooldown.cpp | 134 +++++++++++---------------------------- src/frame/Subclass.cpp | 12 ++++ src/frame/Subclass.h | 139 ++++++++++++++++++++++++++++++++++++++++- src/model/Scene.cpp | 106 ++++++++----------------------- 4 files changed, 212 insertions(+), 179 deletions(-) diff --git a/src/frame/Cooldown.cpp b/src/frame/Cooldown.cpp index d7be6632..ac26c483 100644 --- a/src/frame/Cooldown.cpp +++ b/src/frame/Cooldown.cpp @@ -223,20 +223,28 @@ uint32_t ColorByte(double c) { return static_cast(c * 255.0 + 0.5); } -// ---- vtables ---------------------------------------------------------------- +// ---- the class ---------------------------------------------------------------- -Frame::Subclass::VTable g_vtable; -Frame::Subclass::VTable g_layoutVtable; +// CGCooldown::~CGCooldown, before CSimpleFrame's deleting destructor. +void Destroy(void *frame) { + Fields &f = F(frame); + for (Slot &s : f.slots) { + Texture::ReleaseHandle(s.texture); + s.texture = nullptr; + } + Text::NumericFormatter::Release(f.countdownFormatter); + f.countdownFormatter = nullptr; + delete f.countdownText; + f.countdownText = nullptr; +} -// CSimpleFrame's own slot, for the base call of an override. -template -Fn Base(uintptr_t slot) { - return Frame::Subclass::Slot(Offsets::VTBL_SIMPLEFRAME, slot); -} -template -Fn LayoutBase(uintptr_t slot) { - return Frame::Subclass::Slot(Offsets::VTBL_SIMPLEFRAME_LAYOUT, slot); -} +Frame::Subclass::Class + g_class{"Cooldown", + Offsets::VTBL_SIMPLEFRAME, + Offsets::VTBL_SIMPLEFRAME_LAYOUT, + Offsets::FUN_SIMPLEFRAME_CONSTRUCTOR, + kObjectSize, + &Destroy}; // 1.12's CSimpleFrame::Show / Hide, as Script Show / Hide make them. void Show(void *frame) { @@ -625,23 +633,13 @@ void UpdateCountdown(void *frame) { // ---- the overrides ------------------------------------------------------------ -using DeletingDestructor_t = void *(__fastcall *)(void *self, void *edx, uint32_t flags); -using GetScriptMethod_t = int(__fastcall *)(void *self, void *edx, void *L, const char *name); -using IsA_t = bool(__fastcall *)(void *self, void *edx, int typeId); -using IsObjectType_t = bool(__fastcall *)(void *self, void *edx, const char *name); using SetFrameAlpha_t = void(__fastcall *)(void *self, void *edx, uint8_t alpha); using OnFrameUpdate_t = void(__fastcall *)(void *self, void *edx, float elapsedSec); using EmitLayer_t = void(__fastcall *)(void *self, void *edx, void *batch, int layer); using OnFrameSizeChanged_t = void(__fastcall *)(void *layout, void *edx, const float *oldRect); using LoadXML_t = uintptr_t(__fastcall *)(void *layout, void *edx, const void *node, void *status); -// The class's method hash and type id (Offsets.h "Per-class method hashes"). -Frame::Subclass::MethodHash g_hash; -int g_typeId = 0; - -uintptr_t TypeIdVar() { return reinterpret_cast(&g_typeId); } - -const Frame::ExtraScripts::Script g_onCooldownDone{"oncooldowndone", TypeIdVar()}; +const Frame::ExtraScripts::Script g_onCooldownDone{"oncooldowndone", g_class.TypeIdVar()}; // From the update walk, where the engine runs OnUpdate. void RunOnCooldownDone(void *frame) { @@ -654,52 +652,15 @@ void RunOnCooldownDone(void *frame) { Frame::ScriptArgs::RunSlotWithValues(L, slot, frame, Game::Lua::GetTop(L) + 1, 0); } -// CGCooldown::~CGCooldown, then CSimpleFrame's deleting destructor. -void *__fastcall DeletingDestructor(void *self, void *, uint32_t flags) { - Fields &f = F(self); - for (Slot &s : f.slots) { - Texture::ReleaseHandle(s.texture); - s.texture = nullptr; - } - Text::NumericFormatter::Release(f.countdownFormatter); - f.countdownFormatter = nullptr; - delete f.countdownText; - f.countdownText = nullptr; - return Base(Offsets::VTBL_FRAME_DELETING_DESTRUCTOR)(self, nullptr, - flags); -} - -void *__fastcall LayoutDeletingDestructor(void *layout, void *, uint32_t flags) { - return DeletingDestructor(FrameOfLayout(layout), nullptr, flags); -} - -int __fastcall GetScriptMethod(void *self, void *, void *L, const char *name) { - if (g_hash.PushMethod(L, name)) - return 1; - return Base(Offsets::VTBL_FRAME_GET_SCRIPT_METHOD)(self, nullptr, L, name); -} - -bool __fastcall IsA(void *self, void *, int typeId) { - return (g_typeId != 0 && typeId == g_typeId) || - Base(Offsets::VTBL_FRAME_IS_A)(self, nullptr, typeId); -} - -bool __fastcall IsObjectType(void *self, void *, const char *name) { - return (name != nullptr && _stricmp(name, "Cooldown") == 0) || - Base(Offsets::VTBL_FRAME_IS_OBJECT_TYPE)(self, nullptr, name); -} - -const char *__fastcall GetObjectType(void *, void *) { return "Cooldown"; } - // OFFSET (4.3.4): 0x009FF570 (UpdateAlpha) void __fastcall SetFrameAlpha(void *self, void *, uint8_t alpha) { - Base(Offsets::VTBL_FRAME_SET_FRAME_ALPHA)(self, nullptr, alpha); + g_class.Base(Offsets::VTBL_FRAME_SET_FRAME_ALPHA)(self, nullptr, alpha); UpdateColors(self); } // OFFSET (4.3.4): 0x009FF5A0 (OnLayerUpdate) void __fastcall OnFrameUpdate(void *self, void *, float elapsedSec) { - Base(Offsets::VTBL_FRAME_ON_FRAME_UPDATE)(self, nullptr, elapsedSec); + g_class.Base(Offsets::VTBL_FRAME_ON_FRAME_UPDATE)(self, nullptr, elapsedSec); Fields &f = F(self); // 4.3.4: `now <= start ? 0 : now - start`, compared wrap-safe. A paused // frame reads the time it was paused at. @@ -747,7 +708,7 @@ void __fastcall OnFrameUpdate(void *self, void *, float elapsedSec) { // OFFSET (4.3.4): 0x009FE830 (OnFrameRender) void __fastcall EmitLayer(void *self, void *, void *batch, int layer) { - Base(Offsets::VTBL_FRAME_EMIT_LAYER)(self, nullptr, batch, layer); + g_class.Base(Offsets::VTBL_FRAME_EMIT_LAYER)(self, nullptr, batch, layer); Fields &f = F(self); using RenderBatch::QueueStrip; void *swipe = f.slots[SLOT_SWIPE].texture; @@ -767,7 +728,7 @@ void __fastcall EmitLayer(void *self, void *, void *batch, int layer) { // OFFSET (4.3.4): 0x009FF580 void __fastcall OnFrameSizeChanged(void *layout, void *, const float *oldRect) { - LayoutBase(Offsets::VTBL_LAYOUT_ON_FRAME_SIZE_CHANGED)(layout, nullptr, + g_class.LayoutBase(Offsets::VTBL_LAYOUT_ON_FRAME_SIZE_CHANGED)(layout, nullptr, oldRect); ResetVerts(FrameOfLayout(layout)); } @@ -816,7 +777,7 @@ uintptr_t __fastcall LoadXML(void *layout, void *, const void *node, void *statu return 0; } const uintptr_t result = - LayoutBase(Offsets::VTBL_XML_NODE_LOADER)(layout, nullptr, node, status); + g_class.LayoutBase(Offsets::VTBL_XML_NODE_LOADER)(layout, nullptr, node, status); if (node == nullptr) return result; Fields &f = F(FrameOfLayout(layout)); @@ -852,37 +813,23 @@ uintptr_t __fastcall LoadXML(void *layout, void *, const void *node, void *statu } void BuildVTables() { - if (g_vtable.Built()) + if (!Frame::Subclass::BuildVTables()) return; - g_vtable.CopyFrom(Offsets::VTBL_SIMPLEFRAME); - g_layoutVtable.CopyFrom(Offsets::VTBL_SIMPLEFRAME_LAYOUT); auto fn = [](auto f) { return reinterpret_cast(f); }; - g_vtable.Set(Offsets::VTBL_FRAME_DELETING_DESTRUCTOR, fn(&DeletingDestructor)); - g_vtable.Set(Offsets::VTBL_FRAME_GET_SCRIPT_METHOD, fn(&GetScriptMethod)); - g_vtable.Set(Offsets::VTBL_FRAME_IS_A, fn(&IsA)); - g_vtable.Set(Offsets::VTBL_FRAME_IS_OBJECT_TYPE, fn(&IsObjectType)); - g_vtable.Set(Offsets::VTBL_FRAME_GET_OBJECT_TYPE, fn(&GetObjectType)); - g_vtable.Set(Offsets::VTBL_FRAME_SET_FRAME_ALPHA, fn(&SetFrameAlpha)); - g_vtable.Set(Offsets::VTBL_FRAME_ON_FRAME_UPDATE, fn(&OnFrameUpdate)); - g_vtable.Set(Offsets::VTBL_FRAME_EMIT_LAYER, fn(&EmitLayer)); - g_layoutVtable.Set(Offsets::VTBL_LAYOUT_ON_FRAME_SIZE_CHANGED, fn(&OnFrameSizeChanged)); - g_layoutVtable.Set(Offsets::VTBL_LAYOUT_DELETING_DESTRUCTOR, fn(&LayoutDeletingDestructor)); - g_layoutVtable.Set(Offsets::VTBL_XML_NODE_LOADER, fn(&LoadXML)); + g_class.vtable.Set(Offsets::VTBL_FRAME_SET_FRAME_ALPHA, fn(&SetFrameAlpha)); + g_class.vtable.Set(Offsets::VTBL_FRAME_ON_FRAME_UPDATE, fn(&OnFrameUpdate)); + g_class.vtable.Set(Offsets::VTBL_FRAME_EMIT_LAYER, fn(&EmitLayer)); + g_class.layoutVtable.Set(Offsets::VTBL_LAYOUT_ON_FRAME_SIZE_CHANGED, fn(&OnFrameSizeChanged)); + g_class.layoutVtable.Set(Offsets::VTBL_XML_NODE_LOADER, fn(&LoadXML)); } // OFFSET (4.3.4): 0x008C6390 (CGCooldown::Create) — the "Cooldown" factory. void *__fastcall CreateCooldown(void *parent) { - using SMemAlloc_t = void *(__stdcall *)(size_t size, const char *file, int line, int flags); - using Constructor_t = void(__fastcall *)(void *self, void *edx, void *parent); BuildVTables(); // Zero-filled: 4.3.4's constructor zeroes every CGCooldown field. - void *frame = reinterpret_cast(Offsets::FUN_STORM_SMEM_ALLOC)( - kObjectSize, __FILE__, __LINE__, 0x08); + void *frame = Frame::Subclass::New(parent); if (frame == nullptr) return nullptr; - reinterpret_cast(Offsets::FUN_SIMPLEFRAME_CONSTRUCTOR)(frame, nullptr, parent); - Game::Ref(frame, 0) = g_vtable.slots; - Game::Ref(frame, Offsets::OFF_FRAME_LAYOUT_SUBOBJECT) = g_layoutVtable.slots; Fields &f = F(frame); f.modRate = 1.0f; f.drawSwipe = 1; @@ -900,7 +847,7 @@ void *__fastcall CreateCooldown(void *parent) { // ---- the methods (CGCooldown_Scripts.cpp, then the later clients') ------------------- -void *ResolveCooldown(void *L) { return Game::Lua::ResolveTypedObject(L, 1, TypeIdVar()); } +void *ResolveCooldown(void *L) { return Game::Lua::ResolveTypedObject(L, 1, g_class.TypeIdVar()); } // Seconds to ms on the GetTime() epoch, wrapping like the tick does. uint32_t Ms(double seconds) { return static_cast(static_cast(seconds * 1000.0)); } @@ -1545,21 +1492,16 @@ const Game::Doc::Method g_methodDocs[] = { }; void RegisterLuaFunctions() { - using RegisterFactory_t = int(__fastcall *)(const char *type, void *(__fastcall *)(void *), - int unique); - // The engine's frame-type table outlives the Lua state, so on a /reload - // the name is already there and the registrar keeps the first one. This - // pass runs before FrameXML loads (CGGameUI::Initialize calls + // This pass runs before FrameXML loads (CGGameUI::Initialize calls // LoadScriptFunctions first), like the engine's own game-UI list // FUN_00495940, where 4.3.4 registers "Cooldown". g_countdownForCooldowns = CVar::Factory::Register( "countdownForCooldowns", "Whether to use number countdown instead of radial swipe for action button cooldowns or not.", Offsets::CVAR_CATEGORY_GAME, "0"); - reinterpret_cast(Offsets::FUN_FRAMEXML_REGISTER_FACTORY)( - "Cooldown", &CreateCooldown, 0); - Api::Documentation::NameWidgetSystem(g_hash.Registry(), "FrameAPICooldown"); - Game::Lua::RegisterFrameMethods(g_hash.Registry(), g_methods, g_methodDocs); + Frame::Subclass::RegisterFactory(g_class.name, &CreateCooldown); + Api::Documentation::NameWidgetSystem(g_class.hash.Registry(), "FrameAPICooldown"); + Game::Lua::RegisterFrameMethods(g_class.hash.Registry(), g_methods, g_methodDocs); } const Game::ModuleAutoRegister _autoreg{&RegisterLuaFunctions}; diff --git a/src/frame/Subclass.cpp b/src/frame/Subclass.cpp index d9434c00..f76ce813 100644 --- a/src/frame/Subclass.cpp +++ b/src/frame/Subclass.cpp @@ -21,6 +21,8 @@ namespace { // static initialization whatever the order of the translation units. MethodHash *g_hashes = nullptr; +using SMemAlloc_t = void *(__stdcall *)(size_t size, const char *file, int line, int flags); +using RegisterFactory_t = int(__fastcall *)(const char *type, Factory_t factory, int unique); using PushMethod_t = int(__fastcall *)(void *L, const char *name, void *hash); using UnregisterMethods_t = void(__fastcall *)(void *hash); using UnregisterScriptFunctions_t = void(__cdecl *)(); @@ -39,6 +41,16 @@ const Game::HookAutoRegister _unregisterHook{ } // namespace +void *Allocate(size_t size, const char *file, int line) { + constexpr int kZeroFill = 0x08; + return reinterpret_cast(Offsets::FUN_STORM_SMEM_ALLOC)(size, file, line, + kZeroFill); +} + +void RegisterFactory(const char *type, Factory_t factory) { + reinterpret_cast(Offsets::FUN_FRAMEXML_REGISTER_FACTORY)(type, factory, 0); +} + MethodHash::MethodHash() : words_{}, next_(g_hashes) { words_[0] = Offsets::VTBL_METHOD_HASH_ALLOCATOR; words_[1] = 0xC; // node link offset diff --git a/src/frame/Subclass.h b/src/frame/Subclass.h index e8821c82..4d564126 100644 --- a/src/frame/Subclass.h +++ b/src/frame/Subclass.h @@ -14,9 +14,9 @@ #pragma once // The parts every frame class this DLL adds shares: copied engine vtables with -// overrides in place, and a method hash of the class's own (Offsets.h -// "Subclassing CSimpleFrame", "Per-class method hashes"). Frame::Cooldown and -// Model::Scene are built on it. +// overrides in place, a method hash of the class's own, its type id and +// type-name overrides, and the factory (Offsets.h "Subclassing CSimpleFrame", +// "Per-class method hashes"). Frame::Cooldown and Model::Scene are built on it. #include "Game.h" #include "Offsets.h" @@ -77,4 +77,137 @@ class MethodHash { MethodHash *next_; }; +// CSimpleFrame's own slot, for the base call of an override that answers as +// a Frame whatever engine class the frame extends. +template +Fn FrameBase(uintptr_t slot) { + return Slot(Offsets::VTBL_SIMPLEFRAME, slot); +} + +// A frame class this DLL adds: what it is and what it extends, then the +// state the engine and the shared overrides below keep for it. Declare one +// at file scope; the overrides take it as their template argument. +template +struct Class { + const char *name; // what GetObjectType gives and IsObjectType takes + uintptr_t engineVtable; // the engine class it extends: its vtable, + uintptr_t engineLayoutVtable; // its layout vtable, + uintptr_t constructor; // and its constructor, __thiscall (frame, parent) + size_t size; // the whole object, the engine class's part and ours + void (*destroy)(void *frame); // releases our part, before the engine's destructor + + int typeId = 0; // the engine's type id for the class, once it names one + MethodHash hash; + VTable vtable; + VTable layoutVtable; + + // What Game::Lua::ResolveTypedObject and Frame::ExtraScripts take. + uintptr_t TypeIdVar() { return reinterpret_cast(&typeId); } + // The engine class's own slot, for the base call of an override. + template + Fn Base(uintptr_t slot) const { + return Slot(engineVtable, slot); + } + template + Fn LayoutBase(uintptr_t slot) const { + return Slot(engineLayoutVtable, slot); + } +}; + +// ---- the overrides every class has -------------------------------------------- + +using DeletingDestructor_t = void *(__fastcall *)(void *self, void *edx, uint32_t flags); +using GetScriptMethod_t = int(__fastcall *)(void *self, void *edx, void *L, const char *name); +using IsA_t = bool(__fastcall *)(void *self, void *edx, int typeId); +using IsObjectType_t = bool(__fastcall *)(void *self, void *edx, const char *name); + +// Our part released, then the engine class's deleting destructor. +template +void *__fastcall DeletingDestructor(void *self, void *, uint32_t flags) { + C.destroy(self); + return C.template Base(Offsets::VTBL_FRAME_DELETING_DESTRUCTOR)( + self, nullptr, flags); +} + +template +void *__fastcall LayoutDeletingDestructor(void *layout, void *, uint32_t flags) { + return DeletingDestructor(FrameOfLayout(layout), nullptr, flags); +} + +// The class's methods, then a Frame's: none of the engine class's own +// (ModelScene has no Model methods). +template +int __fastcall GetScriptMethod(void *self, void *, void *L, const char *name) { + if (C.hash.PushMethod(L, name)) + return 1; + return FrameBase(Offsets::VTBL_FRAME_GET_SCRIPT_METHOD)(self, nullptr, L, + name); +} + +// The class, then what a Frame is (retail's ModelScene is not a Model). +template +bool __fastcall IsA(void *self, void *, int typeId) { + return (C.typeId != 0 && typeId == C.typeId) || + FrameBase(Offsets::VTBL_FRAME_IS_A)(self, nullptr, typeId); +} + +template +bool __fastcall IsObjectType(void *self, void *, const char *name) { + return (name != nullptr && Game::StrCmpI(name, C.name) == 0) || + FrameBase(Offsets::VTBL_FRAME_IS_OBJECT_TYPE)(self, nullptr, name); +} + +template +const char *__fastcall GetObjectType(void *, void *) { + return C.name; +} + +// Copies the engine class's two vtables and puts in the overrides above, with +// CSimpleFrame's script names (+0x0C) to match its methods. False when they +// are built already; on true the caller puts in its own overrides. +template +bool BuildVTables() { + if (C.vtable.Built()) + return false; + C.vtable.CopyFrom(C.engineVtable); + C.layoutVtable.CopyFrom(C.engineLayoutVtable); + auto fn = [](auto f) { return reinterpret_cast(f); }; + C.vtable.Set(Offsets::VTBL_FRAME_DELETING_DESTRUCTOR, fn(&DeletingDestructor)); + C.vtable.Set(Offsets::VTBL_FRAME_GET_SCRIPT_METHOD, fn(&GetScriptMethod)); + C.vtable.Set(Offsets::VTBL_FRAME_SCRIPT_RESOLVER, + FrameBase(Offsets::VTBL_FRAME_SCRIPT_RESOLVER)); + C.vtable.Set(Offsets::VTBL_FRAME_IS_A, fn(&IsA)); + C.vtable.Set(Offsets::VTBL_FRAME_IS_OBJECT_TYPE, fn(&IsObjectType)); + C.vtable.Set(Offsets::VTBL_FRAME_GET_OBJECT_TYPE, fn(&GetObjectType)); + C.layoutVtable.Set(Offsets::VTBL_LAYOUT_DELETING_DESTRUCTOR, + fn(&LayoutDeletingDestructor)); + return true; +} + +// The allocation an engine factory makes (the "Frame" one, 0x006EEC10), sized +// for the whole object and zero-filled; null when it fails. +void *Allocate(size_t size, const char *file, int line); + +// A new frame of the class, shaped like the engine's factories: the block +// (our part zero), the engine class's constructor, then our vtables, which +// BuildVTables must have built. Null when the allocation fails. +template +void *New(void *parent) { + using Constructor_t = void(__fastcall *)(void *self, void *edx, void *parent); + void *frame = Allocate(C.size, __FILE__, __LINE__); + if (frame == nullptr) + return nullptr; + reinterpret_cast(C.constructor)(frame, nullptr, parent); + Game::Ref(frame, 0) = C.vtable.slots; + Game::Ref(frame, Offsets::OFF_FRAME_LAYOUT_SUBOBJECT) = C.layoutVtable.slots; + return frame; +} + +using Factory_t = void *(__fastcall *)(void *parent); + +// Adds the frame type to CreateFrame and XML (FUN_FRAMEXML_REGISTER_FACTORY). +// The engine's type table outlives the Lua state, so on a /reload the name +// is there already and the engine keeps the first factory. +void RegisterFactory(const char *type, Factory_t factory); + } // namespace Frame::Subclass diff --git a/src/model/Scene.cpp b/src/model/Scene.cpp index 7864f50f..9ddaa084 100644 --- a/src/model/Scene.cpp +++ b/src/model/Scene.cpp @@ -452,67 +452,34 @@ bool Project(void *frame, const Vec3 &point, double out[3]) { // ---- the overrides ----------------------------------------------------------------- -using DeletingDestructor_t = void *(__fastcall *)(void *self, void *edx, uint32_t flags); -using GetScriptMethod_t = int(__fastcall *)(void *self, void *edx, void *L, const char *name); -using IsA_t = bool(__fastcall *)(void *self, void *edx, int typeId); -using IsObjectType_t = bool(__fastcall *)(void *self, void *edx, const char *name); using SetFrameAlpha_t = void(__fastcall *)(void *self, void *edx, uint8_t alpha); using OnFrameUpdate_t = void(__fastcall *)(void *self, void *edx, float elapsedSec); using EmitLayer_t = void(__fastcall *)(void *self, void *edx, void *batch, int layer); using Callback_t = void(__fastcall *)(void *arg); using AddCallback_t = void(__fastcall *)(void *batch, void *edx, Callback_t fn, void *arg); -Frame::Subclass::VTable g_vtable; -Frame::Subclass::VTable g_layoutVtable; -Frame::Subclass::MethodHash g_hash; -int g_typeId = 0; - -uintptr_t TypeIdVar() { return reinterpret_cast(&g_typeId); } - -template -Fn ModelBase(uintptr_t slot) { - return Frame::Subclass::Slot(Offsets::VTBL_SIMPLEMODEL, slot); -} -template -Fn FrameBase(uintptr_t slot) { - return Frame::Subclass::Slot(Offsets::VTBL_SIMPLEFRAME, slot); -} - -void *__fastcall DeletingDestructor(void *self, void *, uint32_t flags) { - Fields &f = F(self); +// The actors destroyed, before Model's deleting destructor (which frees the +// camera and the scene). +void Destroy(void *frame) { + Fields &f = F(frame); for (Actor *actor : *f.actors) delete actor; delete f.actors; f.actors = nullptr; - return ModelBase(Offsets::VTBL_FRAME_DELETING_DESTRUCTOR)(self, nullptr, - flags); } -void *__fastcall LayoutDeletingDestructor(void *layout, void *, uint32_t flags) { - return DeletingDestructor(FrameOfLayout(layout), nullptr, flags); -} +Frame::Subclass::Class + g_class{"ModelScene", + Offsets::VTBL_SIMPLEMODEL, + Offsets::VTBL_SIMPLEMODEL_LAYOUT, + Offsets::FUN_SIMPLEMODEL_CONSTRUCTOR, + kObjectSize, + &Destroy}; -int __fastcall GetScriptMethod(void *self, void *, void *L, const char *name) { - if (g_hash.PushMethod(L, name)) - return 1; - return FrameBase(Offsets::VTBL_FRAME_GET_SCRIPT_METHOD)(self, nullptr, L, - name); -} - -bool __fastcall IsA(void *self, void *, int typeId) { - return (g_typeId != 0 && typeId == g_typeId) || - FrameBase(Offsets::VTBL_FRAME_IS_A)(self, nullptr, typeId); -} - -bool __fastcall IsObjectType(void *self, void *, const char *name) { - return (name != nullptr && Game::StrCmpI(name, "ModelScene") == 0) || - FrameBase(Offsets::VTBL_FRAME_IS_OBJECT_TYPE)(self, nullptr, name); -} - -const char *__fastcall GetObjectType(void *, void *) { return "ModelScene"; } +using Frame::Subclass::FrameBase; void __fastcall SetFrameAlpha(void *self, void *, uint8_t alpha) { - ModelBase(Offsets::VTBL_FRAME_SET_FRAME_ALPHA)(self, nullptr, alpha); + g_class.Base(Offsets::VTBL_FRAME_SET_FRAME_ALPHA)(self, nullptr, alpha); for (Actor *actor : *F(self).actors) actor->ApplyAlpha(); } @@ -523,7 +490,7 @@ void __fastcall OnFrameUpdate(void *self, void *, float elapsedSec) { if (F(self).paused) FrameBase(Offsets::VTBL_FRAME_ON_FRAME_UPDATE)(self, nullptr, elapsedSec); else - ModelBase(Offsets::VTBL_FRAME_ON_FRAME_UPDATE)(self, nullptr, elapsedSec); + g_class.Base(Offsets::VTBL_FRAME_ON_FRAME_UPDATE)(self, nullptr, elapsedSec); // By index: an actor's script may create another. std::vector &actors = *F(self).actors; for (size_t i = 0; i < actors.size(); ++i) @@ -531,7 +498,7 @@ void __fastcall OnFrameUpdate(void *self, void *, float elapsedSec) { } void __fastcall EmitLayer(void *self, void *, void *batch, int layer) { - ModelBase(Offsets::VTBL_FRAME_EMIT_LAYER)(self, nullptr, batch, layer); + g_class.Base(Offsets::VTBL_FRAME_EMIT_LAYER)(self, nullptr, batch, layer); if (layer == F(self).drawLayer) reinterpret_cast(Offsets::FUN_RENDERBATCH_ADD_CALLBACK)(batch, nullptr, &Paint, self); @@ -574,38 +541,22 @@ uintptr_t __fastcall LoadXML(void *layout, void *, const void *node, void *statu } void BuildVTables() { - if (g_vtable.Built()) + if (!Frame::Subclass::BuildVTables()) return; - g_vtable.CopyFrom(Offsets::VTBL_SIMPLEMODEL); - g_layoutVtable.CopyFrom(Offsets::VTBL_SIMPLEMODEL_LAYOUT); auto fn = [](auto f) { return reinterpret_cast(f); }; - g_vtable.Set(Offsets::VTBL_FRAME_DELETING_DESTRUCTOR, fn(&DeletingDestructor)); - g_vtable.Set(Offsets::VTBL_FRAME_GET_SCRIPT_METHOD, fn(&GetScriptMethod)); - g_vtable.Set(Offsets::VTBL_FRAME_SCRIPT_RESOLVER, - FrameBase(Offsets::VTBL_FRAME_SCRIPT_RESOLVER)); - g_vtable.Set(Offsets::VTBL_FRAME_IS_A, fn(&IsA)); - g_vtable.Set(Offsets::VTBL_FRAME_IS_OBJECT_TYPE, fn(&IsObjectType)); - g_vtable.Set(Offsets::VTBL_FRAME_GET_OBJECT_TYPE, fn(&GetObjectType)); - g_vtable.Set(Offsets::VTBL_FRAME_SET_FRAME_ALPHA, fn(&SetFrameAlpha)); - g_vtable.Set(Offsets::VTBL_FRAME_ON_FRAME_UPDATE, fn(&OnFrameUpdate)); - g_vtable.Set(Offsets::VTBL_FRAME_EMIT_LAYER, fn(&EmitLayer)); - g_vtable.Set(Offsets::VTBL_SIMPLEMODEL_UPDATE_MODEL, fn(&UpdateModel)); - g_layoutVtable.Set(Offsets::VTBL_LAYOUT_DELETING_DESTRUCTOR, fn(&LayoutDeletingDestructor)); - g_layoutVtable.Set(Offsets::VTBL_XML_NODE_LOADER, fn(&LoadXML)); + g_class.vtable.Set(Offsets::VTBL_FRAME_SET_FRAME_ALPHA, fn(&SetFrameAlpha)); + g_class.vtable.Set(Offsets::VTBL_FRAME_ON_FRAME_UPDATE, fn(&OnFrameUpdate)); + g_class.vtable.Set(Offsets::VTBL_FRAME_EMIT_LAYER, fn(&EmitLayer)); + g_class.vtable.Set(Offsets::VTBL_SIMPLEMODEL_UPDATE_MODEL, fn(&UpdateModel)); + g_class.layoutVtable.Set(Offsets::VTBL_XML_NODE_LOADER, fn(&LoadXML)); } // The "ModelScene" factory, shaped like the engine's "Model" one (0x006EECF0). void *__fastcall CreateModelScene(void *parent) { - using SMemAlloc_t = void *(__stdcall *)(size_t size, const char *file, int line, int flags); - using Constructor_t = void(__fastcall *)(void *self, void *edx, void *parent); BuildVTables(); - void *frame = reinterpret_cast(Offsets::FUN_STORM_SMEM_ALLOC)( - kObjectSize, __FILE__, __LINE__, 0x08); + void *frame = Frame::Subclass::New(parent); if (frame == nullptr) return nullptr; - reinterpret_cast(Offsets::FUN_SIMPLEMODEL_CONSTRUCTOR)(frame, nullptr, parent); - Game::Ref(frame, 0) = g_vtable.slots; - Game::Ref(frame, Offsets::OFF_FRAME_LAYOUT_SUBOBJECT) = g_layoutVtable.slots; Fields &f = F(frame); f.actors = new std::vector; f.position = {0.0f, 0.0f, 0.0f}; @@ -622,7 +573,7 @@ void *__fastcall CreateModelScene(void *parent) { // ---- the methods ------------------------------------------------------------------ -void *Self(void *L) { return Game::Lua::ResolveTypedObject(L, 1, TypeIdVar()); } +void *Self(void *L) { return Game::Lua::ResolveTypedObject(L, 1, g_class.TypeIdVar()); } bool NumberArgs(void *L, int first, int count, void *frame, const char *usage) { for (int i = 0; i < count; ++i) { @@ -1250,14 +1201,9 @@ const Game::Doc::Method g_methodDocs[] = { }; void RegisterLuaFunctions() { - using RegisterFactory_t = int(__fastcall *)(const char *type, void *(__fastcall *)(void *), - int unique); - // The engine's frame-type table outlives the Lua state, so on a /reload - // the name is already there and the registrar keeps the first one. - reinterpret_cast(Offsets::FUN_FRAMEXML_REGISTER_FACTORY)( - "ModelScene", &CreateModelScene, 0); - Api::Documentation::NameWidgetSystem(g_hash.Registry(), "FrameAPIModelSceneFrame"); - Game::Lua::RegisterFrameMethods(g_hash.Registry(), g_methods, g_methodDocs); + Frame::Subclass::RegisterFactory(g_class.name, &CreateModelScene); + Api::Documentation::NameWidgetSystem(g_class.hash.Registry(), "FrameAPIModelSceneFrame"); + Game::Lua::RegisterFrameMethods(g_class.hash.Registry(), g_methods, g_methodDocs); RegisterActorClass(); }