| | 1 | | using DCL.Controllers; |
| | 2 | | using DCL.CRDT; |
| | 3 | | using DCL.ECS7.ComponentWrapper; |
| | 4 | | using DCL.ECS7.ComponentWrapper.Generic; |
| | 5 | | using DCL.ECSComponents; |
| | 6 | | using DCL.ECSRuntime; |
| | 7 | | using RPC.Context; |
| | 8 | | using System.Collections.Generic; |
| | 9 | |
|
| | 10 | | namespace DCL.ECS7 |
| | 11 | | { |
| | 12 | | public class ECS7Plugin : IPlugin |
| | 13 | | { |
| | 14 | | private const int MAX_EXPECTED_SCENES = 81; // Scene Load Radius 4 -> max scenes 81 |
| | 15 | |
|
| | 16 | | private readonly IECSComponentWriter componentWriter; |
| | 17 | | private readonly ECS7ComponentsComposer componentsComposer; |
| | 18 | | private readonly ECSSystemsController systemsController; |
| | 19 | | private readonly ECSComponentsFactory componentsFactory; |
| | 20 | | private readonly InternalECSComponents internalEcsComponents; |
| | 21 | | private readonly CrdtExecutorsManager crdtExecutorsManager; |
| | 22 | | private readonly Dictionary<int, IParcelScene> sceneNumberMapping; |
| | 23 | | internal readonly ECSComponentsManager componentsManager; |
| | 24 | | private readonly BaseList<IParcelScene> loadedScenes; |
| | 25 | | private readonly ISceneController sceneController; |
| | 26 | | private readonly SceneStateHandler sceneStateHandler; |
| | 27 | |
|
| | 28 | | private readonly Dictionary<int, ComponentWriter> componentWriters; |
| | 29 | | private readonly Dictionary<int, DualKeyValueSet<long, int, WriteData>> scenesOutgoingMsgs; |
| | 30 | |
|
| 1 | 31 | | public ECS7Plugin() |
| | 32 | | { |
| 1 | 33 | | DataStore.i.ecs7.isEcs7Enabled = true; |
| 1 | 34 | | loadedScenes = DataStore.i.ecs7.scenes; |
| 1 | 35 | | CRDTServiceContext crdtContext = DataStore.i.rpc.context.crdt; |
| 1 | 36 | | RestrictedActionsContext rpcRestrictedActionsContext = DataStore.i.rpc.context.restrictedActions; |
| | 37 | |
|
| 1 | 38 | | sceneController = Environment.i.world.sceneController; |
| 1 | 39 | | Dictionary<int, ICRDTExecutor> crdtExecutors = new Dictionary<int, ICRDTExecutor>(MAX_EXPECTED_SCENES); |
| 1 | 40 | | crdtContext.CrdtExecutors = crdtExecutors; |
| | 41 | |
|
| 1 | 42 | | componentWriters = new Dictionary<int, ComponentWriter>(MAX_EXPECTED_SCENES); |
| 1 | 43 | | scenesOutgoingMsgs = crdtContext.ScenesOutgoingMsgs; |
| | 44 | |
|
| 1 | 45 | | componentsFactory = new ECSComponentsFactory(); |
| | 46 | |
|
| 1 | 47 | | componentsManager = new ECSComponentsManager(componentsFactory.componentBuilders); |
| 1 | 48 | | internalEcsComponents = new InternalECSComponents(componentsManager, componentsFactory, crdtExecutors); |
| | 49 | |
|
| 1 | 50 | | crdtExecutorsManager = new CrdtExecutorsManager(crdtExecutors, componentsManager, sceneController, crdtConte |
| | 51 | |
|
| 1 | 52 | | componentWriter = new ECSComponentWriter(); |
| | 53 | |
|
| 1 | 54 | | componentsComposer = new ECS7ComponentsComposer(componentsFactory, componentWriter, internalEcsComponents); |
| | 55 | |
|
| 1 | 56 | | SystemsContext systemsContext = new SystemsContext( |
| | 57 | | componentWriters, |
| | 58 | | internalEcsComponents, |
| | 59 | | new ComponentGroups(componentsManager), |
| | 60 | | (ECSComponent<PBBillboard>)componentsManager.GetOrCreateComponent(ComponentID.BILLBOARD), |
| | 61 | | (ECSComponent<ECSTransform>)componentsManager.GetOrCreateComponent(ComponentID.TRANSFORM), |
| 0 | 62 | | new WrappedComponentPool<IWrappedComponent<PBCameraMode>>(MAX_EXPECTED_SCENES, () => new ProtobufWrapped |
| 0 | 63 | | new WrappedComponentPool<IWrappedComponent<PBPointerLock>>(MAX_EXPECTED_SCENES, () => new ProtobufWrappe |
| 0 | 64 | | new WrappedComponentPool<IWrappedComponent<ECSTransform>>(MAX_EXPECTED_SCENES * 2, () => new TransformWr |
| 0 | 65 | | new WrappedComponentPool<IWrappedComponent<PBVideoEvent>>(MAX_EXPECTED_SCENES, () => new ProtobufWrapped |
| 0 | 66 | | new WrappedComponentPool<IWrappedComponent<PBRaycastResult>>(MAX_EXPECTED_SCENES, () => new ProtobufWrap |
| 0 | 67 | | new WrappedComponentPool<IWrappedComponent<PBGltfContainerLoadingState>>(MAX_EXPECTED_SCENES * 10, () => |
| 0 | 68 | | new WrappedComponentPool<IWrappedComponent<PBEngineInfo>>(MAX_EXPECTED_SCENES, () => new ProtobufWrapped |
| 0 | 69 | | new WrappedComponentPool<IWrappedComponent<PBUiCanvasInformation>>(MAX_EXPECTED_SCENES, () => new Protob |
| 0 | 70 | | new WrappedComponentPool<IWrappedComponent<PBPointerEventsResult>>(MAX_EXPECTED_SCENES * 10, () => new P |
| 0 | 71 | | new WrappedComponentPool<IWrappedComponent<PBTweenState>>(MAX_EXPECTED_SCENES * 10, () => new ProtobufWr |
| | 72 | | ); |
| | 73 | |
|
| 1 | 74 | | systemsController = new ECSSystemsController(systemsContext); |
| | 75 | |
|
| 1 | 76 | | sceneNumberMapping = new Dictionary<int, IParcelScene>(MAX_EXPECTED_SCENES); // Scene Load Radius 4 -> max s |
| | 77 | |
|
| 1 | 78 | | sceneStateHandler = new SceneStateHandler( |
| | 79 | | crdtContext, |
| | 80 | | rpcRestrictedActionsContext, |
| | 81 | | sceneNumberMapping, |
| | 82 | | internalEcsComponents.EngineInfo, |
| | 83 | | internalEcsComponents.GltfContainerLoadingStateComponent); |
| | 84 | |
|
| 1 | 85 | | sceneController.OnNewSceneAdded += SceneControllerOnNewSceneAdded; |
| 1 | 86 | | sceneController.OnSceneRemoved += SceneControllerOnSceneRemoved; |
| 1 | 87 | | } |
| | 88 | |
|
| | 89 | | public void Dispose() |
| | 90 | | { |
| 1 | 91 | | componentsComposer.Dispose(); |
| 1 | 92 | | componentWriter.Dispose(); |
| 1 | 93 | | systemsController.Dispose(); |
| 1 | 94 | | internalEcsComponents.Dispose(); |
| 1 | 95 | | crdtExecutorsManager.Dispose(); |
| 1 | 96 | | sceneStateHandler.Dispose(); |
| | 97 | |
|
| 1 | 98 | | sceneController.OnNewSceneAdded -= SceneControllerOnNewSceneAdded; |
| 1 | 99 | | sceneController.OnSceneRemoved -= SceneControllerOnSceneRemoved; |
| 1 | 100 | | } |
| | 101 | |
|
| | 102 | | private void SceneControllerOnNewSceneAdded(IParcelScene scene) |
| | 103 | | { |
| 0 | 104 | | if (!scene.sceneData.sdk7) return; |
| | 105 | |
|
| 0 | 106 | | int sceneNumber = scene.sceneData.sceneNumber; |
| 0 | 107 | | sceneNumberMapping.Add(sceneNumber, scene); |
| 0 | 108 | | sceneStateHandler.InitializeEngineInfoComponent(sceneNumber); |
| 0 | 109 | | var outgoingMsgs = new DualKeyValueSet<long, int, WriteData>(10); |
| 0 | 110 | | scenesOutgoingMsgs.Add(sceneNumber, outgoingMsgs); |
| 0 | 111 | | componentWriters.Add(sceneNumber, new ComponentWriter(outgoingMsgs)); |
| | 112 | |
|
| 0 | 113 | | loadedScenes.Add(scene); |
| 0 | 114 | | } |
| | 115 | |
|
| | 116 | | private void SceneControllerOnSceneRemoved(IParcelScene scene) |
| | 117 | | { |
| 0 | 118 | | if (!scene.sceneData.sdk7) return; |
| | 119 | |
|
| 0 | 120 | | loadedScenes.Remove(scene); |
| | 121 | |
|
| 0 | 122 | | int sceneNumber = scene.sceneData.sceneNumber; |
| 0 | 123 | | sceneNumberMapping.Remove(sceneNumber); |
| | 124 | |
|
| 0 | 125 | | if (scenesOutgoingMsgs.TryGetValue(sceneNumber, out var outgoingMsgs)) |
| | 126 | | { |
| 0 | 127 | | var pairs = outgoingMsgs.Pairs; |
| | 128 | |
|
| 0 | 129 | | for (int i = 0; i < pairs.Count; i++) |
| | 130 | | { |
| 0 | 131 | | pairs[i].value.Dispose(); |
| | 132 | | } |
| | 133 | | } |
| | 134 | |
|
| 0 | 135 | | componentWriters.Remove(sceneNumber); |
| 0 | 136 | | } |
| | 137 | | } |
| | 138 | | } |