-
Notifications
You must be signed in to change notification settings - Fork 0
/
vmcore.lua
311 lines (289 loc) · 7.05 KB
/
vmcore.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
--[[
cVMWare
A WIP "virtual machine"-manager in CC.
In case I didn't commit in a long time and this
is not working, you are free to modify and maybe
fix the code.
~Piorjade
]]
--[[
INFO:
This is the "core" of cVM
It has no GUI, so it's meant to be used in the commandline
with arguments.
The current idea of virtual machines has a problem:
It makes "gigantic" virtual "hard-drives".
]]
local oldErr = _G.printError
_G['printError'] = function(str)
local file = fs.open("/ttt", "a")
file.writeLine("ERROR: "..str)
file.close()
return oldErr(str)
end
--Variablen
_ver=0.1
_hdTemplate = {
["bootSector"] = {
["bios.lua"] = nil
},
["sector0"] = {
["disk"] = nil,
},
["sector1"] = {
["folders"] = {
"rom",
"rom/testfolder"
},
["files"] = {
"rom/testfolder/test"
},
["fileData"] = {
"Hello!"
}
}
}
--Funktionen
local function createHardDrive(path)
if fs.exists(path) then return false, "File already exists." end
local file, err = fs.open(path, "w")
if not file then return false, err end
file.write(textutils.serialize(_hdTemplate))
file.close()
end
local function runHardDrive(path)
hdPath = path
if fs.exists("/tmp/"..fs.getName(path)) then fs.delete("/tmp/"..fs.getName(path)) end
sleep(2)
if not fs.exists(path) then return false, "File does not exist." end
local file, err = fs.open(path, "r")
if not file then return false, err end
local inhalt = file.readAll()
inhalt = textutils.unserialize(inhalt)
file.close()
if not inhalt.bootSector["bios.lua"] then return false, "No bios.lua." end
fs.makeDir("/tmp/"..fs.getName(path))
for k, v in ipairs(inhalt.sector1.folders) do
fs.makeDir("/tmp/"..fs.getName(path).."/"..v)
end
for k, v in ipairs(inhalt.sector1.files) do
local file = fs.open("/tmp/"..fs.getName(path).."/"..v, "w")
file.write(inhalt.sector1.fileData[k])
file.close()
end
print("Loading bios.")
local bios, err = loadstring(inhalt.bootSector["bios.lua"])
if not bios then return false, err end
print(tostring(bios))
local env = {}
local allowed = {
"assert",
"collectgarbage",
"dofile",
"error",
"getfenv",
"getmetatable",
"ipairs",
"load",
"loadstring",
"module",
"next",
"pairs",
"pcall",
"print",
"rawequal",
"rawget",
"rawset",
"require",
"select",
"setfenv",
"setmetatable",
"tonumber",
"tostring",
"type",
"unpack",
"xpcall",
"bit",
"bit32",
"keys",
"coroutine",
"io",
"math",
"string",
"table",
"term",
"peripheral",
"http",
"vector",
"read",
"loadfile",
"sleep"
}
--[[for k, v in pairs(_G) do
env[k] = v
end]]
for k, v in ipairs(allowed) do
env[v] = _G[v]
end
env['loadfile'] = function(p)
local f, err = env.fs.open(p, "r")
local inhalt = f.readAll()
f.close()
local a = loadstring(inhalt)
setfenv(a, env)
return a
end
env['os'] = {}
env.os.pullEventRaw = os.pullEventRaw
env.os.pullEvent = os.pullEvent
env.os.queueEvent = os.queueEvent
env.os.startTimer = os.startTimer
env.os.time = os.time
env.os.sleep = os.sleep
env.os.setAlarm = os.setAlarm
local thisHD = inhalt
local file = fs.open("/dummyFS", "r")
local inhalt = file.readAll()
file.close()
local file = fs.open("/tmpfs", "w")
file.writeLine("rootPath='".."/tmp/"..fs.getName(path).."/'")
file.write(inhalt)
file.close()
os.loadAPI("/tmpfs")
env.fs = tmpfs
--[[local forbidden = {"colors", "colours", "disk", "gps", "help", "keys", "paintutils", "parallel", "rednet", "settings", "textutils", "window"}
for k, v in ipairs(forbidden) do
env[v] = nil
end]]
env._G = env
setfenv(bios, env)
--shell.setPath("/tmp/"..fs.getName(path).."/")
print("Starting HD")
local c1 = coroutine.create(bios)
local evt = {}
local function rb()
c1 = coroutine.create(bios)
end
local function sd()
local function nothin()
return "shutdown"
end
c1 = coroutine.create(nothin)
end
env['_vm'] = {}
env._vm.shutdown = function()
return sd()
end
env._vm.reboot = function()
return rb()
end
while true do
local ok, err = coroutine.resume(c1, unpack(evt))
if ok == false then
printError("Bios:"..err)
elseif err == "shutdown" then
break
end
evt = {os.pullEventRaw()}
if evt[1] == "key" and evt[2] == keys.delete then
break
elseif coroutine.status(c1) == "dead" then
break
end
end
local function list(path)
local root = {}
local fData = {}
for k, v in ipairs(fs.list(path)) do
table.insert(root, v)
if fs.isDir(path.."/"..v) then
local g, h = list(path.."/"..v)
for _, a in ipairs(g) do
table.insert(root, v.."/"..a)
end
for _, a in ipairs(h) do
table.insert(fData, a)
end
else
local file, err = fs.open(path.."/"..v, "r")
local inhalt = file.readAll()
file.close()
table.insert(fData, inhalt)
end
end
return root, fData
end
local roott, rootf = list("/tmp/"..fs.getName(hdPath).."/")
thisHD.sector1.folders = {}
thisHD.sector1.files = {}
thisHD.sector1.fileData = {}
for k, v in ipairs(roott) do
if fs.isDir("/tmp/"..fs.getName(hdPath).."/"..v) then
table.insert(thisHD.sector1.folders, v)
else
table.insert(thisHD.sector1.files, v)
end
end
for k, v in ipairs(rootf) do
table.insert(thisHD.sector1.fileData, v)
end
local file = fs.open(hdPath, "w")
file.write(textutils.serialize(thisHD))
file.close()
fs.delete("/tmp/"..fs.getName(hdPath))
print("Finished!")
end
local function setBios(p, bp)
if not fs.exists(p) then return false, "HD not found!" end
if not fs.exists(bp) then return false, "BIOS not found!" end
local file = fs.open(p, "r")
local inhalt = file.readAll()
inhalt = textutils.unserialize(inhalt)
file.close()
local file = fs.open(bp, "r")
inhalt.bootSector["bios.lua"] = file.readAll()
file.close()
local file = fs.open(p, "w")
file.write(textutils.serialize(inhalt))
file.close()
end
--Code
local tArgs = {...}
if #tArgs < 1 then
print("Usage:")
print(" createHD <path>")
print(" runHD <path> <pathToBios>")
print(" insertDisk <folder> <pathToHD>")
print(" insertBios <pathToHD> <pathToBios>")
elseif tArgs[1] == "info" then
print(" INFO:")
print("- Virtual Machines can have only 1 disk inserted")
print("- Other peripherals are currently NOT supported")
elseif tArgs[1] == "createHD" and #tArgs == 2 then
local ok, err = createHardDrive(tArgs[2])
if ok == false then
printError(err)
else
print("Successfully created!")
end
elseif tArgs[1] == "insertBios" and #tArgs == 3 then
local ok, err = setBios(tArgs[2], tArgs[3])
if ok == false then
printError(err)
else
print("End.")
end
elseif tArgs[1] == "runHD" and #tArgs == 2 then
local ok, err = runHardDrive(tArgs[2])
if ok == false then
printError(err)
else
print("End.")
end
else
print("Usage:")
print(" createHD <path>")
print(" runHD <path>")
print(" insertDisk <folder> <pathToHD>")
print(" insertBios <pathToHD> <pathToBios>")
end