diff --git a/src/game/src/config.cpp b/src/game/src/config.cpp index b30bd97..6a25f30 100644 --- a/src/game/src/config.cpp +++ b/src/game/src/config.cpp @@ -1,4 +1,6 @@ #include "stdafx.h" +#include +#include #include #ifndef __WIN32__ #include @@ -342,139 +344,151 @@ void config_init(const string& st_localeServiceName) bool isCommonSQL = false; bool isPlayerSQL = false; - FILE* fpOnlyForDB; + 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" + }; - if (!(fpOnlyForDB = fopen(st_configFileName.c_str(), "r"))) - { - SPDLOG_CRITICAL("Can not open [{}]", st_configFileName); - exit(EXIT_FAILURE); + // 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; + + // 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(); } - while (fgets(buf, 256, fpOnlyForDB)) + 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; + } + } + + for (auto& kvp : config) { + SPDLOG_INFO("{}: {}", kvp.first.c_str(), kvp.second.c_str()); + } + + g_stBlockDate = config["BLOCK_LOGIN"]; + + 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"]; + + g_stHostname = config["HOSTNAME"]; + SPDLOG_INFO("HOSTNAME: {}", g_stHostname); + + str_to_number(g_bChannel, config["CHANNEL"].c_str()); + { - parse_token(buf, token_string, value_string); + 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])); - TOKEN("BLOCK_LOGIN") + 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; + } + + + { + 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]) { - 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; + } + + + { + 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]) { - 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)