| | 1 | | using Cysharp.Threading.Tasks; |
| | 2 | | using MainScripts.DCL.Controllers.AssetManager; |
| | 3 | | using MainScripts.DCL.Controllers.AssetManager.Font; |
| | 4 | | using System; |
| | 5 | | using System.Threading; |
| | 6 | |
|
| | 7 | | namespace DCL |
| | 8 | | { |
| | 9 | | public class AssetPromise_Font : AssetPromise<Asset_Font> |
| | 10 | | { |
| | 11 | | private readonly AssetSource permittedSources; |
| | 12 | | private readonly string src; |
| | 13 | | private CancellationTokenSource cancellationTokenSource; |
| | 14 | |
|
| | 15 | | private Service<IFontAssetResolver> fontResolver; |
| | 16 | |
|
| 46 | 17 | | public AssetPromise_Font(string src, AssetSource permittedSources = AssetSource.ALL) |
| | 18 | | { |
| 46 | 19 | | this.permittedSources = permittedSources; |
| 46 | 20 | | this.src = src; |
| 46 | 21 | | } |
| | 22 | |
|
| 44 | 23 | | protected override void OnAfterLoadOrReuse() { } |
| | 24 | |
|
| 48 | 25 | | protected override void OnBeforeLoadOrReuse() { } |
| | 26 | |
|
| | 27 | | protected override void OnCancelLoading() |
| | 28 | | { |
| 4 | 29 | | CleanCT(); |
| 4 | 30 | | } |
| | 31 | |
|
| | 32 | | public override void Cleanup() |
| | 33 | | { |
| 41 | 34 | | base.Cleanup(); |
| 41 | 35 | | CleanCT(); |
| 41 | 36 | | } |
| | 37 | |
|
| | 38 | | protected override void OnLoad(Action OnSuccess, Action<Exception> OnFail) |
| | 39 | | { |
| | 40 | | //Adding a null-check here. If the promise is kept while active, we should not fire it up again |
| 33 | 41 | | if (cancellationTokenSource != null) return; |
| | 42 | |
|
| 33 | 43 | | cancellationTokenSource = new CancellationTokenSource(); |
| 33 | 44 | | LaunchRequest().Forget(); |
| | 45 | |
|
| | 46 | | async UniTaskVoid LaunchRequest() |
| | 47 | | { |
| 99 | 48 | | FontResponse result = await fontResolver.Ref.GetFontAsync(permittedSources, src, cancellationTokenSource |
| | 49 | |
|
| 33 | 50 | | if (result.IsSuccess) |
| | 51 | | { |
| 29 | 52 | | FontSuccessResponse successResponse = result.GetSuccessResponse(); |
| 29 | 53 | | asset.font = successResponse.Font; |
| 29 | 54 | | OnSuccess?.Invoke(); |
| | 55 | | } |
| 4 | 56 | | else { OnFail?.Invoke(result.GetFailResponse().Exception); } |
| 33 | 57 | | } |
| 33 | 58 | | } |
| | 59 | |
|
| | 60 | | protected override bool AddToLibrary() |
| | 61 | | { |
| 29 | 62 | | if (!library.Add(asset)) { return false; } |
| | 63 | |
|
| 29 | 64 | | asset = library.Get(asset.id); |
| 29 | 65 | | return true; |
| | 66 | | } |
| | 67 | |
|
| | 68 | | public override object GetId() => |
| 256 | 69 | | src; |
| | 70 | |
|
| | 71 | | private void CleanCT() |
| | 72 | | { |
| 45 | 73 | | if (cancellationTokenSource != null) |
| | 74 | | { |
| 29 | 75 | | cancellationTokenSource.Cancel(); |
| 29 | 76 | | cancellationTokenSource.Dispose(); |
| 29 | 77 | | cancellationTokenSource = null; |
| | 78 | | } |
| 45 | 79 | | } |
| | 80 | | } |
| | 81 | | } |