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_INTF_ECSSERIALIZER_H
17 #define SCENEPLUGIN_INTF_ECSSERIALIZER_H
18
19 #include <scene_plugin/interface/intf_entity_collection.h>
20 #include <scene_plugin/namespace.h>
21
22 #include <base/containers/string_view.h>
23 #include <base/containers/unique_ptr.h>
24 #include <core/ecs/intf_component_manager.h>
25 #include <core/ecs/intf_ecs.h>
26 #include <core/json/json.h>
27 #include <core/property/intf_property_api.h>
28 #include <core/property/scoped_handle.h>
29 #include <render/resource_handle.h>
30
SCENE_BEGIN_NAMESPACE()31 SCENE_BEGIN_NAMESPACE()
32
33 class IEcsSerializer {
34 public:
35 static const uint32_t VERSION_MAJOR { 22u };
36 static const uint32_t VERSION_MINOR { 0u };
37
38 struct EntityInfo {
39 CORE_NS::Entity entity;
40 BASE_NS::string srcUri;
41 BASE_NS::string contextUri;
42 };
43
44 struct ExternalCollection {
45 BASE_NS::string src;
46 BASE_NS::string contextUri;
47 };
48
49 class IListener {
50 public:
51 virtual IEntityCollection* GetExternalCollection(
52 CORE_NS::IEcs& ecs, BASE_NS::string_view uri, BASE_NS::string_view contextUri) = 0;
53
54 protected:
55 virtual ~IListener() = default;
56 };
57
58 class IPropertySerializer {
59 public:
60 virtual bool ToJson(const IEntityCollection& ec, const CORE_NS::Property& property, uintptr_t offset,
61 CORE_NS::json::standalone_value& jsonOut) const = 0;
62 virtual bool FromJson(const IEntityCollection& ec, const CORE_NS::json::value& jsonIn,
63 const CORE_NS::Property& property, uintptr_t offset) const = 0;
64
65 protected:
66 virtual ~IPropertySerializer() = default;
67 };
68
69 virtual void SetListener(IListener* listener) = 0;
70
71 virtual void SetDefaultSerializers() = 0;
72 virtual void SetSerializer(const CORE_NS::PropertyTypeDecl& type, IPropertySerializer& serializer) = 0;
73
74 virtual bool WriteEntityCollection(const IEntityCollection& ec, CORE_NS::json::standalone_value& jsonOut) const = 0;
75 virtual bool WriteComponents(
76 const IEntityCollection& ec, CORE_NS::Entity entity, CORE_NS::json::standalone_value& jsonOut) const = 0;
77 virtual bool WriteComponent(const IEntityCollection& ec, CORE_NS::Entity entity,
78 const CORE_NS::IComponentManager& cm, CORE_NS::IComponentManager::ComponentId id,
79 CORE_NS::json::standalone_value& jsonOut) const = 0;
80 virtual bool WriteProperty(const IEntityCollection& ec, const CORE_NS::Property& property, uintptr_t offset,
81 CORE_NS::json::standalone_value& jsonOut) const = 0;
82
83 virtual bool GatherExternalCollections(const CORE_NS::json::value& jsonIn, BASE_NS::string_view contextUri,
84 BASE_NS::vector<ExternalCollection>& externalCollectionsOut) const = 0;
85
86 virtual int /*IIoUtil::SerializationResult*/ ReadEntityCollection(
87 IEntityCollection& ec, const CORE_NS::json::value& jsonIn, BASE_NS::string_view contextUri) const = 0;
88 virtual bool ReadComponents(IEntityCollection& ec, const CORE_NS::json::value& jsonIn, bool setId) const = 0;
89 virtual bool ReadComponent(IEntityCollection& ec, const CORE_NS::json::value& jsonIn, CORE_NS::Entity entity,
90 CORE_NS::IComponentManager& component) const = 0;
91 virtual bool ReadProperty(IEntityCollection& ec, const CORE_NS::json::value& jsonIn,
92 const CORE_NS::Property& property, uintptr_t offset) const = 0;
93
94 virtual RENDER_NS::RenderHandleReference LoadImageResource(BASE_NS::string_view uri) const = 0;
95
96 struct Deleter {
97 constexpr Deleter() noexcept = default;
98 void operator()(IEcsSerializer* ptr) const
99 {
100 ptr->Destroy();
101 }
102 };
103 using Ptr = BASE_NS::unique_ptr<IEcsSerializer, Deleter>;
104
105 protected:
106 IEcsSerializer() = default;
107 virtual ~IEcsSerializer() = default;
108 virtual void Destroy() = 0;
109 };
110
111 SCENE_END_NAMESPACE()
112
113 #endif // SCENEPLUGIN_INTF_SCENESERIALIZER_H
114