manipulators aaarrrggghhh

This commit is contained in:
kepler155c@gmail.com 2019-07-19 10:06:34 -06:00
parent d8dd17333c
commit 92686381c7
4 changed files with 61 additions and 40 deletions

View File

@ -3,3 +3,4 @@ sys/apps/update.lua urlfs http://pastebin.com/raw/UzGHLbNC
sys/apps/Enchat.lua urlfs https://raw.githubusercontent.com/LDDestroier/enchat/master/enchat3.lua sys/apps/Enchat.lua urlfs https://raw.githubusercontent.com/LDDestroier/enchat/master/enchat3.lua
sys/apps/cloud.lua urlfs https://cloud-catcher.squiddev.cc/cloud.lua sys/apps/cloud.lua urlfs https://cloud-catcher.squiddev.cc/cloud.lua
sys/apps/nfttrans.lua urlfs https://pastebin.com/raw/e8XrzeDY sys/apps/nfttrans.lua urlfs https://pastebin.com/raw/e8XrzeDY
rom/modules/main/opus linkfs sys/modules/opus

View File

@ -11,15 +11,22 @@ local containers = {
local cache = { } local cache = { }
-- manipulators will throw an error on listModules
-- if the user has logged off
local function getModules(dev, side) local function getModules(dev, side)
local list = { } local list = { }
if dev and dev.listModules then local s, m = pcall(function()
for _, module in pairs(dev.listModules()) do if dev and dev.listModules then
list[module] = Util.shallowCopy(dev) for _, module in pairs(dev.listModules()) do
list[module].name = module list[module] = Util.shallowCopy(dev)
list[module].type = module list[module].name = module
list[module].side = side list[module].type = module
list[module].side = side
end
end end
end)
if not s and m then
_G._syslog(m)
end end
return list return list
end end
@ -39,20 +46,27 @@ local function addDevice(dev, args, doQueue)
if doQueue then if doQueue then
os.queueEvent('device_attach', name) os.queueEvent('device_attach', name)
end end
return device[name]
end end
-- directly access the peripheral as the methods in getInventory, etc.
-- can become invalid without any way to tell
local function damnManipulator(container, method, args, doQueue) local function damnManipulator(container, method, args, doQueue)
local dev = addDevice(container[method](), args) local dev = { }
for k,v in pairs(dev) do local methods = {
if type(v) == 'function' then 'drop', 'getDocs', 'getItem', 'getItemMeta', 'getTransferLocations',
'list', 'pullItems', 'pushItems', 'size', 'suck',
}
-- the user might not be logged in when the compputer is started
-- and there's no way to know when they have logged in.
-- these methods will error if the user is not logged in
if container[method] then
for _,k in pairs(methods) do
dev[k] = function(...) dev[k] = function(...)
return device[container.name][method]()[k](...) return device[container.name][method]()[k](...)
end end
end end
end
if doQueue then addDevice(dev, args, doQueue)
os.queueEvent('device_attach', args.name)
end end
end end
@ -66,32 +80,29 @@ local function addContainer(v, doQueue)
end end
if v.getName then if v.getName then
pcall(function() local s, m = pcall(function()
local name = v.getName() local name = v.getName()
if name then if name then
if v.getInventory then damnManipulator(v, 'getInventory', {
damnManipulator(v, 'getInventory', { name = name .. ':inventory',
name = name .. ':inventory', type = 'inventory',
type = 'inventory', side = v.side
side = v.side }, doQueue)
}, doQueue) damnManipulator(v, 'getEquipment', {
end name = name .. ':equipment',
if v.getEquipment then type = 'equipment',
damnManipulator(v, 'getEquipment', { side = v.side
name = name .. ':equipment', }, doQueue)
type = 'equipment', damnManipulator(v, 'getEnder', {
side = v.side name = name .. ':enderChest',
}, doQueue) type = 'enderChest',
end side = v.side
if v.getEnder then }, doQueue)
damnManipulator(v, 'getEnder', {
name = name .. ':enderChest',
type = 'enderChest',
side = v.side
}, doQueue)
end
end end
end) end)
if not s and m then
_G._syslog(m)
end
end end
end end
@ -113,9 +124,11 @@ kernel.hook('device_attach', function(_, eventData)
-- a module is removed -- a module is removed
if cache[name] then if cache[name] then
device[name] = cache[name] device[name] = cache[name]
-- TODO: cannot simply merge - need to remove for k,v in pairs(device[name]) do
-- all functions then merge if type(v) == 'function' then
Util.merge(device[name], dev) device[name][k] = nil
end
end
else else
cache[name] = dev cache[name] = dev
end end

View File

@ -1,4 +1,3 @@
local fs = _G.fs local fs = _G.fs
fs.mount('rom/modules/main/opus', 'linkfs', 'sys/modules/opus')
fs.loadTab('sys/etc/fstab') fs.loadTab('sys/etc/fstab')

View File

@ -1,14 +1,22 @@
local os = _G.os local os = _G.os
local peripheral = _G.peripheral
-- Default label -- Default label
if not os.getComputerLabel() then if not os.getComputerLabel() then
local id = os.getComputerID() local id = os.getComputerID()
if _G.turtle then if _G.turtle then
os.setComputerLabel('turtle_' .. id) os.setComputerLabel('turtle_' .. id)
elseif _G.pocket then elseif _G.pocket then
os.setComputerLabel('pocket_' .. id) os.setComputerLabel('pocket_' .. id)
elseif _G.commands then elseif _G.commands then
os.setComputerLabel('command_' .. id) os.setComputerLabel('command_' .. id)
elseif peripheral.find('neuralInterface') then
os.setComputerLabel('neural_' .. id)
else else
os.setComputerLabel('computer_' .. id) os.setComputerLabel('computer_' .. id)
end end