From 60147989bb1c085d6fded7c2175428ff38fc897a Mon Sep 17 00:00:00 2001 From: Brues <5278969+brues-code@users.noreply.github.com> Date: Sat, 16 May 2026 22:04:57 -0500 Subject: [PATCH] Collapse Event::Custom::Fire_* into one variadic template MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The six per-arity helpers (Fire_None / Fire_D / Fire_DD / Fire_DDD / Fire_SD / Fire_S) all wrapped the same FUN_FIRE_EVENT engine call with the same eventID<0 early-out — only the format string and arg list differed. Collapsing into a single template Fire(eventID, fmt, args...) keeps the printf-style format at the call site and frees us from adding a new per-arity overload for each new event shape. --- src/bag/UpdateDelayed.cpp | 4 +-- src/equipmentset/Api.cpp | 6 ++-- src/equipmentset/Data.cpp | 4 +-- src/event/Custom.cpp | 52 -------------------------------- src/event/Custom.h | 53 ++++++++++++++++----------------- src/faction/StandingChanged.cpp | 4 +-- src/input/GlobalMouse.cpp | 4 +-- src/input/Modifier.cpp | 4 +-- src/item/Data.cpp | 4 +-- src/player/InputEvents.cpp | 4 +-- src/quest/Data.cpp | 6 ++-- 11 files changed, 46 insertions(+), 99 deletions(-) diff --git a/src/bag/UpdateDelayed.cpp b/src/bag/UpdateDelayed.cpp index d2ef9677..5c99700f 100644 --- a/src/bag/UpdateDelayed.cpp +++ b/src/bag/UpdateDelayed.cpp @@ -1,4 +1,4 @@ -// This file is part of ClassicAPI. +// 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 @@ -100,7 +100,7 @@ void OnWorldTick() { g_pending = false; const int slot = Event::Custom::Lookup(kEventName); if (slot >= 0) - Event::Custom::Fire_None(slot); + Event::Custom::Fire(slot, ""); } static const Game::HookAutoRegister _hookSlotDiff{ diff --git a/src/equipmentset/Api.cpp b/src/equipmentset/Api.cpp index ed385d6c..80c995a4 100644 --- a/src/equipmentset/Api.cpp +++ b/src/equipmentset/Api.cpp @@ -377,7 +377,7 @@ int __fastcall Script_UseEquipmentSet(void *L) { if (s == nullptr) { const int evt = Event::Custom::Lookup(kSwapFinishedEvent); if (evt >= 0) - Event::Custom::Fire_DD(evt, 0, static_cast(setID)); + Event::Custom::Fire(evt, "%d%d", 0, static_cast(setID)); Game::Lua::PushBoolean(L, 0); return 1; } @@ -387,7 +387,7 @@ int __fastcall Script_UseEquipmentSet(void *L) { // visuals. const int pendingEvt = Event::Custom::Lookup(kSwapPendingEvent); if (pendingEvt >= 0) - Event::Custom::Fire_D(pendingEvt, static_cast(setID)); + Event::Custom::Fire(pendingEvt, "%d", static_cast(setID)); // If the cursor is holding anything, return it to its source slot // and unlock it server-side BEFORE our swap chain runs. The @@ -441,7 +441,7 @@ int __fastcall Script_UseEquipmentSet(void *L) { const int evt = Event::Custom::Lookup(kSwapFinishedEvent); if (evt >= 0) - Event::Custom::Fire_DD(evt, 1, static_cast(setID)); + Event::Custom::Fire(evt, "%d%d", 1, static_cast(setID)); Game::Lua::PushBoolean(L, 1); return 1; diff --git a/src/equipmentset/Data.cpp b/src/equipmentset/Data.cpp index 9301f6ed..9629e6e7 100644 --- a/src/equipmentset/Data.cpp +++ b/src/equipmentset/Data.cpp @@ -1,4 +1,4 @@ -// This file is part of ClassicAPI. +// 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 @@ -74,7 +74,7 @@ uint32_t NextAvailableID() { void FireChanged() { const int slot = Event::Custom::Lookup(kEventName); if (slot >= 0) - Event::Custom::Fire_None(slot); + Event::Custom::Fire(slot, ""); } void Persist() { diff --git a/src/event/Custom.cpp b/src/event/Custom.cpp index 8cf561cd..ee50635c 100644 --- a/src/event/Custom.cpp +++ b/src/event/Custom.cpp @@ -120,58 +120,6 @@ void PrepareForReload() { g_writesEnabled = false; } -void Fire_None(int eventID) { - if (eventID < 0) - return; - using FireEvent_None_t = void(__cdecl *)(int eventID, const char *format); - auto fn = reinterpret_cast(Offsets::FUN_FIRE_EVENT); - fn(eventID, ""); -} - -void Fire_D(int eventID, int arg1) { - if (eventID < 0) - return; - using FireEvent_D_t = void(__cdecl *)(int eventID, const char *format, int a); - auto fn = reinterpret_cast(Offsets::FUN_FIRE_EVENT); - fn(eventID, "%d", arg1); -} - -void Fire_DD(int eventID, int arg1, int arg2) { - if (eventID < 0) - return; - using FireEvent_DD_t = void(__cdecl *)(int eventID, const char *format, - int a, int b); - auto fn = reinterpret_cast(Offsets::FUN_FIRE_EVENT); - fn(eventID, "%d%d", arg1, arg2); -} - -void Fire_DDD(int eventID, int arg1, int arg2, int arg3) { - if (eventID < 0) - return; - using FireEvent_DDD_t = void(__cdecl *)(int eventID, const char *format, - int a, int b, int c); - auto fn = reinterpret_cast(Offsets::FUN_FIRE_EVENT); - fn(eventID, "%d%d%d", arg1, arg2, arg3); -} - -void Fire_SD(int eventID, const char *arg1, int arg2) { - if (eventID < 0) - return; - using FireEvent_SD_t = void(__cdecl *)(int eventID, const char *format, - const char *a, int b); - auto fn = reinterpret_cast(Offsets::FUN_FIRE_EVENT); - fn(eventID, "%s%d", arg1, arg2); -} - -void Fire_S(int eventID, const char *arg1) { - if (eventID < 0) - return; - using FireEvent_S_t = void(__cdecl *)(int eventID, const char *format, - const char *a); - auto fn = reinterpret_cast(Offsets::FUN_FIRE_EVENT); - fn(eventID, "%s", arg1); -} - // Diagnostic Lua functions — registered via the auto-register pattern // at the bottom of this file. Not on any hot path. namespace { diff --git a/src/event/Custom.h b/src/event/Custom.h index 1f9a9ded..d1182c4c 100644 --- a/src/event/Custom.h +++ b/src/event/Custom.h @@ -13,6 +13,8 @@ #pragma once +#include "Offsets.h" + namespace Event::Custom { // Reserve an event name to be claimed in the engine's event table at @@ -47,33 +49,30 @@ struct AutoReserve { // so call this at fire time rather than caching the value. int Lookup(const char *name); -// Dispatches an event with no payload (empty format string). Used by -// `EQUIPMENT_SETS_CHANGED` and other notifications whose only signal -// is "something in this domain changed — go re-read". -void Fire_None(int eventID); - -// Dispatches an event with one int arg (format `"%d"`). Used by -// `EQUIPMENT_SWAP_PENDING(setID)`. -void Fire_D(int eventID, int arg1); - -// Dispatches an event with two int args (format `"%d%d"`). Pass -// booleans as 0/1 since the engine has no native bool format code. -void Fire_DD(int eventID, int arg1, int arg2); - -// Three-int variant — `(eventID, %d%d%d, a, b, c)`. Used by -// `FACTION_STANDING_CHANGED(factionID, newStanding, repGained)`. -void Fire_DDD(int eventID, int arg1, int arg2, int arg3); - -// Dispatches with `(string, int)` — used by `MODIFIER_STATE_CHANGED` -// for `(keyName, down)` payloads. `arg1` must outlive the call (the -// engine doesn't copy strings out of varargs); for compile-time -// literals this is automatic. -void Fire_SD(int eventID, const char *arg1, int arg2); - -// Single string arg (format `"%s"`). Same lifetime caveat as -// `Fire_SD` — used by `GLOBAL_MOUSE_DOWN` / `GLOBAL_MOUSE_UP` for -// `(button)` payloads. -void Fire_S(int eventID, const char *arg1); +// Dispatches a custom event via the engine's printf-style event +// dispatcher at `FUN_FIRE_EVENT` (`0x00703F50`). `format` is a +// concatenation of `%d` (int), `%u` (uint), `%f` (double), `%s` +// (const char *) tokens — one per payload arg, no separators or +// literal text. The engine has no `%b` for booleans — pass `%d` +// with `0` / `1`. String args must outlive the call (engine doesn't +// copy them out of varargs); compile-time literals are fine. +// +// Examples: +// Fire(slot, ""); // EQUIPMENT_SETS_CHANGED (no payload) +// Fire(slot, "%d", setID); // EQUIPMENT_SWAP_PENDING(setID) +// Fire(slot, "%d%d", itemID, success); // ITEM_DATA_LOAD_RESULT(id, ok) +// Fire(slot, "%s%d", keyName, down); // MODIFIER_STATE_CHANGED(key, down) +// +// No-op for `eventID < 0`, which lets callers cheaply guard on the +// `Lookup()` result without an explicit if. +template +inline void Fire(int eventID, const char *format, Args... args) { + if (eventID < 0) + return; + using FireEventFn_t = void(__cdecl *)(int eventID, const char *format, ...); + auto fn = reinterpret_cast(Offsets::FUN_FIRE_EVENT); + fn(eventID, format, args...); +} // Internal: try to claim a slot for every reservation that's still // unclaimed (`slot < 0`). Called from the `Frame::RegisterEvent` hook diff --git a/src/faction/StandingChanged.cpp b/src/faction/StandingChanged.cpp index 3384a431..7402b6aa 100644 --- a/src/faction/StandingChanged.cpp +++ b/src/faction/StandingChanged.cpp @@ -1,4 +1,4 @@ -// This file is part of ClassicAPI. +// 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 @@ -100,7 +100,7 @@ void __fastcall FireNotify_h(int factionID, int delta) { const int slot = Event::Custom::Lookup(kEventName); if (slot >= 0) - Event::Custom::Fire_DDD(slot, factionID, newStanding, delta); + Event::Custom::Fire(slot, "%d%d%d", factionID, newStanding, delta); g_last = {}; } diff --git a/src/input/GlobalMouse.cpp b/src/input/GlobalMouse.cpp index 99992d10..7ce087e3 100644 --- a/src/input/GlobalMouse.cpp +++ b/src/input/GlobalMouse.cpp @@ -1,4 +1,4 @@ -// This file is part of ClassicAPI. +// 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 @@ -82,7 +82,7 @@ void OnWorldTick() { g_prev[i] = effectiveDown; const int slot = Event::Custom::Lookup(effectiveDown ? kDownEvent : kUpEvent); if (slot >= 0) - Event::Custom::Fire_S(slot, kButtons[i].name); + Event::Custom::Fire(slot, "%s", kButtons[i].name); } } diff --git a/src/input/Modifier.cpp b/src/input/Modifier.cpp index a8410a6b..fc9ea0dc 100644 --- a/src/input/Modifier.cpp +++ b/src/input/Modifier.cpp @@ -1,4 +1,4 @@ -// This file is part of ClassicAPI. +// 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 @@ -147,7 +147,7 @@ void ProcessKeyMessage(UINT msg, WPARAM wParam, LPARAM lParam) { // and the index may shift. const int slot = Event::Custom::Lookup(kModifierStateChanged); if (slot >= 0) - Event::Custom::Fire_SD(slot, kKeyName[bitIdx], static_cast(nowDown)); + Event::Custom::Fire(slot, "%s%d", kKeyName[bitIdx], static_cast(nowDown)); } LRESULT CALLBACK GetMsgHook(int code, WPARAM wParam, LPARAM lParam) { diff --git a/src/item/Data.cpp b/src/item/Data.cpp index 0b4d2d64..d4f73590 100644 --- a/src/item/Data.cpp +++ b/src/item/Data.cpp @@ -55,13 +55,13 @@ static const Event::Custom::AutoReserve _reserveGetItemInfoReceived{kGetItemInfo static void FireGetItemInfoReceived(int itemID, int success) { const int slot = Event::Custom::Lookup(kGetItemInfoReceived); if (slot >= 0) - Event::Custom::Fire_DD(slot, itemID, success); + Event::Custom::Fire(slot, "%d%d", itemID, success); } static void FireItemDataLoadResult(int itemID, int success) { const int slot = Event::Custom::Lookup(kItemDataLoadResult); if (slot >= 0) - Event::Custom::Fire_DD(slot, itemID, success); + Event::Custom::Fire(slot, "%d%d", itemID, success); } // Engine callback for **implicit** cache fills (transparent warmup diff --git a/src/player/InputEvents.cpp b/src/player/InputEvents.cpp index c2f5143c..9da26061 100644 --- a/src/player/InputEvents.cpp +++ b/src/player/InputEvents.cpp @@ -1,4 +1,4 @@ -// This file is part of ClassicAPI. +// 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 @@ -91,7 +91,7 @@ void FireTransition(bool now, bool &prev, const char *startedName, prev = now; const int slot = Event::Custom::Lookup(now ? startedName : stoppedName); if (slot >= 0) - Event::Custom::Fire_None(slot); + Event::Custom::Fire(slot, ""); } void OnWorldTick() { diff --git a/src/quest/Data.cpp b/src/quest/Data.cpp index 24b01588..1fbe7fcc 100644 --- a/src/quest/Data.cpp +++ b/src/quest/Data.cpp @@ -1,4 +1,4 @@ -// This file is part of ClassicAPI. +// 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 @@ -36,7 +36,7 @@ static void __stdcall QuestLoadCallback(void *userData, int success) { return; const auto questID = static_cast(reinterpret_cast(userData)); - Event::Custom::Fire_DD(slot, questID, static_cast(success != 0)); + Event::Custom::Fire(slot, "%d%d", questID, static_cast(success != 0)); } static void RequestAndMaybeNotify(uint32_t questID) { @@ -49,7 +49,7 @@ static void RequestAndMaybeNotify(uint32_t questID) { if (wasCached) { const int slot = Event::Custom::Lookup(kQuestDataLoadResult); if (slot >= 0) - Event::Custom::Fire_DD(slot, static_cast(questID), 1); + Event::Custom::Fire(slot, "%d%d", static_cast(questID), 1); } }