Skip to content

Commit

Permalink
Merge pull request #6 from nodec-project/feature/nodec-game-engine-is…
Browse files Browse the repository at this point in the history
…sue74

feat: Add make make serializable component from runtime component, an…
  • Loading branch information
ContentsViewer authored Jul 15, 2024
2 parents bf89095 + 86078d2 commit ef48866
Showing 1 changed file with 74 additions and 7 deletions.
81 changes: 74 additions & 7 deletions include/nodec_scene_serialization/scene_serialization.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,11 @@ class SceneSerialization final {
return serializable_component_type_info_;
}

virtual std::unique_ptr<BaseSerializableComponent> serialize(const void *component) const = 0;
virtual void emplace_component(const BaseSerializableComponent *, SceneEntity, SceneRegistry &) const = 0;
virtual void emplace_or_replace_component(const BaseSerializableComponent *, SceneEntity, SceneRegistry &) const = 0;
virtual std::unique_ptr<BaseSerializableComponent> make_serializable_component() const = 0;
virtual std::unique_ptr<BaseSerializableComponent> make_serializable_component(const void *component) const = 0;
virtual void assign_component(const BaseSerializableComponent *, void *) const = 0;

private:
nodec::type_info component_type_info_;
Expand All @@ -53,26 +55,47 @@ class SceneSerialization final {
ComponentSerialization()
: BaseComponentSerialization(nodec::type_id<Component>(), nodec::type_id<SerializableComponent>()) {}

std::unique_ptr<BaseSerializableComponent> serialize(const void *component) const override {
std::unique_ptr<BaseSerializableComponent> make_serializable_component(const void *component) const override {
assert(component);

return serializer(*static_cast<const Component *>(component));
}

void emplace_component(const BaseSerializableComponent *serializable_component, SceneEntity entity, SceneRegistry &scene_registry) const override {
assert(serializable_component);
assert(serializable_component->type_info() == nodec::type_id<SerializableComponent>());
void emplace_component(const BaseSerializableComponent *source,
SceneEntity entity, SceneRegistry &scene_registry) const override {
assert(source);
assert(source->type_info() == nodec::type_id<SerializableComponent>());

auto result = scene_registry.emplace_component<Component>(entity);
if (!result.second) {
// If the component is already existing, will not overwrite it.
return;
}

auto comp = deserializer(*static_cast<const SerializableComponent *>(serializable_component));
auto comp = deserializer(*static_cast<const SerializableComponent *>(source));
result.first = std::move(comp);
}

void emplace_or_replace_component(const BaseSerializableComponent *source,
SceneEntity entity, SceneRegistry &scene_registry) const override {
assert(source);
assert(source->type_info() == nodec::type_id<SerializableComponent>());

auto result = scene_registry.emplace_component<Component>(entity);
auto comp = deserializer(*static_cast<const SerializableComponent *>(source));
result.first = std::move(comp);
}

void assign_component(const BaseSerializableComponent *source,
void *target_component) const override {
assert(source);
assert(source->type_info() == nodec::type_id<SerializableComponent>());
assert(target_component);

auto comp = deserializer(*static_cast<const SerializableComponent *>(source));
*static_cast<Component *>(target_component) = std::move(comp);
}

std::unique_ptr<BaseSerializableComponent> make_serializable_component() const override {
return std::make_unique<SerializableComponent>();
}
Expand Down Expand Up @@ -151,7 +174,7 @@ class SceneSerialization final {
auto &serialization = iter->second;
assert(static_cast<bool>(serialization));

auto serializable_component = serialization->serialize(comp);
auto serializable_component = serialization->make_serializable_component(comp);
serializable_entity->components.push_back(std::move(serializable_component));
});

Expand Down Expand Up @@ -208,6 +231,34 @@ class SceneSerialization final {
}
}

void emplace_or_replace_component(const BaseSerializableComponent *source,
const nodec_scene::SceneEntity &target,
nodec_scene::SceneRegistry &scene_registry) const {
if (!source) return;

auto iter = serializable_component_dict_.find(source->type_info().seq_index());
if (iter == serializable_component_dict_.end()) return;

auto &serialization = iter->second;
assert(static_cast<bool>(serialization));

serialization->emplace_or_replace_component(source, target, scene_registry);
}

void assign_component(const BaseSerializableComponent *source,
const nodec::type_info &target_component_type_info,
void *target_component) const {
if (!source) return;

auto iter = serializable_component_dict_.find(source->type_info().seq_index());
if (iter == serializable_component_dict_.end()) return;

auto &serialization = iter->second;
if (serialization->type_info() != target_component_type_info) return;

serialization->assign_component(source, target_component);
}

/**
* @brief Makes the serializable component resolved from the given runtime component type.
*/
Expand All @@ -218,6 +269,22 @@ class SceneSerialization final {
return iter->second->make_serializable_component();
}

/**
* @brief Makes the serializable component resolved from the given runtime component.
*
* @param type_info
* @param component
* @return std::unique_ptr<BaseSerializableComponent>
*/
std::unique_ptr<BaseSerializableComponent>
make_serializable_component(
const nodec::type_info &type_info, void *component) const {
auto iter = component_dict_.find(type_info.seq_index());
if (iter == component_dict_.end()) return nullptr;

return iter->second->make_serializable_component(component);
}

/**
* @brief Gets the type info of runtime component from the given serializable component type info.
*/
Expand Down

0 comments on commit ef48866

Please sign in to comment.