small repo system progress

This commit is contained in:
2025-12-24 21:35:25 -08:00
parent a2494b11b0
commit 849b2508df
5 changed files with 85 additions and 21 deletions

View File

@@ -1,3 +1,54 @@
local lib = {}
local expect = require "cc.expect".expect
local file = fs.open("/repos.dat","r")
local repos = {}
if file then
repos = textutils.unserialise(file.readAll() or "") or repos
end
local function saveRepos()
local file = fs.open("/repos.dat","w")
file.write(textutils.serialise(repos))
file.close()
end
function lib.addRepo(id)
expect(1,id,"number")
repos[id] = "computer"
saveRepos()
end
function lib.getRepos()
return repos
end
function lib.rmRepo(id)
expect(1,id,"number")
repos[id] = nil
saveRepos()
end
function lib.listPackages(type)
local queued_requests = {}
local recorded_packages = {}
for k,v in pairs(repos) do
if v == "computer" then
network.send({proto="query",package_type=type},k)
queued_requests[k] = "computer"
end
end
local next_timestamp = os.clock()+5
repeat
local event = {os.pullEvent()}
if event[1] == "network_packet" then
if queued_requests[event[3]] == "computer" and event[2].proto == "query_resp" and event[2].package_type == type then
for name,data in pairs(event[2].data) do
if recorded_packages[name] then
recorded_packages[name].repos[event[3]] = event[4]
else
data.repos = {[event[3]] = event[4]}
recorded_packages[name] = data
end
end
end
end
until next(queued_requests) == nil or os.clock() >= next_timestamp
return recorded_packages
end
_G.repo = lib