Repository navigation
Expand file tree
/
Copy pathstartup.lua
More file actions
139 lines (120 loc) · 3.82 KB
/
Copy pathstartup.lua
File metadata and controls
139 lines (120 loc) · 3.82 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
term.clear()
term.setCursorPos(1, 1)
term.setCursorBlink(false)
local G
local ok, err = pcall(function()
G = require("SystemCatalyst.globals")
end)
if not ok then
print("Fallingback to default globals due to error: " .. tostring(err))
sleep(1)
fs.makeDir("SystemCatalyst")
local f = fs.open("SystemCatalyst/globals.lua", "w")
f.write(
"return { DevMode = false, Version = \"not loaded yet...\", OsDirectory = \"SystemCatalyst\", Monitors = { peripheral.find(\"monitor\") }, fn = {} }")
f.close()
G = require("SystemCatalyst.globals")
print("Default globals loaded.")
end
G.OsDirectory = "SystemCatalyst"
G.Monitors = { peripheral.find("monitor") }
G.DevMode = false -- Set to true to enable developer mode features
G.fn = {}
function G.fn.saveTable(tbl, path)
-- sanitize: copy everything except the "fn" subtable and any functions
local function sanitize(t)
local o = {}
for k, v in pairs(t) do
if k ~= "fn" then
if type(v) == "table" then
o[k] = sanitize(v)
elseif type(v) ~= "function" then
o[k] = v
end
end
end
return o
end
local data = sanitize(tbl)
local f = fs.open(path, "w")
f.write("return " .. textutils.serialize(data))
f.close()
end
function G.fn.onShutdown()
G.fn.saveTable(G, "SystemCatalyst/globals.lua")
end
if G.DevMode then
print("Starting AlchemyOS Dev Environment...")
end
G.Version = "not loaded yet..."
os.pullEvent = os.pullEventRaw
local hangingToTop = false
function G.hang(code, optionalErrText)
if log then
log("Dousing Alchemy: Program hanging at code " .. tostring(code) .. " - " .. tostring(optionalErrText))
else
print("Dousing Alchemy: Program hanging at code " .. tostring(code) .. " - " .. tostring(optionalErrText))
end
if not optionalErrText then
optionalErrText = "generic"
end
local s = "douse"
local i = 1
local formattedCode = string.format("%03d", code)
if hangingToTop then
print("SubError of: ")
error("DousingUp")
end
local douseTime = os.clock() + 3 -- + seconds to douse
while douseTime < os.clock() or not G.ShouldReboot do
local _, c = os.pullEvent("char")
if c == s:sub(i, i) then
i = i + 1
elseif c == "d" then
i = 2
else
i = 1
end
if i > #s then
if G.DevMode and not hangingToTop then
print("Version: " .. tostring(Version))
hangingToTop = true
error("Dousing Alchemy: " .. tostring(optionalErrText))
elseif not G.DevMode then
term.clear()
term.setCursorPos(1, 1)
G.fn.log("Rebooting...")
G.ShouldReboot = true
G.fn.onShutdown()
os.reboot("Dousing Alchemy.. ")
end
end
end
G.fn.log("Rebooting...")
G.ShouldReboot = false
G.fn.onShutdown()
os.reboot()
end
-- Load and run the main initialization script in our own globals
local chunk, loadErr = loadfile(G.OsDirectory .. "/beginTheAlchemy.lua")
if not chunk then
G.hang(1, "Startup Error loading beginTheAlchemy.lua: " .. tostring(loadErr))
end
-- remove any file from the root that is present in the OsDirectory
local function purgeFalseCatalystFiles()
local files = fs.list("/")
for _, file in ipairs(files) do
if fs.exists(fs.combine(G.OsDirectory, file)) then
fs.delete(file)
if G.DevMode then print("Warning Purged false catalyst file: " .. file) end
end
end
end
purgeFalseCatalystFiles()
local success, runErr = pcall(function()
require(G.OsDirectory .. ".beginTheAlchemy")
end)
if not success then
local msg = (type(runErr) == "string") and runErr or tostring(runErr)
G.hang(2, "Runtime Error: " .. msg)
end