Bastion aims to serve as a highly performant, simplisitic, and expandable World of Warcraft data visualization framework.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Bastion/src/Config/Config.lua

85 lines
1.8 KiB

---@type Tinkr
local Tinkr,
---@class Bastion
Bastion = ...
---@class Bastion.Config
---@field instantiated boolean
---@field config Tinkr.Util.Config.Instance
---@field defaults nil | table
local Config = {
instantiated = false,
}
function Config:__index(k)
local response = Config[k]
if response == nil then
response = rawget(self, k)
end
if response == nil and self.instantiated then
response = self:Read(k)
end
return response
end
function Config:__newindex(key, value)
if self.instantiated then
self:Write(key, value)
else
rawset(self, key, value)
end
end
---@generic D
---@param name string
---@param defaults? D
function Config:New(name, defaults)
---@type Bastion.Config
local self = setmetatable({}, Config)
self.config = Tinkr.Util.Config:New(name)
self.defaults = type(defaults) == "table" and defaults or {}
self.instantiated = true
return self
end
---@generic D
---@param key string
---@param default? D
---@return D
function Config:Read(key, default)
if type(default) == "nil" then
default = self.defaults[key]
end
return self.config:Read(key, default)
end
function Config:Reset()
if type(self.defaults) == "table" then
-- Clear all values currently in the config.
for key, _ in pairs(self.config.data) do
self:Write(key, nil)
end
-- Use default table to write new defaults.
for key, value in pairs(self.defaults) do
self:Write(key, value)
end
return true
end
return false
end
---@param key string
---@param value any
function Config:Write(key, value)
self.config:Write(key, value)
end
---@param key string
function Config:Sync(key)
self.config:Sync(key)
end
Bastion.Config = Config