From 6fe1805914fc10ad1f8539835bc53c208c8de417 Mon Sep 17 00:00:00 2001 From: F1F7Y <64418963+F1F7Y@users.noreply.github.com> Date: Thu, 8 Jun 2023 00:54:53 +0200 Subject: Fix `maps *` command, expose to squirrel (#442) --- NorthstarDLL/util/printmaps.cpp | 42 ++++++++++++++++++++++++++++++++--------- 1 file changed, 33 insertions(+), 9 deletions(-) diff --git a/NorthstarDLL/util/printmaps.cpp b/NorthstarDLL/util/printmaps.cpp index 99bda23c..65332887 100644 --- a/NorthstarDLL/util/printmaps.cpp +++ b/NorthstarDLL/util/printmaps.cpp @@ -4,6 +4,7 @@ #include "mods/modmanager.h" #include "core/tier0.h" #include "engine/r2engine.h" +#include "squirrel/squirrel.h" #include #include @@ -32,6 +33,15 @@ std::vector vMapList; void RefreshMapList() { + // Only update the maps list every 10 seconds max to we avoid constantly reading fs + static double fLastRefresh = -999; + + if (fLastRefresh + 10.0 > R2::g_pGlobals->m_flRealTime) + return; + + fLastRefresh = R2::g_pGlobals->m_flRealTime; + + // Rebuild map list vMapList.clear(); // get modded maps @@ -55,7 +65,7 @@ void RefreshMapList() "englishclient_frontend.bsp.pak000_dir.vpk"}; // don't include mp_common here as it contains mp_lobby // matches directory vpks, and captures their map name in the first group - static const std::regex rVpkMapRegex("englishclient_([a-zA-Z_]+)\\.bsp\\.pak000_dir\\.vpk", std::regex::icase); + static const std::regex rVpkMapRegex("englishclient_([a-zA-Z0-9_]+)\\.bsp\\.pak000_dir\\.vpk", std::regex::icase); for (fs::directory_entry file : fs::directory_iterator("./vpk")) { @@ -111,14 +121,7 @@ AUTOHOOK(_Host_Map_f_CompletionFunc, engine.dll + 0x161AE0, int, __fastcall, (const char const* cmdname, const char const* partial, char commands[COMMAND_COMPLETION_MAXITEMS][COMMAND_COMPLETION_ITEM_LENGTH])) // clang-format on { - // don't update our map list often from this func, only refresh every 10 seconds so we avoid constantly reading fs - static double flLastAutocompleteRefresh = -999; - - if (flLastAutocompleteRefresh + 10.0 < R2::g_pGlobals->m_flRealTime) - { - RefreshMapList(); - flLastAutocompleteRefresh = R2::g_pGlobals->m_flRealTime; - } + RefreshMapList(); // use a custom autocomplete func for all map loading commands const int cmdLength = strlen(cmdname); @@ -142,6 +145,27 @@ int, __fastcall, (const char const* cmdname, const char const* partial, char com return numMaps; } +ADD_SQFUNC( + "array", + NSGetLoadedMapNames, + "", + "Returns a string array of loaded map file names", + ScriptContext::UI | ScriptContext::CLIENT | ScriptContext::SERVER) +{ + // Maybe we should call this on mods reload instead + RefreshMapList(); + + g_pSquirrel->newarray(sqvm, 0); + + for (MapVPKInfo& map : vMapList) + { + g_pSquirrel->pushstring(sqvm, map.name.c_str()); + g_pSquirrel->arrayappend(sqvm, -2); + } + + return SQRESULT_NOTNULL; +} + void ConCommand_maps(const CCommand& args) { if (args.ArgC() < 2) -- cgit v1.2.3