From 849b2508df25c0b638ca66ab3419a3dd65bb990f Mon Sep 17 00:00:00 2001 From: Rivulet Date: Wed, 24 Dec 2025 21:35:25 -0800 Subject: [PATCH] small repo system progress --- .gitignore | 1 + install.lua | 4 ++- scan.lua | 6 ++-- startup/00_entrypointclient.lua | 44 ++++++++++++++++------------ startup/01_repo.lua | 51 +++++++++++++++++++++++++++++++++ 5 files changed, 85 insertions(+), 21 deletions(-) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..7fb5d66 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.settings \ No newline at end of file diff --git a/install.lua b/install.lua index ddd7b8c..915b549 100644 --- a/install.lua +++ b/install.lua @@ -20,7 +20,9 @@ local filesystem = { "startup/01_repo.lua", "libs/containers.lua", "libs/windows.lua", - "apps/com.ruffles.launcher", + "libs/deflate.lua", + "libs/persistent-storage.lua", + "apps/org.ruffles.launcher", }, dirs = { "global-libraries", diff --git a/scan.lua b/scan.lua index b039458..f5dac7b 100644 --- a/scan.lua +++ b/scan.lua @@ -1,7 +1,7 @@ local exceptions = { ["/scan.lua"] = true, - ["/install.lua"] = true, ["scan.lua"] = true, + ["/install.lua"] = true, ["install.lua"] = true, ["/.settings"] = true, [".settings"] = true, @@ -10,7 +10,9 @@ local exceptions = { ["/rom"] = true, ["rom"] = true, ["/scan.dat"] = true, - ["scan.dat"] = true + ["scan.dat"] = true, + ["/.gitignore"] = true, + [".gitignore"] = true } local function scan(dir) local out = {} diff --git a/startup/00_entrypointclient.lua b/startup/00_entrypointclient.lua index 4734c84..a3f87ff 100644 --- a/startup/00_entrypointclient.lua +++ b/startup/00_entrypointclient.lua @@ -1,6 +1,3 @@ -local max_distance = 120 -local expect = dofile("rom/modules/main/cc/expect.lua") -local expect, field = expect.expect, expect.field local pullEvent = os.pullEventRaw local modem = peripheral.find("modem",function (s) return peripheral.wrap(s).isWireless() end) term.clear() @@ -12,26 +9,29 @@ if not modem then end local message_queue = {} modem.open(15125) -local canidate = {id = -1, distance = max_distance} +local canidate = {id = -1, distance = 764, max_distance=0} local last_heartbeat = os.epoch("utc") + local function receive() while true do local _, _, channel, _, msg, distance = pullEvent("modem_message") if channel == 15125 then if msg.protocol == "heartbeat" and msg.target == os.getComputerID() and msg.sender == canidate.id then - canidate.distance = distance last_heartbeat = os.epoch("utc") modem.transmit(15125,15125,{protocol="heartbeat_response",sender=os.getComputerID(),target=canidate.id}) - if distance > max_distance then + canidate.distance = distance + canidate.max_distance = msg.max_distance + if distance > (msg.max_distance or 764) then modem.transmit(15125,15125,{protocol="entrypoint_disconnect",sender=os.getComputerID(),target=canidate.id}) - canidate = {id = -1, distance = max_distance} + canidate = {id = -1, distance = 764, max_distance=0} parallel.waitForAny(function () repeat sleep(0.1) until canidate.id ~= -1 end, function () while true do local _, _, channel, _, msg, distance = pullEvent("modem_message") if channel == 15125 then if msg.protocol == "entrypoint_advertise" then - if distance < canidate.distance then + if distance < canidate.distance and distance < (msg.max_distance or 128) then + canidate.max_distance = msg.max_distance canidate.id = msg.sender canidate.distance = distance end @@ -61,21 +61,20 @@ function _G.network.send(msg,destination) message_queue[#message_queue+1] = {protocol="packet",content=msg,destination=destination,sender=os.getComputerID(),hops=0} end -function _G.network.getCandidate() - return canidate -end - local function connect() while true do - if os.epoch("utc") - last_heartbeat > 200 then - canidate = {id = -1, distance = max_distance} + if os.epoch("utc") - last_heartbeat > math.max((300*canidate.distance)/100,300) then + canidate = {id = -1, distance = 764, max_distance=0} parallel.waitForAny(function () repeat sleep(0.1) until canidate.id ~= -1 end, function () while true do local _, _, channel, _, msg, distance = pullEvent("modem_message") if channel == 15125 then if msg.protocol == "entrypoint_advertise" then - if distance < canidate.distance then + if distance > (msg.max_distance or 128) then + modem.transmit(15125,15125,{protocol="out_of_range",sender=os.getComputerID(),target=msg.sender}) + elseif distance < canidate.distance then + canidate.max_distance = msg.max_distance canidate.id = msg.sender canidate.distance = distance end @@ -86,8 +85,12 @@ local function connect() if canidate.id == -1 then sleep(5) else - modem.transmit(15125,15125,{protocol="entrypoint_connect",sender=os.getComputerID(),target=canidate.id}) - last_heartbeat = os.epoch("utc") + if canidate.distance > (canidate.max_distance or 128) then + modem.transmit(15125,15125,{protocol="out_of_range",sender=os.getComputerID(),target=canidate.id}) + else + modem.transmit(15125,15125,{protocol="entrypoint_connect",sender=os.getComputerID(),target=canidate.id}) + last_heartbeat = os.epoch("utc") + end end else local msg = table.remove(message_queue,1) @@ -98,6 +101,9 @@ local function connect() sleep() end end +function _G.network.getCandidate() + return canidate +end function _G.network.run() parallel.waitForAny(function () repeat sleep(0.1) until canidate.id ~= -1 end, function () @@ -105,7 +111,9 @@ function _G.network.run() local _, _, channel, _, msg, distance = pullEvent("modem_message") if channel == 15125 then if msg.protocol == "entrypoint_advertise" then - if distance < canidate.distance then + if distance > msg.max_distance then + modem.transmit(15125,15125,{protocol="out_of_range",sender=os.getComputerID(),target=msg.sender}) + elseif distance < canidate.distance then canidate.id = msg.sender canidate.distance = distance end diff --git a/startup/01_repo.lua b/startup/01_repo.lua index e9afa9f..c01fa13 100644 --- a/startup/01_repo.lua +++ b/startup/01_repo.lua @@ -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 \ No newline at end of file