| | 1 | | using System; |
| | 2 | | using DCL.Components; |
| | 3 | | using DCL.Controllers; |
| | 4 | | using DCL.Helpers; |
| | 5 | | using DCL.SettingsCommon; |
| | 6 | | using UnityEngine; |
| | 7 | | using UnityEngine.Serialization; |
| | 8 | |
|
| | 9 | | namespace DCL |
| | 10 | | { |
| | 11 | | /// <summary> |
| | 12 | | /// This is the InitialScene entry point. |
| | 13 | | /// Most of the application subsystems should be initialized from this class Awake() event. |
| | 14 | | /// </summary> |
| | 15 | | public class Main : MonoBehaviour |
| | 16 | | { |
| | 17 | | [SerializeField] private bool disableSceneDependencies; |
| 0 | 18 | | public static Main i { get; private set; } |
| | 19 | |
|
| | 20 | | public PoolableComponentFactory componentFactory; |
| | 21 | |
|
| | 22 | | private PerformanceMetricsController performanceMetricsController; |
| | 23 | | protected IKernelCommunication kernelCommunication; |
| | 24 | |
|
| | 25 | | private PluginSystem pluginSystem; |
| | 26 | |
|
| | 27 | | protected virtual void Awake() |
| | 28 | | { |
| 0 | 29 | | if (i != null) |
| | 30 | | { |
| 0 | 31 | | Utils.SafeDestroy(this); |
| 0 | 32 | | return; |
| | 33 | | } |
| | 34 | |
|
| 0 | 35 | | i = this; |
| | 36 | |
|
| 0 | 37 | | Settings.CreateSharedInstance(new DefaultSettingsFactory()); |
| | 38 | |
|
| 0 | 39 | | if (!disableSceneDependencies) |
| 0 | 40 | | InitializeSceneDependencies(); |
| | 41 | |
|
| 0 | 42 | | if (!Configuration.EnvironmentSettings.RUNNING_TESTS) |
| | 43 | | { |
| 0 | 44 | | performanceMetricsController = new PerformanceMetricsController(); |
| 0 | 45 | | RenderProfileManifest.i.Initialize(); |
| 0 | 46 | | SetupEnvironment(); |
| | 47 | |
|
| 0 | 48 | | DataStore.i.HUDs.loadingHUD.visible.OnChange += OnLoadingScreenVisibleStateChange; |
| | 49 | | } |
| | 50 | |
|
| 0 | 51 | | SetupPlugins(); |
| | 52 | |
|
| | 53 | | #if UNITY_WEBGL && !UNITY_EDITOR |
| | 54 | | Debug.Log("DCL Unity Build Version: " + DCL.Configuration.ApplicationSettings.version); |
| | 55 | | Debug.unityLogger.logEnabled = false; |
| | 56 | |
|
| | 57 | | kernelCommunication = new NativeBridgeCommunication(Environment.i.world.sceneController); |
| | 58 | | #else |
| | 59 | | // TODO(Brian): Remove this branching once we finish migrating all tests out of the |
| | 60 | | // IntegrationTestSuite_Legacy base class. |
| 0 | 61 | | if (!Configuration.EnvironmentSettings.RUNNING_TESTS) |
| | 62 | | { |
| | 63 | | #if UNITY_STANDALONE && !UNITY_EDITOR |
| | 64 | | kernelCommunication = new WebSocketCommunication(true); |
| | 65 | | #else |
| 0 | 66 | | kernelCommunication = new WebSocketCommunication(DebugConfigComponent.i.webSocketSSL); |
| | 67 | | #endif |
| | 68 | | } |
| | 69 | | #endif |
| | 70 | |
|
| | 71 | | // TODO(Brian): This is a temporary fix to address elevators issue in the xmas event. |
| | 72 | | // We should re-enable this later as produces a performance regression. |
| 0 | 73 | | if (!Configuration.EnvironmentSettings.RUNNING_TESTS) |
| 0 | 74 | | Environment.i.platform.cullingController.SetAnimationCulling(false); |
| 0 | 75 | | } |
| | 76 | |
|
| | 77 | | void OnLoadingScreenVisibleStateChange(bool newVisibleValue, bool previousVisibleValue) |
| | 78 | | { |
| 0 | 79 | | if (newVisibleValue) |
| | 80 | | { |
| | 81 | | // Prewarm shader variants |
| 0 | 82 | | Resources.Load<ShaderVariantCollection>("ShaderVariantCollections/shaderVariants-selected").WarmUp(); |
| 0 | 83 | | DataStore.i.HUDs.loadingHUD.visible.OnChange -= OnLoadingScreenVisibleStateChange; |
| | 84 | | } |
| 0 | 85 | | } |
| | 86 | |
|
| | 87 | | protected virtual void SetupPlugins() |
| | 88 | | { |
| 0 | 89 | | pluginSystem = PluginSystemFactory.Create(); |
| 0 | 90 | | } |
| | 91 | |
|
| | 92 | | protected virtual void SetupEnvironment() |
| | 93 | | { |
| 0 | 94 | | Environment.SetupWithBuilders( |
| | 95 | | messagingBuilder: MessagingContextBuilder, |
| | 96 | | platformBuilder: PlatformContextBuilder, |
| | 97 | | worldRuntimeBuilder: WorldRuntimeContextBuilder, |
| | 98 | | hudBuilder: HUDContextBuilder); |
| 0 | 99 | | } |
| | 100 | |
|
| 0 | 101 | | protected virtual MessagingContext MessagingContextBuilder() { return MessagingContextFactory.CreateDefault(); } |
| | 102 | |
|
| 0 | 103 | | protected virtual PlatformContext PlatformContextBuilder() { return PlatformContextFactory.CreateDefault(); } |
| | 104 | |
|
| 0 | 105 | | protected virtual WorldRuntimeContext WorldRuntimeContextBuilder() { return WorldRuntimeContextFactory.CreateDef |
| | 106 | |
|
| 0 | 107 | | protected virtual HUDContext HUDContextBuilder() { return HUDContextFactory.CreateDefault(); } |
| | 108 | |
|
| | 109 | | protected virtual void Start() |
| | 110 | | { |
| | 111 | | // this event should be the last one to be executed after initialization |
| | 112 | | // it is used by the kernel to signal "EngineReady" or something like that |
| | 113 | | // to prevent race conditions like "SceneController is not an object", |
| | 114 | | // aka sending events before unity is ready |
| 0 | 115 | | DCL.Interface.WebInterface.SendSystemInfoReport(); |
| | 116 | |
|
| | 117 | | // We trigger the Decentraland logic once everything is initialized. |
| 0 | 118 | | DCL.Interface.WebInterface.StartDecentraland(); |
| | 119 | |
|
| 0 | 120 | | Environment.i.world.sceneController.Start(); |
| 0 | 121 | | } |
| | 122 | |
|
| | 123 | | protected virtual void Update() |
| | 124 | | { |
| 0 | 125 | | Environment.i.platform.Update(); |
| 0 | 126 | | performanceMetricsController?.Update(); |
| 0 | 127 | | } |
| | 128 | |
|
| | 129 | | protected virtual void OnDestroy() |
| | 130 | | { |
| 0 | 131 | | DataStore.i.HUDs.loadingHUD.visible.OnChange -= OnLoadingScreenVisibleStateChange; |
| | 132 | |
|
| 0 | 133 | | DataStore.i.common.isWorldBeingDestroyed.Set(true); |
| | 134 | |
|
| 0 | 135 | | pluginSystem?.Dispose(); |
| | 136 | |
|
| 0 | 137 | | if (!Configuration.EnvironmentSettings.RUNNING_TESTS) |
| 0 | 138 | | Environment.Dispose(); |
| | 139 | |
|
| 0 | 140 | | kernelCommunication?.Dispose(); |
| 0 | 141 | | } |
| | 142 | |
|
| | 143 | | protected virtual void InitializeSceneDependencies() |
| | 144 | | { |
| 0 | 145 | | gameObject.AddComponent<UserProfileController>(); |
| 0 | 146 | | gameObject.AddComponent<RenderingController>(); |
| 0 | 147 | | gameObject.AddComponent<CatalogController>(); |
| 0 | 148 | | gameObject.AddComponent<MinimapMetadataController>(); |
| 0 | 149 | | gameObject.AddComponent<ChatController>(); |
| 0 | 150 | | gameObject.AddComponent<FriendsController>(); |
| 0 | 151 | | gameObject.AddComponent<LoadingFeedbackController>(); |
| 0 | 152 | | gameObject.AddComponent<HotScenesController>(); |
| 0 | 153 | | gameObject.AddComponent<GIFProcessingBridge>(); |
| 0 | 154 | | gameObject.AddComponent<RenderProfileBridge>(); |
| 0 | 155 | | gameObject.AddComponent<AssetCatalogBridge>(); |
| 0 | 156 | | gameObject.AddComponent<ScreenSizeWatcher>(); |
| 0 | 157 | | gameObject.AddComponent<SceneControllerBridge>(); |
| | 158 | |
|
| 0 | 159 | | MainSceneFactory.CreateBuilderInWorldBridge(gameObject); |
| 0 | 160 | | MainSceneFactory.CreateBridges(); |
| 0 | 161 | | MainSceneFactory.CreateMouseCatcher(); |
| 0 | 162 | | MainSceneFactory.CreatePlayerSystems(); |
| 0 | 163 | | MainSceneFactory.CreateEnvironment(); |
| 0 | 164 | | MainSceneFactory.CreateAudioHandler(); |
| 0 | 165 | | MainSceneFactory.CreateHudController(); |
| 0 | 166 | | MainSceneFactory.CreateSettingsController(); |
| 0 | 167 | | MainSceneFactory.CreateNavMap(); |
| 0 | 168 | | MainSceneFactory.CreateEventSystem(); |
| 0 | 169 | | MainSceneFactory.CreateInteractionHoverCanvas(); |
| 0 | 170 | | } |
| | 171 | | } |
| | 172 | | } |