// 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 .
// `QUEST_ACCEPTED(questLogIndex, questID)` event — fires once per
// new quest entering the local quest log. Polyfills modern WoW's
// event of the same name (added in 3.1.0). Matches the Cata/WotLK
// signature: `(questLogIndex, questID)`, 1-based log index.
//
// Hook target: `FUN_QUEST_LOG_REBUILD` (`0x004DE510`) — the single
// chokepoint the engine uses to refresh the Lua-visible quest log
// from the player's authoritative slot data at `[CGPlayer + 0xE68 +
// 0x28]`. Every state change that adds, removes, or updates a quest
// flows through here. By snapshotting the log before and after the
// original runs, we can compute the delta and fire QUEST_ACCEPTED
// only for genuine additions.
//
// Login suppression: the same function handles the initial bulk-sync
// at character entry, which appears as a single rebuild call adding
// N quests at once. We suppress fires when more than one quest is
// added in a single rebuild (which can only happen on login / reload
// resync — human input speed can't accept multiple quests in the
// same engine tick). A brand-new character's first accept is
// `0 → 1` and fires correctly.
#include "Game.h"
#include "Offsets.h"
#include "event/Custom.h"
#include
#include
namespace Quest::Accepted {
namespace {
constexpr const char *kEventName = "QUEST_ACCEPTED";
const Event::Custom::AutoReserve _reserve{kEventName};
// Walk the live quest log and collect questIDs of real (non-header)
// entries. Headers are 16-byte rows with a non-NULL pointer at +0x8;
// real quests have NULL there and the questID at +0x0.
void SnapshotQuestIDs(std::unordered_set &out) {
out.clear();
const int count = *reinterpret_cast(
static_cast(Offsets::VAR_QUEST_LOG_ENTRY_COUNT));
if (count <= 0)
return;
const auto *entries = reinterpret_cast(
static_cast(Offsets::VAR_QUEST_LOG_ENTRIES));
out.reserve(static_cast(count));
for (int i = 0; i < count; ++i) {
const uint8_t *entry = entries + i * Offsets::OFF_QUEST_LOG_ENTRY_STRIDE;
const void *hdr = *reinterpret_cast(
entry + Offsets::OFF_QUEST_LOG_ENTRY_HEADER_PTR);
if (hdr != nullptr)
continue;
const int questID = *reinterpret_cast(
entry + Offsets::OFF_QUEST_LOG_ENTRY_QUEST_ID);
if (questID > 0)
out.insert(questID);
}
}
} // namespace
using QuestLogRebuild_t = void(__fastcall *)(int param_1);
QuestLogRebuild_t QuestLogRebuild_o = nullptr;
void __fastcall QuestLogRebuild_h(int param_1) {
// `param_1 == 0` is the engine's "no-op rebuild" signal — just
// refires QUEST_LOG_UPDATE without touching the entry array.
// No diff possible, no accept happening, pass through.
if (param_1 == 0) {
QuestLogRebuild_o(param_1);
return;
}
std::unordered_set pre;
SnapshotQuestIDs(pre);
QuestLogRebuild_o(param_1);
// Walk the post-state in index order so the fired event's
// `questLogIndex` matches the 1-based slot the engine just
// assigned. Skip questIDs already in `pre`.
const int count = *reinterpret_cast(
static_cast(Offsets::VAR_QUEST_LOG_ENTRY_COUNT));
if (count <= 0)
return;
const auto *entries = reinterpret_cast(
static_cast(Offsets::VAR_QUEST_LOG_ENTRIES));
int newCount = 0;
int firstNewIdx = 0;
int firstNewID = 0;
for (int i = 0; i < count; ++i) {
const uint8_t *entry = entries + i * Offsets::OFF_QUEST_LOG_ENTRY_STRIDE;
const void *hdr = *reinterpret_cast(
entry + Offsets::OFF_QUEST_LOG_ENTRY_HEADER_PTR);
if (hdr != nullptr)
continue;
const int questID = *reinterpret_cast(
entry + Offsets::OFF_QUEST_LOG_ENTRY_QUEST_ID);
if (questID <= 0)
continue;
if (pre.count(questID) != 0)
continue;
++newCount;
if (newCount == 1) {
firstNewIdx = i + 1;
firstNewID = questID;
}
}
// Single addition → user-driven accept. Multiple → bulk resync
// (login or post-reload). Modern WoW's QUEST_ACCEPTED doesn't
// fire on initial sync either; we match that.
if (newCount == 1) {
const int slot = Event::Custom::Lookup(kEventName);
if (slot >= 0)
Event::Custom::Fire(slot, "%d%d", firstNewIdx, firstNewID);
}
}
static const Game::HookAutoRegister _hookreg{
Offsets::FUN_QUEST_LOG_REBUILD,
reinterpret_cast(&QuestLogRebuild_h),
reinterpret_cast(&QuestLogRebuild_o)};
} // namespace Quest::Accepted