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_ASSETMANAGER_H
17 #define SCENEPLUGIN_ASSETMANAGER_H
18 
19 #include <scene_plugin/interface/intf_asset_loader.h>
20 #include <scene_plugin/interface/intf_asset_manager.h>
21 #include <scene_plugin/interface/intf_entity_collection.h>
22 
23 #include "ecs_serializer.h"
24 
SCENE_BEGIN_NAMESPACE()25 SCENE_BEGIN_NAMESPACE()
26 
27 class AssetManager : public IAssetManager {
28 public:
29     AssetManager(RENDER_NS::IRenderContext& renderContext, CORE3D_NS::IGraphicsContext& graphicsContext);
30     virtual ~AssetManager();
31 
32     //
33     // From IAssetManager
34     //
35     ExtensionType GetExtensionType(BASE_NS::string_view ext) const override;
36 
37     IAssetLoader::Ptr CreateAssetLoader(
38         IEntityCollection& ec, BASE_NS::string_view src, BASE_NS::string_view contextUri) override;
39 
40     bool LoadAsset(IEntityCollection& ec, BASE_NS::string_view uri, BASE_NS::string_view contextUri) override;
41 
42     bool SaveJsonEntityCollection(
43         const IEntityCollection& ec, BASE_NS::string_view uri, BASE_NS::string_view contextUri) const override;
44 
45     IEntityCollection* LoadAssetToCache(CORE_NS::IEcs& ecs, BASE_NS::string_view cacheUri, BASE_NS::string_view uri,
46         BASE_NS::string_view contextUri, bool active, bool forceReload) override;
47 
48     bool IsCachedCollection(BASE_NS::string_view uri, BASE_NS::string_view contextUri) const override;
49     IEntityCollection* CreateCachedCollection(
50         CORE_NS::IEcs& ecs, BASE_NS::string_view uri, BASE_NS::string_view contextUri) override;
51     IEntityCollection* GetCachedCollection(BASE_NS::string_view uri, BASE_NS::string_view contextUri) const override;
52     void RemoveFromCache(BASE_NS::string_view uri, BASE_NS::string_view contextUri) override;
53     void ClearCache() override;
54 
55     void RefreshAsset(IEntityCollection& ec, bool active) override;
56 
57     IEntityCollection* InstantiateCollection(
58         IEntityCollection& ec, BASE_NS::string_view uri, BASE_NS::string_view contextUri) override;
59 
60     IEcsSerializer& GetEcsSerializer() override;
61 
62     //
63     // From IEcsSerializer::IListener
64     //
65     IEntityCollection* GetExternalCollection(
66         CORE_NS::IEcs& ecs, BASE_NS::string_view uri, BASE_NS::string_view contextUri) override;
67 
68 protected:
69     void Destroy() override;
70 
71 private:
72     RENDER_NS::IRenderContext& renderContext_;
73     CORE3D_NS::IGraphicsContext& graphicsContext_;
74     EcsSerializer ecsSerializer_;
75 
76     // Mapping from uri to a collection of entities. The entities live in the ECS but we need to keep track of some kind
77     // of internal ownership of each cached entity.
78     BASE_NS::unordered_map<BASE_NS::string, IEntityCollection::Ptr> cacheCollections_;
79 };
80 
81 SCENE_END_NAMESPACE()
82 
83 #endif // SCENEPLUGIN_ASSETMANAGER_H
84