From ad9738de25a45d194c75bccf8b110db82b63ea33 Mon Sep 17 00:00:00 2001 From: cat_or_not <41955154+catornot@users.noreply.github.com> Date: Wed, 2 Aug 2023 00:16:46 -0400 Subject: [PATCH 1/6] add a safeguard to map command --- NorthstarDLL/util/printmaps.cpp | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/NorthstarDLL/util/printmaps.cpp b/NorthstarDLL/util/printmaps.cpp index dd825bff3..3f62cd485 100644 --- a/NorthstarDLL/util/printmaps.cpp +++ b/NorthstarDLL/util/printmaps.cpp @@ -31,6 +31,8 @@ struct MapVPKInfo // our current list of maps in the game std::vector vMapList; +FnCommandCallback_t OrignalMapCommand = NULL; + void RefreshMapList() { // Only update the maps list every 10 seconds max to we avoid constantly reading fs @@ -182,10 +184,30 @@ void ConCommand_maps(const CCommand& args) spdlog::info("({}) {}", PrintMapSource.at(map.source), map.name); } +void ConCommand_map(const CCommand& args) +{ + if (args.ArgC() > 1) + { + const char* arg = args.Arg(1); + auto f = [&](MapVPKInfo map) -> bool { return map.name == arg; }; + if (std::find_if(vMapList.begin(), vMapList.end(), f) == vMapList.end()) + { + spdlog::info("Invalid map found"); + return; + } + } + + OrignalMapCommand(args); +} + void InitialiseMapsPrint() { AUTOHOOK_DISPATCH() ConCommand* mapsCommand = R2::g_pCVar->FindCommand("maps"); mapsCommand->m_pCommandCallback = ConCommand_maps; + + ConCommand* mapCommand = R2::g_pCVar->FindCommand("map"); + OrignalMapCommand = mapCommand->m_pCommandCallback; + mapCommand->m_pCommandCallback = ConCommand_map; } From cc61f344eed931bbfa7ae357d22cf16e66cb0373 Mon Sep 17 00:00:00 2001 From: cat_or_not <41955154+catornot@users.noreply.github.com> Date: Wed, 2 Aug 2023 01:34:03 -0400 Subject: [PATCH 2/6] add RefreshMapList into the command --- NorthstarDLL/util/printmaps.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/NorthstarDLL/util/printmaps.cpp b/NorthstarDLL/util/printmaps.cpp index 3f62cd485..f60dab188 100644 --- a/NorthstarDLL/util/printmaps.cpp +++ b/NorthstarDLL/util/printmaps.cpp @@ -188,6 +188,8 @@ void ConCommand_map(const CCommand& args) { if (args.ArgC() > 1) { + RefreshMapList(); + const char* arg = args.Arg(1); auto f = [&](MapVPKInfo map) -> bool { return map.name == arg; }; if (std::find_if(vMapList.begin(), vMapList.end(), f) == vMapList.end()) From 52823a5b29e2ed7d8344376b62bf9e1a496acc8b Mon Sep 17 00:00:00 2001 From: cat_or_not <41955154+catornot@users.noreply.github.com> Date: Thu, 3 Aug 2023 19:10:28 -0400 Subject: [PATCH 3/6] reduce nesting and improve the warn msg --- NorthstarDLL/util/printmaps.cpp | 22 +++++++++------------- 1 file changed, 9 insertions(+), 13 deletions(-) diff --git a/NorthstarDLL/util/printmaps.cpp b/NorthstarDLL/util/printmaps.cpp index f60dab188..9068e12e2 100644 --- a/NorthstarDLL/util/printmaps.cpp +++ b/NorthstarDLL/util/printmaps.cpp @@ -31,7 +31,7 @@ struct MapVPKInfo // our current list of maps in the game std::vector vMapList; -FnCommandCallback_t OrignalMapCommand = NULL; +FnCommandCallback_t OriginalMapCommand = NULL; void RefreshMapList() { @@ -186,20 +186,16 @@ void ConCommand_maps(const CCommand& args) void ConCommand_map(const CCommand& args) { - if (args.ArgC() > 1) - { - RefreshMapList(); + RefreshMapList(); - const char* arg = args.Arg(1); - auto f = [&](MapVPKInfo map) -> bool { return map.name == arg; }; - if (std::find_if(vMapList.begin(), vMapList.end(), f) == vMapList.end()) - { - spdlog::info("Invalid map found"); - return; - } + if (args.ArgC() > 1 && + std::find_if(vMapList.begin(), vMapList.end(), [&](MapVPKInfo map) -> bool { return map.name == args.Arg(1); }) == vMapList.end()) + { + spdlog::warn("Map load failed: {} not found or invalid", args.Arg(1)); + return; } - OrignalMapCommand(args); + OriginalMapCommand(args); } void InitialiseMapsPrint() @@ -210,6 +206,6 @@ void InitialiseMapsPrint() mapsCommand->m_pCommandCallback = ConCommand_maps; ConCommand* mapCommand = R2::g_pCVar->FindCommand("map"); - OrignalMapCommand = mapCommand->m_pCommandCallback; + OriginalMapCommand = mapCommand->m_pCommandCallback; mapCommand->m_pCommandCallback = ConCommand_map; } From 705cab8fd22447c58b745dc4c0b0a6cda9de680c Mon Sep 17 00:00:00 2001 From: cat_or_not <41955154+catornot@users.noreply.github.com> Date: Fri, 4 Aug 2023 18:02:07 -0400 Subject: [PATCH 4/6] add a warning for missing map name --- NorthstarDLL/util/printmaps.cpp | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/NorthstarDLL/util/printmaps.cpp b/NorthstarDLL/util/printmaps.cpp index 9068e12e2..9e7b1b06c 100644 --- a/NorthstarDLL/util/printmaps.cpp +++ b/NorthstarDLL/util/printmaps.cpp @@ -194,6 +194,10 @@ void ConCommand_map(const CCommand& args) spdlog::warn("Map load failed: {} not found or invalid", args.Arg(1)); return; } + else + { + spdlog::warn("Map load failed: no map name provided"); + } OriginalMapCommand(args); } From aa1c9e828fa411935aa74be6cb9a4fecd14a3e6f Mon Sep 17 00:00:00 2001 From: cat_or_not <41955154+catornot@users.noreply.github.com> Date: Wed, 9 Aug 2023 01:35:26 -0400 Subject: [PATCH 5/6] reconstruct map concommand --- NorthstarDLL/util/printmaps.cpp | 26 +++++++++++++++++++------- 1 file changed, 19 insertions(+), 7 deletions(-) diff --git a/NorthstarDLL/util/printmaps.cpp b/NorthstarDLL/util/printmaps.cpp index 9e7b1b06c..3e1798042 100644 --- a/NorthstarDLL/util/printmaps.cpp +++ b/NorthstarDLL/util/printmaps.cpp @@ -31,7 +31,11 @@ struct MapVPKInfo // our current list of maps in the game std::vector vMapList; -FnCommandCallback_t OriginalMapCommand = NULL; +typedef void (*Host_Map_helperType)(const CCommand&, void*); +typedef void (*Host_Changelevel_fType)(const CCommand&); + +Host_Map_helperType Host_Map_helper; +Host_Changelevel_fType Host_Changelevel_f; void RefreshMapList() { @@ -184,7 +188,9 @@ void ConCommand_maps(const CCommand& args) spdlog::info("({}) {}", PrintMapSource.at(map.source), map.name); } -void ConCommand_map(const CCommand& args) +// clang-format off +AUTOHOOK(Host_Map_f, engine.dll + 0x15B340, void, __fastcall, (const CCommand& args)) +// clang-format on { RefreshMapList(); @@ -194,12 +200,16 @@ void ConCommand_map(const CCommand& args) spdlog::warn("Map load failed: {} not found or invalid", args.Arg(1)); return; } - else + else if (args.ArgC() == 1) { spdlog::warn("Map load failed: no map name provided"); + return; } - OriginalMapCommand(args); + if (*R2::g_pServerState >= R2::server_state_t::ss_active) + return Host_Changelevel_f(args); + else + return Host_Map_helper(args, std::nullptr_t()); } void InitialiseMapsPrint() @@ -208,8 +218,10 @@ void InitialiseMapsPrint() ConCommand* mapsCommand = R2::g_pCVar->FindCommand("maps"); mapsCommand->m_pCommandCallback = ConCommand_maps; +} - ConCommand* mapCommand = R2::g_pCVar->FindCommand("map"); - OriginalMapCommand = mapCommand->m_pCommandCallback; - mapCommand->m_pCommandCallback = ConCommand_map; +ON_DLL_LOAD("engine.dll", Host_Map_f, (CModule module)) +{ + Host_Map_helper = module.Offset(0x15AEF0).RCast(); + Host_Changelevel_f = module.Offset(0x15AAD0).RCast(); } From 0757e7aa7899655aa4186506591a4b6e83aa7780 Mon Sep 17 00:00:00 2001 From: cat_or_not <41955154+catornot@users.noreply.github.com> Date: Sun, 27 Aug 2023 14:16:22 -0400 Subject: [PATCH 6/6] use nullptr instead of std::nullptr_t --- NorthstarDLL/util/printmaps.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/NorthstarDLL/util/printmaps.cpp b/NorthstarDLL/util/printmaps.cpp index 3e1798042..abd4e1ea6 100644 --- a/NorthstarDLL/util/printmaps.cpp +++ b/NorthstarDLL/util/printmaps.cpp @@ -209,7 +209,7 @@ AUTOHOOK(Host_Map_f, engine.dll + 0x15B340, void, __fastcall, (const CCommand& a if (*R2::g_pServerState >= R2::server_state_t::ss_active) return Host_Changelevel_f(args); else - return Host_Map_helper(args, std::nullptr_t()); + return Host_Map_helper(args, nullptr); } void InitialiseMapsPrint()