-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathWriteRealmInfoSystem.cs
More file actions
115 lines (91 loc) · 3.9 KB
/
WriteRealmInfoSystem.cs
File metadata and controls
115 lines (91 loc) · 3.9 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
using Arch.Core;
using Arch.SystemGroups;
using CrdtEcsBridge.Components;
using CrdtEcsBridge.ECSToCRDTWriter;
using DCL.ECSComponents;
using DCL.Multiplayer.Connections.GateKeeper.Rooms;
using DCL.Multiplayer.Connections.RoomHubs;
using DCL.Multiplayer.Connections.Rooms.Connective;
using DCL.Utilities;
using ECS;
using ECS.Abstract;
using ECS.Groups;
using SceneRunner.Scene;
namespace DCL.SDKComponents.RealmInfo
{
[UpdateInGroup(typeof(SyncedInitializationSystemGroup))]
public partial class WriteRealmInfoSystem : BaseUnityLoopSystem
{
private readonly IECSToCRDTWriter ecsToCRDTWriter;
private readonly IRealmData realmData;
private readonly CommsRoomInfo commsRoomInfo;
private bool initialized;
internal WriteRealmInfoSystem(World world, IECSToCRDTWriter ecsToCRDTWriter, IRealmData realmData, ObjectProxy<IRoomHub> roomHubProxy, ISceneData sceneData)
: base(world)
{
this.ecsToCRDTWriter = ecsToCRDTWriter;
this.realmData = realmData;
commsRoomInfo = new CommsRoomInfo(roomHubProxy, sceneData);
}
public override void Initialize()
{
if (!realmData.Configured) return;
commsRoomInfo.TryFetchNewInfo();
PropagateToScene();
}
protected override void Update(float t)
{
if (initialized & !commsRoomInfo.TryFetchNewInfo() && !realmData.IsDirty)
return;
if (!realmData.Configured) // Realm is not configured for PX only // TODO review what should be propagated as PX has its own RealmData, here the global one is used
return;
PropagateToScene();
}
private void PropagateToScene()
{
initialized = true;
ecsToCRDTWriter.PutMessage<PBRealmInfo, (IRealmData realmData, CommsRoomInfo commsRoomInfo)>(static (component, data) =>
{
component.BaseUrl = data.realmData.Ipfs.CatalystBaseUrl.Value;
component.RealmName = data.realmData.RealmName;
component.NetworkId = data.realmData.NetworkId;
component.CommsAdapter = data.realmData.CommsAdapter;
component.IsPreview = data.realmData.IsLocalSceneDevelopment;
data.commsRoomInfo.WriteToComponent(component);
}, SpecialEntitiesID.SCENE_ROOT_ENTITY, (realmData, commsRoomInfo));
}
public class CommsRoomInfo
{
private readonly ObjectProxy<IRoomHub> roomHubProxy;
private readonly ISceneData sceneData;
public string IslandSid { get; private set; }
public bool IsConnectedSceneRoom { get; private set; }
public CommsRoomInfo(ObjectProxy<IRoomHub> roomHubProxy, ISceneData sceneData)
{
this.roomHubProxy = roomHubProxy;
this.sceneData = sceneData;
}
/// <summary>
/// Returns true if rooms info has changed
/// </summary>
/// <returns></returns>
public bool TryFetchNewInfo()
{
IRoomHub roomHub = roomHubProxy.StrictObject;
IGateKeeperSceneRoom sceneRoom = roomHub.SceneRoom();
bool isConnectedToSceneRoom = sceneRoom.IsSceneConnected(sceneData.SceneEntityDefinition.id);
string room = roomHub.IslandRoom().Info.Sid ?? string.Empty;
if (IsConnectedSceneRoom == isConnectedToSceneRoom && room == IslandSid)
return false;
IsConnectedSceneRoom = isConnectedToSceneRoom;
IslandSid = room;
return true;
}
public void WriteToComponent(PBRealmInfo component)
{
component.Room = IslandSid;
component.IsConnectedSceneRoom = IsConnectedSceneRoom;
}
}
}
}