From aceb90d3f03acf3aeb98e2a6746085dc4676c5b1 Mon Sep 17 00:00:00 2001 From: Rowan Ramsey Date: Mon, 5 Apr 2021 20:02:41 +0200 Subject: [PATCH 01/12] started a simple version of the serialization registry --- legion/engine/core/core.vcxproj | 1 + legion/engine/core/core.vcxproj.filters | 3 +- .../core/scenemanagement/scenemanager.cpp | 6 +- .../serialization/serializationregistry.hpp | 35 +++++ .../core/serialization/serializationutil.hpp | 136 +++++++----------- legion/engine/physics/physics_statics.hpp | 2 +- 6 files changed, 95 insertions(+), 88 deletions(-) create mode 100644 legion/engine/core/serialization/serializationregistry.hpp diff --git a/legion/engine/core/core.vcxproj b/legion/engine/core/core.vcxproj index b90b52552..71f6420b1 100644 --- a/legion/engine/core/core.vcxproj +++ b/legion/engine/core/core.vcxproj @@ -519,6 +519,7 @@ del /q ".\xcopyexclude" + diff --git a/legion/engine/core/core.vcxproj.filters b/legion/engine/core/core.vcxproj.filters index cac60e094..931cf0662 100644 --- a/legion/engine/core/core.vcxproj.filters +++ b/legion/engine/core/core.vcxproj.filters @@ -448,6 +448,7 @@ + @@ -615,4 +616,4 @@ - \ No newline at end of file + diff --git a/legion/engine/core/scenemanagement/scenemanager.cpp b/legion/engine/core/scenemanagement/scenemanager.cpp index 7c937c817..2d58f083c 100644 --- a/legion/engine/core/scenemanagement/scenemanager.cpp +++ b/legion/engine/core/scenemanagement/scenemanager.cpp @@ -21,7 +21,7 @@ namespace legion::core::scenemanagement //if (sceneEntities.size() == 0) //{ // log::debug("Creating a Scene Entity"); - // sceneEntity = m_ecs->createEntity(); + //sceneEntity = m_ecs->createEntity(); // sceneEntity.add_component(); // sceneEntity.add_component(); // std::vector children; @@ -103,7 +103,7 @@ namespace legion::core::scenemanagement //world.write_component(hry); //log::debug("Child Count After: {}", world.child_count()); - //auto sceneEntity = serialization::SerializationUtil::JSONDeserialize(inFile); + //auto sceneEntity = serialization::serializer_base::deserialize(inFile); //currentScene = name; //SceneManager::saveScene(name, sceneEntity); @@ -114,7 +114,7 @@ namespace legion::core::scenemanagement bool SceneManager::save_scene(L_MAYBEUNUSED const std::string& name, L_MAYBEUNUSED ecs::entity& ent) { //std::ofstream outFile("assets/scenes/" + name + ".cornflake"); - //serialization::SerializationUtil::JSONSerialize(outFile, ent); + //serialization::serializer_base::serialize(outFile, ent); return true; } diff --git a/legion/engine/core/serialization/serializationregistry.hpp b/legion/engine/core/serialization/serializationregistry.hpp new file mode 100644 index 000000000..36c6979de --- /dev/null +++ b/legion/engine/core/serialization/serializationregistry.hpp @@ -0,0 +1,35 @@ +#pragma once +#include +#include +#include +#include +#include + +namespace legion::core::serialization +{ + class SerializationRegistry + { + public: + std::map serializers; + + template + void register_component() + { + auto serializer = json_serializer(); + serializers.emplace(type_hash(),serializer); + } + + template + type_hash readtypehash(std::string input) + { + return type_hash(); + } + + template + prototype deserialize(std::string input) + { + type_hash typehash = readtypehash(input); + return serializers[typehash].deserialize>(input); + } + }; +} diff --git a/legion/engine/core/serialization/serializationutil.hpp b/legion/engine/core/serialization/serializationutil.hpp index 67f2e5b25..e224737d5 100644 --- a/legion/engine/core/serialization/serializationutil.hpp +++ b/legion/engine/core/serialization/serializationutil.hpp @@ -1,20 +1,10 @@ #pragma once #include -#include -#include -#include -#include -#include -#include -#include -#include - #include #include #include #include -#include //Some testing objects for serialization @@ -39,7 +29,6 @@ struct MyRecord template void serialize(Archive& ar) { - ar(CEREAL_NVP(x), CEREAL_NVP(y), CEREAL_NVP(z)); } }; @@ -51,113 +40,94 @@ struct Records template void serialize(Archive& ar) { - ar(CEREAL_NVP(records)); } }; #pragma endregion namespace legion::core::serialization { - - class SerializationUtil + struct serializer_base { public: - /**@brief JSON serialization from a stringstream - * @param os stringstream the ouput for the serialized object - * @param serializable template type that represents the object that needs to be serialized - */ - template - static void JSONSerialize(std::stringstream os, T serializable) + template + std::string serialize(type& serializable) { - cereal::JSONOutputArchive archive(os); // Create an output archive, Output as outputing to a string stream - archive(CEREAL_NVP(serializable)); // Read the data into the archive + //todo + return ""; } - /**@brief JSON deserialization from a stringstream - * @param is stringstream the input of a serialized object - * @returns the the deserialized object as type T + /**@brief JSON deserialization from a string + * @param json the input JSON string + * @returns the the deserialized object as type */ - template - static T JSONDeserialize(std::stringstream is) + template + type deserialize(std::string json) { - cereal::JSONInputArchive iarchive(is); // Create an input archive, Input as inputing to memory - T t; - iarchive(t); // Read the data from the archive + //todo + type t; return t; } + }; - /**@brief Binary serialization from a stringstream - * @param os stringstream the ouput for the serialized object + + struct json_serializer : serializer_base + { + public: + json_serializer() = default; + /**@brief JSON serialization to a string * @param serializable template type that represents the object that needs to be serialized */ - template - static void BinarySerialize(std::stringstream os, T serializable) + template + std::string serialize(type& serializable) { - cereal::BinaryOutputArchive archive(os); // Create an output archive, Output as outputing to a string stream - archive(CEREAL_NVP(serializable)); // Read the data into the archive + //todo + return ""; } - /**@brief Binary deserialization from a stringstream - * @param is stringstream the input of a serialized object - * @returns the the deserialized object as type T + /**@brief JSON deserialization from a string + * @param json the input JSON string + * @returns the the deserialized object as type */ - template - static T BinaryDeserialize(std::stringstream is) + template + type deserialize(std::string json) { - cereal::BinaryInputArchive iarchive(is); // Create an input archive, Input as inputing to memory - T t; - iarchive(t); // Read the data from the archive + //todo + type t; return t; } - /**@brief JSON serialization from a filestream - * @param os filestream the ouput for the serialized object - * @param serializable template type that represents the object that needs to be serialized - */ - template - static void JSONSerialize(std::ofstream& os, T serializable) + template + type deserialize(std::ifstream& fstream) { - log::debug("[CEREAL] Started Serializing"); - time::timer timer; - cereal::JSONOutputArchive archive(os);// Create an output archive, Output as outputing to a filestream - archive(cereal::make_nvp(typeid(T).name(), serializable)); // Read the data to the archive - log::debug("[CEREAL] Finished Serializing in : {}s", timer.end().seconds()); - } - - /**@brief JSON deserialization from a filestream - * @param is filestream the input of a serialized object - * @returns the the deserialized object as type T - */ - template - static T JSONDeserialize(std::ifstream& is) - { - cereal::JSONInputArchive iarchive(is); // Create an input archive, Input as inputing to memory - T t; - iarchive(t); // Read the data from the archive + //todo + type t; return t; } + }; - /**@brief Binary serialization from a filestream - * @param os filestream the ouput for the serialized object - * @param serializable template type that represents the object that needs to be serialized - */ - template - static void BinarySerialize(std::ofstream& os, T serializable) + struct binary_serialize : serializer_base + { + public: + binary_serialize() = default; + /**@brief Binary serialization to a string + * @param serializable template type that represents the object that needs to be serialized + */ + template + std::string serialize(type serializable) { - cereal::BinaryOutputArchive archive(os);// Create an output archive, Output as outputing to a string stream - archive(CEREAL_NVP(serializable)); // Read the data to the archive + //todo + return ""; } - /**@brief Binary deserialization from a filestream - * @param is filestream the input of a serialized object - * @returns the the deserialized object as type T + /**@brief Binary deserialization from a string + * @param binary the input Binary string + * @returns the the deserialized object as type */ - template - static T BinaryDeserialize(std::ifstream& is) + template + type deserialize(std::string binary) { - cereal::BinaryInputArchive iarchive(is); // Create an input archive, Input as inputing to memory - T t; - iarchive(t); // Read the data from the archive + //todo + type t; return t; } }; diff --git a/legion/engine/physics/physics_statics.hpp b/legion/engine/physics/physics_statics.hpp index b641fee7e..eaf739876 100644 --- a/legion/engine/physics/physics_statics.hpp +++ b/legion/engine/physics/physics_statics.hpp @@ -486,7 +486,7 @@ namespace legion::physics { con.draw_cells_json("assets/voronoi/output/cells.json"); std::ifstream f("assets/voronoi/output/cells.json"); - return serialization::SerializationUtil::JSONDeserialize< std::vector>>(f); + return serialization::json_serializer>>::deserialize(f); } /**@brief Checks collision between two AABB colliders and returns whether there is collision From 65db2d69fc29ed928d7c7f4976b5f920eafb16b3 Mon Sep 17 00:00:00 2001 From: Glyn Leine Date: Tue, 6 Apr 2021 17:07:30 +0200 Subject: [PATCH 02/12] example files --- .../sandbox/assets/scenes/example.json | 136 ++++++++++++++++++ .../sandbox/assets/scenes/example.yml | 84 +++++++++++ 2 files changed, 220 insertions(+) create mode 100644 applications/sandbox/assets/scenes/example.json create mode 100644 applications/sandbox/assets/scenes/example.yml diff --git a/applications/sandbox/assets/scenes/example.json b/applications/sandbox/assets/scenes/example.json new file mode 100644 index 000000000..28ca8cbe3 --- /dev/null +++ b/applications/sandbox/assets/scenes/example.json @@ -0,0 +1,136 @@ +{ + "name": "Scene1", + "entities": [ + { + "name": "Entity", + "components": [ + { + "type": 17829838403855360956, + "typename": "legion::core::mesh_filter", + "value0": 5503224543670688896, + "debug": false, + "filepath": "assets:\\\\models\\cube.obj" + }, + { + "type": 14566645459790289390, + "typename": "legion::rendering::mesh_renderer", + "materials": [ + { + "src": "assets:\\\\materials\\skybox.material" + } + ] + }, + { + "type": 7976621606494152606, + "typename": "legion::core::scale", + "x": 1.0, + "y": 1.0, + "z": 1.0 + }, + { + "type": 6841436683689612098, + "typename": "legion::core::rotation", + "x": 0.0, + "y": 0.0, + "z": 0.0, + "w": 1.0 + }, + { + "type": 5024733841066084311, + "typename": "legion::core::position", + "x": 0.0, + "y": 0.0, + "z": 0.0 + } + ] + }, + { + "name": "Entity with child", + "components": [ + { + "type": 17829838403855360956, + "typename": "legion::core::mesh_filter", + "value0": 5503224543670688896, + "debug": "false", + "filepath": "assets:\\\\models\\sphere.obj" + }, + { + "type": 14566645459790289390, + "typename": "legion::rendering::mesh_renderer", + "materials": [ + { + "src": "assets:\\\\materials\\default.material" + } + ] + }, + { + "type": 7976621606494152606, + "typename": "legion::core::scale", + "x": 1.0, + "y": 1.0, + "z": 1.0 + }, + { + "type": 6841436683689612098, + "typename": "legion::core::rotation", + "x": 0.0, + "y": 0.0, + "z": 0.0, + "w": 1.0 + }, + { + "type": 5024733841066084311, + "typename": "legion::core::position", + "x": 0.0, + "y": 0.0, + "z": 0.0 + } + ], + "children": [ + { + "name": "Child entity", + "components": [ + { + "type": 17829838403855360956, + "typename": "legion::core::mesh_filter", + "value0": 5503224543670688896, + "debug": false, + "filepath": "assets:\\\\models\\sphere.obj" + }, + { + "type": 14566645459790289390, + "typename": "legion::rendering::mesh_renderer", + "materials": [ + { + "src": "assets:\\\\materials\\default.material" + } + ] + }, + { + "type": 7976621606494152606, + "typename": "legion::core::scale", + "x": 1.0, + "y": 1.0, + "z": 1.0 + }, + { + "type": 6841436683689612098, + "typename": "legion::core::rotation", + "x": 0.0, + "y": 0.0, + "z": 0.0, + "w": 1.0 + }, + { + "type": 5024733841066084311, + "typename": "legion::core::position", + "x": 0.0, + "y": 0.0, + "z": 0.0 + } + ] + } + ] + } + ] +} \ No newline at end of file diff --git a/applications/sandbox/assets/scenes/example.yml b/applications/sandbox/assets/scenes/example.yml new file mode 100644 index 000000000..73049d26c --- /dev/null +++ b/applications/sandbox/assets/scenes/example.yml @@ -0,0 +1,84 @@ +- name: Scene1 + entities: + - name: Entity + components: + - type: 17829838403855360956 + typename: legion::core::mesh_filter + value0: 5503224543670688896 + debug: false + filepath: assets:\\models\cube.obj + - type: 14566645459790289390 + typename: legion::rendering::mesh_renderer + materials: + - src: assets:\\materials\skybox.material + - type: 7976621606494152606 + typename: legion::core::scale + x: 1.0 + y: 1.0 + z: 1.0 + - type: 6841436683689612098 + typename: legion::core::rotation + x: 0.0 + y: 0.0 + z: 0.0 + w: 1.0 + - type: 5024733841066084311 + typename: legion::core::position + x: 0.0 + y: 0.0 + z: 0.0 + - name: Entity with child + components: + - type: 17829838403855360956 + typename: legion::core::mesh_filter + value0: 5503224543670688896 + debug: false + filepath: assets:\\models\sphere.obj + - type: 14566645459790289390 + typename: legion::rendering::mesh_renderer + materials: + - src: assets:\\materials\default.material + - type: 7976621606494152606 + typename: legion::core::scale + x: 1.0 + y: 1.0 + z: 1.0 + - type: 6841436683689612098 + typename: legion::core::rotation + x: 0.0 + y: 0.0 + z: 0.0 + w: 1.0 + - type: 5024733841066084311 + typename: legion::core::position + x: 0.0 + y: 0.0 + z: 0.0 + children: + - name: Child entity + components: + - type: 17829838403855360956 + typename: legion::core::mesh_filter + value0: 5503224543670688896 + debug: false + filepath: assets:\\models\sphere.obj + - type: 14566645459790289390 + typename: legion::rendering::mesh_renderer + materials: + - src: assets:\\materials\default.material + - type: 7976621606494152606 + typename: legion::core::scale + x: 1.0 + y: 1.0 + z: 1.0 + - type: 6841436683689612098 + typename: legion::core::rotation + x: 0.0 + y: 0.0 + z: 0.0 + w: 1.0 + - type: 5024733841066084311 + typename: legion::core::position + x: 0.0 + y: 0.0 + z: 0.0 \ No newline at end of file From f95a951335ed50d67f688866bf1384f568499bca Mon Sep 17 00:00:00 2001 From: Rowan Ramsey Date: Wed, 7 Apr 2021 19:58:02 +0200 Subject: [PATCH 03/12] trying to get it to compile --- applications/sandbox/module/examplemodule.hpp | 1 + .../sandbox/systems/examplesystem.hpp | 197 +++--------------- applications/unit_tests/source.cpp | 7 + .../serialization/serializationregistry.hpp | 5 +- .../core/serialization/serializationutil.hpp | 26 +-- 5 files changed, 39 insertions(+), 197 deletions(-) diff --git a/applications/sandbox/module/examplemodule.hpp b/applications/sandbox/module/examplemodule.hpp index 4da7df983..aebf72800 100644 --- a/applications/sandbox/module/examplemodule.hpp +++ b/applications/sandbox/module/examplemodule.hpp @@ -6,6 +6,7 @@ #include "../systems/simplecameracontroller.hpp" #include "../systems/gui_test.hpp" + class ExampleModule : public legion::Module { public: diff --git a/applications/sandbox/systems/examplesystem.hpp b/applications/sandbox/systems/examplesystem.hpp index e92294123..0bf1611f0 100644 --- a/applications/sandbox/systems/examplesystem.hpp +++ b/applications/sandbox/systems/examplesystem.hpp @@ -1,202 +1,53 @@ #pragma once #include -#include -#include -#include