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