1 /*
2 * Copyright (c) 2024 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #ifndef SCENEPLUGIN_ECSSERIALIZER_H
17 #define SCENEPLUGIN_ECSSERIALIZER_H
18
19 #include <functional>
20
21 #include <base/containers/unique_ptr.h>
22 #include <base/containers/unordered_map.h>
23 #include <base/containers/vector.h>
24 #include <core/property/property.h>
25 #include <render/intf_render_context.h>
26
27 #include <scene_plugin/interface/intf_ecs_serializer.h>
28
SCENE_BEGIN_NAMESPACE()29 SCENE_BEGIN_NAMESPACE()
30
31 class EcsSerializer : public IEcsSerializer {
32 public:
33 EcsSerializer(RENDER_NS::IRenderContext& renderContext);
34 void Destroy() override;
35 //
36 // From IEcsSerializer
37 //
38 void SetListener(IListener* listener) override;
39
40 void SetDefaultSerializers() override;
41 void SetSerializer(const CORE_NS::PropertyTypeDecl& type, IPropertySerializer& serializer) override;
42
43 bool WriteEntityCollection(const IEntityCollection& ec, CORE_NS::json::standalone_value& jsonOut) const override;
44 bool WriteComponents(
45 const IEntityCollection& ec, CORE_NS::Entity entity, CORE_NS::json::standalone_value& jsonOut) const override;
46 bool WriteComponent(const IEntityCollection& ec, CORE_NS::Entity entity, const CORE_NS::IComponentManager& cm,
47 CORE_NS::IComponentManager::ComponentId id, CORE_NS::json::standalone_value& jsonOut) const override;
48 bool WriteProperty(const IEntityCollection& ec, const CORE_NS::Property& property, uintptr_t offset,
49 CORE_NS::json::standalone_value& jsonOut) const override;
50
51 bool GatherExternalCollections(const CORE_NS::json::value& jsonIn, BASE_NS::string_view contextUri,
52 BASE_NS::vector<ExternalCollection>& externalCollectionsOut) const override;
53
54 /*RUNTIME_NS::IIoUtil::SerializationResult*/ int ReadEntityCollection(
55 IEntityCollection& ec, const CORE_NS::json::value& jsonIn, BASE_NS::string_view contextUri) const override;
56 bool ReadComponents(IEntityCollection& ec, const CORE_NS::json::value& jsonIn, bool setId) const override;
57 bool ReadComponent(IEntityCollection& ec, const CORE_NS::json::value& jsonIn, CORE_NS::Entity entity,
58 CORE_NS::IComponentManager& component) const override;
59 bool ReadProperty(IEntityCollection& ec, const CORE_NS::json::value& jsonIn, const CORE_NS::Property& property,
60 uintptr_t offset) const override;
61
62 RENDER_NS::RenderHandleReference LoadImageResource(BASE_NS::string_view uri) const override;
63
64 private:
65 using PropertyToJsonFunc = std::function<bool(
66 const IEntityCollection& ec, const CORE_NS::Property&, uintptr_t, CORE_NS::json::standalone_value&)>;
67 using PropertyFromJsonFunc = std::function<bool(
68 const IEntityCollection& ec, const CORE_NS::json::value&, const CORE_NS::Property&, uintptr_t)>;
69 IPropertySerializer& Add(PropertyToJsonFunc toJson, PropertyFromJsonFunc fromJson);
70
71 //
72 // A little wrapper class to create a serializer with functions for writing and reading a value.
73 //
74 class SimpleJsonSerializer : public IPropertySerializer {
75 public:
76 bool ToJson(const IEntityCollection& ec, const CORE_NS::Property& property, uintptr_t offset,
77 CORE_NS::json::standalone_value& jsonOut) const override;
78 bool FromJson(const IEntityCollection& ec, const CORE_NS::json::value& jsonIn,
79 const CORE_NS::Property& property, uintptr_t offset) const override;
80
81 SimpleJsonSerializer(PropertyToJsonFunc toJson, PropertyFromJsonFunc fromJson);
82 virtual ~SimpleJsonSerializer() = default;
83
84 private:
85 const PropertyToJsonFunc PropertyToJson;
86 const PropertyFromJsonFunc PropertyFromJson;
87 };
88 BASE_NS::vector<BASE_NS::unique_ptr<SimpleJsonSerializer>> ownedSerializers_;
89
90 RENDER_NS::IRenderContext& renderContext_;
91
92 //
93 // Mapping from each property type to a specific serializer.
94 //
95 using SerializerMap = BASE_NS::unordered_map<CORE_NS::PropertyTypeDecl, IPropertySerializer*>;
96 SerializerMap typetoSerializerMap_;
97
98 IListener* listener_ {};
99 };
100
101 SCENE_END_NAMESPACE()
102
103 #endif // SCENEPLUGIN_ECSSERIALIZER_H
104