< 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        {
 21020            if (asset == null || masterAssets.ContainsKey(asset.id))
 021                return true;
 22
 21023            masterAssets.Add(asset.id, new RefCountedAsset() { asset = asset });
 21024            assetToRefCountedAsset.Add(asset, masterAssets[asset.id]);
 25
 21026            return true;
 27        }
 28
 29        public override void Cleanup()
 30        {
 470231            foreach (var kvp in masterAssets)
 32            {
 8733                kvp.Value.asset.Cleanup();
 34            }
 35
 226436            masterAssets.Clear();
 226437            assetToRefCountedAsset.Clear();
 226438        }
 39
 125640        public override bool Contains(object id) { return masterAssets.ContainsKey(id); }
 41
 42        public override bool Contains(AssetType asset)
 43        {
 27544            if (asset == null)
 45            {
 446                return false;
 47            }
 48
 27149            bool result = masterAssets.ContainsKey(asset.id);
 50
 27151            return result;
 52        }
 53
 54        public override AssetType Get(object id)
 55        {
 26256            if (!Contains(id))
 057                return null;
 58
 26259            masterAssets[id].referenceCount++;
 26260            return masterAssets[id].asset;
 61        }
 62
 63        public override void Release(AssetType asset)
 64        {
 14365            if (!Contains(asset.id))
 066                return;
 67
 14368            var refCountedAsset = assetToRefCountedAsset[asset];
 14369            refCountedAsset.referenceCount--;
 70
 14371            if (refCountedAsset.referenceCount > 0)
 2072                return;
 73
 12374            asset.Cleanup();
 12375            masterAssets.Remove(asset.id);
 12376            assetToRefCountedAsset.Remove(asset);
 12377        }
 78    }
 79}