>.<
This commit is contained in:
106
stratum.lua
Normal file
106
stratum.lua
Normal file
@@ -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)
|
||||
Reference in New Issue
Block a user