diff --git a/storage/init.lua b/storage/init.lua index 4fdccaa..9b6488f 100644 --- a/storage/init.lua +++ b/storage/init.lua @@ -4,6 +4,10 @@ local Sentinel = require("storage.sentinel") local Storage = Sentinel:tag({}, Sentinel.STORAGE) Storage.__index = Storage +local function _attach(stor, name, homogeneous) + table.insert(stor, Chest:fromPeripheral(name, homogeneous)) +end + function Storage.assumeHomogeneous(names) local mappings = {} for _,name in ipairs(names) do @@ -12,12 +16,13 @@ function Storage.assumeHomogeneous(names) return mappings end -function Storage:fromPeripherals(names) +function Storage:fromPeripherals(peripheralDefs) local obj = { - count = #names + count = #peripheralDefs } - for name,homogeneous in pairs(names) do - table.insert(obj, Chest:fromPeripheral(name, homogeneous)) + + for name,homogeneous in pairs(peripheralDefs) do + _attach(obj, name, homogeneous) end setmetatable(obj, self) @@ -30,6 +35,7 @@ function Storage:toSerializable() local ser = { count = self.count } + for _,chest in ipairs(self) do table.insert(ser, chest:toSerializable()) end @@ -43,10 +49,10 @@ function Storage:fromSerializable(ser) for _,chestSer in ipairs(ser) do local chest = Chest:fromSerializable(chestSer) if chest ~= nil then - table.insert(obj, chest) + table.insert(obj, chest) end end - + setmetatable(obj, self) obj.__index = self @@ -67,7 +73,14 @@ function Storage:attach(name, homogeneous) return end - table.insert(self, Chest:fromPeripheral(name, homogeneous)) + _attach(self, name, homogeneous) +end + +function Storage:attachAll(peripheralDefs) + for name,homogeneous in pairs(peripheralDefs) do + self:attach(name, homogeneous) + end + return self end function Storage:detach(name)