-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinit.lua
212 lines (182 loc) · 5.94 KB
/
init.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
---@diagnostic disable: duplicate-set-field
local function path2fancy(path)
local paths = {}
local json_paths = {}
for word in string.gmatch(path..".","[^./]+") do
paths[#paths+1] = word
end
for i, value in pairs(paths) do
if i ~= #paths then
json_paths[#json_paths+1] = {text=value,color="#ff6464"}
json_paths[#json_paths+1] = {text="/",color="#797979"}
else
json_paths[#json_paths+1] = {text=value,color="#ff6464"}
end
end
return json_paths
end
---@diagnostic disable-next-line: lowercase-global
function tracebackError(input)
local path, line, comment = input:gsub("%s+", " "):gsub("^%s*(.-)%s*$", "%1"):match("(.*):(%d+) (.*) stack traceback:")
local compose = {}
compose[#compose+1] = {text="\n"}
compose[#compose+1] = {text="[Traceback]",color="#ff7b72"}
local i = 0
local f ={}
for l in string.gmatch(input,"[^\n]+") do
i = i + 1
if i > 2 then
table.insert(f,1,l)
end
end
for k,l in pairs(f) do
local trace_path,trace_comment = l:gsub("%s+", " "):gsub("^%s*(.-)%s*$", "%1"):match("^(.*): (.*)$")
local trace_line
if trace_path:find(":") then -- if valid traceback format
trace_path,trace_line = trace_path:match("^(.*):(.*)$")
compose[#compose+1] = {text="\n"}
compose[#compose+1] = {text=" "}
compose[#compose+1] = {text = "↓ ",color="#797979"}
compose[#compose+1] = {text="",extra=path2fancy(trace_path)}
compose[#compose+1] = {text = " : ",color="#797979"}
compose[#compose+1] = {text = trace_line,color="#00ecfb"}
compose[#compose+1] = {text = " : ",color="#797979"}
compose[#compose+1] = {text=trace_comment,color="#797979"}
end
end
local json_paths = path2fancy(path)
compose[#compose+1] = {text="\n",color=""}
compose[#compose+1] = {text="[ERROR]",color="#ff7b72"}
compose[#compose+1] = {text="\n"}
compose[#compose+1] = {text=" ",extra=json_paths}
compose[#compose+1] = {text = " : ",color="#797979"}
compose[#compose+1] = {text = line,color="#00ecfb"}
compose[#compose+1] = {text = "\n "}
compose[#compose+1] = {text = comment,color="#ff7b72"}
compose[#compose+1] = {text = "\n\n"}
printJson(toJson(compose))
end
--local ogreq = require
--
--function require(path)
-- local ok, info = pcall(ogreq,path)
-- if not ok then
-- tracebackError(info)
-- end
-- return info
--end
--local og_reg = figuraMetatables.Event.__index.register
--figuraMetatables.Event.__index.register = function (self, func, name)
-- og_reg(self, function (...)
-- local ok, result = pcall(func, ...)
-- if ok then
-- return result
-- else
-- tracebackError(result)
-- end
-- end, name)
--end
--[[
do
local r = require
function require(a, b, ...)
local t = avatar:getCurrentInstructions()
local l = {r(a, b, ...)}
local s = avatar:getCurrentInstructions() - t - 10
if s >= 1 then
print(a, s)
end
return table.unpack(l)
end
end]]
--figuraMetatables.HostAPI.__index.isHost = function ()
-- return false
--end
if events.ERROR then
events.ERROR:register(function (error)
tracebackError(error)
return true
end)
end
if host:isHost() then
local _require = require
function require(path)
local start = client.getSystemTime()
local result = {_require(path)}
local elapsed = client.getSystemTime() - start
--print(path, "took", elapsed, "ms")
return table.unpack(result)
end
-- only send when player has permission to do so
local og = figuraMetatables.HostAPI.__index.sendChatCommand
local queued_commands = {} ---@type string[]
figuraMetatables.HostAPI.__index.sendChatCommand = function (self, command)
queued_commands[#queued_commands+1] = command
end
local max_commands_per_tick = 16
events.WORLD_TICK:register(function ()
if player:getPermissionLevel() < 2 then
queued_commands = {}
end
if player:isLoaded() then
for i = 1, math.min(#queued_commands,max_commands_per_tick), 1 do
local cmd = queued_commands[1]
og(host,cmd)
--if cmd:find("^/?setblock") then
-- local x,y,z,block = cmd:match("^/?setblock (~?[%d.]+) (~?[%d.]+) (~?[%d.]+) ([%S]+)")
-- sound(x,y,z,world.newBlock(block):getSounds().place,0.8)
--end
table.remove(queued_commands,1)
end
end
end)
end
--figuraMetatables.HostAPI.__index.isHost = function () return false end
local n = 0
local function graduallyLoad(path)
local list = listFiles(path,true)
if host:isHost() then
for key, value in pairs(list) do
require(value)
end
end
local i = 0
events.WORLD_TICK:register(function ()
i = i + 1
if i > #list then
events.WORLD_TICK:remove("graduallyLoad"..n)
return
end
require(list[i])
end,"graduallyLoad"..n)
n = n + 1
end
-- services are called first, as they are potentially used by programs, but they use libraries
graduallyLoad("services")
-- programs are the lowest level scripts, they use services and libraries
graduallyLoad("programs")
local t = require("libraries.utils")
for key, metatable in pairs(figuraMetatables) do
figuraMetatables[key] = t.makeTableReadOnly(metatable)
end
local function loopTable(table,i)
table = t.makeTableReadOnly(table)
if i > 50 then
return
end
for key, value in pairs(table) do
if type(value) == "table" then
loopTable(value,i+1)
end
end
end
loopTable(_G, 1)
if not host:isHost() then return end
-- hosts are just programs but only runs on the host
for key, script in pairs(listFiles("host",true)) do
require(script)
end
--- initialize default page
local sidebar = require("host.contextMenu")
local main = require("pages.utilities.screenshot")()
sidebar:setPage(main)