-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathBootstraper.cs
More file actions
399 lines (347 loc) · 17.4 KB
/
Copy pathBootstraper.cs
File metadata and controls
399 lines (347 loc) · 17.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
using Arch.Core;
using CommunicationData.URLHelpers;
using Cysharp.Threading.Tasks;
using DCL.Audio;
using DCL.Chat.History;
using DCL.DebugUtilities;
using DCL.Diagnostics;
using DCL.FeatureFlags;
using DCL.Ipfs;
using DCL.Multiplayer.Connections.DecentralandUrls;
using DCL.Notifications.NewNotification;
using DCL.Optimization.PerformanceBudgeting;
using DCL.PerformanceAndDiagnostics.Analytics;
using DCL.PerformanceAndDiagnostics.DotNetLogging;
using DCL.PluginSystem;
using DCL.PluginSystem.Global;
using DCL.RealmNavigation;
using DCL.SceneLoadingScreens.SplashScreen;
using DCL.UI.MainUI;
using DCL.UserInAppInitializationFlow;
using DCL.Utilities.Extensions;
using DCL.Utility;
using DCL.Web3.Authenticators;
using DCL.Web3.Identities;
using DCL.WebRequests.Analytics;
using ECS;
using ECS.StreamableLoading.Cache.Disk;
using ECS.StreamableLoading.Cache.InMemory;
using ECS.StreamableLoading.Common.Components;
using Global.AppArgs;
using Global.Dynamic.RealmUrl;
using Global.Versioning;
using MVC;
using SceneRunner.Debugging;
using SceneRuntime.Factory.JsSource;
using SceneRuntime.Factory.WebSceneSource;
using System;
using System.Threading;
using UnityEngine;
using UnityEngine.UIElements;
using Utility;
using JsCodeResolver = DCL.AssetsProvision.CodeResolver.JsCodeResolver;
namespace Global.Dynamic
{
public class Bootstrap : IBootstrap
{
private readonly DebugSettings.DebugSettings debugSettings;
private readonly RealmUrls realmUrls;
private readonly IAppArgs appArgs;
private readonly SplashScreen splashScreen;
private readonly RealmLaunchSettings realmLaunchSettings;
private readonly WebRequestsContainer webRequestsContainer;
private readonly IDiskCache diskCache;
private readonly IDiskCache<PartialLoadingState> partialsDiskCache;
private readonly HttpFeatureFlagsProvider featureFlagsProvider;
private readonly World world;
private URLDomain? startingRealm;
private Vector2Int startingParcel;
private DynamicWorldDependencies dynamicWorldDependencies;
public bool EnableAnalytics { private get; init; }
public Bootstrap(
DebugSettings.DebugSettings debugSettings,
IAppArgs appArgs,
SplashScreen splashScreen,
RealmUrls realmUrls,
RealmLaunchSettings realmLaunchSettings,
WebRequestsContainer webRequestsContainer,
IDiskCache diskCache,
IDiskCache<PartialLoadingState> partialsDiskCache,
HttpFeatureFlagsProvider featureFlagsProvider,
World world)
{
this.debugSettings = debugSettings;
this.realmUrls = realmUrls;
this.appArgs = appArgs;
this.splashScreen = splashScreen;
this.realmLaunchSettings = realmLaunchSettings;
this.webRequestsContainer = webRequestsContainer;
this.diskCache = diskCache;
this.partialsDiskCache = partialsDiskCache;
this.world = world;
this.featureFlagsProvider = featureFlagsProvider;
}
public async UniTask PreInitializeSetupAsync(CancellationToken token)
{
splashScreen.Show();
string realm = await realmUrls.StartingRealmAsync(token);
startingRealm = URLDomain.FromString(realm);
// Initialize .NET logging ASAP since it might be used by another systems
// Otherwise we might get exceptions in different platforms
DotNetLoggingPlugin.Initialize();
}
public async UniTask<(StaticContainer?, bool)> LoadStaticContainerAsync(
BootstrapContainer bootstrapContainer,
PluginSettingsContainer globalPluginSettingsContainer,
IDebugContainerBuilder debugContainerBuilder,
RealmData realmData,
Entity playerEntity,
ISystemMemoryCap memoryCap,
IAppArgs appArgs,
CancellationToken ct
) =>
await StaticContainer.CreateAsync(
bootstrapContainer.Analytics,
bootstrapContainer.DecentralandUrlsSource,
realmData,
bootstrapContainer.AssetsProvisioner,
bootstrapContainer.ReportHandlingSettings,
debugContainerBuilder,
webRequestsContainer,
globalPluginSettingsContainer,
bootstrapContainer.DiagnosticsContainer,
bootstrapContainer.IdentityCache,
bootstrapContainer.CompositeWeb3Provider,
bootstrapContainer.LaunchMode,
bootstrapContainer.UseRemoteAssetBundles,
world,
playerEntity,
memoryCap,
bootstrapContainer.VolumeBus,
EnableAnalytics,
diskCache,
partialsDiskCache,
ct,
appArgs
);
public async UniTask<(DynamicWorldContainer?, bool)> LoadDynamicWorldContainerAsync(
BootstrapContainer bootstrapContainer,
StaticContainer staticContainer,
PluginSettingsContainer scenePluginSettingsContainer,
DynamicSceneLoaderSettings settings,
DynamicSettings dynamicSettings,
AudioClipConfig backgroundMusic,
WorldInfoTool worldInfoTool,
Entity playerEntity,
IAppArgs appArgs,
ICoroutineRunner coroutineRunner,
DCLVersion dclVersion,
CancellationToken ct)
{
dynamicWorldDependencies = new DynamicWorldDependencies
(
staticContainer.DebugContainerBuilder,
appArgs,
bootstrapContainer.AssetsProvisioner,
staticContainer,
scenePluginSettingsContainer,
dynamicSettings,
bootstrapContainer.CompositeWeb3Provider!,
bootstrapContainer.IdentityCache,
splashScreen,
worldInfoTool
);
string? localSceneDevelopmentRealm = await realmUrls.LocalSceneDevelopmentRealmAsync(ct);
(DynamicWorldContainer? container, bool success) tuple = await DynamicWorldContainer.CreateAsync(
bootstrapContainer,
dynamicWorldDependencies,
new DynamicWorldParams
{
StaticLoadPositions = realmLaunchSettings.GetPredefinedParcels(),
Realms = settings.Realms,
StartParcel = new StartParcel(realmLaunchSettings.targetScene),
EditorPositionOverrideActive = realmLaunchSettings.HasEditorPositionOverride(),
IsolateScenesCommunication = realmLaunchSettings.isolateSceneCommunication,
EnableLandscape = debugSettings.EnableLandscape,
EnableLOD = debugSettings.EnableLOD && realmLaunchSettings.CurrentMode is LaunchMode.Play,
EnableAnalytics = EnableAnalytics,
HybridSceneParams = realmLaunchSettings.CreateHybridSceneParams(),
LocalSceneDevelopmentRealm = localSceneDevelopmentRealm ?? string.Empty,
},
backgroundMusic,
world,
playerEntity,
appArgs,
coroutineRunner,
dclVersion,
realmUrls,
ct);
return tuple;
}
public async UniTask<bool> InitializePluginsAsync(StaticContainer staticContainer, DynamicWorldContainer dynamicWorldContainer,
PluginSettingsContainer scenePluginSettingsContainer, PluginSettingsContainer globalPluginSettingsContainer, IAnalyticsController analyticsController,
CancellationToken ct)
{
var anyFailure = false;
await UniTask.WhenAll(staticContainer.ECSWorldPlugins.Select(gp => scenePluginSettingsContainer.InitializePluginWithAnalyticsAsync(gp, analyticsController, ct).ContinueWith(OnPluginInitialized)).EnsureNotNull());
await UniTask.WhenAll(dynamicWorldContainer.GlobalPlugins.Select(gp => globalPluginSettingsContainer.InitializePluginWithAnalyticsAsync(gp, analyticsController, ct).ContinueWith(OnPluginInitialized)).EnsureNotNull());
void OnPluginInitialized<TPluginInterface>((TPluginInterface plugin, bool success) result) where TPluginInterface: IDCLPlugin
{
if (!result.success)
anyFailure = true;
}
return anyFailure;
}
public async UniTask InitializeFeatureFlagsAsync(IWeb3Identity? identity, IDecentralandUrlsSource decentralandUrlsSource, CancellationToken ct)
{
try { await featureFlagsProvider.InitializeAsync(decentralandUrlsSource, identity?.Address, appArgs, ct); }
catch (Exception e) when (e is not OperationCanceledException)
{
FeatureFlagsConfiguration.Reset();
FeatureFlagsConfiguration.Initialize(new FeatureFlagsConfiguration(FeatureFlagsResultDto.Empty));
ReportHub.LogException(e, new ReportData(ReportCategory.FEATURE_FLAGS));
}
}
public void InitializeFeaturesRegistry()
{
FeaturesRegistry.Initialize(new FeaturesRegistry(appArgs, realmLaunchSettings.CurrentMode is LaunchMode.LocalSceneDevelopment));
// Gate the v49 deps-digest cache-keying scheme behind the feature flag. Off by default means every
// manifest reports SupportsDepsDigests() == false and the entire pipeline takes the legacy code path.
AssetBundleManifestVersion.DepsDigestKeyingEnabled = FeaturesRegistry.Instance.IsEnabled(FeatureId.AB_DEPS_DIGEST_CACHE_KEY);
}
public GlobalWorld CreateGlobalWorld(
BootstrapContainer bootstrapContainer,
StaticContainer staticContainer,
DynamicWorldContainer dynamicWorldContainer,
UIDocument debugUiRoot,
Entity playerEntity
)
{
IWebJsSources webJsSources = new WebJsSources(new JsCodeResolver(
staticContainer.WebRequestsContainer.WebRequestController));
if (realmLaunchSettings.CurrentMode is LaunchMode.Play)
{
var memoryCache = new MemoryCache<string, string>();
staticContainer.CacheCleaner.Register(memoryCache);
webJsSources = new CachedWebJsSources(webJsSources, memoryCache, new DiskCache<string, SerializeMemoryIterator<StringDiskSerializer.State>>(diskCache, new StringDiskSerializer()));
}
SceneSharedContainer sceneSharedContainer = SceneSharedContainer.Create(
in staticContainer,
bootstrapContainer.DecentralandUrlsSource,
bootstrapContainer.IdentityCache,
staticContainer.WebRequestsContainer.SceneWebRequestController,
dynamicWorldContainer.RealmController.RealmData,
dynamicWorldContainer.ProfileRepository,
dynamicWorldContainer.RoomHub,
dynamicWorldContainer.MvcManager,
dynamicWorldContainer.MessagePipesHub,
dynamicWorldContainer.RemoteMetadata,
webJsSources,
bootstrapContainer.Environment,
dynamicWorldContainer.SystemClipboard
);
GlobalWorld globalWorld = dynamicWorldContainer.GlobalWorldFactory.Create(
sceneSharedContainer.SceneFactory, playerEntity);
dynamicWorldContainer.RealmController.GlobalWorld = globalWorld;
staticContainer.PortableExperiencesController.GlobalWorld = globalWorld;
InitializeDebugPanel(staticContainer.DebugContainerBuilder, debugUiRoot);
return globalWorld;
}
public void InitializePlayerEntity(StaticContainer staticContainer, Entity playerEntity)
{
staticContainer.CharacterContainer.InitializePlayerEntity(world, playerEntity);
}
public async UniTask LoadStartingRealmAsync(DynamicWorldContainer dynamicWorldContainer, CancellationToken ct)
{
string realm = await realmUrls.StartingRealmAsync(ct);
startingRealm = URLDomain.FromString(realm);
if (startingRealm.HasValue == false)
throw new InvalidOperationException("Starting realm is not set");
if (realmLaunchSettings.initialRealm is InitialRealm.World)
{
bool isAuthorized = await dynamicWorldContainer.RealmController
.IsUserAuthorisedToAccessWorldAsync(startingRealm.Value, ct);
if (!isAuthorized)
{
ReportHub.LogWarning(ReportCategory.REALM,
$"[Bootstrap] Startup world '{realmLaunchSettings.TargetWorld}' is not authorized for auto-entry, falling back to Genesis.");
dynamicWorldContainer.ChatHistory.AddMessage(
ChatChannel.NEARBY_CHANNEL_ID,
ChatChannel.ChatChannelType.NEARBY,
ChatMessage.NewFromSystem($"Could not auto-enter '{realmLaunchSettings.TargetWorld}' due to world permissions. You were sent to Genesis Plaza."));
await dynamicWorldContainer.RealmController
.SetRealmAsync(URLDomain.FromString(realmUrls.GenesisRealm()), ct);
return;
}
}
await dynamicWorldContainer.RealmController.SetRealmAsync(startingRealm.Value, ct);
}
public void ApplyFeatureFlagConfigs(FeatureFlagsConfiguration featureFlagsConfigurationCache)
{
realmLaunchSettings.CheckStartParcelOverride(appArgs, featureFlagsConfigurationCache);
webRequestsContainer.SetKTXEnabled(featureFlagsConfigurationCache.IsEnabled(FeatureFlagsStrings.KTX2_CONVERSION));
}
public async UniTask UserInitializationAsync(DynamicWorldContainer dynamicWorldContainer,
BootstrapContainer bootstrapContainer,
GlobalWorld globalWorld, Entity playerEntity, CancellationToken ct)
{
splashScreen.Show();
IWeb3Authenticator authenticator = new TokenFileAuthenticator(
URLAddress.FromString(bootstrapContainer.DecentralandUrlsSource.Url(DecentralandUrl.ApiAuth)),
webRequestsContainer.WebRequestController,
bootstrapContainer.Web3AccountFactory);
if (EnableAnalytics)
authenticator = new TrackedTokenFileAuthenticator((TokenFileAuthenticator)authenticator, bootstrapContainer.Analytics.Controller);
try
{
IWeb3Identity identity = await authenticator.LoginAsync(new LoginPayload(), ct); // doesn't use payload
bootstrapContainer.IdentityCache!.Identity = identity;
if (EnableAnalytics)
bootstrapContainer.Analytics.Controller.Identify(identity);
}
catch (AutoLoginTokenNotFoundException) { } // Exceptions on auto-login should not block the application bootstrap
catch (AutoLoginTokenInvalidException e) { ReportHub.LogException(e, ReportCategory.AUTHENTICATION); }
catch (Exception e) { ReportHub.LogException(e, ReportCategory.AUTHENTICATION); }
await dynamicWorldContainer.UserInAppInAppInitializationFlow.ExecuteAsync(
new UserInAppInitializationFlowParameters
(
showAuthentication: debugSettings.ShowAuthentication,
showLoading: debugSettings.ShowLoading,
IUserInAppInitializationFlow.LoadSource.StartUp,
world: globalWorld.EcsWorld,
playerEntity: playerEntity
), ct);
OpenDefaultUI(dynamicWorldContainer.MvcManager, ct);
splashScreen.Hide();
}
private void OpenDefaultUI(IMVCManager mvcManager, CancellationToken ct)
{
mvcManager.ShowAsync(NewNotificationController.IssueCommand(), ct).Forget();
mvcManager.ShowAsync(MainUIController.IssueCommand(), ct).Forget();
if (appArgs.HasFlag(AppArgsFlags.DISABLE_HUD))
DisableHudOnStartupAsync(mvcManager, ct).Forget();
}
internal static async UniTask DisableHudOnStartupAsync(IMVCManager mvcManager, CancellationToken ct)
{
try
{
// Wait a frame so lazily-mounted MVC views exist before toggling.
await UniTask.NextFrame(ct).SuppressCancellationThrow();
if (ct.IsCancellationRequested)
return;
// Bypass ToggleUIRequest (used by U key) so scene SDK UIDocuments stay visible.
mvcManager.SetAllViewsCanvasActive(false);
}
catch (Exception e) { ReportHub.LogException(e, ReportCategory.STARTUP); }
}
private void InitializeDebugPanel(IDebugContainerBuilder debugContainerBuilder, UIDocument debugUiRoot)
{
debugContainerBuilder.BuildWithFlex(debugUiRoot);
bool hasDebugFlag = appArgs.HasDebugFlag();
// Make Debug Panel available
debugContainerBuilder.IsVisible = hasDebugFlag || appArgs.HasFlag(AppArgsFlags.LOCAL_SCENE);
// Start application with Debug Panel open/closed
debugContainerBuilder.Container.SetPanelVisibility(hasDebugFlag);
}
}
}