< Summary

Class:FacialFeatureController
Assembly:AvatarShape
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Components/Avatar/FacialFeatureController.cs
Covered lines:41
Uncovered lines:21
Coverable lines:62
Total lines:133
Line coverage:66.1% (41 of 62)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
FacialFeatureController(...)0%110100%
Load(...)0%2.092071.43%
PrepareWearable()0%6.296080%
FetchTextures()0%29.4329092%
CleanUp()0%12300%
CreateDefaultFacialFeature(...)0%6200%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Components/Avatar/FacialFeatureController.cs

#LineLine coverage
 1using DCL.Helpers;
 2using System.Collections;
 3using System.Linq;
 4using UnityEngine;
 5using DCL;
 6
 7public class FacialFeatureController
 8{
 09    public bool isReady { get; private set; }
 010    public string wearableId => wearableItem?.id;
 11
 12    private readonly WearableItem wearableItem;
 13    private readonly Material baseMaterial;
 14
 15    internal Texture mainTexture = null;
 16    internal Texture maskTexture = null;
 17    internal AssetPromise_Texture mainTexturePromise = null;
 18    internal AssetPromise_Texture maskTexturePromise = null;
 19
 20    private Color color;
 21    private IBodyShapeController bodyShape;
 22    internal Material baseMaterialCopy;
 23
 424    public FacialFeatureController(WearableItem wearableItem, Material baseMaterial)
 25    {
 426        isReady = false;
 427        this.baseMaterial = baseMaterial;
 428        this.wearableItem = wearableItem;
 429    }
 30
 31    public void Load(IBodyShapeController loadedBody, Color color)
 32    {
 433        this.color = color;
 34
 435        if (isReady)
 36        {
 037            PrepareWearable();
 038            return;
 39        }
 40
 441        this.bodyShape = loadedBody;
 442        CoroutineStarter.Start(FetchTextures());
 443    }
 44
 45    void PrepareWearable()
 46    {
 447        if (baseMaterialCopy == null)
 448            baseMaterialCopy = new Material(baseMaterial);
 49
 450        switch (wearableItem.data.category)
 51        {
 52            case WearableLiterals.Categories.EYES:
 353                bodyShape.SetupEyes(baseMaterialCopy, mainTexture, maskTexture, color);
 354                break;
 55            case WearableLiterals.Categories.EYEBROWS:
 056                bodyShape.SetupEyebrows(baseMaterialCopy, mainTexture, color);
 057                break;
 58            case WearableLiterals.Categories.MOUTH:
 159                bodyShape.SetupMouth(baseMaterialCopy, mainTexture, maskTexture, color);
 60                break;
 61        }
 62
 463        isReady = true;
 464    }
 65
 66    public IEnumerator FetchTextures()
 67    {
 468        if (mainTexturePromise != null)
 069            AssetPromiseKeeper_Texture.i.Forget(mainTexturePromise);
 70
 471        if (maskTexturePromise != null)
 072            AssetPromiseKeeper_Texture.i.Forget(maskTexturePromise);
 73
 474        mainTexture = null;
 475        maskTexture = null;
 76
 477        var representation = wearableItem.GetRepresentation(bodyShape.bodyShapeId);
 78
 879        string mainTextureHash = representation?.contents?.FirstOrDefault(x => x.key == representation?.mainFile)?.hash;
 480        if (mainTextureHash == null)
 381            mainTextureHash = representation?.contents?.FirstOrDefault(x => !x.key.ToLower().Contains("_mask.png"))?.has
 1082        string maskhash = representation?.contents?.FirstOrDefault(x => x.key.ToLower().Contains("_mask.png"))?.hash;
 83
 484        if (!string.IsNullOrEmpty(mainTextureHash))
 85        {
 386            mainTexturePromise = new AssetPromise_Texture(wearableItem.baseUrl + mainTextureHash);
 587            mainTexturePromise.OnSuccessEvent += (x) => mainTexture = x.texture;
 488            mainTexturePromise.OnFailEvent += (x) => mainTexture = null;
 89
 390            AssetPromiseKeeper_Texture.i.Keep(mainTexturePromise);
 91        }
 92
 493        if (!string.IsNullOrEmpty(maskhash))
 94        {
 395            maskTexturePromise = new AssetPromise_Texture(wearableItem.baseUrl + maskhash);
 596            maskTexturePromise.OnSuccessEvent += (x) => maskTexture = x.texture;
 497            maskTexturePromise.OnFailEvent += (x) => maskTexture = null;
 98
 399            AssetPromiseKeeper_Texture.i.Keep(maskTexturePromise);
 100        }
 101
 4102        yield return mainTexturePromise;
 4103        yield return maskTexturePromise;
 104
 4105        PrepareWearable();
 4106    }
 107
 108    public void CleanUp()
 109    {
 0110        if (mainTexturePromise != null)
 0111            AssetPromiseKeeper_Texture.i.Forget(mainTexturePromise);
 112
 0113        if (maskTexturePromise != null)
 0114            AssetPromiseKeeper_Texture.i.Forget(maskTexturePromise);
 115
 0116        Object.Destroy(baseMaterialCopy);
 117
 0118        isReady = false;
 0119    }
 120
 121    public static FacialFeatureController CreateDefaultFacialFeature(string bodyShape, string category, Material materia
 122    {
 0123        string defaultId = WearableLiterals.DefaultWearables.GetDefaultWearable(bodyShape, category);
 0124        CatalogController.wearableCatalog.TryGetValue(defaultId, out WearableItem wearable);
 0125        if (wearable == null)
 126        {
 0127            Debug.LogError($"Couldn't resolve wearable {defaultId}");
 0128            return null;
 129        }
 130
 0131        return new FacialFeatureController(wearable, material);
 132    }
 133}