fix(time): centralize engine ms-tick reads in Time::Clock

The engine's OS millisecond counter (FUN_OS_TICKCOUNT_MS, same epoch
GetTime() scales by 0.001) is unsigned and wraps at 2^32 ms. Nine files
each re-derived the reader; several stored the tick into a signed int,
so past 2^31 ms (~24.9 days uptime) every timestamp went negative and
`startTime + duration` landed in the past -- callers saw every cooldown,
aura, and lockout as already elapsed. The same bug shipped three times
(the cooldown by-ID getters and C_LossOfControl).

Add one source of truth -- Time::Clock (src/time/Clock.h) -- with a
uint32 NowMs() and wrap-safe Reached/Remaining/Elapsed helpers, and route
every tick reader through it. No module re-derives the reader or hand-
rolls a `now < deadline` compare (which inverts across the wrap) anymore.

Also fixes the reported cooldown/LoC getters (unsigned out-params,
wrap-safe compares) and three latent instances of the same `now < end`
inversion found while sweeping: totem/Tracker's two timer getters and
aura/Source's cache-eviction pass.

Named Time::Clock, not Time::Tick, because a Time::Tick inner name
shadows the global Tick namespace (the WorldTick hook) from inside any
Time::* file.

Discovered by fuffc in #17.
This commit is contained in:
Brues
2026-08-12 01:58:54 -05:00
parent b497bb7fb0
commit 39d22ac124
16 changed files with 176 additions and 106 deletions
+25 -23
View File
@@ -48,6 +48,7 @@
#include "spell/CrowdControl.h"
#include "spell/Lookup.h"
#include "tick/WorldTick.h"
#include "time/Clock.h"
#include "unit/Identity.h"
#include <cstdint>
@@ -56,11 +57,8 @@ namespace LossOfControl {
namespace {
using TickMs_t = uint32_t(__fastcall *)();
int NowMs() {
return static_cast<int>(
reinterpret_cast<TickMs_t>(Offsets::FUN_OS_TICKCOUNT_MS)());
}
using Time::Clock::NowMs;
using Time::Clock::Reached;
const char *SpellName(const uint8_t *rec) {
const int locale = *reinterpret_cast<int *>(Offsets::VAR_LOCALE_INDEX);
@@ -90,12 +88,14 @@ int SpellSchoolIndex(const uint8_t *rec) {
// ---- School-interrupt lockout state (fed by SMSG_SPELL_COOLDOWN) -----------
// One active lockout per school index (0..6); active while now < endMs.
// One active lockout per school index (0..6); active until `Reached(endMs)`.
// Self-expiring, per-process — a stale entry from before a relog is already
// in the past on the shared uptime clock, so no reset is needed.
// in the past on the shared uptime clock, so no reset is needed. Unsigned,
// like every engine-tick store: the OS counter wraps at 2^32 ms and a signed
// `int` goes negative past 2^31 ms (~24.9 days uptime). See `Time::Clock`.
struct SchoolLock {
int startMs;
int endMs;
uint32_t startMs;
uint32_t endMs;
};
SchoolLock g_schoolLock[Offsets::SPELL_SCHOOL_COUNT] = {};
@@ -137,9 +137,9 @@ void CooldownSub(uint32_t opcode, Net::CDataStore *packet) {
// (ProhibitSpellSchool); single/varied packets are normal cooldowns and
// ignored.
if (count >= 2 && uniform && firstDur > 0 && schoolIdx >= 0) {
const int now = NowMs();
const uint32_t now = NowMs();
g_schoolLock[schoolIdx].startMs = now;
g_schoolLock[schoolIdx].endMs = now + firstDur;
g_schoolLock[schoolIdx].endMs = now + static_cast<uint32_t>(firstDur);
}
}
@@ -152,17 +152,17 @@ struct LocEntry {
int spellID; // 0 = unknown (school interrupt)
const uint8_t *rec; // spell record for name/icon; null for school interrupt
int schoolMask; // lockoutSchool
int startMs; // 0 = unknown
int endMs; // 0 = unknown / no timing
uint32_t startMs; // 0 = unknown
uint32_t endMs; // 0 = unknown / no timing
};
int BuildList(LocEntry *out, int maxOut) {
const int now = NowMs();
const uint32_t now = NowMs();
int n = 0;
// School-interrupt lockouts first.
for (int s = 0; s < Offsets::SPELL_SCHOOL_COUNT && n < maxOut; ++s) {
if (g_schoolLock[s].endMs != 0 && now < g_schoolLock[s].endMs) {
if (g_schoolLock[s].endMs != 0 && !Reached(now, g_schoolLock[s].endMs)) {
LocEntry &e = out[n++];
e.locType = "SCHOOL_INTERRUPT";
e.spellID = 0;
@@ -204,9 +204,9 @@ int BuildList(LocEntry *out, int maxOut) {
uint32_t expMs = 0, durMs = 0;
if (Aura::Source::Get(playerGuid, spellID, &caster, &expMs, &durMs) &&
expMs != 0) {
e.endMs = static_cast<int>(expMs);
e.endMs = expMs;
if (durMs != 0)
e.startMs = static_cast<int>(expMs - durMs);
e.startMs = expMs - durMs;
}
}
}
@@ -302,7 +302,7 @@ int __fastcall Script_GetActiveLossOfControlData(void *L) {
return 0; // nil
const LocEntry &e = entries[index - 1];
const int now = NowMs();
const uint32_t now = NowMs();
Game::Lua::NewTable(L);
Game::Lua::SetFieldString(L, "locType", e.locType);
@@ -314,12 +314,14 @@ int __fastcall Script_GetActiveLossOfControlData(void *L) {
// Timing (seconds, GetTime() epoch). Nullable — leave a field unset (nil)
// when we don't know it, per the modern contract.
if (e.endMs != 0 && now < e.endMs) {
Game::Lua::SetFieldNumber(L, "timeRemaining", (e.endMs - now) * 0.001);
if (e.endMs != 0 && !Reached(now, e.endMs)) {
Game::Lua::SetFieldNumber(L, "timeRemaining",
static_cast<double>(e.endMs - now) * 0.001);
if (e.startMs != 0) {
Game::Lua::SetFieldNumber(L, "startTime", e.startMs * 0.001);
Game::Lua::SetFieldNumber(L, "duration",
(e.endMs - e.startMs) * 0.001);
Game::Lua::SetFieldNumber(L, "startTime",
static_cast<double>(e.startMs) * 0.001);
Game::Lua::SetFieldNumber(
L, "duration", static_cast<double>(e.endMs - e.startMs) * 0.001);
}
}