-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCloverOS_API.lua
More file actions
208 lines (178 loc) · 4.31 KB
/
CloverOS_API.lua
File metadata and controls
208 lines (178 loc) · 4.31 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
206
207
208
-- CloverOS API module
-- Provides basic OS metadata, installer support, and a small mirrored terminal API.
---@diagnostic disable: undefined-global
-- luacheck: globals fs shell term colors peripheral read write os
local module = {}
local fs = fs
local shell = shell
local term = term
local colors = colors
local peripheral = peripheral
local read = read
local write = write
local monitor = peripheral.find("monitor")
local function findRoot()
if fs.exists("/CloverOS_API.lua") and fs.exists("/CloverOS_OS.lua") then
return "/"
end
for i = 0, 99 do
local root = "/disk" .. (i == 0 and "" or i)
if fs.exists(root .. "/CloverOS_API.lua") and fs.exists(root .. "/CloverOS_OS.lua") then
return root
end
end
return nil
end
local function pathCombine(...)
local parts = { ... }
local path = parts[1] or ""
for i = 2, #parts do
local part = parts[i]
if string.sub(part, 1, 1) == "/" then
path = part
else
path = fs.combine(path, part)
end
end
return path
end
local ROOT = findRoot()
local function loadFilesystemModule()
if not ROOT then
return nil
end
local fsPath = pathCombine(ROOT, "etc", "filesystem", "main.lua")
if not fs.exists(fsPath) then
return nil
end
-- Load the filesystem module in a sandboxed environment that
-- exposes this CloverOS API as a global, while falling back to
-- the normal globals. This lets the filesystem code reference
-- CloverOS_API (or CloverOS) directly.
local env = {}
-- prefer _G when available, otherwise _ENV; ensure we fall back to existing globals
local globals = _G or _ENV
setmetatable(env, { __index = globals })
env.CloverOS_API = module
env.CloverOS = module
local fh = fs.open(fsPath, "r")
if not fh then
return nil
end
local content = fh.readAll()
fh.close()
local chunk, err
-- Lua 5.1 compatibility: use loadstring + setfenv if available
if setfenv then
chunk, err = loadstring(content, fsPath)
if not chunk then
return nil
end
setfenv(chunk, env)
else
-- Lua 5.2+: load accepts an environment parameter
chunk, err = load(content, fsPath, "t", env)
if not chunk then
return nil
end
end
local ok, result = pcall(chunk)
if ok and type(result) == "table" then
return result
end
return nil
end
local function writeToMirrors(text)
write(text)
if monitor then
monitor.write(text)
end
end
local function clearMirrors()
term.clear()
term.setCursorPos(1, 1)
if monitor then
monitor.clear()
monitor.setCursorPos(1, 1)
end
end
local function setCursorMirrors(x, y)
term.setCursorPos(x, y)
if monitor then
monitor.setCursorPos(x, y)
end
end
local function readMirrored(hidden)
if hidden then
return read("*")
end
return read()
end
local GDI = {}
function GDI.setColor(color)
term.setTextColor(color)
if monitor then
monitor.setTextColor(color)
end
end
function GDI.setBGColor(color)
term.setBackgroundColor(color)
if monitor then
monitor.setBackgroundColor(color)
end
end
function GDI.setCursor(x, y)
setCursorMirrors(x, y)
end
function GDI.clear(bg)
if bg then
GDI.setBGColor(bg)
end
clearMirrors()
end
function GDI.text(x, y, text, fg, bg)
if fg then
GDI.setColor(fg)
end
if bg then
GDI.setBGColor(bg)
end
GDI.setCursor(x, y)
writeToMirrors(text)
end
function GDI.rect(x, y, w, h, fg, bg)
for row = 0, h - 1 do
GDI.text(x, y + row, string.rep(" ", w), fg, bg)
end
end
function GDI.box(x, y, w, h, title, fg, bg)
GDI.rect(x, y, w, h, fg, bg)
if w > 1 and h > 1 then
GDI.text(x, y, "+" .. string.rep("-", math.max(0, w - 2)) .. "+", fg, bg)
for row = 1, h - 2 do
GDI.text(x, y + row, "|" .. string.rep(" ", math.max(0, w - 2)) .. "|", fg, bg)
end
GDI.text(x, y + h - 1, "+" .. string.rep("-", math.max(0, w - 2)) .. "+", fg, bg)
if title then
GDI.text(x + 2, y, title, colors.cyan, bg)
end
end
end
function module.version()
return "CloverOS v1.0.0"
end
function module.author()
return "CloverOS Team"
end
function module.runInstaller()
shell.run("wget", "run", "https://palordersoftworksofficial.github.io/CloverOS/netinstall.lua")
end
function module.findRoot()
return ROOT
end
function module.getMonitor()
return monitor
end
module.GDI = GDI
module.filesystem = loadFilesystemModule()
return module