| | 1 | | using System; |
| | 2 | | using System.Threading; |
| | 3 | | using Cysharp.Threading.Tasks; |
| | 4 | | using UnityEngine; |
| | 5 | |
|
| | 6 | | namespace DCL |
| | 7 | | { |
| | 8 | | public class AssetPromise_Gif : AssetPromise<Asset_Gif> |
| | 9 | | { |
| | 10 | | private readonly string url; |
| | 11 | | private Action onSuccsess; |
| | 12 | | private CancellationTokenSource tokenSource; |
| | 13 | | private bool jsGIFProcessingEnabled; |
| | 14 | |
|
| 15 | 15 | | public AssetPromise_Gif(string url) |
| | 16 | | { |
| 15 | 17 | | KernelConfig.i.EnsureConfigInitialized().Then(config => jsGIFProcessingEnabled = config.gifSupported); |
| 15 | 18 | | this.url = url; |
| 15 | 19 | | } |
| | 20 | |
|
| 74 | 21 | | public override object GetId() { return url; } |
| | 22 | |
|
| | 23 | | protected override void OnLoad(Action OnSuccess, Action<Exception> OnFail) |
| | 24 | | { |
| 12 | 25 | | tokenSource = new CancellationTokenSource(); |
| 12 | 26 | | IGifProcessor processor = GetGifProcessor(); |
| 12 | 27 | | onSuccsess = OnSuccess; |
| 12 | 28 | | asset.processor = processor; |
| 12 | 29 | | CancellationToken token = tokenSource.Token; |
| | 30 | |
|
| 12 | 31 | | processor.Load(OnLoadSuccsess, OnFail, token) |
| | 32 | | .AttachExternalCancellation(token) |
| | 33 | | .Forget(); |
| 12 | 34 | | } |
| | 35 | |
|
| | 36 | | private IGifProcessor GetGifProcessor() |
| | 37 | | { |
| 12 | 38 | | if (jsGIFProcessingEnabled) |
| | 39 | | { |
| 0 | 40 | | return new JSGifProcessor(url); |
| | 41 | | } |
| | 42 | |
|
| 12 | 43 | | return new GifDecoderProcessor(url, Environment.i.platform.webRequest); |
| | 44 | | } |
| | 45 | | private void OnLoadSuccsess(GifFrameData[] frames) |
| | 46 | | { |
| 5 | 47 | | asset.frames = frames; |
| 5 | 48 | | onSuccsess?.Invoke(); |
| 5 | 49 | | } |
| | 50 | | protected override void OnCancelLoading() |
| | 51 | | { |
| 7 | 52 | | tokenSource.Cancel(); |
| 7 | 53 | | tokenSource.Dispose(); |
| 7 | 54 | | } |
| | 55 | |
|
| | 56 | | protected override bool AddToLibrary() |
| | 57 | | { |
| 5 | 58 | | if (!library.Add(asset)) |
| | 59 | | { |
| 0 | 60 | | Debug.Log("add to library fail?"); |
| 0 | 61 | | return false; |
| | 62 | | } |
| | 63 | |
|
| 5 | 64 | | asset = library.Get(asset.id); |
| 5 | 65 | | return true; |
| | 66 | | } |
| | 67 | |
|
| 15 | 68 | | protected override void OnBeforeLoadOrReuse() { } |
| | 69 | |
|
| 8 | 70 | | protected override void OnAfterLoadOrReuse() { } |
| | 71 | | } |
| | 72 | | } |