Files
ClassicAPI/src/quest/Data.cpp
T
Brues ad13440743 Item cache: hook response handler instead of racing the engine
`Item:CreateFromItemID(X):ContinueOnItemLoad(cb)` was leaving items
permanently uncached when pfQuest called it at addon-load /
PLAYER_ENTERING_WORLD against an empty WDB. Three intertwined bugs:

1. `GetItemInfoInstant` returned no values for uncached items, so
   `ItemMixin:GetItemID()` returned nil and `AddCallback(nil, cb)`
   crashed with "table index is nil".
2. `DoesItemExistByID` returned false for uncached items, which made
   `IsItemEmpty()` return true and erroneously gated `ContinueOnItemLoad`
   on cache state — modern semantic is "is this a valid ID", independent
   of cache.
3. Our `RequestLoadItemData` path eagerly called `CacheFetch` with our
   own callback at addon-load. That created a cache entry before the
   engine's natural inventory prefetch had a chance to run; the engine
   then saw the entry already existed and skipped its own SMSG, leaving
   the entry permanently pending. `GetItemInfo` retries didn't help —
   subsequent `_GetRecord` calls just appended callbacks to the dead
   list, no new query was sent.

Fixes:

- `GetItemInfoInstant` always pushes the itemID (echoed from input) and
  the remaining 6 fields when cached / nils when not. No cache touch.
- `DoesItemExistByID` returns `itemID > 0`. No cache touch.
- `RequestLoadItemData` tracks itemIDs in a pending set without touching
  the cache. We hook `FUN_ITEMSTATS_CACHE_RESPONSE` (the engine's
  `SMSG_ITEM_QUERY_SINGLE_RESPONSE` handler at `0x0055BDB0`) and sweep
  pending after the engine fills entries — observing the natural
  prefetch instead of racing it. `WorldTick` only handles escalation
  (after ~60 ticks, kick a query for items the engine doesn't prefetch
  itself, e.g. quest rewards) and the hard timeout.

Also fix the truthiness bug in modern (id, success) events:
`Event::Custom::Fire("%d%d", id, success)` was pushing `success` as a
Lua number — both 0 and 1 are truthy, so `if success then` fired the
success branch on failed loads. New `Event::Custom::FireIdSuccess`
pushes `1` for success and `nil` for failure (via the engine's
`lua_pushstring(NULL) → lua_pushnil` tail-jump). `ITEM_DATA_LOAD_RESULT`,
`GET_ITEM_INFO_RECEIVED`, and `QUEST_DATA_LOAD_RESULT` now all use this.
2026-06-04 13:15:59 -05:00

85 lines
3.6 KiB
C++

// 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 <https://www.gnu.org/licenses/>.
#include "Game.h"
#include "event/Custom.h"
#include "quest/Cache.h"
#include <cstdint>
namespace Quest::Data {
static constexpr const char *kQuestDataLoadResult = "QUEST_DATA_LOAD_RESULT";
static const Event::Custom::AutoReserve _reserveQuestDataLoadResult{kQuestDataLoadResult};
// Same `__stdcall(userData, success)` shape as `Item::Data::ItemLoadCallback`.
// The engine's invocation pattern at 0x00562EEB et al. is:
// mov ecx, [entry+0x18] ; userData (= arg4 we passed to _GetRecord)
// push 1 ; success (1/0)
// push ecx ; userData
// call [entry+0x08] ; this callback
// `ret 8` cleans both args. We encode the questID into `userData` at
// request time so the callback knows which quest completed.
static void __stdcall QuestLoadCallback(void *userData, int success) {
const auto questID =
static_cast<int>(reinterpret_cast<uintptr_t>(userData));
Event::Custom::FireIdSuccess(Event::Custom::Lookup(kQuestDataLoadResult),
questID, success != 0);
}
static void RequestAndMaybeNotify(uint32_t questID) {
const bool wasCached = (Quest::Cache::Peek(questID) != nullptr);
void *userData = reinterpret_cast<void *>(static_cast<uintptr_t>(questID));
Quest::Cache::Lookup(questID, reinterpret_cast<void *>(&QuestLoadCallback), userData);
// Cache-hit-already-loaded path: engine returns the record without
// invoking our callback. Fire the event ourselves so addons get the
// notification regardless of cache state, matching modern semantics.
if (wasCached) {
Event::Custom::FireIdSuccess(Event::Custom::Lookup(kQuestDataLoadResult),
static_cast<int>(questID), true);
}
}
static int __fastcall Script_RequestLoadQuestByID(void *L) {
if (!Game::Lua::IsNumber(L, 1)) {
Game::Lua::Error(L, "Usage: C_QuestLog.RequestLoadQuestByID(questID)");
return 0;
}
const int questID = static_cast<int>(Game::Lua::ToNumber(L, 1));
if (questID <= 0)
return 0;
RequestAndMaybeNotify(static_cast<uint32_t>(questID));
return 0;
}
static int __fastcall Script_IsQuestDataCachedByID(void *L) {
const int questID =
Game::Lua::IsNumber(L, 1) ? static_cast<int>(Game::Lua::ToNumber(L, 1)) : 0;
const bool cached =
(questID > 0) && (Quest::Cache::Peek(static_cast<uint32_t>(questID)) != nullptr);
Game::Lua::PushBool(L, cached);
return 1;
}
static void RegisterLuaFunctions() {
Game::Lua::RegisterTableFunction("C_QuestLog", "RequestLoadQuestByID",
&Script_RequestLoadQuestByID);
Game::Lua::RegisterTableFunction("C_QuestLog", "IsQuestDataCachedByID",
&Script_IsQuestDataCachedByID);
// Event name reserved at file scope via `AutoReserve`.
}
static const Game::ModuleAutoRegister _autoreg{&RegisterLuaFunctions};
} // namespace Quest::Data