mirror of
https://github.com/appgurueu/modlib.git
synced 2024-11-29 18:53:44 +01:00
Tabs
This commit is contained in:
parent
1ed07d6015
commit
7e00e3a237
2
init.lua
2
init.lua
@ -78,7 +78,7 @@ end
|
|||||||
local load_module, get_resource, loadfile_exports
|
local load_module, get_resource, loadfile_exports
|
||||||
modlib = setmetatable({
|
modlib = setmetatable({
|
||||||
-- TODO bump on release
|
-- TODO bump on release
|
||||||
version = 64,
|
version = 65,
|
||||||
modname = minetest and minetest.get_current_modname(),
|
modname = minetest and minetest.get_current_modname(),
|
||||||
dir_delim = rawget(_G, "DIR_DELIM") or "/",
|
dir_delim = rawget(_G, "DIR_DELIM") or "/",
|
||||||
_RG = setmetatable({}, {
|
_RG = setmetatable({}, {
|
||||||
|
192
persistence.lua
192
persistence.lua
@ -3,140 +3,140 @@ local files = {}
|
|||||||
local metatable = {__index = lua_log_file}
|
local metatable = {__index = lua_log_file}
|
||||||
|
|
||||||
function lua_log_file.new(file_path, root)
|
function lua_log_file.new(file_path, root)
|
||||||
local self = setmetatable({file_path = assert(file_path), root = root}, metatable)
|
local self = setmetatable({file_path = assert(file_path), root = root}, metatable)
|
||||||
if minetest then
|
if minetest then
|
||||||
files[self] = true
|
files[self] = true
|
||||||
end
|
end
|
||||||
return self
|
return self
|
||||||
end
|
end
|
||||||
|
|
||||||
function lua_log_file:load()
|
function lua_log_file:load()
|
||||||
-- Bytecode is blocked by the engine
|
-- Bytecode is blocked by the engine
|
||||||
local read = assert(loadfile(self.file_path))
|
local read = assert(loadfile(self.file_path))
|
||||||
local env = {}
|
local env = {}
|
||||||
setfenv(read, env)
|
setfenv(read, env)
|
||||||
read()
|
read()
|
||||||
env.R = env.R or {{}}
|
env.R = env.R or {{}}
|
||||||
self.reference_count = #env.R
|
self.reference_count = #env.R
|
||||||
self.root = env.R[1]
|
self.root = env.R[1]
|
||||||
self.references = modlib.table.flip(env.R)
|
self.references = modlib.table.flip(env.R)
|
||||||
end
|
end
|
||||||
|
|
||||||
function lua_log_file:open()
|
function lua_log_file:open()
|
||||||
self.file = io.open(self.file_path, "a+")
|
self.file = io.open(self.file_path, "a+")
|
||||||
end
|
end
|
||||||
|
|
||||||
function lua_log_file:init()
|
function lua_log_file:init()
|
||||||
if modlib.file.exists(self.file_path) then
|
if modlib.file.exists(self.file_path) then
|
||||||
self:load()
|
self:load()
|
||||||
self:_rewrite()
|
self:_rewrite()
|
||||||
self:open()
|
self:open()
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
self:open()
|
self:open()
|
||||||
self.root = {}
|
self.root = {}
|
||||||
self:_write()
|
self:_write()
|
||||||
end
|
end
|
||||||
|
|
||||||
function lua_log_file:log(statement)
|
function lua_log_file:log(statement)
|
||||||
self.file:write(statement)
|
self.file:write(statement)
|
||||||
self.file:write"\n"
|
self.file:write"\n"
|
||||||
end
|
end
|
||||||
|
|
||||||
function lua_log_file:flush()
|
function lua_log_file:flush()
|
||||||
self.file:flush()
|
self.file:flush()
|
||||||
end
|
end
|
||||||
|
|
||||||
function lua_log_file:close()
|
function lua_log_file:close()
|
||||||
self.file:close()
|
self.file:close()
|
||||||
self.file = nil
|
self.file = nil
|
||||||
files[self] = nil
|
files[self] = nil
|
||||||
end
|
end
|
||||||
|
|
||||||
if minetest then
|
if minetest then
|
||||||
minetest.register_on_shutdown(function()
|
minetest.register_on_shutdown(function()
|
||||||
for self in pairs(files) do
|
for self in pairs(files) do
|
||||||
self.file:close()
|
self.file:close()
|
||||||
end
|
end
|
||||||
end)
|
end)
|
||||||
end
|
end
|
||||||
|
|
||||||
-- TODO use shorthand notations
|
-- TODO use shorthand notations
|
||||||
function lua_log_file:dump(value)
|
function lua_log_file:dump(value)
|
||||||
if value == nil then
|
if value == nil then
|
||||||
return "nil"
|
return "nil"
|
||||||
end
|
end
|
||||||
if value == true then
|
if value == true then
|
||||||
return "true"
|
return "true"
|
||||||
end
|
end
|
||||||
if value == false then
|
if value == false then
|
||||||
return "false"
|
return "false"
|
||||||
end
|
end
|
||||||
if value ~= value then
|
if value ~= value then
|
||||||
-- nan
|
-- nan
|
||||||
return "0/0"
|
return "0/0"
|
||||||
end
|
end
|
||||||
local _type = type(value)
|
local _type = type(value)
|
||||||
if _type == "number" then
|
if _type == "number" then
|
||||||
return ("%.17g"):format(value)
|
return ("%.17g"):format(value)
|
||||||
end
|
end
|
||||||
if self.references[value] then
|
if self.references[value] then
|
||||||
return "R[" .. self.references[value] .. "]"
|
return "R[" .. self.references[value] .. "]"
|
||||||
end
|
end
|
||||||
self.reference_count = self.reference_count + 1
|
self.reference_count = self.reference_count + 1
|
||||||
local reference = self.reference_count
|
local reference = self.reference_count
|
||||||
self.references[value] = reference
|
self.references[value] = reference
|
||||||
local formatted
|
local formatted
|
||||||
if _type == "string" then
|
if _type == "string" then
|
||||||
formatted = ("%q"):format(value)
|
formatted = ("%q"):format(value)
|
||||||
elseif _type == "table" then
|
elseif _type == "table" then
|
||||||
local entries = {}
|
local entries = {}
|
||||||
for _, value in ipairs(value) do
|
for _, value in ipairs(value) do
|
||||||
table.insert(entries, self:dump(value))
|
table.insert(entries, self:dump(value))
|
||||||
end
|
end
|
||||||
for key, value in pairs(value) do
|
for key, value in pairs(value) do
|
||||||
if type(key) ~= "number" or key % 1 ~= 0 or key < 1 or key > #value then
|
if type(key) ~= "number" or key % 1 ~= 0 or key < 1 or key > #value then
|
||||||
table.insert(entries, "[" .. self:dump(key) .. "]=" .. self:dump(value))
|
table.insert(entries, "[" .. self:dump(key) .. "]=" .. self:dump(value))
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
formatted = "{" .. table.concat(entries, ";") .. "}"
|
formatted = "{" .. table.concat(entries, ";") .. "}"
|
||||||
else
|
else
|
||||||
error("unsupported type: " .. _type)
|
error("unsupported type: " .. _type)
|
||||||
end
|
end
|
||||||
local key = "R[" .. reference .."]"
|
local key = "R[" .. reference .."]"
|
||||||
self:log(key .. "=" .. formatted)
|
self:log(key .. "=" .. formatted)
|
||||||
return key
|
return key
|
||||||
end
|
end
|
||||||
|
|
||||||
function lua_log_file:set(table, key, value)
|
function lua_log_file:set(table, key, value)
|
||||||
table[key] = value
|
table[key] = value
|
||||||
if not self.references[table] then
|
if not self.references[table] then
|
||||||
error"orphan table"
|
error"orphan table"
|
||||||
end
|
end
|
||||||
self:log(self:dump(table) .. "[" .. self:dump(key) .. "]=" .. self:dump(value))
|
self:log(self:dump(table) .. "[" .. self:dump(key) .. "]=" .. self:dump(value))
|
||||||
end
|
end
|
||||||
|
|
||||||
function lua_log_file:set_root(key, value)
|
function lua_log_file:set_root(key, value)
|
||||||
return self:set(self.root, key, value)
|
return self:set(self.root, key, value)
|
||||||
end
|
end
|
||||||
|
|
||||||
function lua_log_file:_write()
|
function lua_log_file:_write()
|
||||||
self.references = {}
|
self.references = {}
|
||||||
self.reference_count = 0
|
self.reference_count = 0
|
||||||
self:log"R={}"
|
self:log"R={}"
|
||||||
self:dump(self.root)
|
self:dump(self.root)
|
||||||
end
|
end
|
||||||
|
|
||||||
function lua_log_file:_rewrite()
|
function lua_log_file:_rewrite()
|
||||||
self.file = io.open(self.file_path, "w+")
|
self.file = io.open(self.file_path, "w+")
|
||||||
self:_write()
|
self:_write()
|
||||||
self.file:close()
|
self.file:close()
|
||||||
end
|
end
|
||||||
|
|
||||||
function lua_log_file:rewrite()
|
function lua_log_file:rewrite()
|
||||||
if self.file then
|
if self.file then
|
||||||
self.file:close()
|
self.file:close()
|
||||||
end
|
end
|
||||||
self:_rewrite()
|
self:_rewrite()
|
||||||
self.file = io.open(self.file_path, "a+")
|
self.file = io.open(self.file_path, "a+")
|
||||||
end
|
end
|
Loading…
Reference in New Issue
Block a user