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

View File

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

View File

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

View File

@ -34,7 +34,8 @@
"gui/play", "gui/play",
"gui/info", "gui/info",
"gui/world", "gui/world",
"gui/hud" "gui/hud",
"gui/entity"
], ],
"fonts": [ "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; 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[] = { const luaL_Reg entitylib[] = {
{"exists", lua::wrap<l_exists>}, {"exists", lua::wrap<l_exists>},
{"def_index", lua::wrap<l_def_index>}, {"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_box", lua::wrap<l_get_all_in_box>},
{"get_all_in_radius", lua::wrap<l_get_all_in_radius>}, {"get_all_in_radius", lua::wrap<l_get_all_in_radius>},
{"raycast", lua::wrap<l_raycast>}, {"raycast", lua::wrap<l_raycast>},
{"reload_component", lua::wrap<l_reload_component>},
{NULL, NULL} {NULL, NULL}
}; };