This commit is contained in:
2026-04-06 19:40:42 -07:00
commit da480fd7a4
7 changed files with 798 additions and 0 deletions

12
.vscode/settings.json vendored Normal file
View File

@@ -0,0 +1,12 @@
{
"Lua.workspace.library": [
"/home/ruffles/.vscode/extensions/pawz.polytoria-lua-1.1.1/api",
"${addons}/cc-tweaked/module/library"
],
"Lua.runtime.version": "Lua 5.3",
"Lua.runtime.builtin": {
"io": "disable",
"os": "disable"
},
"Lua.workspace.checkThirdParty": false
}

124
libs/entrypointlib.lua Normal file
View File

@@ -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

36
meshnetBackend.lua Normal file
View File

@@ -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

408
orderedit.lua Normal file
View File

@@ -0,0 +1,408 @@
-- SPDX-FileCopyrightText: 2017 Daniel Ratcliffe
--
-- SPDX-License-Identifier: LicenseRef-CCPL
-- Get file to edit
local x, y = 1, 1
local w, h = term.getSize()
local scrollX, scrollY = 0, 0
local tLines = {""}
local bRunning = true
local returnvalue = nil
-- Colours
local highlightColour, keywordColour, textColour, bgColour, errorColour
bgColour = colours.black
textColour = colours.white
highlightColour = colours.white
keywordColour = colours.white
errorColour = colours.white
-- Menus
local menu = require "cc.internal.menu"
local current_menu
local menu_items = {}
table.insert(menu_items, "Done")
local status_ok, status_text
local tCompletions
local nCompletion
local function redrawLines(line, endLine)
if not endLine then endLine = line end
local colour = term.getTextColour()
-- Highlight all lines between line and endLine, highlighting further lines if their
-- lexer state has changed and aborting at the end of the screen.
local changed = false
while (changed or line <= endLine) and line - scrollY < h do
term.setCursorPos(1 - scrollX, line - scrollY)
term.clearLine()
local contents = tLines[line]
if not contents then break end
print(contents)
line = line + 1
end
term.setTextColor(colours.white)
term.setCursorPos(x - scrollX, y - scrollY)
end
local function redrawText()
redrawLines(scrollY + 1, scrollY + h - 1)
end
local function redrawMenu()
-- Clear line
term.setCursorPos(1, h)
term.clearLine()
-- Draw line numbers
term.setCursorPos(w - #("Ln " .. y) + 1, h)
term.setTextColour(highlightColour)
term.write("Ln ")
term.setTextColour(textColour)
term.write(y)
term.setCursorPos(1, h)
if current_menu then
-- Draw menu
menu.draw(current_menu)
else
-- Draw status
term.setTextColour(colors.yellow)
term.write("Press ctrl to view options")
term.setTextColour(textColour)
end
-- Reset cursor
term.setCursorPos(x - scrollX, y - scrollY)
term.setCursorBlink(not current_menu)
end
local tMenuFuncs = {
Done = function()
local order = table.concat(tLines,"\n")
term.clear()
returnvalue = order
end
}
local function setCursor(newX, newY)
local _, oldY = x, y
x, y = newX, newY
local screenX = x - scrollX
local screenY = y - scrollY
local bRedraw = false
if screenX < 1 then
scrollX = x - 1
screenX = 1
bRedraw = true
elseif screenX > w then
scrollX = x - w
screenX = w
bRedraw = true
end
if screenY < 1 then
scrollY = y - 1
screenY = 1
bRedraw = true
elseif screenY > h - 1 then
scrollY = y - (h - 1)
screenY = h - 1
bRedraw = true
end
if bRedraw then
redrawText()
elseif y ~= oldY then
redrawLines(math.min(y, oldY), math.max(y, oldY))
else
redrawLines(y)
end
redrawMenu()
end
-- Actual program functionality begins
term.setBackgroundColour(bgColour)
term.clear()
term.setCursorPos(x, y)
term.setCursorBlink(true)
redrawText()
redrawMenu()
local function acceptCompletion()
if nCompletion then
-- Append the completion
local sCompletion = tCompletions[nCompletion]
tLines[y] = tLines[y] .. sCompletion
setCursor(x + #sCompletion, y)
end
end
local function handleMenuEvent(event)
assert(current_menu)
local result = menu.handle_event(current_menu, table.unpack(event, 1, event.n))
if result == false then
current_menu = nil
redrawMenu()
elseif result ~= nil then
tMenuFuncs[result]()
current_menu = nil
redrawMenu()
end
end
-- Handle input
while bRunning do
local event = table.pack(os.pullEvent())
if event[1] == "key" then
if current_menu then
handleMenuEvent(event)
if returnvalue then
return returnvalue
end
else
local key = event[2]
if key == keys.up then
if nCompletion then
-- Cycle completions
nCompletion = nCompletion - 1
if nCompletion < 1 then
nCompletion = #tCompletions
end
redrawLines(y)
elseif y > 1 then
-- Move cursor up
setCursor(
math.min(x, #tLines[y - 1] + 1),
y - 1
)
end
elseif key == keys.down then
if nCompletion then
-- Cycle completions
nCompletion = nCompletion + 1
if nCompletion > #tCompletions then
nCompletion = 1
end
redrawLines(y)
elseif y < #tLines then
-- Move cursor down
setCursor(
math.min(x, #tLines[y + 1] + 1),
y + 1
)
end
elseif key == keys.tab then
if nCompletion and x == #tLines[y] + 1 then
-- Accept autocomplete
acceptCompletion()
else
-- Indent line
local sLine = tLines[y]
tLines[y] = string.sub(sLine, 1, x - 1) .. " " .. string.sub(sLine, x)
setCursor(x + 4, y)
end
elseif key == keys.pageUp then
-- Move up a page
local newY
if y - (h - 1) >= 1 then
newY = y - (h - 1)
else
newY = 1
end
setCursor(
math.min(x, #tLines[newY] + 1),
newY
)
elseif key == keys.pageDown then
-- Move down a page
local newY
if y + (h - 1) <= #tLines then
newY = y + (h - 1)
else
newY = #tLines
end
local newX = math.min(x, #tLines[newY] + 1)
setCursor(newX, newY)
elseif key == keys.home then
-- Move cursor to the beginning
if x > 1 then
setCursor(1, y)
end
elseif key == keys["end"] then
-- Move cursor to the end
local nLimit = #tLines[y] + 1
if x < nLimit then
setCursor(nLimit, y)
end
elseif key == keys.left then
if x > 1 then
-- Move cursor left
setCursor(x - 1, y)
elseif x == 1 and y > 1 then
setCursor(#tLines[y - 1] + 1, y - 1)
end
elseif key == keys.right then
local nLimit = #tLines[y] + 1
if x < nLimit then
-- Move cursor right
setCursor(x + 1, y)
elseif nCompletion and x == #tLines[y] + 1 then
-- Accept autocomplete
acceptCompletion()
elseif x == nLimit and y < #tLines then
-- Go to next line
setCursor(1, y + 1)
end
elseif key == keys.delete then
local nLimit = #tLines[y] + 1
if x < nLimit then
local sLine = tLines[y]
tLines[y] = string.sub(sLine, 1, x - 1) .. string.sub(sLine, x + 1)
redrawLines(y)
elseif y < #tLines then
tLines[y] = tLines[y] .. tLines[y + 1]
table.remove(tLines, y + 1)
redrawText()
end
elseif key == keys.backspace then
if x > 1 then
-- Remove character
local sLine = tLines[y]
if x > 4 and string.sub(sLine, x - 4, x - 1) == " " and not string.sub(sLine, 1, x - 1):find("%S") then
tLines[y] = string.sub(sLine, 1, x - 5) .. string.sub(sLine, x)
setCursor(x - 4, y)
else
tLines[y] = string.sub(sLine, 1, x - 2) .. string.sub(sLine, x)
setCursor(x - 1, y)
end
elseif y > 1 then
-- Remove newline
local sPrevLen = #tLines[y - 1]
tLines[y - 1] = tLines[y - 1] .. tLines[y]
table.remove(tLines, y)
setCursor(sPrevLen + 1, y - 1)
redrawText()
end
elseif (key == keys.enter or key == keys.numPadEnter) then
-- Newline
local sLine = tLines[y]
local _, spaces = string.find(sLine, "^[ ]+")
if not spaces then
spaces = 0
end
tLines[y] = string.sub(sLine, 1, x - 1)
table.insert(tLines, y + 1, string.rep(' ', spaces) .. string.sub(sLine, x))
setCursor(spaces + 1, y + 1)
redrawText()
elseif key == keys.leftCtrl or key == keys.rightCtrl then
current_menu = menu.create(menu_items)
redrawMenu()
end
end
elseif event[1] == "char" then
if current_menu then
handleMenuEvent(event)
else
-- Input text
local sLine = tLines[y]
tLines[y] = string.sub(sLine, 1, x - 1) .. event[2] .. string.sub(sLine, x)
setCursor(x + 1, y)
end
elseif event[1] == "paste" then
-- Close menu if open
if current_menu then
current_menu = nil
redrawMenu()
end
-- Input text
local text = event[2]
local sLine = tLines[y]
tLines[y] = string.sub(sLine, 1, x - 1) .. text .. string.sub(sLine, x)
setCursor(x + #text, y)
elseif event[1] == "mouse_click" then
local button, cx, cy = event[2], event[3], event[4]
if current_menu then
handleMenuEvent(event)
else
if button == 1 then
-- Left click
if cy < h then
local newY = math.min(math.max(scrollY + cy, 1), #tLines)
local newX = math.min(math.max(scrollX + cx, 1), #tLines[newY] + 1)
setCursor(newX, newY)
else
current_menu = menu.create(menu_items)
redrawMenu()
end
end
end
elseif event[1] == "mouse_scroll" then
if not current_menu then
local direction = event[2]
if direction == -1 then
-- Scroll up
if scrollY > 0 then
-- Move cursor up
scrollY = scrollY - 1
redrawText()
end
elseif direction == 1 then
-- Scroll down
local nMaxScroll = #tLines - (h - 1)
if scrollY < nMaxScroll then
-- Move cursor down
scrollY = scrollY + 1
redrawText()
end
end
end
elseif event[1] == "term_resize" then
w, h = term.getSize()
setCursor(x, y)
redrawMenu()
redrawText()
end
end
-- Cleanup
term.clear()
term.setCursorBlink(false)
term.setCursorPos(1, 1)

64
startup.lua Normal file
View File

@@ -0,0 +1,64 @@
--os.pullEvent = os.pullEventRaw
local stratumDBlib = require("stratumDBlib")
local meshnetBackend = require("meshnetBackend")
meshnetBackend.setTarget(10)
stratumDBlib.setBackend(meshnetBackend)
print("connecting to database...")
local trans = stratumDBlib.loadTransforms("transforms","stratumDBlib")
local greeting = "Welcome to Caden's Smithy!"
local instruction = "Please state your name to make a request:"
local instruction2 = "Now please type out a request."
local pressanykey = "Press any key to continue."
local lastchance1 = "Are you sure you want to put in an order?"
local lastchance3 = "Hold ctrl+r (or cmd+r on macos) to cancel"
local ordersubmitted1 = "Your order has been submitted!"
while true do
local dx,dy = term.getSize()
term.setBackgroundColor(colors.gray)
term.clear()
term.setCursorPos(dx/2-#greeting/2,dy/2-3)
term.write(greeting)
term.setCursorPos(dx/2-#instruction/2,dy/2-2)
term.write(instruction)
term.setBackgroundColor(colors.lightGray)
term.setCursorPos(1,dy/2+1)
term.clearLine()
term.setBackgroundColor(colors.lightGray)
term.setCursorPos(1,dy/2-1)
term.clearLine()
term.setCursorPos(1,dy/2)
term.setBackgroundColor(colors.black)
term.clearLine()
local name = read()
if name ~= "" then
term.setBackgroundColor(colors.gray)
term.clear()
term.setCursorPos(dx/2-#instruction2/2,dy/2)
term.write(instruction2)
term.setCursorPos(dx/2-#pressanykey/2,dy/2+1)
term.write(pressanykey)
os.pullEvent("key")
local request = loadfile("orderedit.lua","t",_ENV)()
if request:gsub("[\n ]","") ~= "" then
term.setBackgroundColor(colors.gray)
term.clear()
term.setCursorPos(dx/2-#lastchance1/2,dy/2)
term.write(lastchance1)
term.setCursorPos(dx/2-#pressanykey/2,dy/2+1)
term.write(pressanykey)
term.setCursorPos(dx/2-#lastchance3/2,dy/2+2)
term.write(lastchance3)
term.setCursorBlink(false)
os.pullEvent("key")
trans.addOrder(name,request)
term.setBackgroundColor(colors.gray)
term.clear()
term.setCursorPos(dx/2-#ordersubmitted1/2,dy/2)
term.write(ordersubmitted1)
term.setCursorPos(dx/2-#pressanykey/2,dy/2+1)
term.write(pressanykey)
os.pullEvent("key")
end
end
end

133
stratumDBlib.lua Normal file
View File

@@ -0,0 +1,133 @@
local stratum = {}
-- 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
---applys macros to code
---@param code string
---@return string
function stratum.applyMacros(code,libpath)
local import_name = ""
local alphabet = {"A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"}
for _ = 1,10 do
import_name = import_name..alphabet[math.random(1,#alphabet)]
end
local import_string = "local "..import_name.." = require(\""..libpath.."\")\n"
local splits = split(" "..code,"%-%-#")
for instanceIndex = 2,#splits do
local instance = splits[instanceIndex]
local lines = split(instance:gsub("\n"," \n").." ","\n")
local nonSpacedLines = split(instance,"\n")
local contextDepth = 0
local finalCodeLines = {}
for lineIndex = 2,#lines do
local line = lines[lineIndex]
local _,dos = line:gsub("do ","")
local _,funcs = line:gsub("function ","")
local _,thens = line:gsub("then ","")
local _,ends = line:gsub("end ","")
contextDepth = contextDepth+dos+funcs+thens-ends
finalCodeLines[#finalCodeLines+1] = nonSpacedLines[lineIndex]
if contextDepth <= 0 then
break
end
end
local replaceSub = table.concat(finalCodeLines,"\n")
local opcount = 0
---@type string
local funcdef = table.remove(finalCodeLines,1)
table.remove(finalCodeLines)
local body = table.concat(finalCodeLines,"\n")
local replace = funcdef
if nonSpacedLines[1] == "data_transform" and string.find(funcdef,"function ") then
funcdef,opcount = funcdef:gsub("function ",""):gsub("local ", "")
local defsplit = split(funcdef, "%(")
defsplit[2] = split(defsplit[2],"%)")[1] or "_"
replace = defsplit[1].." = "..import_name..".createTransform(\""..defsplit[1].."\",\"local data,"..defsplit[2].." = ...\\n"..body:gsub("\n","\\n"):gsub("\"","\\\"").."\")"
if opcount > 0 then
replace = "local "..replace
end
elseif nonSpacedLines[1] == "common_transform" and string.find(funcdef,"function ") then
funcdef = funcdef:gsub("function ",""):gsub("local ", "")
local defsplit = split(funcdef, "%(")
defsplit[2] = split(defsplit[2],"%)")[1] or "_"
replace = replaceSub.."\n"..(import_name..".createTransform(\""..defsplit[1].."\",\"local data,"..defsplit[2].." = ...\\n"..body:gsub("\n","\\n"):gsub("\"","\\\"").."\")")
end
code = code:gsub(replaceSub:gsub("([%(%)%.%%%+%-%*%?%[%]%^%$])", "%%%1"),replace)
end
return import_string..code
end
function stratum.loadTransforms(modules,libpath)
local file,err = package.searchpath(shell.dir(),modules)
if not file then
file,err = package.searchpath(shell.dir(),modules..".lua")
end
if not file then error("failed to find package: "..err,2) end
local file = fs.open(file,"r")
if not file then error("failed to open file",2) end
local contents = file.readAll()
file.close()
if not contents then error("failed to read file",2) end
contents = stratum.applyMacros(contents,libpath)
local trans, err = load(contents,"transforms","t",_ENV)
if not trans then error(err,2) end
return trans()
end
function stratum.setBackend(backend)
if backend.sendMessage and backend.receiveMessage then
_G._STRATUMBACKEND = backend
end
end
function stratum.createTransform(name,func)
local backend = _STRATUMBACKEND
if not backend or not (backend.sendMessage and backend.receiveMessage) then error("stratum backend not defined or defined incorrectly",2) end
backend.sendMessage({protocol="RegisterTransform",name=name,functionBody=func})
while true do
local message = backend.receiveMessage()
if message.protocol == "TransformRegistered" and message.name == name then
return function (...)
backend.sendMessage({protocol = "CallTransform", name=name,params={...}})
while true do
local message = backend.receiveMessage()
if message.protocol == "TransformResult" and message.name == name then
return table.unpack(message.result)
elseif message.protocol == "TransformError" and message.name == name then
error(message.error,2)
end
end
end
end
end
end
return stratum

21
transforms.lua Normal file
View File

@@ -0,0 +1,21 @@
local trans = {}
--#data_transform
function trans.addOrder(requestorName,request)
if not data.orders then data.orders = {} end
data.orders[#data.orders+1] = {name=requestorName,request=request}
end
--#data_transform
function trans.getOrder(index)
local order = (data.orders or {})[index] or {}
return order.name,order.request
end
--#data_transform
function trans.removeOrder(index)
local order = table.remove(data.orders or {},index) or {}
return order.name,order.request
end
return trans