diff --git a/gamemode/core/libs/sv_database.lua b/gamemode/core/libs/sv_database.lua index 6a1789a1..902d9100 100644 --- a/gamemode/core/libs/sv_database.lua +++ b/gamemode/core/libs/sv_database.lua @@ -1,8 +1,6 @@ nut.db = nut.db or {} nut.db.queryQueue = nut.db.queue or {} -nut.util.include("nutscript/gamemode/config/sv_database.lua") - local function ThrowQueryFault(query, fault) MsgC(Color(255, 0, 0), "* "..query.."\n") MsgC(Color(255, 0, 0), fault.."\n") @@ -76,10 +74,8 @@ modules.tmysql4 = { local queryStatus, queryError, affected, lastID, time, data = result.status, result.error, result.affected, result.lastid, result.time, result.data - if (queryStatus and queryStatus == true) then - if (callback) then - callback(data, lastID) - end + if (queryStatus and queryStatus == true and callback) then + callback(data, lastID) end else file.Write("nut_queryerror.txt", query) @@ -189,7 +185,7 @@ modules.mysqloo = { for k, db in pairs(nut.db.pool) do local queueSize = db:queueSize() - if (!lowest || queueSize < lowestCount) then + if (!lowest or queueSize < lowestCount) then lowest = db lowestCount = queueSize lowestIndex = k @@ -207,7 +203,7 @@ modules.mysqloo = { return setNetVar("dbError", system.IsWindows() and "Server is missing VC++ redistributables!" or "Server is missing binaries for mysqloo!") end - if (mysqloo.VERSION != "9" || !mysqloo.MINOR_VERSION || tonumber(mysqloo.MINOR_VERSION) < 1) then + if (mysqloo.VERSION != "9" or !mysqloo.MINOR_VERSION or tonumber(mysqloo.MINOR_VERSION) < 1) then MsgC(Color(255, 0, 0), "You are using an outdated mysqloo version\n") MsgC(Color(255, 0, 0), "Download the latest mysqloo9 from here\n") MsgC(Color(86, 156, 214), "https://github.com/syl0r/MySQLOO/releases") @@ -251,7 +247,7 @@ modules.mysqloo = { if (callback) then callback() end - + hook.Run("OnMySQLOOConnected") end end @@ -356,7 +352,6 @@ CREATE TABLE IF NOT EXISTS `nut_players` ( `_intro` BINARY(1) NULL DEFAULT 0, PRIMARY KEY (`_steamID`) ); - CREATE TABLE IF NOT EXISTS `nut_characters` ( `_id` INT(12) NOT NULL AUTO_INCREMENT, `_steamID` VARCHAR(20) NOT NULL COLLATE 'utf8mb4_general_ci', @@ -372,14 +367,12 @@ CREATE TABLE IF NOT EXISTS `nut_characters` ( `_faction` VARCHAR(24) DEFAULT NULL COLLATE 'utf8mb4_general_ci', PRIMARY KEY (`_id`) ); - CREATE TABLE IF NOT EXISTS `nut_inventories` ( `_invID` INT(12) NOT NULL AUTO_INCREMENT, `_charID` INT(12) NULL DEFAULT NULL, `_invType` VARCHAR(24) NULL DEFAULT NULL COLLATE 'utf8mb4_general_ci', PRIMARY KEY (`_invID`) ); - CREATE TABLE IF NOT EXISTS `nut_items` ( `_itemID` INT(12) NOT NULL AUTO_INCREMENT, `_invID` INT(12) NULL DEFAULT NULL, @@ -390,7 +383,6 @@ CREATE TABLE IF NOT EXISTS `nut_items` ( `_y` INT(4), PRIMARY KEY (`_itemID`) ); - CREATE TABLE IF NOT EXISTS `nut_invdata` ( `_invID` INT(12) NOT NULL, `_key` VARCHAR(32) NOT NULL COLLATE 'utf8mb4_general_ci', @@ -409,7 +401,6 @@ CREATE TABLE IF NOT EXISTS nut_players ( _data varchar, _intro binary ); - CREATE TABLE IF NOT EXISTS nut_characters ( _id integer PRIMARY KEY AUTOINCREMENT, _steamID varchar, @@ -424,13 +415,11 @@ CREATE TABLE IF NOT EXISTS nut_characters ( _money varchar, _faction varchar ); - CREATE TABLE IF NOT EXISTS nut_inventories ( _invID integer PRIMARY KEY AUTOINCREMENT, _charID integer, _invType varchar ); - CREATE TABLE IF NOT EXISTS nut_items ( _itemID integer PRIMARY KEY AUTOINCREMENT, _invID integer, @@ -440,7 +429,6 @@ CREATE TABLE IF NOT EXISTS nut_items ( _x integer, _y integer ); - CREATE TABLE IF NOT EXISTS nut_invdata ( _invID integer, _key text, @@ -684,6 +672,46 @@ function nut.db.delete(dbTable, condition) return d end +local defaultConfig = { + module = "sqlite", + hostname = "127.0.0.1", + username = "", + password = "", + database = "", + port = 3306 +} + +local validConfig = { + engine.ActiveGamemode().."/database.json", + engine.ActiveGamemode().."/nutscript.json", + "nutscript/database.json", + "nutscript/nutscript.json" +} + +function GM:SetupDatabase() + for _, configPath in ipairs(validConfig) do + local config = file.Read(tostring(configPath), "LUA") + + if (config) then + nut.db.config = config + + for k, v in pairs(util.JSONToTable(nut.db.config)) do + nut.db[k] = v + end + + break + end + end + + if (not nut.db.config) then + MsgC(Color(255, 0, 0), "Database not configured.\n") + + for k, v in pairs(defaultConfig) do + nut.db[k] = v + end + end +end + function GM:OnMySQLOOConnected() hook.Run("RegisterPreparedStatements") MYSQLOO_PREPARED = true