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 #include "asset_object.h"
16
17 #include <ecs_serializer/api.h>
18 #include <ecs_serializer/intf_ecs_asset_loader.h>
19 #include <ecs_serializer/intf_ecs_asset_manager.h>
20 #include <ecs_serializer/intf_entity_collection.h>
21 #include <scene/ext/intf_ecs_context.h>
22 #include <scene/ext/intf_internal_scene.h>
23
SCENE_BEGIN_NAMESPACE()24 SCENE_BEGIN_NAMESPACE()
25
26 bool AssetObject::Load(const IScene::Ptr& sc, BASE_NS::string_view uri)
27 {
28 using ECS_SERIALIZER_NS::CreateEcsAssetLoader;
29 using ECS_SERIALIZER_NS::CreateEcsAssetManager;
30 using ECS_SERIALIZER_NS::CreateEntityCollection;
31
32 auto scene = sc->GetInternalScene();
33 auto& ecs = scene->GetEcsContext();
34
35 if (!entities_) {
36 entities_ = CreateEntityCollection(*ecs.GetNativeEcs(), "scene", {});
37 if (!entities_) {
38 CORE_LOG_E("Failed to create entity collection");
39 return false;
40 }
41 }
42 auto manager = CreateEcsAssetManager(scene->GetGraphicsContext());
43 if (!manager) {
44 CORE_LOG_E("Failed to create ecs asset manager");
45 return false;
46 }
47 auto loader = CreateEcsAssetLoader(*manager, scene->GetGraphicsContext(), *entities_, uri, {});
48 if (!loader) {
49 CORE_LOG_E("Failed to create ecs asset loader");
50 return false;
51 }
52
53 loader->LoadAsset();
54
55 if (entities_->GetEntityCount() == 0) {
56 CORE_LOG_E("Entity count is zero in loaded scene");
57 return false;
58 }
59
60 if (!ecs.CreateUnnamedRootNode()) {
61 CORE_LOG_E("Failed to create root node");
62 return false;
63 }
64 return true;
65 }
66
67 SCENE_END_NAMESPACE()
68