| | 1 | | using System; |
| | 2 | | using System.Collections; |
| | 3 | | using UnityEngine; |
| | 4 | | using UnityEngine.Assertions; |
| | 5 | |
|
| | 6 | | namespace DCL |
| | 7 | | { |
| | 8 | | public class AssetPromise_Mock : AssetPromise<Asset_Mock> |
| | 9 | | { |
| | 10 | | public bool forceFail = false; |
| | 11 | | public float loadTime; |
| | 12 | | public object idGenerator; |
| | 13 | | private object id; |
| | 14 | | Coroutine assetMockCoroutine; |
| | 15 | |
|
| | 16 | | public event Action OnUnloaded; |
| | 17 | |
|
| | 18 | | public override object GetId() |
| | 19 | | { |
| 266 | 20 | | Assert.IsTrue(idGenerator != null, "idGenerator should not be null"); |
| | 21 | |
|
| 266 | 22 | | if (id == null) |
| 76 | 23 | | id = idGenerator.GetHashCode(); |
| | 24 | |
|
| 266 | 25 | | return id; |
| | 26 | | } |
| | 27 | |
|
| | 28 | | protected override void OnLoad(Action OnSuccess, Action<Exception> OnFail) |
| | 29 | | { |
| 16 | 30 | | loadTime = 1; |
| 16 | 31 | | assetMockCoroutine = CoroutineStarter.Start(MockLoadingCoroutine(OnSuccess, OnFail)); |
| 16 | 32 | | } |
| | 33 | |
|
| 12 | 34 | | protected override void OnCancelLoading() { CoroutineStarter.Stop(assetMockCoroutine); } |
| | 35 | |
|
| | 36 | | IEnumerator MockLoadingCoroutine(Action OnSuccess, Action<Exception> OnFail) |
| | 37 | | { |
| 16 | 38 | | yield return WaitForSecondsCache.Get(loadTime); |
| | 39 | |
|
| 11 | 40 | | if (forceFail) |
| | 41 | | { |
| 1 | 42 | | OnFail?.Invoke(new Exception("Promise was forced to fail")); |
| 1 | 43 | | } |
| | 44 | | else |
| | 45 | | { |
| 10 | 46 | | OnSuccess?.Invoke(); |
| | 47 | | } |
| 11 | 48 | | } |
| | 49 | |
|
| | 50 | | //NOTE(Brian): Used for testing |
| 10 | 51 | | public void Load_Test() { Load(); } |
| | 52 | |
|
| 0 | 53 | | public Asset_Mock GetAsset_Test() { return asset; } |
| | 54 | |
|
| 16 | 55 | | public void Unload_Test() { Unload(); } |
| | 56 | |
|
| | 57 | | internal override void Unload() |
| | 58 | | { |
| 16 | 59 | | base.Unload(); |
| 16 | 60 | | OnUnloaded?.Invoke(); |
| 1 | 61 | | } |
| | 62 | |
|
| 0 | 63 | | public void SetLibrary_Test(AssetLibrary<Asset_Mock> library) { this.library = library; } |
| | 64 | |
|
| 76 | 65 | | protected override void OnBeforeLoadOrReuse() { } |
| | 66 | |
|
| 70 | 67 | | protected override void OnAfterLoadOrReuse() { } |
| | 68 | | } |
| | 69 | |
|
| | 70 | | public class AssetPromise_Mock_Alt_Loading_Approach : AssetPromise_Mock { } |
| | 71 | | } |