| | 1 | | using Cysharp.Threading.Tasks; |
| | 2 | | using DCL.Helpers; |
| | 3 | | using System; |
| | 4 | | using System.Threading; |
| | 5 | |
|
| | 6 | | namespace DCL |
| | 7 | | { |
| | 8 | | public class Environment |
| | 9 | | { |
| 1 | 10 | | public static Model i = new Model(new ServiceLocator()); |
| | 11 | |
|
| | 12 | | private static CancellationTokenSource cancellationTokenSource; |
| | 13 | |
|
| | 14 | | private static bool initialized; |
| | 15 | |
|
| | 16 | | public static UniTask WaitUntilInitialized() => |
| 1 | 17 | | UniTaskUtils.WaitForBoolean(ref initialized, cancellationToken: cancellationTokenSource?.Token ?? Cancellati |
| | 18 | |
|
| | 19 | | /// <summary> |
| | 20 | | /// Setup the environment with the configured builder funcs. |
| | 21 | | /// </summary> |
| | 22 | | public static void Setup(ServiceLocator serviceLocator) |
| | 23 | | { |
| 759 | 24 | | i.Dispose(); |
| 759 | 25 | | i = new Model(serviceLocator); |
| 759 | 26 | | cancellationTokenSource = new CancellationTokenSource(); |
| 1489 | 27 | | serviceLocator.Initialize(cancellationTokenSource.Token).ContinueWith(() => initialized = true).Forget(); |
| 759 | 28 | | } |
| | 29 | |
|
| | 30 | | public static UniTask SetupAsync(ServiceLocator serviceLocator) |
| | 31 | | { |
| 0 | 32 | | i.Dispose(); |
| 0 | 33 | | i = new Model(serviceLocator); |
| 0 | 34 | | cancellationTokenSource = new CancellationTokenSource(); |
| 0 | 35 | | return serviceLocator.Initialize(cancellationTokenSource.Token).ContinueWith(() => initialized = true); |
| | 36 | | } |
| | 37 | |
|
| | 38 | | /// <summary> |
| | 39 | | /// Dispose() all the current environment systems. |
| | 40 | | /// </summary> |
| | 41 | | public static void Dispose() |
| | 42 | | { |
| 633 | 43 | | i?.Dispose(); |
| | 44 | |
|
| 633 | 45 | | if (cancellationTokenSource != null) |
| | 46 | | { |
| 628 | 47 | | cancellationTokenSource.Cancel(); |
| 628 | 48 | | cancellationTokenSource.Dispose(); |
| 628 | 49 | | cancellationTokenSource = null; |
| | 50 | | } |
| | 51 | |
|
| 633 | 52 | | initialized = false; |
| 633 | 53 | | } |
| | 54 | |
|
| | 55 | | public class Model : IDisposable |
| | 56 | | { |
| | 57 | | public readonly ServiceLocator serviceLocator; |
| | 58 | | public readonly MessagingContext messaging; |
| | 59 | | public readonly PlatformContext platform; |
| | 60 | | public readonly WorldRuntimeContext world; |
| | 61 | | public readonly HUDContext hud; |
| | 62 | |
|
| 760 | 63 | | public Model (ServiceLocator serviceLocator) |
| | 64 | | { |
| 760 | 65 | | this.serviceLocator = serviceLocator; |
| 760 | 66 | | messaging = new MessagingContext(serviceLocator); |
| 760 | 67 | | platform = new PlatformContext(serviceLocator); |
| 760 | 68 | | world = new WorldRuntimeContext(serviceLocator); |
| 760 | 69 | | hud = new HUDContext(serviceLocator); |
| 760 | 70 | | } |
| | 71 | |
|
| | 72 | | public void Dispose() |
| | 73 | | { |
| 1392 | 74 | | this.serviceLocator.Dispose(); |
| 1392 | 75 | | } |
| | 76 | | } |
| | 77 | | } |
| | 78 | | } |