1
0
mirror of https://github.com/Adorable-Catgirl/Zorya-NEO.git synced 2024-11-30 14:18:06 +11:00
Zorya-NEO/mods/util_vdev/init.lua

180 lines
4.1 KiB
Lua
Raw Normal View History

2020-01-09 15:01:35 +11:00
local comp = component
local vdev = {}
local types = {}
local devices = {}
local unpack = unpack or table.unpack
function vdev.register_type(dtype, prototype)
types[dtype] = prototype
end
function vdev.add_device(address, dtype)
devices[#devices+1] = {addr=address, type=dtype}
computer.pushSignal("component_added", address, dtype)
return address
end
function vdev.gen_address()
local bytes = {}
for i=1, 16 do
bytes[#bytes+1] = math.random(0, 255)
end
return string.format("%.2x%.2x%.2x%.2x-%.2x%.2x-%.2x%.2x-%.2x%.2x-%.2x%.2x%.2x%.2x%.2x%.2x", unpack(bytes))
end
function vdev.remove_device(address)
for i=1, #devices do
if (devices[i].addr == address) then
computer.pushSignal("component_removed", address, devices[i].type)
table.remove(devices, i)
return
end
end
end
-- Override component library, and computer.getDeviceInfo()
local cdevinfo = computer.getDeviceInfo
2020-01-15 12:07:27 +11:00
vdev.component = {}
vdev.computer = {}
2020-01-09 15:01:35 +11:00
for k, v in pairs(comp) do
2020-01-15 12:07:27 +11:00
vdev.component[k] = v
2020-01-09 15:01:35 +11:00
end
2020-01-15 12:07:27 +11:00
function vdev.component.list(dtype, match)
dtype = dtype or ""
2020-01-09 15:01:35 +11:00
local lpos = 0
local func = comp.list(dtype)
local ft = {}
for k, v in pairs(func) do
ft[k] = v
end
for i=1, #devices do
2020-01-15 12:07:27 +11:00
if (dtype == devices[i].type or (not match and devices[i].type:sub(1, #dtype) == dtype)) then
2020-01-09 15:01:35 +11:00
ft[ devices[i].addr ] = devices[i].type
end
end
return setmetatable(ft, {__call=function()
if (lpos > #devices or #devices == 0) then
return func()
end
while true do
lpos = lpos + 1
if (lpos > #devices) then
return func()
end
2020-01-15 12:07:27 +11:00
if (dtype == devices[lpos].type or (not match and devices[lpos].type:sub(1, #dtype) == dtype)) then
2020-01-09 15:01:35 +11:00
return devices[lpos].addr
end
end
end})
end
2020-01-15 12:07:27 +11:00
local function debug_log(...)
log("return", ...)
return ...
end
function vdev.component.proxy(addr)
2020-01-09 15:01:35 +11:00
for i=1, #devices do
if (devices[i].addr == addr) then
return setmetatable({}, {__index=function(self, index)
if (types[devices[i].type].methods[index]) then
local func = setmetatable({}, {__call=function(...)
2020-01-15 12:07:27 +11:00
log("proxy", addr, index, ...)
2020-01-09 15:01:35 +11:00
return types[devices[i].type].methods[index](devices[i].addr, ...)
end, __tostring = function()
2020-01-15 12:07:27 +11:00
return types[devices[i].type].doc[index] or (index.."(...):any")
2020-01-09 15:01:35 +11:00
end})
self[index] = func
end
end})
end
end
return comp.proxy(addr)
end
2020-01-15 12:07:27 +11:00
function vdev.component.invoke(addr, meth, ...)
log("invoke", addr, meth, ...)
2020-01-09 15:01:35 +11:00
for i=1, #devices do
if (devices[i].addr == addr) then
2020-01-15 12:07:27 +11:00
if (types[devices[i].type].methods[meth]) then
return debug_log("invoke", types[devices[i].type].methods[meth](addr, ...))
2020-01-09 15:01:35 +11:00
end
end
end
return comp.invoke(addr, meth, ...)
end
2020-01-15 12:07:27 +11:00
function vdev.component.doc(addr, meth)
2020-01-09 15:01:35 +11:00
for i=1, #devices do
if (devices[i].addr == addr) then
if (types[devices[i].type].methods[meth]) then
2020-01-15 12:07:27 +11:00
return types[devices[i].type].doc[meth] or (index.."(...):any")
2020-01-09 15:01:35 +11:00
end
return
end
end
return comp.doc(addr, meth)
end
2020-01-15 12:07:27 +11:00
function vdev.component.type(addr)
2020-01-09 15:01:35 +11:00
for i=1, #devices do
if (devices[i].addr == addr) then
return devices[i].type
end
end
return comp.type(addr)
end
2020-01-15 12:07:27 +11:00
function vdev.component.slot(addr)
2020-01-09 15:01:35 +11:00
for i=1, #devices do
if (devices[i].addr == addr) then
return -1
end
end
return comp.slot(addr)
end
2020-01-15 12:07:27 +11:00
function vdev.component.methods(addr)
2020-01-09 15:01:35 +11:00
for i=1, #devices do
if (devices[i].addr == addr) then
local m = {}
for k, v in pairs(types[devices[i].type].methods) do
m[k] = true
end
return m
end
end
return comp.methods(addr)
end
2020-01-15 12:07:27 +11:00
function vdev.computer.getDeviceInfo()
2020-01-09 15:01:35 +11:00
local tbl = cdevinfo()
for i=1, #devices do
local info = {}
tbl[devices[i].addr] = info
local dtype = types[devices[i].type]
info.vendor = dtype.vendor
info.product = dtype.product
info.class = dtype.class
dtype.getinfo(devices[i].addr, info)
end
return tbl
end
2020-01-15 12:07:27 +11:00
function vdev.overwrite(env)
for k, v in pairs(vdev) do
if (type(v) == "table") then
for j, b in pairs(v) do
env[k][j] = b
end
end
end
end
2020-01-09 15:01:35 +11:00
return vdev