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