Lua SetField cleanup

This commit is contained in:
Brues
2026-05-14 14:05:27 -05:00
parent f6d6e020f3
commit ef7ccb4723
9 changed files with 89 additions and 137 deletions
+18
View File
@@ -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 {
+9
View File
@@ -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
+4 -12
View File
@@ -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) {
+22 -39
View File
@@ -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<double>(ReadStacks(unit, slot)));
SetNumber(L, "spellId", static_cast<double>(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<double>(ReadStacks(unit, slot)));
Game::Lua::SetFieldNumber(L, "spellId", static_cast<double>(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
+8 -22
View File
@@ -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<double>(s.level));
Game::Lua::SetFieldNumber(L, "level", static_cast<double>(s.level));
const char *area = LookupDBCName(
Offsets::VAR_AREATABLE_RECORDS, Offsets::VAR_AREATABLE_COUNT,
static_cast<int>(s.areaID), Offsets::OFF_AREATABLE_NAMES);
if (area != nullptr)
SetTableStringField(L, "areaName", area);
Game::Lua::SetFieldString(L, "areaName", area);
SetTableNumberField(L, "sex", static_cast<double>(s.sex));
Game::Lua::SetFieldNumber(L, "sex", static_cast<double>(s.sex));
char buf[24];
std::snprintf(buf, sizeof(buf), "0x%08X%08X",
static_cast<uint32_t>(s.guid >> 32),
static_cast<uint32_t>(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);
+13 -28
View File
@@ -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<double>(outIdx));
Game::Lua::NewTable(L);
SetTableNumberField(L, "gossipOptionID",
static_cast<double>(optIdx));
SetTableStringField(L, "name", reinterpret_cast<const char *>(
Game::Lua::SetFieldNumber(L, "gossipOptionID",
static_cast<double>(optIdx));
Game::Lua::SetFieldString(L, "name", reinterpret_cast<const char *>(
entry + Offsets::OFF_GOSSIP_OPTION_TEXT));
SetTableNumberField(L, "icon",
Game::Lua::SetFieldNumber(L, "icon",
static_cast<double>(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<double>(boxCoded ? 1u : 0u));
SetTableNumberField(L, "orderIndex", static_cast<double>(outIdx));
Game::Lua::SetFieldNumber(L, "orderIndex",
static_cast<double>(outIdx));
Game::Lua::SetTable(L, -3);
}
@@ -176,17 +159,19 @@ int PushQuestList(void *L, bool wantActive) {
Game::Lua::PushNumber(L, static_cast<double>(outIdx));
Game::Lua::NewTable(L);
SetTableNumberField(L, "questID", static_cast<double>(questID));
SetTableStringField(L, "title", reinterpret_cast<const char *>(
Game::Lua::SetFieldNumber(L, "questID",
static_cast<double>(questID));
Game::Lua::SetFieldString(L, "title", reinterpret_cast<const char *>(
entry + Offsets::OFF_GOSSIP_QUEST_TITLE));
const int32_t level = *reinterpret_cast<const int32_t *>(
entry + Offsets::OFF_GOSSIP_QUEST_LEVEL);
SetTableNumberField(L, "questLevel", static_cast<double>(level));
Game::Lua::SetFieldNumber(L, "questLevel",
static_cast<double>(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);
}
+4 -9
View File
@@ -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<double>(slot));
Game::Lua::SetTable(L, -3);
Game::Lua::SetFieldNumber(L, "equipmentSlotIndex",
static_cast<double>(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<double>(bagID));
Game::Lua::SetTable(L, -3);
Game::Lua::PushString(L, "slotIndex");
Game::Lua::PushNumber(L, static_cast<double>(slotIndex));
Game::Lua::SetTable(L, -3);
Game::Lua::SetFieldNumber(L, "bagID", static_cast<double>(bagID));
Game::Lua::SetFieldNumber(L, "slotIndex", static_cast<double>(slotIndex));
}
} // namespace
+6 -12
View File
@@ -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<double>(t.tm_year + 1900));
SetField(L, "month", static_cast<double>(t.tm_mon + 1));
SetField(L, "monthDay", static_cast<double>(t.tm_mday));
SetField(L, "weekday", static_cast<double>(t.tm_wday + 1));
SetField(L, "hour", static_cast<double>(t.tm_hour));
SetField(L, "minute", static_cast<double>(t.tm_min));
Game::Lua::SetFieldNumber(L, "year", static_cast<double>(t.tm_year + 1900));
Game::Lua::SetFieldNumber(L, "month", static_cast<double>(t.tm_mon + 1));
Game::Lua::SetFieldNumber(L, "monthDay", static_cast<double>(t.tm_mday));
Game::Lua::SetFieldNumber(L, "weekday", static_cast<double>(t.tm_wday + 1));
Game::Lua::SetFieldNumber(L, "hour", static_cast<double>(t.tm_hour));
Game::Lua::SetFieldNumber(L, "minute", static_cast<double>(t.tm_min));
}
// Reads a numeric field off the table at `tableIdx`. Returns false
+5 -15
View File
@@ -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<double>(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)