| | 1 | | using System.ComponentModel; |
| | 2 | | using DCL.Controllers; |
| | 3 | | using UnityEngine; |
| | 4 | | using UnityEngine.SceneManagement; |
| | 5 | |
|
| | 6 | | namespace DCL |
| | 7 | | { |
| | 8 | | public class Environment |
| | 9 | | { |
| 1 | 10 | | public static Model i = new Model(); |
| | 11 | | private static System.Func<MessagingContext> messagingBuilder; |
| | 12 | | private static System.Func<PlatformContext> platformBuilder; |
| | 13 | | private static System.Func<WorldRuntimeContext> worldRuntimeBuilder; |
| | 14 | | private static System.Func<HUDContext> hudBuilder; |
| | 15 | |
|
| | 16 | | /// <summary> |
| | 17 | | /// Configure and setup the environment with custom implementations given by each func passed as parameter. |
| | 18 | | /// |
| | 19 | | /// The funcs are stored. So the same environment can be replicated by just calling Setup() later. |
| | 20 | | /// </summary> |
| | 21 | | /// <param name="messagingBuilder">A func returning a MessagingContext to be used by the environment.</param> |
| | 22 | | /// <param name="platformBuilder">A func returning a PlatformContext to be used by the environment.</param> |
| | 23 | | /// <param name="worldRuntimeBuilder">A func returning a WorldRuntimeContext to be used by the environment.</par |
| | 24 | | public static void SetupWithBuilders( |
| | 25 | | System.Func<MessagingContext> messagingBuilder, |
| | 26 | | System.Func<PlatformContext> platformBuilder, |
| | 27 | | System.Func<WorldRuntimeContext> worldRuntimeBuilder, |
| | 28 | | System.Func<HUDContext> hudBuilder) |
| | 29 | | { |
| 765 | 30 | | Environment.messagingBuilder = messagingBuilder; |
| 765 | 31 | | Environment.platformBuilder = platformBuilder; |
| 765 | 32 | | Environment.worldRuntimeBuilder = worldRuntimeBuilder; |
| 765 | 33 | | Environment.hudBuilder = hudBuilder; |
| 765 | 34 | | Setup(); |
| 765 | 35 | | } |
| | 36 | |
|
| | 37 | | /// <summary> |
| | 38 | | /// Setup the environment with the configured builder funcs. |
| | 39 | | /// </summary> |
| | 40 | | public static void Setup() |
| | 41 | | { |
| 765 | 42 | | i = new Model(messagingBuilder, platformBuilder, worldRuntimeBuilder, hudBuilder); |
| 765 | 43 | | Initialize(); |
| 765 | 44 | | } |
| | 45 | |
|
| | 46 | | /// <summary> |
| | 47 | | /// Wire the system dependencies. We should improve this approach later. |
| | 48 | | /// </summary> |
| | 49 | | private static void Initialize() |
| | 50 | | { |
| 765 | 51 | | Model model = i; |
| | 52 | |
|
| | 53 | | //TODO(Brian): We can move to a RAII scheme + promises later to make this |
| | 54 | | // more scalable. |
| | 55 | |
|
| | 56 | | // Messaging systems |
| 765 | 57 | | model.messaging.manager.Initialize(i.world.sceneController); |
| | 58 | |
|
| | 59 | | // Platform systems |
| 765 | 60 | | model.platform.cullingController.Start(); |
| 765 | 61 | | model.platform.parcelScenesCleaner.Initialize(); |
| | 62 | |
|
| | 63 | | // World context systems |
| 765 | 64 | | model.world.sceneController.Initialize(); |
| 765 | 65 | | model.world.pointerEventsController.Initialize(); |
| 765 | 66 | | model.world.state.Initialize(); |
| 765 | 67 | | model.world.blockersController.InitializeWithDefaultDependencies( |
| | 68 | | model.world.state, model.platform.cullingController); |
| 765 | 69 | | model.world.sceneBoundsChecker.Start(); |
| 765 | 70 | | model.world.componentFactory.Initialize(); |
| | 71 | |
|
| | 72 | | // HUD context system |
| 765 | 73 | | model.hud.controller.Initialize(model.hud.factory); |
| 765 | 74 | | } |
| | 75 | |
|
| | 76 | | /// <summary> |
| | 77 | | /// Dispose() and Setup() using the current environment configuration. |
| | 78 | | /// </summary> |
| | 79 | | public static void Reset() |
| | 80 | | { |
| 0 | 81 | | Dispose(); |
| 0 | 82 | | Setup(); |
| 0 | 83 | | } |
| | 84 | |
|
| | 85 | | /// <summary> |
| | 86 | | /// Dispose() all the current environment systems. |
| | 87 | | /// </summary> |
| 1572 | 88 | | public static void Dispose() { i?.Dispose(); } |
| | 89 | |
|
| | 90 | | public class Model |
| | 91 | | { |
| | 92 | | public readonly MessagingContext messaging; |
| | 93 | | public readonly PlatformContext platform; |
| | 94 | | public readonly WorldRuntimeContext world; |
| | 95 | | public readonly HUDContext hud; |
| | 96 | |
|
| 0 | 97 | | public Model () { } |
| | 98 | |
|
| 765 | 99 | | public Model(System.Func<MessagingContext> messagingBuilder, |
| | 100 | | System.Func<PlatformContext> platformBuilder, |
| | 101 | | System.Func<WorldRuntimeContext> worldBuilder, |
| | 102 | | System.Func<HUDContext> hudBuilder) |
| | 103 | | { |
| 765 | 104 | | this.messaging = messagingBuilder(); |
| 765 | 105 | | this.platform = platformBuilder(); |
| 765 | 106 | | this.world = worldBuilder(); |
| 765 | 107 | | this.hud = hudBuilder(); |
| 765 | 108 | | } |
| | 109 | |
|
| | 110 | | public void Dispose() |
| | 111 | | { |
| 786 | 112 | | messaging?.Dispose(); |
| 786 | 113 | | world?.Dispose(); |
| 786 | 114 | | platform?.Dispose(); |
| 786 | 115 | | hud?.Dispose(); |
| 786 | 116 | | } |
| | 117 | | } |
| | 118 | | } |
| | 119 | | } |