bot: added support for loading from custom folders (ref #498)

This commit is contained in:
jeefo 2023-12-20 00:06:45 +03:00
commit a904f49231
No known key found for this signature in database
GPG key ID: 927BCA0779BEA8ED
7 changed files with 59 additions and 19 deletions

@ -1 +1 @@
Subproject commit 13ca213a079108baac1056b0f5dcdc7e9a7de99f Subproject commit f188cfd399594cdcbca8dda42db842cd0796b7fb

View file

@ -53,8 +53,6 @@ public:
~Folders () = default; ~Folders () = default;
public: public:
static constexpr StringRef bot { "yapb" };
static constexpr StringRef addons { "addons" };
static constexpr StringRef config { "conf" }; static constexpr StringRef config { "conf" };
static constexpr StringRef data { "data" }; static constexpr StringRef data { "data" };
static constexpr StringRef lang { "lang" }; static constexpr StringRef lang { "lang" };

View file

@ -78,6 +78,12 @@ public:
// builds the filename to requested filename // builds the filename to requested filename
String buildPath (int32_t type, bool isMemoryLoad = false); 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 // converts storage option to storage filename
int32_t storageToBotFile (int32_t options); int32_t storageToBotFile (int32_t options);

View file

@ -621,13 +621,15 @@ void BotConfig::loadDifficultyConfig () {
} }
void BotConfig::loadMapSpecificConfig () { 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 // check existence of file
if (plat.fileExists (strings.joinPath (game.getRunningModName (), mapSpecificConfig).chars ())) { if (plat.fileExists (strings.joinPath (bstor.getRunningPath (), mapSpecificConfig).chars ())) {
game.serverCommand ("exec %s", mapSpecificConfig); 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 // save config dir
auto configDir = strings.joinPath (folders.addons, folders.bot, folders.config); auto configDir = strings.joinPath (bstor.getRunningPathVFS (), folders.config);
if (languageDependant) { if (languageDependant) {
if (fileName.startsWith ("lang") && cv_language.str () == "en") { if (fileName.startsWith ("lang") && cv_language.str () == "en") {

View file

@ -206,10 +206,10 @@ int BotControl::cmdCvars () {
// if save requested, dump cvars to main config // if save requested, dump cvars to main config
if (isSave) { 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) { 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.open (cfgPath, "wt");
cfg.puts ("// Configuration file for %s\n\n", product.name); cfg.puts ("// Configuration file for %s\n\n", product.name);

View file

@ -807,8 +807,8 @@ bool Game::postload () {
game.print (msg); game.print (msg);
}); });
auto ensureBotPathExists = [this] (StringRef dir1, StringRef dir2) { auto ensureBotPathExists = [] (StringRef dir1, StringRef dir2) {
File::makePath (strings.joinPath (getRunningModName (), folders.addons, folders.bot, dir1, dir2).chars ()); File::makePath (strings.joinPath (bstor.getRunningPath (), dir1, dir2).chars ());
}; };
// ensure we're have all needed directories // ensure we're have all needed directories

View file

@ -318,13 +318,12 @@ String BotStorage::buildPath (int32_t file, bool isMemoryLoad) {
path.clear (); path.clear ();
// if not memory file we're don't need game dir // if not memory file we're don't need game dir
if (!isMemoryLoad) { if (isMemoryLoad) {
path.emplace (game.getRunningModName ()); path.emplace (getRunningPathVFS ());
}
else {
path.emplace (getRunningPath ());
} }
// always append addons/product
path.emplace (folders.addons);
path.emplace (folders.bot);
// the datadir // the datadir
path.emplace (folders.data); path.emplace (folders.data);
@ -341,7 +340,7 @@ String BotStorage::buildPath (int32_t file, bool isMemoryLoad) {
auto timebuf = strings.chars (); auto timebuf = strings.chars ();
strftime (timebuf, StringBuffer::StaticBufferSize, "L%d%m%Y", &timeinfo); 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 { else {
String mapName = game.getMapName (); String mapName = game.getMapName ();
@ -394,4 +393,39 @@ void BotStorage::unlinkFromDisk () {
graph.reset (); // re-initialize points 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 ("<unk")) {
logger.fatal ("Unable to detect library path. Giving up...");
}
auto parts = path.substr (1).split (kPathSeparator);
parts.pop (); // remove library name
parts.pop (); // remove bin directory
path = path.substr (0, 1) + String::join (parts, kPathSeparator);
}
return path;
}
String BotStorage::getRunningPathVFS () {
static String path;
if (path.empty ()) {
path = getRunningPath ();
path = path.substr (path.find (game.getRunningModName ())); // skip to the game dir
path = path.substr (path.find (kPathSeparator) + 1); // skip the game dir
}
return path;
}
#endif // BOT_STORAGE_EXPLICIT_INSTANTIATIONS #endif // BOT_STORAGE_EXPLICIT_INSTANTIATIONS