diff --git a/README.md b/README.md index 6e368851..260a10dd 100644 --- a/README.md +++ b/README.md @@ -126,6 +126,27 @@ The actual class/subclass values reflect 1.12.1's data, which differs from modern WoW. For example, vanilla had no Cloth subclass under Trade Goods — Silk Cloth lives at `(7, 0)` in this client, not the modern `(7, 5)`. +### `C_EventUtils.IsEventValid(eventName)` + +Returns `true` if the named event exists in the engine's event-name table +and can be registered for, `false` otherwise. Equivalent to the modern +function of the same name; useful for addons that gate code behind feature +detection (e.g. registering for events that exist in some client builds +but not others). + +```lua +if C_EventUtils.IsEventValid("PLAYER_LOGIN") then ... end -- true +if C_EventUtils.IsEventValid("COMBAT_LOG_EVENT") then ... end -- false (added in 3.x) +``` + +The check walks the engine's static event-name table at runtime, so it +sees events added by **any** loaded DLL that injects names by overwriting +`.rdata` strings in place — for example, on a Turtle WoW client running +SuperWoWhook, `IsEventValid("UNIT_CASTEVENT")` returns `true` because +SuperWoWhook patches the binary's event names with its own. This matches +what `frame:RegisterEvent` will actually accept, which is what addon code +needs to know. + ### `GetSpellInfo(spellID)` Returns the same nine values as 3.3.5's `GetSpellInfo`, for **any** spell ID diff --git a/src/DllMain.cpp b/src/DllMain.cpp index 753107e0..d2df19ac 100644 --- a/src/DllMain.cpp +++ b/src/DllMain.cpp @@ -15,6 +15,7 @@ #include "Game.h" #include "MinHook.h" #include "Offsets.h" +#include "event/Util.h" #include "item/Bound.h" #include "item/ID.h" #include "item/Info.h" @@ -45,6 +46,7 @@ static void __fastcall LoadScriptFunctions_h() { Item::Bound::RegisterLuaFunctions(); Item::ID::RegisterLuaFunctions(); Item::Info::RegisterLuaFunctions(); + Event::Util::RegisterLuaFunctions(); } BOOL APIENTRY DllMain(HMODULE hModule, DWORD reason, LPVOID lpReserved) { diff --git a/src/Offsets.h b/src/Offsets.h index df0bad91..ab67677c 100644 --- a/src/Offsets.h +++ b/src/Offsets.h @@ -184,4 +184,28 @@ enum Offsets { // read it on demand in helpers that run outside a Lua callback (e.g. // RegisterTableFunction during LoadScriptFunctions). VAR_LUA_STATE = 0x00CEEF74, + + // Static event-name table (`char *eventNames[]`) populated by the engine + // at boot via a long sequence of `mov [imm32], imm32` writes around + // 0x0051B030+. Each non-NULL slot is a pointer to a `.data` event-name + // string ("PLAYER_LOGIN", "UNIT_HEALTH", etc., plus Turtle's custom + // additions like "LOTTERY_ITEM_UPDATE", "MINIGAME_UPDATE"). Slots are + // sparse — the static analysis pass found 380 entries via the C7 05 + // pattern across a 533-slot span, with gaps presumably populated by + // other init patterns or left NULL. + // + // Use the MAX_SLOTS bound for the walk and the IS_VALID_STRING_PTR + // range to defend against reading past the real end of the table into + // unrelated `.data` (where slot values would be arbitrary u32s, not + // pointers). + VAR_EVENT_NAME_TABLE = 0x00BE11D8, + EVENT_NAME_TABLE_MAX_SLOTS = 1024, + // Event name strings live in `.data` (mostly clustered around + // 0x00851000..0x00855000); event-name string pointers also reach into + // `.rdata` (starts at 0x007FF000) for some entries. Bound the dereference + // range conservatively to "static-data section" — addresses outside this + // window cannot be valid string pointers in this binary, so we treat + // them as NULL during the walk. + EVENT_TABLE_VALID_PTR_LO = 0x007FF000, + EVENT_TABLE_VALID_PTR_HI = 0x00C00000, }; diff --git a/src/event/Util.cpp b/src/event/Util.cpp new file mode 100644 index 00000000..cb773ff2 --- /dev/null +++ b/src/event/Util.cpp @@ -0,0 +1,57 @@ +// 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 "Util.h" + +#include "Game.h" +#include "Offsets.h" + +#include +#include + +namespace Event::Util { + +static bool IsKnownEventName(const char *needle) { + const char *const *table = + reinterpret_cast(Offsets::VAR_EVENT_NAME_TABLE); + for (int i = 0; i < Offsets::EVENT_NAME_TABLE_MAX_SLOTS; i++) { + const char *slot = table[i]; + const auto addr = reinterpret_cast(slot); + if (addr < Offsets::EVENT_TABLE_VALID_PTR_LO || + addr >= Offsets::EVENT_TABLE_VALID_PTR_HI) + continue; + if (std::strcmp(slot, needle) == 0) + return true; + } + return false; +} + +static int __fastcall Script_IsEventValid(void *L) { + if (Game::Lua::Type(L, 1) != Game::Lua::TYPE_STRING) { + Game::Lua::Error(L, "Usage: C_EventUtils.IsEventValid(eventName)"); + return 0; + } + const char *needle = Game::Lua::ToString(L, 1); + if (needle == nullptr || needle[0] == 0) { + Game::Lua::PushBoolean(L, 0); + return 1; + } + Game::Lua::PushBoolean(L, IsKnownEventName(needle) ? 1 : 0); + return 1; +} + +void RegisterLuaFunctions() { + Game::Lua::RegisterTableFunction("C_EventUtils", "IsEventValid", &Script_IsEventValid); +} + +} // namespace Event::Util diff --git a/src/event/Util.h b/src/event/Util.h new file mode 100644 index 00000000..0df20b39 --- /dev/null +++ b/src/event/Util.h @@ -0,0 +1,20 @@ +// 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 . + +#pragma once + +namespace Event::Util { + +void RegisterLuaFunctions(); + +} // namespace Event::Util