-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstartup.lua
More file actions
205 lines (176 loc) · 5.45 KB
/
Copy pathstartup.lua
File metadata and controls
205 lines (176 loc) · 5.45 KB
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
term.clear()
term.setCursorPos(1,1)
local running = true
local function lineViewer(lines)
local _, height = term.getSize()
local currentIndex = 1
local maxIndex = #lines
local pageSize = height - 5
while true do
term.setCursorPos(1, 3)
for i = currentIndex, math.min(currentIndex + pageSize - 1, maxIndex) do
print(lines[i])
end
print("\n-- Select your OS/lua script --")
return
end
end
local function addBoot(name, loc)
if not name then
print("Error : No OS name was specified.")
print("Use : add <nom> <fichier boot>")
return
elseif not loc then
print("Error : No lua file was specified.")
print("Use : add <nom> <fichier boot>")
return
elseif not fs.exists(loc) then
print("Error : The file '" .. loc .. "' doesn't exist.")
print("Example : add FrOS FrOS/boot.lua")
return
end
if not fs.exists("boot.txt") then
local f = fs.open("boot.txt", "w")
if f then
f.write(name .. "|" .. loc .. "\n")
f.close()
print(name .. " was added to the list, reboot to apply with 'reboot'.")
else
print("Error : The file 'boot.txt' can't be created.")
end
else
local f = fs.open("boot.txt", "a")
if f then
f.write(name .. "|" .. loc .. "\n")
f.close()
print(name .. " was added to the list, reboot to apply with 'reboot'.")
else
print("Error : The boot can't be added to 'boot.txt'.")
end
end
end
local function removeBoot(name)
if not name then
print("Error : No OS/lua script was specified.")
print("Use : remove <nom>")
return
end
if fs.exists("boot.txt") then
local lignes = {}
local handle = fs.open("boot.txt", "r")
if not handle then
print("Error : Unreadable file.")
return
end
local fnd = false
while true do
local ligne = handle.readLine()
if not ligne then break end
local key, loc = ligne:match("([%w%.]+)|([^\n]+)")
if key ~= name then
table.insert(lignes, ligne)
else
fnd = true
end
end
handle.close()
handle = fs.open("boot.txt", "w")
for _, ligne in ipairs(lignes) do
handle.writeLine(ligne)
end
handle.close()
if fnd then
print(name .. " was removed to the list, reboot to apply with 'reboot'.")
else
print("Error : You can't remove " .. name .. " because it's not accessible.")
end
else
print("Error : You can't remove " .. name .. " because it's not accessible.")
end
end
local boots = fs.find("/*/boot.lua")
local install = fs.find("/*/install.lua")
local startup = fs.find("/*/startup.lua")
local allFiles
local names = {}
local files = {}
local function readBoot()
allFiles = {}
for _, f in ipairs(boots) do table.insert(allFiles, f) end
for _, f in ipairs(install) do table.insert(allFiles, f) end
for _, f in ipairs(startup) do table.insert(allFiles, f) end
for _, f in ipairs(allFiles) do
files[f] = f
end
if fs.exists("boot.txt") then
local f = fs.open("boot.txt", "r")
while true do
local ligne = f.readLine()
if not ligne then break end
local name, path = ligne:match("([^|]+)|(.+)")
if name and path then
if table_contains(allFiles, path) then
files[path] = name
else
table.insert(allFiles, path)
files[path] = name
end
end
end
f.close()
else
print("Error : The file 'boot.txt' is missing, add OS/lua script with the 'add' command.")
end
for idx, f in ipairs(allFiles) do
table.insert(names, idx .. " : " .. files[f] .. " (" .. f .. ")")
end
lineViewer(names)
end
function table_contains(tbl, x)
local found = false
local idx = 1
for _, v in pairs(tbl) do
if v == x then
found = true
break
end
idx = idx + 1
end
return found, idx
end
print("Bootloader FrOS :")
readBoot()
while running do
write("? ")
local input = read()
if input == nil or input == "" then
input = "nil"
end
local args = {}
for word in string.gmatch(input, "%S+") do
table.insert(args, word)
end
local command = args[1]
local param = args[2]
local idx = tonumber(input)
if idx and idx >= 1 and idx <= #allFiles then
local _, height = term.getSize()
term.setCursorPos(1, height)
write("Starting " .. names[idx])
if files[allFiles[idx]] == "CraftOS" then
term.clear()
term.setCursorPos(1,1)
return
end
running = false
shell.run(allFiles[idx])
elseif command == "add" then
addBoot(param, args[3])
elseif command == "remove" then
removeBoot(param)
elseif command == "reboot" then
os.reboot()
else
print("Error : Unknown OS/lua file : " .. input)
end
end