-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsave.lua2p
76 lines (65 loc) · 1.85 KB
/
save.lua2p
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
local Log = require("modules.log.log")
local Utils = require("utils")
local Save = {
data = {
splash_done = false,
intro_done = false,
outside_intro_done = false,
},
checkpoints = {},
valid_checkpoints = false,
exists = false,
}
local save_filename = $_SAVE_FILENAME
local key_filename = $_SAVE_KEY
local function validate_checkpoints(data)
@@assert(type(data) == "table")
end
function Save.init()
local content, exists = Utils.file.read(save_filename)
local key, exists_key = Utils.file.read(key_filename)
if exists and exists_key then
local hashed = love.data.hash("sha256", content)
if key == hashed then
local data = Utils.serial.de(content)
Save.data = data
validate_checkpoints(data)
else
local error_msg = format("Data integrity is not valid.\n\
Please do not modify the files: '%s' and '%s'.\n\
Please delete the files in '%s' and restart the game",
save_filename, key_filename, love.filesystem.getSaveDirectory())
error(error_msg)
end
else
Save.overwrite()
end
Save.exists = exists
Log.info("Save Data:", pretty.string(Save.data))
end
function Save.overwrite()
local data = Utils.serial.write(save_filename, Save.data)
local hashed = love.data.hash("sha256", data)
Utils.file.write(key_filename, hashed)
Log.info("Save overwritten")
end
function Save.toggle_flag(id, should_overwrite)
@@assert(type(id) == "string")
@@assert(Save.data[id] ~= nil)
@@sassert(should_overwrite, type(should_overwrite) == "boolean")
Save.data[id] = not Save.data[id]
if should_overwrite then
Save.overwrite()
end
end
function Save.set_flag(id, value, should_overwrite)
@@assert(type(id) == "string")
@@assert(Save.data[id] ~= nil)
@@assert(type(value) == "boolean")
@@sassert(should_overwrite, type(should_overwrite) == "boolean")
Save.data[id] = value
if should_overwrite then
Save.overwrite()
end
end
return Save