-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinit.lua
52 lines (39 loc) · 1.08 KB
/
init.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
require("pluto")
local json = require("json-lua")
local inited = false
local localStorage = {
data = {},
filename = "localStorage.json",
autoSave = true
}
function localStorage.init(autoSave, filename)
localStorage.filename = filename or localStorage.filename
local fn = localStorage.filename
localStorage.autoSave = autoSave or true
local isFile = love.filesystem.getInfo(fn)
if (isFile) then
local file = love.filesystem.read("string", fn)
localStorage.data = json.decode(file)
end
inited = true
end
function localStorage.get(fieldName, default)
if not inited then
localStorage.init()
end
return localStorage.data[fieldName] or default
end
function localStorage.set(fieldName, value)
if not inited then
localStorage.init()
end
localStorage.data[fieldName] = value
if localStorage.autoSave then
localStorage.save()
end
end
function localStorage.save()
local s = json.encode(localStorage.data)
love.filesystem.write(localStorage.filename, s)
end
return localStorage