refactor: move config reader into right class

This commit is contained in:
jeefo 2023-05-14 14:48:06 +03:00
commit 48fba9a6b7
No known key found for this signature in database
GPG key ID: 927BCA0779BEA8ED
4 changed files with 43 additions and 43 deletions

View file

@ -131,6 +131,9 @@ public:
// display current custom values // display current custom values
void showCustomValues (); void showCustomValues ();
// opens config helper
bool openConfig (StringRef fileName, StringRef errorIfNotExists, MemFile *outFile, bool languageDependant = false);
private: private:
bool isCommentLine (StringRef line) const { bool isCommentLine (StringRef line) const {
if (line.empty ()) { if (line.empty ()) {

View file

@ -54,9 +54,6 @@ public:
// check if entity is a vip // check if entity is a vip
bool isPlayerVIP (edict_t *ent); bool isPlayerVIP (edict_t *ent);
// opens config helper
bool openConfig (StringRef fileName, StringRef errorIfNotExists, MemFile *outFile, bool languageDependant = false);
// nearest player search helper // nearest player search helper
bool findNearestPlayer (void **holder, edict_t *to, float searchDistance = 4096.0, bool sameTeam = false, bool needBot = false, bool needAlive = false, bool needDrawn = false, bool needBotWithC4 = false); bool findNearestPlayer (void **holder, edict_t *to, float searchDistance = 4096.0, bool sameTeam = false, bool needBot = false, bool needAlive = false, bool needDrawn = false, bool needBotWithC4 = false);

View file

@ -49,7 +49,7 @@ void BotConfig::loadMainConfig (bool isFirstLoad) {
MemFile file; MemFile file;
// this is does the same as exec of engine, but not overwriting values of cvars specified in cv_ignore_cvars_on_changelevel // this is does the same as exec of engine, but not overwriting values of cvars specified in cv_ignore_cvars_on_changelevel
if (util.openConfig (product.nameLower, "Bot main config file is not found.", &file, false)) { if (openConfig (product.nameLower, "Bot main config file is not found.", &file, false)) {
while (file.getLine (line)) { while (file.getLine (line)) {
line.trim (); line.trim ();
@ -117,7 +117,7 @@ void BotConfig::loadNamesConfig () {
MemFile file; MemFile file;
// naming initialization // naming initialization
if (util.openConfig ("names", "Name configuration file not found.", &file, true)) { if (openConfig ("names", "Name configuration file not found.", &file, true)) {
m_botNames.clear (); m_botNames.clear ();
while (file.getLine (line)) { while (file.getLine (line)) {
@ -172,7 +172,7 @@ void BotConfig::loadWeaponsConfig () {
MemFile file; MemFile file;
// weapon data initialization // weapon data initialization
if (util.openConfig ("weapon", "Weapon configuration file not found. Loading defaults.", &file)) { if (openConfig ("weapon", "Weapon configuration file not found. Loading defaults.", &file)) {
while (file.getLine (line)) { while (file.getLine (line)) {
line.trim (); line.trim ();
@ -224,7 +224,7 @@ void BotConfig::loadChatterConfig () {
MemFile file; MemFile file;
// chatter initialization // chatter initialization
if (game.is (GameFlags::HasBotVoice) && cv_radio_mode.int_ () == 2 && util.openConfig ("chatter", "Couldn't open chatter system configuration", &file)) { if (game.is (GameFlags::HasBotVoice) && cv_radio_mode.int_ () == 2 && openConfig ("chatter", "Couldn't open chatter system configuration", &file)) {
m_chatter.clear (); m_chatter.clear ();
struct EventMap { struct EventMap {
@ -364,7 +364,7 @@ void BotConfig::loadChatConfig () {
MemFile file; MemFile file;
// chat config initialization // chat config initialization
if (util.openConfig ("chat", "Chat file not found.", &file, true)) { if (openConfig ("chat", "Chat file not found.", &file, true)) {
StringArray *chat = nullptr; StringArray *chat = nullptr;
StringArray keywords {}; StringArray keywords {};
@ -467,7 +467,7 @@ void BotConfig::loadLanguageConfig () {
MemFile file; MemFile file;
// localizer inititalization // localizer inititalization
if (util.openConfig ("lang", "Specified language not found.", &file, true)) { if (openConfig ("lang", "Specified language not found.", &file, true)) {
String temp; String temp;
Twin <String, String> lang; Twin <String, String> lang;
@ -513,7 +513,7 @@ void BotConfig::loadAvatarsConfig () {
MemFile file; MemFile file;
// avatars inititalization // avatars inititalization
if (util.openConfig ("avatars", "Avatars config file not found. Avatars will not be displayed.", &file)) { if (openConfig ("avatars", "Avatars config file not found. Avatars will not be displayed.", &file)) {
m_avatars.clear (); m_avatars.clear ();
while (file.getLine (line)) { while (file.getLine (line)) {
@ -577,7 +577,7 @@ void BotConfig::loadDifficultyConfig () {
}; };
// avatars inititalization // avatars inititalization
if (util.openConfig ("difficulty", "Difficulty config file not found. Loading defaults.", &file)) { if (openConfig ("difficulty", "Difficulty config file not found. Loading defaults.", &file)) {
while (file.getLine (line)) { while (file.getLine (line)) {
if (isCommentLine (line) || line.length () < 3) { if (isCommentLine (line) || line.length () < 3) {
@ -630,7 +630,7 @@ void BotConfig::loadCustomConfig () {
m_custom["AMXParachuteCvar"] = "sv_parachute"; m_custom["AMXParachuteCvar"] = "sv_parachute";
// custom inititalization // custom inititalization
if (util.openConfig ("custom", "Custom config file not found. Loading defaults.", &file)) { if (openConfig ("custom", "Custom config file not found. Loading defaults.", &file)) {
m_custom.clear (); m_custom.clear ();
while (file.getLine (line)) { while (file.getLine (line)) {
@ -661,7 +661,7 @@ void BotConfig::loadLogosConfig () {
MemFile file; MemFile file;
// logos inititalization // logos inititalization
if (util.openConfig ("logos", "Logos config file not found. Loading defaults.", &file)) { if (openConfig ("logos", "Logos config file not found. Loading defaults.", &file)) {
m_logos.clear (); m_logos.clear ();
while (file.getLine (line)) { while (file.getLine (line)) {
@ -821,3 +821,33 @@ uint32_t BotConfig::hashLangString (StringRef str) {
} }
return res.empty () ? 0 : res.hash (); return res.empty () ? 0 : res.hash ();
} }
bool BotConfig::openConfig (StringRef fileName, StringRef errorIfNotExists, MemFile *outFile, bool languageDependant /*= false*/) {
if (*outFile) {
outFile->close ();
}
// save config dir
auto configDir = strings.joinPath (folders.addons, folders.bot, folders.config);
if (languageDependant) {
if (fileName.startsWith ("lang") && strcmp (cv_language.str (), "en") == 0) {
return false;
}
auto langConfig = strings.joinPath (configDir, folders.lang, strings.format ("%s_%s.%s", cv_language.str (), fileName, kConfigExtension));
// check is file is exists for this language
if (!outFile->open (langConfig)) {
outFile->open (strings.joinPath (configDir, folders.lang, strings.format ("en_%s.%s", fileName, kConfigExtension)));
}
}
else {
outFile->open (strings.joinPath (configDir, strings.format ("%s.%s", fileName, kConfigExtension)));
}
if (!*outFile) {
logger.error (errorIfNotExists.chars ());
return false;
}
return true;
}

View file

@ -233,36 +233,6 @@ bool BotSupport::isFakeClient (edict_t *ent) {
return false; return false;
} }
bool BotSupport::openConfig (StringRef fileName, StringRef errorIfNotExists, MemFile *outFile, bool languageDependant /*= false*/) {
if (*outFile) {
outFile->close ();
}
// save config dir
auto configDir = strings.joinPath (folders.addons, folders.bot, folders.config);
if (languageDependant) {
if (fileName.startsWith ("lang") && strcmp (cv_language.str (), "en") == 0) {
return false;
}
auto langConfig = strings.joinPath (configDir, folders.lang, strings.format ("%s_%s.%s", cv_language.str (), fileName, kConfigExtension));
// check is file is exists for this language
if (!outFile->open (langConfig)) {
outFile->open (strings.joinPath (configDir, folders.lang, strings.format ("en_%s.%s", fileName, kConfigExtension)));
}
}
else {
outFile->open (strings.joinPath (configDir, strings.format ("%s.%s", fileName, kConfigExtension)));
}
if (!*outFile) {
logger.error (errorIfNotExists.chars ());
return false;
}
return true;
}
void BotSupport::checkWelcome () { void BotSupport::checkWelcome () {
// the purpose of this function, is to send quick welcome message, to the listenserver entity. // the purpose of this function, is to send quick welcome message, to the listenserver entity.