From 6298a627ce1794e59dcc8a76625862795a6636ed Mon Sep 17 00:00:00 2001 From: Federico Cecchetto Date: Sun, 10 Jul 2022 20:25:21 +0200 Subject: [PATCH] SP Gamelog --- src/component/game_sp_log.cpp | 82 +++++++++++++++++++++++++++++++++++ src/game/game.cpp | 10 +++++ src/game/game.hpp | 2 + src/game/structs.hpp | 60 +++++++++++++++++++++++++ src/game/symbols.hpp | 14 +++++- 5 files changed, 167 insertions(+), 1 deletion(-) create mode 100644 src/component/game_sp_log.cpp diff --git a/src/component/game_sp_log.cpp b/src/component/game_sp_log.cpp new file mode 100644 index 0000000..df3f5d4 --- /dev/null +++ b/src/component/game_sp_log.cpp @@ -0,0 +1,82 @@ +#include +#include "loader/component_loader.hpp" + +#include "game/game.hpp" + +#include + +namespace game_sp_log +{ + namespace + { + // Use these dvars for SP binary only + const game::dvar_t** g_log = reinterpret_cast(0x3023B20); + const game::dvar_t** g_logSync = reinterpret_cast(0x3023B2C); + + void g_init_game_stub() + { + // G_RegisterDvars + utils::hook::invoke(0x7E1C10); + + game::Com_Printf(game::CON_CHANNEL_SERVER, "------- Game Initialization -------\n"); + + const std::string log_file = (*g_log)->current.string; + + if (log_file.empty()) + { + game::Com_Printf(game::CON_CHANNEL_SERVER, "Not logging to disk.\n"); + return; + } + + const auto mode = (*g_logSync)->current.enabled ? game::FS_APPEND_SYNC : game::FS_APPEND; + + game::FS_FOpenFileByMode(log_file.data(), game::logFile, mode); + + if (*game::logFile == 0) + { + game::Com_PrintWarning(game::CON_CHANNEL_SERVER, "WARNING: Couldn't open logfile: %s\n", log_file.data()); + return; + } + + char info[1024]{}; + game::SV_GetServerinfo(info, sizeof(info)); + + game::G_LogPrintf("------------------------------------------------------------\n"); + game::G_LogPrintf("InitGame: %s\n", info); + + } + + void g_shutdown_game_stub(int free_scripts) + { + utils::hook::invoke(0x607700, free_scripts); // G_ShutdownGame + + if (*game::logFile != 0) + { + game::G_LogPrintf("ShutdownGame:\n"); + game::G_LogPrintf("------------------------------------------------------------\n"); + game::FS_FCloseFile(*game::logFile); + *game::logFile = 0; + } + } + + } + + class component final : public component_interface + { + public: + void post_unpack() override + { + if (game::environment::is_mp()) + { + return; + } + + utils::hook::call(0x51E97F, g_init_game_stub); + + utils::hook::call(0x4FA28A, g_shutdown_game_stub); + utils::hook::call(0x57EF4B, g_shutdown_game_stub); + } + }; +} + +REGISTER_COMPONENT(game_sp_log::component) diff --git a/src/game/game.cpp b/src/game/game.cpp index 1cef98c..aab471a 100644 --- a/src/game/game.cpp +++ b/src/game/game.cpp @@ -22,6 +22,16 @@ namespace game } } + void SV_GetServerinfo(char* buffer, int bufferSize) + { + if (bufferSize < 1) + { + Com_Error(ERR_DROP, "\x15SV_GetServerinfo: bufferSize == %i", bufferSize); + } + + I_strncpyz(buffer, Dvar_InfoString(0, 4), bufferSize); + } + void AddRefToValue(scriptInstance_t inst, const VariableValue* value) { AddRefToValue_(inst, value->type, value->u); diff --git a/src/game/game.hpp b/src/game/game.hpp index 48ab1fd..a25a180 100644 --- a/src/game/game.hpp +++ b/src/game/game.hpp @@ -56,6 +56,8 @@ namespace game T* mp_object_; }; + void SV_GetServerinfo(char* buffer, int bufferSize); + void AddRefToValue(scriptInstance_t inst, const VariableValue* value); void RemoveRefToValue(scriptInstance_t inst, const int type, VariableUnion value); diff --git a/src/game/structs.hpp b/src/game/structs.hpp index c026bb9..672b45c 100644 --- a/src/game/structs.hpp +++ b/src/game/structs.hpp @@ -210,6 +210,66 @@ namespace game unsigned int index; }; + enum fsMode_t + { + FS_READ = 0x0, + FS_WRITE = 0x1, + FS_APPEND = 0x2, + FS_APPEND_SYNC = 0x3, + }; + + enum errorParm_t + { + ERR_FATAL = 0x0, + ERR_DROP = 0x1, + ERR_SERVERDISCONNECT = 0x2, + ERR_DISCONNECT = 0x3, + ERR_SCRIPT = 0x4, + ERR_SCRIPT_DROP = 0x5, + ERR_LOCALIZATION = 0x6, + }; + + enum conChannel_t + { + CON_CHANNEL_DONT_FILTER = 0x0, + CON_CHANNEL_ERROR = 0x1, + CON_CHANNEL_GAMENOTIFY = 0x2, + CON_CHANNEL_BOLDGAME = 0x3, + CON_CHANNEL_SUBTITLE = 0x4, + CON_CHANNEL_OBITUARY = 0x5, + CON_CHANNEL_LOGFILEONLY = 0x6, + CON_CHANNEL_CONSOLEONLY = 0x7, + CON_CHANNEL_GFX = 0x8, + CON_CHANNEL_SOUND = 0x9, + CON_CHANNEL_FILES = 0xA, + CON_CHANNEL_DEVGUI = 0xB, + CON_CHANNEL_PROFILE = 0xC, + CON_CHANNEL_UI = 0xD, + CON_CHANNEL_CLIENT = 0xE, + CON_CHANNEL_SERVER = 0xF, + CON_CHANNEL_SYSTEM = 0x10, + CON_CHANNEL_PLAYERWEAP = 0x11, + CON_CHANNEL_AI = 0x12, + CON_CHANNEL_ANIM = 0x13, + CON_CHANNEL_PHYS = 0x14, + CON_CHANNEL_FX = 0x15, + CON_CHANNEL_LEADERBOARDS = 0x16, + CON_CHANNEL_LIVE = 0x17, + CON_CHANNEL_PARSERSCRIPT = 0x18, + CON_CHANNEL_SCRIPT = 0x19, + CON_CHANNEL_SPAWNSYSTEM = 0x1A, + CON_CHANNEL_COOPINFO = 0x1B, + CON_CHANNEL_SERVERDEMO = 0x1C, + CON_CHANNEL_DDL = 0x1D, + CON_CHANNEL_NETWORK = 0x1E, + CON_CHANNEL_SCHEDULER = 0x1F, + CON_FIRST_DEBUG_CHANNEL = 0x1F, + CON_CHANNEL_TASK = 0x20, + CON_CHANNEL_SPU = 0x21, + + CON_BUILTIN_CHANNEL_COUNT = 0x22, + }; + namespace sp { struct ObjectInfo diff --git a/src/game/symbols.hpp b/src/game/symbols.hpp index 6375e15..f13c6a3 100644 --- a/src/game/symbols.hpp +++ b/src/game/symbols.hpp @@ -8,6 +8,8 @@ namespace game WEAK symbol BG_StringHashValue{0x0, 0x0}; + WEAK symbol G_LogPrintf{0x69EA30, 0x5CD250}; + WEAK symbol Cbuf_InsertText{0x0, 0x0}; WEAK symbol Cbuf_AddText{0x49B930, 0x56EF70}; WEAK symbol Cmd_ExecuteSingleCommand{0x619D00, 0x50B470}; @@ -17,16 +19,21 @@ namespace game WEAK symbol ClientUserInfoChanged{0x0, 0x0}; - WEAK symbol Com_Printf{0x566BC0, 0x64C260}; + WEAK symbol Com_Error{0x651D90, 0x627380}; + WEAK symbol Com_Printf{0x43BF30, 0x4126C0}; + WEAK symbol Com_Printf_NoFilter{0x566BC0, 0x64C260}; + WEAK symbol Com_PrintWarning{0x51CE50, 0x54B6A0}; WEAK symbol Dvar_FindVar{0x5AE810, 0x512F70}; WEAK symbol Dvar_GetInt{0x0, 0x0}; WEAK symbol Dvar_RegisterInt{0x0, 0x0}; + WEAK symbol Dvar_InfoString{0x613060, 0x54A8B0}; WEAK symbol DB_FindXAssetHeader{0x0, 0x0}; WEAK symbol I_CleanStr{0x0, 0x0}; + WEAK symbol I_strncpyz{0x5D4D60, 0x5A7140}; WEAK symbol Player_GetMethod{0x0, 0x0}; WEAK symbol Scr_GetCommonFunction{0x0, 0x0}; @@ -107,6 +114,9 @@ namespace game WEAK symbol Sys_GetValue{0x67D4F0, 0x529EB0}; WEAK symbol Sys_Milliseconds{0x0, 0x0}; + WEAK symbol FS_FOpenFileByMode{0x4DD530, 0x40C790}; + WEAK symbol FS_FCloseFile{0x46CAA0, 0x533020}; + WEAK symbol longjmp{0x96B980, 0x9D05C4}; WEAK symbol _setjmp{0x969EAC, 0x9CED5C}; @@ -131,4 +141,6 @@ namespace game WEAK symbol svs_clients{0x0, 0x0}; WEAK symbol sv_cmd_args{0x243D208, 0x355BD88}; + + WEAK symbol logFile{0x1C0417C, 0x3443838}; }