From 2c95dc20e3cc676d585b762078a1f8e0ef02f8c4 Mon Sep 17 00:00:00 2001 From: Felix Stupp Date: Mon, 18 Mar 2019 17:46:44 +0100 Subject: [PATCH] WIP --- init.lua | 139 ++++++++++++++++++++++++++++--------- system/runtime/access.lua | 8 ++- system/runtime/package.lua | 15 +++- 3 files changed, 127 insertions(+), 35 deletions(-) diff --git a/init.lua b/init.lua index c290fef..faa0ae2 100644 --- a/init.lua +++ b/init.lua @@ -1,7 +1,12 @@ --- system extension +-- system api extension +function assert(ok, ...) + if not ok then + error(...) + end +end function table.copy(tab) local ret = {} - for k,v in pairs(tab) do + for k, v in pairs(tab) do if type(v) == "table" then ret[k] = table.copy(v) else @@ -22,28 +27,38 @@ function string.split(inputstr, sep) end return t end --- both following functions read and write on system drive -local function readFile(filename) + +-- following functions read and write on system drive +local sysdrv = {} +function sysdrv.getFolders(path) + +end +function sysdrv.getFiles(path) + +end +function sysdrv.readFile(filename) + -- read file +end +function sysdrv.readFileTab(filename) -- read file and create table end -local function writeFile(filename,tab) - -- write table to file +function sysdrv.writeFile(filename, txt) + -- write text / table to file end -local function loadFile() local driverControl = {} local signalControl = {} -for dev,evs in pairs(readFile("/apps/drivers.cfg")) do - for ev,pos in pairs(evs) do - driverControl[ev] = {dev,pos} +for dev, evs in pairs(sysdrv.readFileTab("/apps/drivers.cfg")) do + for ev, pos in pairs(evs) do + driverControl[ev] = {dev, pos} end end local defaultPackageRunning = { enabled = false neededFor = {} } -for name,info in pairs(packList) do +for name, info in pairs(packList) do info.running = table.copy(defaultPackageRunning) end @@ -52,46 +67,108 @@ local apiSet = {} local shortcuts = {["fs"]="filesystem"} -- internal runtime api -local running = {} + local runtime = {} -local metaAccess = function(hid,rights) - return (type(hid) == "table" and setmetatable({},{__index=function(t,k) - return metaAccess(hid[k],rights) +local running = {} -- { thread , } +_G.runtime = runtime + +local metaAccess = function(hid, data) + return (type(hid) == "table" and setmetatable({}, {__index=function(t, k) + return metaAccess(hid[k], data) end})) or (type(hid) == "function" and function(...) - return hid(table.copy(rights),...) + return (data and hid(table.copy(data), ...)) or hid(...) end) or hid end -function runtime.add(scriptFunction,dataTable,...) - local env = setmetatable({},{__index=function(t,k) +function runtime.add(scriptFunction, dataTable, ...) + local env = setmetatable({}, {__index=function(t, k) return metaAccess(apiSet[shortcuts[k] or k]) end}) - local cor,err = coroutine.create(scriptFunction,env) + local cor, err = coroutine.create(scriptFunction, env) if not cor then - return nil,err - end - local ok,err = coroutine.resume(cor,...) - if not ok then - return nil,err + return false, err end local pid = table.maxn(running) + 1 - running[pid] = dataTable or {} + data = data or {} + data.runtime = data.runtime or {} + local r = data.runtime + for k, v in pairs({ + isDaemon = false, + byModule = "", + sourceType = "unknown" -- unknown , module , package , filesystem , sideLoaded | filesystem means it was a file what is not registered as part of a package + sourceInfo = "" -- nil , moduleName , packageName , path , executingPid + }) do + r[k] = r[k] or v + end + running[pid] = data running[pid].thread = cor + local ok, err = coroutine.resume(cor, ...) + if not ok then + return false, err + end return pid end function runtime.status(pid) return coroutine.status(running[pid].thread) end --- initialization sequence +-- module initialization + +local modules = {runtime = runtime, sysdrv = sysdrv} +local modEnv = setmetatable({}, {__index = function(t, k) + return metaAccess(modules[k] or _G[k]) +end, __newindex = function(t, k, v) end}) -print("Load packages ...") -for name,info in pairs(packList) do - if info.enabledState > 0 then - local ok,err = package.enable(name) - print(" "..name..((not ok and " could not be enabled: "..err) or " enabled")) +print("Preinstalled modules") +for k, v in pairs(modules) do + print(" "..k) +end +print("Load external modules") +for k, v in pairs(getFiles("/system/runtime")) do + if v:sub(-4) == ".lua" then + local name = tostring(v:sub(1, -5)) + assert(_G[name] or modules[name], "ZockerCore: Name of Module \""..name.."\" is not valid", 0) + print(" "..name) + local raw = sysdrv.readFile("/system/runtime/"..name..".lua") + assert(type(raw) ~= "string", "ZockerCore: Module "..name.." cannot be loaded", 0) + local bin, err = load(raw, "modules."..name, "t", modEnv) + assert(bin, err, 0) + modules[name] = bin() end end +print("Initialize modules") +for k, v in pairs(modules) do + if type(v.init) == "function" then + print(" "..k) + v.init() + end +end +print("Add modules to runtime") +for k, v in pairs(modules) do + if type(v.runtime) == "function" then + local name, desc = k, "/system/runtime/"..k..".lua" + if type(v.infos) == "function" then + name, desc = v.infos() + end + print(" "..name) + local ok, err = runtime.add(v, { + runtime = { + name = name, + description = desc, + isDaemon = true, + byModule = k, + }, + access = { + level = 4 + }, + }) + assert(ok, err, 0) + end +end + +-- run sequence +print("Pass control to modules") +computer.pushSignal("boot_completed") while true do local eD = {computer.pullSignal()} diff --git a/system/runtime/access.lua b/system/runtime/access.lua index 99b69f0..5788664 100644 --- a/system/runtime/access.lua +++ b/system/runtime/access.lua @@ -1,6 +1,5 @@ local acc = {} --- 0: for guest only asking, for else free --- without asking : 1: user | 2: admin | 3:system +-- level : 0: guests (controlled by other app) | 1: user | 2: admin | 3:system -- options : 4: with argument | 8: single asking local perms = { -- "permName" , "permName#argument" changePermissions = 3+4+8, @@ -27,7 +26,7 @@ local perms = { -- "permName" , "permName#argument" changePackages = 3+4+8, } local appPerms = {} -- "packageName" -> "permName" , "packageName#permName#argument" -local cfg = readFile("/apps/access.cfg") +local cfg = readFileTab("/apps/access.cfg") local function splitPerm(name) if type(name) ~= "string" or name:len() < 1 then @@ -140,6 +139,9 @@ function acc.save() writeFile("/apps/access.cfg",cfg) end +function acc.infos() + return "", +end function acc.init() for end diff --git a/system/runtime/package.lua b/system/runtime/package.lua index 1f50792..b2d3376 100644 --- a/system/runtime/package.lua +++ b/system/runtime/package.lua @@ -1,5 +1,5 @@ local package = {} -local packList = readFile("/apps/installed.cfg") +local packList = readFileTab("/apps/installed.cfg") local packageLoading = {} function package.setEntry(name,info) @@ -102,4 +102,17 @@ function package.save() writeFile("/apps/installed.cfg",sav) end +function package.init() + print("Load packages ...") + for name,info in pairs(packList) do + if info.enabledState > 0 then + local ok,err = package.enable(name) + print(" "..name..((not ok and " could not be enabled: "..err) or " enabled")) + end + end +end +function package.runtime() + +end + return package \ No newline at end of file