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