| | 1 | | using Cysharp.Threading.Tasks; |
| | 2 | | using MainScripts.DCL.Controllers.AssetManager.AssetBundles.SceneAB; |
| | 3 | | using System; |
| | 4 | | using System.Collections; |
| | 5 | | using System.Collections.Generic; |
| | 6 | | using UnityEngine; |
| | 7 | | using UnityEngine.Assertions; |
| | 8 | |
|
| | 9 | | namespace DCL.Components |
| | 10 | | { |
| | 11 | | public class RendereableAssetLoadHelper |
| | 12 | | { |
| | 13 | | private const string NEW_CDN_FF = "ab-new-cdn"; |
| | 14 | |
|
| | 15 | | public event Action<Rendereable> OnSuccessEvent; |
| | 16 | | public event Action<Exception> OnFailEvent; |
| | 17 | |
|
| | 18 | | public enum LoadingType |
| | 19 | | { |
| | 20 | | ASSET_BUNDLE_WITH_GLTF_FALLBACK, |
| | 21 | | ASSET_BUNDLE_ONLY, |
| | 22 | | GLTF_ONLY, |
| | 23 | | DEFAULT |
| | 24 | | } |
| | 25 | |
|
| | 26 | | private const string AB_GO_NAME_PREFIX = "AB:"; |
| | 27 | | private const string GLTFAST_GO_NAME_PREFIX = "GLTFast:"; |
| | 28 | | private const string FROM_ASSET_BUNDLE_TAG = "FromAssetBundle"; |
| | 29 | | private const string FROM_RAW_GLTF_TAG = "FromRawGLTF"; |
| | 30 | |
|
| | 31 | | public static bool VERBOSE = false; |
| | 32 | | public static bool useCustomContentServerUrl = false; |
| | 33 | | public static string customContentServerUrl; |
| | 34 | | public static LoadingType defaultLoadingType = LoadingType.ASSET_BUNDLE_WITH_GLTF_FALLBACK; |
| | 35 | |
|
| 95 | 36 | | public AssetPromiseSettings_Rendering settings = new (); |
| 287 | 37 | | public Rendereable loadedAsset { get; protected set; } |
| | 38 | |
|
| | 39 | | private readonly string bundlesContentUrl; |
| | 40 | | private readonly ContentProvider contentProvider; |
| | 41 | | private AssetPromise_GLTFast_Instance gltfastPromise; |
| | 42 | | private AssetPromise_AB_GameObject abPromise; |
| | 43 | | private AssetPromise_SceneAB sceneABPromise; |
| | 44 | | private string currentLoadingSystem; |
| 97 | 45 | | private FeatureFlag featureFlags => DataStore.i.featureFlags.flags.Get(); |
| | 46 | | private string targetUrl; |
| | 47 | |
|
| | 48 | | public IEnumerator Promise |
| | 49 | | { |
| | 50 | | get |
| | 51 | | { |
| 42 | 52 | | if (gltfastPromise != null) |
| 41 | 53 | | yield return gltfastPromise; |
| | 54 | |
|
| 42 | 55 | | if (abPromise != null) |
| 0 | 56 | | yield return abPromise; |
| | 57 | |
|
| 42 | 58 | | yield return null; |
| 42 | 59 | | } |
| | 60 | | } |
| | 61 | |
|
| | 62 | | public bool IsFinished |
| | 63 | | { |
| | 64 | | get |
| | 65 | | { |
| 1 | 66 | | if (gltfastPromise != null) |
| 1 | 67 | | return gltfastPromise.state == AssetPromiseState.FINISHED; |
| | 68 | |
|
| 0 | 69 | | if (abPromise != null) |
| 0 | 70 | | return abPromise.state == AssetPromiseState.FINISHED; |
| | 71 | |
|
| 0 | 72 | | return false; |
| | 73 | | } |
| | 74 | | } |
| | 75 | |
|
| | 76 | | #if UNITY_EDITOR |
| | 77 | | public override string ToString() |
| | 78 | | { |
| 0 | 79 | | float loadTime = Mathf.Min(loadFinishTime, Time.realtimeSinceStartup) - loadStartTime; |
| | 80 | |
|
| 0 | 81 | | string result = "not loading"; |
| | 82 | |
|
| 0 | 83 | | if (abPromise != null) { result = $"ASSET BUNDLE -> promise state = {abPromise.ToString()} ({loadTime} load |
| | 84 | |
|
| 0 | 85 | | return result; |
| | 86 | | } |
| | 87 | |
|
| | 88 | | float loadStartTime = 0; |
| 95 | 89 | | float loadFinishTime = float.MaxValue; |
| | 90 | | #endif |
| | 91 | |
|
| 95 | 92 | | public RendereableAssetLoadHelper(ContentProvider contentProvider, string bundlesContentUrl) |
| | 93 | | { |
| 95 | 94 | | this.contentProvider = contentProvider; |
| 95 | 95 | | this.bundlesContentUrl = bundlesContentUrl; |
| 95 | 96 | | } |
| | 97 | |
|
| | 98 | | public void Load(string targetUrl, LoadingType forcedLoadingType = LoadingType.DEFAULT) |
| | 99 | | { |
| 100 | 100 | | this.targetUrl = targetUrl; |
| 100 | 101 | | Assert.IsFalse(string.IsNullOrEmpty(targetUrl), "url is null!!"); |
| | 102 | | #if UNITY_EDITOR |
| 100 | 103 | | loadStartTime = Time.realtimeSinceStartup; |
| | 104 | | #endif |
| 100 | 105 | | LoadingType finalLoadingType = forcedLoadingType == LoadingType.DEFAULT ? defaultLoadingType : forcedLoading |
| | 106 | |
|
| | 107 | | switch (finalLoadingType) |
| | 108 | | { |
| | 109 | | case LoadingType.ASSET_BUNDLE_ONLY: |
| 0 | 110 | | LoadAssetBundle(targetUrl, OnSuccessEvent, OnFailEvent, false); |
| 0 | 111 | | break; |
| | 112 | | case LoadingType.GLTF_ONLY: |
| 2 | 113 | | LoadGLTFast(targetUrl, OnSuccessEvent, OnFailEvent, false); |
| 2 | 114 | | break; |
| | 115 | | case LoadingType.DEFAULT: |
| | 116 | | case LoadingType.ASSET_BUNDLE_WITH_GLTF_FALLBACK: |
| 196 | 117 | | LoadAssetBundle(targetUrl, OnSuccessEvent, exception => LoadGLTFast(targetUrl, OnSuccessEvent, OnFai |
| | 118 | | break; |
| | 119 | | } |
| 98 | 120 | | } |
| | 121 | |
|
| | 122 | | public void Unload() |
| | 123 | | { |
| 64 | 124 | | UnloadAB(); |
| 64 | 125 | | UnloadGLTFast(); |
| 64 | 126 | | } |
| | 127 | |
|
| | 128 | | void UnloadAB() |
| | 129 | | { |
| 64 | 130 | | if (abPromise != null) { AssetPromiseKeeper_AB_GameObject.i.Forget(abPromise); } |
| | 131 | |
|
| 64 | 132 | | if (sceneABPromise != null) { AssetPromiseKeeper_SceneAB.i.Forget(sceneABPromise); } |
| 64 | 133 | | } |
| | 134 | |
|
| | 135 | | void UnloadGLTFast() |
| | 136 | | { |
| 141 | 137 | | if (gltfastPromise != null) { AssetPromiseKeeper_GLTFast_Instance.i.Forget(gltfastPromise); } |
| 71 | 138 | | } |
| | 139 | |
|
| | 140 | | private void LoadAssetBundle(string targetUrl, Action<Rendereable> OnSuccess, Action<Exception> OnFail, bool has |
| | 141 | | { |
| 98 | 142 | | currentLoadingSystem = AB_GO_NAME_PREFIX; |
| | 143 | |
|
| 98 | 144 | | if (abPromise != null) |
| | 145 | | { |
| 0 | 146 | | UnloadAB(); |
| | 147 | |
|
| 0 | 148 | | if (VERBOSE) |
| 0 | 149 | | Debug.Log("Forgetting not null promise..." + targetUrl); |
| | 150 | | } |
| | 151 | |
|
| 98 | 152 | | if (!contentProvider.TryGetContentsUrl_Raw(targetUrl, out string hash)) |
| | 153 | | { |
| 1 | 154 | | OnFailWrapper(OnFail, new Exception($"Content url does not contains {targetUrl}"), hasFallback); |
| 1 | 155 | | return; |
| | 156 | | } |
| | 157 | |
|
| 97 | 158 | | if (featureFlags.IsFeatureEnabled(NEW_CDN_FF)) |
| | 159 | | { |
| 0 | 160 | | FetchAssetBundlesManifest(OnSuccess, OnFail, hasFallback, hash); |
| | 161 | | } |
| | 162 | | else |
| | 163 | | { |
| 97 | 164 | | string bundlesBaseUrl = useCustomContentServerUrl ? customContentServerUrl : bundlesContentUrl; |
| | 165 | |
|
| 97 | 166 | | if (string.IsNullOrEmpty(bundlesBaseUrl)) |
| | 167 | | { |
| 97 | 168 | | OnFailWrapper(OnFail, new Exception("bundlesBaseUrl is null"), hasFallback); |
| 97 | 169 | | return; |
| | 170 | | } |
| | 171 | |
|
| 0 | 172 | | LoadAssetBundles(OnSuccess, OnFail, hasFallback, bundlesBaseUrl, hash); |
| | 173 | | } |
| 0 | 174 | | } |
| | 175 | |
|
| | 176 | | private void FetchAssetBundlesManifest(Action<Rendereable> onSuccess, Action<Exception> onFail, bool hasFallback |
| | 177 | | { |
| 0 | 178 | | if (contentProvider.assetBundles.Contains(hash)) |
| 0 | 179 | | LoadAssetBundles(onSuccess, onFail, hasFallback, contentProvider.assetBundlesBaseUrl, hash); |
| | 180 | | else |
| | 181 | | { |
| 0 | 182 | | if (contentProvider.assetBundlesFetched) |
| | 183 | | { |
| 0 | 184 | | OnFailWrapper(onFail, new Exception("Asset not converted to asset bundles"), hasFallback); |
| 0 | 185 | | return; |
| | 186 | | } |
| | 187 | |
|
| 0 | 188 | | sceneABPromise = new AssetPromise_SceneAB(contentProvider.baseUrlBundles, contentProvider.sceneCid); |
| 0 | 189 | | sceneABPromise.OnSuccessEvent += ab => |
| | 190 | | { |
| 0 | 191 | | if (ab.IsSceneConverted()) |
| | 192 | | { |
| 0 | 193 | | contentProvider.assetBundles = ab.GetConvertedFiles(); |
| 0 | 194 | | contentProvider.assetBundlesBaseUrl = ab.GetBaseUrl(); |
| 0 | 195 | | contentProvider.assetBundlesVersion = ab.GetVersion(); |
| 0 | 196 | | LoadAssetBundles(onSuccess, onFail, hasFallback, contentProvider.assetBundlesBaseUrl, hash); |
| | 197 | | } |
| 0 | 198 | | else { OnFailWrapper(onFail, new Exception("Asset not converted to asset bundles"), hasFallback); } |
| | 199 | |
|
| 0 | 200 | | contentProvider.assetBundlesFetched = true; |
| 0 | 201 | | }; |
| | 202 | |
|
| 0 | 203 | | sceneABPromise.OnFailEvent += (_, exception) => { OnFailWrapper(onFail, exception, hasFallback); }; |
| | 204 | |
|
| 0 | 205 | | AssetPromiseKeeper_SceneAB.i.Keep(sceneABPromise); |
| | 206 | | } |
| 0 | 207 | | } |
| | 208 | |
|
| | 209 | | private void LoadAssetBundles(Action<Rendereable> OnSuccess, Action<Exception> OnFail, bool hasFallback, string |
| | 210 | | { |
| 0 | 211 | | abPromise = new AssetPromise_AB_GameObject(bundlesBaseUrl, hash); |
| 0 | 212 | | abPromise.settings = this.settings; |
| | 213 | |
|
| 0 | 214 | | abPromise.OnSuccessEvent += (x) => |
| | 215 | | { |
| | 216 | | #if UNITY_EDITOR |
| 0 | 217 | | x.container.name = AB_GO_NAME_PREFIX + hash + " - " + contentProvider.assetBundlesVersion; |
| | 218 | | #endif |
| 0 | 219 | | var r = new Rendereable() |
| | 220 | | { |
| | 221 | | container = x.container, |
| | 222 | | totalTriangleCount = x.totalTriangleCount, |
| | 223 | | meshes = x.meshes, |
| | 224 | | renderers = x.renderers, |
| | 225 | | materials = x.materials, |
| | 226 | | textures = x.textures, |
| | 227 | | meshToTriangleCount = x.meshToTriangleCount, |
| | 228 | | animationClipSize = x.animationClipSize, |
| | 229 | | animationClips = x.animationClips, |
| | 230 | | meshDataSize = x.meshDataSize |
| | 231 | | }; |
| | 232 | |
|
| 0 | 233 | | foreach (var someRenderer in r.renderers) |
| 0 | 234 | | someRenderer.tag = FROM_ASSET_BUNDLE_TAG; |
| | 235 | |
|
| 0 | 236 | | OnSuccessWrapper(r, OnSuccess); |
| 0 | 237 | | }; |
| | 238 | |
|
| 0 | 239 | | abPromise.OnFailEvent += (x, exception) => OnFailWrapper(OnFail, exception, hasFallback); |
| | 240 | |
|
| 0 | 241 | | AssetPromiseKeeper_AB_GameObject.i.Keep(abPromise); |
| 0 | 242 | | } |
| | 243 | |
|
| | 244 | | private void LoadGLTFast(string targetUrl, Action<Rendereable> OnSuccess, Action<Exception> OnFail, bool hasFall |
| | 245 | | { |
| 100 | 246 | | currentLoadingSystem = GLTFAST_GO_NAME_PREFIX; |
| | 247 | |
|
| 100 | 248 | | if (gltfastPromise != null) |
| | 249 | | { |
| 7 | 250 | | UnloadGLTFast(); |
| | 251 | |
|
| 7 | 252 | | if (VERBOSE) |
| 0 | 253 | | Debug.Log("Forgetting not null promise... " + targetUrl); |
| | 254 | | } |
| | 255 | |
|
| 100 | 256 | | if (!contentProvider.TryGetContentsUrl_Raw(targetUrl, out string hash)) |
| | 257 | | { |
| 1 | 258 | | OnFailWrapper(OnFail, new Exception($"Content provider does not contains url {targetUrl}"), hasFallback) |
| 1 | 259 | | return; |
| | 260 | | } |
| | 261 | |
|
| 99 | 262 | | gltfastPromise = new AssetPromise_GLTFast_Instance(targetUrl, hash, |
| | 263 | | Environment.i.platform.webRequest, contentProvider, settings); |
| | 264 | |
|
| 99 | 265 | | gltfastPromise.OnSuccessEvent += (Asset_GLTFast_Instance x) => |
| | 266 | | { |
| | 267 | | #if UNITY_EDITOR |
| 90 | 268 | | x.container.name = GLTFAST_GO_NAME_PREFIX + hash; |
| | 269 | | #endif |
| 90 | 270 | | Rendereable r = x.ToRendereable(); |
| | 271 | |
|
| 492 | 272 | | foreach (var someRenderer in r.renderers) |
| 156 | 273 | | someRenderer.tag = FROM_RAW_GLTF_TAG; |
| | 274 | |
|
| 90 | 275 | | OnSuccessWrapper(r, OnSuccess); |
| 90 | 276 | | }; |
| | 277 | |
|
| 99 | 278 | | gltfastPromise.OnFailEvent += (asset, exception) => { OnFailWrapper(OnFail, exception, hasFallback); }; |
| | 279 | |
|
| 99 | 280 | | AssetPromiseKeeper_GLTFast_Instance.i.Keep(gltfastPromise); |
| 99 | 281 | | } |
| | 282 | |
|
| | 283 | | private void OnFailWrapper(Action<Exception> OnFail, Exception exception, bool hasFallback) |
| | 284 | | { |
| | 285 | | #if UNITY_EDITOR |
| 99 | 286 | | loadFinishTime = Time.realtimeSinceStartup; |
| | 287 | | #endif |
| | 288 | |
|
| | 289 | | // If the entity is destroyed while loading, the exception is expected to be null and no error should be thr |
| 99 | 290 | | if (exception != null) |
| | 291 | | { |
| 99 | 292 | | if (!hasFallback) |
| 1 | 293 | | Debug.LogWarning("All fallbacks failed for " + targetUrl); |
| 98 | 294 | | else if (VERBOSE) |
| 0 | 295 | | Debug.LogWarning($"Load Fail Detected, trying to use a fallback, " + |
| | 296 | | $"loading type was: {currentLoadingSystem} and error was: {exception.Message}"); |
| | 297 | | } |
| | 298 | |
|
| 99 | 299 | | OnFail?.Invoke(exception); |
| 99 | 300 | | ClearEvents(); |
| 99 | 301 | | } |
| | 302 | |
|
| | 303 | | private void OnSuccessWrapper(Rendereable loadedAsset, Action<Rendereable> OnSuccess) |
| | 304 | | { |
| | 305 | | #if UNITY_EDITOR |
| 90 | 306 | | loadFinishTime = Time.realtimeSinceStartup; |
| | 307 | | #endif |
| 90 | 308 | | if (VERBOSE) |
| | 309 | | { |
| 0 | 310 | | if (gltfastPromise != null) |
| 0 | 311 | | Debug.Log($"GLTF Load(): target URL -> {gltfastPromise.GetId()}. Success!"); |
| | 312 | | else |
| 0 | 313 | | Debug.Log($"AB Load(): target URL -> {abPromise.hash}. Success!"); |
| | 314 | | } |
| | 315 | |
|
| 90 | 316 | | this.loadedAsset = loadedAsset; |
| 90 | 317 | | OnSuccess?.Invoke(loadedAsset); |
| 90 | 318 | | ClearEvents(); |
| 90 | 319 | | } |
| | 320 | |
|
| | 321 | | public void ClearEvents() |
| | 322 | | { |
| 210 | 323 | | OnSuccessEvent = null; |
| 210 | 324 | | OnFailEvent = null; |
| 210 | 325 | | } |
| | 326 | |
|
| | 327 | | private void SendMetric(string eventToSend, string targetUrl, string reason) |
| | 328 | | { |
| 0 | 329 | | GenericAnalytics.SendAnalytic(eventToSend, new Dictionary<string, string> { { "targetUrl", targetUrl }, { "r |
| 0 | 330 | | } |
| | 331 | | } |
| | 332 | | } |