summaryrefslogtreecommitdiff
path: root/gamemode/server/database.lua
blob: 9f8608dba69c44c433ac1b4026bc50b919340ab3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
--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
--]]