fix: pack environment not passed to components

This commit is contained in:
MihailRis 2025-12-01 22:01:18 +03:00 committed by ShiftyX1
parent 59df377fde
commit 5ae34c2dc2
4 changed files with 19 additions and 4 deletions

View File

@ -492,6 +492,7 @@ void ContentLoader::loadScripts(Content& content) {
load_scripts(content, content.items); load_scripts(content, content.items);
for (const auto& [packid, runtime] : content.getPacks()) { for (const auto& [packid, runtime] : content.getPacks()) {
auto env = runtime->getEnvironment();
const auto& pack = runtime->getInfo(); const auto& pack = runtime->getInfo();
const auto& folder = pack.folder; const auto& folder = pack.folder;
@ -500,9 +501,10 @@ void ContentLoader::loadScripts(Content& content) {
// 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, env](const io::path& file) {
auto name = pack.id + ":" + file.stem(); auto name = pack.id + ":" + file.stem();
scripting::load_entity_component( scripting::load_entity_component(
env,
name, name,
file, file,
pack.id + ":scripts/components/" + file.name() pack.id + ":scripts/components/" + file.name()

View File

@ -1,6 +1,7 @@
#include "libentity.hpp" #include "libentity.hpp"
#include "content/Content.hpp" #include "content/Content.hpp"
#include "content/ContentPack.hpp"
#include "engine/Engine.hpp" #include "engine/Engine.hpp"
#include "engine/EnginePaths.hpp" #include "engine/EnginePaths.hpp"
#include "objects/Entities.hpp" #include "objects/Entities.hpp"
@ -236,7 +237,14 @@ static int l_reload_component(lua::State* L) {
} }
auto filename = name.substr(0, pos + 1) + "scripts/components/" + auto filename = name.substr(0, pos + 1) + "scripts/components/" +
name.substr(pos + 1) + ".lua"; name.substr(pos + 1) + ".lua";
scripting::load_entity_component(name, filename, filename); auto prefix = name.substr(0, pos);
auto runtime = content->getPackRuntime(prefix);
if (runtime == nullptr) {
throw std::runtime_error("pack '" + prefix + "' content is not loaded");
}
scripting::load_entity_component(
runtime->getEnvironment(), name, filename, filename
);
return 0; return 0;
} }

View File

@ -708,12 +708,15 @@ void scripting::load_content_script(
} }
void scripting::load_entity_component( void scripting::load_entity_component(
const std::string& name, const io::path& file, const std::string& fileName const scriptenv& env,
const std::string& name,
const io::path& file,
const std::string& fileName
) { ) {
auto L = lua::get_main_state(); auto L = lua::get_main_state();
std::string src = io::read_string(file); std::string src = io::read_string(file);
logger.info() << "script (component) " << file.string(); logger.info() << "script (component) " << file.string();
lua::loadbuffer(L, 0, src, fileName); lua::loadbuffer(L, *env, src, fileName);
lua::store_in(L, lua::CHUNKS_TABLE, name); lua::store_in(L, lua::CHUNKS_TABLE, name);
} }

View File

@ -186,10 +186,12 @@ namespace scripting {
); );
/// @brief Load component script /// @brief Load component script
/// @param env environment
/// @param name component full name (packid:name) /// @param name component full name (packid:name)
/// @param file component script file path /// @param file component script file path
/// @param fileName script file path using the engine format /// @param fileName script file path using the engine format
void load_entity_component( void load_entity_component(
const scriptenv& env,
const std::string& name, const std::string& name,
const io::path& file, const io::path& file,
const std::string& fileName const std::string& fileName