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 SCENE_SRC_SERIALIZATION_SCENE_SER_H
17 #define SCENE_SRC_SERIALIZATION_SCENE_SER_H
18
19 #include <scene/interface/intf_scene.h>
20
21 #include <core/resources/intf_resource_manager.h>
22
23 #include <meta/ext/implementation_macros.h>
24 #include <meta/ext/minimal_object.h>
25 #include <meta/ext/object.h>
26 #include <meta/ext/object_container.h>
27 #include <meta/ext/serialization/serializer.h>
28 #include <meta/interface/serialization/intf_serializable.h>
29
SCENE_BEGIN_NAMESPACE()30 SCENE_BEGIN_NAMESPACE()
31
32 class ISceneNodeSer : public CORE_NS::IInterface {
33 META_INTERFACE(CORE_NS::IInterface, ISceneNodeSer, "183fa566-f548-4257-949f-0ae873785264")
34 public:
35 virtual BASE_NS::string GetName() const = 0;
36 virtual void SetName(BASE_NS::string_view name) = 0;
37 virtual META_NS::ObjectId GetId() const = 0;
38 virtual void SetId(META_NS::ObjectId id) = 0;
39 virtual void SetAttachments(BASE_NS::vector<META_NS::IObject::Ptr> attas) = 0;
40 virtual BASE_NS::vector<META_NS::IObject::Ptr> GetAttachments() const = 0;
41 };
42
43 class IExternalAttachment : public CORE_NS::IInterface {
44 META_INTERFACE(CORE_NS::IInterface, IExternalAttachment, "a0fe2a52-eff5-4ad7-b846-ac438032699f")
45 public:
46 virtual BASE_NS::string GetPath() const = 0;
47 virtual void SetPath(BASE_NS::string_view path) = 0;
48 virtual META_NS::IObject::Ptr GetAttachment() const = 0;
49 virtual void SetAttachment(META_NS::IObject::Ptr) = 0;
50 };
51
52 class ISceneExternalNodeSer : public CORE_NS::IInterface {
53 META_INTERFACE(CORE_NS::IInterface, ISceneExternalNodeSer, "981df612-a670-493b-987a-76180303c888")
54 public:
55 virtual BASE_NS::string GetName() const = 0;
56 virtual void SetName(BASE_NS::string_view name) = 0;
57 virtual CORE_NS::ResourceId GetResourceId() const = 0;
58 virtual void SetResourceId(CORE_NS::ResourceId id) = 0;
59 virtual BASE_NS::vector<IExternalAttachment::Ptr> GetAttachments() const = 0;
60 virtual void AddAttachment(BASE_NS::string_view path, META_NS::IObject::Ptr) = 0;
61 };
62
63 META_REGISTER_CLASS(SceneNodeSer, "f339e04b-317d-4527-8d3c-460467ff43da", META_NS::ObjectCategoryBits::NO_CATEGORY)
64 META_REGISTER_CLASS(
65 ExternalAttachment, "781963d3-85d7-4a79-8a1b-90377d61ef8a", META_NS::ObjectCategoryBits::NO_CATEGORY)
66 META_REGISTER_CLASS(
67 SceneExternalNodeSer, "08c7759d-72f4-40a2-a437-04b00e0dead5", META_NS::ObjectCategoryBits::NO_CATEGORY)
68
69 class SceneNodeSer
70 : public META_NS::IntroduceInterfaces<META_NS::CommonObjectContainerFwd, ISceneNodeSer, META_NS::ISerializable> {
META_OBJECT(SceneNodeSer,ClassId::SceneNodeSer,IntroduceInterfaces)71 META_OBJECT(SceneNodeSer, ClassId::SceneNodeSer, IntroduceInterfaces)
72 public:
73 BASE_NS::string GetName() const override
74 {
75 return name_;
76 }
SetName(BASE_NS::string_view name)77 void SetName(BASE_NS::string_view name) override
78 {
79 name_ = name;
80 }
GetId()81 META_NS::ObjectId GetId() const override
82 {
83 return id_;
84 }
SetId(META_NS::ObjectId id)85 void SetId(META_NS::ObjectId id) override
86 {
87 id_ = id;
88 }
SetAttachments(BASE_NS::vector<META_NS::IObject::Ptr> attas)89 void SetAttachments(BASE_NS::vector<META_NS::IObject::Ptr> attas) override
90 {
91 attas_ = BASE_NS::move(attas);
92 }
GetAttachments()93 BASE_NS::vector<META_NS::IObject::Ptr> GetAttachments() const override
94 {
95 return attas_;
96 }
97
Export(META_NS::IExportContext & c)98 META_NS::ReturnError Export(META_NS::IExportContext& c) const override
99 {
100 // the name is automatically serialised for objects using the GetName in IObject
101 return META_NS::Serializer(c) & META_NS::AutoSerialize() & META_NS::NamedValue("id", id_) &
102 META_NS::NamedValue("attachments", attas_);
103 }
Import(META_NS::IImportContext & c)104 META_NS::ReturnError Import(META_NS::IImportContext& c) override
105 {
106 META_NS::Serializer res(c);
107 if (res & META_NS::AutoSerialize() & META_NS::NamedValue("id", id_) &
108 META_NS::NamedValue("attachments", attas_)) {
109 name_ = c.GetName();
110 }
111 return res;
112 }
113
114 private:
115 BASE_NS::string name_;
116 META_NS::ObjectId id_;
117 BASE_NS::vector<META_NS::IObject::Ptr> attas_;
118 };
119
120 class SceneExternalNodeSer
121 : public META_NS::IntroduceInterfaces<META_NS::MetaObject, ISceneExternalNodeSer, META_NS::ISerializable> {
META_OBJECT(SceneExternalNodeSer,ClassId::SceneExternalNodeSer,IntroduceInterfaces)122 META_OBJECT(SceneExternalNodeSer, ClassId::SceneExternalNodeSer, IntroduceInterfaces)
123 public:
124 BASE_NS::string GetName() const override
125 {
126 return name_;
127 }
SetName(BASE_NS::string_view name)128 void SetName(BASE_NS::string_view name) override
129 {
130 name_ = name;
131 }
GetResourceId()132 CORE_NS::ResourceId GetResourceId() const override
133 {
134 return id_;
135 }
SetResourceId(CORE_NS::ResourceId id)136 void SetResourceId(CORE_NS::ResourceId id) override
137 {
138 id_ = id;
139 }
140
GetAttachments()141 BASE_NS::vector<IExternalAttachment::Ptr> GetAttachments() const override
142 {
143 return attachments_;
144 }
AddAttachment(BASE_NS::string_view path,META_NS::IObject::Ptr obj)145 void AddAttachment(BASE_NS::string_view path, META_NS::IObject::Ptr obj) override
146 {
147 auto ea = META_NS::GetObjectRegistry().Create<IExternalAttachment>(ClassId::ExternalAttachment);
148 if (ea) {
149 ea->SetPath(path);
150 ea->SetAttachment(obj);
151 attachments_.push_back(ea);
152 }
153 }
154
Export(META_NS::IExportContext & c)155 META_NS::ReturnError Export(META_NS::IExportContext& c) const override
156 {
157 // the name is automatically serialised for objects using the GetName in IObject
158 return META_NS::Serializer(c) & META_NS::AutoSerialize() & META_NS::NamedValue("resourceId.name", id_.name) &
159 META_NS::NamedValue("resourceId.group", id_.group) & META_NS::NamedValue("attachments", attachments_);
160 }
Import(META_NS::IImportContext & c)161 META_NS::ReturnError Import(META_NS::IImportContext& c) override
162 {
163 META_NS::Serializer res(c);
164 if (res & META_NS::AutoSerialize() & META_NS::NamedValue("resourceId.name", id_.name) &
165 META_NS::NamedValue("resourceId.group", id_.group) & META_NS::NamedValue("attachments", attachments_)) {
166 name_ = c.GetName();
167 }
168 return res;
169 }
170
171 private:
172 BASE_NS::string name_;
173 CORE_NS::ResourceId id_;
174 BASE_NS::vector<IExternalAttachment::Ptr> attachments_;
175 };
176
177 class ExternalAttachment
178 : public META_NS::IntroduceInterfaces<META_NS::MinimalObject, IExternalAttachment, META_NS::ISerializable> {
META_OBJECT(ExternalAttachment,ClassId::ExternalAttachment,IntroduceInterfaces)179 META_OBJECT(ExternalAttachment, ClassId::ExternalAttachment, IntroduceInterfaces)
180 public:
181 BASE_NS::string GetName() const override
182 {
183 return path_;
184 }
GetPath()185 BASE_NS::string GetPath() const override
186 {
187 return path_;
188 }
SetPath(BASE_NS::string_view path)189 void SetPath(BASE_NS::string_view path) override
190 {
191 path_ = path;
192 }
GetAttachment()193 META_NS::IObject::Ptr GetAttachment() const override
194 {
195 return obj_;
196 }
SetAttachment(META_NS::IObject::Ptr obj)197 void SetAttachment(META_NS::IObject::Ptr obj) override
198 {
199 obj_ = obj;
200 }
Export(META_NS::IExportContext & c)201 META_NS::ReturnError Export(META_NS::IExportContext& c) const override
202 {
203 return META_NS::Serializer(c) & META_NS::NamedValue("object", obj_);
204 }
Import(META_NS::IImportContext & c)205 META_NS::ReturnError Import(META_NS::IImportContext& c) override
206 {
207 META_NS::Serializer res(c);
208 if (res & META_NS::NamedValue("object", obj_)) {
209 path_ = c.GetName();
210 }
211 return res;
212 }
213
214 private:
215 BASE_NS::string path_;
216 META_NS::IObject::Ptr obj_;
217 };
218
219 SCENE_END_NAMESPACE()
220
221 META_INTERFACE_TYPE(SCENE_NS::IExternalAttachment)
222
223 #endif