recipe books overhaul
This commit is contained in:
@@ -1,38 +1,57 @@
|
|||||||
local JSON = require('json')
|
local JSON = require('json')
|
||||||
local TableDB = require('tableDB')
|
local TableDB = require('tableDB')
|
||||||
|
local Util = require('util')
|
||||||
|
|
||||||
local fs = _G.fs
|
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()
|
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)
|
for _,file in ipairs(files) do
|
||||||
table.sort(files)
|
local mod = file:match('(%S+).json')
|
||||||
|
if mod then
|
||||||
|
local blocks = JSON.decodeFromFile(fs.combine(directory, file))
|
||||||
|
|
||||||
for _,file in ipairs(files) do
|
if not blocks then
|
||||||
local mod = file:match('(%S+).json')
|
error('Unable to read ' .. fs.combine(directory, file))
|
||||||
local blocks = JSON.decodeFromFile(fs.combine(NAME_DIR, file))
|
end
|
||||||
|
|
||||||
if not blocks then
|
for strId, block in pairs(blocks) do
|
||||||
error('Unable to read ' .. fs.combine(NAME_DIR, file))
|
strId = string.format('%s:%s', mod, strId)
|
||||||
end
|
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
|
elseif file:match('(%S+).db') then
|
||||||
strId = string.format('%s:%s', mod, strId)
|
local names = Util.readTable(fs.combine(directory, file))
|
||||||
if type(block.name) == 'string' then
|
if not names then
|
||||||
self.data[strId .. ':0'] = block.name
|
error('Unable to read ' .. fs.combine(directory, file))
|
||||||
else
|
end
|
||||||
for nid,name in pairs(block.name) do
|
for key,name in pairs(names) do
|
||||||
self.data[strId .. ':' .. (nid-1)] = name
|
self.data[key] = name
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
function nameDB:load()
|
||||||
|
self:loadDirectory(CORE_DIR)
|
||||||
|
self:loadDirectory(USER_DIR)
|
||||||
|
end
|
||||||
|
|
||||||
function nameDB:getName(strId)
|
function nameDB:getName(strId)
|
||||||
return self.data[strId] or strId
|
return self.data[strId] or strId
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -11,6 +11,7 @@ local Craft = {
|
|||||||
STATUS_SUCCESS = 'success',
|
STATUS_SUCCESS = 'success',
|
||||||
|
|
||||||
RECIPES_DIR = 'packages/core/etc/recipes',
|
RECIPES_DIR = 'packages/core/etc/recipes',
|
||||||
|
USER_DIR = 'usr/etc/recipes',
|
||||||
USER_RECIPES = 'usr/config/recipes.db',
|
USER_RECIPES = 'usr/config/recipes.db',
|
||||||
MACHINE_LOOKUP = 'usr/config/machine_crafting.db',
|
MACHINE_LOOKUP = 'usr/config/machine_crafting.db',
|
||||||
}
|
}
|
||||||
@@ -414,10 +415,11 @@ function Craft.loadRecipes()
|
|||||||
|
|
||||||
Util.merge(Craft.recipes, (Util.readTable(fs.combine(Craft.RECIPES_DIR, 'minecraft.db')) or { }).recipes)
|
Util.merge(Craft.recipes, (Util.readTable(fs.combine(Craft.RECIPES_DIR, 'minecraft.db')) or { }).recipes)
|
||||||
|
|
||||||
local config = Util.readTable('usr/config/recipeBooks.db') or { }
|
if fs.exists(Craft.USER_DIR) then
|
||||||
for _, book in pairs(config) do
|
for _, file in pairs(fs.list(Craft.USER_DIR)) do
|
||||||
local recipeFile = Util.readTable(book)
|
local recipeFile = Util.readTable(fs.combine(Craft.USER_DIR, file))
|
||||||
Util.merge(Craft.recipes, recipeFile.recipes)
|
Util.merge(Craft.recipes, recipeFile.recipes)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
local recipes = Util.readTable(Craft.USER_RECIPES) or { }
|
local recipes = Util.readTable(Craft.USER_RECIPES) or { }
|
||||||
|
|||||||
7
recipeBook/etc/apps.db
Normal file
7
recipeBook/etc/apps.db
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
{
|
||||||
|
[ "9fb1092f32d4fcbf9e061ffd718d4ec689c6c95e" ] = {
|
||||||
|
title = "Recipes",
|
||||||
|
category = "Apps",
|
||||||
|
run = "recipeBook",
|
||||||
|
},
|
||||||
|
}
|
||||||
20
recipeBook/etc/recipeBook.db
Normal file
20
recipeBook/etc/recipeBook.db
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
{
|
||||||
|
{
|
||||||
|
url = "https://pastebin.com/raw/dMCDeCie",
|
||||||
|
version = "MC 1.8+",
|
||||||
|
localName = "appliedenergistics2",
|
||||||
|
name = "Applied Energistics",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
url = "https://pastebin.com/raw/Y6bQMUeE",
|
||||||
|
version = "MC 1.8+",
|
||||||
|
localName = "botania",
|
||||||
|
name = "Botania",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
url = "https://pastebin.com/raw/YPWgiFFW",
|
||||||
|
version = "MC 1.8+",
|
||||||
|
localName = "computercraft",
|
||||||
|
name = "Computercraft",
|
||||||
|
},
|
||||||
|
}
|
||||||
@@ -1,39 +1,34 @@
|
|||||||
_G.requireInjector(_ENV)
|
|
||||||
|
|
||||||
local Ansi = require('ansi')
|
local Ansi = require('ansi')
|
||||||
local UI = require('ui')
|
local UI = require('ui')
|
||||||
local Util = require('util')
|
local Util = require('util')
|
||||||
|
|
||||||
local colors = _G.colors
|
local colors = _G.colors
|
||||||
local fs = _G.fs
|
local fs = _G.fs
|
||||||
|
local textutils = _G.textutils
|
||||||
|
|
||||||
local RECIPES_DIR = 'usr/etc/recipes'
|
local RECIPES_DIR = 'usr/etc/recipes'
|
||||||
|
local NAMES_DIR = 'usr/etc/names'
|
||||||
|
local RECIPE_BOOKS = 'packages/recipeBook/etc/recipeBook.db'
|
||||||
|
|
||||||
|
local db = Util.readTable(RECIPE_BOOKS)
|
||||||
|
|
||||||
local function getRecipeBooks()
|
local function getRecipeBooks()
|
||||||
local books = { }
|
local books = { }
|
||||||
|
|
||||||
local files = fs.list(RECIPES_DIR)
|
if not fs.exists(RECIPES_DIR) then
|
||||||
table.sort(files)
|
fs.makeDir(RECIPES_DIR)
|
||||||
Util.removeByValue(files, 'minecraft.db')
|
|
||||||
|
|
||||||
for _,file in ipairs(files) do
|
|
||||||
local path = fs.combine(RECIPES_DIR, file)
|
|
||||||
local recipeFile = Util.readTable(path)
|
|
||||||
if recipeFile then
|
|
||||||
table.insert(books, {
|
|
||||||
path = path,
|
|
||||||
name = recipeFile.name,
|
|
||||||
version = recipeFile.version,
|
|
||||||
})
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
local config = Util.readTable('usr/config/recipeBooks.db') or { }
|
for _,book in pairs(db) do
|
||||||
for _, book in pairs(config) do
|
local path = fs.combine(RECIPES_DIR, book.localName .. '.db')
|
||||||
local b = Util.find(books, 'path', book)
|
table.insert(books, {
|
||||||
if b then
|
recipePath = path,
|
||||||
b.enabled = true
|
namePath = fs.combine(NAMES_DIR, book.localName .. '.db'),
|
||||||
end
|
name = book.name,
|
||||||
|
url = book.url,
|
||||||
|
version = book.version,
|
||||||
|
enabled = fs.exists(path),
|
||||||
|
})
|
||||||
end
|
end
|
||||||
|
|
||||||
return books
|
return books
|
||||||
@@ -43,10 +38,15 @@ local page = UI.Page {
|
|||||||
info = UI.Window {
|
info = UI.Window {
|
||||||
x = 2, ex = -2, y = 2, ey = 5,
|
x = 2, ex = -2, y = 2, ey = 5,
|
||||||
button = UI.Button {
|
button = UI.Button {
|
||||||
ex = -1, y = 3, width = 9,
|
ex = -1, y = 3, width = 10,
|
||||||
text = 'Enable',
|
text = 'Enable',
|
||||||
event = 'grid_select',
|
event = 'grid_select',
|
||||||
}
|
},
|
||||||
|
addButton = UI.Button {
|
||||||
|
ex = -12, y = 3, width = 10,
|
||||||
|
text = 'Add Book',
|
||||||
|
event = 'add_book',
|
||||||
|
},
|
||||||
},
|
},
|
||||||
grid = UI.ScrollingGrid {
|
grid = UI.ScrollingGrid {
|
||||||
y = 6,
|
y = 6,
|
||||||
@@ -60,10 +60,39 @@ local page = UI.Page {
|
|||||||
sortColumn = 'name',
|
sortColumn = 'name',
|
||||||
autospace = true,
|
autospace = true,
|
||||||
},
|
},
|
||||||
accelerators = {
|
add = UI.SlideOut {
|
||||||
q = 'quit',
|
backgroundColor = colors.cyan,
|
||||||
space = 'grid_select',
|
titleBar = UI.TitleBar {
|
||||||
|
title = 'Add a new book',
|
||||||
|
},
|
||||||
|
form = UI.Form {
|
||||||
|
x = 2, ex = -2, y = 2, ey = -1,
|
||||||
|
[1] = UI.TextEntry {
|
||||||
|
formLabel = 'Name', formKey = 'name',
|
||||||
|
shadowText = 'Friendly name',
|
||||||
|
limit = 64,
|
||||||
|
required = true,
|
||||||
|
},
|
||||||
|
[2] = UI.TextEntry {
|
||||||
|
formLabel = 'Version', formKey = 'version',
|
||||||
|
shadowText = 'Mod version',
|
||||||
|
limit = 10,
|
||||||
|
},
|
||||||
|
[3] = UI.TextEntry {
|
||||||
|
formLabel = 'URL', formKey = 'url',
|
||||||
|
shadowText = 'URL for recipes',
|
||||||
|
limit = 128,
|
||||||
|
required = true,
|
||||||
|
},
|
||||||
|
[4] = UI.TextEntry {
|
||||||
|
formLabel = 'File name', formKey = 'localName',
|
||||||
|
shadowText = 'Short name for saving file',
|
||||||
|
limit = 20,
|
||||||
|
required = true,
|
||||||
|
},
|
||||||
|
},
|
||||||
},
|
},
|
||||||
|
notification = UI.Notification { },
|
||||||
}
|
}
|
||||||
|
|
||||||
function page.info:draw()
|
function page.info:draw()
|
||||||
@@ -79,6 +108,7 @@ function page.info:draw()
|
|||||||
|
|
||||||
self.button.text = book.enabled and 'Disable' or 'Enable'
|
self.button.text = book.enabled and 'Disable' or 'Enable'
|
||||||
self.button:draw()
|
self.button:draw()
|
||||||
|
self.addButton:draw()
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -89,25 +119,55 @@ function page.grid:getRowTextColor(row, selected)
|
|||||||
return selected and colors.lightGray or colors.gray
|
return selected and colors.lightGray or colors.gray
|
||||||
end
|
end
|
||||||
|
|
||||||
function page:save()
|
function page:save(book, enable)
|
||||||
local t = { }
|
if enable then
|
||||||
|
self.notification:info('Downloading...')
|
||||||
for _, book in pairs(self.grid.values) do
|
self:sync()
|
||||||
if book.enabled then
|
local s = pcall(function()
|
||||||
table.insert(t, book.path)
|
local recipes = Util.download(book.url)
|
||||||
|
if recipes then
|
||||||
|
recipes = textutils.unserialize(recipes)
|
||||||
|
local names = { }
|
||||||
|
for k,v in pairs(recipes.recipes) do
|
||||||
|
names[k] = v.displayName
|
||||||
|
v.displayName = nil
|
||||||
|
end
|
||||||
|
Util.writeTable(book.namePath, names)
|
||||||
|
Util.writeTable(book.recipePath, recipes)
|
||||||
|
end
|
||||||
|
book.enabled = true
|
||||||
|
self.notification:success('Download complete')
|
||||||
|
end)
|
||||||
|
if not s then
|
||||||
|
self.notification:error('Download failed')
|
||||||
end
|
end
|
||||||
|
else
|
||||||
|
fs.delete(book.recipePath)
|
||||||
|
fs.delete(book.namePath)
|
||||||
|
book.enabled = false
|
||||||
end
|
end
|
||||||
|
|
||||||
Util.writeTable('usr/config/recipeBooks.db', t)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
function page:eventHandler(event)
|
function page:eventHandler(event)
|
||||||
if event.type == 'grid_select' then
|
if event.type == 'grid_select' then
|
||||||
local book = self.grid:getSelected()
|
local book = self.grid:getSelected()
|
||||||
book.enabled = not book.enabled
|
self:save(book, not book.enabled)
|
||||||
self.info:draw()
|
self.info:draw()
|
||||||
self.grid:draw()
|
self.grid:draw()
|
||||||
self:save()
|
|
||||||
|
elseif event.type == 'add_book' then
|
||||||
|
self.add.form:setValues({ })
|
||||||
|
self.add:show()
|
||||||
|
|
||||||
|
elseif event.type == 'form_complete' then
|
||||||
|
self.add:hide()
|
||||||
|
table.insert(db, self.add.form.values)
|
||||||
|
Util.writeTable(RECIPE_BOOKS, db)
|
||||||
|
self.grid:setValues(getRecipeBooks())
|
||||||
|
self.grid:draw()
|
||||||
|
|
||||||
|
elseif event.type == 'form_cancel' then
|
||||||
|
self.add:hide()
|
||||||
|
|
||||||
elseif event.type == 'grid_focus_row' then
|
elseif event.type == 'grid_focus_row' then
|
||||||
self.info:draw()
|
self.info:draw()
|
||||||
|
|||||||
Reference in New Issue
Block a user