-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathuser_config.lua
53 lines (42 loc) · 1.27 KB
/
user_config.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
local user_config = {}
local log = require "log"
local json = require "json"
user_config.config_file_name = "user_config.json"
user_config.config = {}
function user_config.get(key)
return user_config.config[key]
end
function user_config.set(key, value)
user_config.config[key] = value
end
function user_config.set_default(key, default_value)
if user_config.config[key] == nil then
user_config.config[key] = default_value
end
end
function user_config.save_to_file()
local encoded = json.encode(user_config.config)
local success, message = love.filesystem.write(user_config.config_file_name, encoded)
if not success then
log.error("Failed to save user config: " .. message)
return false
end
return true
end
function user_config.load_from_file()
local content, errormsg = love.filesystem.read(user_config.config_file_name)
if content == nil then
log.error("Failed to load user config: " .. errormsg)
return false
else
local success, result = pcall(json.decode, content)
if success then
user_config.config = result
return true
else
log.error("json decode error: " .. result)
return false
end
end
end
return user_config