| | 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> OnFailEvent; |
| | 38 | |
|
| 7153 | 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() |
| | 48 | | { |
| 8 | 49 | | OnPreFinishEvent = null; |
| 8 | 50 | | CallAndClearEvents(false); |
| 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 = true) |
| | 64 | | { |
| 394 | 65 | | if (asset == null) |
| | 66 | | { |
| 17 | 67 | | isSuccess = false; |
| | 68 | | } |
| | 69 | |
|
| 394 | 70 | | OnPreFinishEvent?.Invoke(this); |
| 394 | 71 | | OnPreFinishEvent = null; |
| | 72 | |
|
| 394 | 73 | | Action<AssetType> finalEvent = isSuccess ? OnSuccessEvent : OnFailEvent; |
| 394 | 74 | | finalEvent?.Invoke(asset); |
| | 75 | |
|
| 394 | 76 | | ClearEvents(); |
| 394 | 77 | | } |
| | 78 | |
|
| | 79 | | internal virtual void Load() |
| | 80 | | { |
| 425 | 81 | | if (state == AssetPromiseState.LOADING || state == AssetPromiseState.FINISHED) |
| 2 | 82 | | return; |
| | 83 | |
|
| 423 | 84 | | state = AssetPromiseState.LOADING; |
| | 85 | |
|
| | 86 | | // NOTE(Brian): Get existent library element |
| 423 | 87 | | object libraryAssetCheckId = GetLibraryAssetCheckId(); |
| 423 | 88 | | if (library.Contains(libraryAssetCheckId)) |
| | 89 | | { |
| 135 | 90 | | asset = GetAsset(libraryAssetCheckId); |
| | 91 | |
|
| 135 | 92 | | if (asset != null) |
| | 93 | | { |
| 135 | 94 | | OnBeforeLoadOrReuse(); |
| 135 | 95 | | OnReuse(OnReuseFinished); |
| 135 | 96 | | } |
| | 97 | | else |
| | 98 | | { |
| 0 | 99 | | CallAndClearEvents(false); |
| | 100 | | } |
| | 101 | |
|
| 0 | 102 | | return; |
| | 103 | | } |
| | 104 | |
|
| | 105 | | // NOTE(Brian): Get new library element |
| 288 | 106 | | asset = new AssetType(); |
| 288 | 107 | | OnBeforeLoadOrReuse(); |
| 288 | 108 | | asset.id = GetId(); |
| | 109 | |
|
| 288 | 110 | | OnLoad(OnLoadSuccess, OnLoadFailure); |
| 288 | 111 | | } |
| | 112 | |
|
| 332 | 113 | | protected virtual object GetLibraryAssetCheckId() { return GetId(); } |
| | 114 | |
|
| 117 | 115 | | protected virtual AssetType GetAsset(object id) { return library.Get(id); } |
| | 116 | |
|
| 164 | 117 | | protected virtual void OnReuse(Action OnFinish) { OnFinish?.Invoke(); } |
| | 118 | |
|
| | 119 | | protected void OnReuseFinished() |
| | 120 | | { |
| 133 | 121 | | OnAfterLoadOrReuse(); |
| 133 | 122 | | state = AssetPromiseState.FINISHED; |
| 133 | 123 | | CallAndClearEvents(isSuccess: true); |
| 133 | 124 | | } |
| | 125 | |
|
| | 126 | | protected void OnLoadSuccess() |
| | 127 | | { |
| 205 | 128 | | if (AddToLibrary()) |
| | 129 | | { |
| 205 | 130 | | OnAfterLoadOrReuse(); |
| 205 | 131 | | state = AssetPromiseState.FINISHED; |
| 205 | 132 | | CallAndClearEvents(isSuccess: true); |
| 205 | 133 | | } |
| | 134 | | else |
| | 135 | | { |
| 0 | 136 | | OnLoadFailure(); |
| | 137 | | } |
| 0 | 138 | | } |
| | 139 | |
|
| | 140 | | protected void OnLoadFailure() |
| | 141 | | { |
| 48 | 142 | | CallAndClearEvents(isSuccess: false); |
| 48 | 143 | | Cleanup(); |
| 48 | 144 | | } |
| | 145 | |
|
| 10 | 146 | | protected virtual bool AddToLibrary() { return library.Add(asset); } |
| | 147 | |
|
| | 148 | | internal virtual void Unload() |
| | 149 | | { |
| 272 | 150 | | if (state == AssetPromiseState.IDLE_AND_EMPTY) |
| 22 | 151 | | return; |
| | 152 | |
|
| 250 | 153 | | Cleanup(); |
| 250 | 154 | | } |
| | 155 | |
|
| | 156 | | public void Cleanup() |
| | 157 | | { |
| 313 | 158 | | if (state == AssetPromiseState.LOADING) |
| | 159 | | { |
| 82 | 160 | | OnCancelLoading(); |
| 82 | 161 | | ClearEvents(); |
| | 162 | | } |
| | 163 | |
|
| 313 | 164 | | state = AssetPromiseState.IDLE_AND_EMPTY; |
| | 165 | |
|
| 313 | 166 | | if (asset != null) |
| | 167 | | { |
| 294 | 168 | | if (library.Contains(asset)) |
| 214 | 169 | | library.Release(asset); |
| | 170 | | else |
| 80 | 171 | | asset.Cleanup(); |
| | 172 | |
|
| 294 | 173 | | asset = null; |
| | 174 | | } |
| 313 | 175 | | } |
| | 176 | |
|
| | 177 | | internal virtual void OnForget() |
| | 178 | | { |
| 558 | 179 | | isForgotten = true; |
| 558 | 180 | | ClearEvents(); |
| 558 | 181 | | } |
| | 182 | |
|
| | 183 | | protected abstract void OnCancelLoading(); |
| | 184 | | protected abstract void OnLoad(Action OnSuccess, Action OnFail); |
| | 185 | | protected abstract void OnBeforeLoadOrReuse(); |
| | 186 | | protected abstract void OnAfterLoadOrReuse(); |
| | 187 | | public abstract object GetId(); |
| | 188 | | } |
| | 189 | | } |