Files
cash-register/stratumBackend/stratumDBlib.lua
2026-04-06 14:35:45 -07:00

133 lines
5.2 KiB
Lua

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