feat: component script reloading

This commit is contained in:
MihailRis 2025-03-16 22:03:37 +03:00
parent 267aebe7bd
commit 4761c520d5
6 changed files with 42 additions and 22 deletions

View File

@ -96,10 +96,15 @@ function build_files_list(filenames, selected)
end
local parent = file.parent(filename)
local info = registry.get_info(actual_filename)
local icon = "file"
if info then
icon = info.type == "component" and "entity" or info.type
end
files_list:add(gui.template("script_file", {
path = parent .. (parent[#parent] == ':' and '' or '/'),
name = file.name(filename),
type = info and info.type or "file",
icon = icon,
unit = info and info.unit or '',
filename = actual_filename
}))
end
@ -153,6 +158,7 @@ function run_current_file()
end
local info = registry.get_info(current_file.filename)
local script_type = info and info.type or "file"
local unit = info and info.unit
save_current_file()
local func = function()
@ -160,16 +166,15 @@ function run_current_file()
xpcall(chunk, function(msg) __vc__error(msg, 1, 1, stack_size) end)
end
if script_type == "block" then
func = function() block.reload_script(info.unit) end
elseif script_type == "item" then
func = function() item.reload_script(info.unit) end
elseif script_type == "world" then
func = function() world.reload_script(info.unit) end
elseif script_type == "hud" then
func = function() hud.reload_script(info.unit) end
end
local output = core.capture_output(func)
local funcs = {
block = block.reload_script,
item = item.reload_script,
world = world.reload_script,
hud = hud.reload_script,
component = entities.reload_component,
}
func = funcs[script_type] or func
local output = core.capture_output(function() func(unit) end)
document.output:add(
string.format(
"<label enabled='false' multiline='true' margin='2'>%s</label>",

View File

@ -1,10 +1,11 @@
<container size="18">
<image src="gui/%{type}" size="16"></image>
<image src="gui/%{icon}" size="16"></image>
<label hover-color='#30A0FF'
pos="20,2"
interactive="true"
onclick='open_file_in_editor("%{filename}")'
markup='md'
tooltip='%{unit}'
sizefunc="-1,-1">
[#FFFFFF80]%{path}[#FFFFFFFF]%{name}
</label>

View File

@ -7,19 +7,19 @@ local function collect_components(dirname, dest)
if file.ext(filename) == "lua" then
table.insert(dest, filename)
export.classification[filename] = {
type="entity",
unit=file.prefix(filename)..":"..file.name(filename)
type="component",
unit=file.prefix(filename)..":"..file.stem(filename)
}
end
end
end
end
local function collect_scripts(dirname, dest)
local function collect_scripts(dirname, dest, ismodule)
if file.isdir(dirname) then
local files = file.list(dirname)
for i, filename in ipairs(files) do
if file.name(filename) == "components" then
if file.name(filename) == "components" and not ismodule then
collect_components(filename, dest)
elseif file.isdir(filename) then
collect_scripts(filename, dest)
@ -33,18 +33,18 @@ end
local function load_scripts_list()
local packs = pack.get_installed()
for _, packid in ipairs(packs) do
collect_scripts(packid..":modules", export.filenames)
collect_scripts(packid..":modules", export.filenames, true)
end
for _, filename in ipairs(export.filenames) do
export.classification[filename] = {
type="module",
unit=file.prefix(filename)..":"..filename:sub(filename:find("/")+1)
unit=file.join(file.parent(file.prefix(filename)..":"..
filename:sub(filename:find("/")+1)),
file.stem(filename))
}
end
for _, packid in ipairs(packs) do
collect_scripts(packid..":scripts", export.filenames)
collect_scripts(packid..":scripts", export.filenames, false)
end
end

View File

@ -34,7 +34,8 @@
"gui/play",
"gui/info",
"gui/world",
"gui/hud"
"gui/hud",
"gui/entity"
],
"fonts": [
{

BIN
res/textures/gui/entity.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 150 B

View File

@ -223,6 +223,18 @@ static int l_raycast(lua::State* L) {
return 0;
}
static int l_reload_component(lua::State* L) {
std::string name = lua::require_string(L, 1);
size_t pos = name.find(':');
if (pos == std::string::npos) {
throw std::runtime_error("missing entry point");
}
auto filename = name.substr(0, pos + 1) + "scripts/components/" +
name.substr(pos + 1) + ".lua";
scripting::load_entity_component(name, filename, filename);
return 0;
}
const luaL_Reg entitylib[] = {
{"exists", lua::wrap<l_exists>},
{"def_index", lua::wrap<l_def_index>},
@ -238,5 +250,6 @@ const luaL_Reg entitylib[] = {
{"get_all_in_box", lua::wrap<l_get_all_in_box>},
{"get_all_in_radius", lua::wrap<l_get_all_in_radius>},
{"raycast", lua::wrap<l_raycast>},
{"reload_component", lua::wrap<l_reload_component>},
{NULL, NULL}
};