diff --git a/README.md b/README.md index d5430e47..ae7bf1b2 100644 --- a/README.md +++ b/README.md @@ -35,7 +35,7 @@ Full per-function reference: **[docs/API.md](docs/API.md)**. | Item | `C_Item.DoesItemExist`, `C_Item.DoesItemExistByID`, `C_Item.EquipItemByName`, `C_Item.GetCurrentItemLevel`, `C_Item.GetDetailedItemLevelInfo`, `C_Item.GetItemCount`, `C_Item.GetItemFamily`, `C_Item.GetItemGUID`, `C_Item.GetItemIcon`, `C_Item.GetItemIconByID`, `C_Item.GetItemID`, `C_Item.GetItemInfoInstant`, `C_Item.GetItemInventoryType`, `C_Item.GetItemInventoryTypeByID`, `C_Item.GetItemLink`, `C_Item.GetItemLocation`, `C_Item.GetItemMaxStackSize`, `C_Item.GetItemMaxStackSizeByID`, `C_Item.GetItemName`, `C_Item.GetItemNameByID`, `C_Item.GetItemQuality`, `C_Item.GetItemQualityByID`, `C_Item.GetItemSpell`, `C_Item.GetWeaponEnchantInfo`, `C_Item.IsBound`, `C_Item.IsEquippableItem`, `C_Item.IsEquippedItem`, `C_Item.IsItemDataCached`, `C_Item.IsItemDataCachedByID`, `C_Item.IsLocked`, `C_Item.RequestLoadItemData`, `C_Item.RequestLoadItemDataByID`, `GetAuctionItemID`, `GetAuctionSellItemID`, `GetCraftReagentItemID`, `GetInboxItemID`, `GetInventoryItemDurability`, `GetInventoryItemID`, `GetInventoryItemRepairCost`, `GetItemIcon`, `GetLootRollItemID`, `GetLootSlotItemID`, `GetMerchantItemID`, `GetQuestItemID`, `GetQuestLogItemID`, `GetTradePlayerItemID`, `GetTradeSkillItemID`, `GetTradeSkillReagentItemID`, `GetTradeTargetItemID`, `OffhandHasWeapon` | | NameCache | `C_PlayerCache.IsEnabled`, `C_PlayerCache.IsScanEnabled`, `C_PlayerCache.RememberPlayer`, `C_PlayerCache.SetEnabled`, `C_PlayerCache.SetScanEnabled`, `C_PlayerInfo.GUIDIsCreature`, `C_PlayerInfo.GUIDIsGameObject`, `C_PlayerInfo.GUIDIsPet`, `C_PlayerInfo.GUIDIsPlayer`, `GetPlayerInfoByGUID` | | Quest | `C_QuestLog.GetTitleForQuestID`, `C_QuestLog.RequestLoadQuestByID`, `GetQuestIDFromLogIndex` | -| Spell | `C_Spell.GetSpellDescription`, `C_Spell.GetSpellInfo`, `C_Spell.GetSpellLink`, `C_Spell.GetSpellName`, `C_Spell.GetSpellTexture`, `C_Spell.IsSpellHarmful`, `C_Spell.IsSpellHelpful`, `C_Spell.IsSpellPassive`, `C_Spell.IsSpellUsable`, `C_SpellBook.GetCurrentLevelSpells`, `C_SpellBook.GetSpellLevelLearned`, `CastAutoRepeatSpell`, `FindSpellBookSlotByID`, `GetCraftSpellID`, `GetSpellInfo`, `GetSpellLink`, `GetSpellSchool`, `IsHarmfulSpell`, `IsHelpfulSpell`, `IsPassiveSpell`, `IsPlayerSpell`, `IsSpellKnown`, `IsUsableSpell` | +| Spell | `C_Spell.GetSpellDescription`, `C_Spell.GetSpellInfo`, `C_Spell.GetSpellLink`, `C_Spell.GetSpellName`, `C_Spell.GetSpellTexture`, `C_Spell.IsSpellHarmful`, `C_Spell.IsSpellHelpful`, `C_Spell.IsSpellPassive`, `C_Spell.IsSpellUsable`, `C_SpellBook.GetCurrentLevelSpells`, `C_SpellBook.GetSpellLevelLearned`, `C_SpellBook.GetSpellSkillLine`, `CastAutoRepeatSpell`, `FindSpellBookSlotByID`, `GetCraftSpellID`, `GetSpellInfo`, `GetSpellLink`, `GetSpellSchool`, `IsHarmfulSpell`, `IsHelpfulSpell`, `IsPassiveSpell`, `IsPlayerSpell`, `IsSpellKnown`, `IsUsableSpell` | | State | `IsAssistingRitual`, `IsFalling`, `IsMounted`, `IsStealthed`, `IsSwimming` | | Table | `table.wipe` | | Talent | `GetTalentIDByIndex`, `GetTalentSpellID` | diff --git a/docs/API.md b/docs/API.md index 2be76976..5466ee59 100644 --- a/docs/API.md +++ b/docs/API.md @@ -25,6 +25,7 @@ build instructions. - [`C_Spell.IsSpellHarmful(spellID)` / `C_Spell.IsSpellHelpful(spellID)`](#c_spellisspellharmfulspellid--c_spellisspellhelpfulspellid) - [`C_SpellBook.GetSpellLevelLearned(spellID)`](#c_spellbookgetspelllevellearnedspellid) - [`C_SpellBook.GetCurrentLevelSpells([level])`](#c_spellbookgetcurrentlevelspellslevel) + - [`C_SpellBook.GetSpellSkillLine(spellID)`](#c_spellbookgetspellskilllinespellid) - [`GetSpellSchool(spellID)`](#getspellschoolspellid) - [`CastAutoRepeatSpell(name | spellID)`](#castautorepeatspellname--spellid) - [Macros](#macros) @@ -744,6 +745,46 @@ clean class-string→classID lookup. Returns an empty table at character select / pre-login (no CGPlayer yet) and for levels where no class/race spells match. +### `C_SpellBook.GetSpellSkillLine(spellID)` + +Returns `(name, skillID)` — the SkillLine.dbc row that the given spell +belongs to. The name is the user-facing skill/tab string in the engine's +current locale: spellbook tab names for class spells (`"Protection"`, +`"Fire"`, `"Holy"`), profession headers (`"Tailoring"`, `"Engineering"`), +weapon-skill rows (`"Swords"`, `"Bows"`), etc. + +```lua +C_SpellBook.GetSpellSkillLine(1671) -- "Protection", 26 (Shield Bash) +C_SpellBook.GetSpellSkillLine(133) -- "Fire", 8 (Fireball) +C_SpellBook.GetSpellSkillLine(2050) -- "Holy", 56 (Lesser Heal) +C_SpellBook.GetSpellSkillLine(2480) -- "Bows", 45 (Shoot Bow) +C_SpellBook.GetSpellSkillLine(3273) -- "First Aid", 129 (Anesthetic) +``` + +Walks `SkillLineAbility.dbc` for any row whose `spellID` matches, then +reads the localized `Name[locale]` field at offset `+0x0C` of the +referenced `SkillLine.dbc` record. For spells with multiple SLA rows +(race-locked variants, Turtle WoW's faction-specific entries) the +lookup prefers a row whose class/race masks match the local player — +so the result reflects "what tab is this spell in *for me*" when one +exists. Falls back to the first matching row otherwise, so the +function still answers for spells the local player can't actually use +(inspecting other classes, parsing combat-log entries from unfamiliar +specs). + +Returns `(nil, nil)` for: +- non-numeric or non-positive input +- spellIDs with no `SkillLineAbility.dbc` row (most temporary auras, + proc-only spells, item-on-use effects, GM-debug spells) +- rows whose `skillID` doesn't resolve in `SkillLine.dbc` + +Vanilla's `GetSpellTabInfo(tabIndex)` enumerates spellbook tabs by +1-based index — it can't answer "what tab is *this spellID* in". +Addons that need a spellID→tab mapping have historically had to walk +every tab's slot range and string-match against `GetSpellName`. This +function reads it directly off the DBC for any spellID, including +spells the player hasn't learned. + ### `GetSpellSchool(spellID)` Returns `(schoolID, schoolName)` for any spell — known to the player diff --git a/src/Offsets.h b/src/Offsets.h index 5e3ccef7..033aa0c3 100644 --- a/src/Offsets.h +++ b/src/Offsets.h @@ -1013,12 +1013,32 @@ enum Offsets { // +0x18 excludeClass (bitmask of classes this entry does NOT apply to) // +0x1C minSkillRank // ... (more fields) + OFF_SLA_SKILL_ID = 0x04, OFF_SLA_SPELL_ID = 0x08, OFF_SLA_RACE_MASK = 0x0C, OFF_SLA_CLASS_MASK = 0x10, OFF_SLA_EXCLUDE_RACE = 0x14, OFF_SLA_EXCLUDE_CLASS = 0x18, + // SkillLine.dbc — the skill/category each SLA row points into via + // its `skillId` field. Each record carries a 9-locale localized + // name array (the user-facing tab name in the spellbook, profession + // header, weapon-skill name, etc.) plus a SpellIcon ID at +0x54. + // Standard 5-DWORD class instance at `0x00C0D924`; records-array + // pointer at `0x00C0D92C`, count at `0x00C0D930`. + // + // Record layout (verified against `Script_GetSpellTabInfo` at + // `0x004B3CE0`): + // +0x00 id + // +0x0C `Name[9]` — locale string pointers, indexed via the + // global locale at `0x00C0E080` (read as + // `*(char **)(record + 0x0C + locale * 4)`) + // +0x54 SpellIcon.dbc ID (looked up via `SpellIcon` for the + // spellbook tab/profession icon) + VAR_SKILL_LINE_RECORDS = 0x00C0D92C, + VAR_SKILL_LINE_COUNT = 0x00C0D930, + OFF_SKILL_LINE_NAME = 0x0C, + // Spell.dbc `BaseLevel` — the level a spell becomes available // (trainer offers it, quest reward grants it, etc.). Distinct // from `SpellLevel` at +0x74 which is the effective level used diff --git a/src/spell/SkillLine.cpp b/src/spell/SkillLine.cpp new file mode 100644 index 00000000..08d06c60 --- /dev/null +++ b/src/spell/SkillLine.cpp @@ -0,0 +1,183 @@ +// This file is part of ClassicAPI. +// +// ClassicAPI is free software: you can redistribute it and/or modify it under the terms +// of the GNU Lesser 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 Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public License along with +// ClassicAPI. If not, see . + +#include "Game.h" +#include "Offsets.h" +#include "dbc/Lookup.h" + +#include + +namespace Spell::SkillLine { + +namespace { + +using ResolveUnitToken_t = void *(__fastcall *)(const char *token); + +// Reads UNIT_FIELD_BYTES_0 from the local player's descriptor. Same +// pattern Spell::Level::PlayerDescriptor uses. Returns (0, 0) when the +// player isn't resolvable (pre-login, character select); callers treat +// that as "no filter" and accept the first SLA match. +void ReadPlayerClassRace(uint8_t *outClassByte, uint8_t *outRaceByte) { + *outClassByte = 0; + *outRaceByte = 0; + auto fn = reinterpret_cast(Offsets::FUN_RESOLVE_UNIT_TOKEN); + auto *player = static_cast(fn("player")); + if (player == nullptr) + return; + auto *desc = *reinterpret_cast( + player + Offsets::OFF_UNIT_DESCRIPTOR); + if (desc == nullptr) + return; + *outClassByte = *(desc + Offsets::OFF_UNIT_DESCRIPTOR_CLASS_BYTE); + *outRaceByte = *(desc + Offsets::OFF_UNIT_DESCRIPTOR_RACE_BYTE); +} + +// Walks SkillLineAbility.dbc looking for rows with `spellID`. Returns +// the skill ID of the best-matching row, or 0 if no row exists. +// +// "Best" prefers a row whose class/race masks match the local player — +// so race/class-locked variants (Turtle WoW has many of these) resolve +// to the row appropriate to the asker. Falls back to the first match +// when no player-matching row exists (so the function still answers for +// spells the player can't actually use, e.g. inspecting another class's +// spell). +uint32_t FindSkillIDForSpell(int spellID) { + if (spellID <= 0) + return 0; + + auto *const *records = *reinterpret_cast( + static_cast(Offsets::VAR_SKILL_LINE_ABILITY_RECORDS)); + const int count = *reinterpret_cast( + static_cast(Offsets::VAR_SKILL_LINE_ABILITY_COUNT)); + if (records == nullptr || count <= 0) + return 0; + + uint8_t classByte = 0; + uint8_t raceByte = 0; + ReadPlayerClassRace(&classByte, &raceByte); + const uint32_t playerClassBit = classByte ? (1u << (classByte - 1)) : 0; + const uint32_t playerRaceBit = raceByte ? (1u << (raceByte - 1)) : 0; + + uint32_t fallback = 0; + + for (int i = 1; i <= count; ++i) { + const uint8_t *rec = records[i]; + if (rec == nullptr) + continue; + const int rowSpell = *reinterpret_cast( + rec + Offsets::OFF_SLA_SPELL_ID); + if (rowSpell != spellID) + continue; + + const uint32_t skill = *reinterpret_cast( + rec + Offsets::OFF_SLA_SKILL_ID); + if (skill == 0) + continue; + if (fallback == 0) + fallback = skill; + + // If we don't know who "player" is yet, we can't preference — + // the first match wins. + if (playerClassBit == 0) + return skill; + + const uint32_t classMask = *reinterpret_cast( + rec + Offsets::OFF_SLA_CLASS_MASK); + const uint32_t raceMask = *reinterpret_cast( + rec + Offsets::OFF_SLA_RACE_MASK); + const uint32_t excludeClass = *reinterpret_cast( + rec + Offsets::OFF_SLA_EXCLUDE_CLASS); + const uint32_t excludeRace = *reinterpret_cast( + rec + Offsets::OFF_SLA_EXCLUDE_RACE); + if ((excludeClass & playerClassBit) != 0) + continue; + if ((excludeRace & playerRaceBit) != 0) + continue; + const bool classOk = (classMask == 0) || + (classMask & playerClassBit) != 0; + const bool raceOk = (raceMask == 0) || + (raceMask & playerRaceBit) != 0; + if (classOk && raceOk) + return skill; + } + + return fallback; +} + +// `C_SpellBook.GetSpellSkillLine(spellID) → (name, skillID)` +// +// Resolves a `spellID` to the SkillLine.dbc row it belongs to via +// SkillLineAbility.dbc. The skill ID is what populates the spellbook +// tab name (e.g. Shield Bash → Protection, Fireball → Fire), the +// profession header (Tailoring, Engineering), or the weapon-skill row +// (Swords, Maces). +// +// Returns `(localizedName, skillID)` for spells that have an SLA entry +// — `localizedName` is the user-facing tab/skill name in the engine's +// current locale, pulled from `SkillLine.dbc::Name[locale]`. +// +// Returns `(nil, nil)` for: +// - non-numeric / non-positive input +// - spellIDs with no SkillLineAbility row (most temporary auras, +// proc spells, item-on-use effects, GM commands) +// - rows whose `skillID` doesn't resolve in SkillLine.dbc (shouldn't +// happen in practice but matches the engine's defensive behavior) +// +// For spells with multiple SLA rows (race-locked variants, Turtle WoW +// faction-specific entries), `FindSkillIDForSpell` prefers a row whose +// class/race masks match the local player. This gives the "what tab +// is this spell in *for me*" answer when one exists, and falls back to +// the first matching row otherwise — so the function still answers for +// spells the local player can't use (inspecting other classes, parsing +// combat log entries from unfamiliar specs). +int __fastcall Script_GetSpellSkillLine(void *L) { + if (!Game::Lua::IsNumber(L, 1)) { + Game::Lua::PushNil(L); + Game::Lua::PushNil(L); + return 2; + } + const int spellID = static_cast(Game::Lua::ToNumber(L, 1)); + + const uint32_t skillID = FindSkillIDForSpell(spellID); + if (skillID == 0) { + Game::Lua::PushNil(L); + Game::Lua::PushNil(L); + return 2; + } + + const char *name = DBC::LocalizedField( + Offsets::VAR_SKILL_LINE_RECORDS, + Offsets::VAR_SKILL_LINE_COUNT, + skillID, + Offsets::OFF_SKILL_LINE_NAME); + if (name == nullptr) { + Game::Lua::PushNil(L); + Game::Lua::PushNil(L); + return 2; + } + + Game::Lua::PushString(L, name); + Game::Lua::PushNumber(L, static_cast(skillID)); + return 2; +} + +void RegisterLuaFunctions() { + Game::Lua::RegisterTableFunction("C_SpellBook", "GetSpellSkillLine", + &Script_GetSpellSkillLine); +} + +const Game::ModuleAutoRegister _autoreg{&RegisterLuaFunctions}; + +} // namespace + +} // namespace Spell::SkillLine \ No newline at end of file