feat: world script reloading

This commit is contained in:
MihailRis 2025-03-14 17:07:10 +03:00
parent bcbfdd50c7
commit f9998f0a93
10 changed files with 56 additions and 13 deletions

View File

@ -39,7 +39,8 @@
</panel>
</splitbox>
<splitbox id="editorContainer" split-pos="0.8">
<container color="#00000080">
<container color="#00000080"
onclick="document.editor.focused = true document.editor.caret = -1">
<container size-func="-1,30" color="#00000020">
<image id="lockIcon" src="gui/lock" tooltip="@Read only"
interactive="true" onclick="unlock_access()"

View File

@ -171,6 +171,8 @@ function run_current_file()
func = function() block.reload_script(unit) end
elseif script_type == "item" then
func = function() item.reload_script(unit) end
elseif script_type == "world" then
func = function() world.reload_script(unit) end
end
local output = core.capture_output(func)
document.output:add(
@ -387,6 +389,10 @@ local function build_scripts_classification()
for id, props in pairs(item.properties) do
scripts_classification[props["script-file"]] = {"item", item.name(id)}
end
local packs = pack.get_installed()
for _, packid in ipairs(packs) do
scripts_classification[packid..":scripts/world.lua"] = {"world", packid}
end
end
local function load_scripts_list()
@ -402,7 +408,6 @@ local function load_scripts_list()
for _, packid in ipairs(packs) do
collect_scripts(packid..":scripts", filenames)
end
end
function on_open(mode)

View File

@ -32,7 +32,8 @@
"gui/file",
"gui/module",
"gui/play",
"gui/info"
"gui/info",
"gui/world"
],
"fonts": [
{

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

Binary file not shown.

After

Width:  |  Height:  |  Size: 178 B

View File

@ -79,6 +79,14 @@ const ContentPackRuntime* Content::getPackRuntime(const std::string& id) const {
return found->second.get();
}
ContentPackRuntime* Content::getPackRuntime(const std::string& id) {
auto found = packs.find(id);
if (found == packs.end()) {
return nullptr;
}
return found->second.get();
}
const UptrsMap<std::string, BlockMaterial>& Content::getBlockMaterials() const {
return blockMaterials;
}

View File

@ -248,6 +248,7 @@ public:
const rigging::SkeletonConfig* getSkeleton(const std::string& id) const;
const BlockMaterial* findBlockMaterial(const std::string& id) const;
const ContentPackRuntime* getPackRuntime(const std::string& id) const;
ContentPackRuntime* getPackRuntime(const std::string& id);
const UptrsMap<std::string, BlockMaterial>& getBlockMaterials() const;
const UptrsMap<std::string, ContentPackRuntime>& getPacks() const;

View File

@ -889,6 +889,21 @@ void ContentLoader::reloadScript(const Content& content, ItemDef& item) {
load_script(content, item);
}
void ContentLoader::loadWorldScript(ContentPackRuntime& runtime) {
const auto& pack = runtime.getInfo();
const auto& folder = pack.folder;
io::path scriptFile = folder / "scripts/world.lua";
if (io::is_regular_file(scriptFile)) {
scripting::load_world_script(
runtime.getEnvironment(),
pack.id,
scriptFile,
pack.id + ":scripts/world.lua",
runtime.worldfuncsset
);
}
}
void ContentLoader::loadScripts(Content& content) {
load_scripts(content, content.blocks);
load_scripts(content, content.items);
@ -898,16 +913,8 @@ void ContentLoader::loadScripts(Content& content) {
const auto& folder = pack.folder;
// Load main world script
io::path scriptFile = folder / "scripts/world.lua";
if (io::is_regular_file(scriptFile)) {
scripting::load_world_script(
runtime->getEnvironment(),
pack.id,
scriptFile,
pack.id + ":scripts/world.lua",
runtime->worldfuncsset
);
}
loadWorldScript(*runtime);
// Load entity components
io::path componentsDir = folder / "scripts/components";
foreach_file(componentsDir, [&pack](const io::path& file) {

View File

@ -77,6 +77,7 @@ public:
void load();
static void loadScripts(Content& content);
static void loadWorldScript(ContentPackRuntime& pack);
static void reloadScript(const Content& content, Block& block);
static void reloadScript(const Content& content, ItemDef& item);
};

View File

@ -6,6 +6,7 @@
#include "assets/AssetsLoader.hpp"
#include "coders/json.hpp"
#include "content/Content.hpp"
#include "content/ContentLoader.hpp"
#include "engine/Engine.hpp"
#include "world/files/WorldFiles.hpp"
#include "io/engine_paths.hpp"
@ -213,6 +214,17 @@ static int l_count_chunks(lua::State* L) {
return lua::pushinteger(L, level->chunks->size());
}
static int l_reload_script(lua::State* L) {
auto packid = lua::require_string(L, 1);
if (content == nullptr) {
throw std::runtime_error("content is not initialized");
}
auto& writeableContent = *engine->getWriteableContent();
auto pack = writeableContent.getPackRuntime(packid);
ContentLoader::loadWorldScript(*pack);
return 0;
}
const luaL_Reg worldlib[] = {
{"is_open", lua::wrap<l_is_open>},
{"get_list", lua::wrap<l_get_list>},
@ -230,5 +242,6 @@ const luaL_Reg worldlib[] = {
{"set_chunk_data", lua::wrap<l_set_chunk_data>},
{"save_chunk_data", lua::wrap<l_save_chunk_data>},
{"count_chunks", lua::wrap<l_count_chunks>},
{"reload_script", lua::wrap<l_reload_script>},
{NULL, NULL}
};

View File

@ -836,6 +836,8 @@ void scripting::load_content_script(
) {
int env = *senv;
lua::pop(lua::get_main_state(), load_script(env, "block", file, fileName));
funcsset = {};
funcsset.init = register_event(env, "init", prefix + ".init");
funcsset.update = register_event(env, "on_update", prefix + ".update");
funcsset.randupdate =
@ -861,6 +863,8 @@ void scripting::load_content_script(
) {
int env = *senv;
lua::pop(lua::get_main_state(), load_script(env, "item", file, fileName));
funcsset = {};
funcsset.init = register_event(env, "init", prefix + ".init");
funcsset.on_use = register_event(env, "on_use", prefix + ".use");
funcsset.on_use_on_block =
@ -888,6 +892,8 @@ void scripting::load_world_script(
) {
int env = *senv;
lua::pop(lua::get_main_state(), load_script(env, "world", file, fileName));
funcsset = {};
register_event(env, "init", prefix + ".init");
register_event(env, "on_world_open", prefix + ":.worldopen");
register_event(env, "on_world_tick", prefix + ":.worldtick");