| | 1 | | using Cysharp.Threading.Tasks; |
| | 2 | | using DCL.ABConverter; |
| | 3 | | using DCL.EditorEnvironment; |
| | 4 | | using MainScripts.DCL.AssetsEmbedment.Runtime; |
| | 5 | | using System.Collections.Generic; |
| | 6 | | using System.Linq; |
| | 7 | | using UnityEditor; |
| | 8 | | using UnityEngine; |
| | 9 | | using Progress = UnityEditor.Progress; |
| | 10 | |
|
| | 11 | | namespace 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 | | { |
| 0 | 26 | | EmbedBaseWearablesAsync().Forget(); |
| 0 | 27 | | } |
| | 28 | |
|
| | 29 | | public static async UniTask EmbedBaseWearablesAsync() |
| | 30 | | { |
| 0 | 31 | | if (inProgress) |
| | 32 | | { |
| 0 | 33 | | Progress.ShowDetails(); |
| 0 | 34 | | return; |
| | 35 | | } |
| | 36 | |
|
| 0 | 37 | | inProgress = true; |
| | 38 | |
|
| | 39 | | try |
| | 40 | | { |
| 0 | 41 | | using var env = EditorEnvironmentSetup.Execute(); |
| 0 | 42 | | var wearables = WearablesCollectionClient.GetBaseWearableCollections(); |
| | 43 | |
|
| 0 | 44 | | await UniTask.WhenAll(DownloadAndEmbedThumbnailsAsync(wearables), DownloadAndEmbedAssetBundlesAsync(wear |
| | 45 | |
|
| 0 | 46 | | AssetDatabase.Refresh(); |
| 0 | 47 | | } |
| | 48 | | finally |
| | 49 | | { |
| 0 | 50 | | inProgress = false; |
| | 51 | | } |
| 0 | 52 | | } |
| | 53 | |
|
| | 54 | | private static async UniTask DownloadAndEmbedThumbnailsAsync(IReadOnlyList<WearableItem> wearables) |
| | 55 | | { |
| 0 | 56 | | var thumbnails = wearables.Select(w => w.thumbnail).ToList(); |
| 0 | 57 | | var thumbnailsCount = thumbnails.Count; |
| 0 | 58 | | var progressId = Progress.Start("Download Thumbnails"); |
| 0 | 59 | | Progress.ShowDetails(false); |
| | 60 | |
|
| | 61 | | try |
| | 62 | | { |
| 0 | 63 | | var results = new Dictionary<string, Texture2D>(); |
| | 64 | |
|
| 0 | 65 | | for (var i = 0; i < thumbnailsCount; i++) |
| | 66 | | { |
| 0 | 67 | | string hash = thumbnails[i].Split('/').Last(); |
| | 68 | |
|
| 0 | 69 | | Progress.Report(progressId, i / (float)thumbnailsCount, $"Downloading {hash}"); |
| 0 | 70 | | await EditorTexturesDownloader.DownloadTexture2DAsync(TEXTURES_URL_ORG, hash, results); |
| | 71 | | } |
| | 72 | |
|
| 0 | 73 | | await TextureEmbedder.EmbedAsync(results, ""); |
| 0 | 74 | | } |
| 0 | 75 | | finally { Progress.Remove(progressId); } |
| 0 | 76 | | } |
| | 77 | |
|
| | 78 | | private static async UniTask DownloadAndEmbedAssetBundlesAsync(IReadOnlyList<WearableItem> wearables) |
| | 79 | | { |
| 0 | 80 | | var mainFiles = wearables.SelectMany(w => w.data.representations |
| 0 | 81 | | .Select(r => r.contents.FirstOrDefault(c => c.key == r.mainFile)) |
| 0 | 82 | | .Where(p => p != null) |
| | 83 | | .ToList(); |
| | 84 | |
|
| 0 | 85 | | var mainFilesCount = mainFiles.Count; |
| | 86 | |
|
| 0 | 87 | | var loadedData = new Dictionary<string, byte[]>(); |
| | 88 | |
|
| 0 | 89 | | var progressId = Progress.Start("Download Asset Bundles"); |
| 0 | 90 | | Progress.ShowDetails(false); |
| | 91 | |
|
| | 92 | | try |
| | 93 | | { |
| 0 | 94 | | for (var i = 0; i < mainFilesCount; i++) |
| | 95 | | { |
| 0 | 96 | | var currentProgress = i / (float)mainFilesCount; |
| 0 | 97 | | var mainFile = mainFiles[i]; |
| 0 | 98 | | Progress.Report(progressId, currentProgress, $"Downloading {mainFile.key} {mainFile.hash}"); |
| | 99 | |
|
| 0 | 100 | | await EditorAssetBundlesDownloader.DownloadAssetBundleWithDependenciesAsync(ASSET_BUNDLES_URL_ORG, m |
| | 101 | |
|
| | 102 | | // don't need to handle 'success': such situation should not be: `GetAwaiter` throws `UnityWebReques |
| 0 | 103 | | await AssetBundleEmbedder.EmbedAsync(loadedData, EmbeddedWearablesPath.VALUE); |
| 0 | 104 | | await UniTask.SwitchToMainThread(); |
| | 105 | | } |
| 0 | 106 | | } |
| 0 | 107 | | finally { Progress.Remove(progressId); } |
| 0 | 108 | | } |
| | 109 | | } |
| | 110 | | } |