diff --git a/src/Offsets.h b/src/Offsets.h index d20903bb..cc7da514 100644 --- a/src/Offsets.h +++ b/src/Offsets.h @@ -877,9 +877,12 @@ enum Offsets { // populated (verified: the draw gate tests region+0xD4 != 0), and normally // SetPoint→layout-resolve calls this. We call it directly to place an icon by // its screen rect with NO anchors (anchoring 100+ textures/frame corrupts the - // UI-manager pending-layout list — the FUN_00765650 crash). Co-hooked by - // texture/Rotation.cpp, which re-applies rotation right after the engine - // restores the axis-aligned corners (layout resolve / SetTexCoord). + // UI-manager pending-layout list — the FUN_00765650 crash). Hooked by + // Texture::CornerStore, whose listeners re-apply rotation + // (Texture::Transform) right after the engine restores the axis-aligned + // corners (layout resolve / SetTexCoord). Writes only the corners: + // nothing marks the layer changed, since the layer batch reads them + // through a pointer on every draw. FUN_REGION_STORE_CORNERS = 0x007705B0, // CSimpleTexture::OnFrameSizeChanged — `__thiscall(texture+OFF_REGION_ANCHOR, // const float *oldRect)`, the layout callback: lays out dependents, then diff --git a/src/texture/Atlas.cpp b/src/texture/Atlas.cpp index 02899d59..20e228a7 100644 --- a/src/texture/Atlas.cpp +++ b/src/texture/Atlas.cpp @@ -97,9 +97,7 @@ std::set g_misses; // HTEXTURE that set installed. Comparing the stored handle against the region's // current one on read means a plain `SetTexture` afterwards correctly clears the // atlas — and a recycled region can't report a stale name either, which is what -// lets this module skip a `FUN_SIMPLETEXTURE_CTOR` hook. It must skip one: -// `Texture::Mask` already owns that target, and MinHook refuses a duplicate, -// which would abort the entire hook install. +// lets this module skip a `Texture::Construct` listener. // Texture coordinates in the engine's own corner order: UL, LL, UR, LR, each an // (x, y) pair — the order GetTexCoord pushes them in. Identity selects all of // whatever the texture points at. diff --git a/src/texture/Construct.cpp b/src/texture/Construct.cpp new file mode 100644 index 00000000..f323167d --- /dev/null +++ b/src/texture/Construct.cpp @@ -0,0 +1,43 @@ +// This file is part of ClassicAPI. +// +// ClassicAPI is free software: you can redistribute it and/or modify it under the terms +// of the GNU General Public License as published by the Free Software Foundation, either +// version 3 of the License, or (at your option) any later version. +// +// ClassicAPI is distributed in the hope that it will be useful, but WITHOUT ANY +// WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR +// PURPOSE. See the GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License along with +// ClassicAPI. If not, see . + +#include "texture/Construct.h" + +#include "Game.h" +#include "Offsets.h" + +namespace Texture::Construct { + +namespace { + +AutoSubscribe *g_head = nullptr; + +// __thiscall(mem, parent, layer, sublayer) -> region; dummy-EDX __fastcall. +using Ctor_t = void *(__fastcall *)(void *mem, void *edx, void *parent, int layer, int sublayer); +Ctor_t g_origCtor = nullptr; + +void *__fastcall Ctor_h(void *mem, void *edx, void *parent, int layer, int sublayer) { + if (mem != nullptr) + for (const AutoSubscribe *s = g_head; s != nullptr; s = s->next) + s->fn(mem); + return g_origCtor(mem, edx, parent, layer, sublayer); +} + +const Game::HookAutoRegister _hook{Offsets::FUN_SIMPLETEXTURE_CTOR, reinterpret_cast(&Ctor_h), + reinterpret_cast(&g_origCtor)}; + +} // namespace + +AutoSubscribe::AutoSubscribe(Listener fn) : fn(fn), next(g_head) { g_head = this; } + +} // namespace Texture::Construct diff --git a/src/texture/Construct.h b/src/texture/Construct.h new file mode 100644 index 00000000..6d076aca --- /dev/null +++ b/src/texture/Construct.h @@ -0,0 +1,36 @@ +// This file is part of ClassicAPI. +// +// ClassicAPI is free software: you can redistribute it and/or modify it under the terms +// of the GNU General Public License as published by the Free Software Foundation, either +// version 3 of the License, or (at your option) any later version. +// +// ClassicAPI is distributed in the hope that it will be useful, but WITHOUT ANY +// WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR +// PURPOSE. See the GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License along with +// ClassicAPI. If not, see . + +#pragma once + +// Shared observer for the CSimpleTexture constructor (FUN_SIMPLETEXTURE_CTOR), +// which builds every texture region, in fresh or recycled pool memory alike. +// Listeners run BEFORE the constructor, on the memory it is about to build, so +// state keyed by a region pointer is dropped before a new region takes the +// address. +// +// MinHook allows ONE hook per address and more than one feature keeps state +// keyed by a texture region (Texture::Mask), so this module owns +// the hook and fans out. Declare a file-scope +// static const Texture::Construct::AutoSubscribe _sub{&OnTextureConstructed}; +namespace Texture::Construct { + +using Listener = void (*)(void *region); + +struct AutoSubscribe { + explicit AutoSubscribe(Listener fn); + Listener fn; + AutoSubscribe *next; +}; + +} // namespace Texture::Construct diff --git a/src/texture/CornerStore.cpp b/src/texture/CornerStore.cpp new file mode 100644 index 00000000..59f0a906 --- /dev/null +++ b/src/texture/CornerStore.cpp @@ -0,0 +1,42 @@ +// This file is part of ClassicAPI. +// +// ClassicAPI is free software: you can redistribute it and/or modify it under the terms +// of the GNU General Public License as published by the Free Software Foundation, either +// version 3 of the License, or (at your option) any later version. +// +// ClassicAPI is distributed in the hope that it will be useful, but WITHOUT ANY +// WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR +// PURPOSE. See the GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License along with +// ClassicAPI. If not, see . + +#include "texture/CornerStore.h" + +#include "Game.h" +#include "Offsets.h" + +namespace Texture::CornerStore { + +namespace { + +AutoSubscribe *g_head = nullptr; + +// __thiscall(region, float rect[4]); dummy-EDX form. +using StoreCorners_t = void(__fastcall *)(void *region, void *edx, float *rect); +StoreCorners_t g_origStore = nullptr; + +void __fastcall StoreCorners_h(void *region, void *edx, float *rect) { + g_origStore(region, edx, rect); + for (const AutoSubscribe *s = g_head; s != nullptr; s = s->next) + s->fn(region); +} + +const Game::HookAutoRegister _hook{Offsets::FUN_REGION_STORE_CORNERS, reinterpret_cast(&StoreCorners_h), + reinterpret_cast(&g_origStore)}; + +} // namespace + +AutoSubscribe::AutoSubscribe(Listener fn) : fn(fn), next(g_head) { g_head = this; } + +} // namespace Texture::CornerStore diff --git a/src/texture/CornerStore.h b/src/texture/CornerStore.h new file mode 100644 index 00000000..afaa35c0 --- /dev/null +++ b/src/texture/CornerStore.h @@ -0,0 +1,36 @@ +// This file is part of ClassicAPI. +// +// ClassicAPI is free software: you can redistribute it and/or modify it under the terms +// of the GNU General Public License as published by the Free Software Foundation, either +// version 3 of the License, or (at your option) any later version. +// +// ClassicAPI is distributed in the hope that it will be useful, but WITHOUT ANY +// WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR +// PURPOSE. See the GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License along with +// ClassicAPI. If not, see . + +#pragma once + +// Shared observer for a texture's corner store (FUN_REGION_STORE_CORNERS), +// which writes the drawn quad from a rect: every layout of the texture +// (FUN_TEXTURE_ON_FRAME_SIZE_CHANGED), a coordinate change on a texture with +// SetTexCoordModifiesRect on, and the stores ClassicAPI makes itself. +// Listeners run after the corners are written. +// +// MinHook allows ONE hook per address and more than one feature follows the +// quad (Texture::Transform's rotation), so this module owns the hook and fans +// out. Declare a file-scope +// static const Texture::CornerStore::AutoSubscribe _sub{&OnCornersStored}; +namespace Texture::CornerStore { + +using Listener = void (*)(void *region); + +struct AutoSubscribe { + explicit AutoSubscribe(Listener fn); + Listener fn; + AutoSubscribe *next; +}; + +} // namespace Texture::CornerStore diff --git a/src/texture/Mask.cpp b/src/texture/Mask.cpp index 160cae9d..fff270a1 100644 --- a/src/texture/Mask.cpp +++ b/src/texture/Mask.cpp @@ -49,7 +49,7 @@ // co-hook on the indexed submit pops the matrices and the state journal after // the draw, exactly the op order of the engine's own disc draw. // -// A cold co-hook on the region ctor clears a region's mask state when its +// A Texture::Construct listener clears a region's mask state when its // pooled memory is reused, so a recycled region can never inherit (or serve) a // stale mask. Kill switch `_classicapi_TextureMaskEnable`; an SEH latch trips // it on a draw fault. @@ -57,6 +57,7 @@ #include "Game.h" #include "Offsets.h" #include "frame/AlphaGradient.h" +#include "texture/Construct.h" #include "texture/Load.h" #include "texture/Mask.h" #include "texture/Transform.h" @@ -437,37 +438,28 @@ static const Game::HookAutoRegister _primIdxHook{ Offsets::FUN_GX_PRIM_INDEXED, reinterpret_cast(&PrimIndexed_h), reinterpret_cast(&g_primIndexedOriginal)}; -// --- region ctor co-hook: drop a stale mask on pooled reuse ------------------ +// --- region construction: drop a stale mask on pooled reuse ------------------ -// __thiscall(mem, parent, layer, sublayer) -> region; dummy-EDX __fastcall. -using Ctor_t = void *(__fastcall *)(void *mem, void *edx, void *parent, int layer, int sublayer); -Ctor_t g_ctorOriginal = nullptr; - -void *__fastcall Ctor_h(void *mem, void *edx, void *parent, int layer, int sublayer) { - if (mem != nullptr) { - // A (re)constructed region owns no mask and is no longer a mask source. - ClearPathMask(mem); - g_baseMasks.erase(mem); - // If the recycled memory WAS a mask region, detach it from every base - // still holding it — otherwise the draw hook would read the recycled - // region as a mask (stale rect/texture at best, garbage at worst). - if (g_maskRegions.erase(mem) != 0 && !g_baseMasks.empty()) { - for (auto it = g_baseMasks.begin(); it != g_baseMasks.end();) { - auto &v = it->second; - v.erase(std::remove(v.begin(), v.end(), mem), v.end()); - if (v.empty()) - it = g_baseMasks.erase(it); - else - ++it; - } +void OnTextureConstructed(void *mem) { + // A (re)constructed region owns no mask and is no longer a mask source. + ClearPathMask(mem); + g_baseMasks.erase(mem); + // If the recycled memory WAS a mask region, detach it from every base + // still holding it — otherwise the draw hook would read the recycled + // region as a mask (stale rect/texture at best, garbage at worst). + if (g_maskRegions.erase(mem) != 0 && !g_baseMasks.empty()) { + for (auto it = g_baseMasks.begin(); it != g_baseMasks.end();) { + auto &v = it->second; + v.erase(std::remove(v.begin(), v.end(), mem), v.end()); + if (v.empty()) + it = g_baseMasks.erase(it); + else + ++it; } } - return g_ctorOriginal(mem, edx, parent, layer, sublayer); } -static const Game::HookAutoRegister _ctorHook{Offsets::FUN_SIMPLETEXTURE_CTOR, - reinterpret_cast(&Ctor_h), - reinterpret_cast(&g_ctorOriginal)}; +static const Texture::Construct::AutoSubscribe _constructSub{&OnTextureConstructed}; // --- Lua surface ------------------------------------------------------------- diff --git a/src/texture/Transform.cpp b/src/texture/Transform.cpp index a260e95b..bcd1ba9d 100644 --- a/src/texture/Transform.cpp +++ b/src/texture/Transform.cpp @@ -22,8 +22,9 @@ // `radians`, after the engine's loaders (Xml::TypeLoad). // // The engine re-derives axis-aligned corners from the layout rect on every -// resolve via FUN_REGION_STORE_CORNERS (0x007705B0). We co-hook that store and, -// for a transformed region, overwrite the just-written corners. So a static +// resolve via FUN_REGION_STORE_CORNERS (0x007705B0). We listen to that store +// (Texture::CornerStore) and, for a transformed region, overwrite the +// just-written corners. So a static // transform is written once and costs nothing per frame; a moving/resizing // region re-transforms automatically. // @@ -57,6 +58,7 @@ #include "texture/Transform.h" #include "animation/Region.h" +#include "texture/CornerStore.h" #include "xml/FrameLoad.h" #include "xml/TypeLoad.h" @@ -86,10 +88,6 @@ struct Xf { std::unordered_map g_xf; -// FUN_REGION_STORE_CORNERS — __thiscall(region, float rect[4]); dummy-EDX form. -using StoreCorners_t = void(__fastcall *)(void *region, void *edx, float *rect); -StoreCorners_t g_storeOriginal = nullptr; - // FUN_REGION_GET_RECT — __thiscall(region+OFF_REGION_ANCHOR, float out[4]) -> int. using GetRect_t = int(__fastcall *)(void *anchor, void *edx, float *outRect); // FUN_REGION_TEXCOORD_CROP — __thiscall(region, float rect[4]) (rect in/out). @@ -222,13 +220,12 @@ void UpdateAndApply(void *region, const Xf &xf) { ApplyFromRect(region, xf); // identity xf restores the plain quad } -// Co-hook on the corner store: the engine writes axis-aligned corners, then we +// After the corner store: the engine wrote axis-aligned corners, and we // overwrite them with the transformed ones for a tracked region. Reading the -// edges back from the corners the original just wrote keeps this +// edges back from the corners the store just wrote keeps this // transform-source-agnostic (SetTexCoord's cropped rect vs. the raw layout rect // both resolve here). -void __fastcall StoreCorners_h(void *region, void *edx, float *rect) { - g_storeOriginal(region, edx, rect); +void OnCornersStored(void *region) { if (g_xf.empty()) return; auto it = g_xf.find(region); @@ -564,30 +561,22 @@ int __fastcall Script_SetRotationFS(void *L) { // FontString attributes `degrees` and `radians` (4.3.4's loader; `radians` // wins when both are given). Each applies when present and non-empty, about // the default pivot. -bool AngleAttr(const void *node, const char *name, float &out) { - const char *value = Xml::Attr(node, name); - if (value == nullptr || *value == '\0') - return false; - out = reinterpret_cast(Offsets::FUN_SSTR_TO_FLOAT)(value); - return true; -} - constexpr float kRadiansPerDegree = 0.017453292f; void OnTextureNode(void *object, const void *node, void * /*status*/) { float degrees = 0.0f; - if (AngleAttr(node, "rotation", degrees)) + if (Xml::FloatAttr(node, "rotation", degrees)) SetTextureRotation(object, degrees * kRadiansPerDegree, nullptr, nullptr); } void OnFontStringNode(void *object, const void *node, void * /*status*/) { float angle = 0.0f; bool given = false; - if (AngleAttr(node, "degrees", angle)) { + if (Xml::FloatAttr(node, "degrees", angle)) { angle *= kRadiansPerDegree; given = true; } - if (AngleAttr(node, "radians", angle)) + if (Xml::FloatAttr(node, "radians", angle)) given = true; if (given) SetFontStringRotation(object, angle, nullptr, nullptr); @@ -740,9 +729,7 @@ void RegisterLuaFunctions() { } const Game::ModuleAutoRegister _autoreg{&RegisterLuaFunctions}; -const Game::HookAutoRegister _hookStoreCorners{ - Offsets::FUN_REGION_STORE_CORNERS, reinterpret_cast(&StoreCorners_h), - reinterpret_cast(&g_storeOriginal)}; +const Texture::CornerStore::AutoSubscribe _cornerSub{&OnCornersStored}; } // namespace diff --git a/src/xml/FrameLoad.cpp b/src/xml/FrameLoad.cpp index 7f2b172f..951be557 100644 --- a/src/xml/FrameLoad.cpp +++ b/src/xml/FrameLoad.cpp @@ -28,6 +28,7 @@ using GetAttr_t = const char *(__thiscall *)(const void *node, const char *name) using TemplateLookup_t = const void *(__fastcall *)(const char *name); using Hash_t = uint32_t(__stdcall *)(const char *s); using StatusAdd_t = void(__cdecl *)(void *status, int level, const char *fmt, ...); +using ToFloat_t = float(__stdcall *)(const char *s); } // namespace @@ -85,6 +86,14 @@ void LoadNode(void *object, const void *node, void *status) { Game::Read(vtable, Offsets::VTBL_XML_NODE_LOADER)(self, node, status); } +bool FloatAttr(const void *node, const char *name, float &out) { + const char *value = Attr(node, name); + if (value == nullptr || *value == '\0') + return false; + out = reinterpret_cast(Offsets::FUN_SSTR_TO_FLOAT)(value); + return true; +} + void Warn(void *status, const char *fmt, const char *a, const char *b, const char *c) { if (status == nullptr) return; diff --git a/src/xml/FrameLoad.h b/src/xml/FrameLoad.h index ba1e9b9d..af71f3c8 100644 --- a/src/xml/FrameLoad.h +++ b/src/xml/FrameLoad.h @@ -39,6 +39,9 @@ const char *Tag(const void *node); bool TagIs(const void *node, const char *tag); // case-insensitive, like the engine's // The engine's attribute getter; nullptr when the attribute is absent. const char *Attr(const void *node, const char *name); +// A number attribute through the engine's SStrToFloat (the frame loader's +// `alpha` conversion); false, `out` untouched, when absent or empty. +bool FloatAttr(const void *node, const char *name, float &out); // The template definition node `inherits=` resolves `name` to // (FUN_XML_TEMPLATE_LOOKUP); nullptr when no template has that name. const void *Template(const char *name); diff --git a/src/xml/NativeSetters.cpp b/src/xml/NativeSetters.cpp index 285746a4..4648d1b5 100644 --- a/src/xml/NativeSetters.cpp +++ b/src/xml/NativeSetters.cpp @@ -43,21 +43,12 @@ namespace Xml::NativeSetters { namespace { -using ToFloat_t = float(__stdcall *)(const char *s); using GetColor_t = void(__fastcall *)(void *region, void *edx, uint32_t *bgra); using SetColor_t = void(__fastcall *)(void *region, void *edx, const uint32_t *bgra); using SetFrameScale_t = bool(__fastcall *)(void *frame, void *edx, float scale, int force); using Tokenize_t = void(__stdcall *)(const char **cursor, char *out, unsigned outSize, const char *delims, int *outQuoted); -bool FloatAttr(const void *node, const char *name, float &out) { - const char *value = Xml::Attr(node, name); - if (value == nullptr || *value == '\0') - return false; - out = reinterpret_cast(Offsets::FUN_SSTR_TO_FLOAT)(value); - return true; -} - void OnRegionNode(void *object, const void *node, void * /*status*/) { float alpha = 0.0f; if (!FloatAttr(node, "alpha", alpha))