This repository has been archived by the owner on Nov 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
crfos.lua
281 lines (250 loc) · 7.03 KB
/
crfos.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
print("CrfOS v1.7.4")
local nativesetfenv = setfenv
local nativeprint = print
local nativegetfenv = getfenv
-- Prevent access to metatables or environments of strings, as these are global between all computers
do
local nativegetmetatable = getmetatable
local nativeerror = error
local nativetype = type
local string_metatable = nativegetmetatable("")
function getmetatable( t )
local mt = nativegetmetatable( t )
if mt == string_metatable then
nativeerror( "Attempt to access string metatable", 2 )
else
return mt
end
end
end
-- Install lua parts of the os api
function os.version()
return "CraftOS 1.7"
end
function os.pullEventRaw( sFilter )
return coroutine.yield( sFilter )
end
function os.pullEvent( sFilter )
local eventData = { os.pullEventRaw( sFilter ) }
if eventData[1] == "terminate" then
error( "Terminated", 0 )
end
return table.unpack( eventData )
end
-- Install globals
function sleep( nTime )
local timer = os.startTimer( nTime or 0 )
repeat
local sEvent, param = os.pullEvent( "timer" )
until param == timer
end
function write( sText )
njs.write(sText)
end
function print( ... )
nativeprint(...)
end
function printError( ... )
print( ... )
end
function read( _sReplaceChar, _tHistory, _fnComplete )
sLine = njs.read()
return sLine
end
loadfile = function( _sFile, _tEnv )
local file = fs.open( _sFile, "r" )
if file then
local func, err = load( file.readAll(), fs.getName( _sFile ), "t", _tEnv )
file.close()
return func, err
end
return nil, "File not found"
end
-- Install the rest of the OS api
function os.run( _tEnv, _sPath, ... )
local tArgs = { ... }
local tEnv = _tEnv
setmetatable( tEnv, { __index = _G } )
local fnFile, err = loadfile( _sPath, tEnv )
if fnFile then
local ok, err = pcall( function()
fnFile( table.unpack( tArgs ) )
end )
if not ok then
if err and err ~= "" then
printError( err )
end
return false
end
return true
end
if err and err ~= "" then
printError( err )
end
return false
end
local tAPIsLoading = {}
function os.loadAPI( _sPath )
local sName = fs.getName( _sPath )
print('load api '..sName)
if tAPIsLoading[sName] == true then
printError( "API "..sName.." is already being loaded" )
return false
end
tAPIsLoading[sName] = true
local tEnv = {}
setmetatable( tEnv, { __index = _G } )
local fnAPI, err = loadfile( _sPath, tEnv )
if fnAPI then
local ok, err = pcall( fnAPI )
if not ok then
printError( err )
tAPIsLoading[sName] = nil
return false
end
else
printError( err )
tAPIsLoading[sName] = nil
return false
end
local tAPI = {}
for k,v in pairs( tEnv ) do
if k ~= "_ENV" then
tAPI[k] = v
end
end
_G[sName] = tAPI
tAPIsLoading[sName] = nil
return true
end
function os.unloadAPI( _sName )
if _sName ~= "_G" and type(_G[_sName]) == "table" then
_G[_sName] = nil
end
end
function os.sleep( nTime )
sleep( nTime )
end
-- Install the lua part of the HTTP api (if enabled)
if http then
local nativeHTTPRequest = http.request
local function wrapRequest( _url, _post, _headers )
local ok, err = nativeHTTPRequest( _url, _post, _headers )
if ok then
while true do
local event, param1, param2 = os.pullEvent()
if event == "http_success" and param1 == _url then
return param2
elseif event == "http_failure" and param1 == _url then
return nil, param2
end
end
end
return nil, err
end
http.get = function( _url, _headers )
return wrapRequest( _url, nil, _headers )
end
http.post = function( _url, _post, _headers )
return wrapRequest( _url, _post or "", _headers )
end
http.request = function( _url, _post, _headers )
local ok, err = nativeHTTPRequest( _url, _post, _headers )
if not ok then
os.queueEvent( "http_failure", _url, err )
end
return ok, err
end
end
-- Install the lua part of the FS api
local tEmpty = {}
function fs.complete( sPath, sLocation, bIncludeFiles, bIncludeDirs )
bIncludeFiles = (bIncludeFiles ~= false)
bIncludeDirs = (bIncludeDirs ~= false)
local sDir = sLocation
local nStart = 1
local nSlash = string.find( sPath, "[/\\]", nStart )
if nSlash == 1 then
sDir = ""
nStart = 2
end
local sName
while not sName do
local nSlash = string.find( sPath, "[/\\]", nStart )
if nSlash then
local sPart = string.sub( sPath, nStart, nSlash - 1 )
sDir = fs.combine( sDir, sPart )
nStart = nSlash + 1
else
sName = string.sub( sPath, nStart )
end
end
if fs.isDir( sDir ) then
local tResults = {}
if bIncludeDirs and sPath == "" then
table.insert( tResults, "." )
end
if sDir ~= "" then
if sPath == "" then
table.insert( tResults, (bIncludeDirs and "..") or "../" )
elseif sPath == "." then
table.insert( tResults, (bIncludeDirs and ".") or "./" )
end
end
local tFiles = fs.list( sDir )
for n=1,#tFiles do
local sFile = tFiles[n]
if #sFile >= #sName and string.sub( sFile, 1, #sName ) == sName then
local bIsDir = fs.isDir( fs.combine( sDir, sFile ) )
local sResult = string.sub( sFile, #sName + 1 )
if bIsDir then
table.insert( tResults, sResult .. "/" )
if bIncludeDirs and #sResult > 0 then
table.insert( tResults, sResult )
end
else
if bIncludeFiles and #sResult > 0 then
table.insert( tResults, sResult )
end
end
end
end
return tResults
end
return tEmpty
end
-- Load APIs
local bAPIError = false
local tApis = fs.list("rom/apis")
for n,sFile in ipairs( tApis ) do
if string.sub( sFile, 1, 1 ) ~= "." then
local sPath = fs.combine( "rom/apis", sFile )
if not fs.isDir( sPath ) then
if not os.loadAPI( sPath ) then
bAPIError = true
end
end
end
end
if bAPIError then
error('An API failed to load')
end
-- Run the shell
local ok, err = pcall( function()
parallel.waitForAny(
function()
print("done")
print()
njs.docode()
end,
function()
rednet.run()
end )
end )
-- If the shell errored, let the user read it.
term.redirect( term.native() )
if not ok then
print(err)
end
-- End
os.shutdown()