-
Notifications
You must be signed in to change notification settings - Fork 1
/
c3d.lua
162 lines (135 loc) · 5.6 KB
/
c3d.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
local args = {...}
local terminal = window.create(term.current(),1,1,term.getSize())
local ok,C3DData = pcall(require,"libC3D")
if not ok then error("C3D could not be loaded \n"..C3DData,0) end
local init_win,ox,oy = C3DData.util.window.get_parent_info(terminal)
local advice_api = "https://api.adviceslip.com/advice"
local function error_screen(err,is_C3D,trace_func)
local trace = ""
if trace_func then trace = debug.traceback(trace_func) end
local tid = os.startTimer(0.1)
local last = {init_win.getSize()}
while true do
local ev = table.pack(os.pullEvent())
if ev[1] == "timer" and tid == ev[2] then
terminal.setVisible(false)
tid = os.startTimer(0.1)
local w,h = init_win.getSize()
if last[1] ~= w or last[2] ~= h then
terminal.reposition(1,1,w,h)
end
terminal.setBackgroundColor(colors.blue)
terminal.clear()
terminal.setCursorPos(3,3)
terminal.setBackgroundColor(colors.red)
terminal.write(C3DData.util.string.ensure_size("C3D Error",w-4))
terminal.setBackgroundColor(colors.blue)
if is_C3D then terminal.setBackgroundColor(colors.red) end
terminal.setCursorPos(3,5)
local err_line_taking = C3DData.util.draw.respect_newlines(terminal,
C3DData.util.string.wrap(err,w-4)
)
terminal.setBackgroundColor(colors.gray)
terminal.setCursorPos(3,6+err_line_taking)
C3DData.util.draw.respect_newlines(terminal,
C3DData.util.string.ensure_line_size(
C3DData.util.string.wrap_lines(
C3DData.util.parse.stack_trace(trace),
w-4
),
w-4)
)
terminal.setBackgroundColor(colors.blue)
terminal.setCursorPos(3,h-1)
terminal.write("Press \"C\" to cry.")
terminal.setVisible(true)
elseif ev[1] == "key" and ev[2] == keys.c then
init_win.setBackgroundColor(colors.black)
init_win.clear()
init_win.setCursorPos(1,1)
break
end
end
end
local function no_scene_screen()
local web = http.get(advice_api)
local advice = "Try out GuiH !"
if web then
advice = textutils.unserializeJSON(web.readAll()).slip.advice
end
local tid = os.startTimer(0.1)
local last = {init_win.getSize()}
while true do
local ev = table.pack(os.pullEvent())
if ev[1] == "timer" and tid == ev[2] then
terminal.setVisible(false)
terminal.setBackgroundColor(colors.blue)
terminal.clear()
tid = os.startTimer(0.1)
local w,h = init_win.getSize()
if last[1] ~= w or last[2] ~= h then
terminal.reposition(1,1,w,h)
end
terminal.setBackgroundColor(colors.red)
terminal.setCursorPos(3,3)
local lines = C3DData.util.draw.respect_newlines(terminal,
C3DData.util.string.ensure_line_size(
C3DData.util.string.wrap("No scene.",w-4),
w-4)
)
terminal.setBackgroundColor(colors.black)
terminal.setCursorPos(3,4+lines)
C3DData.util.draw.respect_newlines(terminal,
C3DData.util.string.ensure_line_size(
C3DData.util.string.wrap("\""..advice.."\"",w-4),
w-4)
)
terminal.setBackgroundColor(colors.blue)
terminal.setCursorPos(3,h-1)
C3DData.util.draw.respect_newlines(terminal,
C3DData.util.string.wrap("Press enter to exit",w-4)
)
terminal.setVisible(true)
elseif ev[1] == "key" and ev[2] == keys.enter then
init_win.setBackgroundColor(colors.black)
init_win.clear()
init_win.setCursorPos(1,1)
break
end
end
end
if not C3DData.init_ok then error_screen("Internal C3D error: " .. tostring(C3DData.env),true) end
local errored = true
local function run_f(f)
local ok,err,trace_function = C3DData.env({f},"/main.lua",terminal,init_win,ox,oy)
if not ok then error_screen("Runtime error: " .. tostring(err),false,trace_function) end
end
if not args[2] then
local ok,err = pcall(function()
if not next(args) then
no_scene_screen()
errored = true
elseif not fs.exists(args[1]) or not fs.isDir(args[1]) then
error_screen("Loading error: folder does not exist")
errored = true
elseif fs.exists(args[1]) and not fs.isDir(args[1]) then
error_screen("Loading error: must be ran on a folder")
errored = true
elseif fs.exists(args[1]) and fs.isDir(args[1]) then
local full_path = fs.combine(args[1],"main.lua")
if fs.exists(full_path) then
local ok,err,trace_function = C3DData.env({loadfile(full_path)},full_path,terminal,init_win,ox,oy)
if not ok then error_screen("Runtime error: " .. tostring(err),false,trace_function) end
else
error_screen("Loading error: No code to run\nmake sure you have a main.lua file on the top level of the folder")
end
else errored = false end
end)
if not ok and not errored then
error_screen("Runtime error: " .. err)
elseif not ok then
init_win.setBackgroundColor(colors.black)
init_win.clear()
init_win.setCursorPos(1,1)
end
else return {run=run_f} end