refactor: share ASCII case-insensitive compare + DBC reverse token lookup

Add baselib/Ascii.h (header-only Ascii::ToLower / Ascii::EqualCI) and
replace the two hand-rolled case-insensitive compares (frame/Attributes'
EqI, taxi/Map's IEqual + AsciiLower).

Add DBC::ClassIdForToken / RaceIdForToken (token -> ChrClasses/ChrRaces
id, the reverse of ClassToken/RaceToken) backed by a shared
IdForStringField walk using Ascii::EqualCI, and replace player/NameCache's
two near-identical ResolveClassToken / ResolveRaceToken loops.
This commit is contained in:
Brues
2026-08-08 02:09:08 -05:00
parent b72c52d9f7
commit 76e40fec96
6 changed files with 105 additions and 113 deletions
+38
View File
@@ -0,0 +1,38 @@
// 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 <https://www.gnu.org/licenses/>.
#pragma once
// Tiny ASCII string helpers. Header-only — no locale, no allocation, safe from
// any context (hook callbacks, DBC walks). Use these instead of hand-rolling
// yet another case-insensitive compare.
namespace Ascii {
// Fold a single ASCII char to lower case (non-letters unchanged).
inline char ToLower(char c) {
return (c >= 'A' && c <= 'Z') ? static_cast<char>(c - 'A' + 'a') : c;
}
// Case-insensitive full-string equality over ASCII. Two null pointers compare
// equal; a null vs non-null does not.
inline bool EqualCI(const char *a, const char *b) {
if (a == nullptr || b == nullptr)
return a == b;
for (; *a && *b; ++a, ++b)
if (ToLower(*a) != ToLower(*b))
return false;
return *a == *b;
}
} // namespace Ascii
+40
View File
@@ -15,9 +15,37 @@
#include "Lookup.h"
#include "Offsets.h"
#include "baselib/Ascii.h"
namespace DBC {
namespace {
// Reverse lookup shared by ClassIdForToken / RaceIdForToken: the id whose
// single-string field at `offset` matches `token` (case-insensitive), or 0.
uint32_t IdForStringField(uintptr_t recordsVar, uintptr_t countVar, int offset,
const char *token) {
if (token == nullptr || *token == '\0')
return 0;
const int count = *reinterpret_cast<const int *>(countVar);
const uint8_t *const *records =
*reinterpret_cast<const uint8_t *const *const *>(recordsVar);
if (records == nullptr)
return 0;
for (int i = 1; i <= count; ++i) {
const uint8_t *rec = records[i];
if (rec == nullptr)
continue;
const char *field =
*reinterpret_cast<const char *const *>(rec + offset);
if (field != nullptr && Ascii::EqualCI(field, token))
return static_cast<uint32_t>(i);
}
return 0;
}
} // namespace
const char *ClassName(uint32_t classID) {
if (classID == 0)
return nullptr;
@@ -50,6 +78,18 @@ const char *RaceToken(uint32_t raceID) {
Offsets::OFF_CHRRACES_FILENAME);
}
uint32_t ClassIdForToken(const char *token) {
return IdForStringField(Offsets::VAR_CHRCLASSES_RECORDS,
Offsets::VAR_CHRCLASSES_COUNT,
Offsets::OFF_CHRCLASSES_FILENAME, token);
}
uint32_t RaceIdForToken(const char *token) {
return IdForStringField(Offsets::VAR_CHRRACES_RECORDS,
Offsets::VAR_CHRRACES_COUNT,
Offsets::OFF_CHRRACES_FILENAME, token);
}
const char *AreaName(uint32_t areaID, bool resolveToParent) {
if (areaID == 0)
return nullptr;
+6
View File
@@ -39,6 +39,12 @@ const char *RaceName(uint32_t raceID);
// `UnitRace`'s second return / GetPlayerInfoByGUID's englishRace use.
const char *RaceToken(uint32_t raceID);
// Reverse of ClassToken / RaceToken: the `ChrClasses.dbc` / `ChrRaces.dbc` id
// whose client-filename token matches `token` (case-insensitive), or 0 if none
// matches. Walks the table (cheap — a handful of records).
uint32_t ClassIdForToken(const char *token);
uint32_t RaceIdForToken(const char *token);
// Localized zone name for an `AreaTable.dbc` id. When `resolveToParent` is
// true, a sub-area reports its parent zone (so "Goldshire" → "Elwynn
// Forest"); when false, the area's own name is returned (what the engine's
+15 -23
View File
@@ -96,6 +96,7 @@
#include "Game.h"
#include "Offsets.h"
#include "baselib/Ascii.h"
#include "cursor/Info.h"
#include "spell/AtCursor.h"
#include "spell/AtUnit.h"
@@ -147,15 +148,6 @@ void Compose3Lower(char *dst, size_t n, const char *a, const char *b, const char
dst[i] = '\0';
}
// ASCII case-insensitive full-string equality.
bool EqI(const char *a, const char *b) {
for (; *a && *b; ++a, ++b)
if (std::tolower(static_cast<unsigned char>(*a)) !=
std::tolower(static_cast<unsigned char>(*b)))
return false;
return *a == *b;
}
// True if `lname` (already lowercase) is a click "type" action attribute: an
// optional modifier prefix (alt-/ctrl-/shift-, any order/combo) followed by
// "type" and an optional button digit — i.e. anything the click resolver
@@ -510,10 +502,10 @@ void BuildModifierPrefix(void *L, char *buf, size_t n) {
// Button name -> attribute suffix (retail's convention: the button number).
const char *ButtonSuffix(const char *btn) {
if (EqI(btn, "RightButton")) return "2";
if (EqI(btn, "MiddleButton")) return "3";
if (EqI(btn, "Button4")) return "4";
if (EqI(btn, "Button5")) return "5";
if (Ascii::EqualCI(btn, "RightButton")) return "2";
if (Ascii::EqualCI(btn, "MiddleButton")) return "3";
if (Ascii::EqualCI(btn, "Button4")) return "4";
if (Ascii::EqualCI(btn, "Button5")) return "5";
return "1"; // LeftButton / unknown
}
@@ -601,10 +593,10 @@ bool CallGlobalNum2(void *L, const char *name, double a, double b) {
// Returns true if it owned the click (so the chained handler is skipped).
bool DispatchVerb(void *L, int fi, const char *prefix, const char *suffix,
const char *verb, const char *unit) {
if (EqI(verb, "target")) {
if (Ascii::EqualCI(verb, "target")) {
if (!unit) return false;
// `unit="none"` clears the target (retail's SecureActionButton behavior).
if (EqI(unit, "none")) {
if (Ascii::EqualCI(unit, "none")) {
Game::Lua::CallGlobal(L, "ClearTarget");
return true;
}
@@ -621,17 +613,17 @@ bool DispatchVerb(void *L, int fi, const char *prefix, const char *suffix,
Game::Lua::CallGlobalString(L, "TargetUnit", unit);
return true;
}
if (EqI(verb, "assist")) {
if (Ascii::EqualCI(verb, "assist")) {
if (!unit) return false;
Game::Lua::CallGlobalString(L, "AssistUnit", unit);
return true;
}
if (EqI(verb, "focus")) {
if (Ascii::EqualCI(verb, "focus")) {
if (!unit) return false;
Unit::Focus::Set(Unit::Identity::GuidForToken(unit));
return true;
}
if (EqI(verb, "spell")) {
if (Ascii::EqualCI(verb, "spell")) {
if (!unit) return false;
char spell[128];
if (!ReadModAttr(L, fi, prefix, "spell", suffix, spell, sizeof spell))
@@ -639,7 +631,7 @@ bool DispatchVerb(void *L, int fi, const char *prefix, const char *suffix,
Spell::AtUnit::CastByName(spell, unit);
return true;
}
if (EqI(verb, "item")) {
if (Ascii::EqualCI(verb, "item")) {
char item[128];
int bag, slot;
if (ReadModAttr(L, fi, prefix, "item", suffix, item, sizeof item)) {
@@ -657,7 +649,7 @@ bool DispatchVerb(void *L, int fi, const char *prefix, const char *suffix,
}
return false;
}
if (EqI(verb, "macro")) {
if (Ascii::EqualCI(verb, "macro")) {
char macro[512];
if (!ReadModAttr(L, fi, prefix, "macrotext", suffix, macro, sizeof macro) &&
!ReadModAttr(L, fi, prefix, "macro", suffix, macro, sizeof macro))
@@ -669,11 +661,11 @@ bool DispatchVerb(void *L, int fi, const char *prefix, const char *suffix,
RunMacroTextC(L, macro);
return true;
}
if (EqI(verb, "stop") || EqI(verb, "stopcasting")) {
if (Ascii::EqualCI(verb, "stop") || Ascii::EqualCI(verb, "stopcasting")) {
Game::Lua::CallGlobal(L, "SpellStopCasting");
return true;
}
if (EqI(verb, "menu") || EqI(verb, "togglemenu")) {
if (Ascii::EqualCI(verb, "menu") || Ascii::EqualCI(verb, "togglemenu")) {
if (!unit) return false;
// The unit dropdown is pure FrameXML work (UnitPopup + ToggleDropDown),
// so it lives in the !!!ClassicAPI addon; we just pop it at the cursor.
@@ -793,7 +785,7 @@ int __fastcall FrameResolver_h(void *frame, void *edx, const char *name) {
const int slot = g_frameResolverOriginal(frame, edx, name);
if (slot != 0) // a real base-frame / subtype script — leave it
return slot;
if (EqI(name, "onattributechanged"))
if (Ascii::EqualCI(name, "onattributechanged"))
return reinterpret_cast<int>(AttrSlotFor(frame, /*create*/ true));
return 0;
}
+3 -76
View File
@@ -66,6 +66,7 @@
#include "Game.h"
#include "Offsets.h"
#include "dbc/Names.h"
#include "guid/Guid.h"
#include "settings/Account.h"
#include "unit/Identity.h"
@@ -636,80 +637,6 @@ bool ParseGUID(const char *str, uint64_t *outGUID) {
return Guid::Parse(str, outGUID);
}
// Returns the ChrClasses.dbc record ID for a class token like
// `"WARRIOR"`, `"MAGE"`, etc. Case-insensitive. 0 if not matched.
// 1.12 has 9 player classes; we walk the DBC each call (cheap — 9
// records, each filename a few bytes) rather than building a static
// table that risks getting out-of-sync.
uint32_t ResolveClassToken(const char *token) {
if (token == nullptr || *token == '\0')
return 0;
const int count = *reinterpret_cast<const int *>(
static_cast<uintptr_t>(Offsets::VAR_CHRCLASSES_COUNT));
const uint8_t *const *records = *reinterpret_cast<const uint8_t *const *const *>(
static_cast<uintptr_t>(Offsets::VAR_CHRCLASSES_RECORDS));
if (records == nullptr)
return 0;
for (int i = 1; i <= count; ++i) {
const uint8_t *rec = records[i];
if (rec == nullptr)
continue;
const char *filename = *reinterpret_cast<const char *const *>(
rec + Offsets::OFF_CHRCLASSES_FILENAME);
if (filename == nullptr)
continue;
// Case-insensitive compare; vanilla tokens are uppercase
// ("WARRIOR", "MAGE", ...) and modern Classic uses the same.
const char *a = filename;
const char *b = token;
bool match = true;
while (*a && *b) {
char ca = *a, cb = *b;
if (ca >= 'a' && ca <= 'z') ca -= 32;
if (cb >= 'a' && cb <= 'z') cb -= 32;
if (ca != cb) { match = false; break; }
++a; ++b;
}
if (match && *a == '\0' && *b == '\0')
return static_cast<uint32_t>(i);
}
return 0;
}
// Same shape as ResolveClassToken but walks ChrRaces.dbc.
uint32_t ResolveRaceToken(const char *token) {
if (token == nullptr || *token == '\0')
return 0;
const int count = *reinterpret_cast<const int *>(
static_cast<uintptr_t>(Offsets::VAR_CHRRACES_COUNT));
const uint8_t *const *records = *reinterpret_cast<const uint8_t *const *const *>(
static_cast<uintptr_t>(Offsets::VAR_CHRRACES_RECORDS));
if (records == nullptr)
return 0;
for (int i = 1; i <= count; ++i) {
const uint8_t *rec = records[i];
if (rec == nullptr)
continue;
const char *filename = *reinterpret_cast<const char *const *>(
rec + Offsets::OFF_CHRRACES_FILENAME);
if (filename == nullptr)
continue;
const char *a = filename;
const char *b = token;
bool match = true;
while (*a && *b) {
char ca = *a, cb = *b;
if (ca >= 'a' && ca <= 'z') ca -= 32;
if (cb >= 'a' && cb <= 'z') cb -= 32;
if (ca != cb) { match = false; break; }
++a; ++b;
}
if (match && *a == '\0' && *b == '\0')
return static_cast<uint32_t>(i);
}
return 0;
}
// `C_PlayerCache.RememberPlayer(guid, name, classToken [, raceToken
// [, sex]])` — stores the entry in the persistent cache. Class and
// race tokens are uppercase engine tokens (`"WARRIOR"`, `"NIGHTELF"`,
@@ -740,13 +667,13 @@ int __fastcall Script_C_PlayerCache_RememberPlayer(void *L) {
return 1;
}
const char *classToken = Game::Lua::ToString(L, 3);
const uint32_t classID = ResolveClassToken(classToken);
const uint32_t classID = DBC::ClassIdForToken(classToken);
// Optional race + sex. Missing / non-string / unknown values
// resolve to 0, which Remember() treats as "leave existing value
// alone" — so a 3-arg call preserves any prior race/sex data.
uint32_t raceID = 0;
if (Game::Lua::IsString(L, 4))
raceID = ResolveRaceToken(Game::Lua::ToString(L, 4));
raceID = DBC::RaceIdForToken(Game::Lua::ToString(L, 4));
uint32_t sex = 0;
if (Game::Lua::IsNumber(L, 5)) {
const double raw = Game::Lua::ToNumber(L, 5);
+3 -14
View File
@@ -34,6 +34,7 @@
#include "Game.h"
#include "Offsets.h"
#include "baselib/Ascii.h"
#include "dbc/Lookup.h"
#include "dbc/Names.h"
#include "map/Area.h"
@@ -67,24 +68,12 @@ int IntField(const uint8_t *rec, int off) {
// suffix against AreaTable and fall back to the geometric resolver only when a
// node has no ", Zone" suffix or the zone name doesn't match.
char AsciiLower(char c) { return (c >= 'A' && c <= 'Z') ? char(c - 'A' + 'a') : c; }
bool IEqual(const char *a, const char *b) {
while (*a && *b) {
if (AsciiLower(*a) != AsciiLower(*b))
return false;
++a;
++b;
}
return *a == *b;
}
// True if `name` begins with `prefix` followed by a space — a word-prefix, so
// the abbreviated taxi suffixes ("Redridge", "Arathi") match the full
// AreaTable names ("Redridge Mountains", "Arathi Highlands").
bool IStartsWord(const char *name, const char *prefix) {
while (*prefix) {
if (!*name || AsciiLower(*name) != AsciiLower(*prefix))
if (!*name || Ascii::ToLower(*name) != Ascii::ToLower(*prefix))
return false;
++name;
++prefix;
@@ -135,7 +124,7 @@ int ResolveZoneByName(const char *zone) {
DBC::AreaName(static_cast<uint32_t>(id), /*resolveToParent=*/false);
if (nm == nullptr)
continue;
if (IEqual(nm, zone))
if (Ascii::EqualCI(nm, zone))
return id; // exact wins outright
if (prefixHit == 0 && IStartsWord(nm, zone))
prefixHit = id;