From e2170e96c21c6b2d00a52b9c8dfdfd8af217cff0 Mon Sep 17 00:00:00 2001 From: Diavolo Date: Sun, 26 Jun 2022 21:36:23 +0200 Subject: [PATCH] [Logger] Decouple NetworkLog from the logFile --- src/Components/Modules/Logger.cpp | 36 ++++++++++++++++++------------ src/Components/Modules/Logger.hpp | 2 +- src/Components/Modules/Theatre.cpp | 12 +++++----- src/Game/Functions.cpp | 3 ++- src/Game/Functions.hpp | 7 ++++-- 5 files changed, 36 insertions(+), 24 deletions(-) diff --git a/src/Components/Modules/Logger.cpp b/src/Components/Modules/Logger.cpp index 2a25e37e..2cef7c80 100644 --- a/src/Components/Modules/Logger.cpp +++ b/src/Components/Modules/Logger.cpp @@ -153,22 +153,30 @@ namespace Components } } - __declspec(naked) void Logger::GameLogStub() + void Logger::G_LogPrintfStub(const char* fmt, ...) { - __asm + char string[1024]; + char string2[1024]; + + va_list ap; + va_start(ap, fmt); + vsnprintf_s(string2, _TRUNCATE, fmt, ap); + va_end(ap); + + const auto min = Game::level->time / 1000 / 60; + const auto tens = Game::level->time / 1000 % 60 / 10; + const auto sec = Game::level->time / 1000 % 60 % 10; + + _snprintf_s(string, _TRUNCATE, "%3i:%i%i %s", + min, tens, sec, string2); + + if (Game::level->logFile != nullptr) { - pushad - - push 1 - push [esp + 28h] - call Logger::NetworkLog - add esp, 8h - - popad - - push 4576C0h - retn + Game::FS_Write(string, &string[std::strlen(string) + 1] - &string[1], reinterpret_cast(Game::level->logFile)); } + + // Allow the network log to run even if logFile was not opened + Logger::NetworkLog(string, true); } __declspec(naked) void Logger::PrintMessageStub() @@ -361,7 +369,7 @@ namespace Components Scheduler::Loop(Logger::Frame, Scheduler::Pipeline::SERVER); - Utils::Hook(0x4B0218, Logger::GameLogStub, HOOK_CALL).install()->quick(); + Utils::Hook(Game::G_LogPrintf, Logger::G_LogPrintfStub, HOOK_JUMP).install()->quick(); Utils::Hook(Game::Com_PrintMessage, Logger::PrintMessageStub, HOOK_JUMP).install()->quick(); if (Loader::IsPerformingUnitTests()) diff --git a/src/Components/Modules/Logger.hpp b/src/Components/Modules/Logger.hpp index 5d3b7fa7..88f9f1da 100644 --- a/src/Components/Modules/Logger.hpp +++ b/src/Components/Modules/Logger.hpp @@ -100,7 +100,7 @@ namespace Components static void(*PipeCallback)(const std::string&); static void Frame(); - static void GameLogStub(); + static void G_LogPrintfStub(const char* fmt, ...); static void PrintMessageStub(); static void PrintMessagePipe(const char* data); static void EnqueueMessage(const std::string& message); diff --git a/src/Components/Modules/Theatre.cpp b/src/Components/Modules/Theatre.cpp index 4acc40e4..855a54e9 100644 --- a/src/Components/Modules/Theatre.cpp +++ b/src/Components/Modules/Theatre.cpp @@ -19,7 +19,7 @@ namespace Components void Theatre::RecordGamestateStub() { int sequence = (*Game::serverMessageSequence - 1); - Game::FS_Write(&sequence, 4, *Game::demoFile); + Game::FS_WriteToDemo(&sequence, 4, *Game::demoFile); } void Theatre::StoreBaseline(PBYTE snapshotMsg) @@ -62,10 +62,10 @@ namespace Components int byte8 = 8; char byte0 = 0; - Game::FS_Write(&byte0, 1, *Game::demoFile); - Game::FS_Write(Game::serverMessageSequence, 4, *Game::demoFile); - Game::FS_Write(&fileCompressedSize, 4, *Game::demoFile); - Game::FS_Write(&byte8, 4, *Game::demoFile); + Game::FS_WriteToDemo(&byte0, 1, *Game::demoFile); + Game::FS_WriteToDemo(Game::serverMessageSequence, 4, *Game::demoFile); + Game::FS_WriteToDemo(&fileCompressedSize, 4, *Game::demoFile); + Game::FS_WriteToDemo(&byte8, 4, *Game::demoFile); for (int i = 0; i < compressedSize; i += 1024) { @@ -77,7 +77,7 @@ namespace Components break; } - Game::FS_Write(&cmpData[i], size, *Game::demoFile); + Game::FS_WriteToDemo(&cmpData[i], size, *Game::demoFile); } } diff --git a/src/Game/Functions.cpp b/src/Game/Functions.cpp index 4a4bf52e..1f6db33d 100644 --- a/src/Game/Functions.cpp +++ b/src/Game/Functions.cpp @@ -148,7 +148,8 @@ namespace Game FS_FOpenFileReadForThread_t FS_FOpenFileReadForThread = FS_FOpenFileReadForThread_t(0x643270); FS_FCloseFile_t FS_FCloseFile = FS_FCloseFile_t(0x462000); FS_WriteFile_t FS_WriteFile = FS_WriteFile_t(0x426450); - FS_Write_t FS_Write = FS_Write_t(0x4C06E0); + FS_WriteToDemo_t FS_WriteToDemo = FS_WriteToDemo_t(0x4C06E0); + FS_Write_t FS_Write = FS_Write_t(0x4576C0); FS_Printf_t FS_Printf = FS_Printf_t(0x459320); FS_Read_t FS_Read = FS_Read_t(0x4A04C0); FS_Seek_t FS_Seek = FS_Seek_t(0x4A63D0); diff --git a/src/Game/Functions.hpp b/src/Game/Functions.hpp index fd611c21..9d5c67c3 100644 --- a/src/Game/Functions.hpp +++ b/src/Game/Functions.hpp @@ -365,10 +365,13 @@ namespace Game typedef bool(__cdecl * FS_FileExists_t)(const char* file); extern FS_FileExists_t FS_FileExists; - typedef bool(__cdecl * FS_WriteFile_t)(char* filename, char* folder, void* buffer, int size); + typedef bool(__cdecl * FS_WriteFile_t)(const char* filename, const char* folder, const void* buffer, int size); extern FS_WriteFile_t FS_WriteFile; - typedef int(__cdecl * FS_Write_t)(const void* buffer, size_t size, int file); + typedef int(__cdecl * FS_WriteToDemo_t)(const void* buffer, int size, int file); + extern FS_WriteToDemo_t FS_WriteToDemo; + + typedef int(__cdecl * FS_Write_t)(const void* buffer, int len, int h); extern FS_Write_t FS_Write; typedef int(__cdecl * FS_Printf_t)(int file, const char* fmt, ...);