add Lua traceback inspector (WIP) && add chat prototype

This commit is contained in:
MihailRis 2024-11-19 07:32:03 +03:00
parent 66be215101
commit 29d8c4bbb8
7 changed files with 174 additions and 8 deletions

View File

@ -1,5 +1,18 @@
<container color='#00000080' size='400' size-func="unpack(gui.get_viewport())">
<container size-func="gui.get_viewport()[1],gui.get_viewport()[2]-40">
<panel interval="0"
orientation="horizontal"
color="#00000010"
size-func="gui.get_viewport()[1]-350,30">
<button id="s_chat" size="110,30" onclick="modes:set('chat')">@Chat</button>
<button id="s_console" size="110,30" onclick="modes:set('console')">@Console</button>
<button id="s_debug" size="110,30" onclick="modes:set('debug')">@Debug</button>
</panel>
<container pos="0,30" size-func="gui.get_viewport()[1]-350,30" color="#00000020">
<label id="title" pos="8,8"></label>
</container>
<container id="logContainer" pos="0,60"
size-func="unpack(vec2.add(gui.get_viewport(), {0,-70}))">
<textbox
id='log'
color='0'
@ -11,12 +24,33 @@
gravity="bottom-left"
></textbox>
</container>
<container id="editorContainer" pos="0,60" color="#00000080"
size-func="unpack(vec2.add(gui.get_viewport(), {-350,-230}))">
<textbox
id='editor'
color='0'
autoresize='true'
margin='0'
padding='5'
editable='false'
multiline='true'
line-numbers='true'
text-color="#FFFFFFA0"
size-func="gui.get_viewport()[1]-350,40"
gravity="top-left"
text-wrap='false'
scroll-step='50'
></textbox>
</container>
<panel id="traceback" gravity="bottom-left" padding="4" color="#000000A0"
max-length="170" size-func="gui.get_viewport()[1]-350,170">
</panel>
<panel id="problemsLog"
color="#00000010"
position-func="gui.get_viewport()[1]-350,0"
size-func="350,gui.get_viewport()[2]-40"
padding="5,15,5,15">
<label>@Problems</label>
<label margin="0,0,0,5">@Problems</label>
</panel>
<textbox id='prompt'
consumer='submit'

View File

