commit 6451e59c3e2f6fc62691ba1d86491c08bafafea0 Author: Rivulet Date: Mon Apr 6 14:17:45 2026 -0700 >.< diff --git a/backends/meshnetBackend.lua b/backends/meshnetBackend.lua new file mode 100644 index 0000000..9bd94c6 --- /dev/null +++ b/backends/meshnetBackend.lua @@ -0,0 +1,36 @@ +local backend = {} +local network = require("libs.entrypointlib") +local loop = coroutine.create(network.loop or function () while true do sleep(100) end end) +local filter = nil + +-- some black magic bs that i pulled from https://gist.github.com/MCJack123/473475f07b980d57dd2bd818026c97e8 +local env = getfenv(rednet.run) +env.os = setmetatable({ + pullEventRaw = function () + local ev = table.pack(coroutine.yield()) + if filter == nil or filter == ev[1] then + _,filter = coroutine.resume(loop,table.unpack(ev, 1, ev.n)) + end + return table.unpack(ev, 1, ev.n) + end +},{__index=env.os}) + +local target = nil +function backend.setTarget(id) + target = id +end + +function backend.sendMessage(msg, id) + if not (id or target) then error("failed to imply or specify target id",2) end + network.send(msg,id or target) +end + +function backend.receiveMessage() + local _,msg,id,hops + repeat + _,msg,id,hops = os.pullEvent("network_packet") + until id == (target or id) + return msg,id +end + +return backend \ No newline at end of file diff --git a/data.db b/data.db new file mode 100644 index 0000000..5a34769 --- /dev/null +++ b/data.db @@ -0,0 +1,5 @@ +{ + materials = { + Steel = 0.0625, + }, +} \ No newline at end of file diff --git a/libs/entrypointlib.lua b/libs/entrypointlib.lua new file mode 100644 index 0000000..449df35 --- /dev/null +++ b/libs/entrypointlib.lua @@ -0,0 +1,124 @@ +local pullEvent = os.pullEventRaw +local modem = peripheral.find("modem",function (s) return peripheral.wrap(s).isWireless() end) +term.clear() +term.setCursorPos(1,1) +local network = {} +local message_queue = {} +modem.open(15125) +local 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 and distance < (msg.max_distance or 128) then + canidate.max_distance = (msg.max_distance or 128) + canidate.id = msg.sender + canidate.distance = distance + end + end + end + end +end) +local last_heartbeat = os.epoch("utc") +modem.transmit(15125,15125,{protocol="entrypoint_connect",sender=os.getComputerID(),target=canidate.id}) + +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 + last_heartbeat = os.epoch("utc") + modem.transmit(15125,15125,{protocol="heartbeat_response",sender=os.getComputerID(),target=canidate.id}) + 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 = 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 and distance > (msg.max_distance or 128) then + canidate.max_distance = msg.max_distance + canidate.id = msg.sender + canidate.distance = distance + end + end + end + end + end) + 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") + end + end + elseif msg.protocol == "packet" then + if msg.hops >= 1 and msg.destination == os.computerID() then + os.queueEvent("network_packet",msg.content,msg.sender,msg.hops) + end + end + end + end +end + +function network.send(msg,destination) + if not msg then error("No message provided",2) end + if not destination then error("No destination provided",2) end + message_queue[#message_queue+1] = {protocol="packet",content=msg,destination=destination,sender=os.getComputerID(),hops=0} +end + +local function connect() + while true do + 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 > (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 + end + end + end + end) + if canidate.id == -1 then + sleep(5) + else + 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) + if msg then + modem.transmit(15125,15125,msg) + end + end + sleep() + end +end +function network.getCandidate() + return canidate +end + +function network.loop() + parallel.waitForAny(connect,receive) +end + +return network diff --git a/stratum.lua b/stratum.lua new file mode 100644 index 0000000..0f8bb47 --- /dev/null +++ b/stratum.lua @@ -0,0 +1,106 @@ +local backend = require("backends.meshnetBackend") +local registeredTransforms = {} +local message_queue = {} + +local data = {} + +local function loadData() + if fs.exists("data.db") then + local file = fs.open("data.db","r") + if file then + data = textutils.unserialise(file.readAll() or "{}") or {} + file.close() + end + end +end + +local function saveData() + local file = fs.open("data.db","w") + if file then + file.write(textutils.serialise(data)) + file.close() + end +end + +loadData() + +-- Source - https://stackoverflow.com/a/1579673 +-- Posted by Faisal Hanif, modified by community. See post 'Timeline' for change history +-- Retrieved 2026-04-05, License - CC BY-SA 3.0 + +---splits a string by a pattern +---@param pString string +---@param pPattern string +---@return string[] +local function split(pString, pPattern) + local Table = {} -- NOTE: use {n = 0} in Lua-5.0 + local fpat = "(.-)" .. pPattern + local last_end = 1 + local s, e, cap = pString:find(fpat, 1) + while s do + if s ~= 1 or cap ~= "" then + table.insert(Table,cap) + end + last_end = e+1 + s, e, cap = pString:find(fpat, last_end) + end + if last_end <= #pString then + cap = pString:sub(last_end) + table.insert(Table, cap) + end + return Table +end + +local function generateEnviorment() + local env = {string=string,colors=colors,math=math,table=table,print=print,textutils=textutils,error=error,pairs=pairs,ipairs=ipairs} + for k,v in pairs(registeredTransforms) do + local focus = env + local splits = split(k,"%.") + for i,indx in ipairs(splits) do + if i < #splits then + if not focus[indx] then focus[indx] = {} end + focus = focus[indx] + else + focus[indx] = function (...) return load(v.body,v.name,"t",env)({},...) end + end + end + end + return env +end + +local function process() + while true do + local msg = table.remove(message_queue,1) + if msg then + local id,msg = msg.id,msg.msg + if msg.protocol == "RegisterTransform" then + registeredTransforms[msg.name] = {body = msg.functionBody,name = msg.name} + backend.sendMessage({protocol="TransformRegistered",name=msg.name},id) + elseif msg.protocol == "CallTransform" then + local func = (registeredTransforms[msg.name] or {body="function() end",name="not real"}) + table.insert(msg.params,1,data) + local data = {pcall(load(func.body,func.name,"t",generateEnviorment()) or function () end,table.unpack(msg.params))} + if table.remove(data,1) == true then + backend.sendMessage({protocol="TransformResult",result = data,name=msg.name},id) + saveData() + else + backend.sendMessage({protocol="TransformError",error = data[1],name=msg.name},id) + loadData() + end + end + end + sleep(0) + end +end + +local function receive() + while true do + local msg,id = backend.receiveMessage() + print(msg.protocol..": ",id) + message_queue[#message_queue+1] = {msg=msg,id=id} + end +end + + +print("starting stratum...") +parallel.waitForAny(process,receive) \ No newline at end of file diff --git a/test.lua b/test.lua new file mode 100644 index 0000000..d999ecb --- /dev/null +++ b/test.lua @@ -0,0 +1,10 @@ +local eplib = require("libs.entrypointlib") + +local function test() + while true do + print(os.pullEvent("network_packet")) + eplib.send("test",9) + end +end + +parallel.waitForAny(test,eplib.loop) \ No newline at end of file