@ -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 t ext / t able 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 ( ) }