-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathos.lua
More file actions
182 lines (159 loc) · 6.72 KB
/
os.lua
File metadata and controls
182 lines (159 loc) · 6.72 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
local expect = require "cc.expect"
local syscall = require "_syscall"
local handles = require "_handles"
local oldos = os
local os = {date = oldos.date}
function os.loadAPI(path)
expect(1, path, "string")
local env = setmetatable({}, {__index = _G})
local fn = loadfile(path, nil, env)
if not fn then return false end
if not pcall(fn) then return false end
setmetatable(env, nil)
_G[path:match("([^/]+)%.lua$") or path:match("[^/]+")] = env
return true
end
function os.unloadAPI(name)
_G[name] = nil
end
function os.pullEvent(filter)
local ev = table.pack(os.pullEventRaw(filter))
if ev[1] == "terminate" then error("Terminated", 2) end
return table.unpack(ev, 1, ev.n)
end
function os._convertEvent(event, param)
local ev
if event == "alarm" or event == "timer" then ev = {event, param.id, n = 2}
elseif event == "char" then ev = {"char", param.character, n = 2}
elseif event == "disk" or event == "disk_eject" or event == "speaker_audio_empty" or event == "monitor_resize" or event == "device_added" or event == "device_removed" then ev = {event, param.device, n = 2}
elseif event == "key" then ev = {"key", param.keycode, param.isRepeat, n = 3}
elseif event == "key_up" then ev = {"key_up", param.keycode, n = 2}
elseif event == "modem_message" then ev = {"modem_message", param.device, param.channel, param.replyChannel, param.message, param.distance, n = 6}
elseif event == "mouse_click" or event == "mouse_drag" or event == "mouse_up" or event == "mouse_move" then ev = {event, param.button, param.x, param.y, n = 4}
elseif event == "mouse_scroll" then ev = {"mouse_scroll", param.direction, param.x, param.y, n = 4}
elseif event == "paste" then ev = {"paste", param.text, n = 2}
elseif event == "redstone" or event == "term_resize" or event == "turtle_inventory" or event == "terminate" then ev = {event, n = 1}
elseif event == "craftos_event" then ev = param
elseif event == "handle_status_change" then
if handles.http[param.id] then
if param.status == "open" then
if math.floor(handles.http[param.id].handle:responseCode() / 100) ~= 2 then
ev = {"http_failure", handles.http[param.id].url, handles.http[param.id].handle.error, handles.http[param.id].craftos, n = 4}
else
ev = {"http_success", handles.http[param.id].url, handles.http[param.id].craftos, n = 3}
end
handles.http[param.id] = nil
elseif param.status == "error" then
ev = {"http_failure", handles.http[param.id].url, handles.http[param.id].handle.error, handles.http[param.id].craftos, n = 4}
handles.http[param.id] = nil
end
elseif handles.websocket[param.id] then
if param.status == "open" then ev = {"websocket_success", handles.websocket[param.id].url, handles.websocket[param.id].craftos, n = 3}
elseif param.status == "error" then ev = {"websocket_failure", handles.websocket[param.id].url, handles.websocket[param.id].error, n = 3}
elseif param.status == "closed" then
ev = {"websocket_closed", handles.websocket[param.id].url, n = 2}
handles.websocket[param.id] = nil
end
end
elseif event == "handle_data_ready" then
if handles.websocket[param.id] then ev = {"websocket_message", handles.websocket[param.id].url, handles.websocket[param.id].handle:read("*a"), true, n = 4}
elseif handles.rednet[param.id] then ev = {"rednet_message", handles.rednet[param.id].id, handles.rednet[param.id].handle:read("*a"), n = 3} end
else ev = {"phoenix_event", event, param, n = 3} end
return ev
end
function os.pullEventRaw(filter)
local ev
repeat
ev = table.pack(coroutine.yield())
if ev.n == 2 and type(ev[1]) == "string" and type(ev[2]) == "table" then
local event, param = ev[1], ev[2]
ev = os._convertEvent(event, param)
end
until filter == nil or ev[1] == filter or ev[1] == "terminate"
return table.unpack(ev, 1, ev.n)
end
function os.sleep(time)
expect(1, time, "number")
local tm = syscall.timer(time)
repeat local ev, param = os.pullEvent()
until ev == "timer" and param == tm
end
function os.version()
return "Phoenix " .. syscall.version()
end
function os.run(env, path, ...)
expect(1, env, "table")
expect(2, path, "string")
setmetatable(env, {__index = _ENV})
local fn, err = loadfile(path, nil, env)
-- printError?
if not fn then return false end
local ok, res = pcall(fn, ...)
return ok
end
function os.queueEvent(name, ...)
expect(1, name, "string")
syscall.queueEvent("craftos_event", table.pack(name, ...))
end
function os.startTimer(time)
expect(1, time, "number")
return syscall.timer(time)
end
function os.cancelTimer(timer)
expect(1, timer, "number")
return syscall.cancel(timer)
end
os.cancelAlarm = os.cancelTimer
function os.setAlarm(time)
expect(1, time, "number")
return syscall.alarm(time)
end
function os.shutdown()
syscall.devcall("/", "shutdown")
end
function os.reboot()
syscall.devcall("/", "reboot")
end
function os.getComputerID()
return syscall.devinfo("/").metadata.id
end
os.computerID = os.getComputerID
function os.getComputerLabel()
return syscall.devcall("/", "getLabel")
end
os.computerLabel = os.getComputerLabel
function os.setComputerLabel(label)
expect(1, label, "string")
return syscall.devcall("/", "setLabel", label)
end
function os.clock()
return syscall.uptime()
end
function os.time(locale)
expect(1, locale, "string", "table", "nil")
if type(locale) == "table" then return oldos.time(locale) end
local d
if locale == "utc" then d = os.date("!*t")
elseif locale == "local" then d = os.date("*t")
elseif locale == "ingame" or d == nil then d = os.date("?*t")
else error("Unsupported operation", 2) end
return d.hour + d.min / 60 + d.sec / 3600
end
function os.day(locale)
expect(1, locale, "string", "nil")
local d
if locale == "utc" then d = os.date("!*t")
elseif locale == "local" then d = os.date("*t")
elseif locale == "ingame" or d == nil then d = os.date("?*t")
else error("Unsupported operation", 2) end
return math.floor((d.year - 1970) * 365.24) + d.yday
end
function os.epoch(locale)
expect(1, locale, "string", "nil")
if locale == "utc" then return oldos.time() * 1000
elseif locale == "local" then return (oldos.time() + (oldos.time(os.date("*t")) - oldos.time(os.date("!*t")))) * 1000
elseif locale == "ingame" then return oldos.time "ingame" * 1000
elseif locale == "nano" then return oldos.time "nano"
else error("Unsupported operation", 2) end
end
return os