--Provides an interface to store data in a database of some kind --[[ Uses: GM.StorageMethod 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) 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) local storetext = convertTableText(table) if(GAMEMODE.StorageMethod == "File:Compressed") then storetext = util.Compress(storetext) 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 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 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 end --[[ local function convertTableStatic(tbl) if (GM.StorageStaticMap == nil) then print("---------ERROR: database.lua-----------") print("\tGM.StorageMethod is set to \"Static Map\", but GM.StoreageStaticMap is not defined!") return end end local function convertTableCompressed(tbl) end local function parseStaticTable(tbl) end --]]