blob: 8dd85f2db612382327d9b596d673c55777b6a114 (
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
|
local util = require("util")
local function save_file_write(filename, data)
os.rename(filename, filename..".bak")
local f = io.open(filename, "w")
if not f then
return false
end
f:write(util.json_enc(data))
f:close()
print("[save_file] saved to " .. filename)
return true
end
local function save_file_read(filename)
local data
local f = io.open(filename, "r")
if f then
data = util.json_dec(f:read("*all"))
f:close()
if not data then
return nil, "save_corrupted"
end
print("[save_file] loaded " .. filename)
else
data = {}
end
if not save_file_write(filename, data) then
return nil, "save_failed_write"
end
return data
end
return {
write = save_file_write,
read = save_file_read,
}
|