This commit is contained in:
2026-04-06 14:13:46 -07:00
commit eb4659817e
11 changed files with 857 additions and 0 deletions

10
stratumBackend/init.lua Normal file
View File

@@ -0,0 +1,10 @@
local sdb = require("stratumBackend.stratumDBlib")
local meshnetBackend = require("stratumBackend.meshnetBackend")
print("loaded meshent backend")
meshnetBackend.setTarget(10)
print("set target")
sdb.setBackend(meshnetBackend)
print("set backend")
local trans = sdb.loadTransforms("stratumBackend/transforms","stratumBackend.stratumDBlib")
print("finished preparing transforms")
return trans

View File

@@ -0,0 +1,36 @@
local backend = {}
local network = require("entrypointlib")
local loop = coroutine.create(network.loop)
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

View File

@@ -0,0 +1,137 @@
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
print("applying macros")
contents = stratum.applyMacros(contents,libpath)
print("loading contents")
local trans, err = load(contents,"transforms","t",_ENV)
if not trans then error(err,2) end
print("running contents")
return trans()
end
function stratum.setBackend(backend)
if backend.sendMessage and backend.receiveMessage then
_G._STRATUMBACKEND = backend
end
end
function stratum.createTransform(name,func)
print("registering "..name)
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

View File

@@ -0,0 +1,54 @@
local db = {}
--#data_transform
function db.getMaterials()
local materials = {}
for k,_ in pairs(data.materials or {}) do
materials[#materials+1] = k
end
return materials
end
--#data_transform
function db.getPrice(mat)
return (data.materials or {})[mat]
end
--#data_transform
function db.getCurrencies()
local currencies = {}
for k,_ in pairs(data.currencies or {}) do
currencies[#currencies+1] = k
end
return currencies
end
--#data_transform
function db.getCurrencyWorth(cur)
return (data.currencies or {})[cur]
end
--#data_transform
function db.setPrice(mat,price)
if not data.materials then data.materials = {} end
data.materials[mat] = price
end
--#data_transform
function db.setWorth(cur,worth)
if not data.currencies then data.currencies = {} end
data.currencies[cur] = worth
end
--#data_transform
function db.getCurrencyUnit()
return data.currency_unit or "cr"
end
--#data_transform
function db.setCurrencyUnit(unit)
data.currency_unit = unit
end
return db