1
0

add save and load to file

This commit is contained in:
Rokas Puzonas 2022-09-03 19:15:57 +00:00
parent c1a3ca6bb8
commit 7ae6f135fa
2 changed files with 35 additions and 13 deletions

View File

@ -33,19 +33,14 @@ end
local function main()
local main_view = require("views.main")
local bundles = {
green = {
["minecraft:kelp"] = 64,
["minecraft:grass_block"] = 64
},
terraform = {
["minecraft:grass_block"] = 64,
["minecraft:stone"] = 64,
["#green"] = 1
}
}
local inventories = list_inventories()
main_view:prepare(inventories, bundles, "minecraft:barrel_4")
local save_file = "/storage-config.txt"
if fs.exists(save_file) then
main_view:load_from_file(save_file)
else
local inventories = list_inventories()
main_view:prepare(inventories, {}, "minecraft:barrel_4")
end
main_view.save_file = save_file
local update_interval = 0.1
local update_timer = os.startTimer(update_interval)

View File

@ -296,6 +296,29 @@ function main_view:prepare(inventories, bundles, result_inventory)
end
end
function main_view:save_to_file(filename)
local f, err = io.open(filename, "w")
if not f then return nil, err end
f:write(textutils.serialise{
inventories = self.inventories,
bundles = self.bundles,
result_inventory = self.result_inventory
})
f:close()
end
function main_view:load_from_file(filename)
local f, err = io.open(filename, "r")
if not f then return nil, err end
local data = textutils.unserialise(f:read("a"))
main_view:prepare(data.inventories, data.bundles, data.result_inventory)
f:close()
end
@ -589,6 +612,10 @@ function main_view:save_bundle(name, items, bundles)
self.left_store.bundles = derive_available_bundles(self.left_store.items, self.bundle_details)
self:refresh_filtered_names(self.left_store, self.left_area)
if self.save_file then
self:save_to_file(self.save_file)
end
return true
end