rework plugin management

This commit is contained in:
kepler155c@gmail.com
2019-01-14 10:03:53 -05:00
parent 1cc9829eed
commit b674c6ab90
14 changed files with 80 additions and 76 deletions

View File

@@ -68,6 +68,7 @@ local context = {
craftingQueue = { },
tasks = { },
queue = { },
plugins = { },
storage = Storage(),
turtleInventory = {
@@ -84,14 +85,22 @@ Milo:init(context)
context.storage:initStorage()
context.storage.turtleInventory = context.turtleInventory
local function loadPlugin(file)
local s, plugin = Util.run(_ENV, file, context)
if not s and plugin then
_G.printError('Error loading: ' .. file)
error(plugin or 'Unknown error')
end
if plugin and type(plugin) == 'table' then
Milo:registerPlugin(plugin)
end
end
local function loadDirectory(dir)
for _, file in pairs(fs.list(dir)) do
if not fs.isDir(fs.combine(dir, file)) then
local s, m = Util.run(_ENV, fs.combine(dir, file), context)
if not s and m then
_G.printError('Error loading: ' .. file)
error(m or 'Unknown error')
end
loadPlugin(fs.combine(dir, file))
end
end
end
@@ -99,6 +108,11 @@ end
local programDir = fs.getDir(shell.getRunningProgram())
loadDirectory(fs.combine(programDir, 'core'))
loadDirectory(fs.combine(programDir, 'plugins'))
loadDirectory(fs.combine(programDir, 'plugins/item'))
for k in pairs(Milo:getState('plugins') or { }) do
loadPlugin(k)
end
table.sort(context.tasks, function(a, b)
return a.priority < b.priority

View File

@@ -220,9 +220,6 @@ function Craft.craftRecipeInternal(recipe, count, storage, origItem)
count = canCraft
end
--_G._debug({'eval', recipe.result, count })
--local maxCount = math.floor((recipe.maxCount or 64) / recipe.count)
local maxCount = recipe.maxCount or math.floor(64 / recipe.count)
repeat
@@ -260,7 +257,7 @@ function Craft.craftRecipeInternal(recipe, count, storage, origItem)
while canCraft > 0 do
local batch = math.min(canCraft, maxCount)
local machine = Craft.machineLookup[recipe.result]
--_G._debug({ 'crafting', recipe.result, batch })
if machine then
if not machineCraft(recipe, storage, machine, request, batch, origItem) then
break
@@ -277,7 +274,6 @@ function Craft.craftRecipeInternal(recipe, count, storage, origItem)
if request.aborted then
origItem.aborted = true
_debug('aborted')
return 0
end

View File

@@ -20,6 +20,16 @@ function Milo:getContext()
return self.context
end
function Milo:registerPlugin(plugin)
for pluginType, value in pairs(plugin) do
if not self.context.plugins[pluginType] then
self.context.plugins[pluginType] = { value }
else
table.insert(self.context.plugins[pluginType], value)
end
end
end
function Milo:pauseCrafting(reason)
local _, key = Util.find(self.context.state, 'key', reason.key)
if not key then

View File

@@ -35,25 +35,7 @@ function craftTask:craft(recipe, item)
item.ingredients[recipe.result].total = item.count
item.ingredients[recipe.result].crafted = item.crafted
--[[
_G._p2 = item
if not item.history then
item.history = { }
end
local t = Util.shallowCopy(item)
t.history = { input = { }, output = { } }
for k,v in pairs(item.ingredients) do
t.history.input[k] = Util.shallowCopy(v)
end
table.insert(item.history, t)
]]
Craft.craftRecipe(recipe, item.requested - item.crafted, context.storage, item)
--[[
for k,v in pairs(item.ingredients) do
t.history.output[k] = Util.shallowCopy(v)
end
]]
end
function craftTask:cycle()

View File

@@ -1,28 +1,8 @@
local Milo = require('milo')
local UI = require('ui')
local Util = require('util')
local fs = _G.fs
local shell = _ENV.shell
local function loadDirectory(dir)
local tabs = { }
for _, file in pairs(fs.list(dir)) do
if not fs.isDir(fs.combine(dir, file)) then
local s, m = Util.run(_ENV, fs.combine(dir, file))
if not s and m then
_G.printError('Error loading: ' .. file)
error(m or 'Unknown error')
end
table.insert(tabs, m)
end
end
return tabs
end
local programDir = fs.getDir(shell.getRunningProgram())
local tabs = loadDirectory(fs.combine(programDir, 'plugins/item'))
table.sort(tabs, function(a, b) return a.index < b.index end)
local context = Milo:getContext()
local page = UI.Page {
titleBar = UI.TitleBar {
@@ -34,12 +14,21 @@ local page = UI.Page {
}
function page:enable(item)
for _, v in pairs(tabs) do
if not self.tabs then
table.sort(context.plugins.itemTab, function(a, b) return a.index < b.index end)
local t = Util.shallowCopy(context.plugins.itemTab)
t.y = 2
t.ey = -2
self:add({ tabs = UI.Tabs(t) })
end
for _, v in pairs(context.plugins.itemTab) do
if v.UIElement then
v:setItem(item)
end
end
self.tabs:selectTab(tabs[1])
self.tabs:selectTab(context.plugins.itemTab[1])
UI.Page.enable(self)
end
@@ -69,10 +58,4 @@ function page:eventHandler(event)
return true
end
local t = Util.shallowCopy(tabs)
t.y = 2
t.ey = -2
page:add({ tabs = UI.Tabs(t) })
UI:addPage('item', page)

View File

@@ -43,4 +43,4 @@ function infoTab:draw()
UI.Window.draw(self)
end
return infoTab
return { itemTab = infoTab }

View File

@@ -61,4 +61,4 @@ function machinesTab:eventHandler(event)
end
end
return machinesTab
return { itemTab = machinesTab }

View File

@@ -93,4 +93,4 @@ function manageTab:eventHandler(event)
return true
end
return manageTab
return { itemTab = manageTab }

View File

@@ -87,4 +87,4 @@ function recipeTab:eventHandler(event)
end
end
return recipeTab
return { itemTab = recipeTab }

View File

@@ -52,4 +52,4 @@ function resetTab:eventHandler(event)
end
end
return resetTab
return { itemTab = resetTab }

24
swshop/installPlugin.lua Normal file
View File

@@ -0,0 +1,24 @@
local Util = require('util')
local fs = _G.fs
local read = _G.read
local shell = _ENV.shell
local CONFIG_FILE = '/usr/config/milo.state'
local config = Util.readTable(CONFIG_FILE) or { }
if not config.plugins then
config.plugins = { }
end
local dir = fs.getDir(shell.getRunningProgram())
config.plugins[fs.combine(dir, 'shopConfig.lua')] = true
config.plugins[fs.combine(dir, 'shopTab.lua')] = true
config.plugins[fs.combine(dir, 'shopView.lua')] = true
Util.writeTable(CONFIG_FILE, config)
print('Plugin Installed')
print('Press enter to exit')
read()

View File

@@ -43,11 +43,6 @@ local wizardPage = UI.Window {
},
help = 'Adjust text scaling',
},
warning = UI.Text {
x = 2, y = -1,
textColor = colors.orange,
value = 'Package swshop must be installed',
},
},
}

View File

@@ -5,11 +5,11 @@ local os = _G.os
local config = Config.load('shop')
local storeTab = UI.Window {
local shopTab = UI.Window {
tabTitle = 'Store',
index = 6,
index = 2,
form = UI.Form {
x = 2, ex = -2, y = 2, ey = -2,
x = 2, ex = -2, y = 1, ey = -2,
manualControls = true,
[1] = UI.TextEntry {
formLabel = 'Name', formKey = 'name',
@@ -43,12 +43,12 @@ local storeTab = UI.Window {
},
}
function storeTab:setItem(item)
function shopTab:setItem(item)
self.item = item
self.form:setValues(config[item.key] or { })
end
function storeTab:eventHandler(event)
function shopTab:eventHandler(event)
if event.type == 'clear' then
self.form:setValues({ })
config[self.item.key] = nil
@@ -70,4 +70,4 @@ function storeTab:eventHandler(event)
return true
end
return storeTab
return { itemTab = shopTab }

View File

@@ -49,7 +49,7 @@ local function createPage(node)
local page = UI.Page {
parent = monitor,
header = UI.Window {
backgroundColor = colors.blue,
backgroundColor = colors.cyan,
ey = 3,
},
grid = UI.Grid {
@@ -201,7 +201,7 @@ end)
--[[ Task ]]--
local StoreTask = {
name = 'store',
name = 'shop',
priority = 30,
}