e
This commit is contained in:
187
AceVM/init
187
AceVM/init
@@ -1,25 +1,9 @@
|
||||
local function deepcopy(orig, copies)
|
||||
copies = copies or {}
|
||||
|
||||
if type(orig) ~= 'table' then
|
||||
return orig
|
||||
elseif copies[orig] then
|
||||
return copies[orig]
|
||||
end
|
||||
|
||||
local copy = {}
|
||||
copies[orig] = copy
|
||||
|
||||
for k, v in next, orig, nil do
|
||||
local copied_key = deepcopy(k, copies)
|
||||
local copied_val = deepcopy(v, copies)
|
||||
copy[copied_key] = copied_val
|
||||
end
|
||||
|
||||
return copy
|
||||
end
|
||||
local _VG = deepcopy(_G)
|
||||
_VG._G=_VG
|
||||
aceVM = {}
|
||||
aceVM.components={}
|
||||
aceVM.aceKeys={}
|
||||
aceVM.aceKeys[keys.enter]="\n"
|
||||
aceVM.aceKeys[keys.backspace]="\b"
|
||||
aceVM.aceKeys[keys.tab]="\t"
|
||||
|
||||
function string.split(str, delim, maxResultCountOrNil)
|
||||
assert(#delim == 1, "only delim len 1 supported for now")
|
||||
@@ -39,64 +23,29 @@ function string.split(str, delim, maxResultCountOrNil)
|
||||
return rv
|
||||
end
|
||||
|
||||
function _VG.sleep(nTime)
|
||||
local timer = os.startTimer(nTime or 0)
|
||||
repeat
|
||||
local _, param = coroutine.yield("timer")
|
||||
until param == timer
|
||||
aceVM.INTERNAL_EVENT_QUEUE={}
|
||||
aceVM.INTERNAL_COMPONENT_REGESTRY={}
|
||||
aceVM.INTERNAL_RUNTIME_FUNCTIONS={}
|
||||
aceVM.BIOS_CORO=nil
|
||||
|
||||
function aceVM.addEventRaw(...)
|
||||
aceVM.INTERNAL_EVENT_QUEUE[#aceVM.INTERNAL_EVENT_QUEUE+1] = {...}
|
||||
end
|
||||
|
||||
INTERNAL_DISKS={}
|
||||
INTERNAL_SCREENS={}
|
||||
INTERNAL_NET={}
|
||||
INTERNAL_EVENT_QUEUE={}
|
||||
INTERNAL_COMPONENT_REGESTRY={}
|
||||
INTERNAL_RUNTIME_FUNCTIONS={}
|
||||
BIOS_CORO=nil
|
||||
|
||||
function addEventRaw(...)
|
||||
INTERNAL_EVENT_QUEUE[#INTERNAL_EVENT_QUEUE+1] = {...}
|
||||
end
|
||||
|
||||
function newComponent(type, object)
|
||||
local id = #INTERNAL_COMPONENT_REGESTRY+1
|
||||
INTERNAL_COMPONENT_REGESTRY[id] = {type=type, object=object}
|
||||
addEventRaw("componentAdded", type, object)
|
||||
function aceVM.newComponent(type, object)
|
||||
local id = #aceVM.INTERNAL_COMPONENT_REGESTRY+1
|
||||
aceVM.INTERNAL_COMPONENT_REGESTRY[id] = {type=type, object=object}
|
||||
aceVM.addEventRaw("componentAdded", type, object)
|
||||
return {
|
||||
remove=function()
|
||||
INTERNAL_COMPONENT_REGESTRY[id]=nil
|
||||
addEventRaw("componentRemoved", type)
|
||||
aceVM.INTERNAL_COMPONENT_REGESTRY[id]=nil
|
||||
aceVM.addEventRaw("componentRemoved", type)
|
||||
end
|
||||
}
|
||||
end
|
||||
|
||||
function addRuntimeFunction(func)
|
||||
INTERNAL_RUNTIME_FUNCTIONS[#INTERNAL_RUNTIME_FUNCTIONS+1] = func
|
||||
end
|
||||
|
||||
_VG.component={}
|
||||
|
||||
function _VG.component.list()
|
||||
local i = 0
|
||||
return function()
|
||||
i=i+1
|
||||
if INTERNAL_COMPONENT_REGESTRY[i] then
|
||||
return INTERNAL_COMPONENT_REGESTRY[i]["type"], INTERNAL_COMPONENT_REGESTRY[i]["object"]
|
||||
else
|
||||
return
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
function _VG.component.getFirst(type)
|
||||
local object={}
|
||||
for i,v in pairs(INTERNAL_COMPONENT_REGESTRY) do
|
||||
if INTERNAL_COMPONENT_REGESTRY[i].type == type then
|
||||
object=INTERNAL_COMPONENT_REGESTRY[i]
|
||||
break
|
||||
end
|
||||
end
|
||||
return object["object"]
|
||||
function aceVM.addRuntimeFunction(func)
|
||||
aceVM.INTERNAL_RUNTIME_FUNCTIONS[#aceVM.INTERNAL_RUNTIME_FUNCTIONS+1] = func
|
||||
end
|
||||
|
||||
for i,v in ipairs(fs.list("/AceVM/components/")) do
|
||||
@@ -106,36 +55,70 @@ for i,v in ipairs(fs.list("/AceVM/components/")) do
|
||||
load(code)()
|
||||
end
|
||||
|
||||
|
||||
local file=fs.open("/computers/0/bios.lua","r")
|
||||
local bios=file.readAll()
|
||||
file.close()
|
||||
file=nil
|
||||
local func = load(bios,"@bios",nil,_VG)
|
||||
if not func then error("BIOS ERR") end
|
||||
local BIOS_CORO = coroutine.create(func)
|
||||
debug.sethook(BIOS_CORO, function() coroutine.yield("CC:TWEAKED", "CORO_TIMEOUT") end, "l", 20)
|
||||
while true do
|
||||
local ret = {coroutine.resume(BIOS_CORO)}
|
||||
if ret[1] == false then
|
||||
os.shutdown()
|
||||
function aceVM.initComponents(id)
|
||||
for i,v in pairs(aceVM.components) do
|
||||
aceVM.components[i](id)
|
||||
end
|
||||
local timer = os.startTimer(0)
|
||||
local exit = false
|
||||
repeat
|
||||
local event = {coroutine.yield()}
|
||||
if event[1] == "timer" and event[2] == timer then
|
||||
exit=true
|
||||
elseif event[1]=="timer"or nil then
|
||||
elseif event[1]=="key" then
|
||||
addEventRaw("keyPressed", 1, event[2])
|
||||
if akeys[event[2]] then
|
||||
addEventRaw("keyTyped", 1, akeys[event[2]])
|
||||
end
|
||||
elseif event[1]=="char" then
|
||||
addEventRaw("keyTyped", 1, event[2])
|
||||
elseif event[1]=="key_up" then
|
||||
addEventRaw("keyReleased", 1, event[2])
|
||||
end
|
||||
|
||||
function aceVM.start(id, disks)
|
||||
term.clear()
|
||||
term.setCursorPos(1,1)
|
||||
aceVM.INTERNAL_COMPONENT_REGESTRY={}
|
||||
aceVM.BIOS_CORO=coroutine.create(function()end)
|
||||
aceVM.INTERNAL_EVENT_QUEUE={}
|
||||
aceVM.INTERNAL_RUNTIME_FUNCTIONS={}
|
||||
print("reset internals")
|
||||
aceVM.id=id
|
||||
aceVM.initVenv()
|
||||
print("created Venv")
|
||||
aceVM.initDisks(disks)
|
||||
print("added disks")
|
||||
aceVM.initComponents(id)
|
||||
print("added components")
|
||||
local file=fs.open("/AceVM/computers/"..tostring(id).."/bios.lua","r")
|
||||
local bios=file.readAll()
|
||||
file.close()
|
||||
file=nil
|
||||
print("loaded bios")
|
||||
local func = load(bios,"@bios",nil,aceVM._VG)
|
||||
if not func then error("BIOS ERR") end
|
||||
aceVM.BIOS_CORO = coroutine.create(func)
|
||||
print("created coroutine")
|
||||
debug.sethook(aceVM.BIOS_CORO, function() coroutine.yield("CC:TWEAKED", "CORO_TIMEOUT") end, "l", 30000000)
|
||||
print("added hook")
|
||||
print("starting VM")
|
||||
sleep(0.3)
|
||||
term.clear()
|
||||
term.setCursorPos(1,1)
|
||||
while not aceVM.exitVM do
|
||||
local ret = {coroutine.resume(aceVM.BIOS_CORO)}
|
||||
if ret[1] == false then
|
||||
break
|
||||
end
|
||||
until exit
|
||||
local timer = os.startTimer(0)
|
||||
local exit = false
|
||||
repeat
|
||||
local event = {coroutine.yield()}
|
||||
if event[1] == "timer" and event[2]==timer then
|
||||
exit=true
|
||||
elseif event[1]==nil then
|
||||
elseif event[1]=="key" then
|
||||
aceVM.addEventRaw("keyPressed", 1, event[2])
|
||||
if aceVM.aceKeys[event[2]] then
|
||||
aceVM.addEventRaw("keyTyped", 1, aceVM.aceKeys[event[2]])
|
||||
end
|
||||
elseif event[1]=="char" then
|
||||
aceVM.addEventRaw("keyTyped", 1, event[2])
|
||||
elseif event[1]=="key_up" then
|
||||
aceVM.addEventRaw("keyReleased", 1, event[2])
|
||||
end
|
||||
until exit
|
||||
for i,v in ipairs(aceVM.INTERNAL_RUNTIME_FUNCTIONS) do
|
||||
v()
|
||||
end
|
||||
end
|
||||
aceVM.exitVM = false
|
||||
term.clear()
|
||||
term.setCursorPos(1,1)
|
||||
end
|
||||
Reference in New Issue
Block a user