< 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:28
Uncovered lines:3
Coverable lines:31
Total lines:79
Line coverage:90.3% (28 of 31)
Covered branches:0
Total branches:0
Covered methods:7
Total methods:7
Method coverage:100% (7 of 7)

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.013090%

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
 10015        public Dictionary<object, RefCountedAsset> masterAssets = new Dictionary<object, RefCountedAsset>();
 10016        public Dictionary<AssetType, RefCountedAsset> assetToRefCountedAsset = new Dictionary<AssetType, RefCountedAsset
 17
 18        public override bool Add(AssetType asset)
 19        {
 34320            if (asset == null || masterAssets.ContainsKey(asset.id))
 021                return true;
 22
 34323            masterAssets.Add(asset.id, new RefCountedAsset() { asset = asset });
 34324            assetToRefCountedAsset.Add(asset, masterAssets[asset.id]);
 25
 34326            return true;
 27        }
 28
 29        public override void Cleanup()
 30        {
 426631            foreach (var kvp in masterAssets)
 32            {
 9133                kvp.Value.asset.Cleanup();
 34            }
 35
 204236            masterAssets.Clear();
 204237            assetToRefCountedAsset.Clear();
 204238        }
 39
 172840        public override bool Contains(object id) { return masterAssets.ContainsKey(id); }
 41
 42        public override bool Contains(AssetType asset)
 43        {
 37544            if (asset == null)
 45            {
 446                return false;
 47            }
 48
 37149            bool result = masterAssets.ContainsKey(asset.id);
 50
 37151            return result;
 52        }
 53
 54        public override AssetType Get(object id)
 55        {
 40656            if (!Contains(id))
 057                return null;
 58
 40659            masterAssets[id].referenceCount++;
 40660            return masterAssets[id].asset;
 61        }
 62
 63        public override void Release(AssetType asset)
 64        {
 27865            if (!Contains(asset.id))
 066                return;
 67
 27868            var refCountedAsset = assetToRefCountedAsset[asset];
 27869            refCountedAsset.referenceCount--;
 70
 27871            if (refCountedAsset.referenceCount > 0)
 2772                return;
 73
 25174            asset.Cleanup();
 25175            masterAssets.Remove(asset.id);
 25176            assetToRefCountedAsset.Remove(asset);
 25177        }
 78    }
 79}