diff --git a/code/controllers/configuration.dm b/code/controllers/configuration.dm index c7e24f54627..f347605febe 100644 --- a/code/controllers/configuration.dm +++ b/code/controllers/configuration.dm @@ -277,7 +277,7 @@ var/list/gamemode_cache = list() config.log_access = 1 if ("sql_enabled") - config.sql_enabled = text2num(value) + config.sql_enabled = 1 if ("log_say") config.log_say = 1 @@ -737,46 +737,6 @@ var/list/gamemode_cache = list() else log_misc("Unknown setting in configuration: '[name]'") -/datum/configuration/proc/loadsql(filename) // -- TLE - var/list/Lines = file2list(filename) - for(var/t in Lines) - if(!t) continue - - t = trim(t) - if (length(t) == 0) - continue - else if (copytext(t, 1, 2) == "#") - continue - - var/pos = findtext(t, " ") - var/name = null - var/value = null - - if (pos) - name = lowertext(copytext(t, 1, pos)) - value = copytext(t, pos + 1) - else - name = lowertext(t) - - if (!name) - continue - - switch (name) - if ("address") - sqladdress = value - if ("port") - sqlport = value - if ("database") - sqldb = value - if ("login") - sqllogin = value - if ("password") - sqlpass = value - if ("enable_stat_tracking") - sqllogging = 1 - else - log_misc("Unknown setting in configuration: '[name]'") - /datum/configuration/proc/pick_mode(mode_name) // I wish I didn't have to instance the game modes in order to look up // their information, but it is the only way (at least that I know of). diff --git a/code/defines/procs/dbcore.dm b/code/defines/procs/dbcore.dm index 88335bd0aa4..67dc76a8e24 100644 --- a/code/defines/procs/dbcore.dm +++ b/code/defines/procs/dbcore.dm @@ -58,7 +58,7 @@ DBConnection/New(server, port = 3306, database, username, password_handler, curs _db_con = _dm_db_new_con() DBConnection/proc/Connect(dbi_handler = con_dbi, user_handler = con_user, password_handler = con_password, cursor_handler) - if (!sqllogging) + if (!config.sql_enabled) return 0 if (!src) return 0 @@ -71,7 +71,7 @@ DBConnection/proc/Disconnect() return _dm_db_close(_db_con) DBConnection/proc/IsConnected() - if(!sqllogging) + if(!config.sql_enabled) return 0 var/success = _dm_db_is_connected(_db_con) return success diff --git a/code/defines/procs/statistics.dm b/code/defines/procs/statistics.dm index 8eaf7a90a27..e807e4cbbce 100644 --- a/code/defines/procs/statistics.dm +++ b/code/defines/procs/statistics.dm @@ -1,5 +1,5 @@ proc/sql_poll_population() - if(!sqllogging) + if(!config.sql_enabled) return var/admincount = admins.len var/playercount = 0 @@ -18,15 +18,15 @@ proc/sql_poll_population() proc/sql_report_round_start() // TODO - if(!sqllogging) + if(!config.sql_enabled) return proc/sql_report_round_end() // TODO - if(!sqllogging) + if(!config.sql_enabled) return proc/sql_report_death(var/mob/living/carbon/human/H) - if(!sqllogging) + if(!config.sql_enabled) return if(!H) return @@ -60,7 +60,7 @@ proc/sql_report_death(var/mob/living/carbon/human/H) proc/sql_report_cyborg_death(var/mob/living/silicon/robot/H) - if(!sqllogging) + if(!config.sql_enabled) return if(!H) return @@ -94,7 +94,7 @@ proc/sql_report_cyborg_death(var/mob/living/silicon/robot/H) proc/statistic_cycle() - if(!sqllogging) + if(!config.sql_enabled) return while(1) sql_poll_population() diff --git a/code/global.dm b/code/global.dm index ed69df69f45..ccb57713b0e 100644 --- a/code/global.dm +++ b/code/global.dm @@ -187,22 +187,13 @@ var/datum/subsystem/alarm/alarm_manager = new() // Alarm Manager, the manager fo var/list/awaydestinations = list() // Away missions. A list of landmarks that the warpgate can take you to. -// MySQL configuration -var/sqladdress = "localhost" -var/sqlport = "3306" -var/sqldb = "tgstation" -var/sqllogin = "root" -var/sqlpass = "" -var/sqllogging = 0 // Should we log deaths, population stats, etc.? - // For FTP requests. (i.e. downloading runtime logs.) // However it'd be ok to use for accessing attack logs and such too, which are even laggier. var/fileaccess_timer = 0 var/custom_event_msg = null -// Database connections. A connection is established on world creation. +// Database connections. A connection is established along with /hook/startup/proc/load_databases(). // Ideally, the connection dies when the server restarts (After feedback logging.). -// Feedback database. Constructor in /hook/startup/proc/connectDB() var/DBConnection/dbcon // Reference list for disposal sort junctions. Filled up by sorting junction's New() diff --git a/code/world.dm b/code/world.dm index bc23a3a54ea..6148324b93b 100644 --- a/code/world.dm +++ b/code/world.dm @@ -534,7 +534,6 @@ var/world_topic_spam_protect_time = world.timeofday config = new /datum/configuration() config.load("config/config.txt") config.load("config/game_options.txt","game_options") - config.loadsql("config/dbconfig.txt") /hook/startup/proc/loadMods() world.load_mods() @@ -642,12 +641,10 @@ var/world_topic_spam_protect_time = world.timeofday src.status = s #define FAILED_DB_CONNECTION_CUTOFF 5 -var/failed_db_connections = 0 -var/failed_old_db_connections = 0 -/hook/startup/proc/connectDB() - //Construct the database object now that configs are loaded - dbcon = new(sqladdress, sqlport, sqldb, sqllogin, sqlpass) +/hook/startup/proc/load_databases() + //Construct the database object from an init file. + dbcon = initialize_database_object("config/dbconfig.txt") if (!setup_database_connection(dbcon)) world.log << "Your server failed to establish a connection with the feedback database." @@ -655,8 +652,48 @@ var/failed_old_db_connections = 0 world.log << "Feedback database connection established." return 1 +/proc/initialize_database_object(var/filename) + if (!filename) + return 0 + + var/list/data = list("address", "port", "database", "login", "password") + + var/list/Lines = file2list(filename) + for (var/t in Lines) + if (!t) + continue + + t = trim(t) + if (length(t) == 0) + continue + else if (copytext(t, 1, 2) == "#") + continue + + var/pos = findtext(t, " ") + var/name = null + var/value = null + + name = lowertext(copytext(t, 1, pos)) + value = copytext(t, pos + 1) + + if (!name) + continue + + if (name in data) + data[name] = value + else + log_misc("Unknown setting while setting up database connection. Filename: '[filename]', value: '[value]'.") + + //Validate the data before proceeding. + for (var/d in data) + if (!data[d] || data[d] == null) + return 0 + + return new/DBConnection(data["address"], data["port"], data["database"], data["login"], data["password"]) + /proc/setup_database_connection(var/DBConnection/con) if (!con) + error("No DBConnection object passed to setup_database_connection().") return 0 if (con.failed_connections > FAILED_DB_CONNECTION_CUTOFF) //If it failed to establish a connection more than 5 times in a row, don't bother attempting to conenct anymore. @@ -675,6 +712,7 @@ var/failed_old_db_connections = 0 //This proc ensures that the connection to the feedback database (global variable dbcon) is established /proc/establish_db_connection(var/DBConnection/con) if (!con) + error("No DBConnection object passed to establish_db_connection() proc.") return 0 if (con.failed_connections > FAILED_DB_CONNECTION_CUTOFF)