@ -1,21 +1,93 @@
console_mode = "console"
history = session.get_entry("commands_history")
history_pointer = #history
local warnings_all = {}
local errors_all = {}
local warning_id = 0
events.on("core:warning", function (wtype, text)
local error_id = 0
events.on("core:warning", function (wtype, text, traceback)
local full = wtype..": "..text
if table.has(warnings_all, full) then
return
end
local encoded = base64.encode(bjson.tobytes({frames=traceback}))
document.problemsLog:add(gui.template("problem", {
type="warning", text=full, id=tostring(warning_id)
type="warning",
text=full,
traceback=encoded,
id=tostring(warning_id)
}))
warning_id = warning_id + 1
table.insert(warnings_all, full)
end)
events.on("core:error", function (msg, traceback)
local _, endindex = string.find(msg, ": ")
local full = ""
for i,frame in ipairs(traceback) do
full = full..frame.source..tostring(frame.currentline)
end
if table.has(errors_all, full) then
return
end
local encoded = base64.encode(bjson.tobytes({frames=traceback}))
document.problemsLog:add(gui.template("problem", {
type="error",
text=msg:sub(endindex),
traceback=encoded,
id=tostring(error_id)
}))
error_id = error_id + 1
table.insert(errors_all, full)
end)
events.on("core:open_traceback", function(traceback_b64)
local traceback = bjson.frombytes(base64.decode(traceback_b64))
modes:set('debug')
local tb_list = document.traceback
local srcsize = tb_list.size
tb_list:clear()
tb_list:add("<label enabled='false' margin='2'>@devtools.traceback</label>")
for _, frame in ipairs(traceback.frames) do
local callback = ""
local framestr = ""
if frame.what == "C" then
framestr = "C/C++ "
else
framestr = frame.source..":"..tostring(frame.currentline).." "
if file.exists(frame.source) then
callback = "local source = file.read('"..frame.source.."') "..
"document.editor.text = source "..
"document.editor.focused = true "..
"time.post_runnable(function() document.editor.caret = document.editor:linePos("..
tostring(frame.currentline-1)..") end)"
else
callback = "document.editor.text = 'Could not open source file'"
end
callback = callback.." document.title.text = gui.str('File')..' - "
..frame.source.."'"
end
if frame.name then
framestr = framestr.."("..tostring(frame.name)..")"
end
local color = "#FFFFFF"
if frame.source:starts_with("core:") then
color = "#C0D0C5"
end
tb_list:add(gui.template("stack_frame", {
location=framestr,
color=color,
callback=callback
}))
end
tb_list.size = srcsize
end)
function setup_variables()
local pid = hud.get_player()
local x,y,z = player.get_pos(pid)
@ -56,10 +128,19 @@ function add_to_history(text)
end
function submit(text)
text = text:trim()
add_to_history(text)
if console_mode == "chat" then
if not text:starts_with("/") then
text = "chat "..string.escape(text)
else
text = text:sub(2)
end
end
setup_variables()
text = text:trim()
local name
for s in text:gmatch("%S+") do
name = s
@ -84,6 +165,35 @@ function submit(text)
document.prompt.focused = true
end
function on_open()
document.prompt.focused = true
function set_mode(mode)
local show_prompt = mode == 'chat' or mode == 'console'
document.title.text = ""
document.editorContainer.visible = mode == 'debug'
document.logContainer.visible = mode ~= 'debug'
if mode == 'debug' then
document.root.color = {16, 18, 20, 220}
else
document.root.color = {0, 0, 0, 128}
end
document.traceback.visible = mode == 'debug'
document.prompt.visible = show_prompt
if show_prompt then
document.prompt.focused = true
end
console_mode = mode
end
function on_open()
if modes == nil then
modes = RadioGroup({
chat=document.s_chat,
console=document.s_console,
debug=document.s_debug
}, function (mode)
set_mode(mode)
end, "console")
end
end

View File

@ -1,4 +1,5 @@
<container id="%{id}" size="32" tooltip="%{text}">
<container id="%{id}" size="32" tooltip="%{text}"
onclick="events.emit('core:open_traceback', '%{traceback}')">
<image src="gui/%{type}" size="32"/>
<label pos="36,2">%{text}</label>
<image src="gui/cross" interactive="true" size="16" gravity="top-right"

View File

@ -0,0 +1,3 @@
<label hover-color="#A0A0FF" interactive="true" onclick="%{callback}" color="%{color}">
%{location}
</label>

View File

@ -256,6 +256,14 @@ console.add_command(
end
)
console.add_command(
"chat text:str",
"Send chat message",
function (args, kwargs)
console.log("[you] "..args[1])
end
)
console.cheats = {
"blocks.fill",
"tp",

View File

@ -11,6 +11,8 @@ world.delete-confirm=Do you want to delete world forever?
world.generators.default=Default
world.generators.flat=Flat
devtools.traceback=Traceback (most recent call first)
# Tooltips
graphics.gamma.tooltip=Lighting brightness curve
graphics.backlight.tooltip=Backlight to prevent total darkness

View File

@ -12,7 +12,15 @@ Dependencies=Зависимости
Description=Описание
Converting world...=Выполняется конвертация мира...
Unlimited=Неограниченно
Chat=Чат
Console=Консоль
Log=Лог
Problems=Проблемы
Monitor=Мониторинг
Debug=Отладка
File=Файл
devtools.traceback=Стек вызовов (от последнего)
error.pack-not-found=Не удалось найти пакет
error.dependency-not-found=Используемая зависимость не найдена
pack.remove-confirm=Удалить весь поставляемый паком/паками контент из мира (безвозвратно)?