mirror of
https://github.com/MihailRis/voxelcore.git
synced 2026-10-11 14:01:49 +00:00
commit
4a174d079a
5 changed files with 29 additions and 3 deletions
|
|
@ -188,6 +188,7 @@ public:
|
||||||
ContentUnitDefs<GeneratorDef> generators;
|
ContentUnitDefs<GeneratorDef> generators;
|
||||||
std::unique_ptr<DrawGroups> const drawGroups;
|
std::unique_ptr<DrawGroups> const drawGroups;
|
||||||
ResourceIndicesSet resourceIndices {};
|
ResourceIndicesSet resourceIndices {};
|
||||||
|
std::set<std::string> components {};
|
||||||
|
|
||||||
Content(
|
Content(
|
||||||
std::unique_ptr<ContentIndices> indices,
|
std::unique_ptr<ContentIndices> indices,
|
||||||
|
|
|
||||||
|
|
@ -506,6 +506,7 @@ void ContentLoader::loadScripts(Content& content) {
|
||||||
scripting::on_scripts_loading();
|
scripting::on_scripts_loading();
|
||||||
load_scripts(content, content.blocks);
|
load_scripts(content, content.blocks);
|
||||||
load_scripts(content, content.items);
|
load_scripts(content, content.items);
|
||||||
|
auto& tmpContent = content;
|
||||||
|
|
||||||
for (const auto& [packid, runtime] : content.getPacks()) {
|
for (const auto& [packid, runtime] : content.getPacks()) {
|
||||||
auto env = runtime->getEnvironment();
|
auto env = runtime->getEnvironment();
|
||||||
|
|
@ -516,7 +517,7 @@ 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, env](const io::path& file) {
|
foreach_file(componentsDir, [&pack, env, &tmpContent](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,
|
env,
|
||||||
|
|
@ -524,9 +525,21 @@ void ContentLoader::loadScripts(Content& content) {
|
||||||
file,
|
file,
|
||||||
pack.id + ":scripts/components/" + file.name()
|
pack.id + ":scripts/components/" + file.name()
|
||||||
);
|
);
|
||||||
|
tmpContent.components.insert(name);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
for (const auto& [eid, def] : content.entities.getDefs()) {
|
||||||
|
for (const auto& instance : def->components) {
|
||||||
|
if (content.components.find(instance.component) == content.components.end()) {
|
||||||
|
throw std::runtime_error(
|
||||||
|
"component " + instance.component +
|
||||||
|
" is not available (required by entity " + eid + ")"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
scripting::on_content_loaded();
|
scripting::on_content_loaded();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -744,9 +744,10 @@ void scripting::load_entity_component(
|
||||||
const io::path& file,
|
const io::path& file,
|
||||||
const std::string& fileName
|
const std::string& fileName
|
||||||
) {
|
) {
|
||||||
|
logger.info() << "script (component) " << file.string();
|
||||||
|
|
||||||
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();
|
|
||||||
lua::loadbuffer(L, *env, src, fileName);
|
lua::loadbuffer(L, *env, src, fileName);
|
||||||
lua::store_in_registry(L, lua::CHUNKS_TABLE, name);
|
lua::store_in_registry(L, lua::CHUNKS_TABLE, name);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -148,7 +148,12 @@ void scripting::on_entity_spawn(
|
||||||
lua::call(L, 1);
|
lua::call(L, 1);
|
||||||
}
|
}
|
||||||
for (auto& component : components) {
|
for (auto& component : components) {
|
||||||
create_component(L, -1, *component, args, saved);
|
try {
|
||||||
|
create_component(L, -1, *component, args, saved);
|
||||||
|
} catch (const std::runtime_error& err) {
|
||||||
|
logger.error() << "could not to initialize user component "
|
||||||
|
<< component->name << ": " << err.what();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for (auto& [packid, pack] : content->getPacks()) {
|
for (auto& [packid, pack] : content->getPacks()) {
|
||||||
|
|
@ -166,6 +171,9 @@ static void process_entity_callback(
|
||||||
const std::string& name,
|
const std::string& name,
|
||||||
std::function<int(lua::State*)> args
|
std::function<int(lua::State*)> args
|
||||||
) {
|
) {
|
||||||
|
if (env == nullptr) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
auto L = lua::get_main_state();
|
auto L = lua::get_main_state();
|
||||||
lua::pushenv(L, *env);
|
lua::pushenv(L, *env);
|
||||||
if (lua::hasfield(L, "__disabled")) {
|
if (lua::hasfield(L, "__disabled")) {
|
||||||
|
|
|
||||||
|
|
@ -71,6 +71,9 @@ dv::value Entity::serialize() const {
|
||||||
if (!scripts.components.empty()) {
|
if (!scripts.components.empty()) {
|
||||||
auto& compsMap = root.object("comps");
|
auto& compsMap = root.object("comps");
|
||||||
for (auto& comp : scripts.components) {
|
for (auto& comp : scripts.components) {
|
||||||
|
if (comp->env == nullptr) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
auto data =
|
auto data =
|
||||||
scripting::get_component_value(comp->env, SAVED_DATA_VARNAME);
|
scripting::get_component_value(comp->env, SAVED_DATA_VARNAME);
|
||||||
compsMap[comp->name] = data;
|
compsMap[comp->name] = data;
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue