From bab81a44de8bd5619c2883d2951bc130328208fb Mon Sep 17 00:00:00 2001 From: Diavolo Date: Tue, 21 Feb 2023 11:17:54 +0100 Subject: [PATCH] fix(file_system): small leak --- src/game/dvars.cpp | 4 ++++ src/game/dvars.hpp | 2 ++ src/game/game.cpp | 4 ++++ src/game/game.hpp | 3 +++ src/module/file_system.cpp | 10 ++++++++++ src/module/file_system.hpp | 1 + src/module/gsc/script_loading.cpp | 26 ++++++++++++++++++++++---- src/module/test_clients.cpp | 4 ++-- 8 files changed, 48 insertions(+), 6 deletions(-) diff --git a/src/game/dvars.cpp b/src/game/dvars.cpp index 5c680f9..13a6d85 100644 --- a/src/game/dvars.cpp +++ b/src/game/dvars.cpp @@ -7,6 +7,8 @@ namespace dvars const game::native::dvar_t** com_sv_running; const game::native::dvar_t** sv_maxclients; + const game::native::dvar_t** sv_g_gametype; + const game::native::dvar_t** sv_mapname; const game::native::dvar_t** loc_language; @@ -15,6 +17,8 @@ namespace dvars com_sv_running = reinterpret_cast(SELECT_VALUE(0x1769F50, 0x1CEF588)); sv_maxclients = reinterpret_cast(SELECT_VALUE(0x0, 0x21223C0)); + sv_g_gametype = reinterpret_cast(SELECT_VALUE(0x0, 0x21223F8)); + sv_mapname = reinterpret_cast(SELECT_VALUE(0x47D7C6, 0x58D4784)); loc_language = reinterpret_cast(SELECT_VALUE(0x1BF6938, 0x58D5A90)); } diff --git a/src/game/dvars.hpp b/src/game/dvars.hpp index d5c99a9..d65c8d6 100644 --- a/src/game/dvars.hpp +++ b/src/game/dvars.hpp @@ -5,6 +5,8 @@ namespace dvars extern const game::native::dvar_t** com_sv_running; extern const game::native::dvar_t** sv_maxclients; + extern const game::native::dvar_t** sv_g_gametype; + extern const game::native::dvar_t** sv_mapname; extern const game::native::dvar_t** loc_language; diff --git a/src/game/game.cpp b/src/game/game.cpp index e0de234..ab02c1a 100644 --- a/src/game/game.cpp +++ b/src/game/game.cpp @@ -78,6 +78,8 @@ namespace game Hunk_AllocateTempMemoryHighInternal_t Hunk_AllocateTempMemoryHighInternal; Hunk_FreeTempMemory_t Hunk_FreeTempMemory; + Hunk_UserDestroy_t Hunk_UserDestroy; + VM_Notify_t VM_Notify; BG_NetDataChecksum_t BG_NetDataChecksum; @@ -805,6 +807,8 @@ namespace game native::Hunk_AllocateTempMemoryHighInternal = native::Hunk_AllocateTempMemoryHighInternal_t(SELECT_VALUE(0x517870, 0x5B6C60)); native::Hunk_FreeTempMemory = native::Hunk_FreeTempMemory_t(SELECT_VALUE(0x434A40, 0x5B6F90)); + native::Hunk_UserDestroy = native::Hunk_UserDestroy_t(SELECT_VALUE(0x50FE20, 0x5B7520)); + native::VM_Notify = native::VM_Notify_t(SELECT_VALUE(0x610200, 0x569720)); native::BG_NetDataChecksum = native::BG_NetDataChecksum_t(SELECT_VALUE(0x0, 0x41BB20)); diff --git a/src/game/game.hpp b/src/game/game.hpp index 0cfd313..8b272b3 100644 --- a/src/game/game.hpp +++ b/src/game/game.hpp @@ -173,6 +173,9 @@ namespace game typedef void (*Hunk_FreeTempMemory_t)(void* buf); extern Hunk_FreeTempMemory_t Hunk_FreeTempMemory; + typedef void (*Hunk_UserDestroy_t)(HunkUser* user); + extern Hunk_UserDestroy_t Hunk_UserDestroy; + typedef void (*VM_Notify_t)(unsigned int notifyListOwnerId, unsigned int stringValue, VariableValue* top); extern VM_Notify_t VM_Notify; diff --git a/src/module/file_system.cpp b/src/module/file_system.cpp index 7d882c3..622aba5 100644 --- a/src/module/file_system.cpp +++ b/src/module/file_system.cpp @@ -632,6 +632,16 @@ char** file_system::list_files(const char* path, const char* extension, game::na return game::native::FS_ListFilteredFiles(*game::native::fs_searchpaths, path, extension, nullptr, behavior, numfiles, allocTrackType); } +void file_system::free_file_list(char** list) +{ + if (!list) + { + return; + } + --list; + game::native::Hunk_UserDestroy(reinterpret_cast(*list)); +} + void file_system::post_load() { fs_homepath = reinterpret_cast(SELECT_VALUE(0x1C2B538, 0x59ADD18)); diff --git a/src/module/file_system.hpp b/src/module/file_system.hpp index 1b38793..e3c4607 100644 --- a/src/module/file_system.hpp +++ b/src/module/file_system.hpp @@ -11,4 +11,5 @@ public: static int write(const char* buffer, int len, int h); static char** list_files(const char* path, const char* extension, game::native::FsListBehavior_e behavior, int* numfiles, int allocTrackType); + static void free_file_list(char** list); }; diff --git a/src/module/gsc/script_loading.cpp b/src/module/gsc/script_loading.cpp index d3672d9..a8ff47f 100644 --- a/src/module/gsc/script_loading.cpp +++ b/src/module/gsc/script_loading.cpp @@ -1,6 +1,7 @@ #include #include #include "game/game.hpp" +#include "game/dvars.hpp" #include "script_loading.hpp" @@ -11,6 +12,7 @@ #include #include #include +#include #include @@ -156,19 +158,23 @@ namespace gsc return game::native::DB_IsXAssetDefault(type, name); } - void load_scripts() + void load_scripts_from_folder(const char* dir) { char path[game::native::MAX_OSPATH]{}; + char search_path[game::native::MAX_OSPATH]{}; + + strncpy_s(search_path, dir, _TRUNCATE); + strncat_s(search_path, "/", _TRUNCATE); auto num_files = 0; - auto** files = file_system::list_files("scripts/", "gsc", game::native::FS_LIST_ALL, &num_files, 10); + auto** list = file_system::list_files(search_path, "gsc", game::native::FS_LIST_ALL, &num_files, 10); for (auto i = 0; i < num_files; ++i) { - const auto* script_file = files[i]; + const auto* script_file = list[i]; console::info("Loading script %s...\n", script_file); - const auto len = sprintf_s(path, "%s/%s", "scripts", script_file); + const auto len = sprintf_s(path, "%s/%s", dir, script_file); if (len == -1) { continue; @@ -199,6 +205,18 @@ namespace gsc init_handles[path] = init_handle; } } + + file_system::free_file_list(list); + } + + void load_scripts() + { + // Both SP & MP + load_scripts_from_folder("scripts"); + + // Game specific + const auto* game_dir = game::is_mp() ? "scripts/mp" : "scripts/sp"; + load_scripts_from_folder(game_dir); } void g_scr_load_scripts_mp_stub() diff --git a/src/module/test_clients.cpp b/src/module/test_clients.cpp index e3ab388..b784fc9 100644 --- a/src/module/test_clients.cpp +++ b/src/module/test_clients.cpp @@ -163,8 +163,8 @@ __declspec(naked) void test_clients::reset_reliable_mp() bool test_clients::check_timeouts(const game::native::mp::client_t* client) { - return (!client->bIsTestClient || client->header.state == game::native::clientState_t::CS_ZOMBIE) && - client->header.netchan.remoteAddress.type != game::native::netadrtype_t::NA_LOOPBACK; + return (!client->bIsTestClient || client->header.state == game::native::CS_ZOMBIE) && + client->header.netchan.remoteAddress.type != game::native::NA_LOOPBACK; } __declspec(naked) void test_clients::check_timeouts_stub_mp()