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") maxResultCountOrNil = (maxResultCountOrNil or 0)-1 local rv = {} local buf = "" for i = 1, #str do local c = string.sub(str,i,i) if #rv ~= maxResultCountOrNil and c == delim then table.insert(rv, buf) buf = "" else buf = buf..c end end table.insert(rv, buf) return rv end 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 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() aceVM.INTERNAL_COMPONENT_REGESTRY[id]=nil aceVM.addEventRaw("componentRemoved", type) end } end 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 local file = fs.open("/AceVM/components/"..v, "r") local code = file.readAll() file.close() load(code)() end function aceVM.initComponents(id) for i,v in pairs(aceVM.components) do aceVM.components[i](id) end 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 local timer = os.queueEvent("nosleep") local exit = false repeat local event = {coroutine.yield()} if event[1] == "nosleep" 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