-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.lua
101 lines (84 loc) · 2.98 KB
/
main.lua
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
93
94
95
96
97
98
99
100
101
DataManager = {}
DataManager.configPrefix = "custom/__config_"
DataManager.dataPrefix = "custom/__data_"
local function getConfigPath(scriptName)
return DataManager.configPrefix .. scriptName .. ".json"
end
local function getDataPath(scriptName)
return DataManager.dataPrefix .. scriptName .. ".json"
end
if jsonConfig then
function DataManager.saveConfiguration(scriptName, config, keyOrder)
end
function DataManager.loadConfiguration(scriptName, defaultConfig, keyOrder)
return jsonConfig.Load(scriptName, defaultConfig, keyOrder)
end
else
function DataManager.saveConfiguration(scriptName, config, keyOrder)
local filePath = getConfigPath(scriptName)
return jsonInterface.save(filePath, config, keyOrder)
end
function DataManager.loadConfiguration(scriptName, defaultConfig, keyOrder)
local filePath = getConfigPath(scriptName)
local config = jsonInterface.load(filePath)
if config == nil then
config = defaultConfig
end
DataManager.saveConfiguration(scriptName, config, keyOrder)
return config
end
jsonConfig = {}
function jsonConfig.Load(name, default, keyOrderArray)
return DataManager.loadConfiguration(name, default, keyOrderArray)
end
end
if storage then
-- storage data gets saved automatically
function DataManager.saveData(scriptName, data, keyOrder)end
function DataManager.loadData(scriptName, defaultData, keyOrder)
storage.Load(scriptName, defaultData)
end
else
function DataManager.saveData(scriptName, data, keyOrder)
local filePath = getDataPath(scriptName)
return jsonInterface.save(filePath, data, keyOrder)
end
function DataManager.loadData(scriptName, defaultData, keyOrder)
local filePath = getDataPath(scriptName)
local data = jsonInterface.load(filePath)
if data == nil then
data = defaultData
end
DataManager.saveData(scriptName, data, keyOrder)
return data
end
storage = {
data = {}
}
function storage.Save(key)
DataManager.saveData(key, storage.data[key])
end
function storage.SaveAll()
for key in pairs(storage.data) do
storage.Save(key)
end
end
function storage.SaveAllAsync()
return storage.SaveAll() -- no async in 0.7
end
function storage.Load(key, default)
if not storage.data[key] then
local eventStatus = customEventHooks.triggerValidators('OnStorageLoad', {key})
if eventStatus.validDefaultHandler then
storage.data[key] = DataManager.loadData(key, default)
end
customEventHooks.triggerHandlers('OnStorageLoad', eventStatus, {key})
end
return storage.data[key]
end
customEventHooks.registerHandler("OnServerExit", function(eventStatus)
if not eventStatus.validDefaultHandler then return end
storage.SaveAll()
end)
end
return DataManager