From b674c6ab901efa4bdcb68dbc169d4fc43a970fc5 Mon Sep 17 00:00:00 2001 From: "kepler155c@gmail.com" Date: Mon, 14 Jan 2019 10:03:53 -0500 Subject: [PATCH] rework plugin management --- milo/Milo.lua | 24 ++++++++++--- milo/apis/craft2.lua | 6 +--- milo/apis/milo.lua | 10 ++++++ milo/plugins/craftTask.lua | 18 ---------- milo/plugins/item.lua | 43 +++++++---------------- milo/plugins/item/infoTab.lua | 2 +- milo/plugins/item/machinesTab.lua | 2 +- milo/plugins/item/manageTab.lua | 2 +- milo/plugins/item/recipeTab.lua | 2 +- milo/plugins/item/resetTab.lua | 2 +- swshop/installPlugin.lua | 24 +++++++++++++ {milo/plugins => swshop}/shopConfig.lua | 5 --- {milo/plugins/item => swshop}/shopTab.lua | 12 +++---- {milo/plugins => swshop}/shopView.lua | 4 +-- 14 files changed, 80 insertions(+), 76 deletions(-) create mode 100644 swshop/installPlugin.lua rename {milo/plugins => swshop}/shopConfig.lua (92%) rename {milo/plugins/item => swshop}/shopTab.lua (89%) rename {milo/plugins => swshop}/shopView.lua (98%) diff --git a/milo/Milo.lua b/milo/Milo.lua index 3d28cb4..ce0a8e2 100644 --- a/milo/Milo.lua +++ b/milo/Milo.lua @@ -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 diff --git a/milo/apis/craft2.lua b/milo/apis/craft2.lua index 35cc0c7..6da9810 100644 --- a/milo/apis/craft2.lua +++ b/milo/apis/craft2.lua @@ -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 diff --git a/milo/apis/milo.lua b/milo/apis/milo.lua index 7fba1eb..e993e61 100644 --- a/milo/apis/milo.lua +++ b/milo/apis/milo.lua @@ -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 diff --git a/milo/plugins/craftTask.lua b/milo/plugins/craftTask.lua index 2cf6c70..5763fc5 100644 --- a/milo/plugins/craftTask.lua +++ b/milo/plugins/craftTask.lua @@ -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() diff --git a/milo/plugins/item.lua b/milo/plugins/item.lua index 7e330b1..59cd1b1 100644 --- a/milo/plugins/item.lua +++ b/milo/plugins/item.lua @@ -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) diff --git a/milo/plugins/item/infoTab.lua b/milo/plugins/item/infoTab.lua index 48decdd..ce31d32 100644 --- a/milo/plugins/item/infoTab.lua +++ b/milo/plugins/item/infoTab.lua @@ -43,4 +43,4 @@ function infoTab:draw() UI.Window.draw(self) end -return infoTab +return { itemTab = infoTab } diff --git a/milo/plugins/item/machinesTab.lua b/milo/plugins/item/machinesTab.lua index f94f13a..5246c26 100644 --- a/milo/plugins/item/machinesTab.lua +++ b/milo/plugins/item/machinesTab.lua @@ -61,4 +61,4 @@ function machinesTab:eventHandler(event) end end -return machinesTab +return { itemTab = machinesTab } diff --git a/milo/plugins/item/manageTab.lua b/milo/plugins/item/manageTab.lua index 8c1b8a0..3851518 100644 --- a/milo/plugins/item/manageTab.lua +++ b/milo/plugins/item/manageTab.lua @@ -93,4 +93,4 @@ function manageTab:eventHandler(event) return true end -return manageTab +return { itemTab = manageTab } diff --git a/milo/plugins/item/recipeTab.lua b/milo/plugins/item/recipeTab.lua index 658b4b4..53b8caa 100644 --- a/milo/plugins/item/recipeTab.lua +++ b/milo/plugins/item/recipeTab.lua @@ -87,4 +87,4 @@ function recipeTab:eventHandler(event) end end -return recipeTab +return { itemTab = recipeTab } diff --git a/milo/plugins/item/resetTab.lua b/milo/plugins/item/resetTab.lua index 172d8a0..5587950 100644 --- a/milo/plugins/item/resetTab.lua +++ b/milo/plugins/item/resetTab.lua @@ -52,4 +52,4 @@ function resetTab:eventHandler(event) end end -return resetTab +return { itemTab = resetTab } diff --git a/swshop/installPlugin.lua b/swshop/installPlugin.lua new file mode 100644 index 0000000..14024b6 --- /dev/null +++ b/swshop/installPlugin.lua @@ -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() diff --git a/milo/plugins/shopConfig.lua b/swshop/shopConfig.lua similarity index 92% rename from milo/plugins/shopConfig.lua rename to swshop/shopConfig.lua index 2420e19..6d5c9fe 100644 --- a/milo/plugins/shopConfig.lua +++ b/swshop/shopConfig.lua @@ -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', - }, }, } diff --git a/milo/plugins/item/shopTab.lua b/swshop/shopTab.lua similarity index 89% rename from milo/plugins/item/shopTab.lua rename to swshop/shopTab.lua index 3f8f320..2bf220e 100644 --- a/milo/plugins/item/shopTab.lua +++ b/swshop/shopTab.lua @@ -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 } diff --git a/milo/plugins/shopView.lua b/swshop/shopView.lua similarity index 98% rename from milo/plugins/shopView.lua rename to swshop/shopView.lua index 23ea831..3f372b8 100644 --- a/milo/plugins/shopView.lua +++ b/swshop/shopView.lua @@ -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, }