| | 1 | | using System; |
| | 2 | | using DCL.Components; |
| | 3 | | using DCL.Controllers; |
| | 4 | | using DCL.Helpers; |
| | 5 | | using UnityEngine; |
| | 6 | | using UnityEngine.Serialization; |
| | 7 | |
|
| | 8 | | namespace DCL |
| | 9 | | { |
| | 10 | | /// <summary> |
| | 11 | | /// This is the InitialScene entry point. |
| | 12 | | /// Most of the application subsystems should be initialized from this class Awake() event. |
| | 13 | | /// </summary> |
| | 14 | | public class Main : MonoBehaviour |
| | 15 | | { |
| | 16 | | [SerializeField] private bool disableSceneDependencies; |
| 0 | 17 | | public static Main i { get; private set; } |
| | 18 | |
|
| | 19 | | public PoolableComponentFactory componentFactory; |
| | 20 | |
|
| | 21 | | private PerformanceMetricsController performanceMetricsController; |
| | 22 | | private IKernelCommunication kernelCommunication; |
| | 23 | |
|
| | 24 | | private PluginSystem pluginSystem; |
| | 25 | |
|
| | 26 | | protected virtual void Awake() |
| | 27 | | { |
| 125 | 28 | | if (i != null) |
| | 29 | | { |
| 0 | 30 | | Utils.SafeDestroy(this); |
| 0 | 31 | | return; |
| | 32 | | } |
| | 33 | |
|
| 125 | 34 | | i = this; |
| | 35 | |
|
| 125 | 36 | | if (!disableSceneDependencies) |
| 104 | 37 | | InitializeSceneDependencies(); |
| | 38 | |
|
| 125 | 39 | | if (!Configuration.EnvironmentSettings.RUNNING_TESTS) |
| | 40 | | { |
| 0 | 41 | | performanceMetricsController = new PerformanceMetricsController(); |
| 0 | 42 | | RenderProfileManifest.i.Initialize(); |
| 0 | 43 | | SetupEnvironment(); |
| | 44 | | } |
| | 45 | |
|
| 125 | 46 | | SetupPlugins(); |
| | 47 | |
|
| | 48 | | #if UNITY_WEBGL && !UNITY_EDITOR |
| | 49 | | Debug.Log("DCL Unity Build Version: " + DCL.Configuration.ApplicationSettings.version); |
| | 50 | | Debug.unityLogger.logEnabled = false; |
| | 51 | |
|
| | 52 | | kernelCommunication = new NativeBridgeCommunication(Environment.i.world.sceneController); |
| | 53 | | #else |
| | 54 | | // TODO(Brian): Remove this branching once we finish migrating all tests out of the |
| | 55 | | // IntegrationTestSuite_Legacy base class. |
| 125 | 56 | | if (!Configuration.EnvironmentSettings.RUNNING_TESTS) |
| | 57 | | { |
| 0 | 58 | | kernelCommunication = new WebSocketCommunication(); |
| | 59 | | } |
| | 60 | | #endif |
| | 61 | |
|
| | 62 | | // TODO(Brian): This is a temporary fix to address elevators issue in the xmas event. |
| | 63 | | // We should re-enable this later as produces a performance regression. |
| 125 | 64 | | if (!Configuration.EnvironmentSettings.RUNNING_TESTS) |
| 0 | 65 | | Environment.i.platform.cullingController.SetAnimationCulling(false); |
| 125 | 66 | | } |
| | 67 | |
|
| | 68 | | protected virtual void SetupPlugins() |
| | 69 | | { |
| 125 | 70 | | pluginSystem = PluginSystemFactory.Create(); |
| 125 | 71 | | } |
| | 72 | |
|
| | 73 | | protected virtual void SetupEnvironment() |
| | 74 | | { |
| 0 | 75 | | Environment.SetupWithBuilders( |
| | 76 | | messagingBuilder: MessagingContextBuilder, |
| | 77 | | platformBuilder: PlatformContextBuilder, |
| | 78 | | worldRuntimeBuilder: WorldRuntimeContextBuilder, |
| | 79 | | hudBuilder: HUDContextBuilder); |
| 0 | 80 | | } |
| | 81 | |
|
| 0 | 82 | | protected virtual MessagingContext MessagingContextBuilder() { return MessagingContextFactory.CreateDefault(); } |
| | 83 | |
|
| 0 | 84 | | protected virtual PlatformContext PlatformContextBuilder() { return PlatformContextFactory.CreateDefault(); } |
| | 85 | |
|
| 0 | 86 | | protected virtual WorldRuntimeContext WorldRuntimeContextBuilder() { return WorldRuntimeContextFactory.CreateDef |
| | 87 | |
|
| 0 | 88 | | protected virtual HUDContext HUDContextBuilder() { return HUDContextFactory.CreateDefault(); } |
| | 89 | |
|
| | 90 | | private void Start() |
| | 91 | | { |
| | 92 | | // this event should be the last one to be executed after initialization |
| | 93 | | // it is used by the kernel to signal "EngineReady" or something like that |
| | 94 | | // to prevent race conditions like "SceneController is not an object", |
| | 95 | | // aka sending events before unity is ready |
| 125 | 96 | | DCL.Interface.WebInterface.SendSystemInfoReport(); |
| | 97 | |
|
| | 98 | | // We trigger the Decentraland logic once everything is initialized. |
| 125 | 99 | | DCL.Interface.WebInterface.StartDecentraland(); |
| | 100 | |
|
| 125 | 101 | | Environment.i.world.sceneController.Start(); |
| 125 | 102 | | } |
| | 103 | |
|
| | 104 | | protected virtual void Update() |
| | 105 | | { |
| 21447 | 106 | | Environment.i.platform.Update(); |
| 21447 | 107 | | Environment.i.world.sceneController.Update(); |
| 21447 | 108 | | performanceMetricsController?.Update(); |
| 0 | 109 | | } |
| | 110 | |
|
| | 111 | | protected virtual void LateUpdate() |
| | 112 | | { |
| 21446 | 113 | | Environment.i.world.sceneController.LateUpdate(); |
| 21446 | 114 | | } |
| | 115 | |
|
| | 116 | | protected virtual void OnDestroy() |
| | 117 | | { |
| 124 | 118 | | if (!Configuration.EnvironmentSettings.RUNNING_TESTS) |
| 0 | 119 | | Environment.Dispose(); |
| 124 | 120 | | pluginSystem?.Dispose(); |
| 124 | 121 | | kernelCommunication?.Dispose(); |
| 0 | 122 | | } |
| | 123 | |
|
| | 124 | | protected virtual void InitializeSceneDependencies() |
| | 125 | | { |
| 104 | 126 | | var bridges = Init("Bridges"); |
| 104 | 127 | | var mouseCatcher = Init("MouseCatcher").GetComponent<MouseCatcher>(); |
| 104 | 128 | | var environment = Init("Environment").GetComponent<EnvironmentReferences>(); |
| 104 | 129 | | var playerReferences = Init("Player").GetComponent<PlayerReferences>(); |
| | 130 | |
|
| 104 | 131 | | Init("HUDController"); |
| 104 | 132 | | Init("HUDAudioHandler"); |
| 104 | 133 | | Init("NavMap"); |
| 104 | 134 | | Init("SettingsController"); |
| | 135 | |
|
| 104 | 136 | | SceneReferences.i.Initialize( |
| | 137 | | mouseCatcher, |
| | 138 | | environment.ground, |
| | 139 | | playerReferences.biwCameraRoot, |
| | 140 | | playerReferences.inputController, |
| | 141 | | playerReferences.cursorCanvas, |
| | 142 | | gameObject, |
| | 143 | | playerReferences.avatarController, |
| | 144 | | playerReferences.cameraController, |
| | 145 | | playerReferences.mainCamera, |
| | 146 | | bridges, |
| | 147 | | environment.environmentLight, |
| | 148 | | environment.postProcessVolume, |
| | 149 | | playerReferences.thirdPersonCamera, |
| | 150 | | playerReferences.firstPersonCamera); |
| 104 | 151 | | } |
| | 152 | |
|
| | 153 | | private static GameObject Init(string name) |
| | 154 | | { |
| 832 | 155 | | GameObject instance = Instantiate(Resources.Load(name)) as GameObject; |
| 832 | 156 | | instance.name = name; |
| 832 | 157 | | return instance; |
| | 158 | | } |
| | 159 | | } |
| | 160 | | } |