feat: world script reloading
This commit is contained in:
parent
bcbfdd50c7
commit
f9998f0a93
@ -39,7 +39,8 @@
|
|||||||
</panel>
|
</panel>
|
||||||
</splitbox>
|
</splitbox>
|
||||||
<splitbox id="editorContainer" split-pos="0.8">
|
<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">
|
<container size-func="-1,30" color="#00000020">
|
||||||
<image id="lockIcon" src="gui/lock" tooltip="@Read only"
|
<image id="lockIcon" src="gui/lock" tooltip="@Read only"
|
||||||
interactive="true" onclick="unlock_access()"
|
interactive="true" onclick="unlock_access()"
|
||||||
|
|||||||
@ -171,6 +171,8 @@ function run_current_file()
|
|||||||
func = function() block.reload_script(unit) end
|
func = function() block.reload_script(unit) end
|
||||||
elseif script_type == "item" then
|
elseif script_type == "item" then
|
||||||
func = function() item.reload_script(unit) end
|
func = function() item.reload_script(unit) end
|
||||||
|
elseif script_type == "world" then
|
||||||
|
func = function() world.reload_script(unit) end
|
||||||
end
|
end
|
||||||
local output = core.capture_output(func)
|
local output = core.capture_output(func)
|
||||||
document.output:add(
|
document.output:add(
|
||||||
@ -387,6 +389,10 @@ local function build_scripts_classification()
|
|||||||
for id, props in pairs(item.properties) do
|
for id, props in pairs(item.properties) do
|
||||||
scripts_classification[props["script-file"]] = {"item", item.name(id)}
|
scripts_classification[props["script-file"]] = {"item", item.name(id)}
|
||||||
end
|
end
|
||||||
|
local packs = pack.get_installed()
|
||||||
|
for _, packid in ipairs(packs) do
|
||||||
|
scripts_classification[packid..":scripts/world.lua"] = {"world", packid}
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
local function load_scripts_list()
|
local function load_scripts_list()
|
||||||
@ -402,7 +408,6 @@ local function load_scripts_list()
|
|||||||
for _, packid in ipairs(packs) do
|
for _, packid in ipairs(packs) do
|
||||||
collect_scripts(packid..":scripts", filenames)
|
collect_scripts(packid..":scripts", filenames)
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
function on_open(mode)
|
function on_open(mode)
|
||||||
|
|||||||
@ -32,7 +32,8 @@
|
|||||||
"gui/file",
|
"gui/file",
|
||||||
"gui/module",
|
"gui/module",
|
||||||
"gui/play",
|
"gui/play",
|
||||||
"gui/info"
|
"gui/info",
|
||||||
|
"gui/world"
|
||||||
],
|
],
|
||||||
"fonts": [
|
"fonts": [
|
||||||
{
|
{
|
||||||
|
|||||||
BIN
res/textures/gui/world.png
Normal file
BIN
res/textures/gui/world.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 178 B |
@ -79,6 +79,14 @@ const ContentPackRuntime* Content::getPackRuntime(const std::string& id) const {
|
|||||||
return found->second.get();
|
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 {
|
const UptrsMap<std::string, BlockMaterial>& Content::getBlockMaterials() const {
|
||||||
return blockMaterials;
|
return blockMaterials;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -248,6 +248,7 @@ public:
|
|||||||
const rigging::SkeletonConfig* getSkeleton(const std::string& id) const;
|
const rigging::SkeletonConfig* getSkeleton(const std::string& id) const;
|
||||||
const BlockMaterial* findBlockMaterial(const std::string& id) const;
|
const BlockMaterial* findBlockMaterial(const std::string& id) const;
|
||||||
const ContentPackRuntime* getPackRuntime(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, BlockMaterial>& getBlockMaterials() const;
|
||||||
const UptrsMap<std::string, ContentPackRuntime>& getPacks() const;
|
const UptrsMap<std::string, ContentPackRuntime>& getPacks() const;
|
||||||
|
|||||||
@ -889,6 +889,21 @@ void ContentLoader::reloadScript(const Content& content, ItemDef& item) {
|
|||||||
load_script(content, 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) {
|
void ContentLoader::loadScripts(Content& content) {
|
||||||
load_scripts(content, content.blocks);
|
load_scripts(content, content.blocks);
|
||||||
load_scripts(content, content.items);
|
load_scripts(content, content.items);
|
||||||
@ -898,16 +913,8 @@ void ContentLoader::loadScripts(Content& content) {
|
|||||||
const auto& folder = pack.folder;
|
const auto& folder = pack.folder;
|
||||||
|
|
||||||
// Load main world script
|
// Load main world script
|
||||||
io::path scriptFile = folder / "scripts/world.lua";
|
loadWorldScript(*runtime);
|
||||||
if (io::is_regular_file(scriptFile)) {
|
|
||||||
scripting::load_world_script(
|
|
||||||
runtime->getEnvironment(),
|
|
||||||
pack.id,
|
|
||||||
scriptFile,
|
|
||||||
pack.id + ":scripts/world.lua",
|
|
||||||
runtime->worldfuncsset
|
|
||||||
);
|
|
||||||
}
|
|
||||||
// Load entity components
|
// Load entity components
|
||||||
io::path componentsDir = folder / "scripts/components";
|
io::path componentsDir = folder / "scripts/components";
|
||||||
foreach_file(componentsDir, [&pack](const io::path& file) {
|
foreach_file(componentsDir, [&pack](const io::path& file) {
|
||||||
|
|||||||
@ -77,6 +77,7 @@ public:
|
|||||||
void load();
|
void load();
|
||||||
|
|
||||||
static void loadScripts(Content& content);
|
static void loadScripts(Content& content);
|
||||||
|
static void loadWorldScript(ContentPackRuntime& pack);
|
||||||
static void reloadScript(const Content& content, Block& block);
|
static void reloadScript(const Content& content, Block& block);
|
||||||
static void reloadScript(const Content& content, ItemDef& item);
|
static void reloadScript(const Content& content, ItemDef& item);
|
||||||
};
|
};
|
||||||
|
|||||||
@ -6,6 +6,7 @@
|
|||||||
#include "assets/AssetsLoader.hpp"
|
#include "assets/AssetsLoader.hpp"
|
||||||
#include "coders/json.hpp"
|
#include "coders/json.hpp"
|
||||||
#include "content/Content.hpp"
|
#include "content/Content.hpp"
|
||||||
|
#include "content/ContentLoader.hpp"
|
||||||
#include "engine/Engine.hpp"
|
#include "engine/Engine.hpp"
|
||||||
#include "world/files/WorldFiles.hpp"
|
#include "world/files/WorldFiles.hpp"
|
||||||
#include "io/engine_paths.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());
|
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[] = {
|
const luaL_Reg worldlib[] = {
|
||||||
{"is_open", lua::wrap<l_is_open>},
|
{"is_open", lua::wrap<l_is_open>},
|
||||||
{"get_list", lua::wrap<l_get_list>},
|
{"get_list", lua::wrap<l_get_list>},
|
||||||
@ -230,5 +242,6 @@ const luaL_Reg worldlib[] = {
|
|||||||
{"set_chunk_data", lua::wrap<l_set_chunk_data>},
|
{"set_chunk_data", lua::wrap<l_set_chunk_data>},
|
||||||
{"save_chunk_data", lua::wrap<l_save_chunk_data>},
|
{"save_chunk_data", lua::wrap<l_save_chunk_data>},
|
||||||
{"count_chunks", lua::wrap<l_count_chunks>},
|
{"count_chunks", lua::wrap<l_count_chunks>},
|
||||||
|
{"reload_script", lua::wrap<l_reload_script>},
|
||||||
{NULL, NULL}
|
{NULL, NULL}
|
||||||
};
|
};
|
||||||
|
|||||||
@ -836,6 +836,8 @@ void scripting::load_content_script(
|
|||||||
) {
|
) {
|
||||||
int env = *senv;
|
int env = *senv;
|
||||||
lua::pop(lua::get_main_state(), load_script(env, "block", file, fileName));
|
lua::pop(lua::get_main_state(), load_script(env, "block", file, fileName));
|
||||||
|
|
||||||
|
funcsset = {};
|
||||||
funcsset.init = register_event(env, "init", prefix + ".init");
|
funcsset.init = register_event(env, "init", prefix + ".init");
|
||||||
funcsset.update = register_event(env, "on_update", prefix + ".update");
|
funcsset.update = register_event(env, "on_update", prefix + ".update");
|
||||||
funcsset.randupdate =
|
funcsset.randupdate =
|
||||||
@ -861,6 +863,8 @@ void scripting::load_content_script(
|
|||||||
) {
|
) {
|
||||||
int env = *senv;
|
int env = *senv;
|
||||||
lua::pop(lua::get_main_state(), load_script(env, "item", file, fileName));
|
lua::pop(lua::get_main_state(), load_script(env, "item", file, fileName));
|
||||||
|
|
||||||
|
funcsset = {};
|
||||||
funcsset.init = register_event(env, "init", prefix + ".init");
|
funcsset.init = register_event(env, "init", prefix + ".init");
|
||||||
funcsset.on_use = register_event(env, "on_use", prefix + ".use");
|
funcsset.on_use = register_event(env, "on_use", prefix + ".use");
|
||||||
funcsset.on_use_on_block =
|
funcsset.on_use_on_block =
|
||||||
@ -888,6 +892,8 @@ void scripting::load_world_script(
|
|||||||
) {
|
) {
|
||||||
int env = *senv;
|
int env = *senv;
|
||||||
lua::pop(lua::get_main_state(), load_script(env, "world", file, fileName));
|
lua::pop(lua::get_main_state(), load_script(env, "world", file, fileName));
|
||||||
|
|
||||||
|
funcsset = {};
|
||||||
register_event(env, "init", prefix + ".init");
|
register_event(env, "init", prefix + ".init");
|
||||||
register_event(env, "on_world_open", prefix + ":.worldopen");
|
register_event(env, "on_world_open", prefix + ":.worldopen");
|
||||||
register_event(env, "on_world_tick", prefix + ":.worldtick");
|
register_event(env, "on_world_tick", prefix + ":.worldtick");
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user