recipe books overhaul

This commit is contained in:
kepler155c@gmail.com
2019-01-08 18:24:01 -05:00
parent c5c79e6de3
commit df8855acd5
5 changed files with 169 additions and 61 deletions

View File

@@ -1,38 +1,57 @@
local JSON = require('json')
local TableDB = require('tableDB')
local Util = require('util')
local fs = _G.fs
local NAME_DIR = '/packages/core/etc/names'
local CORE_DIR = '/packages/core/etc/names'
local USER_DIR = '/usr/etc/names'
local nameDB = TableDB()
function nameDB:load()
function nameDB:loadDirectory(directory)
if fs.exists(directory) then
local files = fs.list(directory)
table.sort(files)
local files = fs.list(NAME_DIR)
table.sort(files)
for _,file in ipairs(files) do
local mod = file:match('(%S+).json')
if mod then
local blocks = JSON.decodeFromFile(fs.combine(directory, file))
for _,file in ipairs(files) do
local mod = file:match('(%S+).json')
local blocks = JSON.decodeFromFile(fs.combine(NAME_DIR, file))
if not blocks then
error('Unable to read ' .. fs.combine(directory, file))
end
if not blocks then
error('Unable to read ' .. fs.combine(NAME_DIR, file))
end
for strId, block in pairs(blocks) do
strId = string.format('%s:%s', mod, strId)
if type(block.name) == 'string' then
self.data[strId .. ':0'] = block.name
else
for nid,name in pairs(block.name) do
self.data[strId .. ':' .. (nid-1)] = name
end
end
end
for strId, block in pairs(blocks) do
strId = string.format('%s:%s', mod, strId)
if type(block.name) == 'string' then
self.data[strId .. ':0'] = block.name
else
for nid,name in pairs(block.name) do
self.data[strId .. ':' .. (nid-1)] = name
elseif file:match('(%S+).db') then
local names = Util.readTable(fs.combine(directory, file))
if not names then
error('Unable to read ' .. fs.combine(directory, file))
end
for key,name in pairs(names) do
self.data[key] = name
end
end
end
end
end
function nameDB:load()
self:loadDirectory(CORE_DIR)
self:loadDirectory(USER_DIR)
end
function nameDB:getName(strId)
return self.data[strId] or strId
end