< Summary

Class:MainScripts.DCL.AssetsEmbedment.Editor.WearablesEmbedment
Assembly:AssetsEmbedmentEditor
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/AssetsEmbedment/Editor/WearablesEmbedment.cs
Covered lines:0
Uncovered lines:43
Coverable lines:43
Total lines:110
Line coverage:0% (0 of 43)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
EmbedBaseWearablesMenuItem()0%2100%
EmbedBaseWearablesAsync()0%90900%
DownloadAndEmbedThumbnailsAsync()0%90900%
DownloadAndEmbedAssetBundlesAsync()0%1561200%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/AssetsEmbedment/Editor/WearablesEmbedment.cs

#LineLine coverage
 1using Cysharp.Threading.Tasks;
 2using DCL.ABConverter;
 3using DCL.EditorEnvironment;
 4using MainScripts.DCL.AssetsEmbedment.Runtime;
 5using System.Collections.Generic;
 6using System.Linq;
 7using UnityEditor;
 8using UnityEngine;
 9using Progress = UnityEditor.Progress;
 10
 11namespace MainScripts.DCL.AssetsEmbedment.Editor
 12{
 13    /// <summary>
 14    /// Fetches Asset Bundles and thumbnails and saves them to the streaming assets folder
 15    /// </summary>
 16    public static class WearablesEmbedment
 17    {
 18        private const string ASSET_BUNDLES_URL_ORG = "https://content-assets-as-bundle.decentraland.org/v23/";
 19        private const string TEXTURES_URL_ORG = "https://interconnected.online/content/contents/";
 20
 21        private static bool inProgress;
 22
 23        [MenuItem("Decentraland/Embed Base Wearables", priority = 2)]
 24        public static void EmbedBaseWearablesMenuItem()
 25        {
 026            EmbedBaseWearablesAsync().Forget();
 027        }
 28
 29        public static async UniTask EmbedBaseWearablesAsync()
 30        {
 031            if (inProgress)
 32            {
 033                Progress.ShowDetails();
 034                return;
 35            }
 36
 037            inProgress = true;
 38
 39            try
 40            {
 041                using var env = EditorEnvironmentSetup.Execute();
 042                var wearables = WearablesCollectionClient.GetBaseWearableCollections();
 43
 044                await UniTask.WhenAll(DownloadAndEmbedThumbnailsAsync(wearables), DownloadAndEmbedAssetBundlesAsync(wear
 45
 046                AssetDatabase.Refresh();
 047            }
 48            finally
 49            {
 050                inProgress = false;
 51            }
 052        }
 53
 54        private static async UniTask DownloadAndEmbedThumbnailsAsync(IReadOnlyList<WearableItem> wearables)
 55        {
 056            var thumbnails = wearables.Select(w => w.thumbnail).ToList();
 057            var thumbnailsCount = thumbnails.Count;
 058            var progressId = Progress.Start("Download Thumbnails");
 059            Progress.ShowDetails(false);
 60
 61            try
 62            {
 063                var results = new Dictionary<string, Texture2D>();
 64
 065                for (var i = 0; i < thumbnailsCount; i++)
 66                {
 067                    string hash = thumbnails[i].Split('/').Last();
 68
 069                    Progress.Report(progressId, i / (float)thumbnailsCount, $"Downloading {hash}");
 070                    await EditorTexturesDownloader.DownloadTexture2DAsync(TEXTURES_URL_ORG, hash, results);
 71                }
 72
 073                await TextureEmbedder.EmbedAsync(results, "");
 074            }
 075            finally { Progress.Remove(progressId); }
 076        }
 77
 78        private static async UniTask DownloadAndEmbedAssetBundlesAsync(IReadOnlyList<WearableItem> wearables)
 79        {
 080            var mainFiles = wearables.SelectMany(w => w.data.representations
 081                                                       .Select(r => r.contents.FirstOrDefault(c => c.key == r.mainFile))
 082                                     .Where(p => p != null)
 83                                     .ToList();
 84
 085            var mainFilesCount = mainFiles.Count;
 86
 087            var loadedData = new Dictionary<string, byte[]>();
 88
 089            var progressId = Progress.Start("Download Asset Bundles");
 090            Progress.ShowDetails(false);
 91
 92            try
 93            {
 094                for (var i = 0; i < mainFilesCount; i++)
 95                {
 096                    var currentProgress = i / (float)mainFilesCount;
 097                    var mainFile = mainFiles[i];
 098                    Progress.Report(progressId, currentProgress, $"Downloading {mainFile.key} {mainFile.hash}");
 99
 0100                    await EditorAssetBundlesDownloader.DownloadAssetBundleWithDependenciesAsync(ASSET_BUNDLES_URL_ORG, m
 101
 102                    // don't need to handle 'success': such situation should not be: `GetAwaiter` throws `UnityWebReques
 0103                    await AssetBundleEmbedder.EmbedAsync(loadedData, EmbeddedWearablesPath.VALUE);
 0104                    await UniTask.SwitchToMainThread();
 105                }
 0106            }
 0107            finally { Progress.Remove(progressId); }
 0108        }
 109    }
 110}