e
This commit is contained in:
1
AceVM/components/aceVM.lua
Normal file
1
AceVM/components/aceVM.lua
Normal file
@@ -0,0 +1 @@
|
||||
local object = {}
|
||||
19
AceVM/components/bios.lua
Normal file
19
AceVM/components/bios.lua
Normal file
@@ -0,0 +1,19 @@
|
||||
local object = {}
|
||||
local file = fs.open("/computers/0/bios.lua", "r")
|
||||
local bios = file.readAll()
|
||||
file.close()
|
||||
|
||||
function object.getData()
|
||||
return bios
|
||||
end
|
||||
|
||||
function object.setData(data)
|
||||
bios=data
|
||||
end
|
||||
|
||||
function object.saveData()
|
||||
local file=fs.open("/bios.lua", "w")
|
||||
file.write(bios)
|
||||
end
|
||||
|
||||
newComponent("bios", object)
|
||||
42
AceVM/components/computer.lua
Normal file
42
AceVM/components/computer.lua
Normal file
@@ -0,0 +1,42 @@
|
||||
local object = {}
|
||||
local file = fs.open("/computers/0/nvram.dat", "r")
|
||||
local nvram = string.split(file.readAll(), "\n")
|
||||
file.close()
|
||||
|
||||
function object.getMachineEvent()
|
||||
if INTERNAL_EVENT_QUEUE[1]==nil then
|
||||
return nil
|
||||
end
|
||||
local event = INTERNAL_EVENT_QUEUE[1]
|
||||
INTERNAL_EVENT_QUEUE[1]=nil
|
||||
return table.unpack(event)
|
||||
end
|
||||
|
||||
function object.getData(addr)
|
||||
if type(addr)~="number" then
|
||||
error()
|
||||
end
|
||||
return nvram[addr]
|
||||
end
|
||||
|
||||
function object.setData(addr, data)
|
||||
if type(addr)~="number" then
|
||||
error()
|
||||
end
|
||||
if type(data)~="string" then
|
||||
error()
|
||||
end
|
||||
nvram[addr]=data
|
||||
end
|
||||
|
||||
function object.saveData()
|
||||
local file=fs.open("/nvram.dat", "w")
|
||||
file.write(table.concat(nvram,"\n"))
|
||||
end
|
||||
|
||||
object.reboot=os.reboot
|
||||
object.shutdown=os.shutdown
|
||||
|
||||
function object.beep() end -- IDK how i should implement this
|
||||
|
||||
newComponent("computer", object)
|
||||
137
AceVM/components/disks.lua
Normal file
137
AceVM/components/disks.lua
Normal file
@@ -0,0 +1,137 @@
|
||||
local fss = {}
|
||||
local object = {}
|
||||
local uObject = {}
|
||||
|
||||
local function getFs(self)
|
||||
return fss[tostring(self.__UDATA_id)]
|
||||
end
|
||||
|
||||
local function normalize(path)
|
||||
if path:sub(1,1)=="/" then
|
||||
path=path:sub(2)
|
||||
end
|
||||
return path
|
||||
end
|
||||
|
||||
function object:open(path)
|
||||
path=normalize(path)
|
||||
local f = getFs(self)
|
||||
return {
|
||||
read=function()
|
||||
local file = fs.open(f..path, "r")
|
||||
local text = file.readAll()
|
||||
file.close()
|
||||
return text
|
||||
end,
|
||||
write=function(text)
|
||||
local file = fs.open(f..path, "w")
|
||||
file.write(text)
|
||||
file.close()
|
||||
end,
|
||||
append=function(text)
|
||||
local file = fs.open(f..path, "a")
|
||||
file.write(text)
|
||||
file.close()
|
||||
end
|
||||
}
|
||||
end
|
||||
|
||||
function object:list(path)
|
||||
path=normalize(path)
|
||||
local f = getFs(self)
|
||||
return fs.list(f..path)
|
||||
end
|
||||
|
||||
function object:fileExists(path)
|
||||
path=normalize(path)
|
||||
local f = getFs(self)
|
||||
return fs.exists(f..path) and not fs.isDir(f..path)
|
||||
end
|
||||
|
||||
function object:directoryExists(path)
|
||||
path=normalize(path)
|
||||
local f = getFs(self)
|
||||
return fs.exists(f..path) and fs.isDir(f..path)
|
||||
end
|
||||
|
||||
function object:makeDirectory(path)
|
||||
path=normalize(path)
|
||||
local f = getFs(self)
|
||||
fs.makeDir(f..path)
|
||||
end
|
||||
|
||||
function object:delete(path)
|
||||
path=normalize(path)
|
||||
local f = getFs(self)
|
||||
fs.delete(f..path)
|
||||
end
|
||||
|
||||
function object:getSize(path)
|
||||
path=normalize(path)
|
||||
local f = getFs(self)
|
||||
return fs.getSize(f..path)
|
||||
end
|
||||
|
||||
function uObject:readBytes(start, length)
|
||||
local f = getFs(self)
|
||||
local file=fs.open(f, "r")
|
||||
local data=fs.readAll()
|
||||
file.close()
|
||||
return data:sub(start, start+length)
|
||||
end
|
||||
|
||||
function uObject:writeBytes(start, dat)
|
||||
local f = getFs(self)
|
||||
local file=fs.open(f, "r")
|
||||
local data=fs.readAll()
|
||||
file.close()
|
||||
return data:sub(1, start)..dat..data:sub(start+#dat, #data)
|
||||
end
|
||||
|
||||
function uObject:getSize()
|
||||
local f = getFs(self)
|
||||
local file=fs.open(f, "r")
|
||||
local data=fs.readAll()
|
||||
file.close()
|
||||
return #data
|
||||
end
|
||||
|
||||
local function findDisks(tble)
|
||||
for i,v in ipairs(tble) do
|
||||
if fs.exists("/disks/"..tostring(v).."h") then
|
||||
local obj = deepcopy(object)
|
||||
obj.__UDATA_id=v
|
||||
obj.id="disk_"..tostring(v)
|
||||
obj.type="hdd"
|
||||
newComponent("disk", obj)
|
||||
fss[tostring(v)]="/disks/"..tostring(v).."h/"
|
||||
elseif fs.exists("/disks/"..tostring(v).."f") then
|
||||
local obj = deepcopy(object)
|
||||
obj.__UDATA_id=v
|
||||
obj.id="disk_"..tostring(v)
|
||||
obj.type="fdd"
|
||||
newComponent("disk", obj)
|
||||
fss[tostring(v)]="/disks/"..tostring(v).."f/"
|
||||
elseif fs.exists("/disks/"..tostring(v).."u") then
|
||||
local obj = deepcopy(uObject)
|
||||
obj.__UDATA_id=v
|
||||
obj.id="disk_"..tostring(v)
|
||||
obj.type="udd"
|
||||
newComponent("disk", obj)
|
||||
fss[tostring(v)]="/disks/"..tostring(v).."u"
|
||||
else
|
||||
fs.makeDir("/disks/"..tostring(v).."f/")
|
||||
local obj = deepcopy(object)
|
||||
obj.__UDATA_id=v
|
||||
obj.id="disk_"..tostring(v)
|
||||
obj.type="fdd"
|
||||
newComponent("disk", obj)
|
||||
fss[tostring(v)]="/disks/"..tostring(v).."f/"
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
local file = fs.open("/computers/0/computer.ltable", "r")
|
||||
local config = load("return "..file.readAll())()
|
||||
file.close()
|
||||
findDisks(config.disks)
|
||||
28
AceVM/components/internet.lua
Normal file
28
AceVM/components/internet.lua
Normal file
@@ -0,0 +1,28 @@
|
||||
local internet = {}
|
||||
|
||||
local function wrap_request(_url, ...)
|
||||
local ok, err = http.request(...)
|
||||
if ok then
|
||||
while true do
|
||||
local event, param1, param2, param3 = coroutine.yield()
|
||||
if event == "http_success" and param1 == _url then
|
||||
return param2
|
||||
elseif event == "http_failure" and param1 == _url then
|
||||
return nil, param2, param3
|
||||
end
|
||||
end
|
||||
end
|
||||
return nil, err
|
||||
end
|
||||
|
||||
local function get(_url)
|
||||
if type(_url) ~= "string" then
|
||||
error("URL must be a string")
|
||||
end
|
||||
|
||||
return wrap_request(_url, _url)
|
||||
end
|
||||
|
||||
function internet.get(url) return get(url).readAll() end
|
||||
|
||||
newComponent("internet", internet)
|
||||
88
AceVM/components/screen.lua
Normal file
88
AceVM/components/screen.lua
Normal file
@@ -0,0 +1,88 @@
|
||||
local screen = {}
|
||||
|
||||
local function write(sText)
|
||||
local w, h = term.getSize()
|
||||
local x, y = term.getCursorPos()
|
||||
|
||||
local nLinesPrinted = 0
|
||||
local function newLine()
|
||||
if y + 1 <= h then
|
||||
term.setCursorPos(1, y + 1)
|
||||
else
|
||||
term.setCursorPos(1, h)
|
||||
term.scroll(1)
|
||||
end
|
||||
x, y = term.getCursorPos()
|
||||
nLinesPrinted = nLinesPrinted + 1
|
||||
end
|
||||
|
||||
sText = tostring(sText)
|
||||
while #sText > 0 do
|
||||
local whitespace = string.match(sText, "^[ \t]+")
|
||||
if whitespace then
|
||||
term.write(whitespace)
|
||||
x, y = term.getCursorPos()
|
||||
sText = string.sub(sText, #whitespace + 1)
|
||||
end
|
||||
|
||||
local newline = string.match(sText, "^\n")
|
||||
if newline then
|
||||
newLine()
|
||||
sText = string.sub(sText, 2)
|
||||
end
|
||||
|
||||
local text = string.match(sText, "^[^ \t\n]+")
|
||||
if text then
|
||||
sText = string.sub(sText, #text + 1)
|
||||
if #text > w then
|
||||
while #text > 0 do
|
||||
if x > w then
|
||||
newLine()
|
||||
end
|
||||
term.write(text)
|
||||
text = string.sub(text, w - x + 2)
|
||||
x, y = term.getCursorPos()
|
||||
end
|
||||
else
|
||||
if x + #text - 1 > w then
|
||||
newLine()
|
||||
end
|
||||
term.write(text)
|
||||
x, y = term.getCursorPos()
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
return nLinesPrinted
|
||||
end
|
||||
|
||||
function screen.print(...)
|
||||
local nLinesPrinted = 0
|
||||
local nLimit = select("#", ...)
|
||||
for n = 1, nLimit do
|
||||
local s = tostring(select(n, ...))
|
||||
if n < nLimit then
|
||||
s = s .. "\t"
|
||||
end
|
||||
nLinesPrinted = nLinesPrinted + write(s)
|
||||
end
|
||||
nLinesPrinted = nLinesPrinted + write("\n")
|
||||
end
|
||||
|
||||
function screen.printInline(...)
|
||||
local nLinesPrinted = 0
|
||||
local nLimit = select("#", ...)
|
||||
for n = 1, nLimit do
|
||||
local s = tostring(select(n, ...))
|
||||
nLinesPrinted = nLinesPrinted + write(s)
|
||||
end
|
||||
end
|
||||
|
||||
function screen.clear()
|
||||
term.clear()
|
||||
end
|
||||
|
||||
screen.id = "screen_1"
|
||||
screen.__UDATA_ID = 1
|
||||
|
||||
newComponent("screen", screen)
|
||||
102
AceVM/init
Normal file
102
AceVM/init
Normal file
@@ -0,0 +1,102 @@
|
||||
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
|
||||
|
||||
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 = tostring(#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
|
||||
return INTERNAL_COMPONENT_REGESTRY[tostring(i)].type, INTERNAL_COMPONENT_REGESTRY[tostring(i)].object
|
||||
end
|
||||
end
|
||||
|
||||
function _VG.component.getFirst(type)
|
||||
local object={}
|
||||
for i,v in pairs(INTERNAL_COMPONENT_REGESTRY) do
|
||||
if INTERNAL_COMPONENT_REGESTRY[tostring(i)].type == type then
|
||||
object=INTERNAL_COMPONENT_REGESTRY[tostring(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
|
||||
load(bios,"@bios",nil,_VG)()
|
||||
Reference in New Issue
Block a user