< Summary

Class:DCL.AssetLibrary_RefCounted[AssetType]
Assembly:AssetPromiseKeeper
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/AssetManager/Common/AssetLibrary_RefCounted.cs
Covered lines:27
Uncovered lines:4
Coverable lines:31
Total lines:79
Line coverage:87% (27 of 31)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
AssetLibrary_RefCounted()0%110100%
Add(...)0%3.073080%
Cleanup()0%220100%
Contains(...)0%110100%
Contains(...)0%220100%
Get(...)0%2.062075%
Release(...)0%3.073080%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/AssetManager/Common/AssetLibrary_RefCounted.cs

#LineLine coverage
 1using System.Collections.Generic;
 2using UnityEngine;
 3
 4namespace DCL
 5{
 6    public class AssetLibrary_RefCounted<AssetType> : AssetLibrary<AssetType>
 7        where AssetType : Asset
 8    {
 9        public class RefCountedAsset
 10        {
 11            public AssetType asset;
 12            public int referenceCount = 0;
 13        }
 14
 2915        public Dictionary<object, RefCountedAsset> masterAssets = new Dictionary<object, RefCountedAsset>();
 2916        public Dictionary<AssetType, RefCountedAsset> assetToRefCountedAsset = new Dictionary<AssetType, RefCountedAsset
 17
 18        public override bool Add(AssetType asset)
 19        {
 8620            if (asset == null || masterAssets.ContainsKey(asset.id))
 021                return true;
 22
 8623            masterAssets.Add(asset.id, new RefCountedAsset() { asset = asset });
 8624            assetToRefCountedAsset.Add(asset, masterAssets[asset.id]);
 25
 8626            return true;
 27        }
 28
 29        public override void Cleanup()
 30        {
 142431            foreach (var kvp in masterAssets)
 32            {
 1133                kvp.Value.asset.Cleanup();
 34            }
 35
 70136            masterAssets.Clear();
 70137            assetToRefCountedAsset.Clear();
 70138        }
 39
 59240        public override bool Contains(object id) { return masterAssets.ContainsKey(id); }
 41
 42        public override bool Contains(AssetType asset)
 43        {
 13744            if (asset == null)
 45            {
 446                return false;
 47            }
 48
 13349            bool result = masterAssets.ContainsKey(asset.id);
 50
 13351            return result;
 52        }
 53
 54        public override AssetType Get(object id)
 55        {
 10856            if (!Contains(id))
 057                return null;
 58
 10859            masterAssets[id].referenceCount++;
 10860            return masterAssets[id].asset;
 61        }
 62
 63        public override void Release(AssetType asset)
 64        {
 7165            if (!Contains(asset.id))
 066                return;
 67
 7168            var refCountedAsset = assetToRefCountedAsset[asset];
 7169            refCountedAsset.referenceCount--;
 70
 7171            if (refCountedAsset.referenceCount > 0)
 072                return;
 73
 7174            asset.Cleanup();
 7175            masterAssets.Remove(asset.id);
 7176            assetToRefCountedAsset.Remove(asset);
 7177        }
 78    }
 79}