-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathinit.lua
More file actions
157 lines (156 loc) · 4.14 KB
/
Copy pathinit.lua
File metadata and controls
157 lines (156 loc) · 4.14 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
-- magiczockerOS - Copyright by Julian Kriete 2016-2021
-- My ComputerCraft-Forum account:
-- http://www.computercraft.info/forums2/index.php?showuser=57180
if component then
os.queueEvent = computer.pushSignal
os.reboot = function() computer.shutdown(true) end
os.shutdown = function() computer.shutdown(false) end
-- https://oc.cil.li/topic/1793-how-to-mount-filesystem-component-without-openos/?do=findComment&comment=8263
for k,v in next,component.list('filesystem',true) do
fs=component.proxy(k)
if fs.slot>0 then
break
end
end
local function get_file_system(file)
for a in component.list('filesystem') do
if component.invoke(a, 'exists', file or '/init.lua') then
return a
end
end
return computer.getBootAddress() or nil
end
local function prepare_path(path)
path = (path .. '/'):gsub('^/+', ''):gsub('/+', '/')
local path_part = path:find('/')
local drive = path_part and path:sub(1, path_part - 1) or nil
if drive and drive:find('-') then
path = path:sub(#drive + 1)
path = path:gsub('^/+', '')
else
drive = nil
end
return drive, '/' .. path
end
local function get_file_content(dr,da)
local to_return = ''
while true do
local tmp = component.invoke(dr, 'read', da, 1024)
if tmp then
to_return = to_return .. tmp
else
break
end
end
return to_return
end
fs = {
exists = function(path)
local drive, path = prepare_path(path)
drive = drive or get_file_system('/init.lua')
return component.invoke(drive, 'exists', path)
end,
isReadOnly = function(path)
local drive, path = prepare_path(path)
drive = drive or get_file_system('/init.lua')
return component.invoke(drive, 'isReadOnly', path)
end,
isDir = function(path)
local drive, path = prepare_path(path)
drive = drive or get_file_system('/init.lua')
if path == '/' then
return true
end
return component.invoke(drive, 'isDirectory', path)
end,
list = function(path)
local drive, path2 = prepare_path(path)
drive = drive or get_file_system('/init.lua')
return component.invoke(drive, 'list', path2 or '/')
end,
makeDir = function(path)
local drive, path = prepare_path(path)
drive = drive or get_file_system('/init.lua')
if drive then
component.invoke(drive, 'makeDirectory', path)
end
end or nil,
open = function(path, mode)
local drive, path = prepare_path(path)
drive = drive or get_file_system('/init.lua')
local file = ({component.invoke(drive, 'open', path, mode)})[1]
if not file then
return false
end
local is_opened = true
local content = mode == 'r' and get_file_content(drive,file) or nil
local content_ = content
return {
close = function()
if is_opened then
is_opened = false
component.invoke(drive, 'close', file)
end
end,
readAll = mode == 'r' and function()
if is_opened then
return content
end
end or nil,
readLine = mode == 'r' and function()
if is_opened and content_ then
local text = ({content_:find('\n')})[1]
if text then
local __ = content_:sub(1, text - 1)
content_ = content_:sub(text + 1)
return __
else
local __ = content_
content_ = nil
return __
end
end
end or nil,
write = mode == 'w' and function(content)
if is_opened then
component.invoke(drive, 'write', file, (content or ''))
end
end or nil,
writeLine = mode == 'w' and function(content)
if is_opened then
component.invoke(drive, 'write', file, (content or '') .. '\n')
end
end or nil,
}
end,
}
end
local file = fs.open('/magiczockerOS/core.lua', 'r')
local env = setmetatable( {
dumps = {}
}, {
__index = _G
} )
env._G = env
if not file then
error('/magiczockerOS/core.lua: File not exists', 0)
end
local content, err = (loadstring or load)(file.readAll(), '@/magiczockerOS/core.lua', 't', env)
file.close()
if content then
env._ENV = env
if setfenv then
setfenv(content, env)
end
else
if err and err ~= '' then
error(err, 0)
end
return
end
local ok, err = xpcall(function() return content() end, function(err) return err end)
if not ok then
if err and err ~= '' then
error(err, 0)
end
end