Nonfunctional virtual component support

This commit is contained in:
TheWahlolly
2025-06-14 18:31:09 +03:00
parent 21472ba797
commit f425e0d690
3 changed files with 81 additions and 12 deletions
+2 -2
View File
@@ -12,8 +12,8 @@ local conversionTables = {
} }
} }
function table.find(table, item) function table.find(tab, item)
for k, v in pairs(table) do for k, v in pairs(tab) do
if v == item then if v == item then
return k return k
end end
+76 -9
View File
@@ -1,15 +1,82 @@
local componentlib local compLib
local LLcomponent
if table.copy then if table.copy then
componentlib = table.copy(component) compLib = table.copy(component)
LLcomponent = table.copy(component)
else else
componentlib = {} compLib = {}
LLcomponent = component
end end
function componentlib.get(address) local ocelot = LLcomponent.proxy(LLcomponent.list("ocelot")())
ocelot.log("loaded")
_G.componentlib = {["additions"] = {}, ["removals"] = {}}
compLib.virtual = {}
function compLib.virtual.add(address, componentType, proxy)
checkArg(1, address, "string") checkArg(1, address, "string")
assert(#address >= 3, "abbreviated address must be at least 3 characters long") checkArg(2, componentType, "string")
local components = component.list() checkArg(3, proxy, "table")
for currentAddress, name in pairs(components) do componentlib.additions[address] = {["componentType"] = componentType, ["proxy"] = proxy}
if componentlib.removals[address] then
componentlib.removals[address] = nil
end
end
function compLib.virtual.remove(address)
checkArg(1, address, "string")
if componentlib.additions[address] then
componentlib.additions[address] = nil
else
table.insert(componentlib.removals, address)
end
end
function compLib.list(componentType)
checkArg(1, componentType, "string", "nil")
local componentList = table.copy(LLcomponent.list(componentType))
for address, dataTable in pairs(componentlib.additions) do
if dataTable.componentType == componentType or not componentType then
componentList[address] = dataTable.componentType
end
end
for _, address in pairs(componentlib.removals) do
componentList[address] = nil
end
local i, value
setmetatable(componentList, {__call = function(self)
i, value = next(self, i)
return i, value
end})
return componentList
end
function compLib.proxy(address)
if componentlib.additions[address] then
ocelot.log("vcomponent")
return componentlib.additions[address].proxy
else
return LLcomponent.proxy(address)
end
end
function compLib.invoke(address, funcName, ...)
ocelot.log("Invoking " .. funcName .. " from " .. address)
if componentlib.additions[address] then
ocelot.log("vcomponent")
return componentlib.additions[address].proxy[funcName](...)
else
return LLcomponent.invoke(address, funcName, ...)
end
end
function compLib.get(address)
checkArg(1, address, "string")
if #address < 3 then
return nil, "abbreviated address must be at least 3 characters long"
end
for currentAddress, name in compLib.list() do
if currentAddress:find("^" .. address) then if currentAddress:find("^" .. address) then
return(currentAddress) return(currentAddress)
end end
@@ -17,6 +84,6 @@ function componentlib.get(address)
return nil, "full address not found" return nil, "full address not found"
end end
componentlib.invoke = component.invoke compLib.invoke = component.invoke -- trust me, this is needed
return componentlib return compLib
+3 -1
View File
@@ -25,7 +25,9 @@ gpu.setBackground(0x000000)
gpu.fill(1, 1, resx, resy, " ") gpu.fill(1, 1, resx, resy, " ")
local result, reason = xpcall(loadthething, handleError) local result, reason = xpcall(loadthething, handleError)
if not result then if not result then
local computer = import("computer") or computer if import then
local computer = import("computer")
end
gpu.setBackground(0x000000) gpu.setBackground(0x000000)
gpu.fill(1, 1, resx, resy, " ") gpu.fill(1, 1, resx, resy, " ")
gpu.setBackground(0x800000) gpu.setBackground(0x800000)