-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathSceneFactory.cs
More file actions
296 lines (263 loc) · 13.1 KB
/
Copy pathSceneFactory.cs
File metadata and controls
296 lines (263 loc) · 13.1 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
using CommunicationData.URLHelpers;
using CRDT.Serializer;
using CrdtEcsBridge.Components;
using CrdtEcsBridge.JsModulesImplementation;
using CrdtEcsBridge.JsModulesImplementation.Communications;
using CrdtEcsBridge.JsModulesImplementation.Communications.SDKMessageBus;
using CrdtEcsBridge.PoolsProviders;
using CrdtEcsBridge.RestrictedActions;
using Cysharp.Threading.Tasks;
using DCL.Clipboard;
using DCL.Interaction.Utility;
using DCL.Ipfs;
using DCL.Multiplayer.Connections.DecentralandUrls;
using DCL.Multiplayer.Connections.RoomHubs;
using DCL.Multiplayer.Profiles.Poses;
using DCL.Profiles;
using DCL.SkyBox;
using DCL.Web3;
using DCL.Web3.Identities;
using DCL.WebRequests;
using ECS;
using ECS.Prioritization.Components;
using Microsoft.ClearScript;
using MVC;
using PortableExperiences.Controller;
using SceneRunner.ECSWorld;
using SceneRunner.Scene;
using SceneRunner.Scene.ExceptionsHandling;
using SceneRuntime;
using SceneRuntime.Apis.Modules.EngineApi.SDKObservableEvents;
using SceneRuntime.Factory;
using SceneRuntime.ScenePermissions;
using System;
using System.Threading;
using System.Threading.Tasks;
using UnityEngine;
using UnityEngine.Assertions;
using UnityEngine.Networking;
using Utility;
using Utility.Multithreading;
namespace SceneRunner
{
public class SceneFactory : ISceneFactory
{
private const bool ENABLE_SDK_OBSERVABLES = true;
private readonly ICRDTSerializer crdtSerializer;
private readonly IECSWorldFactory ecsWorldFactory;
private readonly ISceneEntityFactory entityFactory;
private readonly IEntityCollidersGlobalCache entityCollidersGlobalCache;
private readonly IEthereumApi ethereumApi;
private readonly IProfileRepository profileRepository;
private readonly IWeb3IdentityCache identityCache;
private readonly IDecentralandUrlsSource decentralandUrlsSource;
private readonly IWebRequestController webRequestController;
private readonly IRoomHub roomHub;
private readonly SceneRuntimeFactory sceneRuntimeFactory;
private readonly ISDKComponentsRegistry sdkComponentsRegistry;
private readonly ISharedPoolsProvider sharedPoolsProvider;
private readonly IMVCManager mvcManager;
private readonly IRealmData realmData;
private readonly IPortableExperiencesController portableExperiencesController;
private readonly SkyboxSettingsAsset skyboxSettings;
private readonly ISceneCommunicationPipe messagePipesHub;
private readonly IRemoteMetadata remoteMetadata;
private readonly DecentralandEnvironment dclEnvironment;
private readonly ISystemClipboard systemClipboard;
private readonly string installSource;
private IGlobalWorldActions globalWorldActions = null!;
public SceneFactory(
IECSWorldFactory ecsWorldFactory,
SceneRuntimeFactory sceneRuntimeFactory,
ISharedPoolsProvider sharedPoolsProvider,
ICRDTSerializer crdtSerializer,
ISDKComponentsRegistry sdkComponentsRegistry,
ISceneEntityFactory entityFactory,
IEntityCollidersGlobalCache entityCollidersGlobalCache,
IEthereumApi ethereumApi,
IMVCManager mvcManager,
IProfileRepository profileRepository,
IWeb3IdentityCache identityCache,
IDecentralandUrlsSource decentralandUrlsSource,
IWebRequestController webRequestController,
IRoomHub roomHub,
IRealmData realmData,
IPortableExperiencesController portableExperiencesController,
SkyboxSettingsAsset skyboxSettings,
ISceneCommunicationPipe messagePipesHub,
IRemoteMetadata remoteMetadata,
DecentralandEnvironment dclEnvironment,
ISystemClipboard systemClipboard,
string installSource)
{
Assert.IsNotNull(realmData, $"{nameof(realmData)} must not be null");
this.ecsWorldFactory = ecsWorldFactory;
this.sceneRuntimeFactory = sceneRuntimeFactory;
this.sharedPoolsProvider = sharedPoolsProvider;
this.crdtSerializer = crdtSerializer;
this.sdkComponentsRegistry = sdkComponentsRegistry;
this.entityFactory = entityFactory;
this.entityCollidersGlobalCache = entityCollidersGlobalCache;
this.ethereumApi = ethereumApi;
this.mvcManager = mvcManager;
this.profileRepository = profileRepository;
this.identityCache = identityCache;
this.decentralandUrlsSource = decentralandUrlsSource;
this.webRequestController = webRequestController;
this.roomHub = roomHub;
this.systemClipboard = systemClipboard;
this.realmData = realmData;
this.portableExperiencesController = portableExperiencesController;
this.skyboxSettings = skyboxSettings;
this.messagePipesHub = messagePipesHub;
this.remoteMetadata = remoteMetadata;
this.dclEnvironment = dclEnvironment;
this.installSource = installSource;
}
public async UniTask<ISceneFacade> CreateSceneFromFileAsync(string jsCodeUrl, IPartitionComponent partitionProvider, CancellationToken ct, string id = "")
{
int lastSlash = jsCodeUrl.LastIndexOf("/", StringComparison.Ordinal);
string mainScenePath = jsCodeUrl[(lastSlash + 1)..];
var baseUrl = URLDomain.FromString(jsCodeUrl[..(lastSlash + 1)]);
var sceneDefinition = new SceneEntityDefinition(
id,
new SceneMetadata
{
main = mainScenePath,
runtimeVersion = "7",
}
);
var sceneData = new SceneData(new SceneNonHashedContent(baseUrl), sceneDefinition, Vector2Int.zero,
ParcelMathHelper.UNDEFINED_SCENE_GEOMETRY, Array.Empty<Vector2Int>(), StaticSceneMessages.EMPTY, new ISceneData.FakeInitialSceneState());
return await CreateSceneAsync(sceneData, new AllowEverythingJsApiPermissionsProvider(), partitionProvider, ct);
}
public async UniTask<ISceneFacade> CreateSceneFromStreamableDirectoryAsync(string directoryName, IPartitionComponent partitionProvider, CancellationToken ct)
{
const string SCENE_JSON_FILE_NAME = "scene.json";
var fullPath = URLDomain.FromString($"file://{Application.dataPath}/Scenes/TestJsScenes/{directoryName}/");
string rawSceneJsonPath = fullPath.Value + SCENE_JSON_FILE_NAME;
using var request = UnityWebRequest.Get(rawSceneJsonPath);
await request.SendWebRequest().WithCancellation(ct);
SceneMetadata sceneMetadata = JsonUtility.FromJson<SceneMetadata>(request.downloadHandler.text);
var sceneDefinition = new SceneEntityDefinition(directoryName, sceneMetadata);
var sceneData = new SceneData(new SceneNonHashedContent(fullPath), sceneDefinition,
Vector2Int.zero, ParcelMathHelper.UNDEFINED_SCENE_GEOMETRY, Array.Empty<Vector2Int>(), StaticSceneMessages.EMPTY, new ISceneData.FakeInitialSceneState());
return await CreateSceneAsync(sceneData, new AllowEverythingJsApiPermissionsProvider(), partitionProvider, ct);
}
public UniTask<ISceneFacade> CreateSceneFromSceneDefinition(ISceneData sceneData, IJsApiPermissionsProvider permissionsProvider, IPartitionComponent partitionProvider, CancellationToken ct) =>
CreateSceneAsync(sceneData, permissionsProvider, partitionProvider, ct);
public void SetGlobalWorldActions(IGlobalWorldActions actions)
{
globalWorldActions = actions;
}
private async UniTask<ISceneFacade> CreateSceneAsync(ISceneData sceneData, IJsApiPermissionsProvider permissionsProvider, IPartitionComponent partitionProvider, CancellationToken ct)
{
var deps = new SceneInstanceDependencies(sdkComponentsRegistry, entityCollidersGlobalCache, sceneData, permissionsProvider, partitionProvider, ecsWorldFactory, entityFactory);
// Try to create scene runtime
SceneRuntimeImpl sceneRuntime;
try { sceneRuntime = await sceneRuntimeFactory.CreateByPathAsync(deps.SceneCodeUrl, deps.PoolsProvider, sceneData.SceneShortInfo, ct, SceneRuntimeFactory.InstantiationBehavior.SwitchToThreadPool); }
catch (Exception e)
{
await ReportExceptionAsync(e, deps, deps.ExceptionsHandler);
throw;
}
if (ct.IsCancellationRequested)
{
await UniTask.SwitchToMainThread(PlayerLoopTiming.Initialization);
deps.Dispose();
sceneRuntime?.Dispose();
throw new OperationCanceledException();
}
SceneInstanceDependencies.WithRuntimeAndJsAPIBase runtimeDeps;
var engineAPIMutexOwner = new MultiThreadSync.Owner(nameof(EngineAPIImplementation));
var ethereumApiImpl = new RestrictedEthereumApi(ethereumApi, permissionsProvider);
if (ENABLE_SDK_OBSERVABLES)
{
var sdkCommsControllerAPI = new SDKMessageBusCommsAPIImplementation(sceneData, messagePipesHub, sceneRuntime);
sceneRuntime.RegisterSDKMessageBusCommsApi(sdkCommsControllerAPI);
runtimeDeps = new SceneInstanceDependencies.WithRuntimeJsAndSDKObservablesEngineAPI(deps, sceneRuntime,
sharedPoolsProvider, crdtSerializer, mvcManager, globalWorldActions, realmData, messagePipesHub,
webRequestController, skyboxSettings, engineAPIMutexOwner, profileRepository, systemClipboard, roomHub, installSource);
sceneRuntime.RegisterAll(
(ISDKObservableEventsEngineApi)runtimeDeps.EngineAPI,
sdkCommsControllerAPI,
deps.ExceptionsHandler,
roomHub,
profileRepository,
runtimeDeps.SceneApiImplementation,
webRequestController,
runtimeDeps.RestrictedActionsAPI,
runtimeDeps.RuntimeImplementation,
ethereumApiImpl,
runtimeDeps.WebSocketAipImplementation,
identityCache,
dclEnvironment,
runtimeDeps.CommunicationsControllerAPI,
deps.PoolsProvider,
runtimeDeps.SimpleFetchApi,
sceneData,
realmData,
portableExperiencesController,
remoteMetadata,
messagePipesHub
);
}
else
{
runtimeDeps = new SceneInstanceDependencies.WithRuntimeAndJsAPI(deps, sceneRuntime, sharedPoolsProvider,
crdtSerializer, mvcManager, globalWorldActions, realmData, messagePipesHub, webRequestController,
skyboxSettings, engineAPIMutexOwner, profileRepository, systemClipboard, roomHub, installSource);
sceneRuntime.RegisterAll(
runtimeDeps.EngineAPI,
deps.ExceptionsHandler,
roomHub,
profileRepository,
runtimeDeps.SceneApiImplementation,
webRequestController,
runtimeDeps.RestrictedActionsAPI,
dclEnvironment,
runtimeDeps.RuntimeImplementation,
ethereumApiImpl,
runtimeDeps.WebSocketAipImplementation,
identityCache,
runtimeDeps.CommunicationsControllerAPI,
deps.PoolsProvider,
runtimeDeps.SimpleFetchApi,
sceneData,
realmData,
portableExperiencesController,
remoteMetadata,
messagePipesHub
);
}
try
{
sceneRuntime.ExecuteSceneJson();
}
catch (Exception e)
{
await ReportExceptionAsync(e, runtimeDeps, deps.ExceptionsHandler);
throw;
}
if (sceneData.IsPortableExperience())
{
return new PortableExperienceSceneFacade(
sceneData,
runtimeDeps
);
}
return new SceneFacade(
sceneData,
runtimeDeps
);
}
private static async Task ReportExceptionAsync<T>(Exception e, T deps, ISceneExceptionsHandler exceptionsHandler) where T : IDisposable
{
// ScriptEngineException.ErrorDetails is ignored through the logging process which is vital in the reporting information
if (e is ScriptEngineException scriptEngineException)
exceptionsHandler.OnJavaScriptException(new ScriptEngineException(scriptEngineException.ErrorDetails));
await UniTask.SwitchToMainThread(PlayerLoopTiming.Initialization);
deps.Dispose();
}
}
}