From 165ccf3e9c1dfd51d6c1803b7772ac6c26b6e77c Mon Sep 17 00:00:00 2001 From: Brues <5278969+brues-code@users.noreply.github.com> Date: Fri, 23 Oct 2026 01:15:38 -0500 Subject: [PATCH] refactor(lua): share the engine's flag readers as Game::Lua::ToFlag and Game::StringToFlag Twenty modules each declared their own typedef or wrapper for FUN_LUA_TO_BOOLEAN_LOOSE (4.3.4's StringToBOOL(L, index, default)) or FUN_XML_ATTR_TO_BOOL (StringToBOOL(const char *)). Route them all through one helper each. frame/Cooldown.cpp had a C++ copy of both that read unknown attribute text as true; the engine's reader, which 4.3.4's XML loader uses, reads it as false. SetReverse/SetDrawEdge now also truncate numbers like every other 1.12 flag method. --- src/Game.cpp | 12 ++++++ src/Game.h | 13 +++++++ src/Offsets.h | 6 ++- src/animation/Group.cpp | 8 +--- src/bindings/Api.cpp | 12 ++---- src/combatlog/History.cpp | 11 ++---- src/editbox/CountInvisibleLetters.cpp | 3 +- src/equipmentset/Action.cpp | 3 +- src/fontstring/IndentedWordWrap.cpp | 8 +--- src/fontstring/WordWrap.cpp | 3 +- src/frame/Attributes.cpp | 3 +- src/frame/Cooldown.cpp | 48 ++---------------------- src/frame/DontSavePosition.cpp | 5 +-- src/frame/HyperlinksEnabled.cpp | 3 +- src/frame/KeyboardPropagation.cpp | 5 +-- src/frame/MotionScriptsWhileDisabled.cpp | 6 +-- src/item/Count.cpp | 6 +-- src/spell/Tooltip.cpp | 6 +-- src/statusbar/Fill.cpp | 8 +--- src/target/Nearest.cpp | 5 +-- src/texture/Desaturation.cpp | 4 +- src/unit/Interact.cpp | 5 +-- src/xml/KeyValues.cpp | 3 +- 23 files changed, 66 insertions(+), 120 deletions(-) diff --git a/src/Game.cpp b/src/Game.cpp index 8dbbe2ce..ad0d12f7 100644 --- a/src/Game.cpp +++ b/src/Game.cpp @@ -40,6 +40,11 @@ double InternalToPixel(float internal) { return static_cast(internal) * div * Offsets::UI_COORD_SCALE_UNIT / mul; } +bool StringToFlag(const char *text) { + using StringToFlag_t = int(__fastcall *)(const char *text); + return reinterpret_cast(Offsets::FUN_XML_ATTR_TO_BOOL)(text) != 0; +} + namespace Lua { // Each entry binds a typed function pointer in `Game::Lua::` to the // corresponding raw VA in `Offsets`. The X-macro keeps the column-aligned @@ -131,6 +136,13 @@ void PushObject(void *L, void *object) { RawGetI(L, REGISTRY_INDEX, ObjectLuaRef(object)); } +bool ToFlag(void *L, int index, bool defaultValue) { + // Returns a full int in EAX on every path (Ghidra's `bool` is wrong). + using ToFlag_t = int(__fastcall *)(void *L, int index, int defaultValue); + return reinterpret_cast(Offsets::FUN_LUA_TO_BOOLEAN_LOOSE)(L, index, + defaultValue ? 1 : 0) != 0; +} + const char *ObjectDisplayName(void *object) { using GetName_t = const char *(__thiscall *)(void *); const void *vtable = Game::Read(object, 0); diff --git a/src/Game.h b/src/Game.h index c4829402..54d65736 100644 --- a/src/Game.h +++ b/src/Game.h @@ -104,6 +104,12 @@ using LoadScriptFunctions_t = void(__fastcall *)(); // registering its 109 glue functions. See FUN_LOAD_GLUE_SCRIPT_FUNCTIONS. using LoadGlueScriptFunctions_t = void(__stdcall *)(); +// The engine's text-to-flag reader (FUN_XML_ATTR_TO_BOOL, 4.3.4's +// `StringToBOOL(const char *)`), which XML boolean attributes go through: +// true for text starting 1-9/T/Y/t/y or equal to "on"/"enabled" (any case), +// false for anything else, including null. +bool StringToFlag(const char *text); + // --- API documentation descriptors ----------------------------------------- // // Every Lua registration (see the `Register*` functions in `Game::Lua`) can @@ -312,6 +318,13 @@ extern const lua_pushboolean_t PushBoolean; // engine's `lua_pushboolean` ABI wants `int` (any non-zero = true), so // this just funnels through the raw binding with a single conversion. inline void PushBool(void *L, bool b) { PushBoolean(L, static_cast(b)); } +// The engine's script-flag argument reader (FUN_LUA_TO_BOOLEAN_LOOSE, 4.3.4's +// `StringToBOOL(L, index, default)`), which every engine method taking an +// optional flag uses: nil is false, a boolean is itself, a number is its +// truncated int (so 0.5 is false), a string reads as StringToFlag except that +// text it does not recognize is `defaultValue`, and any other type is +// `defaultValue`. +bool ToFlag(void *L, int index, bool defaultValue = false); extern const lua_pushstring_t PushString; extern const lua_pushlstring_t PushLString; extern const lua_pushvalue_t PushValue; diff --git a/src/Offsets.h b/src/Offsets.h index 6f68cf9a..ab9c1247 100644 --- a/src/Offsets.h +++ b/src/Offsets.h @@ -11225,7 +11225,7 @@ enum Offsets { GXSHADER_TYPE_PIXEL = 1, GXSHADER_CODE_D3D = 2, GXRS_PIXEL_SHADER = 0x3F, // pointer-form selector (0x40 = vertex shader); doc only — the engine's draw binds it - FUN_LUA_TO_BOOLEAN_LOOSE = 0x006F1C10, // __fastcall(L, idx, default): nil→0, boolean, number→int, "1/t/y…"→1, "0/f/n…"→0, "enabled"→1, "disabled"→0, other→default + FUN_LUA_TO_BOOLEAN_LOOSE = 0x006F1C10, // __fastcall(L, idx, default): nil→0, boolean, number→int, "1/t/y…"→1, "0/f/n…"→0, "enabled"→1, "disabled"→0, other→default. Called through Game::Lua::ToFlag (4.3.4 StringToBOOL(L, idx, default)) // --- Texture memory accounting: the zero-dimension hang ------------------- // FUN_GX_TEXTURE_MIP_BYTES totals a texture's bytes across its mip chain: @@ -11896,7 +11896,9 @@ enum Offsets { // (0x00779FC6), then reads its own attributes: "letters" (0x0077A046), // "autoFocus" (0x0077A0A5), "multiLine", "numeric", "historyLines". // Booleans go through FUN_XML_ATTR_TO_BOOL - `int __fastcall(const char *)`, - // 1 for text starting 1/t/y - as for autoFocus at 0x0077A0BC. + // 4.3.4's StringToBOOL(const char *): 1 for text starting 1-9/T/Y or equal + // to on/enabled, else 0 (null too) - as for autoFocus at 0x0077A0BC. + // Called through Game::StringToFlag. FUN_XML_EDITBOX_LOAD = 0x00779FB0, FUN_XML_ATTR_TO_BOOL = 0x006F1B30, // Selection-highlight regions — three consecutive CSimpleTexture pointers diff --git a/src/animation/Group.cpp b/src/animation/Group.cpp index ed858ec7..cc739566 100644 --- a/src/animation/Group.cpp +++ b/src/animation/Group.cpp @@ -471,14 +471,10 @@ namespace { Group *Self(void *L) { return static_cast(Resolve(L, 1, Group::s_class)); } -using LooseBool_t = int(__fastcall *)(void *L, int idx, int defaultValue); -bool LooseBool(void *L, int idx, int dflt) { - return reinterpret_cast(Offsets::FUN_LUA_TO_BOOLEAN_LOOSE)(L, idx, dflt) != 0; -} int __fastcall Script_Play(void *L) { if (Group *g = Self(L)) - g->Play(LooseBool(L, 2, 0)); + g->Play(Game::Lua::ToFlag(L, 2)); return 0; } int __fastcall Script_Pause(void *L) { @@ -620,7 +616,7 @@ int __fastcall Script_SetIgnoreFramerateThrottle(void *L) { Group *g = Self(L); if (g == nullptr) return 0; - if (LooseBool(L, 2, 0)) + if (Game::Lua::ToFlag(L, 2)) g->flags |= GROUP_IGNORE_THROTTLE; else g->flags &= ~GROUP_IGNORE_THROTTLE; diff --git a/src/bindings/Api.cpp b/src/bindings/Api.cpp index 41ffd053..a977b837 100644 --- a/src/bindings/Api.cpp +++ b/src/bindings/Api.cpp @@ -63,7 +63,6 @@ using KeyDispatch_t = int(__fastcall *)(void *mgr, void *edx, const char *key, i using CommandExecute_t = int(__fastcall *)(void *mgr, void *edx, const char *cmd, int isDown); using GetCommandAction_t = const char *(__thiscall *)(void *mgr, const char *key); using ScriptFn_t = int(__fastcall *)(void *L); -using ToBool_t = int(__fastcall *)(void *L, int idx, int dflt); KeyDispatch_t g_keyDispatchOriginal = nullptr; CommandExecute_t g_commandExecuteOriginal = nullptr; @@ -260,9 +259,6 @@ int __fastcall CommandExecute_h(void *mgr, void *edx, const char *command, int i // --- Lua ---------------------------------------------------------------------- -bool LooseBool(void *L, int idx) { - return reinterpret_cast(Offsets::FUN_LUA_TO_BOOLEAN_LOOSE)(L, idx, 0) != 0; -} const char *OptString(void *L, int idx) { return Game::Lua::IsString(L, idx) ? Game::Lua::ToString(L, idx) : nullptr; @@ -321,7 +317,7 @@ int __fastcall Script_SetOverrideBinding(void *L) { Game::Lua::Error(L, "Usage: SetOverrideBinding(owner, isPriority, \"KEY\"[, \"COMMAND\"])"); return 0; } - SetOverrideBinding(owner, LooseBool(L, 2), Game::Lua::ToString(L, 3), OptString(L, 4)); + SetOverrideBinding(owner, Game::Lua::ToFlag(L, 2), Game::Lua::ToString(L, 3), OptString(L, 4)); return 0; } @@ -332,7 +328,7 @@ int SetOverrideCommand(void *L, const char *prefix, const char *usage) { return 0; } const std::string command = std::string(prefix) + Game::Lua::ToString(L, 4); - SetOverrideBinding(owner, LooseBool(L, 2), Game::Lua::ToString(L, 3), command.c_str()); + SetOverrideBinding(owner, Game::Lua::ToFlag(L, 2), Game::Lua::ToString(L, 3), command.c_str()); return 0; } @@ -358,7 +354,7 @@ int __fastcall Script_SetOverrideBindingClick(void *L) { return 0; } const std::string command = ClickCommand(Game::Lua::ToString(L, 4), OptString(L, 5)); - SetOverrideBinding(owner, LooseBool(L, 2), Game::Lua::ToString(L, 3), command.c_str()); + SetOverrideBinding(owner, Game::Lua::ToFlag(L, 2), Game::Lua::ToString(L, 3), command.c_str()); return 0; } @@ -376,7 +372,7 @@ int __fastcall Script_ClearOverrideBindings(void *L) { // GetBindingAction(key [, checkOverride]): 4.3.4's GetCommandAction checks the // override first when asked; the saved binding is the engine's own answer. int __fastcall Script_GetBindingAction(void *L) { - if (Game::Lua::IsString(L, 1) && LooseBool(L, 2)) { + if (Game::Lua::IsString(L, 1) && Game::Lua::ToFlag(L, 2)) { if (const char *command = ActiveOverride(Game::Lua::ToString(L, 1))) { Game::Lua::PushString(L, command); return 1; diff --git a/src/combatlog/History.cpp b/src/combatlog/History.cpp index 7402782e..b57fc8b0 100644 --- a/src/combatlog/History.cpp +++ b/src/combatlog/History.cpp @@ -82,17 +82,12 @@ constexpr int kMaxRetention = 0x7FFFFFFF / 1000; using SStrCmpI_t = int(__stdcall *)(const char *a, const char *b, int n); using Tokenize_t = void(__stdcall *)(char **cursor, char *out, unsigned outSize, const char *delims, int *outQuoted); -using LooseBool_t = int(__fastcall *)(void *L, int idx, int dflt); int StrCmpI(const char *a, const char *b) { return reinterpret_cast(static_cast(Offsets::FUN_SSTR_CMP_I))( a, b, kStormMaxStr); } -bool ArgFlag(void *L, int idx) { - return reinterpret_cast( - static_cast(Offsets::FUN_LUA_TO_BOOLEAN_LOOSE))(L, idx, 0) != 0; -} bool Complete(uint32_t matched) { return (matched & AFFILIATION_MASK) && (matched & REACTION_MASK) && (matched & CONTROL_MASK) && @@ -269,7 +264,7 @@ int __fastcall Script_CombatLogSetRetentionTime(void *L) { } int __fastcall Script_CombatLogGetNumEntries(void *L) { - const bool all = ArgFlag(L, 1); + const bool all = Game::Lua::ToFlag(L, 1); int count = 0; for (const Entry &e : g_entries) { if (Matches(e, all)) @@ -286,7 +281,7 @@ int __fastcall Script_CombatLogSetCurrentEntry(void *L) { } g_current = -1; int index = static_cast(Game::Lua::ToNumber(L, 1)); - const bool all = ArgFlag(L, 2); + const bool all = Game::Lua::ToFlag(L, 2); if (index <= 0) { // 0 is the newest entry, -1 the one before it. for (int i = Count() - 1; i >= 0; --i) { @@ -320,7 +315,7 @@ int __fastcall Script_CombatLogAdvanceEntry(void *L) { return 0; } int count = static_cast(Game::Lua::ToNumber(L, 1)); - const bool all = ArgFlag(L, 2); + const bool all = Game::Lua::ToFlag(L, 2); if (count != 0) { // The walk starts at the current entry, which counts when it matches. int result = -1; diff --git a/src/editbox/CountInvisibleLetters.cpp b/src/editbox/CountInvisibleLetters.cpp index 56f9d41a..d5ed2367 100644 --- a/src/editbox/CountInvisibleLetters.cpp +++ b/src/editbox/CountInvisibleLetters.cpp @@ -86,7 +86,6 @@ const Game::HookAutoRegister _countHook{Offsets::FUN_EDITBOX_COUNT_CHARS, reinte using Load_t = uintptr_t(__thiscall *)(void *self, const void *node, void *status); using GetAttr_t = const char *(__thiscall *)(const void *node, const char *name); -using AttrToBool_t = int(__fastcall *)(const char *text); Load_t g_origEditBoxLoad = nullptr; // `self` is the loader sub-object inside the edit box. @@ -99,7 +98,7 @@ uintptr_t __fastcall EditBoxLoad_h(void *self, void * /*edx*/, const void *node, if (value == nullptr || *value == '\0') return result; void *editBox = static_cast(self) - Offsets::OFF_XML_LOADER_SUBOBJECT; - if (reinterpret_cast(Offsets::FUN_XML_ATTR_TO_BOOL)(value)) + if (Game::StringToFlag(value)) g_counting.insert(editBox); else g_counting.erase(editBox); diff --git a/src/equipmentset/Action.cpp b/src/equipmentset/Action.cpp index de121246..6e0e48af 100644 --- a/src/equipmentset/Action.cpp +++ b/src/equipmentset/Action.cpp @@ -76,7 +76,6 @@ using Notify_t = void(__fastcall *)(uint32_t slot0, int sendToServer, int quiet) using Usable_t = uint32_t(__fastcall *)(uint32_t slot0, int *outNoMana); using Texture_t = const char *(__fastcall *)(uint32_t slot0); using Script_t = int(__fastcall *)(void *L); -using LooseBool_t = int(__fastcall *)(void *L, int idx, int defaultValue); // `__stdcall(a, packet)`, RET 8 — declared `__fastcall` with the two // register slots passed through untouched, which is the same frame. using ActionButtons_t = int(__fastcall *)(void *ecx, void *edx, void *a, void *packet); @@ -193,7 +192,7 @@ int __fastcall UseAction_h(void *L) { if (slot < 0 || slot >= static_cast(kSlots)) return g_origUseAction(L); const uint32_t slot0 = static_cast(slot); - if (reinterpret_cast(Offsets::FUN_LUA_TO_BOOLEAN_LOOSE)(L, 2, 0) != 0) { + if (Game::Lua::ToFlag(L, 2)) { if (const uint32_t id = CursorSetID()) { PlaceSet(slot0, id); return 0; diff --git a/src/fontstring/IndentedWordWrap.cpp b/src/fontstring/IndentedWordWrap.cpp index 3cae5c58..9907b099 100644 --- a/src/fontstring/IndentedWordWrap.cpp +++ b/src/fontstring/IndentedWordWrap.cpp @@ -34,16 +34,12 @@ namespace FontString::IndentedWordWrap { namespace { -using LooseBool_t = int(__fastcall *)(void *L, int idx, int defaultValue); using SetStyleFlags_t = void(__fastcall *)(void *fontString, void *edx, uint32_t flags); using UpdateObjects_t = void(__fastcall *)(void *font); using ReadTextType_t = int(__fastcall *)(void *L); constexpr uint32_t kIndented = Offsets::FONT_STYLE_INDENTED_WORD_WRAP; -bool ReadIndent(void *L) { - return reinterpret_cast(Offsets::FUN_LUA_TO_BOOLEAN_LOOSE)(L, 2, 1) != 0; -} int PushIndented(void *L, uint32_t styleFlags) { if (styleFlags & kIndented) @@ -93,7 +89,7 @@ void *HtmlFont(void *L) { template int __fastcall Script_SetFontIndented(void *L) { void *font = Font(L); if (font != nullptr) - SetFontIndented(font, ReadIndent(L)); + SetFontIndented(font, Game::Lua::ToFlag(L, 2, true)); return 0; } @@ -115,7 +111,7 @@ int __fastcall Script_SetFontStringIndented(void *L) { void *fontString = Game::Lua::ResolveFontString(L); if (fontString == nullptr) return 0; - const bool indented = ReadIndent(L); + const bool indented = Game::Lua::ToFlag(L, 2, true); Game::Ref(fontString, Offsets::OFF_FONTSTRING_SETTABLE_STYLE) &= ~kIndented; const uint32_t flags = Game::Read(fontString, Offsets::OFF_FONTSTRING_FLAGS); reinterpret_cast(Offsets::FUN_FONTSTRING_SET_STYLE_FLAGS)( diff --git a/src/fontstring/WordWrap.cpp b/src/fontstring/WordWrap.cpp index 50c37ac2..6f2ab390 100644 --- a/src/fontstring/WordWrap.cpp +++ b/src/fontstring/WordWrap.cpp @@ -32,7 +32,6 @@ namespace FontString::WordWrap { namespace { -using LooseBool_t = int(__fastcall *)(void *L, int idx, int defaultValue); constexpr uint32_t kNoWrap = Offsets::FONT_STYLE_NO_WORD_WRAP; @@ -40,7 +39,7 @@ int __fastcall Script_SetWordWrap(void *L) { void *fontString = Game::Lua::ResolveFontString(L); if (fontString == nullptr) return 0; - const bool wrap = reinterpret_cast(Offsets::FUN_LUA_TO_BOOLEAN_LOOSE)(L, 2, 1) != 0; + const bool wrap = Game::Lua::ToFlag(L, 2, true); Game::Ref(fontString, Offsets::OFF_FONTSTRING_SETTABLE_STYLE) &= ~kNoWrap; const uint32_t flags = Game::Read(fontString, Offsets::OFF_FONTSTRING_FLAGS); const uint32_t wanted = wrap ? flags & ~kNoWrap : flags | kNoWrap; diff --git a/src/frame/Attributes.cpp b/src/frame/Attributes.cpp index 2e393c9a..a60abbd6 100644 --- a/src/frame/Attributes.cpp +++ b/src/frame/Attributes.cpp @@ -1100,7 +1100,6 @@ int __fastcall Script_GetAttribute(void *L) { // inherited templates are loaded first, so the frame's own values win. // Problems are the engine's load warnings, with 4.3.4's texts. void LoadXmlAttributes(void *L, void *frame, const void *attributes, void *status) { - using ToBool_t = int(__fastcall *)(const char *); using ToFloat_t = float(__stdcall *)(const char *); const int top = Game::Lua::GetTop(L); @@ -1138,7 +1137,7 @@ void LoadXmlAttributes(void *L, void *frame, const void *attributes, void *statu if (isNil) Game::Lua::PushNil(L); else if (Ascii::EqualCI(type, "boolean")) - Game::Lua::PushBool(L, reinterpret_cast(Offsets::FUN_XML_ATTR_TO_BOOL)(value) != 0); + Game::Lua::PushBool(L, Game::StringToFlag(value)); else if (Ascii::EqualCI(type, "number")) Game::Lua::PushNumber(L, reinterpret_cast(Offsets::FUN_SSTR_TO_FLOAT)(value)); else diff --git a/src/frame/Cooldown.cpp b/src/frame/Cooldown.cpp index e09a7e3a..2dc15ee4 100644 --- a/src/frame/Cooldown.cpp +++ b/src/frame/Cooldown.cpp @@ -655,8 +655,6 @@ void __fastcall OnFrameSizeChanged(void *layout, void *, const float *oldRect) { ResetVerts(FrameOfLayout(layout)); } -bool StringToBool(const char *s); - // The stock CooldownFrameTemplate: a Model template (Cooldown.xml, 1.12). bool IsStockTemplate(const void *node) { if (!Xml::TagIs(node, "Model")) @@ -691,9 +689,9 @@ uintptr_t __fastcall LoadXML(void *layout, void *, const void *node, void *statu return result; Fields &f = F(FrameOfLayout(layout)); if (const char *reverse = Xml::Attr(node, "reverse")) - f.reverse = StringToBool(reverse); + f.reverse = Game::StringToFlag(reverse); if (const char *drawEdge = Xml::Attr(node, "drawEdge")) - f.drawEdge = StringToBool(drawEdge); + f.drawEdge = Game::StringToFlag(drawEdge); return result; } @@ -743,44 +741,6 @@ void *__fastcall CreateCooldown(void *parent) { void *ResolveCooldown(void *L) { return Game::Lua::ResolveTypedObject(L, 1, TypeIdVar()); } -// 4.3.4's StringToBOOL on a string (FrameXML/LoadXML.cpp 0x004EF0C0): the -// first letter (0/F/N false, 1-9/T/Y true), then off/disabled/on/enabled, -// and true for anything else. -bool StringToBool(const char *s) { - switch (*s) { - case '0': case 'F': case 'N': case 'f': case 'n': - return false; - case '1': case '2': case '3': case '4': case '5': - case '6': case '7': case '8': case '9': - case 'T': case 'Y': case 't': case 'y': - return true; - default: - break; - } - if (_stricmp(s, "off") == 0 || _stricmp(s, "disabled") == 0) - return false; - return true; -} - -// StringToBOOL(L, index, 1): nil is false, a boolean is itself, a number is -// != 0, a string as above, and anything else the default. -bool ArgToBool(void *L, int index) { - switch (Game::Lua::Type(L, index)) { - case Game::Lua::TYPE_NIL: - return false; - case Game::Lua::TYPE_BOOLEAN: - return Game::Lua::ToBoolean(L, index) != 0; - case Game::Lua::TYPE_NUMBER: - return Game::Lua::ToNumber(L, index) != 0.0; - case Game::Lua::TYPE_STRING: { - const char *s = Game::Lua::ToString(L, index); - return s == nullptr || StringToBool(s); - } - default: - return true; - } -} - // OFFSET (4.3.4): 0x009FFBD0 int __fastcall Script_SetCooldown(void *L) { void *frame = ResolveCooldown(L); @@ -802,7 +762,7 @@ int __fastcall Script_SetReverse(void *L) { void *frame = ResolveCooldown(L); if (frame == nullptr) return 0; - F(frame).reverse = ArgToBool(L, 2); + F(frame).reverse = Game::Lua::ToFlag(L, 2, true); return 0; } @@ -821,7 +781,7 @@ int __fastcall Script_SetDrawEdge(void *L) { if (frame == nullptr) return 0; Fields &f = F(frame); - const uint32_t drawEdge = ArgToBool(L, 2); + const uint32_t drawEdge = Game::Lua::ToFlag(L, 2, true); if (drawEdge != f.drawEdge) { f.drawEdge = drawEdge; NotifyLayerChanged(frame, 3); diff --git a/src/frame/DontSavePosition.cpp b/src/frame/DontSavePosition.cpp index d315917c..effdb481 100644 --- a/src/frame/DontSavePosition.cpp +++ b/src/frame/DontSavePosition.cpp @@ -36,7 +36,6 @@ namespace { using SaveFrame_t = void(__fastcall *)(void *frame, void *file); using SetFrameFlag_t = void(__fastcall *)(void *frame, void *edx, uint32_t mask, int on); -using LooseBool_t = int(__fastcall *)(void *L, int idx, int defaultValue); SaveFrame_t g_origSaveFrame = nullptr; @@ -58,9 +57,9 @@ int __fastcall Script_SetDontSavePosition(void *L) { void *frame = Game::Lua::ResolveFrame(L); if (frame == nullptr) return 0; - const int on = reinterpret_cast(Offsets::FUN_LUA_TO_BOOLEAN_LOOSE)(L, 2, 1); + const bool on = Game::Lua::ToFlag(L, 2, true); reinterpret_cast(Offsets::FUN_FRAME_SET_FRAME_FLAG)( - frame, nullptr, Offsets::FRAME_FLAG_DONT_SAVE_POSITION, on != 0); + frame, nullptr, Offsets::FRAME_FLAG_DONT_SAVE_POSITION, on); return 0; } diff --git a/src/frame/HyperlinksEnabled.cpp b/src/frame/HyperlinksEnabled.cpp index be81174b..0adb29a2 100644 --- a/src/frame/HyperlinksEnabled.cpp +++ b/src/frame/HyperlinksEnabled.cpp @@ -56,7 +56,6 @@ namespace { // other things. std::unordered_set g_disabled; -using LooseBool_t = int(__fastcall *)(void *L, int idx, int defaultValue); bool OnRun(void *frame, uint32_t *slotPtr, const char * /*fmt*/, void * /*varargs*/) { if (g_disabled.empty() || g_disabled.count(frame) == 0) @@ -71,7 +70,7 @@ int __fastcall Script_SetHyperlinksEnabled(void *L) { void *frame = Game::Lua::ResolveFrame(L); if (frame == nullptr) return 0; - if (reinterpret_cast(Offsets::FUN_LUA_TO_BOOLEAN_LOOSE)(L, 2, 1) != 0) + if (Game::Lua::ToFlag(L, 2, true)) g_disabled.erase(frame); else g_disabled.insert(frame); diff --git a/src/frame/KeyboardPropagation.cpp b/src/frame/KeyboardPropagation.cpp index 6f05b2a0..43047d96 100644 --- a/src/frame/KeyboardPropagation.cpp +++ b/src/frame/KeyboardPropagation.cpp @@ -47,7 +47,6 @@ namespace { using KeySlot_t = int(__fastcall *)(void *frame, void *edx, void *event); using SetFrameFlag_t = void(__fastcall *)(void *frame, void *edx, uint32_t mask, int on); -using LooseBool_t = int(__fastcall *)(void *L, int idx, int defaultValue); KeySlot_t g_origFrameOnChar = nullptr; KeySlot_t g_origFrameOnKeyDown = nullptr; @@ -86,9 +85,9 @@ int __fastcall Script_SetPropagateKeyboardInput(void *L) { void *frame = Game::Lua::ResolveFrame(L); if (frame == nullptr) return 0; - const int on = reinterpret_cast(Offsets::FUN_LUA_TO_BOOLEAN_LOOSE)(L, 2, 0); + const bool on = Game::Lua::ToFlag(L, 2); reinterpret_cast(Offsets::FUN_FRAME_SET_FRAME_FLAG)( - frame, nullptr, Offsets::FRAME_FLAG_PROPAGATE_KEYBOARD, on != 0); + frame, nullptr, Offsets::FRAME_FLAG_PROPAGATE_KEYBOARD, on); return 0; } diff --git a/src/frame/MotionScriptsWhileDisabled.cpp b/src/frame/MotionScriptsWhileDisabled.cpp index 71ce2dc6..8fc488be 100644 --- a/src/frame/MotionScriptsWhileDisabled.cpp +++ b/src/frame/MotionScriptsWhileDisabled.cpp @@ -48,8 +48,6 @@ namespace { using OnEnter_t = void(__fastcall *)(void *button, void *edx); using OnLeave_t = void(__fastcall *)(void *button, void *edx, int removed); using FireScript_t = void(__fastcall *)(void *frame, void *edx, int *slot); -using LooseBool_t = int(__fastcall *)(void *L, int idx, int defaultValue); -using AttrToBool_t = int(__fastcall *)(const char *text); std::unordered_set g_enabled; @@ -99,7 +97,7 @@ void OnFrameNode(void *object, const void *node, void * /*status*/) { const char *value = Xml::Attr(node, "motionScriptsWhileDisabled"); if (value == nullptr || *value == '\0' || !Game::Lua::IsA(object, Offsets::VAR_BUTTON_LUA_TYPE_ID)) return; - Set(object, reinterpret_cast(Offsets::FUN_XML_ATTR_TO_BOOL)(value) != 0); + Set(object, Game::StringToFlag(value)); } const Xml::FrameLoad::AutoSubscribe _xmlSub{&OnFrameNode}; @@ -115,7 +113,7 @@ int __fastcall Script_SetMotionScriptsWhileDisabled(void *L) { void *button = ResolveButton(L); if (button == nullptr) return 0; - Set(button, reinterpret_cast(Offsets::FUN_LUA_TO_BOOLEAN_LOOSE)(L, 2, 0) != 0); + Set(button, Game::Lua::ToFlag(L, 2)); return 0; } diff --git a/src/item/Count.cpp b/src/item/Count.cpp index 282cf889..bfb1a862 100644 --- a/src/item/Count.cpp +++ b/src/item/Count.cpp @@ -46,7 +46,6 @@ namespace Item::Count { namespace { using CountByID_t = int(__thiscall *)(const void *invMgr, int itemID, uint32_t flags); -using LooseBool_t = int(__fastcall *)(void *L, int idx, int defaultValue); int CountByID(uintptr_t fn, int itemID, uint32_t flags) { const uint8_t *invMgr = Unit::Identity::PlayerInventoryManager(); @@ -86,9 +85,8 @@ int CountUses(int itemID, int total) { } int __fastcall Script_C_Item_GetItemCount(void *L) { - const auto looseBool = reinterpret_cast(Offsets::FUN_LUA_TO_BOOLEAN_LOOSE); - const bool includeBank = looseBool(L, 2, 0) != 0; - const bool includeUses = looseBool(L, 3, 0) != 0; + const bool includeBank = Game::Lua::ToFlag(L, 2); + const bool includeUses = Game::Lua::ToFlag(L, 3); const uint32_t flags = includeBank ? Offsets::INVMGR_SEARCH_BANK : 0; const Item::Arg::Resolved arg = Item::Arg::Resolve(L, 1); diff --git a/src/spell/Tooltip.cpp b/src/spell/Tooltip.cpp index 645ba974..1a5d9a06 100644 --- a/src/spell/Tooltip.cpp +++ b/src/spell/Tooltip.cpp @@ -75,7 +75,6 @@ void RewriteAppendHeader(void *tooltip, int headerLine, int spellID, bool showSu *numLines = after; } -using LooseBool_t = int(__fastcall *)(void *L, int idx, int defaultValue); // 4.3.4's Script_SetSpellByID / Script_AddSpellByID (Tooltip_Scripts.cpp // 0x004733C0 / 0x00473530), which differ only in `append`: (spellID @@ -93,9 +92,8 @@ int SetOrAddSpellByID(void *L, bool append) { Game::Lua::ObjectDisplayName(tooltip)); return 0; } - const auto looseBool = reinterpret_cast(Offsets::FUN_LUA_TO_BOOLEAN_LOOSE); - const bool isPet = looseBool(L, 3, 0) != 0; - const bool showSubtext = looseBool(L, 4, 0) != 0; + const bool isPet = Game::Lua::ToFlag(L, 3); + const bool showSubtext = Game::Lua::ToFlag(L, 4); const int before = Game::Read(tooltip, Offsets::OFF_GAMETOOLTIP_NUM_LINES); const uint32_t result = BuildSpell(tooltip, spellID, CooldownRemainingMs(spellID, isPet), diff --git a/src/statusbar/Fill.cpp b/src/statusbar/Fill.cpp index 150ef0d1..d58f23fa 100644 --- a/src/statusbar/Fill.cpp +++ b/src/statusbar/Fill.cpp @@ -264,17 +264,13 @@ const State *Find(void *bar) { void *ResolveBar(void *L) { return Game::Lua::ResolveTypedObject(L, 1, Offsets::VAR_STATUSBAR_LUA_TYPE_ID); } -using LooseBool_t = int(__fastcall *)(void *L, int idx, int defaultValue); -bool ArgBool(void *L, int idx) { - return reinterpret_cast(Offsets::FUN_LUA_TO_BOOLEAN_LOOSE)(L, idx, 0) != 0; -} int __fastcall Script_SetReverseFill(void *L) { void *bar = ResolveBar(L); if (bar == nullptr) return 0; - const bool reverse = ArgBool(L, 2); + const bool reverse = Game::Lua::ToFlag(L, 2); Modify(bar, [reverse](State &s) { s.reverseFill = reverse; }); return 0; } @@ -292,7 +288,7 @@ int __fastcall Script_SetRotatesTexture(void *L) { void *bar = ResolveBar(L); if (bar == nullptr) return 0; - const bool rotates = ArgBool(L, 2); + const bool rotates = Game::Lua::ToFlag(L, 2); Modify(bar, [rotates](State &s) { s.rotatesTexture = rotates; }); // 4.3.4 sets the full coordinates at once, so a bar with no value yet // shows its image turned, or not, before the first layout. Full diff --git a/src/target/Nearest.cpp b/src/target/Nearest.cpp index d021d698..acb362cb 100644 --- a/src/target/Nearest.cpp +++ b/src/target/Nearest.cpp @@ -67,7 +67,6 @@ namespace { using Cycle_t = void(__fastcall *)(int reverse, int mode); using Valid_t = int(__fastcall *)(void *player, void *cand, int mode); -using ToBool_t = int(__fastcall *)(void *L, int idx, int dflt); using SetSelection_t = void(__stdcall *)(uint32_t guidLo, uint32_t guidHi); using EnumCallback_t = int(__fastcall *)(void *ctx, void *unusedEdx, uint64_t guid); using EnumVisibleObjects_t = int(__fastcall *)(EnumCallback_t cb, void *ctx); @@ -110,9 +109,7 @@ const Game::HookAutoRegister _validHook{Offsets::FUN_TARGET_CANDIDATE_VALID, reinterpret_cast(&g_validOriginal)}; void Cycle(void *L, int mode) { - const int reverse = - reinterpret_cast(Offsets::FUN_LUA_TO_BOOLEAN_LOOSE)(L, 1, 0); - reinterpret_cast(Offsets::FUN_TARGET_NEAREST_CYCLE)(reverse, mode); + reinterpret_cast(Offsets::FUN_TARGET_NEAREST_CYCLE)(Game::Lua::ToFlag(L, 1), mode); } void SetSelection(uint64_t guid) { diff --git a/src/texture/Desaturation.cpp b/src/texture/Desaturation.cpp index d91f5eee..4a47c127 100644 --- a/src/texture/Desaturation.cpp +++ b/src/texture/Desaturation.cpp @@ -66,7 +66,6 @@ using ShaderCreate_t = void(__fastcall *)(int type, void **outSlot, const char * using ArrayGrow_t = void(__thiscall *)(void *desc, uint32_t newCap); using CompilePs_t = void(__thiscall *)(void *device, void *shader); using SetShader_t = void(__thiscall *)(void *region, void *shader); -using LooseBool_t = int(__fastcall *)(void *L, int idx, int defaultValue); using ScriptFn_t = int(__fastcall *)(void *L); using GetUiPixelShader_t = void *(__fastcall *)(int index); @@ -380,8 +379,7 @@ int __fastcall Script_SetDesaturated(void *L) { Game::Lua::PushNil(L); return 1; } - const int flag = reinterpret_cast(Offsets::FUN_LUA_TO_BOOLEAN_LOOSE)(L, 2, 1); - if (!Apply(region, flag != 0 ? kFull : 0)) { + if (!Apply(region, Game::Lua::ToFlag(L, 2, true) ? kFull : 0)) { Game::Lua::PushNil(L); return 1; } diff --git a/src/unit/Interact.cpp b/src/unit/Interact.cpp index d3b21b3c..40626c85 100644 --- a/src/unit/Interact.cpp +++ b/src/unit/Interact.cpp @@ -57,15 +57,14 @@ namespace { using OnSpriteRightClick_t = int(__stdcall *)(uint32_t guidLo, uint32_t guidHi); using TokenToGuid_t = uint64_t(__fastcall *)(const char *token); -using ToBool_t = int(__fastcall *)(void *L, int idx, int dflt); uint64_t ResolveGuid(void *L, const char *token) { if (_stricmp(token, "mouseover") == 0) return Game::Read(Offsets::VAR_MOUSEOVER_GUID_LO); if (Unit::TokenResolve::IsUnitToken(token)) return reinterpret_cast(Offsets::FUN_TOKEN_TO_GUID)(token); - const int exact = reinterpret_cast(Offsets::FUN_LUA_TO_BOOLEAN_LOOSE)(L, 2, 0); - return Unit::Identity::FindGuidByName(token, Offsets::TYPEMASK_OBJECT, exact != 0); + return Unit::Identity::FindGuidByName(token, Offsets::TYPEMASK_OBJECT, + Game::Lua::ToFlag(L, 2)); } bool Interact(void *L) { diff --git a/src/xml/KeyValues.cpp b/src/xml/KeyValues.cpp index 17819efa..db40481a 100644 --- a/src/xml/KeyValues.cpp +++ b/src/xml/KeyValues.cpp @@ -64,11 +64,10 @@ Kind KindOf(const char *type) { } void Push(void *L, Kind kind, const char *text) { - using ToBool_t = int(__fastcall *)(const char *); using ToFloat_t = float(__stdcall *)(const char *); switch (kind) { case Kind::Boolean: - Game::Lua::PushBool(L, reinterpret_cast(Offsets::FUN_XML_ATTR_TO_BOOL)(text) != 0); + Game::Lua::PushBool(L, Game::StringToFlag(text)); break; case Kind::Number: Game::Lua::PushNumber(L, reinterpret_cast(Offsets::FUN_SSTR_TO_FLOAT)(text));