@dcl/ecs 7.3.31 → 7.3.32
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/components/index.d.ts +10 -0
- package/dist/components/index.js +12 -0
- package/dist/components/manual/NetworkEntity.d.ts +12 -0
- package/dist/components/manual/NetworkEntity.js +9 -0
- package/dist/components/manual/NetworkParent.d.ts +12 -0
- package/dist/components/manual/NetworkParent.js +9 -0
- package/dist/components/types.d.ts +2 -0
- package/dist/engine/index.js +8 -21
- package/dist/engine/lww-element-set-component-definition.js +5 -2
- package/dist/engine/types.d.ts +0 -12
- package/dist/index.d.ts +11 -1
- package/dist/index.js +10 -0
- package/dist/runtime/helpers/tree.d.ts +1 -1
- package/dist/runtime/helpers/tree.js +25 -2
- package/dist/serialization/crdt/index.d.ts +3 -0
- package/dist/serialization/crdt/index.js +3 -0
- package/dist/serialization/crdt/message.js +12 -0
- package/dist/serialization/crdt/network/deleteComponentNetwork.d.ts +14 -0
- package/dist/serialization/crdt/network/deleteComponentNetwork.js +43 -0
- package/dist/serialization/crdt/network/deleteEntityNetwork.d.ts +11 -0
- package/dist/serialization/crdt/network/deleteEntityNetwork.js +33 -0
- package/dist/serialization/crdt/network/putComponentNetwork.d.ts +15 -0
- package/dist/serialization/crdt/network/putComponentNetwork.js +49 -0
- package/dist/serialization/crdt/network/utils.d.ts +9 -0
- package/dist/serialization/crdt/network/utils.js +62 -0
- package/dist/serialization/crdt/types.d.ts +48 -3
- package/dist/serialization/crdt/types.js +5 -1
- package/dist/systems/crdt/index.js +111 -69
- package/dist/systems/crdt/types.d.ts +9 -1
- package/dist-cjs/components/index.d.ts +10 -0
- package/dist-cjs/components/index.js +15 -1
- package/dist-cjs/components/manual/NetworkEntity.d.ts +12 -0
- package/dist-cjs/components/manual/NetworkEntity.js +11 -0
- package/dist-cjs/components/manual/NetworkParent.d.ts +12 -0
- package/dist-cjs/components/manual/NetworkParent.js +11 -0
- package/dist-cjs/components/types.d.ts +2 -0
- package/dist-cjs/engine/index.js +8 -21
- package/dist-cjs/engine/lww-element-set-component-definition.js +5 -2
- package/dist-cjs/engine/types.d.ts +0 -12
- package/dist-cjs/index.d.ts +11 -1
- package/dist-cjs/index.js +11 -1
- package/dist-cjs/runtime/helpers/tree.d.ts +1 -1
- package/dist-cjs/runtime/helpers/tree.js +25 -2
- package/dist-cjs/serialization/crdt/index.d.ts +3 -0
- package/dist-cjs/serialization/crdt/index.js +3 -0
- package/dist-cjs/serialization/crdt/message.js +12 -0
- package/dist-cjs/serialization/crdt/network/deleteComponentNetwork.d.ts +14 -0
- package/dist-cjs/serialization/crdt/network/deleteComponentNetwork.js +46 -0
- package/dist-cjs/serialization/crdt/network/deleteEntityNetwork.d.ts +11 -0
- package/dist-cjs/serialization/crdt/network/deleteEntityNetwork.js +36 -0
- package/dist-cjs/serialization/crdt/network/putComponentNetwork.d.ts +15 -0
- package/dist-cjs/serialization/crdt/network/putComponentNetwork.js +52 -0
- package/dist-cjs/serialization/crdt/network/utils.d.ts +9 -0
- package/dist-cjs/serialization/crdt/network/utils.js +69 -0
- package/dist-cjs/serialization/crdt/types.d.ts +48 -3
- package/dist-cjs/serialization/crdt/types.js +5 -1
- package/dist-cjs/systems/crdt/index.js +133 -68
- package/dist-cjs/systems/crdt/types.d.ts +9 -1
- package/package.json +2 -2
|
@@ -8,6 +8,8 @@ import { TweenComponentDefinitionExtended } from './extended/Tween';
|
|
|
8
8
|
import { LwwComponentGetter, GSetComponentGetter } from './generated/index.gen';
|
|
9
9
|
import { NameType } from './manual/Name';
|
|
10
10
|
import { ISyncComponentsType } from './manual/SyncComponents';
|
|
11
|
+
import { INetowrkEntityType } from './manual/NetworkEntity';
|
|
12
|
+
import { INetowrkParentType } from './manual/NetworkParent';
|
|
11
13
|
import { TransformComponentExtended } from './manual/Transform';
|
|
12
14
|
export * from './generated/index.gen';
|
|
13
15
|
export type { GrowOnlyValueSetComponentDefinition, LastWriteWinElementSetComponentDefinition, LwwComponentGetter, GSetComponentGetter };
|
|
@@ -25,3 +27,11 @@ export declare const Name: (engine: Pick<IEngine, 'defineComponent'>) => LastWri
|
|
|
25
27
|
* @alpha
|
|
26
28
|
*/
|
|
27
29
|
export declare const SyncComponents: (engine: Pick<IEngine, 'defineComponent'>) => LastWriteWinElementSetComponentDefinition<ISyncComponentsType>;
|
|
30
|
+
/**
|
|
31
|
+
* @alpha
|
|
32
|
+
*/
|
|
33
|
+
export declare const NetworkEntity: (engine: Pick<IEngine, 'defineComponent'>) => LastWriteWinElementSetComponentDefinition<INetowrkEntityType>;
|
|
34
|
+
/**
|
|
35
|
+
* @alpha
|
|
36
|
+
*/
|
|
37
|
+
export declare const NetworkParent: (engine: Pick<IEngine, 'defineComponent'>) => LastWriteWinElementSetComponentDefinition<INetowrkParentType>;
|
package/dist/components/index.js
CHANGED
|
@@ -5,6 +5,8 @@ import { defineMeshRendererComponent } from './extended/MeshRenderer';
|
|
|
5
5
|
import { defineTweenComponent } from './extended/Tween';
|
|
6
6
|
import defineNameComponent from './manual/Name';
|
|
7
7
|
import defineSyncComponent from './manual/SyncComponents';
|
|
8
|
+
import defineNetworkEntity from './manual/NetworkEntity';
|
|
9
|
+
import defineNetworkParent from './manual/NetworkParent';
|
|
8
10
|
import { defineTransformComponent } from './manual/Transform';
|
|
9
11
|
export * from './generated/index.gen';
|
|
10
12
|
/* @__PURE__ */
|
|
@@ -29,3 +31,13 @@ export const Name = (engine) => defineNameComponent(engine);
|
|
|
29
31
|
*/
|
|
30
32
|
/* @__PURE__ */
|
|
31
33
|
export const SyncComponents = (engine) => defineSyncComponent(engine);
|
|
34
|
+
/**
|
|
35
|
+
* @alpha
|
|
36
|
+
*/
|
|
37
|
+
/* @__PURE__ */
|
|
38
|
+
export const NetworkEntity = (engine) => defineNetworkEntity(engine);
|
|
39
|
+
/**
|
|
40
|
+
* @alpha
|
|
41
|
+
*/
|
|
42
|
+
/* @__PURE__ */
|
|
43
|
+
export const NetworkParent = (engine) => defineNetworkParent(engine);
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { Entity } from '../../engine';
|
|
2
|
+
import { IEngine, LastWriteWinElementSetComponentDefinition } from '../../engine/types';
|
|
3
|
+
export interface INetowrkEntityType {
|
|
4
|
+
networkId: number;
|
|
5
|
+
entityId: Entity;
|
|
6
|
+
}
|
|
7
|
+
export type INetowrkEntity = LastWriteWinElementSetComponentDefinition<INetowrkEntityType>;
|
|
8
|
+
declare function defineNetworkEntityComponent(engine: Pick<IEngine, 'defineComponent'>): import("../../engine").MapComponentDefinition<import("../..").MapResult<{
|
|
9
|
+
networkId: import("../../schemas").ISchema<number>;
|
|
10
|
+
entityId: import("../../schemas").ISchema<Entity>;
|
|
11
|
+
}>>;
|
|
12
|
+
export default defineNetworkEntityComponent;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { Schemas } from '../../schemas';
|
|
2
|
+
function defineNetworkEntityComponent(engine) {
|
|
3
|
+
const EntityNetwork = engine.defineComponent('core-schema::Network-Entity', {
|
|
4
|
+
networkId: Schemas.Int64,
|
|
5
|
+
entityId: Schemas.Entity
|
|
6
|
+
});
|
|
7
|
+
return EntityNetwork;
|
|
8
|
+
}
|
|
9
|
+
export default defineNetworkEntityComponent;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { Entity } from '../../engine';
|
|
2
|
+
import { IEngine, LastWriteWinElementSetComponentDefinition } from '../../engine/types';
|
|
3
|
+
export interface INetowrkParentType {
|
|
4
|
+
networkId: number;
|
|
5
|
+
entityId: Entity;
|
|
6
|
+
}
|
|
7
|
+
export type INetowrkParent = LastWriteWinElementSetComponentDefinition<INetowrkParentType>;
|
|
8
|
+
declare function defineNetworkParentComponent(engine: Pick<IEngine, 'defineComponent'>): import("../../engine").MapComponentDefinition<import("../..").MapResult<{
|
|
9
|
+
networkId: import("../../schemas").ISchema<number>;
|
|
10
|
+
entityId: import("../../schemas").ISchema<Entity>;
|
|
11
|
+
}>>;
|
|
12
|
+
export default defineNetworkParentComponent;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { Schemas } from '../../schemas';
|
|
2
|
+
function defineNetworkParentComponent(engine) {
|
|
3
|
+
const EntityNetwork = engine.defineComponent('core-schema::Network-Parent', {
|
|
4
|
+
networkId: Schemas.Int64,
|
|
5
|
+
entityId: Schemas.Entity
|
|
6
|
+
});
|
|
7
|
+
return EntityNetwork;
|
|
8
|
+
}
|
|
9
|
+
export default defineNetworkParentComponent;
|
|
@@ -6,3 +6,5 @@ export type { TweenHelper, TweenComponentDefinitionExtended } from './extended/T
|
|
|
6
6
|
export type { TransformComponentExtended, TransformTypeWithOptionals } from './manual/Transform';
|
|
7
7
|
export type { NameComponent, NameType } from './manual/Name';
|
|
8
8
|
export type { ISyncComponents, ISyncComponentsType } from './manual/SyncComponents';
|
|
9
|
+
export type { INetowrkEntity, INetowrkEntityType } from './manual/NetworkEntity';
|
|
10
|
+
export type { INetowrkParent, INetowrkParentType } from './manual/NetworkParent';
|
package/dist/engine/index.js
CHANGED
|
@@ -15,7 +15,6 @@ function preEngine() {
|
|
|
15
15
|
const entityContainer = EntityContainer();
|
|
16
16
|
const componentsDefinition = new Map();
|
|
17
17
|
const systems = SystemContainer();
|
|
18
|
-
let networkManager;
|
|
19
18
|
let sealed = false;
|
|
20
19
|
function addSystem(fn, priority = SYSTEMS_REGULAR_PRIORITY, name) {
|
|
21
20
|
systems.add(fn, priority, name);
|
|
@@ -23,30 +22,22 @@ function preEngine() {
|
|
|
23
22
|
function removeSystem(selector) {
|
|
24
23
|
return systems.remove(selector);
|
|
25
24
|
}
|
|
26
|
-
function getNetworkManager() {
|
|
27
|
-
if (!networkManager)
|
|
28
|
-
throw new Error('Network manager not initialized. Start CRDT Server');
|
|
29
|
-
return networkManager;
|
|
30
|
-
}
|
|
31
|
-
function addNetworkManager(reservedLocalEntities, range) {
|
|
32
|
-
entityContainer.setNetworkEntitiesRange(reservedLocalEntities, range);
|
|
33
|
-
networkManager = {
|
|
34
|
-
addEntity: () => entityContainer.generateEntity(true)
|
|
35
|
-
};
|
|
36
|
-
return networkManager;
|
|
37
|
-
}
|
|
38
25
|
function addEntity() {
|
|
39
26
|
const entity = entityContainer.generateEntity();
|
|
40
27
|
return entity;
|
|
41
28
|
}
|
|
42
29
|
function removeEntity(entity) {
|
|
43
30
|
for (const [, component] of componentsDefinition) {
|
|
31
|
+
// TODO: hack for the moment. It should be enough to delete the entity, but the renderer is not cleaning the components.
|
|
32
|
+
// So we still need the NetworkEntity to forward this message to the SyncTransport.
|
|
33
|
+
if (component.componentName === 'core-schema::Network-Entity')
|
|
34
|
+
continue;
|
|
44
35
|
component.entityDeleted(entity, true);
|
|
45
36
|
}
|
|
46
37
|
return entityContainer.removeEntity(entity);
|
|
47
38
|
}
|
|
48
39
|
function removeEntityWithChildren(entity) {
|
|
49
|
-
return removeEntityWithChildrenEngine({ removeEntity, defineComponentFromSchema, getEntitiesWith }, entity);
|
|
40
|
+
return removeEntityWithChildrenEngine({ removeEntity, defineComponentFromSchema, getEntitiesWith, defineComponent }, entity);
|
|
50
41
|
}
|
|
51
42
|
function registerComponentDefinition(componentName, component) {
|
|
52
43
|
/* istanbul ignore next */
|
|
@@ -123,7 +114,7 @@ function preEngine() {
|
|
|
123
114
|
const componentId = typeof componentIdOrName === 'number' ? componentIdOrName : componentNumberFromName(componentIdOrName);
|
|
124
115
|
const component = componentsDefinition.get(componentId);
|
|
125
116
|
if (!component) {
|
|
126
|
-
throw new Error(`Component ${
|
|
117
|
+
throw new Error(`Component ${componentIdOrName} not found. You need to declare the components at the beginnig of the engine declaration`);
|
|
127
118
|
}
|
|
128
119
|
return component;
|
|
129
120
|
}
|
|
@@ -197,9 +188,7 @@ function preEngine() {
|
|
|
197
188
|
registerComponentDefinition,
|
|
198
189
|
entityContainer,
|
|
199
190
|
componentsIter,
|
|
200
|
-
seal
|
|
201
|
-
addNetworkManager,
|
|
202
|
-
getNetworkManager
|
|
191
|
+
seal
|
|
203
192
|
};
|
|
204
193
|
}
|
|
205
194
|
/**
|
|
@@ -243,8 +232,6 @@ export function Engine(options) {
|
|
|
243
232
|
CameraEntity: 2,
|
|
244
233
|
getEntityState: partialEngine.entityContainer.getEntityState,
|
|
245
234
|
addTransport: crdtSystem.addTransport,
|
|
246
|
-
entityContainer: partialEngine.entityContainer
|
|
247
|
-
addNetworkManager: partialEngine.addNetworkManager,
|
|
248
|
-
getNetworkManager: partialEngine.getNetworkManager
|
|
235
|
+
entityContainer: partialEngine.entityContainer
|
|
249
236
|
};
|
|
250
237
|
}
|
|
@@ -81,7 +81,10 @@ export function createUpdateLwwFromCrdt(componentId, timestamps, schema, data) {
|
|
|
81
81
|
}
|
|
82
82
|
return (msg) => {
|
|
83
83
|
/* istanbul ignore next */
|
|
84
|
-
if (msg.type !== CrdtMessageType.PUT_COMPONENT &&
|
|
84
|
+
if (msg.type !== CrdtMessageType.PUT_COMPONENT &&
|
|
85
|
+
msg.type !== CrdtMessageType.PUT_COMPONENT_NETWORK &&
|
|
86
|
+
msg.type !== CrdtMessageType.DELETE_COMPONENT &&
|
|
87
|
+
msg.type !== CrdtMessageType.DELETE_COMPONENT_NETWORK)
|
|
85
88
|
/* istanbul ignore next */
|
|
86
89
|
return [null, data.get(msg.entityId)];
|
|
87
90
|
const action = crdtRuleForCurrentState(msg);
|
|
@@ -90,7 +93,7 @@ export function createUpdateLwwFromCrdt(componentId, timestamps, schema, data) {
|
|
|
90
93
|
case ProcessMessageResultType.StateUpdatedData:
|
|
91
94
|
case ProcessMessageResultType.StateUpdatedTimestamp: {
|
|
92
95
|
timestamps.set(entity, msg.timestamp);
|
|
93
|
-
if (msg.type === CrdtMessageType.PUT_COMPONENT) {
|
|
96
|
+
if (msg.type === CrdtMessageType.PUT_COMPONENT || msg.type === CrdtMessageType.PUT_COMPONENT_NETWORK) {
|
|
94
97
|
const buf = new ReadWriteByteBuffer(msg.data);
|
|
95
98
|
data.set(entity, schema.deserialize(buf));
|
|
96
99
|
}
|
package/dist/engine/types.d.ts
CHANGED
|
@@ -227,16 +227,4 @@ export interface IEngine {
|
|
|
227
227
|
* components that will be available to this engine and to run optimizations.
|
|
228
228
|
*/
|
|
229
229
|
seal(): void;
|
|
230
|
-
/**
|
|
231
|
-
* @alpha
|
|
232
|
-
* Initialize network manager
|
|
233
|
-
*/
|
|
234
|
-
addNetworkManager(reservedLocalEntities: number, range: [number, number]): {
|
|
235
|
-
addEntity: IEngine['addEntity'];
|
|
236
|
-
};
|
|
237
|
-
/**
|
|
238
|
-
* @alpha
|
|
239
|
-
* Get netowrk manager to create entities.
|
|
240
|
-
*/
|
|
241
|
-
getNetworkManager(): ReturnType<IEngine['addNetworkManager']>;
|
|
242
230
|
}
|
package/dist/index.d.ts
CHANGED
|
@@ -10,7 +10,7 @@ export * from './systems/videoEvents';
|
|
|
10
10
|
export * from './systems/async-task';
|
|
11
11
|
export * from './engine/entity';
|
|
12
12
|
export * from './components/types';
|
|
13
|
-
import { MaterialComponentDefinitionExtended, MeshColliderComponentDefinitionExtended, MeshRendererComponentDefinitionExtended, TransformComponentExtended, AnimatorComponentDefinitionExtended, ISyncComponents, TweenComponentDefinitionExtended } from './components/types';
|
|
13
|
+
import { MaterialComponentDefinitionExtended, MeshColliderComponentDefinitionExtended, MeshRendererComponentDefinitionExtended, TransformComponentExtended, AnimatorComponentDefinitionExtended, ISyncComponents, TweenComponentDefinitionExtended, INetowrkEntity, INetowrkParent } from './components/types';
|
|
14
14
|
import { NameComponent } from './components/manual/Name';
|
|
15
15
|
export declare const Transform: TransformComponentExtended;
|
|
16
16
|
export declare const Animator: AnimatorComponentDefinitionExtended;
|
|
@@ -25,6 +25,16 @@ export declare const Tween: TweenComponentDefinitionExtended;
|
|
|
25
25
|
* Can be modified in the future since it's still in research
|
|
26
26
|
*/
|
|
27
27
|
export declare const SyncComponents: ISyncComponents;
|
|
28
|
+
/**
|
|
29
|
+
* @alpha
|
|
30
|
+
* Tag a entity to be syncronized through comms
|
|
31
|
+
*/
|
|
32
|
+
export declare const NetworkEntity: INetowrkEntity;
|
|
33
|
+
/**
|
|
34
|
+
* @alpha
|
|
35
|
+
* Tag a entity to be syncronized through comms
|
|
36
|
+
*/
|
|
37
|
+
export declare const NetworkParent: INetowrkParent;
|
|
28
38
|
export * from './components/generated/global.gen';
|
|
29
39
|
export * from './components/generated/types.gen';
|
|
30
40
|
export * from './serialization/crdt';
|
package/dist/index.js
CHANGED
|
@@ -30,6 +30,16 @@ export const Tween = /* @__PURE__*/ components.Tween(engine);
|
|
|
30
30
|
* Can be modified in the future since it's still in research
|
|
31
31
|
*/
|
|
32
32
|
export const SyncComponents = /* @__PURE__*/ components.SyncComponents(engine);
|
|
33
|
+
/**
|
|
34
|
+
* @alpha
|
|
35
|
+
* Tag a entity to be syncronized through comms
|
|
36
|
+
*/
|
|
37
|
+
export const NetworkEntity = /* @__PURE__*/ components.NetworkEntity(engine);
|
|
38
|
+
/**
|
|
39
|
+
* @alpha
|
|
40
|
+
* Tag a entity to be syncronized through comms
|
|
41
|
+
*/
|
|
42
|
+
export const NetworkParent = /* @__PURE__*/ components.NetworkParent(engine);
|
|
33
43
|
// export components for global engine
|
|
34
44
|
export * from './components/generated/global.gen';
|
|
35
45
|
export * from './components/generated/types.gen';
|
|
@@ -29,4 +29,4 @@ export declare function getComponentEntityTree<T>(engine: Pick<IEngine, 'getEnti
|
|
|
29
29
|
* @param firstEntity - the root entity of the tree
|
|
30
30
|
* @public
|
|
31
31
|
*/
|
|
32
|
-
export declare function removeEntityWithChildren(engine: Pick<IEngine, 'getEntitiesWith' | 'defineComponentFromSchema' | 'removeEntity'>, entity: Entity): void;
|
|
32
|
+
export declare function removeEntityWithChildren(engine: Pick<IEngine, 'getEntitiesWith' | 'defineComponentFromSchema' | 'removeEntity' | 'defineComponent'>, entity: Entity): void;
|
|
@@ -35,6 +35,24 @@ export function getComponentEntityTree(engine, entity, component) {
|
|
|
35
35
|
const entities = new Map(engine.getEntitiesWith(component));
|
|
36
36
|
return genEntityTree(entity, entities);
|
|
37
37
|
}
|
|
38
|
+
// I swear by all the gods that this is being tested on test/sdk/network/sync-engines.spec.ts
|
|
39
|
+
/* istanbul ignore next */
|
|
40
|
+
function removeNetworkEntityChildrens(engine, parent) {
|
|
41
|
+
const NetworkParent = components.NetworkParent(engine);
|
|
42
|
+
const NetworkEntity = components.NetworkEntity(engine);
|
|
43
|
+
// Remove parent
|
|
44
|
+
engine.removeEntity(parent);
|
|
45
|
+
// Remove childs
|
|
46
|
+
const network = NetworkEntity.getOrNull(parent);
|
|
47
|
+
if (network) {
|
|
48
|
+
for (const [entity, parent] of engine.getEntitiesWith(NetworkParent)) {
|
|
49
|
+
if (parent.entityId === network.entityId && parent.networkId === network.networkId) {
|
|
50
|
+
removeNetworkEntityChildrens(engine, entity);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
return;
|
|
55
|
+
}
|
|
38
56
|
/**
|
|
39
57
|
* Remove all components of each entity in the tree made with Transform parenting
|
|
40
58
|
* @param engine - the engine running the entities
|
|
@@ -43,7 +61,12 @@ export function getComponentEntityTree(engine, entity, component) {
|
|
|
43
61
|
*/
|
|
44
62
|
export function removeEntityWithChildren(engine, entity) {
|
|
45
63
|
const Transform = components.Transform(engine);
|
|
46
|
-
|
|
47
|
-
|
|
64
|
+
const NetworkEntity = components.NetworkEntity(engine);
|
|
65
|
+
/* istanbul ignore if */
|
|
66
|
+
if (NetworkEntity.has(entity)) {
|
|
67
|
+
return removeNetworkEntityChildrens(engine, entity);
|
|
68
|
+
}
|
|
69
|
+
for (const ent of getComponentEntityTree(engine, entity, Transform)) {
|
|
70
|
+
engine.removeEntity(ent);
|
|
48
71
|
}
|
|
49
72
|
}
|
|
@@ -2,5 +2,8 @@ export * from './deleteComponent';
|
|
|
2
2
|
export * from './appendValue';
|
|
3
3
|
export * from './deleteEntity';
|
|
4
4
|
export * from './putComponent';
|
|
5
|
+
export * from './network/putComponentNetwork';
|
|
6
|
+
export * from './network/deleteComponentNetwork';
|
|
7
|
+
export * from './network/deleteEntityNetwork';
|
|
5
8
|
export * from './types';
|
|
6
9
|
export * from './crdtMessageProtocol';
|
|
@@ -2,5 +2,8 @@ export * from './deleteComponent';
|
|
|
2
2
|
export * from './appendValue';
|
|
3
3
|
export * from './deleteEntity';
|
|
4
4
|
export * from './putComponent';
|
|
5
|
+
export * from './network/putComponentNetwork';
|
|
6
|
+
export * from './network/deleteComponentNetwork';
|
|
7
|
+
export * from './network/deleteEntityNetwork';
|
|
5
8
|
export * from './types';
|
|
6
9
|
export * from './crdtMessageProtocol';
|
|
@@ -4,6 +4,9 @@ import { PutComponentOperation } from './putComponent';
|
|
|
4
4
|
import { DeleteComponent } from './deleteComponent';
|
|
5
5
|
import { DeleteEntity } from './deleteEntity';
|
|
6
6
|
import { AppendValueOperation } from './appendValue';
|
|
7
|
+
import { PutNetworkComponentOperation } from './network/putComponentNetwork';
|
|
8
|
+
import { DeleteComponentNetwork } from './network/deleteComponentNetwork';
|
|
9
|
+
import { DeleteEntityNetwork } from './network/deleteEntityNetwork';
|
|
7
10
|
export function readMessage(buf) {
|
|
8
11
|
const header = CrdtMessageProtocol.getHeader(buf);
|
|
9
12
|
if (!header)
|
|
@@ -11,14 +14,23 @@ export function readMessage(buf) {
|
|
|
11
14
|
if (header.type === CrdtMessageType.PUT_COMPONENT) {
|
|
12
15
|
return PutComponentOperation.read(buf);
|
|
13
16
|
}
|
|
17
|
+
else if (header.type === CrdtMessageType.PUT_COMPONENT_NETWORK) {
|
|
18
|
+
return PutNetworkComponentOperation.read(buf);
|
|
19
|
+
}
|
|
14
20
|
else if (header.type === CrdtMessageType.DELETE_COMPONENT) {
|
|
15
21
|
return DeleteComponent.read(buf);
|
|
16
22
|
}
|
|
23
|
+
else if (header.type === CrdtMessageType.DELETE_COMPONENT_NETWORK) {
|
|
24
|
+
return DeleteComponentNetwork.read(buf);
|
|
25
|
+
}
|
|
17
26
|
else if (header.type === CrdtMessageType.APPEND_VALUE) {
|
|
18
27
|
return AppendValueOperation.read(buf);
|
|
19
28
|
}
|
|
20
29
|
else if (header.type === CrdtMessageType.DELETE_ENTITY) {
|
|
21
30
|
return DeleteEntity.read(buf);
|
|
22
31
|
}
|
|
32
|
+
else if (header.type === CrdtMessageType.DELETE_ENTITY_NETWORK) {
|
|
33
|
+
return DeleteEntityNetwork.read(buf);
|
|
34
|
+
}
|
|
23
35
|
return null;
|
|
24
36
|
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { Entity } from '../../../engine/entity';
|
|
2
|
+
import { ByteBuffer } from '../../ByteBuffer';
|
|
3
|
+
import { DeleteComponentNetworkMessage } from '../types';
|
|
4
|
+
/**
|
|
5
|
+
* @public
|
|
6
|
+
*/
|
|
7
|
+
export declare namespace DeleteComponentNetwork {
|
|
8
|
+
const MESSAGE_HEADER_LENGTH = 16;
|
|
9
|
+
/**
|
|
10
|
+
* Write DeleteComponent message
|
|
11
|
+
*/
|
|
12
|
+
function write(entity: Entity, componentId: number, timestamp: number, networkId: number, buf: ByteBuffer): void;
|
|
13
|
+
function read(buf: ByteBuffer): DeleteComponentNetworkMessage | null;
|
|
14
|
+
}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { CrdtMessageProtocol } from '../crdtMessageProtocol';
|
|
2
|
+
import { CrdtMessageType, CRDT_MESSAGE_HEADER_LENGTH } from '../types';
|
|
3
|
+
/**
|
|
4
|
+
* @public
|
|
5
|
+
*/
|
|
6
|
+
export var DeleteComponentNetwork;
|
|
7
|
+
(function (DeleteComponentNetwork) {
|
|
8
|
+
DeleteComponentNetwork.MESSAGE_HEADER_LENGTH = 16;
|
|
9
|
+
/**
|
|
10
|
+
* Write DeleteComponent message
|
|
11
|
+
*/
|
|
12
|
+
function write(entity, componentId, timestamp, networkId, buf) {
|
|
13
|
+
// reserve the beginning
|
|
14
|
+
const messageLength = CRDT_MESSAGE_HEADER_LENGTH + DeleteComponentNetwork.MESSAGE_HEADER_LENGTH;
|
|
15
|
+
const startMessageOffset = buf.incrementWriteOffset(messageLength);
|
|
16
|
+
// Write CrdtMessage header
|
|
17
|
+
buf.setUint32(startMessageOffset, messageLength);
|
|
18
|
+
buf.setUint32(startMessageOffset + 4, CrdtMessageType.DELETE_COMPONENT_NETWORK);
|
|
19
|
+
// Write ComponentOperation header
|
|
20
|
+
buf.setUint32(startMessageOffset + 8, entity);
|
|
21
|
+
buf.setUint32(startMessageOffset + 12, componentId);
|
|
22
|
+
buf.setUint32(startMessageOffset + 16, timestamp);
|
|
23
|
+
buf.setUint32(startMessageOffset + 20, networkId);
|
|
24
|
+
}
|
|
25
|
+
DeleteComponentNetwork.write = write;
|
|
26
|
+
function read(buf) {
|
|
27
|
+
const header = CrdtMessageProtocol.readHeader(buf);
|
|
28
|
+
if (!header) {
|
|
29
|
+
return null;
|
|
30
|
+
}
|
|
31
|
+
if (header.type !== CrdtMessageType.DELETE_COMPONENT_NETWORK) {
|
|
32
|
+
throw new Error('DeleteComponentOperation tried to read another message type.');
|
|
33
|
+
}
|
|
34
|
+
return {
|
|
35
|
+
...header,
|
|
36
|
+
entityId: buf.readUint32(),
|
|
37
|
+
componentId: buf.readUint32(),
|
|
38
|
+
timestamp: buf.readUint32(),
|
|
39
|
+
networkId: buf.readUint32()
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
DeleteComponentNetwork.read = read;
|
|
43
|
+
})(DeleteComponentNetwork || (DeleteComponentNetwork = {}));
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { Entity } from '../../../engine/entity';
|
|
2
|
+
import { ByteBuffer } from '../../ByteBuffer';
|
|
3
|
+
import { DeleteEntityNetworkMessage } from '../types';
|
|
4
|
+
/**
|
|
5
|
+
* @public
|
|
6
|
+
*/
|
|
7
|
+
export declare namespace DeleteEntityNetwork {
|
|
8
|
+
const MESSAGE_HEADER_LENGTH = 8;
|
|
9
|
+
function write(entity: Entity, networkId: number, buf: ByteBuffer): void;
|
|
10
|
+
function read(buf: ByteBuffer): DeleteEntityNetworkMessage | null;
|
|
11
|
+
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { CrdtMessageProtocol } from '../crdtMessageProtocol';
|
|
2
|
+
import { CrdtMessageType, CRDT_MESSAGE_HEADER_LENGTH } from '../types';
|
|
3
|
+
/**
|
|
4
|
+
* @public
|
|
5
|
+
*/
|
|
6
|
+
export var DeleteEntityNetwork;
|
|
7
|
+
(function (DeleteEntityNetwork) {
|
|
8
|
+
DeleteEntityNetwork.MESSAGE_HEADER_LENGTH = 8;
|
|
9
|
+
function write(entity, networkId, buf) {
|
|
10
|
+
// Write CrdtMessage header
|
|
11
|
+
buf.writeUint32(CRDT_MESSAGE_HEADER_LENGTH + 4);
|
|
12
|
+
buf.writeUint32(CrdtMessageType.DELETE_ENTITY_NETWORK);
|
|
13
|
+
// body
|
|
14
|
+
buf.writeUint32(entity);
|
|
15
|
+
buf.writeUint32(networkId);
|
|
16
|
+
}
|
|
17
|
+
DeleteEntityNetwork.write = write;
|
|
18
|
+
function read(buf) {
|
|
19
|
+
const header = CrdtMessageProtocol.readHeader(buf);
|
|
20
|
+
if (!header) {
|
|
21
|
+
return null;
|
|
22
|
+
}
|
|
23
|
+
if (header.type !== CrdtMessageType.DELETE_ENTITY_NETWORK) {
|
|
24
|
+
throw new Error('DeleteEntityNetwork tried to read another message type.');
|
|
25
|
+
}
|
|
26
|
+
return {
|
|
27
|
+
...header,
|
|
28
|
+
entityId: buf.readUint32(),
|
|
29
|
+
networkId: buf.readUint32()
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
DeleteEntityNetwork.read = read;
|
|
33
|
+
})(DeleteEntityNetwork || (DeleteEntityNetwork = {}));
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { Entity } from '../../../engine/entity';
|
|
2
|
+
import { ByteBuffer } from '../../ByteBuffer';
|
|
3
|
+
import { PutNetworkComponentMessage } from '../types';
|
|
4
|
+
/**
|
|
5
|
+
* @public
|
|
6
|
+
*/
|
|
7
|
+
export declare namespace PutNetworkComponentOperation {
|
|
8
|
+
const MESSAGE_HEADER_LENGTH = 20;
|
|
9
|
+
/**
|
|
10
|
+
* Call this function for an optimal writing data passing the ByteBuffer
|
|
11
|
+
* already allocated
|
|
12
|
+
*/
|
|
13
|
+
function write(entity: Entity, timestamp: number, componentId: number, networkId: number, data: Uint8Array, buf: ByteBuffer): void;
|
|
14
|
+
function read(buf: ByteBuffer): PutNetworkComponentMessage | null;
|
|
15
|
+
}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { CrdtMessageProtocol } from '../crdtMessageProtocol';
|
|
2
|
+
import { CrdtMessageType, CRDT_MESSAGE_HEADER_LENGTH } from '../types';
|
|
3
|
+
/**
|
|
4
|
+
* @public
|
|
5
|
+
*/
|
|
6
|
+
export var PutNetworkComponentOperation;
|
|
7
|
+
(function (PutNetworkComponentOperation) {
|
|
8
|
+
PutNetworkComponentOperation.MESSAGE_HEADER_LENGTH = 20;
|
|
9
|
+
/**
|
|
10
|
+
* Call this function for an optimal writing data passing the ByteBuffer
|
|
11
|
+
* already allocated
|
|
12
|
+
*/
|
|
13
|
+
function write(entity, timestamp, componentId, networkId, data, buf) {
|
|
14
|
+
// reserve the beginning
|
|
15
|
+
const startMessageOffset = buf.incrementWriteOffset(CRDT_MESSAGE_HEADER_LENGTH + PutNetworkComponentOperation.MESSAGE_HEADER_LENGTH);
|
|
16
|
+
// write body
|
|
17
|
+
buf.writeBuffer(data, false);
|
|
18
|
+
const messageLength = buf.currentWriteOffset() - startMessageOffset;
|
|
19
|
+
// Write CrdtMessage header
|
|
20
|
+
buf.setUint32(startMessageOffset, messageLength);
|
|
21
|
+
buf.setUint32(startMessageOffset + 4, CrdtMessageType.PUT_COMPONENT_NETWORK);
|
|
22
|
+
// Write ComponentOperation header
|
|
23
|
+
buf.setUint32(startMessageOffset + 8, entity);
|
|
24
|
+
buf.setUint32(startMessageOffset + 12, componentId);
|
|
25
|
+
buf.setUint32(startMessageOffset + 16, timestamp);
|
|
26
|
+
buf.setUint32(startMessageOffset + 20, networkId);
|
|
27
|
+
const dataLength = messageLength - PutNetworkComponentOperation.MESSAGE_HEADER_LENGTH - CRDT_MESSAGE_HEADER_LENGTH;
|
|
28
|
+
buf.setUint32(startMessageOffset + 24, dataLength);
|
|
29
|
+
}
|
|
30
|
+
PutNetworkComponentOperation.write = write;
|
|
31
|
+
function read(buf) {
|
|
32
|
+
const header = CrdtMessageProtocol.readHeader(buf);
|
|
33
|
+
if (!header) {
|
|
34
|
+
return null;
|
|
35
|
+
}
|
|
36
|
+
if (header.type !== CrdtMessageType.PUT_COMPONENT_NETWORK) {
|
|
37
|
+
throw new Error('PutComponentNetworkOperation tried to read another message type.');
|
|
38
|
+
}
|
|
39
|
+
return {
|
|
40
|
+
...header,
|
|
41
|
+
entityId: buf.readUint32(),
|
|
42
|
+
componentId: buf.readUint32(),
|
|
43
|
+
timestamp: buf.readUint32(),
|
|
44
|
+
networkId: buf.readUint32(),
|
|
45
|
+
data: buf.readBuffer()
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
PutNetworkComponentOperation.read = read;
|
|
49
|
+
})(PutNetworkComponentOperation || (PutNetworkComponentOperation = {}));
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { Entity } from '../../../engine';
|
|
2
|
+
import { ReceiveMessage, TransformType } from '../../../runtime/types';
|
|
3
|
+
import { ReceiveNetworkMessage } from '../../../systems/crdt/types';
|
|
4
|
+
import { ByteBuffer } from '../../ByteBuffer';
|
|
5
|
+
import { INetowrkEntityType } from '../../../components/types';
|
|
6
|
+
export declare function isNetworkMessage(message: ReceiveMessage): message is ReceiveNetworkMessage;
|
|
7
|
+
export declare function networkMessageToLocal(message: ReceiveNetworkMessage, localEntityId: Entity, buffer: ByteBuffer, destinationBuffer: ByteBuffer): void;
|
|
8
|
+
export declare function localMessageToNetwork(message: ReceiveMessage, network: INetowrkEntityType, buffer: ByteBuffer, destinationBuffer: ByteBuffer): void;
|
|
9
|
+
export declare function fixTransformParent(message: ReceiveMessage, transformValue?: TransformType, parent?: Entity): Uint8Array;
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import { ReadWriteByteBuffer } from '../../ByteBuffer';
|
|
2
|
+
import { PutComponentOperation } from '../putComponent';
|
|
3
|
+
import { CrdtMessageType } from '../types';
|
|
4
|
+
import { DeleteComponent } from '../deleteComponent';
|
|
5
|
+
import { DeleteEntity } from '../deleteEntity';
|
|
6
|
+
import { PutNetworkComponentOperation } from './putComponentNetwork';
|
|
7
|
+
import { DeleteComponentNetwork } from './deleteComponentNetwork';
|
|
8
|
+
import { DeleteEntityNetwork } from './deleteEntityNetwork';
|
|
9
|
+
import { TransformSchema } from '../../../components/manual/Transform';
|
|
10
|
+
/* istanbul ignore next */
|
|
11
|
+
export function isNetworkMessage(message) {
|
|
12
|
+
return [
|
|
13
|
+
CrdtMessageType.DELETE_COMPONENT_NETWORK,
|
|
14
|
+
CrdtMessageType.DELETE_ENTITY_NETWORK,
|
|
15
|
+
CrdtMessageType.PUT_COMPONENT_NETWORK
|
|
16
|
+
].includes(message.type);
|
|
17
|
+
}
|
|
18
|
+
/* istanbul ignore next */
|
|
19
|
+
export function networkMessageToLocal(message, localEntityId, buffer, destinationBuffer) {
|
|
20
|
+
const offset = buffer.currentWriteOffset();
|
|
21
|
+
if (message.type === CrdtMessageType.PUT_COMPONENT_NETWORK) {
|
|
22
|
+
PutComponentOperation.write(localEntityId, message.timestamp, message.componentId, message.data, buffer);
|
|
23
|
+
}
|
|
24
|
+
else if (message.type === CrdtMessageType.DELETE_COMPONENT_NETWORK) {
|
|
25
|
+
DeleteComponent.write(localEntityId, message.componentId, message.timestamp, buffer);
|
|
26
|
+
}
|
|
27
|
+
else if (message.type === CrdtMessageType.DELETE_ENTITY_NETWORK) {
|
|
28
|
+
DeleteEntity.write(localEntityId, buffer);
|
|
29
|
+
}
|
|
30
|
+
destinationBuffer.writeBuffer(buffer.buffer().subarray(offset, buffer.currentWriteOffset()), false);
|
|
31
|
+
}
|
|
32
|
+
/* istanbul ignore next */
|
|
33
|
+
export function localMessageToNetwork(message, network, buffer, destinationBuffer) {
|
|
34
|
+
const offset = buffer.currentWriteOffset();
|
|
35
|
+
if (message.type === CrdtMessageType.PUT_COMPONENT) {
|
|
36
|
+
PutNetworkComponentOperation.write(network.entityId, message.timestamp, message.componentId, network.networkId, message.data, buffer);
|
|
37
|
+
}
|
|
38
|
+
else if (message.type === CrdtMessageType.DELETE_COMPONENT) {
|
|
39
|
+
DeleteComponentNetwork.write(network.entityId, message.componentId, message.timestamp, network.networkId, buffer);
|
|
40
|
+
}
|
|
41
|
+
else if (message.type === CrdtMessageType.DELETE_ENTITY) {
|
|
42
|
+
DeleteEntityNetwork.write(network.entityId, network.networkId, buffer);
|
|
43
|
+
}
|
|
44
|
+
destinationBuffer.writeBuffer(buffer.buffer().subarray(offset, buffer.currentWriteOffset()), false);
|
|
45
|
+
}
|
|
46
|
+
/* istanbul ignore next */
|
|
47
|
+
export function fixTransformParent(message, transformValue, parent) {
|
|
48
|
+
let transform = transformValue;
|
|
49
|
+
const buffer = new ReadWriteByteBuffer();
|
|
50
|
+
if (!transform && 'data' in message) {
|
|
51
|
+
buffer.writeBuffer(message.data);
|
|
52
|
+
transform = TransformSchema.deserialize(buffer);
|
|
53
|
+
buffer.resetBuffer();
|
|
54
|
+
}
|
|
55
|
+
if (!transform)
|
|
56
|
+
throw new Error('Invalid parent transform');
|
|
57
|
+
// Generate new transform raw data with the parent
|
|
58
|
+
const newTransform = { ...transform, parent };
|
|
59
|
+
buffer.resetBuffer();
|
|
60
|
+
TransformSchema.serialize(newTransform, buffer);
|
|
61
|
+
return buffer.toBinary();
|
|
62
|
+
}
|