From 6f25e16528f9f035c784055304b9f07c59f6782d Mon Sep 17 00:00:00 2001 From: Alexander Pickering Date: Sat, 21 May 2016 19:29:52 -0400 Subject: First pass at createing a databaseing system --- gamemode/server/database.lua | 106 ++++++++++++++++++++----------------------- 1 file changed, 48 insertions(+), 58 deletions(-) (limited to 'gamemode/server/database.lua') diff --git a/gamemode/server/database.lua b/gamemode/server/database.lua index 20efafc..9f8608d 100644 --- a/gamemode/server/database.lua +++ b/gamemode/server/database.lua @@ -2,84 +2,74 @@ --[[ Uses: GM.StorageMethod - Can be: "Text", "Static Map", "LZWCompressed" - If method is "Static Map", then GM.StoreageStaticMap must be a table of strings to string ID's. - GM.StorageBackend - Can be: "File" + Can be: "File:Text", "File:Compressed" + GM.Compressor + Can be: "GLON", "VON", "PON", "JSON" GM.StorageDebug Can be: true, false Enables extra debugging to find errors easily ]] +--[[ +Provides: + loadTable(uniqueID) + loads a table with the given unique id, and returns it + storeTable(uniqueID, table) + stores the given table into the database, needs a uniqueid to find it again. +]] print("Hello from database.lua!") +GAMEMODE = GAMEMODE or {} function loadTable(uniqueIdentifier) - - return nil + local rfile = file.Open("gmstranded/" .. uniqueIdentifier .. ".txt", "r", "DATA") + local text = rfile:Read(rfile:Size()) + if(GAMEMODE.StorageMethod == "File:Compressed") then + text = util.Decompress(text) + end + return convertTextTable(text) end function storeTable(uniqueIdentifier, table) - if (GM.StorageDebug) then - print("Asked to store:") - PrintTable(table) + local storetext = convertTableText(table) + if(GAMEMODE.StorageMethod == "File:Compressed") then + storetext = util.Compress(storetext) end - if (GM.StorageDebug) then - for k,v in pairs(tbl) do - if (isfunction(v)) then - print("Database.lua error: tried to save a table that has a function in it!") - PrintTable(tbl) - print("At field:") - print(k) - end - end + if !(file.IsDir("gmstranded","DATA")) then + file.CreateDir("gmstranded") end - + local tfile = file.Open("gmstranded/" .. uniqueIdentifier .. ".txt", "w","DATA") + tfile:Write(storetext) + tfile:Flush() + tfile:Close() end ---Remove symbols that might be used as part of the storage scheme -local function encodeSymbolic(mstring) - local replacements = { - {"\\", "\\\\"}, - {"{","\\{"}, - {"}","\\}"}, - {"(","\\("}, - {")","\\)"}, - } - local ostring = mstring - for k,v in pairs(replacements) do - ostring = string.Replace(ostring,v[1],v[2]) +function convertTableText(tbl) + if(GAMEMODE.Compressor == "GLON") then + return glon.encode(tbl) + elseif(GAMEMODE.Compressor == "VON") then + return von.serialize(tbl) + elseif(GAMEMODE.Compressor == "PON") then + return pon.encode(tbl) + elseif(GAMEMODE.Compressor == "JSON") then + return util.TableToJSON(tbl) + else + error("GM.Compressor is not set correctly! It is:" .. GM.Compressor .. " but should be one of:GLON,VON,PON,JSON") end end -local function convertTableText(tbl) - local output = "t(" - local function addtoout(k) - if (istable(k)) then - output = output .. convertTableText(k) - elseif (isstring(k)) then - output = output .. "s(" .. encodeSymbolic(k) .. ")" - elseif (isnumber(k)) then - output = output .. "n(" .. k .. ")" - elseif (isbool(k)) then - if (k) then output = output .. "b(true)" - else output = output .. "b(false)" end - elseif (isangle(k)) then - output = output .. "a(" .. k.p .. "," .. k.y .. "," .. k.r .. ")" - elseif (isvector(k)) then - output = output .. "v(" .. k.x .. "," .. k.y .. "," .. k.z .. ")" - else - assert(true,"Tried to store unknown value type:") - printi(k) - end - end - for k,v in pairs(tbl) do - addtoout(k) - output = output .. ":" - addtoout(v) +function convertTextTable(text) + if(GAMEMODE.Compressor == "GLON") then + return glon.decode(text) + elseif(GAMEMODE.Compressor == "VON") then + return von.deserialize(text) + elseif(GAMEMODE.Compressor == "PON") then + return pon.decode(text) + elseif(GAMEMODE.Compressor == "JSON") then + return util.JSONToTable(text) + else + error("GM.Compressor is not set correctly! It is:" .. GM.Compressor .. " but should be one of:GLON,VON,PON,JSON") end - output = output .. ")" end - --[[ local function convertTableStatic(tbl) if (GM.StorageStaticMap == nil) then -- cgit v1.2.3-70-g09d2