From 8f9e9dc602a0074cf189f27879cba84262bdaad7 Mon Sep 17 00:00:00 2001 From: Tr0n Date: Tue, 4 Jun 2024 23:19:06 +0200 Subject: [PATCH 1/3] first proof of concept for env overrides --- src/game/src/config.cpp | 261 +++++++++++++++++++++++----------------- 1 file changed, 150 insertions(+), 111 deletions(-) diff --git a/src/game/src/config.cpp b/src/game/src/config.cpp index eacad23..1124ada 100644 --- a/src/game/src/config.cpp +++ b/src/game/src/config.cpp @@ -1,4 +1,10 @@ #include "stdafx.h" +#include +#include +#include +#include +#include +#include // For std::strcpy #include #ifndef __WIN32__ #include @@ -344,139 +350,172 @@ void config_init(const string& st_localeServiceName) bool isCommonSQL = false; bool isPlayerSQL = false; - FILE* fpOnlyForDB; + // Open the file + std::ifstream inputFile(st_configFileName); + + // Check if the file is open + if (!inputFile.is_open()) { + std::cerr << "Error opening file: " << st_configFileName << std::endl; + return ; + } + + // Container to store the key-value pairs + std::unordered_map keyValuePairs; + + std::string line; + // Read the file line by line + while (std::getline(inputFile, line)) { + std::istringstream lineStream(line); + std::string key; + + // Get the key + if (std::getline(lineStream, key, ':')) { + std::string value; + + // Get the value + if (std::getline(lineStream >> std::ws, value)) { + // Allocate memory for the value + char* valueCStr = new char[value.length() + 1]; + std::strcpy(valueCStr, value.c_str()); + + // Store the key and value in the map + keyValuePairs[key] = valueCStr; + } + } + } + + // Close the file + inputFile.close(); - if (!(fpOnlyForDB = fopen(st_configFileName.c_str(), "r"))) - { - SPDLOG_CRITICAL("Can not open [{}]", st_configFileName); - exit(EXIT_FAILURE); + for (auto& kvp : keyValuePairs) { + // Check if the key is an environment variable + const char* envValue = std::getenv(kvp.first.c_str()); + if (envValue) { + // Free the old value memory + delete[] kvp.second; + + // Allocate memory for the new environment variable value + kvp.second = new char[std::strlen(envValue) + 1]; + std::strcpy(kvp.second, envValue); + } + + char buffer[256]; + std::snprintf(buffer, sizeof(buffer), "KEY: %s, VALUE: %s", kvp.first.c_str(), kvp.second); + SPDLOG_INFO("{}: {}", kvp.first.c_str(), kvp.second); + std::cout << buffer << std::endl; + } + + if (keyValuePairs.find("BLOCK_LOGIN") != keyValuePairs.end()){ + g_stBlockDate = keyValuePairs["BLOCK_LOGIN"]; } - while (fgets(buf, 256, fpOnlyForDB)) - { - parse_token(buf, token_string, value_string); + + if (keyValuePairs.find("ADMINPAGE_IP") != keyValuePairs.end()){ + FN_add_adminpageIP(keyValuePairs["ADMINPAGE_IP"]); + //g_stAdminPageIP[0] = value_string; + } - TOKEN("BLOCK_LOGIN") + + if (keyValuePairs.find("ADMINPAGE_IP1") != keyValuePairs.end()){ + FN_add_adminpageIP(keyValuePairs["ADMINPAGE_IP1"]); + //g_stAdminPageIP[0] = value_string; + } + + + if (keyValuePairs.find("ADMINPAGE_IP2") != keyValuePairs.end()) { + FN_add_adminpageIP(keyValuePairs["ADMINPAGE_IP2"]); + //g_stAdminPageIP[1] = value_string; + } + + + if (keyValuePairs.find("ADMINPAGE_IP3") != keyValuePairs.end()) { + FN_add_adminpageIP(keyValuePairs["ADMINPAGE_IP3"]); + //g_stAdminPageIP[2] = value_string; + } + + + if (keyValuePairs.find("ADMINPAGE_PASSWORD") != keyValuePairs.end()){ + g_stAdminPagePassword = keyValuePairs["ADMINPAGE_PASSWORD"]; + } + + + if (keyValuePairs.find("HOSTNAME") != keyValuePairs.end()) { + g_stHostname = keyValuePairs["HOSTNAME"]; + SPDLOG_INFO("HOSTNAME: {}", g_stHostname); + } + + + if (keyValuePairs.find("CHANNEL") != keyValuePairs.end()){ + str_to_number(g_bChannel, keyValuePairs["CHANNEL"]); + } + + + if (keyValuePairs.find("PLAYER_SQL") != keyValuePairs.end()){ + const char * line = two_arguments(keyValuePairs["PLAYER_SQL"], db_host[0], sizeof(db_host[0]), db_user[0], sizeof(db_user[0])); + line = two_arguments(line, db_pwd[0], sizeof(db_pwd[0]), db_db[0], sizeof(db_db[0])); + + if ('\0' != line[0]) { - g_stBlockDate = value_string; + char buf[256]; + one_argument(line, buf, sizeof(buf)); + str_to_number(mysql_db_port[0], buf); } - TOKEN("adminpage_ip") + if (!*db_host[0] || !*db_user[0] || !*db_pwd[0] || !*db_db[0]) { - FN_add_adminpageIP(value_string); - //g_stAdminPageIP[0] = value_string; + SPDLOG_CRITICAL("PLAYER_SQL syntax: logsql "); + exit(EXIT_FAILURE); } - TOKEN("adminpage_ip1") + char buf[1024]; + snprintf(buf, sizeof(buf), "PLAYER_SQL: %s %s %s %s %d", db_host[0], db_user[0], db_pwd[0], db_db[0], mysql_db_port[0]); + isPlayerSQL = true; + } + + + if (keyValuePairs.find("COMMON_SQL") != keyValuePairs.end()){ + const char * line = two_arguments(keyValuePairs["COMMON_SQL"], db_host[1], sizeof(db_host[1]), db_user[1], sizeof(db_user[1])); + line = two_arguments(line, db_pwd[1], sizeof(db_pwd[1]), db_db[1], sizeof(db_db[1])); + + if ('\0' != line[0]) { - FN_add_adminpageIP(value_string); - //g_stAdminPageIP[0] = value_string; + char buf[256]; + one_argument(line, buf, sizeof(buf)); + str_to_number(mysql_db_port[1], buf); } - TOKEN("adminpage_ip2") + if (!*db_host[1] || !*db_user[1] || !*db_pwd[1] || !*db_db[1]) { - FN_add_adminpageIP(value_string); - //g_stAdminPageIP[1] = value_string; + SPDLOG_CRITICAL("COMMON_SQL syntax: logsql "); + exit(EXIT_FAILURE); } - TOKEN("adminpage_ip3") + char buf[1024]; + snprintf(buf, sizeof(buf), "COMMON_SQL: %s %s %s %s %d", db_host[1], db_user[1], db_pwd[1], db_db[1], mysql_db_port[1]); + isCommonSQL = true; + } + + + if (keyValuePairs.find("LOG_SQL") != keyValuePairs.end()){ + const char * line = two_arguments(keyValuePairs["LOG_SQL"], log_host, sizeof(log_host), log_user, sizeof(log_user)); + line = two_arguments(line, log_pwd, sizeof(log_pwd), log_db, sizeof(log_db)); + + if ('\0' != line[0]) { - FN_add_adminpageIP(value_string); - //g_stAdminPageIP[2] = value_string; + char buf[256]; + one_argument(line, buf, sizeof(buf)); + str_to_number(log_port, buf); } - TOKEN("adminpage_password") + if (!*log_host || !*log_user || !*log_pwd || !*log_db) { - g_stAdminPagePassword = value_string; + SPDLOG_CRITICAL("LOG_SQL syntax: logsql "); + exit(EXIT_FAILURE); } - TOKEN("hostname") - { - g_stHostname = value_string; - SPDLOG_INFO("HOSTNAME: {}", g_stHostname); - continue; - } - - TOKEN("channel") - { - str_to_number(g_bChannel, value_string); - continue; - } - - TOKEN("player_sql") - { - const char * line = two_arguments(value_string, db_host[0], sizeof(db_host[0]), db_user[0], sizeof(db_user[0])); - line = two_arguments(line, db_pwd[0], sizeof(db_pwd[0]), db_db[0], sizeof(db_db[0])); - - if ('\0' != line[0]) - { - char buf[256]; - one_argument(line, buf, sizeof(buf)); - str_to_number(mysql_db_port[0], buf); - } - - if (!*db_host[0] || !*db_user[0] || !*db_pwd[0] || !*db_db[0]) - { - SPDLOG_CRITICAL("PLAYER_SQL syntax: logsql "); - exit(EXIT_FAILURE); - } - - char buf[1024]; - snprintf(buf, sizeof(buf), "PLAYER_SQL: %s %s %s %s %d", db_host[0], db_user[0], db_pwd[0], db_db[0], mysql_db_port[0]); - isPlayerSQL = true; - continue; - } - - TOKEN("common_sql") - { - const char * line = two_arguments(value_string, db_host[1], sizeof(db_host[1]), db_user[1], sizeof(db_user[1])); - line = two_arguments(line, db_pwd[1], sizeof(db_pwd[1]), db_db[1], sizeof(db_db[1])); - - if ('\0' != line[0]) - { - char buf[256]; - one_argument(line, buf, sizeof(buf)); - str_to_number(mysql_db_port[1], buf); - } - - if (!*db_host[1] || !*db_user[1] || !*db_pwd[1] || !*db_db[1]) - { - SPDLOG_CRITICAL("COMMON_SQL syntax: logsql "); - exit(EXIT_FAILURE); - } - - char buf[1024]; - snprintf(buf, sizeof(buf), "COMMON_SQL: %s %s %s %s %d", db_host[1], db_user[1], db_pwd[1], db_db[1], mysql_db_port[1]); - isCommonSQL = true; - continue; - } - - TOKEN("log_sql") - { - const char * line = two_arguments(value_string, log_host, sizeof(log_host), log_user, sizeof(log_user)); - line = two_arguments(line, log_pwd, sizeof(log_pwd), log_db, sizeof(log_db)); - - if ('\0' != line[0]) - { - char buf[256]; - one_argument(line, buf, sizeof(buf)); - str_to_number(log_port, buf); - } - - if (!*log_host || !*log_user || !*log_pwd || !*log_db) - { - SPDLOG_CRITICAL("LOG_SQL syntax: logsql "); - exit(EXIT_FAILURE); - } - - char buf[1024]; - snprintf(buf, sizeof(buf), "LOG_SQL: %s %s %s %s %d", log_host, log_user, log_pwd, log_db, log_port); - continue; - } - } - - //처리가 끝났으니 파일을 닫자. - fclose(fpOnlyForDB); + char buf[1024]; + snprintf(buf, sizeof(buf), "LOG_SQL: %s %s %s %s %d", log_host, log_user, log_pwd, log_db, log_port); + } // CONFIG_SQL_INFO_ERROR if (!isCommonSQL) -- 2.45.2 From 2b9651febcc47f508d02de15aae187c53a0bef56 Mon Sep 17 00:00:00 2001 From: Tr0n Date: Wed, 5 Jun 2024 21:53:50 +0200 Subject: [PATCH 2/3] remove dependency on config file (use env as config) --- src/game/src/config.cpp | 169 ++++++++++++++++++---------------------- 1 file changed, 74 insertions(+), 95 deletions(-) diff --git a/src/game/src/config.cpp b/src/game/src/config.cpp index 1124ada..209a20e 100644 --- a/src/game/src/config.cpp +++ b/src/game/src/config.cpp @@ -350,108 +350,87 @@ void config_init(const string& st_localeServiceName) bool isCommonSQL = false; bool isPlayerSQL = false; - // Open the file - std::ifstream inputFile(st_configFileName); - - // Check if the file is open - if (!inputFile.is_open()) { - std::cerr << "Error opening file: " << st_configFileName << std::endl; - return ; - } - - // Container to store the key-value pairs - std::unordered_map keyValuePairs; - - std::string line; - // Read the file line by line - while (std::getline(inputFile, line)) { - std::istringstream lineStream(line); - std::string key; - - // Get the key - if (std::getline(lineStream, key, ':')) { - std::string value; - - // Get the value - if (std::getline(lineStream >> std::ws, value)) { - // Allocate memory for the value - char* valueCStr = new char[value.length() + 1]; - std::strcpy(valueCStr, value.c_str()); - - // Store the key and value in the map - keyValuePairs[key] = valueCStr; - } - } - } - - // Close the file - inputFile.close(); - - for (auto& kvp : keyValuePairs) { - // Check if the key is an environment variable - const char* envValue = std::getenv(kvp.first.c_str()); - if (envValue) { - // Free the old value memory - delete[] kvp.second; - - // Allocate memory for the new environment variable value - kvp.second = new char[std::strlen(envValue) + 1]; - std::strcpy(kvp.second, envValue); - } - - char buffer[256]; - std::snprintf(buffer, sizeof(buffer), "KEY: %s, VALUE: %s", kvp.first.c_str(), kvp.second); - SPDLOG_INFO("{}: {}", kvp.first.c_str(), kvp.second); - std::cout << buffer << std::endl; - } - - if (keyValuePairs.find("BLOCK_LOGIN") != keyValuePairs.end()){ - g_stBlockDate = keyValuePairs["BLOCK_LOGIN"]; - } + const std::string config_keys[] = { + "BLOCK_LOGIN", + "ADMINPAGE_IP", + "ADMINPAGE_IP1", + "ADMINPAGE_IP2", + "ADMINPAGE_IP3", + "ADMINPAGE_PASSWORD", + "HOSTNAME", + "CHANNEL", + "PLAYER_SQL", + "COMMON_SQL", + "LOG_SQL" + }; + // Container to store the config file key-value pairs + std::unordered_map cfg_file_entries; + // Container to store the final config with env overrides + std::unordered_map config; - if (keyValuePairs.find("ADMINPAGE_IP") != keyValuePairs.end()){ - FN_add_adminpageIP(keyValuePairs["ADMINPAGE_IP"]); - //g_stAdminPageIP[0] = value_string; + // Open the file + std::ifstream config_file(st_configFileName); + + // Check if the file is open + if (!config_file.is_open()) { + SPDLOG_WARN("Error opening config file, can't fall back for missing envs."); + } else { + std::string line; + // Read the config file + while (std::getline(config_file, line)) { + std::istringstream line_stream(line); + std::string key; + + // Get the key + if (std::getline(line_stream, key, ':')) { + std::string value; + + // Get the value + if (std::getline(line_stream >> std::ws, value)) { + // Store the key and value in the map + cfg_file_entries[key] = value; + } + } + } + config_file.close(); } - - if (keyValuePairs.find("ADMINPAGE_IP1") != keyValuePairs.end()){ - FN_add_adminpageIP(keyValuePairs["ADMINPAGE_IP1"]); - //g_stAdminPageIP[0] = value_string; + for (auto& config_key : config_keys) { + // Check if the key is an environment variable + const char* envValue = std::getenv(config_key.c_str()); + if (envValue) { + // Update the value in the map with the environment variable value + config[config_key] = envValue; + continue; + } + // Fallback to config file entries when there's no env override + if (cfg_file_entries.find(config_key) != cfg_file_entries.end()){ + config[config_key] = cfg_file_entries[config_key]; + continue; + } } - - if (keyValuePairs.find("ADMINPAGE_IP2") != keyValuePairs.end()) { - FN_add_adminpageIP(keyValuePairs["ADMINPAGE_IP2"]); - //g_stAdminPageIP[1] = value_string; + for (auto& kvp : config) { + SPDLOG_INFO("{}: {}", kvp.first.c_str(), kvp.second.c_str()); } - - if (keyValuePairs.find("ADMINPAGE_IP3") != keyValuePairs.end()) { - FN_add_adminpageIP(keyValuePairs["ADMINPAGE_IP3"]); - //g_stAdminPageIP[2] = value_string; - } + g_stBlockDate = config["BLOCK_LOGIN"]; - - if (keyValuePairs.find("ADMINPAGE_PASSWORD") != keyValuePairs.end()){ - g_stAdminPagePassword = keyValuePairs["ADMINPAGE_PASSWORD"]; - } + g_stAdminPageIP.push_back(config["ADMINPAGE_IP"]); + g_stAdminPageIP.push_back(config["ADMINPAGE_IP1"]); + g_stAdminPageIP.push_back(config["ADMINPAGE_IP2"]); + g_stAdminPageIP.push_back(config["ADMINPAGE_IP3"]); + g_stAdminPagePassword = config["ADMINPAGE_PASSWORD"]; - if (keyValuePairs.find("HOSTNAME") != keyValuePairs.end()) { - g_stHostname = keyValuePairs["HOSTNAME"]; - SPDLOG_INFO("HOSTNAME: {}", g_stHostname); - } + g_stHostname = config["HOSTNAME"]; + SPDLOG_INFO("HOSTNAME: {}", g_stHostname); - - if (keyValuePairs.find("CHANNEL") != keyValuePairs.end()){ - str_to_number(g_bChannel, keyValuePairs["CHANNEL"]); - } + str_to_number(g_bChannel, config["CHANNEL"].c_str()); - - if (keyValuePairs.find("PLAYER_SQL") != keyValuePairs.end()){ - const char * line = two_arguments(keyValuePairs["PLAYER_SQL"], db_host[0], sizeof(db_host[0]), db_user[0], sizeof(db_user[0])); + { + const char * line = two_arguments(config["PLAYER_SQL"].c_str(), db_host[0], sizeof(db_host[0]), db_user[0], sizeof(db_user[0])); line = two_arguments(line, db_pwd[0], sizeof(db_pwd[0]), db_db[0], sizeof(db_db[0])); if ('\0' != line[0]) @@ -473,8 +452,8 @@ void config_init(const string& st_localeServiceName) } - if (keyValuePairs.find("COMMON_SQL") != keyValuePairs.end()){ - const char * line = two_arguments(keyValuePairs["COMMON_SQL"], db_host[1], sizeof(db_host[1]), db_user[1], sizeof(db_user[1])); + { + const char * line = two_arguments(config["COMMON_SQL"].c_str(), db_host[1], sizeof(db_host[1]), db_user[1], sizeof(db_user[1])); line = two_arguments(line, db_pwd[1], sizeof(db_pwd[1]), db_db[1], sizeof(db_db[1])); if ('\0' != line[0]) @@ -486,7 +465,7 @@ void config_init(const string& st_localeServiceName) if (!*db_host[1] || !*db_user[1] || !*db_pwd[1] || !*db_db[1]) { - SPDLOG_CRITICAL("COMMON_SQL syntax: logsql "); + SPDLOG_CRITICAL("COMMON_SQL syntax: logsql "); exit(EXIT_FAILURE); } @@ -496,8 +475,8 @@ void config_init(const string& st_localeServiceName) } - if (keyValuePairs.find("LOG_SQL") != keyValuePairs.end()){ - const char * line = two_arguments(keyValuePairs["LOG_SQL"], log_host, sizeof(log_host), log_user, sizeof(log_user)); + { + const char * line = two_arguments(config["LOG_SQL"].c_str(), log_host, sizeof(log_host), log_user, sizeof(log_user)); line = two_arguments(line, log_pwd, sizeof(log_pwd), log_db, sizeof(log_db)); if ('\0' != line[0]) @@ -509,7 +488,7 @@ void config_init(const string& st_localeServiceName) if (!*log_host || !*log_user || !*log_pwd || !*log_db) { - SPDLOG_CRITICAL("LOG_SQL syntax: logsql "); + SPDLOG_CRITICAL("LOG_SQL syntax: logsql "); exit(EXIT_FAILURE); } -- 2.45.2 From e642a6db52b9d14d13092bb3413d68742c7383d9 Mon Sep 17 00:00:00 2001 From: Tr0n Date: Wed, 5 Jun 2024 22:12:21 +0200 Subject: [PATCH 3/3] includes cleanup --- src/game/src/config.cpp | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/game/src/config.cpp b/src/game/src/config.cpp index 6391930..6a25f30 100644 --- a/src/game/src/config.cpp +++ b/src/game/src/config.cpp @@ -1,10 +1,6 @@ #include "stdafx.h" -#include #include -#include #include -#include -#include // For std::strcpy #include #ifndef __WIN32__ #include -- 2.45.2