From ef7ccb472344cd9e6e9497fcee61ba8b8604a597 Mon Sep 17 00:00:00 2001 From: Brues <5278969+brues-code@users.noreply.github.com> Date: Thu, 14 May 2026 14:05:27 -0500 Subject: [PATCH] Lua SetField cleanup --- src/Game.cpp | 18 +++++++++++ src/Game.h | 9 ++++++ src/aura/Api.cpp | 16 +++------- src/aura/Data.cpp | 61 +++++++++++++----------------------- src/charlist/Cache.cpp | 30 +++++------------- src/gossip/Info.cpp | 41 ++++++++---------------- src/item/GetItemLocation.cpp | 13 +++----- src/time/DateAndTime.cpp | 18 ++++------- src/ui/Color.cpp | 20 +++--------- 9 files changed, 89 insertions(+), 137 deletions(-) diff --git a/src/Game.cpp b/src/Game.cpp index d5f8de5e..32f5e16d 100644 --- a/src/Game.cpp +++ b/src/Game.cpp @@ -101,6 +101,24 @@ void RegisterTableFunction(const char *tableName, const char *methodName, CFunct SetTable(L, -3); // pops methodName+closure: tbl[m]=c. [tbl] SetTop(L, -2); // pop tbl. [] } + +void SetFieldNumber(void *L, const char *key, double value) { + PushString(L, key); + PushNumber(L, value); + SetTable(L, -3); +} + +void SetFieldString(void *L, const char *key, const char *value) { + PushString(L, key); + PushString(L, value != nullptr ? value : ""); + SetTable(L, -3); +} + +void SetFieldBool(void *L, const char *key, bool value) { + PushString(L, key); + PushBoolean(L, value ? 1 : 0); + SetTable(L, -3); +} } // namespace Lua namespace { diff --git a/src/Game.h b/src/Game.h index f20d3608..76d15ba7 100644 --- a/src/Game.h +++ b/src/Game.h @@ -124,6 +124,15 @@ void RegisterFrameMethods(void *context, const FrameMethodEntry *table, int coun // functions, so we manipulate the globals table directly via the Lua C API. void RegisterTableFunction(const char *tableName, const char *methodName, CFunction func); + +// Set `t[key] = value` on the table currently at stack[-1] (the most +// common shape used when populating a struct-style table mid-build). +// Pushes the key + value, calls `SetTable(L, -3)`, which pops both and +// leaves the table on the stack — so it's safe to chain. NULL string +// values are coerced to `""` so callers don't have to gate each push. +void SetFieldNumber(void *L, const char *key, double value); +void SetFieldString(void *L, const char *key, const char *value); +void SetFieldBool(void *L, const char *key, bool value); } // namespace Lua // Self-registration for API modules. Each module .cpp declares a file-scope diff --git a/src/aura/Api.cpp b/src/aura/Api.cpp index cd24b299..7f978db5 100644 --- a/src/aura/Api.cpp +++ b/src/aura/Api.cpp @@ -252,18 +252,10 @@ void PushColor(void *L, double r, double g, double b, double a) { Game::Lua::SetTop(L, -2); } Game::Lua::NewTable(L); - Game::Lua::PushString(L, "r"); - Game::Lua::PushNumber(L, r); - Game::Lua::SetTable(L, -3); - Game::Lua::PushString(L, "g"); - Game::Lua::PushNumber(L, g); - Game::Lua::SetTable(L, -3); - Game::Lua::PushString(L, "b"); - Game::Lua::PushNumber(L, b); - Game::Lua::SetTable(L, -3); - Game::Lua::PushString(L, "a"); - Game::Lua::PushNumber(L, a); - Game::Lua::SetTable(L, -3); + Game::Lua::SetFieldNumber(L, "r", r); + Game::Lua::SetFieldNumber(L, "g", g); + Game::Lua::SetFieldNumber(L, "b", b); + Game::Lua::SetFieldNumber(L, "a", a); } int __fastcall Script_GetAuraDispelTypeColor(void *L) { diff --git a/src/aura/Data.cpp b/src/aura/Data.cpp index c03705b8..be954b28 100644 --- a/src/aura/Data.cpp +++ b/src/aura/Data.cpp @@ -86,24 +86,6 @@ const char *SpellIconPath(const uint8_t *spellRecord) { iconRecord + OFF_SPELLICON_PATH); } -void SetString(void *L, const char *key, const char *value) { - Game::Lua::PushString(L, key); - Game::Lua::PushString(L, value); - Game::Lua::SetTable(L, -3); -} - -void SetNumber(void *L, const char *key, double value) { - Game::Lua::PushString(L, key); - Game::Lua::PushNumber(L, value); - Game::Lua::SetTable(L, -3); -} - -void SetBool(void *L, const char *key, bool value) { - Game::Lua::PushString(L, key); - Game::Lua::PushBoolean(L, value ? 1 : 0); - Game::Lua::SetTable(L, -3); -} - } // namespace uint32_t ReadSpellID(const uint8_t *unit, int slot) { @@ -230,33 +212,34 @@ void Push(void *L, const uint8_t *unit, int slot) { Game::Lua::NewTable(L); - SetString(L, "name", name != nullptr ? name : ""); - SetString(L, "icon", icon != nullptr ? icon : ""); - SetNumber(L, "applications", static_cast(ReadStacks(unit, slot))); - SetNumber(L, "spellId", static_cast(spellID)); - SetString(L, "dispelName", dispel != nullptr ? dispel : ""); - SetBool(L, "isHelpful", isHelpful); - SetBool(L, "isHarmful", !isHelpful); + Game::Lua::SetFieldString(L, "name", name); + Game::Lua::SetFieldString(L, "icon", icon); + Game::Lua::SetFieldNumber(L, "applications", + static_cast(ReadStacks(unit, slot))); + Game::Lua::SetFieldNumber(L, "spellId", static_cast(spellID)); + Game::Lua::SetFieldString(L, "dispelName", dispel); + Game::Lua::SetFieldBool(L, "isHelpful", isHelpful); + Game::Lua::SetFieldBool(L, "isHarmful", !isHelpful); // Numeric fields modern always sets — 0 for "not applicable" to // match modern semantics (`if data.duration > 0` etc. still // works the same on vanilla). - SetNumber(L, "duration", 0); - SetNumber(L, "expirationTime", 0); - SetNumber(L, "charges", 0); - SetNumber(L, "maxCharges", 0); - SetNumber(L, "timeMod", 1); + Game::Lua::SetFieldNumber(L, "duration", 0); + Game::Lua::SetFieldNumber(L, "expirationTime", 0); + Game::Lua::SetFieldNumber(L, "charges", 0); + Game::Lua::SetFieldNumber(L, "maxCharges", 0); + Game::Lua::SetFieldNumber(L, "timeMod", 1); // Boolean fields whose modern semantics don't apply in vanilla. - SetBool(L, "isStealable", false); - SetBool(L, "isBossAura", false); - SetBool(L, "isFromPlayerOrPlayerPet", false); - SetBool(L, "isNameplateOnly", false); - SetBool(L, "nameplateShowAll", false); - SetBool(L, "nameplateShowPersonal", false); - SetBool(L, "canApplyAura", false); - SetBool(L, "shouldConsolidate", false); - SetBool(L, "isRaid", false); + Game::Lua::SetFieldBool(L, "isStealable", false); + Game::Lua::SetFieldBool(L, "isBossAura", false); + Game::Lua::SetFieldBool(L, "isFromPlayerOrPlayerPet", false); + Game::Lua::SetFieldBool(L, "isNameplateOnly", false); + Game::Lua::SetFieldBool(L, "nameplateShowAll", false); + Game::Lua::SetFieldBool(L, "nameplateShowPersonal", false); + Game::Lua::SetFieldBool(L, "canApplyAura", false); + Game::Lua::SetFieldBool(L, "shouldConsolidate", false); + Game::Lua::SetFieldBool(L, "isRaid", false); // `sourceUnit`, `auraInstanceID`, `points` deliberately omitted // — modern returns nil for those when they don't apply, and diff --git a/src/charlist/Cache.cpp b/src/charlist/Cache.cpp index 2f401b67..53dfd1cb 100644 --- a/src/charlist/Cache.cpp +++ b/src/charlist/Cache.cpp @@ -205,20 +205,6 @@ int __fastcall Script_GetCharacterInfo(void *L) { return 8; } -// Pushes a key/string-value pair into the table at stack[-3] (the -// table we're populating in `Script_GetCharacters`). -void SetTableStringField(void *L, const char *key, const char *value) { - Game::Lua::PushString(L, key); - Game::Lua::PushString(L, value != nullptr ? value : ""); - Game::Lua::SetTable(L, -3); -} - -void SetTableNumberField(void *L, const char *key, double value) { - Game::Lua::PushString(L, key); - Game::Lua::PushNumber(L, value); - Game::Lua::SetTable(L, -3); -} - // `C_CharacterList.GetCharacters()` — returns a numeric-keyed table // of CharacterInfo tables. Each entry is a struct-style table with // the same fields `GetCharacterInfo` returns as a tuple, but named. @@ -240,35 +226,35 @@ int __fastcall Script_GetCharacters(void *L) { Game::Lua::NewTable(L); // Stack: [outer-table, key, inner-table] - SetTableStringField(L, "name", s.name.c_str()); + Game::Lua::SetFieldString(L, "name", s.name.c_str()); - SetTableStringField(L, "localizedRace", LookupDBCName( + Game::Lua::SetFieldString(L, "localizedRace", LookupDBCName( Offsets::VAR_CHRRACES_RECORDS, Offsets::VAR_CHRRACES_COUNT, s.raceID, Offsets::OFF_CHRRACES_NAMES)); - SetTableStringField(L, "englishRace", LookupDBCField( + Game::Lua::SetFieldString(L, "englishRace", LookupDBCField( Offsets::VAR_CHRRACES_RECORDS, Offsets::VAR_CHRRACES_COUNT, s.raceID, Offsets::OFF_CHRRACES_FILENAME)); - SetTableStringField(L, "localizedClass", LookupDBCName( + Game::Lua::SetFieldString(L, "localizedClass", LookupDBCName( Offsets::VAR_CHRCLASSES_RECORDS, Offsets::VAR_CHRCLASSES_COUNT, s.classID, Offsets::OFF_CHRCLASSES_NAMES)); - SetTableNumberField(L, "level", static_cast(s.level)); + Game::Lua::SetFieldNumber(L, "level", static_cast(s.level)); const char *area = LookupDBCName( Offsets::VAR_AREATABLE_RECORDS, Offsets::VAR_AREATABLE_COUNT, static_cast(s.areaID), Offsets::OFF_AREATABLE_NAMES); if (area != nullptr) - SetTableStringField(L, "areaName", area); + Game::Lua::SetFieldString(L, "areaName", area); - SetTableNumberField(L, "sex", static_cast(s.sex)); + Game::Lua::SetFieldNumber(L, "sex", static_cast(s.sex)); char buf[24]; std::snprintf(buf, sizeof(buf), "0x%08X%08X", static_cast(s.guid >> 32), static_cast(s.guid)); - SetTableStringField(L, "guid", buf); + Game::Lua::SetFieldString(L, "guid", buf); // Insert inner-table into outer-table at numeric key. Game::Lua::SetTable(L, -3); diff --git a/src/gossip/Info.cpp b/src/gossip/Info.cpp index 0b4a6175..5d5db7c6 100644 --- a/src/gossip/Info.cpp +++ b/src/gossip/Info.cpp @@ -79,24 +79,6 @@ bool IsActiveQuest(uint32_t status) { return status == 3 || status == 4; } -void SetTableNumberField(void *L, const char *key, double value) { - Game::Lua::PushString(L, key); - Game::Lua::PushNumber(L, value); - Game::Lua::SetTable(L, -3); -} - -void SetTableStringField(void *L, const char *key, const char *value) { - Game::Lua::PushString(L, key); - Game::Lua::PushString(L, value != nullptr ? value : ""); - Game::Lua::SetTable(L, -3); -} - -void SetTableBoolField(void *L, const char *key, bool value) { - Game::Lua::PushString(L, key); - Game::Lua::PushBoolean(L, value ? 1 : 0); - Game::Lua::SetTable(L, -3); -} - // `C_GossipInfo.GetText()` — pushes the greeting string the engine // resolved for the gossip-giver's NPC_TEXT.dbc entry. Mirrors what // `Script_GetGossipText` does (one inline `lua_pushstring` of the @@ -138,16 +120,17 @@ int __fastcall Script_C_GossipInfo_GetOptions(void *L) { Game::Lua::PushNumber(L, static_cast(outIdx)); Game::Lua::NewTable(L); - SetTableNumberField(L, "gossipOptionID", - static_cast(optIdx)); - SetTableStringField(L, "name", reinterpret_cast( + Game::Lua::SetFieldNumber(L, "gossipOptionID", + static_cast(optIdx)); + Game::Lua::SetFieldString(L, "name", reinterpret_cast( entry + Offsets::OFF_GOSSIP_OPTION_TEXT)); - SetTableNumberField(L, "icon", + Game::Lua::SetFieldNumber(L, "icon", static_cast(entry[Offsets::OFF_GOSSIP_OPTION_ICON])); const uint8_t boxCoded = entry[Offsets::OFF_GOSSIP_OPTION_BOX_CODED]; - SetTableNumberField(L, "flags", + Game::Lua::SetFieldNumber(L, "flags", static_cast(boxCoded ? 1u : 0u)); - SetTableNumberField(L, "orderIndex", static_cast(outIdx)); + Game::Lua::SetFieldNumber(L, "orderIndex", + static_cast(outIdx)); Game::Lua::SetTable(L, -3); } @@ -176,17 +159,19 @@ int PushQuestList(void *L, bool wantActive) { Game::Lua::PushNumber(L, static_cast(outIdx)); Game::Lua::NewTable(L); - SetTableNumberField(L, "questID", static_cast(questID)); - SetTableStringField(L, "title", reinterpret_cast( + Game::Lua::SetFieldNumber(L, "questID", + static_cast(questID)); + Game::Lua::SetFieldString(L, "title", reinterpret_cast( entry + Offsets::OFF_GOSSIP_QUEST_TITLE)); const int32_t level = *reinterpret_cast( entry + Offsets::OFF_GOSSIP_QUEST_LEVEL); - SetTableNumberField(L, "questLevel", static_cast(level)); + Game::Lua::SetFieldNumber(L, "questLevel", + static_cast(level)); // `isComplete` is only meaningful for active quests; vanilla's // status==4 row corresponds to "ready to turn in" (the engine // also branches on this value in the gossip-icon-color path). if (wantActive) - SetTableBoolField(L, "isComplete", status == 4); + Game::Lua::SetFieldBool(L, "isComplete", status == 4); Game::Lua::SetTable(L, -3); } diff --git a/src/item/GetItemLocation.cpp b/src/item/GetItemLocation.cpp index 8cb41ee5..2f5d667e 100644 --- a/src/item/GetItemLocation.cpp +++ b/src/item/GetItemLocation.cpp @@ -23,20 +23,15 @@ namespace { void PushEquipmentLocation(void *L, int slot) { Game::Lua::SetTop(L, 0); Game::Lua::NewTable(L); - Game::Lua::PushString(L, "equipmentSlotIndex"); - Game::Lua::PushNumber(L, static_cast(slot)); - Game::Lua::SetTable(L, -3); + Game::Lua::SetFieldNumber(L, "equipmentSlotIndex", + static_cast(slot)); } void PushBagLocation(void *L, int bagID, int slotIndex) { Game::Lua::SetTop(L, 0); Game::Lua::NewTable(L); - Game::Lua::PushString(L, "bagID"); - Game::Lua::PushNumber(L, static_cast(bagID)); - Game::Lua::SetTable(L, -3); - Game::Lua::PushString(L, "slotIndex"); - Game::Lua::PushNumber(L, static_cast(slotIndex)); - Game::Lua::SetTable(L, -3); + Game::Lua::SetFieldNumber(L, "bagID", static_cast(bagID)); + Game::Lua::SetFieldNumber(L, "slotIndex", static_cast(slotIndex)); } } // namespace diff --git a/src/time/DateAndTime.cpp b/src/time/DateAndTime.cpp index a00cadda..f03c9e65 100644 --- a/src/time/DateAndTime.cpp +++ b/src/time/DateAndTime.cpp @@ -52,23 +52,17 @@ namespace { constexpr int SECONDS_PER_DAY = 86400; constexpr int SECONDS_PER_MINUTE = 60; -void SetField(void *L, const char *key, double value) { - Game::Lua::PushString(L, key); - Game::Lua::PushNumber(L, value); - Game::Lua::SetTable(L, -3); -} - // Pushes a fresh CalendarTime table at the top of the stack. Caller // owns the stack — clear it before invoking if you need the new table // at a specific index. void PushCalendarTime(void *L, const std::tm &t) { Game::Lua::NewTable(L); - SetField(L, "year", static_cast(t.tm_year + 1900)); - SetField(L, "month", static_cast(t.tm_mon + 1)); - SetField(L, "monthDay", static_cast(t.tm_mday)); - SetField(L, "weekday", static_cast(t.tm_wday + 1)); - SetField(L, "hour", static_cast(t.tm_hour)); - SetField(L, "minute", static_cast(t.tm_min)); + Game::Lua::SetFieldNumber(L, "year", static_cast(t.tm_year + 1900)); + Game::Lua::SetFieldNumber(L, "month", static_cast(t.tm_mon + 1)); + Game::Lua::SetFieldNumber(L, "monthDay", static_cast(t.tm_mday)); + Game::Lua::SetFieldNumber(L, "weekday", static_cast(t.tm_wday + 1)); + Game::Lua::SetFieldNumber(L, "hour", static_cast(t.tm_hour)); + Game::Lua::SetFieldNumber(L, "minute", static_cast(t.tm_min)); } // Reads a numeric field off the table at `tableIdx`. Returns false diff --git a/src/ui/Color.cpp b/src/ui/Color.cpp index 56a1c8b1..874643a2 100644 --- a/src/ui/Color.cpp +++ b/src/ui/Color.cpp @@ -56,18 +56,10 @@ RGBA Decode(int32_t argb) { // addon-side `CreateColor` isn't available yet. void PushPlainColor(void *L, const RGBA &c) { Game::Lua::NewTable(L); - Game::Lua::PushString(L, "r"); - Game::Lua::PushNumber(L, c.r); - Game::Lua::SetTable(L, -3); - Game::Lua::PushString(L, "g"); - Game::Lua::PushNumber(L, c.g); - Game::Lua::SetTable(L, -3); - Game::Lua::PushString(L, "b"); - Game::Lua::PushNumber(L, c.b); - Game::Lua::SetTable(L, -3); - Game::Lua::PushString(L, "a"); - Game::Lua::PushNumber(L, c.a); - Game::Lua::SetTable(L, -3); + Game::Lua::SetFieldNumber(L, "r", c.r); + Game::Lua::SetFieldNumber(L, "g", c.g); + Game::Lua::SetFieldNumber(L, "b", c.b); + Game::Lua::SetFieldNumber(L, "a", c.a); } // With `CreateColor` already at stack index `createColorIdx`, duplicates @@ -112,9 +104,7 @@ int __fastcall Script_GetColors(void *L) { Game::Lua::PushNumber(L, static_cast(i + 1)); // key Game::Lua::NewTable(L); // inner table - Game::Lua::PushString(L, "baseTag"); - Game::Lua::PushString(L, entry.baseTag); - Game::Lua::SetTable(L, -3); + Game::Lua::SetFieldString(L, "baseTag", entry.baseTag); Game::Lua::PushString(L, "color"); if (haveCreateColor)