Merge pull request #821 from Xertis/main

Added script-listener to packs loading
This commit is contained in:
MihailRis 2026-04-03 20:59:21 +03:00 • committed by GitHub
commit 936a5c8525
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 99 additions and 3 deletions

View file

@ -198,11 +198,26 @@ function on_close(invid: int)
Called on element removed from the screen.
## Content events
Callbacks specified in *content.lua* script.
```lua
function on_scripts_loading()
```
Called before the content pack scripts are loaded
```lua
function on_content_loaded()
```
Called after the content pack has been fully loaded
## HUD events
Callbacks specified in *hud.lua* script.
```lua
function on_hud_open(playerid: int)
```

View file

@ -5,6 +5,7 @@
- [События предметов](#события-предметов)
- [События мира](#события-мира)
- [События макета](#события-макета)
- [События контента](#события-контента)
- [События HUD](#события-hud)
- [События ECS](ecs.md#события-компонента)
- [Библиотека *Events*](#библиотека-events)
@ -189,7 +190,6 @@ function on_inventory_closed(invid: int, playerid: int)
Вызывается при закрытии инвентаря.
## События макета
События прописываются в файле `layouts/имя_макета.xml.lua`.
@ -208,6 +208,22 @@ function on_close(invid: int)
Вызывается при удалении элемента с экрана.
## События контента
События жизненного цикла контент-пака прописываются в файле `scripts/content.lua`
```lua
function on_scripts_loading()
```
Вызывается перед загрузкой скриптов контент-пака
```lua
function on_content_loaded()
```
Вызывается после того, как контент-пак был полностью загружен
## События HUD
События связанные с игровым интерфейсом прописываются в файле `scripts/hud.lua`

View file

@ -334,6 +334,8 @@ void ContentLoader::load() {
fixPackIndices();
loadContentScript(*runtime);
auto folder = pack->folder;
builder.defaults = paths.readCombinedObject(
@ -462,6 +464,20 @@ void ContentLoader::reloadScript(const Content& content, ItemDef& item) {
load_script(content, item);
}
void ContentLoader::loadContentScript(ContentPackRuntime& runtime) {
const auto& pack = runtime.getInfo();
const auto& folder = pack.folder;
io::path scriptFile = folder / "scripts/content.lua";
if (io::is_regular_file(scriptFile)) {
scripting::load_content_script(
runtime.getEnvironment(),
pack.id,
scriptFile,
pack.id + ":scripts/content.lua"
);
}
}
void ContentLoader::loadWorldScript(ContentPackRuntime& runtime) {
const auto& pack = runtime.getInfo();
const auto& folder = pack.folder;
@ -478,6 +494,7 @@ void ContentLoader::loadWorldScript(ContentPackRuntime& runtime) {
}
void ContentLoader::loadScripts(Content& content) {
scripting::on_scripts_loading();
load_scripts(content, content.blocks);
load_scripts(content, content.items);
@ -486,7 +503,6 @@ void ContentLoader::loadScripts(Content& content) {
const auto& pack = runtime->getInfo();
const auto& folder = pack.folder;
// Load main world script
loadWorldScript(*runtime);
// Load entity components
@ -501,6 +517,8 @@ void ContentLoader::loadScripts(Content& content) {
);
});
}
scripting::on_content_loaded();
}
void ContentLoader::loadResources(ResourceType type, const dv::value& list) {

View file

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

View file

@ -627,6 +627,26 @@ void scripting::on_ui_close(UiDocument* layout, Inventory* inventory) {
});
}
void scripting::on_scripts_loading() {
auto L = lua::get_main_state();
for (auto& pack : content_control->getAllContentPacks()) {
lua::emit_event(L, pack.id + ":.onscriptsloading", [](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 +783,19 @@ void scripting::load_world_script(
register_event(env, "on_inventory_closed", prefix + ":.inventoryclosed");
}
void scripting::load_content_script(
const scriptenv& senv,
const std::string& prefix,
const io::path& file,
const std::string& fileName
) {
int env = *senv;
lua::pop(lua::get_main_state(), load_script(env, "content", file, fileName));
register_event(env, "on_scripts_loading", prefix + ":.onscriptsloading");
register_event(env, "on_content_loaded", prefix + ":.oncontentloaded");
}
void scripting::load_layout_script(
const scriptenv& senv,
const std::string& prefix,

View file

@ -160,6 +160,12 @@ namespace scripting {
/// @brief Called on UI view close
void on_ui_close(UiDocument* layout, Inventory* inventory);
/// @brief Called on Content loading
void on_scripts_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 +227,13 @@ namespace scripting {
WorldFuncsSet& funcsset
);
void load_content_script(
const scriptenv& env,
const std::string& packid,
const io::path& file,
const std::string& fileName
);
/// @brief Load script associated with an UiDocument
/// @param env environment
/// @param prefix pack id