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