mirror of
https://github.com/MihailRis/voxelcore.git
synced 2026-10-04 18:41:51 +00:00
Added script-listener to packs loading
This commit is contained in:
parent
b5f5aeb5c6
commit
61625ffe00
5 changed files with 92 additions and 1 deletions
|
|
@ -334,12 +334,16 @@ void ContentLoader::load() {
|
|||
|
||||
fixPackIndices();
|
||||
|
||||
loadMainScript(*runtime);
|
||||
|
||||
auto folder = pack->folder;
|
||||
|
||||
builder.defaults = paths.readCombinedObject(
|
||||
EnginePaths::CONFIG_DEFAULTS.string()
|
||||
);
|
||||
|
||||
scripting::on_content_initialization();
|
||||
|
||||
// Load world generators
|
||||
io::path generatorsDir = folder / "generators";
|
||||
foreach_file(generatorsDir, [this](const io::path& file) {
|
||||
|
|
@ -462,6 +466,21 @@ void ContentLoader::reloadScript(const Content& content, ItemDef& item) {
|
|||
load_script(content, item);
|
||||
}
|
||||
|
||||
void ContentLoader::loadMainScript(ContentPackRuntime& runtime) {
|
||||
const auto& pack = runtime.getInfo();
|
||||
const auto& folder = pack.folder;
|
||||
io::path scriptFile = folder / "scripts/main.lua";
|
||||
if (io::is_regular_file(scriptFile)) {
|
||||
scripting::load_main_script(
|
||||
runtime.getEnvironment(),
|
||||
pack.id,
|
||||
scriptFile,
|
||||
pack.id + ":scripts/main.lua",
|
||||
runtime.mainfuncsset
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
void ContentLoader::loadWorldScript(ContentPackRuntime& runtime) {
|
||||
const auto& pack = runtime.getInfo();
|
||||
const auto& folder = pack.folder;
|
||||
|
|
@ -478,6 +497,7 @@ void ContentLoader::loadWorldScript(ContentPackRuntime& runtime) {
|
|||
}
|
||||
|
||||
void ContentLoader::loadScripts(Content& content) {
|
||||
scripting::on_content_loading();
|
||||
load_scripts(content, content.blocks);
|
||||
load_scripts(content, content.items);
|
||||
|
||||
|
|
@ -486,7 +506,7 @@ void ContentLoader::loadScripts(Content& content) {
|
|||
const auto& pack = runtime->getInfo();
|
||||
const auto& folder = pack.folder;
|
||||
|
||||
// Load main world script
|
||||
// Load world script
|
||||
loadWorldScript(*runtime);
|
||||
|
||||
// Load entity components
|
||||
|
|
@ -501,6 +521,8 @@ void ContentLoader::loadScripts(Content& content) {
|
|||
);
|
||||
});
|
||||
}
|
||||
|
||||
scripting::on_content_loaded();
|
||||
}
|
||||
|
||||
void ContentLoader::loadResources(ResourceType type, const dv::value& list) {
|
||||
|
|
|
|||
|
|
@ -58,6 +58,7 @@ public:
|
|||
void load();
|
||||
|
||||
static void loadScripts(Content& content);
|
||||
static void loadMainScript(ContentPackRuntime& pack);
|
||||
static void loadWorldScript(ContentPackRuntime& pack);
|
||||
static void reloadScript(const Content& content, Block& block);
|
||||
static void reloadScript(const Content& content, ItemDef& item);
|
||||
|
|
|
|||
|
|
@ -129,12 +129,17 @@ struct WorldFuncsSet {
|
|||
bool oninventoryclosed;
|
||||
};
|
||||
|
||||
struct MainFuncsSet {
|
||||
bool oncontentloading;
|
||||
};
|
||||
|
||||
class ContentPackRuntime {
|
||||
ContentPack info;
|
||||
ContentPackStats stats {};
|
||||
scriptenv env;
|
||||
public:
|
||||
WorldFuncsSet worldfuncsset {};
|
||||
MainFuncsSet mainfuncsset {};
|
||||
|
||||
ContentPackRuntime(ContentPack info, scriptenv env);
|
||||
~ContentPackRuntime();
|
||||
|
|
|
|||
|
|
@ -627,6 +627,36 @@ void scripting::on_ui_close(UiDocument* layout, Inventory* inventory) {
|
|||
});
|
||||
}
|
||||
|
||||
void scripting::on_content_initialization() {
|
||||
auto L = lua::get_main_state();
|
||||
|
||||
for (auto& pack : content_control->getAllContentPacks()) {
|
||||
lua::emit_event(L, pack.id + ":.oncontentinitialization", [](auto L) {
|
||||
return 0;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
void scripting::on_content_loading() {
|
||||
auto L = lua::get_main_state();
|
||||
|
||||
for (auto& pack : content_control->getAllContentPacks()) {
|
||||
lua::emit_event(L, pack.id + ":.oncontentloading", [](auto L) {
|
||||
return 0;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
void scripting::on_content_loaded() {
|
||||
auto L = lua::get_main_state();
|
||||
|
||||
for (auto& pack : content_control->getAllContentPacks()) {
|
||||
lua::emit_event(L, pack.id + ":.oncontentloaded", [](auto L) {
|
||||
return 0;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
bool scripting::register_event(
|
||||
int env, const std::string& name, const std::string& id
|
||||
) {
|
||||
|
|
@ -763,6 +793,21 @@ void scripting::load_world_script(
|
|||
register_event(env, "on_inventory_closed", prefix + ":.inventoryclosed");
|
||||
}
|
||||
|
||||
void scripting::load_main_script(
|
||||
const scriptenv& senv,
|
||||
const std::string& prefix,
|
||||
const io::path& file,
|
||||
const std::string& fileName,
|
||||
MainFuncsSet& funcsset
|
||||
) {
|
||||
int env = *senv;
|
||||
lua::pop(lua::get_main_state(), load_script(env, "main", file, fileName));
|
||||
|
||||
register_event(env, "on_content_initialization", prefix + ":.oncontentinitialization");
|
||||
register_event(env, "on_content_loading", prefix + ":.oncontentloading");
|
||||
register_event(env, "on_content_loaded", prefix + ":.oncontentloaded");
|
||||
}
|
||||
|
||||
void scripting::load_layout_script(
|
||||
const scriptenv& senv,
|
||||
const std::string& prefix,
|
||||
|
|
|
|||
|
|
@ -27,6 +27,7 @@ struct BlockFuncNamesCache;
|
|||
struct ItemFuncsSet;
|
||||
struct ItemFuncNamesCache;
|
||||
struct WorldFuncsSet;
|
||||
struct MainFuncsSet;
|
||||
struct UserComponent;
|
||||
struct UiDocScript;
|
||||
class BlocksController;
|
||||
|
|
@ -160,6 +161,15 @@ namespace scripting {
|
|||
/// @brief Called on UI view close
|
||||
void on_ui_close(UiDocument* layout, Inventory* inventory);
|
||||
|
||||
/// @brief Called before loading
|
||||
void on_content_initialization();
|
||||
|
||||
/// @brief Called on Content loading
|
||||
void on_content_loading();
|
||||
|
||||
/// @brief Called on Content loading finish
|
||||
void on_content_loaded();
|
||||
|
||||
/// @brief Load script associated with a Block
|
||||
/// @param env environment
|
||||
/// @param prefix pack id
|
||||
|
|
@ -221,6 +231,14 @@ namespace scripting {
|
|||
WorldFuncsSet& funcsset
|
||||
);
|
||||
|
||||
void load_main_script(
|
||||
const scriptenv& env,
|
||||
const std::string& packid,
|
||||
const io::path& file,
|
||||
const std::string& fileName,
|
||||
MainFuncsSet& funcsset
|
||||
);
|
||||
|
||||
/// @brief Load script associated with an UiDocument
|
||||
/// @param env environment
|
||||
/// @param prefix pack id
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue