1
0
mirror of https://github.com/kepler155c/opus synced 2024-11-18 06:34:49 +00:00
opus/sys/init/3.modules.lua

139 lines
3.3 KiB
Lua
Raw Normal View History

local Util = require('opus.util')
2019-03-15 12:26:56 +00:00
local device = _G.device
local kernel = _G.kernel
local os = _G.os
local containers = {
2019-06-18 19:19:24 +00:00
manipulator = true,
neuralInterface = true,
2019-03-15 12:26:56 +00:00
}
local cache = { }
2019-07-19 16:06:34 +00:00
-- manipulators will throw an error on listModules
-- if the user has logged off
2019-03-15 12:26:56 +00:00
local function getModules(dev, side)
2019-06-18 19:19:24 +00:00
local list = { }
2019-07-19 16:06:34 +00:00
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
2019-06-18 19:19:24 +00:00
end
2019-07-19 16:06:34 +00:00
end)
if not s and m then
_G._syslog(m)
2019-06-18 19:19:24 +00:00
end
return list
2019-03-15 12:26:56 +00:00
end
-- if a device has been reattached, reuse the existing
-- table so any references to the table are retained
local function addDevice(dev, args, doQueue)
local name = args.name
if not cache[name] then
cache[name] = { }
end
device[name] = cache[name]
Util.merge(device[name], dev)
Util.merge(device[name], args)
if doQueue then
os.queueEvent('device_attach', name)
end
end
2019-07-19 16:06:34 +00:00
-- 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)
2019-07-19 16:06:34 +00:00
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
2019-07-19 16:06:34 +00:00
addDevice(dev, args, doQueue)
end
end
local function addContainer(v, doQueue)
-- add devices like plethora:scanner
for name, dev in pairs(getModules(v, v.side)) do
-- neural and attached modules have precedence over manipulator modules
if not device[name] or v.type ~= 'manipulator' then
addDevice(dev, { name = dev.name, type = dev.name, side = dev.side }, doQueue)
2019-06-18 19:19:24 +00:00
end
end
if v.getName then
2019-07-19 16:06:34 +00:00
local s, m = pcall(function()
local name = v.getName()
if name then
2019-07-19 16:06:34 +00:00
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)
2019-07-19 16:06:34 +00:00
if not s and m then
_G._syslog(m)
end
end
end
for k,v in pairs(device) do
if containers[v.type] then
cache[k] = v
addContainer(v)
end
2019-03-15 12:26:56 +00:00
end
-- register modules as peripherals
kernel.hook('device_attach', function(_, eventData)
local name = eventData[1]
local dev = device[name]
2019-03-15 12:26:56 +00:00
2019-06-18 19:19:24 +00:00
if dev and containers[dev.type] then
-- so... basically, if you get a handle to device.neuralInterface
-- (or manipulator) - that handle will still be valid after
-- a module is removed
if cache[name] then
device[name] = cache[name]
2019-07-19 16:06:34 +00:00
for k,v in pairs(device[name]) do
if type(v) == 'function' then
device[name][k] = nil
end
end
2019-07-19 16:12:25 +00:00
Util.merge(device[name], dev)
else
cache[name] = dev
2019-06-18 19:19:24 +00:00
end
addContainer(dev, true)
2019-06-18 19:19:24 +00:00
end
2019-03-15 12:26:56 +00:00
end)