-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpersistent_storage.lua
41 lines (32 loc) · 1.15 KB
/
persistent_storage.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
persistent_storage = {}
local binser = require("binser.binser")
local file_name = "storage.ser"
local function load_storage()
file_info = love.filesystem.getInfo(file_name)
if file_info == nil or not file_info["type"] == "file" then
persistent_storage.storage = {}
else
local des = love.filesystem.read(file_name)
persistent_storage.storage, len = binser.deserialize(des)
-- for some reason, a table is returned by binser, so we need to get the storage out of the table
persistent_storage.storage = persistent_storage.storage[1]
end
end
local function save_storage()
return love.filesystem.write(file_name, binser.serialize(persistent_storage.storage))
end
persistent_storage.get = function(key, default)
load_storage()
if not persistent_storage.storage[key] then
return default
end
return persistent_storage.storage[key]
end
persistent_storage.set = function(key, value)
--- refresh storage in case none existed
load_storage()
--- update value
persistent_storage.storage[key] = value
--- save storage table on disk immediately
return save_storage()
end