1
0
mirror of https://github.com/kepler155c/opus synced 2024-12-28 17:40:26 +00:00

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/cloud.lua urlfs https://cloud-catcher.squiddev.cc/cloud.lua
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 = { }
-- manipulators will throw an error on listModules
-- if the user has logged off
local function getModules(dev, side)
local list = { }
if dev and dev.listModules then
for _, module in pairs(dev.listModules()) do
list[module] = Util.shallowCopy(dev)
list[module].name = module
list[module].type = module
list[module].side = side
local s, m = pcall(function()
if dev and dev.listModules then
for _, module in pairs(dev.listModules()) do
list[module] = Util.shallowCopy(dev)
list[module].name = module
list[module].type = module
list[module].side = side
end
end
end)
if not s and m then
_G._syslog(m)
end
return list
end
@ -39,20 +46,27 @@ local function addDevice(dev, args, doQueue)
if doQueue then
os.queueEvent('device_attach', name)
end
return device[name]
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 dev = addDevice(container[method](), args)
for k,v in pairs(dev) do
if type(v) == 'function' then
local dev = { }
local methods = {
'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(...)
return device[container.name][method]()[k](...)
end
end
end
if doQueue then
os.queueEvent('device_attach', args.name)
addDevice(dev, args, doQueue)
end
end
@ -66,32 +80,29 @@ local function addContainer(v, doQueue)
end
if v.getName then
pcall(function()
local s, m = pcall(function()
local name = v.getName()
if name then
if v.getInventory then
damnManipulator(v, 'getInventory', {
name = name .. ':inventory',
type = 'inventory',
side = v.side
}, doQueue)
end
if v.getEquipment then
damnManipulator(v, 'getEquipment', {
name = name .. ':equipment',
type = 'equipment',
side = v.side
}, doQueue)
end
if v.getEnder then
damnManipulator(v, 'getEnder', {
name = name .. ':enderChest',
type = 'enderChest',
side = v.side
}, doQueue)
end
damnManipulator(v, 'getInventory', {
name = name .. ':inventory',
type = 'inventory',
side = v.side
}, doQueue)
damnManipulator(v, 'getEquipment', {
name = name .. ':equipment',
type = 'equipment',
side = v.side
}, doQueue)
damnManipulator(v, 'getEnder', {
name = name .. ':enderChest',
type = 'enderChest',
side = v.side
}, doQueue)
end
end)
if not s and m then
_G._syslog(m)
end
end
end
@ -113,9 +124,11 @@ kernel.hook('device_attach', function(_, eventData)
-- a module is removed
if cache[name] then
device[name] = cache[name]
-- TODO: cannot simply merge - need to remove
-- all functions then merge
Util.merge(device[name], dev)
for k,v in pairs(device[name]) do
if type(v) == 'function' then
device[name][k] = nil
end
end
else
cache[name] = dev
end

View File

@ -1,4 +1,3 @@
local fs = _G.fs
fs.mount('rom/modules/main/opus', 'linkfs', 'sys/modules/opus')
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
if not os.getComputerLabel() then
local id = os.getComputerID()
if _G.turtle then
os.setComputerLabel('turtle_' .. id)
elseif _G.pocket then
os.setComputerLabel('pocket_' .. id)
elseif _G.commands then
os.setComputerLabel('command_' .. id)
elseif peripheral.find('neuralInterface') then
os.setComputerLabel('neural_' .. id)
else
os.setComputerLabel('computer_' .. id)
end