| | 1 | | using System; |
| | 2 | | using UnityEngine; |
| | 3 | |
|
| | 4 | | namespace DCL |
| | 5 | | { |
| | 6 | | public enum AssetPromiseState |
| | 7 | | { |
| | 8 | | IDLE_AND_EMPTY, |
| | 9 | | WAITING, |
| | 10 | | LOADING, |
| | 11 | | FINISHED |
| | 12 | | } |
| | 13 | |
|
| | 14 | | /// <summary> |
| | 15 | | /// AssetPromise is in charge of handling all the logic related to loading the specified AssetType. |
| | 16 | | /// It also should return the cached asset if applicable. |
| | 17 | | /// |
| | 18 | | /// If we need settings related to how the asset should be loaded/handled, we add them here. |
| | 19 | | /// |
| | 20 | | /// If we need many ways of loading the same Asset type, we can create different AssetPromise |
| | 21 | | /// subclasses to do so. This can be useful to attach the GLTFSceneImporter loading logic for |
| | 22 | | /// materials/textures to system. |
| | 23 | | /// </summary> |
| | 24 | | /// <typeparam name="AssetType">The Asset type to be loaded.</typeparam> |
| | 25 | | public abstract class AssetPromise<AssetType> : CustomYieldInstruction |
| | 26 | | where AssetType : Asset, new() |
| | 27 | | { |
| | 28 | | internal bool isDirty = false; |
| | 29 | | internal AssetLibrary<AssetType> library; |
| 0 | 30 | | public AssetType asset { get; protected set; } |
| | 31 | |
|
| 0 | 32 | | public AssetPromiseState state { get; protected set; } |
| 0 | 33 | | public bool isForgotten { get; protected set; } |
| | 34 | |
|
| | 35 | | internal event Action<AssetPromise<AssetType>> OnPreFinishEvent; |
| | 36 | | public event Action<AssetType> OnSuccessEvent; |
| | 37 | | public event Action<AssetType, Exception> OnFailEvent; |
| | 38 | |
|
| 6660 | 39 | | public override bool keepWaiting { get { return state == AssetPromiseState.LOADING || state == AssetPromiseState |
| | 40 | |
|
| | 41 | | public void ClearEvents() |
| | 42 | | { |
| 0 | 43 | | OnSuccessEvent = null; |
| 0 | 44 | | OnFailEvent = null; |
| 0 | 45 | | } |
| | 46 | |
|
| | 47 | | internal void ForceFail(Exception reason) |
| | 48 | | { |
| 8 | 49 | | OnPreFinishEvent = null; |
| 8 | 50 | | CallAndClearEvents(false, reason); |
| 8 | 51 | | state = AssetPromiseState.IDLE_AND_EMPTY; |
| 8 | 52 | | } |
| | 53 | |
|
| | 54 | | internal void SetWaitingState() |
| | 55 | | { |
| | 56 | | //TODO(Brian): This is made to make the promises yielding not return automatically when the promise is block |
| | 57 | | // |
| | 58 | | // Managing the blocked promises handling entirely in the AssetPromiseKeeper is coupling the cod |
| | 59 | | // It's better to have a "WaitForPromise" method here and lighten the APK logic a bit. For now t |
| 0 | 60 | | state = AssetPromiseState.WAITING; |
| 0 | 61 | | } |
| | 62 | |
|
| | 63 | | protected void CallAndClearEvents(bool isSuccess, Exception exception) |
| | 64 | | { |
| 537 | 65 | | if (asset == null) |
| | 66 | | { |
| 106 | 67 | | isSuccess = false; |
| | 68 | | } |
| | 69 | |
|
| 537 | 70 | | OnPreFinishEvent?.Invoke(this); |
| 537 | 71 | | OnPreFinishEvent = null; |
| | 72 | |
|
| 537 | 73 | | if (isSuccess) |
| 364 | 74 | | OnSuccessEvent?.Invoke(asset); |
| | 75 | | else |
| 173 | 76 | | OnFailEvent?.Invoke(asset, exception); |
| | 77 | |
|
| 537 | 78 | | ClearEvents(); |
| 537 | 79 | | } |
| | 80 | |
|
| | 81 | | internal virtual void Load() |
| | 82 | | { |
| 558 | 83 | | if (state == AssetPromiseState.LOADING || state == AssetPromiseState.FINISHED) |
| 2 | 84 | | return; |
| | 85 | |
|
| 556 | 86 | | state = AssetPromiseState.LOADING; |
| | 87 | |
|
| | 88 | | // NOTE(Brian): Get existent library element |
| 556 | 89 | | object libraryAssetCheckId = GetLibraryAssetCheckId(); |
| 556 | 90 | | if (library.Contains(libraryAssetCheckId)) |
| | 91 | | { |
| 150 | 92 | | asset = GetAsset(libraryAssetCheckId); |
| | 93 | |
|
| 150 | 94 | | if (asset != null) |
| | 95 | | { |
| 150 | 96 | | OnBeforeLoadOrReuse(); |
| 150 | 97 | | OnReuse(OnReuseFinished); |
| 150 | 98 | | } |
| | 99 | | else |
| | 100 | | { |
| 0 | 101 | | CallAndClearEvents(false, new Exception("Asset is null")); |
| | 102 | | } |
| | 103 | |
|
| 0 | 104 | | return; |
| | 105 | | } |
| | 106 | |
|
| | 107 | | // NOTE(Brian): Get new library element |
| 406 | 108 | | asset = new AssetType(); |
| 406 | 109 | | OnBeforeLoadOrReuse(); |
| 406 | 110 | | asset.id = GetId(); |
| | 111 | |
|
| 406 | 112 | | OnLoad(OnLoadSuccess, OnLoadFailure); |
| 406 | 113 | | } |
| | 114 | |
|
| 431 | 115 | | protected virtual object GetLibraryAssetCheckId() { return GetId(); } |
| | 116 | |
|
| 132 | 117 | | protected virtual AssetType GetAsset(object id) { return library.Get(id); } |
| | 118 | |
|
| 186 | 119 | | protected virtual void OnReuse(Action OnFinish) { OnFinish?.Invoke(); } |
| | 120 | |
|
| | 121 | | protected void OnReuseFinished() |
| | 122 | | { |
| 149 | 123 | | OnAfterLoadOrReuse(); |
| 149 | 124 | | state = AssetPromiseState.FINISHED; |
| 149 | 125 | | CallAndClearEvents(true, null); |
| 149 | 126 | | } |
| | 127 | |
|
| | 128 | | protected void OnLoadSuccess() |
| | 129 | | { |
| 215 | 130 | | if (AddToLibrary()) |
| | 131 | | { |
| 215 | 132 | | OnAfterLoadOrReuse(); |
| 215 | 133 | | state = AssetPromiseState.FINISHED; |
| 215 | 134 | | CallAndClearEvents(true, null); |
| 215 | 135 | | } |
| | 136 | | else |
| | 137 | | { |
| 0 | 138 | | OnLoadFailure(new Exception("Could not add asset to library")); |
| | 139 | | } |
| 0 | 140 | | } |
| | 141 | |
|
| | 142 | | protected void OnLoadFailure(Exception exception) |
| | 143 | | { |
| | 144 | | #if UNITY_STANDALONE || UNITY_EDITOR |
| 165 | 145 | | if (DataStore.i.common.isApplicationQuitting.Get()) |
| 0 | 146 | | return; |
| | 147 | | #endif |
| | 148 | |
|
| 165 | 149 | | CallAndClearEvents(false, exception); |
| 165 | 150 | | Cleanup(); |
| 165 | 151 | | } |
| | 152 | |
|
| 10 | 153 | | protected virtual bool AddToLibrary() { return library.Add(asset); } |
| | 154 | |
|
| | 155 | | internal virtual void Unload() |
| | 156 | | { |
| 296 | 157 | | if (state == AssetPromiseState.IDLE_AND_EMPTY) |
| 51 | 158 | | return; |
| | 159 | |
|
| 245 | 160 | | Cleanup(); |
| 245 | 161 | | } |
| | 162 | |
|
| | 163 | | public void Cleanup() |
| | 164 | | { |
| 454 | 165 | | if (state == AssetPromiseState.LOADING) |
| | 166 | | { |
| 192 | 167 | | OnCancelLoading(); |
| 192 | 168 | | ClearEvents(); |
| | 169 | | } |
| | 170 | |
|
| 454 | 171 | | state = AssetPromiseState.IDLE_AND_EMPTY; |
| | 172 | |
|
| 454 | 173 | | if (asset != null) |
| | 174 | | { |
| 331 | 175 | | if (library.Contains(asset)) |
| 138 | 176 | | library.Release(asset); |
| | 177 | | else |
| 193 | 178 | | asset.Cleanup(); |
| | 179 | |
|
| 331 | 180 | | asset = null; |
| | 181 | | } |
| 454 | 182 | | } |
| | 183 | |
|
| | 184 | | internal virtual void OnForget() |
| | 185 | | { |
| 793 | 186 | | isForgotten = true; |
| 793 | 187 | | ClearEvents(); |
| 793 | 188 | | } |
| | 189 | |
|
| | 190 | | protected abstract void OnCancelLoading(); |
| | 191 | | protected abstract void OnLoad(Action OnSuccess, Action<Exception> OnFail); |
| | 192 | | protected abstract void OnBeforeLoadOrReuse(); |
| | 193 | | protected abstract void OnAfterLoadOrReuse(); |
| | 194 | | public abstract object GetId(); |
| | 195 | | } |
| | 196 | | } |