diff --git a/src/Offsets.h b/src/Offsets.h index e1c416c7..b9278b5f 100644 --- a/src/Offsets.h +++ b/src/Offsets.h @@ -2113,6 +2113,25 @@ enum Offsets { // own text is empty), as 4.3.4 calls UnitCombatLogSpellFail. Returns for a // null player or spell (disassembly, 0x0062C37C-0x0062C38E). FUN_COMBAT_LOG_SPELL_FAIL = 0x0062C360, + // LoggingCombat's flag (s_loggingFlags[1]; [0] at 0x00B4FDB4 is chat): + // nonzero while the combat log is written to a file. Cleared by the chat + // teardown (0x00498700). Disassembled, 0x0049FE70 / 0x0049B1D3. + VAR_COMBAT_LOGGING = 0x00B4FDB8, + // Storm SLogCreate — `bool __stdcall(const char *path, uint32_t flags, + // uint32_t *log)`. Flag 4 appends; the file is opened (and its folders + // made) on the first write. LoggingCombat opens Logs\WoWCombatLog.txt with + // it (0x0049FEA2). Storm closes every open log at exit (0x0065A4B0). + // benilla-re storm/log. + FUN_SLOG_CREATE = 0x0065A0C0, + // Storm SLogWrite — `void __cdecl(uint32_t log, const char *format, ...)`: + // one line, prefixed with "M/D HH:MM:SS.mmm " and ended with "\r\n". The + // combat log's own line writer (0x0049B1E8) calls it with "%s". + // benilla-re storm/log. + FUN_SLOG_WRITE = 0x0065ACE0, + // Storm SLogClose — `void __stdcall(uint32_t log)`: writes the buffered + // lines to the file, flushes and closes it. The chat teardown closes the + // chat and combat logs with it (0x0049871B). benilla-re storm/log. + FUN_SLOG_CLOSE = 0x00659EC0, // A unit's death — `void __fastcall(CGUnit *unit)`: writes the death // line (0x0062C160), shows the pending XP gain, plays the death animation // and sound. Its one caller is CGUnit_C::OnDied (0x006058AE), from the diff --git a/src/combatlog/Entry.cpp b/src/combatlog/Entry.cpp index f6799b41..13481567 100644 --- a/src/combatlog/Entry.cpp +++ b/src/combatlog/Entry.cpp @@ -35,6 +35,7 @@ #include "combatlog/Entry.h" #include "Game.h" +#include "combatlog/File.h" #include "combatlog/History.h" #include "Offsets.h" #include "event/Custom.h" @@ -405,9 +406,7 @@ int Push(void *L, const Entry &e) { PushString(L, name); else PushNil(L); - PushNumber(L, spell != nullptr - ? SchoolMask(Game::Read(spell, Offsets::OFF_SPELL_RECORD_SCHOOL)) - : e.spellSchool); + PushNumber(L, spell != nullptr ? SpellSchoolMask(spell) : e.spellSchool); count += 3; } if (e.info & INFO_COUNT) { @@ -419,14 +418,12 @@ int Push(void *L, const Entry &e) { ++count; } if (e.info & INFO_MISS) { - const int miss = e.v[0]; - PushString(L, (miss >= 0 && miss < MISS_COUNT) ? kMissNames[miss] : kMissNames[0]); + PushString(L, MissName(e.v[0])); PushFlag(L, e.offHand); count += 2; } if (e.info & INFO_ENVIRONMENTAL) { - const int type = e.v[0]; - PushString(L, (type >= 0 && type < 6) ? kEnvironmentalNames[type] : kEnvironmentalNames[0]); + PushString(L, EnvironmentalName(e.v[0])); ++count; } if (e.info & INFO_DAMAGE) { @@ -488,9 +485,7 @@ int Push(void *L, const Entry &e) { PushString(L, name); else PushNil(L); - PushNumber(L, extra != nullptr ? SchoolMask(Game::Read( - extra, Offsets::OFF_SPELL_RECORD_SCHOOL)) - : 0); + PushNumber(L, SpellSchoolMask(extra)); count += 3; } if (e.info & INFO_ITEM) { @@ -581,6 +576,14 @@ const char *Name(int event) { return (event >= 0 && event < EVENT_COUNT) ? kEventNames[event] : ""; } +const char *MissName(int missType) { + return (missType >= 0 && missType < MISS_COUNT) ? kMissNames[missType] : kMissNames[0]; +} + +const char *EnvironmentalName(int type) { + return (type >= 0 && type < 6) ? kEnvironmentalNames[type] : kEnvironmentalNames[0]; +} + int PushEvent(void *L, const Entry &entry) { return Push(L, entry); } const uint8_t *SpellRecord(int spellId) { return Spell::Lookup::RecordForID(spellId); } @@ -608,6 +611,12 @@ bool Ranged(const uint8_t *spell) { uint32_t SchoolMask(uint32_t school) { return school < kSchoolCount ? 1u << school : 0; } +uint32_t SpellSchoolMask(const uint8_t *spell) { + return spell != nullptr + ? SchoolMask(Game::Read(spell, Offsets::OFF_SPELL_RECORD_SCHOOL)) + : 0; +} + bool HasAuraEffect(const uint8_t *spell, int auraName) { if (spell == nullptr) return false; @@ -700,6 +709,7 @@ void Queue(Entry &e) { g_startTimeMs = e.timeMs; } History::Record(e); + File::Write(e); Send(e); } diff --git a/src/combatlog/Entry.h b/src/combatlog/Entry.h index 6665963e..6f2c4933 100644 --- a/src/combatlog/Entry.h +++ b/src/combatlog/Entry.h @@ -72,6 +72,12 @@ enum Subevent : int { const char *Name(int event); +// The INFO_MISS value as 4.3.4's g_spellMissStrings names it. +const char *MissName(int missType); + +// The INFO_ENVIRONMENTAL value as 4.3.4's s_environmentalDamageTypes names it. +const char *EnvironmentalName(int type); + // 4.3.4's m_infoFlags: which suffix blocks the event carries, pushed in // this order after the spell prefix. enum Info : uint32_t { @@ -158,6 +164,9 @@ bool Ranged(const uint8_t *spell); // A school index (Spell.dbc School, the packets' school) as a school mask. uint32_t SchoolMask(uint32_t school); +// The spell's school as a mask (4.3.4's SpellRec m_schoolMask), 0 for nullptr. +uint32_t SpellSchoolMask(const uint8_t *spell); + // Whether one of the spell's three effects applies the aura `auraName` // (EffectApplyAuraName). bool HasAuraEffect(const uint8_t *spell, int auraName); diff --git a/src/combatlog/File.cpp b/src/combatlog/File.cpp new file mode 100644 index 00000000..cd7a37dc --- /dev/null +++ b/src/combatlog/File.cpp @@ -0,0 +1,212 @@ +// This file is part of ClassicAPI. +// +// ClassicAPI is free software: you can redistribute it and/or modify it under the terms +// of the GNU 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 General Public License for more details. +// +// You should have received a copy of the GNU General Public License along with +// ClassicAPI. If not, see . + +// The combat log file — 4.3.4's CombatLogEntry::PackEvent line, written as +// SendCombatLogEvents writes it while LoggingCombat is on. +// +// The engine pieces: +// - the switch is LoggingCombat's own flag, so /combatlog and +// LoggingCombat() turn both files on and off, and logging out (the chat +// teardown in CGGameUI::ShutdownGame, 0x00498700, clears the flag) stops +// both; +// - each time the flag turns on, a new file starts, named as retail names +// it: Logs\WoWCombatLog-MMDDYY_HHMMSS.txt, the local time logging started. +// 1.12's own Logs\WoWCombatLog.txt keeps its chat-text lines, which 1.12 +// log readers parse; +// - the file is a Storm log, opened as LoggingCombat opens its own (append) +// and written with SLogWrite (which stamps "M/D HH:MM:SS.mmm " on each +// line, as in 4.3.4's file). When the flag turns off it is closed, which +// writes its buffered lines. Storm closes what is still open at exit; +// - the line is PackEvent's: no hideCaster, flags as hex, the spell school +// as a hex mask (the extra spell's in decimal), damage values as numbers, +// flags as 1 or nil, no isOffHand and no alternate power type. +// +// The flag is read once a frame (FrameTick, which also runs on the glue +// screen, so a logout's cleared flag is seen there) and before each line. +// LoggingCombat itself is not hooked: other DLLs add their own logs to it. + +#include "combatlog/File.h" + +#include "Game.h" +#include "Offsets.h" +#include "guid/Guid.h" +#include "tick/FrameTick.h" + +#include +#include +#include + +namespace CombatLog::File { + +namespace { + +constexpr uint32_t kAppend = 4; // SLogCreate: open at the end of the file + +uint32_t g_log = 0; + +bool Logging() { return Game::Read(Offsets::VAR_COMBAT_LOGGING) != 0; } + +// A new log named for the local time now. True when it is open. +bool Open() { + const std::time_t now = std::time(nullptr); + std::tm local{}; + if (localtime_s(&local, &now) != 0) + return false; + char path[64]; + std::strftime(path, sizeof path, "Logs\\WoWCombatLog-%m%d%y_%H%M%S.txt", &local); + using Create_t = int(__stdcall *)(const char *path, uint32_t flags, uint32_t *log); + const auto create = reinterpret_cast(static_cast(Offsets::FUN_SLOG_CREATE)); + if (create(path, kAppend, &g_log) == 0) + g_log = 0; + return g_log != 0; +} + +void Close() { + using Close_t = void(__stdcall *)(uint32_t log); + reinterpret_cast(static_cast(Offsets::FUN_SLOG_CLOSE))(g_log); + g_log = 0; +} + +// Follows the flag: a new file when it turns on, the file closed when it +// turns off. +void OnFrame() { + const bool logging = Logging(); + if (logging && g_log == 0) + Open(); + else if (!logging && g_log != 0) + Close(); +} + +const Tick::FrameTick::AutoSubscribe _frame{&OnFrame}; + +// PackString +void Plain(std::string &line, const char *text) { + line += ','; + line += text; +} + +// PackQuotedString, or PackString "nil" when there is no text. +void Quoted(std::string &line, const char *text) { + if (text == nullptr) { + Plain(line, "nil"); + return; + } + line += ",\""; + for (const char *c = text; *c != '\0'; ++c) { + if (*c == '"' || *c == '\\') + line += '\\'; + line += *c; + } + line += '"'; +} + +// PackHex +void Hex(std::string &line, uint32_t value) { + char text[16]; + std::snprintf(text, sizeof text, ",0x%x", value); + line += text; +} + +void Number(std::string &line, int value) { + char text[16]; + std::snprintf(text, sizeof text, ",%d", value); + line += text; +} + +// PackBool +void Flag(std::string &line, bool value) { line += value ? ",1" : ",nil"; } + +void Unit(std::string &line, uint64_t guid, bool named, const std::string &name, uint32_t flags, + uint32_t raidFlags) { + char text[Guid::STRING_SIZE]; + line += ','; + line += Guid::FormatAsString(guid, text, sizeof text); + Quoted(line, named ? name.c_str() : nullptr); + Hex(line, flags); + Hex(line, raidFlags); +} + +// CombatLogEntry::PackEvent, the blocks 1.12 fills. +std::string Pack(const Entry &e) { + std::string line = Name(e.event); + Unit(line, e.source, e.sourceNamed, e.sourceName, e.sourceFlags, e.sourceRaidFlags); + Unit(line, e.dest, e.destNamed, e.destName, e.destFlags, e.destRaidFlags); + if (e.spellPrefix) { + const uint8_t *spell = SpellRecord(e.spellId); + Number(line, e.spellId); + Quoted(line, SpellName(spell)); + Hex(line, spell != nullptr ? SpellSchoolMask(spell) : e.spellSchool); + } + if (e.info & INFO_COUNT) + Number(line, e.v[0]); + if (e.info & INFO_TEXT) + Quoted(line, e.text.c_str()); + if (e.info & INFO_MISS) + Plain(line, MissName(e.v[0])); + if (e.info & INFO_ENVIRONMENTAL) + Plain(line, EnvironmentalName(e.v[0])); + if (e.info & INFO_DAMAGE) { + for (const int slot : {1, 2, 3, 5, 6, 4}) + Number(line, e.v[slot]); + Flag(line, e.critical); + Flag(line, e.glancing); + Flag(line, e.crushing); + } + if (e.info & INFO_MISS_ABSORBED) + Number(line, e.v[4]); + if (e.info & INFO_MISS_RESISTED) + Number(line, e.v[5]); + if (e.info & INFO_MISS_BLOCKED) + Number(line, e.v[6]); + if (e.info & INFO_HEAL) { + for (int i = 0; i < 3; ++i) + Number(line, e.v[i]); + Flag(line, e.critical); + } + if (e.info & INFO_ENERGIZE) { + for (int i = 0; i < 2; ++i) + Number(line, e.v[i]); + } + if (e.info & INFO_DRAIN) { + for (int i = 0; i < 3; ++i) + Number(line, e.v[i]); + } + if (e.info & INFO_EXTRA_SPELL) { + const uint8_t *extra = SpellRecord(e.v[0]); + Number(line, e.v[0]); + Quoted(line, SpellName(extra)); + Number(line, static_cast(SpellSchoolMask(extra))); + } + if (e.info & INFO_ITEM) { + Number(line, e.v[0]); + Quoted(line, e.itemNamed ? e.itemName.c_str() : nullptr); + } + if (e.info & INFO_AURA_TYPE) + Plain(line, e.harmful ? "DEBUFF" : "BUFF"); + if (e.info & INFO_AURA_AMOUNT) + Number(line, e.v[2]); + return line; +} + +} // namespace + +void Write(const Entry &entry) { + // A line in the frame logging turned on comes before the frame's check. + if (!Logging() || (g_log == 0 && !Open())) + return; + using Write_t = void(__cdecl *)(uint32_t log, const char *format, ...); + reinterpret_cast(static_cast(Offsets::FUN_SLOG_WRITE))( + g_log, "%s", Pack(entry).c_str()); +} + +} // namespace CombatLog::File diff --git a/src/combatlog/File.h b/src/combatlog/File.h new file mode 100644 index 00000000..ce481bbe --- /dev/null +++ b/src/combatlog/File.h @@ -0,0 +1,29 @@ +// This file is part of ClassicAPI. +// +// ClassicAPI is free software: you can redistribute it and/or modify it under the terms +// of the GNU 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 General Public License for more details. +// +// You should have received a copy of the GNU General Public License along with +// ClassicAPI. If not, see . + +#pragma once + +// The combat log file — 4.3.4's WoWCombatLog.txt line, written while +// LoggingCombat is on to a file per logging session named as retail names it +// (Logs\WoWCombatLog-MMDDYY_HHMMSS.txt). 1.12's own WoWCombatLog.txt keeps +// its chat-text lines. + +#include "combatlog/Entry.h" + +namespace CombatLog::File { + +// SendCombatLogEvents' write: CombatLogEntry::PackEvent's line, through +// CGChat::AddCombatMessage's SLogWrite, while combat logging is on. +void Write(const Entry &entry); + +} // namespace CombatLog::File