refactor parallel code

This commit is contained in:
kepler155c@gmail.com
2019-04-05 17:32:22 -04:00
parent d5eb07c3b9
commit 426c856dfb
44 changed files with 3645 additions and 182 deletions

View File

@@ -1,7 +1,6 @@
local itemDB = require('core.itemDB')
local Milo = require('milo')
local parallel = _G.parallel
local Tasks = require('milo.taskRunner')
local ExportTask = {
name = 'exporter',
@@ -13,86 +12,81 @@ local function filter(a)
end
function ExportTask:cycle(context)
local tasks = { }
local tasks = Tasks({
errorMsg = 'EXPORTER error: ',
})
for node in context.storage:filterActive('machine', filter) do
table.insert(tasks, function()
local s, m = pcall(function()
for _, entry in pairs(node.exports) do
tasks:add(function()
for _, entry in pairs(node.exports) do
if not entry.filter then
-- exports must have a filter
-- TODO: validate in exportView
break
if not entry.filter then
-- exports must have a filter
-- TODO: validate in exportView
break
end
local function exportSingleSlot()
local slot = node.adapter.getItemMeta(entry.slot)
if slot and slot.count == slot.maxCount then
return
end
local function exportSingleSlot()
local slot = node.adapter.getItemMeta(entry.slot)
if slot and slot.count == slot.maxCount then
return
end
if slot then
-- something is in the slot, find what we can export
for key in pairs(entry.filter) do
local filterItem = itemDB:splitKey(key)
if (slot.name == filterItem.name and
(entry.ignoreDamage or slot.damage == filterItem.damage) and
(entry.ignoreNbtHash or slot.nbtHash == filterItem.nbtHash)) then
local items = Milo:getMatches(filterItem, entry)
local _, item = next(items)
if item then
local count = math.min(item.count, slot.maxCount - slot.count)
context.storage:export(node, entry.slot, count, item)
end
break
end
end
return
end
-- slot is empty - export first matching item we have in storage
if slot then
-- something is in the slot, find what we can export
for key in pairs(entry.filter) do
local items = Milo:getMatches(itemDB:splitKey(key), entry)
local _, item = next(items)
if item then
local count = math.min(item.count, itemDB:getMaxCount(item))
context.storage:export(node, entry.slot, count, item)
local filterItem = itemDB:splitKey(key)
if (slot.name == filterItem.name and
(entry.ignoreDamage or slot.damage == filterItem.damage) and
(entry.ignoreNbtHash or slot.nbtHash == filterItem.nbtHash)) then
local items = Milo:getMatches(filterItem, entry)
local _, item = next(items)
if item then
local count = math.min(item.count, slot.maxCount - slot.count)
context.storage:export(node, entry.slot, count, item)
end
break
end
end
return
end
-- slot is empty - export first matching item we have in storage
for key in pairs(entry.filter) do
local items = Milo:getMatches(itemDB:splitKey(key), entry)
local _, item = next(items)
if item then
local count = math.min(item.count, itemDB:getMaxCount(item))
context.storage:export(node, entry.slot, count, item)
break
end
end
end
local function exportItems()
for key in pairs(entry.filter) do
local items = Milo:getMatches(itemDB:splitKey(key), entry)
for _,item in pairs(items) do
if context.storage:export(node, nil, item.count, item) == 0 then
-- TODO: really shouldn't break here as there may be room in other slots
-- leaving for now for performance reasons
break
end
end
end
local function exportItems()
for key in pairs(entry.filter) do
local items = Milo:getMatches(itemDB:splitKey(key), entry)
for _,item in pairs(items) do
if context.storage:export(node, nil, item.count, item) == 0 then
-- TODO: really shouldn't break here as there may be room in other slots
-- leaving for now for performance reasons
break
end
end
end
end
if type(entry.slot) == 'number' then
exportSingleSlot()
else
exportItems()
end
end
end)
if not s and m then
_G._debug('EXPORTER error: ' .. m)
if type(entry.slot) == 'number' then
exportSingleSlot()
else
exportItems()
end
end
end)
end
if #tasks > 0 then
parallel.waitForAll(table.unpack(tasks))
end
tasks:run()
end
Milo:registerTask(ExportTask)

View File

@@ -1,7 +1,6 @@
local itemDB = require('core.itemDB')
local Milo = require('milo')
local parallel = _G.parallel
local Tasks = require('milo.taskRunner')
local ImportTask = {
name = 'importer',
@@ -13,73 +12,67 @@ local function filter(a)
end
function ImportTask:cycle(context)
local tasks = { }
local tasks = Tasks({
errorMsg = 'IMPORT error: '
})
for node in context.storage:filterActive('machine', filter) do
table.insert(tasks, function()
local s, m = pcall(function()
for _, entry in pairs(node.imports) do
tasks:add(function()
for _, entry in pairs(node.imports) do
local function itemMatchesFilter(item)
if not entry.ignoreDamage and not entry.ignoreNbtHash then
local key = itemDB:makeKey(item)
return entry.filter[key]
end
for key in pairs(entry.filter) do
local v = itemDB:splitKey(key)
if item.name == v.name and
(entry.ignoreDamage or item.damage == v.damage) and
(entry.ignoreNbtHash or item.nbtHash == v.nbtHash) then
return true
end
end
local function itemMatchesFilter(item)
if not entry.ignoreDamage and not entry.ignoreNbtHash then
local key = itemDB:makeKey(item)
return entry.filter[key]
end
local function matchesFilter(item)
if not entry.filter then
for key in pairs(entry.filter) do
local v = itemDB:splitKey(key)
if item.name == v.name and
(entry.ignoreDamage or item.damage == v.damage) and
(entry.ignoreNbtHash or item.nbtHash == v.nbtHash) then
return true
end
if entry.blacklist then
return not itemMatchesFilter(item)
end
return itemMatchesFilter(item)
end
local list = node.adapter.list()
local function importSlot(slotNo)
local item = itemDB:get(list[slotNo], function()
return node.adapter.getItemMeta(slotNo)
end)
if item and matchesFilter(item) then
context.storage:import(node, slotNo, item.count, item)
end
end
if type(entry.slot) == 'number' then
if list[entry.slot] then
importSlot(entry.slot)
end
else
for i in pairs(list) do
importSlot(i)
end
end
end
end)
if not s and m then
_G._debug('IMPORTER error: ' .. m)
local function matchesFilter(item)
if not entry.filter then
return true
end
if entry.blacklist then
return not itemMatchesFilter(item)
end
return itemMatchesFilter(item)
end
local list = node.adapter.list()
local function importSlot(slotNo)
local item = itemDB:get(list[slotNo], function()
return node.adapter.getItemMeta(slotNo)
end)
if item and matchesFilter(item) then
context.storage:import(node, slotNo, item.count, item)
end
end
if type(entry.slot) == 'number' then
if list[entry.slot] then
importSlot(entry.slot)
end
else
for i in pairs(list) do
importSlot(i)
end
end
end
end)
end
if #tasks > 0 then
parallel.waitForAll(table.unpack(tasks))
end
tasks:run()
end
Milo:registerTask(ImportTask)

View File

@@ -1,6 +1,5 @@
local Milo = require('milo')
local parallel = _G.parallel
local Milo = require('milo')
local Tasks = require('milo.taskRunner')
local InputChest = {
name = 'input',
@@ -8,29 +7,24 @@ local InputChest = {
}
function InputChest:cycle(context)
local tasks = { }
for node in context.storage:filterActive('input') do
table.insert(tasks, function()
local s, m = pcall(function()
for slot, item in pairs(node.adapter.list()) do
local s, m = pcall(function()
context.storage:import(node, slot, item.count, item)
end)
if not s and m then
_G._debug('INPUT error: ' .. m)
end
end
end)
local tasks = Tasks({
errorMsg = 'INPUT error: '
})
if not s and m then
_G._debug('INPUT error: ' .. m)
for node in context.storage:filterActive('input') do
local s, m = pcall(function()
for slot, item in pairs(node.adapter.list()) do
tasks:add(function()
context.storage:import(node, slot, item.count, item)
end)
end
end)
if not s and m then
_G._debug('INPUT error: ' .. m)
end
end
if #tasks > 0 then
parallel.waitForAll(table.unpack(tasks))
end
tasks:run()
end
Milo:registerTask(InputChest)

View File

@@ -1,9 +1,9 @@
local Event = require('event')
local Milo = require('milo')
local UI = require('ui')
local Milo = require('milo')
local Tasks = require('milo.taskRunner')
local UI = require('ui')
local colors = _G.colors
local device = _G.device
local colors = _G.colors
local device = _G.device
--[[ Configuration Screen ]]
local wizardPage = UI.WizardPage {
@@ -76,16 +76,20 @@ local function filter(a)
end
function task:cycle(context)
local tasks = Tasks()
for node in context.storage:filterActive('trashcan', filter) do
Event.onTimeout(0, function() -- run on a background thread
pcall(function()
for k in pairs(node.adapter.list()) do
local direction = node.dropDirection or 'down'
pcall(function()
for k in pairs(node.adapter.list()) do
local direction = node.dropDirection or 'down'
tasks:add(function()
node.adapter.drop(k, 64, direction)
end
end)
end)
end
end)
end
tasks:run()
end
Milo:registerTask(task)