-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsettings.lua
More file actions
148 lines (124 loc) · 3.25 KB
/
settings.lua
File metadata and controls
148 lines (124 loc) · 3.25 KB
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
---@class SettingsManager
SettingsManager = {}
-- TODO:
-- Check how / if defines (Details) are saved
local textutils = require("textutils")
---@alias settingType
---| "string"
---| "number"
---| "boolean"
---| "table"
---@class SettingDetail
---@field default any
---@field description string
---@field type settingType
---@type {[string]: SettingDetail}
local details = {}
---@type {[string]: any}
local settings = {}
local defaultPath = "tmp/.settings"
local function reserialize(value)
if type(value) ~= "table" then return value end
return textutils.unserialize(textutils.serialize(value))
end
---@param name string
---@param options SettingDetail
function SettingsManager.define(name, options)
details[name] = {}
if options ~= nil then
details[name] = options
end
end
---@param name string
function SettingsManager.undefine(name)
details[name] = nil
end
---@param name string
---@param value any
function SettingsManager.set(name, value)
settings[name] = value
end
---@param name string
---@param default any
---@return any
function SettingsManager.get(name, default)
if settings[name] ~= nil then
return settings[name]
end
if default ~= nil then
return default
end
return (details[name] and details[name]["default"]) or nil
end
---@param name string
---@return any
function SettingsManager.getDetails(name)
local tmp = details[name]
if settings[name] ~= nil then
if tmp == nil then
tmp = {}
end
tmp["value"] = settings[name]
end
return tmp
end
---@param name string
function SettingsManager.unset(name)
settings[name] = nil
end
--- clears Settings (values only)
function SettingsManager.clear()
settings = {}
end
--- clears Details (Defines only)
--- does not exist in cc, but might be usefull for testing
function SettingsManager.clearDetails()
details = {}
end
---@return table<string>
function SettingsManager.getNames()
---@source https://github.com/cc-tweaked/CC-Tweaked/blob/876fd8ddb805365c33942afb81d6da7cbd0cbca5/projects/core/src/main/resources/data/computercraft/lua/rom/apis/settings.lua#L199
local result, n = {}, 1
for k in pairs(details) do
result[n], n = k, n + 1
end
for k in pairs(settings) do
if not details[k] then result[n], n = k, n + 1 end
end
table.sort(result)
return result
end
---@param path string | nil
function SettingsManager.load(path)
path = path or defaultPath
local file = io.open(path, "r")
if file == nil then
return false
end
local content = file:read("*all")
file:close()
local result = textutils.unserialize(content)
if not result or (type(result) ~= "table") then
return false
end
settings = result
return true
end
---@param path string | nil
function SettingsManager.save(path)
if path == nil then
path = defaultPath
end
if not os.execute("mkdir -p $(dirname ".. path .. ")") then
error("Could not create the file")
end
local result = textutils.serialize(settings)
local file = io.open(path, "w")
if file == nil then
error("Could not save to File: "..path)
end
file:write(result)
file:close()
return true
end
return SettingsManager