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
_G.deepcopy=deepcopy
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

function _VG.sleep(nTime)
    local timer = os.startTimer(nTime or 0)
    repeat
        local _, param = coroutine.yield("timer")
    until param == timer
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)
    return {
        remove=function()
            INTERNAL_COMPONENT_REGESTRY[id]=nil
            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"]
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

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()
    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, keys[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
    until exit
end