recipe books overhaul
This commit is contained in:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user