-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathLoadSmartWearableSceneSystem.cs
More file actions
117 lines (100 loc) · 5.53 KB
/
Copy pathLoadSmartWearableSceneSystem.cs
File metadata and controls
117 lines (100 loc) · 5.53 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
using Arch.SystemGroups;
using CommunicationData.URLHelpers;
using Cysharp.Threading.Tasks;
using DCL.AvatarRendering.Wearables.Components;
using DCL.AvatarRendering.Wearables.Helpers;
using DCL.Diagnostics;
using DCL.Ipfs;
using DCL.Multiplayer.Connections.DecentralandUrls;
using DCL.WebRequests;
using ECS.Prioritization.Components;
using ECS.SceneLifeCycle.SceneDefinition;
using ECS.StreamableLoading.Cache;
using ECS.StreamableLoading.Common.Components;
using ECS.StreamableLoading.Common.Systems;
using Runtime.Wearables;
using SceneRunner;
using SceneRunner.Scene;
using SceneRuntime.ScenePermissions;
using System;
using System.Threading;
namespace ECS.SceneLifeCycle.Systems
{
/// <summary>
/// Handles the <see cref="GetSmartWearableSceneIntention"/> to load the actual scene associated with the smart wearable.
/// </summary>
[UpdateInGroup(typeof(RealmGroup))]
public partial class LoadSmartWearableSceneSystem : LoadSystemBase<GetSmartWearableSceneIntention.Result, GetSmartWearableSceneIntention>
{
private readonly ISceneFactory sceneFactory;
private readonly IWebRequestController webRequestController;
private readonly SmartWearableCache smartWearableCache;
private readonly IDecentralandUrlsSource urlsSource;
public LoadSmartWearableSceneSystem(
Arch.Core.World world,
IStreamableCache<GetSmartWearableSceneIntention.Result, GetSmartWearableSceneIntention> cache,
IWebRequestController webRequestController,
ISceneFactory sceneFactory,
SmartWearableCache smartWearableCache, IDecentralandUrlsSource urlsSource) : base(world, cache)
{
this.webRequestController = webRequestController;
this.sceneFactory = sceneFactory;
this.smartWearableCache = smartWearableCache;
this.urlsSource = urlsSource;
}
protected override async UniTask<StreamableLoadingResult<GetSmartWearableSceneIntention.Result>> FlowInternalAsync(GetSmartWearableSceneIntention intention, StreamableLoadingState state, IPartitionComponent partition, CancellationToken ct)
{
IWearable wearable = intention.SmartWearable;
(ISceneContent? sceneContent, SceneMetadata? sceneMetadata) = await smartWearableCache.GetCachedSceneInfoAsync(wearable, ct);
if (ct.IsCancellationRequested) return new StreamableLoadingResult<GetSmartWearableSceneIntention.Result>();
if (sceneContent == null)
{
return new StreamableLoadingResult<GetSmartWearableSceneIntention.Result>(
ReportCategory.WEARABLE,
new Exception($"sceneContent of {wearable.GetName()} is null"));
}
if (sceneMetadata == null)
{
return new StreamableLoadingResult<GetSmartWearableSceneIntention.Result>(
ReportCategory.WEARABLE,
new Exception($"sceneMetadata of {wearable.GetName()} is null"));
}
AssetBundleManifestVersion manifestVersion = wearable.DTO.assetBundleManifestVersion!;
SceneEntityDefinition sceneDefinition = new SceneEntityDefinition(wearable.DTO.id!, sceneMetadata, manifestVersion);
ReadOnlyMemory<byte> crdt = await GetCrdtAsync(sceneContent, ct);
if (ct.IsCancellationRequested) return new StreamableLoadingResult<GetSmartWearableSceneIntention.Result>();
var definitionComponent = SceneDefinitionComponentFactory.CreateFromDefinition(sceneDefinition, new IpfsPath(sceneDefinition.id!, URLDomain.FromString(urlsSource.Url(DecentralandUrl.Content))), true);
var sceneData = new SceneData(
sceneContent,
sceneDefinition,
sceneDefinition.metadata.scene.DecodedBase,
definitionComponent.SceneGeometry,
definitionComponent.Parcels,
new StaticSceneMessages(crdt));
// Set the flag is we are using the special DTO for builder collection preview
sceneData.IsWearableBuilderCollectionPreview = wearable.DTO is BuilderWearableDTO;
var requiredPermissions = sceneDefinition.metadata.requiredPermissions;
var scenePermissions = new RestrictedJsApiPermissionsProvider(requiredPermissions);
ISceneFacade? sceneFacade = await sceneFactory.CreateSceneFromSceneDefinition(sceneData, scenePermissions, partition, ct);
if (ct.IsCancellationRequested) return new StreamableLoadingResult<GetSmartWearableSceneIntention.Result>();
await UniTask.SwitchToMainThread();
sceneFacade?.Initialize();
ReportHub.Log(GetReportCategory(), $"Smart Wearable scene {SmartWearableCache.GetCacheId(wearable)} loaded");
var result = new GetSmartWearableSceneIntention.Result
{
SceneDefinition = definitionComponent,
SceneFacade = sceneFacade
};
return new StreamableLoadingResult<GetSmartWearableSceneIntention.Result>(result);
}
private async UniTask<ReadOnlyMemory<byte>> GetCrdtAsync(ISceneContent sceneContent, CancellationToken ct)
{
if (!sceneContent.TryGetContentUrl("main.crdt", out var url))
{
// We do not report any error since the 'main.crdt' file is not mandatory
return ReadOnlyMemory<byte>.Empty;
}
return await webRequestController.GetAsync(new CommonArguments(url), ct, GetReportData()).GetDataCopyAsync();
}
}
}