-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmod_storage.lua
71 lines (64 loc) · 1.84 KB
/
mod_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
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
----------------------
-- saving functions --
----------------------
---@param key string
---@param value string
function save_string(key, value)
-- save directly to storage, no conversion needed
mod_storage_save(tostring(key), encrypt_string(value))
end
---@param key string
---@param value boolean
function save_bool(key, value)
-- convert value to string
local strValue = tostring(value)
-- add tag_bool_ to the beginning
strValue = "tag_bool_" .. strValue
-- save
mod_storage_save(tostring(key), encrypt_string(strValue))
end
---@param key string
---@param value integer
function save_int(key, value)
-- convert value to string
local strValue = tostring(value)
-- add tag_int_ to the beginning
strValue = "tag_int_" .. strValue
-- save
mod_storage_save(tostring(key), encrypt_string(strValue))
end
-----------------------
-- loading functions --
-----------------------
---@param key string
---@return string
function load_string(key)
-- return storage data
return decrypt_string(mod_storage_load(key))
end
---@param key string
---@return boolean|nil
function load_bool(key)
-- get storage data
local data = decrypt_string(mod_storage_load(key))
-- sanity check
if data == nil then return nil end
if data == "" then return nil end
-- remove the tag_bool_ part from the string
data = data:gsub("tag_bool_", "")
-- return converted bool from string
return tobool(data)
end
---@param key string
---@return integer|nil
function load_int(key)
-- get storage data
local data = decrypt_string(mod_storage_load(tostring(key)))
-- sanity check
if data == nil then return nil end
if data == "" then return nil end
-- remove the tag_int_ part from the string
data = data:gsub("tag_int_", "")
-- return converted int from string
return tonumber(data)
end