From a904f4923119c9ed468084d663bf037e53af0078 Mon Sep 17 00:00:00 2001 From: jeefo Date: Wed, 20 Dec 2023 00:06:45 +0300 Subject: [PATCH] bot: added support for loading from custom folders (ref #498) --- ext/crlib | 2 +- inc/product.h | 2 -- inc/storage.h | 6 ++++++ src/config.cpp | 12 +++++++----- src/control.cpp | 4 ++-- src/engine.cpp | 4 ++-- src/storage.cpp | 48 +++++++++++++++++++++++++++++++++++++++++------- 7 files changed, 59 insertions(+), 19 deletions(-) diff --git a/ext/crlib b/ext/crlib index 13ca213..f188cfd 160000 --- a/ext/crlib +++ b/ext/crlib @@ -1 +1 @@ -Subproject commit 13ca213a079108baac1056b0f5dcdc7e9a7de99f +Subproject commit f188cfd399594cdcbca8dda42db842cd0796b7fb diff --git a/inc/product.h b/inc/product.h index 20b46dd..a04c365 100644 --- a/inc/product.h +++ b/inc/product.h @@ -53,8 +53,6 @@ public: ~Folders () = default; public: - static constexpr StringRef bot { "yapb" }; - static constexpr StringRef addons { "addons" }; static constexpr StringRef config { "conf" }; static constexpr StringRef data { "data" }; static constexpr StringRef lang { "lang" }; diff --git a/inc/storage.h b/inc/storage.h index 4f835d0..20e7af7 100644 --- a/inc/storage.h +++ b/inc/storage.h @@ -78,6 +78,12 @@ public: // builds the filename to requested filename String buildPath (int32_t type, bool isMemoryLoad = false); + // get's relative path against bot library (bot library should reside in bin dir) + String getRunningPath (); + + // same as above, but with valve-specific filesystem paths (loadfileforme) + String getRunningPathVFS (); + // converts storage option to storage filename int32_t storageToBotFile (int32_t options); diff --git a/src/config.cpp b/src/config.cpp index 488c1da..bf0cc8c 100644 --- a/src/config.cpp +++ b/src/config.cpp @@ -621,13 +621,15 @@ void BotConfig::loadDifficultyConfig () { } void BotConfig::loadMapSpecificConfig () { - auto mapSpecificConfig = strings.joinPath (folders.addons, folders.bot, folders.config, "maps", strings.format ("%s.%s", game.getMapName (), kConfigExtension)); + auto mapSpecificConfig = strings.joinPath (folders.config, "maps", strings.format ("%s.%s", game.getMapName (), kConfigExtension)); // check existence of file - if (plat.fileExists (strings.joinPath (game.getRunningModName (), mapSpecificConfig).chars ())) { - game.serverCommand ("exec %s", mapSpecificConfig); + if (plat.fileExists (strings.joinPath (bstor.getRunningPath (), mapSpecificConfig).chars ())) { + auto mapSpecificConfigForExec = strings.joinPath (bstor.getRunningPathVFS (), mapSpecificConfig); + mapSpecificConfigForExec.replace ("\\", "/"); - ctrl.msg ("Executed map-specific config: %s", mapSpecificConfig); + game.serverCommand ("exec %s", mapSpecificConfigForExec); + ctrl.msg ("Executed map-specific config: %s", mapSpecificConfigForExec); } } @@ -837,7 +839,7 @@ bool BotConfig::openConfig (StringRef fileName, StringRef errorIfNotExists, MemF } // save config dir - auto configDir = strings.joinPath (folders.addons, folders.bot, folders.config); + auto configDir = strings.joinPath (bstor.getRunningPathVFS (), folders.config); if (languageDependant) { if (fileName.startsWith ("lang") && cv_language.str () == "en") { diff --git a/src/control.cpp b/src/control.cpp index f753c71..7a74ff1 100644 --- a/src/control.cpp +++ b/src/control.cpp @@ -206,10 +206,10 @@ int BotControl::cmdCvars () { // if save requested, dump cvars to main config if (isSave) { - auto cfgPath = strings.joinPath (game.getRunningModName (), folders.addons, folders.bot, folders.config, strings.format ("%s.%s", product.nameLower, kConfigExtension)); + auto cfgPath = strings.joinPath (bstor.getRunningPath (), folders.config, strings.format ("%s.%s", product.nameLower, kConfigExtension)); if (isSaveMap) { - cfgPath = strings.joinPath (game.getRunningModName (), folders.addons, folders.bot, folders.config, "maps", strings.format ("%s.%s", game.getMapName (), kConfigExtension)); + cfgPath = strings.joinPath (bstor.getRunningPath (), folders.config, "maps", strings.format ("%s.%s", game.getMapName (), kConfigExtension)); } cfg.open (cfgPath, "wt"); cfg.puts ("// Configuration file for %s\n\n", product.name); diff --git a/src/engine.cpp b/src/engine.cpp index bfd37f7..46c8262 100644 --- a/src/engine.cpp +++ b/src/engine.cpp @@ -807,8 +807,8 @@ bool Game::postload () { game.print (msg); }); - auto ensureBotPathExists = [this] (StringRef dir1, StringRef dir2) { - File::makePath (strings.joinPath (getRunningModName (), folders.addons, folders.bot, dir1, dir2).chars ()); + auto ensureBotPathExists = [] (StringRef dir1, StringRef dir2) { + File::makePath (strings.joinPath (bstor.getRunningPath (), dir1, dir2).chars ()); }; // ensure we're have all needed directories diff --git a/src/storage.cpp b/src/storage.cpp index 868baf2..bd44c21 100644 --- a/src/storage.cpp +++ b/src/storage.cpp @@ -318,13 +318,12 @@ String BotStorage::buildPath (int32_t file, bool isMemoryLoad) { path.clear (); // if not memory file we're don't need game dir - if (!isMemoryLoad) { - path.emplace (game.getRunningModName ()); + if (isMemoryLoad) { + path.emplace (getRunningPathVFS ()); + } + else { + path.emplace (getRunningPath ()); } - - // always append addons/product - path.emplace (folders.addons); - path.emplace (folders.bot); // the datadir path.emplace (folders.data); @@ -341,7 +340,7 @@ String BotStorage::buildPath (int32_t file, bool isMemoryLoad) { auto timebuf = strings.chars (); strftime (timebuf, StringBuffer::StaticBufferSize, "L%d%m%Y", &timeinfo); - path.emplace (strings.format ("%s_%s.%s", folders.bot, timebuf, paths[file].second)); + path.emplace (strings.format ("%s_%s.%s", product.nameLower, timebuf, paths[file].second)); } else { String mapName = game.getMapName (); @@ -394,4 +393,39 @@ void BotStorage::unlinkFromDisk () { graph.reset (); // re-initialize points } +String BotStorage::getRunningPath () { + // this function get's relative path against bot library (bot library should reside in bin dir) + + static String path; + + // compute the full path to the our folder + if (path.empty ()) { + path = SharedLibrary::path (&bstor); + + if (path.startsWith ("