< 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

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
 9915        public Dictionary<object, RefCountedAsset> masterAssets = new Dictionary<object, RefCountedAsset>();
 9916        public Dictionary<AssetType, RefCountedAsset> assetToRefCountedAsset = new Dictionary<AssetType, RefCountedAsset
 17
 18        public override bool Add(AssetType asset)
 19        {
 18120            if (asset == null || masterAssets.ContainsKey(asset.id))
 021                return true;
 22
 18123            masterAssets.Add(asset.id, new RefCountedAsset() { asset = asset });
 18124            assetToRefCountedAsset.Add(asset, masterAssets[asset.id]);
 25
 18126            return true;
 27        }
 28
 29        public override void Cleanup()
 30        {
 648831            foreach (var kvp in masterAssets)
 32            {
 8133                kvp.Value.asset.Cleanup();
 34            }
 35
 316336            masterAssets.Clear();
 316337            assetToRefCountedAsset.Clear();
 316338        }
 39
 118040        public override bool Contains(object id) { return masterAssets.ContainsKey(id); }
 41
 42        public override bool Contains(AssetType asset)
 43        {
 26644            if (asset == null)
 45            {
 446                return false;
 47            }
 48
 26249            bool result = masterAssets.ContainsKey(asset.id);
 50
 26251            return result;
 52        }
 53
 54        public override AssetType Get(object id)
 55        {
 23056            if (!Contains(id))
 057                return null;
 58
 23059            masterAssets[id].referenceCount++;
 23060            return masterAssets[id].asset;
 61        }
 62
 63        public override void Release(AssetType asset)
 64        {
 11765            if (!Contains(asset.id))
 066                return;
 67
 11768            var refCountedAsset = assetToRefCountedAsset[asset];
 11769            refCountedAsset.referenceCount--;
 70
 11771            if (refCountedAsset.referenceCount > 0)
 1772                return;
 73
 10074            asset.Cleanup();
 10075            masterAssets.Remove(asset.id);
 10076            assetToRefCountedAsset.Remove(asset);
 10077        }
 78    }
 79}