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 #include "object_resource.h"
17
18 #include <meta/api/metadata_util.h>
19 #include <meta/ext/serialization/serializer.h>
20 #include <meta/interface/intf_object_registry.h>
21 #include <meta/interface/serialization/intf_exporter.h>
22 #include <meta/interface/serialization/intf_importer.h>
23
24 #include "resource_placeholder.h"
25
META_BEGIN_NAMESPACE()26 META_BEGIN_NAMESPACE()
27
28 BASE_NS::Uid ObjectResource::GetResourceType() const
29 {
30 return type_.ToUid();
31 }
SetResourceType(const ObjectId & id)32 void ObjectResource::SetResourceType(const ObjectId& id)
33 {
34 type_ = id;
35 }
Export(IExportContext & c) const36 ReturnError ObjectResource::Export(IExportContext& c) const
37 {
38 if (SerialiseAsResourceId(c) && c.UserContext() != GetSelf()) {
39 if (!type_.IsValid()) {
40 CORE_LOG_W("Invalid resource type");
41 return GenericError::FAIL;
42 }
43 return ExportResourceId(c);
44 }
45 return Serializer(c) & NamedValue("resourceType", type_) & AutoSerialize();
46 }
Import(IImportContext & c)47 ReturnError ObjectResource::Import(IImportContext& c)
48 {
49 return Serializer(c) & NamedValue("resourceType", type_) & AutoSerialize();
50 }
51
GetResourceName() const52 BASE_NS::string ObjectResourceType::GetResourceName() const
53 {
54 return "ObjectResource";
55 }
GetResourceType() const56 BASE_NS::Uid ObjectResourceType::GetResourceType() const
57 {
58 return type_.ToUid();
59 }
LoadResource(const StorageInfo & s) const60 CORE_NS::IResource::Ptr ObjectResourceType::LoadResource(const StorageInfo& s) const
61 {
62 CORE_NS::IResource::Ptr res;
63 if (s.payload) {
64 if (auto importer = GetObjectRegistry().Create<IFileImporter>(META_NS::ClassId::JsonImporter)) {
65 importer->SetResourceManager(s.self);
66 auto obj = interface_pointer_cast<CORE_NS::IResource>(importer->Import(*s.payload));
67 if (!obj || obj->GetResourceType() != type_.ToUid()) {
68 CORE_LOG_W("Invalid resource");
69 return nullptr;
70 }
71 if (auto i = interface_cast<CORE_NS::ISetResourceId>(obj)) {
72 i->SetResourceId(s.id);
73 }
74 res = interface_pointer_cast<CORE_NS::IResource>(obj);
75 }
76 }
77 return res;
78 }
SaveResource(const CORE_NS::IResource::ConstPtr & p,const StorageInfo & s) const79 bool ObjectResourceType::SaveResource(const CORE_NS::IResource::ConstPtr& p, const StorageInfo& s) const
80 {
81 bool res = true;
82 if (s.payload) {
83 auto i = interface_cast<CORE_NS::IResource>(p);
84 if (!i || i->GetResourceType() != type_.ToUid()) {
85 CORE_LOG_W("Invalid resource");
86 return false;
87 }
88 if (auto exporter = GetObjectRegistry().Create<IFileExporter>(META_NS::ClassId::JsonExporter)) {
89 exporter->SetUserContext(
90 interface_pointer_cast<IObject>(CORE_NS::IResource::Ptr(p, const_cast<CORE_NS::IResource*>(p.get()))));
91 exporter->SetResourceManager(s.self);
92 exporter->SetMetadata(META_NS::SerMetadataValues()
93 .SetVersion({ 1, 0 })
94 .SetType("ObjectResource")
95 .Set("sub-type", type_.ToString()));
96 res = exporter->Export(*s.payload, interface_pointer_cast<IObject>(p));
97 }
98 }
99 return res;
100 }
ReloadResource(const StorageInfo & s,const CORE_NS::IResource::Ptr &) const101 bool ObjectResourceType::ReloadResource(const StorageInfo& s, const CORE_NS::IResource::Ptr&) const
102 {
103 return false;
104 }
SetResourceType(const ObjectId & id)105 void ObjectResourceType::SetResourceType(const ObjectId& id)
106 {
107 type_ = id;
108 }
109
Load(CORE_NS::IFile & options,const CORE_NS::ResourceManagerPtr & rman,const CORE_NS::ResourceContextPtr & context)110 bool ObjectResourceOptions::Load(
111 CORE_NS::IFile& options, const CORE_NS::ResourceManagerPtr& rman, const CORE_NS::ResourceContextPtr& context)
112 {
113 bool res = false;
114 if (auto importer = GetObjectRegistry().Create<IFileImporter>(META_NS::ClassId::JsonImporter)) {
115 importer->SetResourceManager(rman);
116 importer->SetUserContext(interface_pointer_cast<IObject>(context));
117 if (auto obj = importer->Import(options)) {
118 if (auto oro = interface_cast<IObjectResourceOptions>(obj)) {
119 SetBaseResource(oro->GetBaseResource());
120 if (auto in = interface_cast<IMetadata>(obj)) {
121 Clone(*in, *this);
122 res = true;
123 }
124 }
125 }
126 }
127 return res;
128 }
Save(CORE_NS::IFile & options,const CORE_NS::ResourceManagerPtr & rman) const129 bool ObjectResourceOptions::Save(CORE_NS::IFile& options, const CORE_NS::ResourceManagerPtr& rman) const
130 {
131 bool res = false;
132 if (auto exporter = GetObjectRegistry().Create<IFileExporter>(META_NS::ClassId::JsonExporter)) {
133 exporter->SetResourceManager(rman);
134 exporter->SetMetadata(META_NS::SerMetadataValues().SetVersion({ 1, 0 }).SetType("ObjectResourceOptions"));
135 res = exporter->Export(options, GetSelf());
136 }
137 return res;
138 }
GetProperty(BASE_NS::string_view name)139 IProperty::Ptr ObjectResourceOptions::GetProperty(BASE_NS::string_view name)
140 {
141 auto i = GetSelf<META_NS::IMetadata>();
142 return i ? i->GetProperty(name, META_NS::MetadataQuery::EXISTING) : nullptr;
143 }
SetProperty(const IProperty::Ptr & p)144 void ObjectResourceOptions::SetProperty(const IProperty::Ptr& p)
145 {
146 if (auto i = GetSelf<META_NS::IMetadata>()) {
147 if (auto prop = GetProperty(p->GetName())) {
148 i->RemoveProperty(prop);
149 }
150 i->AddProperty(p);
151 }
152 }
Export(META_NS::IExportContext & c) const153 META_NS::ReturnError ObjectResourceOptions::Export(META_NS::IExportContext& c) const
154 {
155 return Serializer(c) & AutoSerialize() & NamedValue("baseResource.name", baseResource_.name) &
156 NamedValue("baseResource.group", baseResource_.group);
157 }
Import(META_NS::IImportContext & c)158 META_NS::ReturnError ObjectResourceOptions::Import(META_NS::IImportContext& c)
159 {
160 return Serializer(c) & AutoSerialize() & NamedValue("baseResource.name", baseResource_.name) &
161 NamedValue("baseResource.group", baseResource_.group);
162 }
163
164 META_END_NAMESPACE()
165