| | 1 | | using System; |
| | 2 | | using System.Collections.Generic; |
| | 3 | | using DCL; |
| | 4 | | using DCL.Controllers; |
| | 5 | | using DCL.CRDT; |
| | 6 | | using DCL.ECSRuntime; |
| | 7 | | using RPC.Context; |
| | 8 | | using UnityEngine; |
| | 9 | |
|
| | 10 | | public class CrdtExecutorsManager : IDisposable |
| | 11 | | { |
| | 12 | | private readonly Dictionary<string, ICRDTExecutor> crdtExecutors; |
| | 13 | | private readonly ISceneController sceneController; |
| | 14 | | private readonly IWorldState worldState; |
| | 15 | | private readonly ECSComponentsManager componentsManager; |
| | 16 | | private readonly CRDTServiceContext rpcCrdtServiceContext; |
| | 17 | |
|
| | 18 | | private string cachedSceneId; |
| | 19 | | private ICRDTExecutor cachedCrdtExecutor; |
| | 20 | |
|
| 6 | 21 | | public CrdtExecutorsManager(Dictionary<string, ICRDTExecutor> crdtExecutors, |
| | 22 | | ECSComponentsManager componentsManager, ISceneController sceneController, IWorldState worldState, |
| | 23 | | CRDTServiceContext rpcCrdtServiceContext) |
| | 24 | | { |
| 6 | 25 | | this.crdtExecutors = crdtExecutors; |
| 6 | 26 | | this.sceneController = sceneController; |
| 6 | 27 | | this.worldState = worldState; |
| 6 | 28 | | this.componentsManager = componentsManager; |
| 6 | 29 | | this.rpcCrdtServiceContext = rpcCrdtServiceContext; |
| | 30 | |
|
| 6 | 31 | | sceneController.OnSceneRemoved += OnSceneRemoved; |
| 6 | 32 | | rpcCrdtServiceContext.CrdtMessageReceived += CrdtMessageReceived; |
| 6 | 33 | | } |
| | 34 | |
|
| | 35 | | public void Dispose() |
| | 36 | | { |
| 2 | 37 | | sceneController.OnSceneRemoved -= OnSceneRemoved; |
| 2 | 38 | | rpcCrdtServiceContext.CrdtMessageReceived -= CrdtMessageReceived; |
| | 39 | |
|
| 10 | 40 | | foreach (ICRDTExecutor crdtExecutor in crdtExecutors.Values) |
| | 41 | | { |
| 3 | 42 | | crdtExecutor.Dispose(); |
| | 43 | | } |
| 2 | 44 | | crdtExecutors.Clear(); |
| 2 | 45 | | } |
| | 46 | |
|
| | 47 | | private void OnSceneRemoved(IParcelScene scene) |
| | 48 | | { |
| 1 | 49 | | string sceneId = scene.sceneData.id; |
| 1 | 50 | | if (crdtExecutors.TryGetValue(sceneId, out ICRDTExecutor crdtExecutor)) |
| | 51 | | { |
| 1 | 52 | | crdtExecutor.Dispose(); |
| 1 | 53 | | crdtExecutors.Remove(sceneId); |
| | 54 | | } |
| 1 | 55 | | } |
| | 56 | |
|
| | 57 | | private ICRDTExecutor GetCachedExecutor(string sceneId) |
| | 58 | | { |
| 4 | 59 | | if (cachedSceneId != sceneId) |
| | 60 | | { |
| 3 | 61 | | cachedCrdtExecutor = null; |
| 3 | 62 | | cachedSceneId = null; |
| 3 | 63 | | if (crdtExecutors.TryGetValue(sceneId, out cachedCrdtExecutor)) |
| | 64 | | { |
| 0 | 65 | | cachedSceneId = sceneId; |
| 0 | 66 | | } |
| 3 | 67 | | else if (worldState.TryGetScene(sceneId, out IParcelScene scene)) |
| | 68 | | { |
| 2 | 69 | | cachedSceneId = sceneId; |
| 2 | 70 | | if (scene.crdtExecutor == null) |
| | 71 | | { |
| 2 | 72 | | cachedCrdtExecutor = new CRDTExecutor(scene, componentsManager); |
| 2 | 73 | | scene.crdtExecutor = cachedCrdtExecutor; |
| 2 | 74 | | crdtExecutors[sceneId] = cachedCrdtExecutor; |
| 2 | 75 | | } |
| | 76 | | else |
| | 77 | | { |
| 0 | 78 | | cachedCrdtExecutor = scene.crdtExecutor; |
| | 79 | | } |
| | 80 | | } |
| | 81 | | } |
| 4 | 82 | | return cachedCrdtExecutor; |
| | 83 | | } |
| | 84 | |
|
| | 85 | | private void CrdtMessageReceived(string sceneId, CRDTMessage crdtMessage) |
| | 86 | | { |
| 4 | 87 | | ICRDTExecutor executor = GetCachedExecutor(sceneId); |
| | 88 | |
|
| 4 | 89 | | if (executor != null) |
| | 90 | | { |
| 3 | 91 | | executor.Execute(crdtMessage); |
| 3 | 92 | | } |
| | 93 | | else |
| | 94 | | { |
| 1 | 95 | | Debug.LogError($"CrdtExecutor not found for sceneId {sceneId}"); |
| | 96 | | } |
| 1 | 97 | | } |
| | 98 | | } |