@dcl/ecs 7.20.2-22104870534.commit-0df3cc0 → 7.20.2-22169778016.commit-030cbfe
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/generated/component-names.gen.js +0 -2
- package/dist/components/generated/global.gen.d.ts +0 -4
- package/dist/components/generated/global.gen.js +0 -2
- package/dist/components/generated/index.gen.d.ts +0 -8
- package/dist/components/generated/index.gen.js +0 -10
- package/dist/components/generated/pb/decentraland/sdk/components/common/input_action.gen.d.ts +1 -2
- package/dist/components/generated/pb/decentraland/sdk/components/common/input_action.gen.js +0 -1
- package/dist/components/generated/pb/decentraland/sdk/components/virtual_camera.gen.d.ts +0 -3
- package/dist/components/generated/pb/decentraland/sdk/components/virtual_camera.gen.js +1 -10
- package/dist/components/index.d.ts +5 -4
- package/dist/components/index.js +5 -5
- package/dist/components/manual/CreatedBy.d.ts +9 -0
- package/dist/components/manual/CreatedBy.js +8 -0
- package/dist/components/manual/Transform.d.ts +9 -0
- package/dist/components/manual/Transform.js +3 -3
- package/dist/components/types.d.ts +1 -1
- package/dist/engine/component.d.ts +52 -1
- package/dist/engine/grow-only-value-set-component-definition.js +45 -2
- package/dist/engine/lww-element-set-component-definition.d.ts +3 -1
- package/dist/engine/lww-element-set-component-definition.js +65 -12
- package/dist/index.d.ts +2 -2
- package/dist/index.js +1 -1
- package/dist/serialization/crdt/authoritativePutComponent.d.ts +15 -0
- package/dist/serialization/crdt/authoritativePutComponent.js +47 -0
- package/dist/serialization/crdt/index.d.ts +1 -0
- package/dist/serialization/crdt/index.js +1 -0
- package/dist/serialization/crdt/types.d.ts +25 -3
- package/dist/serialization/crdt/types.js +3 -1
- package/dist/systems/crdt/index.d.ts +0 -1
- package/dist/systems/crdt/index.js +55 -146
- package/dist-cjs/components/generated/component-names.gen.js +0 -2
- package/dist-cjs/components/generated/global.gen.d.ts +0 -4
- package/dist-cjs/components/generated/global.gen.js +1 -3
- package/dist-cjs/components/generated/index.gen.d.ts +0 -8
- package/dist-cjs/components/generated/index.gen.js +2 -14
- package/dist-cjs/components/generated/pb/decentraland/sdk/components/common/input_action.gen.d.ts +1 -2
- package/dist-cjs/components/generated/pb/decentraland/sdk/components/common/input_action.gen.js +0 -1
- package/dist-cjs/components/generated/pb/decentraland/sdk/components/virtual_camera.gen.d.ts +0 -3
- package/dist-cjs/components/generated/pb/decentraland/sdk/components/virtual_camera.gen.js +1 -10
- package/dist-cjs/components/index.d.ts +5 -4
- package/dist-cjs/components/index.js +7 -7
- package/dist-cjs/components/manual/CreatedBy.d.ts +9 -0
- package/dist-cjs/components/manual/CreatedBy.js +10 -0
- package/dist-cjs/components/manual/Transform.d.ts +9 -0
- package/dist-cjs/components/manual/Transform.js +3 -3
- package/dist-cjs/components/types.d.ts +1 -1
- package/dist-cjs/engine/component.d.ts +52 -1
- package/dist-cjs/engine/grow-only-value-set-component-definition.js +44 -1
- package/dist-cjs/engine/lww-element-set-component-definition.d.ts +3 -1
- package/dist-cjs/engine/lww-element-set-component-definition.js +68 -13
- package/dist-cjs/index.d.ts +2 -2
- package/dist-cjs/index.js +2 -2
- package/dist-cjs/serialization/crdt/authoritativePutComponent.d.ts +15 -0
- package/dist-cjs/serialization/crdt/authoritativePutComponent.js +50 -0
- package/dist-cjs/serialization/crdt/index.d.ts +1 -0
- package/dist-cjs/serialization/crdt/index.js +1 -0
- package/dist-cjs/serialization/crdt/types.d.ts +25 -3
- package/dist-cjs/serialization/crdt/types.js +3 -1
- package/dist-cjs/systems/crdt/index.d.ts +0 -1
- package/dist-cjs/systems/crdt/index.js +55 -169
- package/package.json +2 -2
- package/dist/components/extended/AudioAnalysis.d.ts +0 -55
- package/dist/components/extended/AudioAnalysis.js +0 -66
- package/dist/components/generated/AudioAnalysis.gen.d.ts +0 -1
- package/dist/components/generated/AudioAnalysis.gen.js +0 -25
- package/dist/components/generated/AvatarLocomotionSettings.gen.d.ts +0 -1
- package/dist/components/generated/AvatarLocomotionSettings.gen.js +0 -25
- package/dist/components/generated/pb/decentraland/sdk/components/audio_analysis.gen.d.ts +0 -37
- package/dist/components/generated/pb/decentraland/sdk/components/audio_analysis.gen.js +0 -161
- package/dist/components/generated/pb/decentraland/sdk/components/avatar_locomotion_settings.gen.d.ts +0 -29
- package/dist/components/generated/pb/decentraland/sdk/components/avatar_locomotion_settings.gen.js +0 -93
- package/dist/serialization/crdt/network/utils.d.ts +0 -9
- package/dist/serialization/crdt/network/utils.js +0 -60
- package/dist-cjs/components/extended/AudioAnalysis.d.ts +0 -55
- package/dist-cjs/components/extended/AudioAnalysis.js +0 -70
- package/dist-cjs/components/generated/AudioAnalysis.gen.d.ts +0 -1
- package/dist-cjs/components/generated/AudioAnalysis.gen.js +0 -28
- package/dist-cjs/components/generated/AvatarLocomotionSettings.gen.d.ts +0 -1
- package/dist-cjs/components/generated/AvatarLocomotionSettings.gen.js +0 -28
- package/dist-cjs/components/generated/pb/decentraland/sdk/components/audio_analysis.gen.d.ts +0 -37
- package/dist-cjs/components/generated/pb/decentraland/sdk/components/audio_analysis.gen.js +0 -167
- package/dist-cjs/components/generated/pb/decentraland/sdk/components/avatar_locomotion_settings.gen.d.ts +0 -29
- package/dist-cjs/components/generated/pb/decentraland/sdk/components/avatar_locomotion_settings.gen.js +0 -99
- package/dist-cjs/serialization/crdt/network/utils.d.ts +0 -9
- package/dist-cjs/serialization/crdt/network/utils.js +0 -67
|
@@ -30,16 +30,12 @@ export function createDumpLwwFunctionFromCrdt(componentId, timestamps, schema, d
|
|
|
30
30
|
}
|
|
31
31
|
};
|
|
32
32
|
}
|
|
33
|
-
|
|
33
|
+
const __GLOBAL_ENTITY = '__GLOBAL_ENTITY';
|
|
34
|
+
export function createCrdtRuleValidator(timestamps, schema, data) {
|
|
34
35
|
/**
|
|
35
|
-
*
|
|
36
|
-
*
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
* Returns the recieved data if the lamport number was bigger than ours.
|
|
40
|
-
* If it was an outdated message, then we return void
|
|
41
|
-
* @public
|
|
42
|
-
*/
|
|
36
|
+
* Shared CRDT conflict resolution logic
|
|
37
|
+
* @public
|
|
38
|
+
*/
|
|
43
39
|
function crdtRuleForCurrentState(message) {
|
|
44
40
|
const { entityId, timestamp } = message;
|
|
45
41
|
const currentTimestamp = timestamps.get(entityId);
|
|
@@ -49,7 +45,6 @@ export function createUpdateLwwFromCrdt(componentId, timestamps, schema, data) {
|
|
|
49
45
|
}
|
|
50
46
|
// Outdated Message. Resend our state message through the wire.
|
|
51
47
|
if (currentTimestamp > timestamp) {
|
|
52
|
-
// console.log('2', currentTimestamp, timestamp)
|
|
53
48
|
return ProcessMessageResultType.StateOutdatedTimestamp;
|
|
54
49
|
}
|
|
55
50
|
// Deletes are idempotent
|
|
@@ -66,7 +61,6 @@ export function createUpdateLwwFromCrdt(componentId, timestamps, schema, data) {
|
|
|
66
61
|
currentDataGreater = dataCompare(null, message.data);
|
|
67
62
|
}
|
|
68
63
|
// Same data, same timestamp. Weirdo echo message.
|
|
69
|
-
// console.log('3', currentDataGreater, writeBuffer.toBinary(), (message as any).data || null)
|
|
70
64
|
if (currentDataGreater === 0) {
|
|
71
65
|
return ProcessMessageResultType.NoChanges;
|
|
72
66
|
}
|
|
@@ -75,10 +69,36 @@ export function createUpdateLwwFromCrdt(componentId, timestamps, schema, data) {
|
|
|
75
69
|
return ProcessMessageResultType.StateOutdatedData;
|
|
76
70
|
}
|
|
77
71
|
else {
|
|
78
|
-
//
|
|
72
|
+
// Current data is lower
|
|
79
73
|
return ProcessMessageResultType.StateUpdatedData;
|
|
80
74
|
}
|
|
81
75
|
}
|
|
76
|
+
return crdtRuleForCurrentState;
|
|
77
|
+
}
|
|
78
|
+
export function createForceUpdateLwwFromCrdt(componentId, timestamps, schema, data) {
|
|
79
|
+
/**
|
|
80
|
+
* Force update component state regardless of timestamp - used for server authoritative messages
|
|
81
|
+
*/
|
|
82
|
+
return (msg) => {
|
|
83
|
+
console.log('[ BOEDO ] [ CASLA ] ', msg);
|
|
84
|
+
const buffer = new ReadWriteByteBuffer(msg.data);
|
|
85
|
+
const deserializedValue = schema.deserialize(buffer);
|
|
86
|
+
data.set(msg.entityId, deserializedValue);
|
|
87
|
+
timestamps.set(msg.entityId, msg.timestamp);
|
|
88
|
+
return [null, deserializedValue];
|
|
89
|
+
};
|
|
90
|
+
}
|
|
91
|
+
export function createUpdateLwwFromCrdt(componentId, timestamps, schema, data) {
|
|
92
|
+
/**
|
|
93
|
+
* Process the received message only if the lamport number recieved is higher
|
|
94
|
+
* than the stored one. If its lower, we spread it to the network to correct the peer.
|
|
95
|
+
* If they are equal, the bigger raw data wins.
|
|
96
|
+
|
|
97
|
+
* Returns the recieved data if the lamport number was bigger than ours.
|
|
98
|
+
* If it was an outdated message, then we return void
|
|
99
|
+
* @public
|
|
100
|
+
*/
|
|
101
|
+
const crdtRuleForCurrentState = createCrdtRuleValidator(timestamps, schema, data);
|
|
82
102
|
return (msg) => {
|
|
83
103
|
/* istanbul ignore next */
|
|
84
104
|
if (msg.type !== CrdtMessageType.PUT_COMPONENT &&
|
|
@@ -171,6 +191,7 @@ export function createComponentDefinitionFromSchema(componentName, componentId,
|
|
|
171
191
|
const dirtyIterator = new Set();
|
|
172
192
|
const timestamps = new Map();
|
|
173
193
|
const onChangeCallbacks = new Map();
|
|
194
|
+
const validateCallbacks = new Map();
|
|
174
195
|
return {
|
|
175
196
|
get componentId() {
|
|
176
197
|
return componentId;
|
|
@@ -262,7 +283,39 @@ export function createComponentDefinitionFromSchema(componentName, componentId,
|
|
|
262
283
|
},
|
|
263
284
|
getCrdtUpdates: createGetCrdtMessagesForLww(componentId, timestamps, dirtyIterator, schema, data),
|
|
264
285
|
updateFromCrdt: createUpdateLwwFromCrdt(componentId, timestamps, schema, data),
|
|
286
|
+
__forceUpdateFromCrdt: createForceUpdateLwwFromCrdt(componentId, timestamps, schema, data),
|
|
287
|
+
__dry_run_updateFromCrdt: createCrdtRuleValidator(timestamps, schema, data),
|
|
265
288
|
dumpCrdtStateToBuffer: createDumpLwwFunctionFromCrdt(componentId, timestamps, schema, data),
|
|
289
|
+
validateBeforeChange(entityOrCb, cb) {
|
|
290
|
+
if (arguments.length === 1) {
|
|
291
|
+
// Second overload: just callback (global validation)
|
|
292
|
+
validateCallbacks.set(__GLOBAL_ENTITY, entityOrCb);
|
|
293
|
+
}
|
|
294
|
+
else {
|
|
295
|
+
if (cb) {
|
|
296
|
+
validateCallbacks.set(entityOrCb, cb);
|
|
297
|
+
}
|
|
298
|
+
}
|
|
299
|
+
},
|
|
300
|
+
__run_validateBeforeChange(entity, newValue, senderAddress, createdBy) {
|
|
301
|
+
const cb = entity && validateCallbacks.get(entity);
|
|
302
|
+
const globalCb = validateCallbacks.get(__GLOBAL_ENTITY);
|
|
303
|
+
const currentValue = data.get(entity);
|
|
304
|
+
const value = { entity, currentValue, newValue, senderAddress, createdBy };
|
|
305
|
+
const globalResult = globalCb?.(value) ?? true;
|
|
306
|
+
const entityResult = (globalResult && cb?.(value)) ?? true;
|
|
307
|
+
return globalResult && entityResult;
|
|
308
|
+
},
|
|
309
|
+
getCrdtState(entity) {
|
|
310
|
+
const componentData = data.get(entity);
|
|
311
|
+
const timestamp = timestamps.get(entity);
|
|
312
|
+
if (componentData && timestamp !== undefined) {
|
|
313
|
+
const buffer = new ReadWriteByteBuffer();
|
|
314
|
+
schema.serialize(deepReadonly(componentData), buffer);
|
|
315
|
+
return { data: buffer.toBinary(), timestamp };
|
|
316
|
+
}
|
|
317
|
+
return null;
|
|
318
|
+
},
|
|
266
319
|
onChange(entity, cb) {
|
|
267
320
|
const cbs = onChangeCallbacks.get(entity) ?? [];
|
|
268
321
|
cbs.push(cb);
|
package/dist/index.d.ts
CHANGED
|
@@ -13,13 +13,12 @@ export * from './systems/tween';
|
|
|
13
13
|
export * from './systems/triggerArea';
|
|
14
14
|
export * from './engine/entity';
|
|
15
15
|
export * from './components/types';
|
|
16
|
-
import { MaterialComponentDefinitionExtended, MeshColliderComponentDefinitionExtended, MeshRendererComponentDefinitionExtended, TransformComponentExtended, AnimatorComponentDefinitionExtended, AudioSourceComponentDefinitionExtended,
|
|
16
|
+
import { MaterialComponentDefinitionExtended, MeshColliderComponentDefinitionExtended, MeshRendererComponentDefinitionExtended, TransformComponentExtended, AnimatorComponentDefinitionExtended, AudioSourceComponentDefinitionExtended, AudioStreamComponentDefinitionExtended, ISyncComponents, TweenComponentDefinitionExtended, INetowrkEntity, INetowrkParent, VirtualCameraComponentDefinitionExtended, InputModifierComponentDefinitionExtended, LightSourceComponentDefinitionExtended, TriggerAreaComponentDefinitionExtended, ICreatedBy } from './components/types';
|
|
17
17
|
import { NameComponent } from './components/manual/Name';
|
|
18
18
|
import { TagsComponentDefinitionExtended } from './components/manual/Tags';
|
|
19
19
|
export declare const Transform: TransformComponentExtended;
|
|
20
20
|
export declare const Animator: AnimatorComponentDefinitionExtended;
|
|
21
21
|
export declare const AudioSource: AudioSourceComponentDefinitionExtended;
|
|
22
|
-
export declare const AudioAnalysis: AudioAnalysisComponentDefinitionExtended;
|
|
23
22
|
export declare const AudioStream: AudioStreamComponentDefinitionExtended;
|
|
24
23
|
export declare const Material: MaterialComponentDefinitionExtended;
|
|
25
24
|
export declare const MeshRenderer: MeshRendererComponentDefinitionExtended;
|
|
@@ -47,6 +46,7 @@ export declare const NetworkEntity: INetowrkEntity;
|
|
|
47
46
|
* Tag a entity to be syncronized through comms
|
|
48
47
|
*/
|
|
49
48
|
export declare const NetworkParent: INetowrkParent;
|
|
49
|
+
export declare const CreatedBy: ICreatedBy;
|
|
50
50
|
export * from './components/generated/global.gen';
|
|
51
51
|
export * from './components/generated/types.gen';
|
|
52
52
|
export * from './serialization/crdt';
|
package/dist/index.js
CHANGED
|
@@ -23,7 +23,6 @@ import { engine } from './runtime/initialization';
|
|
|
23
23
|
export const Transform = /* @__PURE__*/ components.Transform(engine);
|
|
24
24
|
export const Animator = /* @__PURE__*/ components.Animator(engine);
|
|
25
25
|
export const AudioSource = /* @__PURE__*/ components.AudioSource(engine);
|
|
26
|
-
export const AudioAnalysis = /* @__PURE__*/ components.AudioAnalysis(engine);
|
|
27
26
|
export const AudioStream = /* @__PURE__*/ components.AudioStream(engine);
|
|
28
27
|
export const Material = /* @__PURE__*/ components.Material(engine);
|
|
29
28
|
export const MeshRenderer = /* @__PURE__*/ components.MeshRenderer(engine);
|
|
@@ -51,6 +50,7 @@ export const NetworkEntity = /* @__PURE__*/ components.NetworkEntity(engine);
|
|
|
51
50
|
* Tag a entity to be syncronized through comms
|
|
52
51
|
*/
|
|
53
52
|
export const NetworkParent = /* @__PURE__*/ components.NetworkParent(engine);
|
|
53
|
+
export const CreatedBy = /* @__PURE__*/ components.CreatedBy(engine);
|
|
54
54
|
// export components for global engine
|
|
55
55
|
export * from './components/generated/global.gen';
|
|
56
56
|
export * from './components/generated/types.gen';
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { Entity } from '../../engine/entity';
|
|
2
|
+
import { ByteBuffer } from '../ByteBuffer';
|
|
3
|
+
import { AuthoritativePutComponentMessage } from './types';
|
|
4
|
+
/**
|
|
5
|
+
* @public
|
|
6
|
+
*/
|
|
7
|
+
export declare namespace AuthoritativePutComponentOperation {
|
|
8
|
+
const MESSAGE_HEADER_LENGTH = 16;
|
|
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, data: Uint8Array, buf: ByteBuffer): void;
|
|
14
|
+
function read(buf: ByteBuffer): AuthoritativePutComponentMessage | null;
|
|
15
|
+
}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import { CrdtMessageProtocol } from './crdtMessageProtocol';
|
|
2
|
+
import { CrdtMessageType, CRDT_MESSAGE_HEADER_LENGTH } from './types';
|
|
3
|
+
/**
|
|
4
|
+
* @public
|
|
5
|
+
*/
|
|
6
|
+
export var AuthoritativePutComponentOperation;
|
|
7
|
+
(function (AuthoritativePutComponentOperation) {
|
|
8
|
+
AuthoritativePutComponentOperation.MESSAGE_HEADER_LENGTH = 16;
|
|
9
|
+
/**
|
|
10
|
+
* Call this function for an optimal writing data passing the ByteBuffer
|
|
11
|
+
* already allocated
|
|
12
|
+
*/
|
|
13
|
+
function write(entity, timestamp, componentId, data, buf) {
|
|
14
|
+
// reserve the beginning
|
|
15
|
+
const startMessageOffset = buf.incrementWriteOffset(CRDT_MESSAGE_HEADER_LENGTH + AuthoritativePutComponentOperation.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.AUTHORITATIVE_PUT_COMPONENT);
|
|
22
|
+
// Write ComponentOperation header
|
|
23
|
+
buf.setUint32(startMessageOffset + 8, entity);
|
|
24
|
+
buf.setUint32(startMessageOffset + 12, componentId);
|
|
25
|
+
buf.setUint32(startMessageOffset + 16, timestamp);
|
|
26
|
+
const newLocal = messageLength - AuthoritativePutComponentOperation.MESSAGE_HEADER_LENGTH - CRDT_MESSAGE_HEADER_LENGTH;
|
|
27
|
+
buf.setUint32(startMessageOffset + 20, newLocal);
|
|
28
|
+
}
|
|
29
|
+
AuthoritativePutComponentOperation.write = write;
|
|
30
|
+
function read(buf) {
|
|
31
|
+
const header = CrdtMessageProtocol.readHeader(buf);
|
|
32
|
+
if (!header) {
|
|
33
|
+
return null;
|
|
34
|
+
}
|
|
35
|
+
if (header.type !== CrdtMessageType.AUTHORITATIVE_PUT_COMPONENT) {
|
|
36
|
+
throw new Error('AuthoritativePutComponentOperation tried to read another message type.');
|
|
37
|
+
}
|
|
38
|
+
return {
|
|
39
|
+
...header,
|
|
40
|
+
entityId: buf.readUint32(),
|
|
41
|
+
componentId: buf.readUint32(),
|
|
42
|
+
timestamp: buf.readUint32(),
|
|
43
|
+
data: buf.readBuffer()
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
AuthoritativePutComponentOperation.read = read;
|
|
47
|
+
})(AuthoritativePutComponentOperation || (AuthoritativePutComponentOperation = {}));
|
|
@@ -11,7 +11,8 @@ export declare enum CrdtMessageType {
|
|
|
11
11
|
PUT_COMPONENT_NETWORK = 5,
|
|
12
12
|
DELETE_COMPONENT_NETWORK = 6,
|
|
13
13
|
DELETE_ENTITY_NETWORK = 7,
|
|
14
|
-
|
|
14
|
+
AUTHORITATIVE_PUT_COMPONENT = 8,
|
|
15
|
+
MAX_MESSAGE_TYPE = 9
|
|
15
16
|
}
|
|
16
17
|
/**
|
|
17
18
|
* Min length = 8 bytes
|
|
@@ -48,6 +49,23 @@ export type PutNetworkComponentMessageBody = Omit<PutComponentMessageBody, 'type
|
|
|
48
49
|
type: CrdtMessageType.PUT_COMPONENT_NETWORK;
|
|
49
50
|
networkId: number;
|
|
50
51
|
};
|
|
52
|
+
/**
|
|
53
|
+
* Server authoritative message - identical to PutComponentMessageBody but with forced processing
|
|
54
|
+
* Min. length = header (8 bytes) + 16 bytes = 24 bytes
|
|
55
|
+
*
|
|
56
|
+
* @param entity - Uint32 number of the entity
|
|
57
|
+
* @param componentId - Uint32 number of id
|
|
58
|
+
* @param timestamp - Uint32 Lamport timestamp (server's authoritative timestamp)
|
|
59
|
+
* @param data - Uint8[] data of component => length(4 bytes) + block of bytes[0..length-1]
|
|
60
|
+
* @public
|
|
61
|
+
*/
|
|
62
|
+
export type AuthoritativePutComponentMessageBody = {
|
|
63
|
+
type: CrdtMessageType.AUTHORITATIVE_PUT_COMPONENT;
|
|
64
|
+
entityId: Entity;
|
|
65
|
+
componentId: number;
|
|
66
|
+
timestamp: number;
|
|
67
|
+
data: Uint8Array;
|
|
68
|
+
};
|
|
51
69
|
/**
|
|
52
70
|
* Min. length = header (8 bytes) + 16 bytes = 24 bytes
|
|
53
71
|
*
|
|
@@ -114,6 +132,10 @@ export type AppendValueMessage = CrdtMessageHeader & AppendValueMessageBody;
|
|
|
114
132
|
* @public
|
|
115
133
|
*/
|
|
116
134
|
export type PutComponentMessage = CrdtMessageHeader & PutComponentMessageBody;
|
|
135
|
+
/**
|
|
136
|
+
* @public
|
|
137
|
+
*/
|
|
138
|
+
export type AuthoritativePutComponentMessage = CrdtMessageHeader & AuthoritativePutComponentMessageBody;
|
|
117
139
|
/**
|
|
118
140
|
* @public
|
|
119
141
|
*/
|
|
@@ -137,7 +159,7 @@ export type DeleteEntityNetworkMessage = CrdtMessageHeader & DeleteEntityNetwork
|
|
|
137
159
|
/**
|
|
138
160
|
* @public
|
|
139
161
|
*/
|
|
140
|
-
export type CrdtMessage = PutComponentMessage | DeleteComponentMessage | AppendValueMessage | DeleteEntityMessage | PutNetworkComponentMessage | DeleteComponentNetworkMessage | DeleteEntityNetworkMessage;
|
|
162
|
+
export type CrdtMessage = PutComponentMessage | AuthoritativePutComponentMessage | DeleteComponentMessage | AppendValueMessage | DeleteEntityMessage | PutNetworkComponentMessage | DeleteComponentNetworkMessage | DeleteEntityNetworkMessage;
|
|
141
163
|
/**
|
|
142
164
|
* @public
|
|
143
165
|
*/
|
|
@@ -145,7 +167,7 @@ export type CrdtNetworkMessageBody = PutNetworkComponentMessageBody | DeleteComp
|
|
|
145
167
|
/**
|
|
146
168
|
* @public
|
|
147
169
|
*/
|
|
148
|
-
export type CrdtMessageBody = PutComponentMessageBody | DeleteComponentMessageBody | DeleteEntityMessageBody | AppendValueMessageBody | CrdtNetworkMessageBody;
|
|
170
|
+
export type CrdtMessageBody = PutComponentMessageBody | AuthoritativePutComponentMessageBody | DeleteComponentMessageBody | DeleteEntityMessageBody | AppendValueMessageBody | CrdtNetworkMessageBody;
|
|
149
171
|
export declare enum ProcessMessageResultType {
|
|
150
172
|
/**
|
|
151
173
|
* Typical message and new state set.
|
|
@@ -13,7 +13,9 @@ export var CrdtMessageType;
|
|
|
13
13
|
CrdtMessageType[CrdtMessageType["PUT_COMPONENT_NETWORK"] = 5] = "PUT_COMPONENT_NETWORK";
|
|
14
14
|
CrdtMessageType[CrdtMessageType["DELETE_COMPONENT_NETWORK"] = 6] = "DELETE_COMPONENT_NETWORK";
|
|
15
15
|
CrdtMessageType[CrdtMessageType["DELETE_ENTITY_NETWORK"] = 7] = "DELETE_ENTITY_NETWORK";
|
|
16
|
-
|
|
16
|
+
// Server authoritative operation - forces component state regardless of timestamp
|
|
17
|
+
CrdtMessageType[CrdtMessageType["AUTHORITATIVE_PUT_COMPONENT"] = 8] = "AUTHORITATIVE_PUT_COMPONENT";
|
|
18
|
+
CrdtMessageType[CrdtMessageType["MAX_MESSAGE_TYPE"] = 9] = "MAX_MESSAGE_TYPE";
|
|
17
19
|
})(CrdtMessageType || (CrdtMessageType = {}));
|
|
18
20
|
/**
|
|
19
21
|
* @public
|
|
@@ -1,24 +1,17 @@
|
|
|
1
1
|
import { EntityState } from '../../engine/entity';
|
|
2
2
|
import { ReadWriteByteBuffer } from '../../serialization/ByteBuffer';
|
|
3
|
-
import { AppendValueOperation, CrdtMessageProtocol
|
|
3
|
+
import { AppendValueOperation, CrdtMessageProtocol } from '../../serialization/crdt';
|
|
4
4
|
import { DeleteComponent } from '../../serialization/crdt/deleteComponent';
|
|
5
5
|
import { DeleteEntity } from '../../serialization/crdt/deleteEntity';
|
|
6
6
|
import { PutComponentOperation } from '../../serialization/crdt/putComponent';
|
|
7
|
+
import { AuthoritativePutComponentOperation } from '../../serialization/crdt/authoritativePutComponent';
|
|
7
8
|
import { CrdtMessageType } from '../../serialization/crdt/types';
|
|
8
|
-
import { PutNetworkComponentOperation } from '../../serialization/crdt/network/putComponentNetwork';
|
|
9
|
-
import { NetworkEntity as defineNetworkEntity, NetworkParent as defineNetworkParent, Transform as defineTransform } from '../../components';
|
|
10
|
-
import * as networkUtils from '../../serialization/crdt/network/utils';
|
|
11
|
-
// NetworkMessages can only have a MAX_SIZE of 12kb. So we need to send it in chunks.
|
|
12
|
-
export const LIVEKIT_MAX_SIZE = 12;
|
|
13
9
|
/**
|
|
14
10
|
* @internal
|
|
15
11
|
*/
|
|
16
12
|
export function crdtSceneSystem(engine, onProcessEntityComponentChange) {
|
|
17
13
|
const transports = [];
|
|
18
|
-
//
|
|
19
|
-
const NetworkEntity = defineNetworkEntity(engine);
|
|
20
|
-
const NetworkParent = defineNetworkParent(engine);
|
|
21
|
-
const Transform = defineTransform(engine);
|
|
14
|
+
// No network components needed - pure CRDT processing only
|
|
22
15
|
// Messages that we received at transport.onMessage waiting to be processed
|
|
23
16
|
const receivedMessages = [];
|
|
24
17
|
// Messages already processed by the engine but that we need to broadcast to other transports.
|
|
@@ -43,27 +36,20 @@ export function crdtSceneSystem(engine, onProcessEntityComponentChange) {
|
|
|
43
36
|
if (header.type === CrdtMessageType.DELETE_COMPONENT) {
|
|
44
37
|
message = DeleteComponent.read(buffer);
|
|
45
38
|
}
|
|
46
|
-
else if (header.type === CrdtMessageType.DELETE_COMPONENT_NETWORK) {
|
|
47
|
-
message = DeleteComponentNetwork.read(buffer);
|
|
48
|
-
}
|
|
49
39
|
else if (header.type === CrdtMessageType.PUT_COMPONENT) {
|
|
50
40
|
message = PutComponentOperation.read(buffer);
|
|
51
41
|
}
|
|
52
|
-
else if (header.type === CrdtMessageType.
|
|
53
|
-
message =
|
|
42
|
+
else if (header.type === CrdtMessageType.AUTHORITATIVE_PUT_COMPONENT) {
|
|
43
|
+
message = AuthoritativePutComponentOperation.read(buffer);
|
|
54
44
|
}
|
|
55
45
|
else if (header.type === CrdtMessageType.DELETE_ENTITY) {
|
|
56
46
|
message = DeleteEntity.read(buffer);
|
|
57
47
|
}
|
|
58
|
-
else if (header.type === CrdtMessageType.DELETE_ENTITY_NETWORK) {
|
|
59
|
-
message = DeleteEntityNetwork.read(buffer);
|
|
60
|
-
}
|
|
61
48
|
else if (header.type === CrdtMessageType.APPEND_VALUE) {
|
|
62
49
|
message = AppendValueOperation.read(buffer);
|
|
63
|
-
// Unknown message, we skip it
|
|
64
50
|
}
|
|
65
51
|
else {
|
|
66
|
-
//
|
|
52
|
+
// Unknown message, we skip it (including NETWORK messages)
|
|
67
53
|
buffer.incrementReadOffset(header.length);
|
|
68
54
|
}
|
|
69
55
|
if (message) {
|
|
@@ -84,22 +70,6 @@ export function crdtSceneSystem(engine, onProcessEntityComponentChange) {
|
|
|
84
70
|
const messagesToProcess = value.splice(0, value.length);
|
|
85
71
|
return messagesToProcess;
|
|
86
72
|
}
|
|
87
|
-
/**
|
|
88
|
-
* Find the local entityId associated to the network component message.
|
|
89
|
-
* It's a mapping Network -> to Local
|
|
90
|
-
* If it's not a network message, return the entityId received by the message
|
|
91
|
-
*/
|
|
92
|
-
function findNetworkId(msg) {
|
|
93
|
-
const hasNetworkId = 'networkId' in msg;
|
|
94
|
-
if (hasNetworkId) {
|
|
95
|
-
for (const [entityId, network] of engine.getEntitiesWith(NetworkEntity)) {
|
|
96
|
-
if (network.networkId === msg.networkId && network.entityId === msg.entityId) {
|
|
97
|
-
return { entityId, network };
|
|
98
|
-
}
|
|
99
|
-
}
|
|
100
|
-
}
|
|
101
|
-
return { entityId: msg.entityId };
|
|
102
|
-
}
|
|
103
73
|
/**
|
|
104
74
|
* This fn will be called on every tick.
|
|
105
75
|
* Process all the messages queue received by the transport
|
|
@@ -108,46 +78,52 @@ export function crdtSceneSystem(engine, onProcessEntityComponentChange) {
|
|
|
108
78
|
const messagesToProcess = getMessages(receivedMessages);
|
|
109
79
|
const entitiesShouldBeCleaned = [];
|
|
110
80
|
for (const msg of messagesToProcess) {
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
entityId = engine.addEntity();
|
|
115
|
-
network = { entityId: msg.entityId, networkId: msg.networkId };
|
|
116
|
-
NetworkEntity.createOrReplace(entityId, network);
|
|
117
|
-
}
|
|
118
|
-
if (msg.type === CrdtMessageType.DELETE_ENTITY || msg.type === CrdtMessageType.DELETE_ENTITY_NETWORK) {
|
|
119
|
-
entitiesShouldBeCleaned.push(entityId);
|
|
81
|
+
// Simple CRDT processing - no network logic
|
|
82
|
+
if (msg.type === CrdtMessageType.DELETE_ENTITY) {
|
|
83
|
+
entitiesShouldBeCleaned.push(msg.entityId);
|
|
120
84
|
broadcastMessages.push(msg);
|
|
121
85
|
}
|
|
122
86
|
else {
|
|
123
|
-
const entityState = engine.entityContainer.getEntityState(entityId);
|
|
124
|
-
// Skip updates from removed
|
|
87
|
+
const entityState = engine.entityContainer.getEntityState(msg.entityId);
|
|
88
|
+
// Skip updates from removed entities
|
|
125
89
|
if (entityState === EntityState.Removed)
|
|
126
90
|
continue;
|
|
127
|
-
// Entities with unknown
|
|
91
|
+
// Entities with unknown state should update its entity state
|
|
128
92
|
if (entityState === EntityState.Unknown) {
|
|
129
|
-
engine.entityContainer.updateUsedEntity(entityId);
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
if (
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
93
|
+
engine.entityContainer.updateUsedEntity(msg.entityId);
|
|
94
|
+
}
|
|
95
|
+
// Only process component-related messages (not DELETE_ENTITY)
|
|
96
|
+
if ('componentId' in msg) {
|
|
97
|
+
const component = engine.getComponentOrNull(msg.componentId);
|
|
98
|
+
if (component) {
|
|
99
|
+
// Handle authoritative messages differently - they force the state regardless of timestamp
|
|
100
|
+
const tryUpdate = () => {
|
|
101
|
+
try {
|
|
102
|
+
return msg.type === CrdtMessageType.AUTHORITATIVE_PUT_COMPONENT
|
|
103
|
+
? component.__forceUpdateFromCrdt(msg)
|
|
104
|
+
: component.updateFromCrdt(msg);
|
|
105
|
+
}
|
|
106
|
+
catch (e) {
|
|
107
|
+
console.error('[receiveMessages] ERROR processing message', msg, e);
|
|
108
|
+
return null;
|
|
109
|
+
}
|
|
110
|
+
};
|
|
111
|
+
const result = tryUpdate();
|
|
112
|
+
if (!result)
|
|
113
|
+
continue;
|
|
114
|
+
const [conflictMessage, value] = result;
|
|
115
|
+
if (!conflictMessage) {
|
|
116
|
+
// Add message to broadcast queue when no conflict
|
|
117
|
+
broadcastMessages.push(msg);
|
|
118
|
+
onProcessEntityComponentChange && onProcessEntityComponentChange(msg.entityId, msg.type, component, value);
|
|
119
|
+
}
|
|
139
120
|
}
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
121
|
+
else {
|
|
122
|
+
// Component not found - still broadcast for editor compatibility
|
|
123
|
+
/* istanbul ignore next */
|
|
143
124
|
broadcastMessages.push(msg);
|
|
144
|
-
onProcessEntityComponentChange && onProcessEntityComponentChange(entityId, msg.type, component, value);
|
|
145
125
|
}
|
|
146
126
|
}
|
|
147
|
-
else {
|
|
148
|
-
// TODO: test this line, it is fundammental to make the editor work
|
|
149
|
-
broadcastMessages.push(msg);
|
|
150
|
-
}
|
|
151
127
|
}
|
|
152
128
|
}
|
|
153
129
|
// the last stage of the syncrhonization is to delete the entities
|
|
@@ -161,16 +137,17 @@ export function crdtSceneSystem(engine, onProcessEntityComponentChange) {
|
|
|
161
137
|
}
|
|
162
138
|
}
|
|
163
139
|
/**
|
|
164
|
-
*
|
|
140
|
+
* Simple CRDT message broadcasting - no network-specific logic
|
|
165
141
|
*/
|
|
166
142
|
async function sendMessages(entitiesDeletedThisTick) {
|
|
167
|
-
//
|
|
143
|
+
// Get messages from broadcast queue and component updates
|
|
168
144
|
const crdtMessages = getMessages(broadcastMessages);
|
|
169
145
|
const buffer = new ReadWriteByteBuffer();
|
|
146
|
+
// Generate CRDT messages from component updates
|
|
170
147
|
for (const component of engine.componentsIter()) {
|
|
171
148
|
for (const message of component.getCrdtUpdates()) {
|
|
172
149
|
const offset = buffer.currentWriteOffset();
|
|
173
|
-
//
|
|
150
|
+
// Only create messages if there's a transport that will handle it
|
|
174
151
|
if (transports.some((t) => t.filter(message))) {
|
|
175
152
|
if (message.type === CrdtMessageType.PUT_COMPONENT) {
|
|
176
153
|
PutComponentOperation.write(message.entityId, message.timestamp, message.componentId, message.data, buffer);
|
|
@@ -194,7 +171,7 @@ export function crdtSceneSystem(engine, onProcessEntityComponentChange) {
|
|
|
194
171
|
}
|
|
195
172
|
}
|
|
196
173
|
}
|
|
197
|
-
//
|
|
174
|
+
// Handle deleted entities
|
|
198
175
|
for (const entityId of entitiesDeletedThisTick) {
|
|
199
176
|
const offset = buffer.currentWriteOffset();
|
|
200
177
|
DeleteEntity.write(entityId, buffer);
|
|
@@ -205,87 +182,19 @@ export function crdtSceneSystem(engine, onProcessEntityComponentChange) {
|
|
|
205
182
|
});
|
|
206
183
|
onProcessEntityComponentChange && onProcessEntityComponentChange(entityId, CrdtMessageType.DELETE_ENTITY);
|
|
207
184
|
}
|
|
208
|
-
//
|
|
209
|
-
const
|
|
210
|
-
|
|
211
|
-
const __NetworkMessagesBuffer = [];
|
|
212
|
-
const transportIndex = Number(index);
|
|
213
|
-
const transport = transports[transportIndex];
|
|
214
|
-
const isRendererTransport = transport.type === 'renderer';
|
|
215
|
-
const isNetworkTransport = transport.type === 'network';
|
|
216
|
-
// Reset Buffer for each Transport
|
|
217
|
-
transportBuffer.resetBuffer();
|
|
218
|
-
const buffer = new ReadWriteByteBuffer();
|
|
219
|
-
// Then we send all the new crdtMessages that the transport needs to process
|
|
185
|
+
// Simple transport broadcasting - no network-specific transforms
|
|
186
|
+
for (const transport of transports) {
|
|
187
|
+
const transportBuffer = new ReadWriteByteBuffer();
|
|
220
188
|
for (const message of crdtMessages) {
|
|
221
|
-
// Check if adding this message would exceed the size limit
|
|
222
|
-
const currentBufferSize = transportBuffer.toBinary().byteLength;
|
|
223
|
-
const messageSize = message.messageBuffer.byteLength;
|
|
224
|
-
if (isNetworkTransport && (currentBufferSize + messageSize) / 1024 > LIVEKIT_MAX_SIZE) {
|
|
225
|
-
// If the current buffer has content, save it as a chunk
|
|
226
|
-
if (currentBufferSize > 0) {
|
|
227
|
-
__NetworkMessagesBuffer.push(transportBuffer.toCopiedBinary());
|
|
228
|
-
transportBuffer.resetBuffer();
|
|
229
|
-
}
|
|
230
|
-
// If the message itself is larger than the limit, we need to handle it specially
|
|
231
|
-
// For now, we'll skip it to prevent infinite loops
|
|
232
|
-
if (messageSize / 1024 > LIVEKIT_MAX_SIZE) {
|
|
233
|
-
console.error(`Message too large (${messageSize} bytes), skipping message for entity ${message.entityId}`);
|
|
234
|
-
continue;
|
|
235
|
-
}
|
|
236
|
-
}
|
|
237
189
|
// Avoid echo messages
|
|
238
|
-
if (message.transportId ===
|
|
239
|
-
continue;
|
|
240
|
-
// Redundant message for the transport
|
|
241
|
-
if (!transport.filter(message))
|
|
242
|
-
continue;
|
|
243
|
-
const { entityId } = findNetworkId(message);
|
|
244
|
-
const transformNeedsFix = 'componentId' in message &&
|
|
245
|
-
message.componentId === Transform.componentId &&
|
|
246
|
-
Transform.has(entityId) &&
|
|
247
|
-
NetworkParent.has(entityId) &&
|
|
248
|
-
NetworkEntity.has(entityId);
|
|
249
|
-
// If there was a LOCAL change in the transform. Add the parent to that transform
|
|
250
|
-
if (isRendererTransport && message.type === CrdtMessageType.PUT_COMPONENT && transformNeedsFix) {
|
|
251
|
-
const parent = findNetworkId(NetworkParent.get(entityId));
|
|
252
|
-
const transformData = networkUtils.fixTransformParent(message, Transform.get(entityId), parent.entityId);
|
|
253
|
-
const offset = buffer.currentWriteOffset();
|
|
254
|
-
PutComponentOperation.write(entityId, message.timestamp, message.componentId, transformData, buffer);
|
|
255
|
-
transportBuffer.writeBuffer(buffer.buffer().subarray(offset, buffer.currentWriteOffset()), false);
|
|
190
|
+
if (message.transportId === transports.indexOf(transport))
|
|
256
191
|
continue;
|
|
192
|
+
// Check if transport wants this message
|
|
193
|
+
if (transport.filter(message)) {
|
|
194
|
+
transportBuffer.writeBuffer(message.messageBuffer, false);
|
|
257
195
|
}
|
|
258
|
-
if (isRendererTransport && networkUtils.isNetworkMessage(message)) {
|
|
259
|
-
// If it's the renderer transport and its a NetworkMessage, we need to fix the entityId field and convert it to a known Message.
|
|
260
|
-
// PUT_NETWORK_COMPONENT -> PUT_COMPONENT
|
|
261
|
-
let transformData = 'data' in message ? message.data : new Uint8Array();
|
|
262
|
-
if (transformNeedsFix) {
|
|
263
|
-
const parent = findNetworkId(NetworkParent.get(entityId));
|
|
264
|
-
transformData = networkUtils.fixTransformParent(message, Transform.get(entityId), parent.entityId);
|
|
265
|
-
}
|
|
266
|
-
networkUtils.networkMessageToLocal({ ...message, data: transformData }, entityId, buffer, transportBuffer);
|
|
267
|
-
// Iterate the next message
|
|
268
|
-
continue;
|
|
269
|
-
}
|
|
270
|
-
// If its a network transport and its a PUT_COMPONENT that has a NetworkEntity component, we need to send this message
|
|
271
|
-
// through comms with the EntityID and NetworkID from ther NetworkEntity so everyone can recieve this message and map to their custom entityID.
|
|
272
|
-
if (isNetworkTransport && !networkUtils.isNetworkMessage(message)) {
|
|
273
|
-
const networkData = NetworkEntity.getOrNull(message.entityId);
|
|
274
|
-
// If it has networkData convert the message to PUT_NETWORK_COMPONENT.
|
|
275
|
-
if (networkData) {
|
|
276
|
-
networkUtils.localMessageToNetwork(message, networkData, buffer, transportBuffer);
|
|
277
|
-
// Iterate the next message
|
|
278
|
-
continue;
|
|
279
|
-
}
|
|
280
|
-
}
|
|
281
|
-
// Common message
|
|
282
|
-
transportBuffer.writeBuffer(message.messageBuffer, false);
|
|
283
|
-
}
|
|
284
|
-
if (isNetworkTransport && transportBuffer.currentWriteOffset()) {
|
|
285
|
-
__NetworkMessagesBuffer.push(transportBuffer.toBinary());
|
|
286
196
|
}
|
|
287
|
-
|
|
288
|
-
await transport.send(message);
|
|
197
|
+
await transport.send(transportBuffer.toBinary());
|
|
289
198
|
}
|
|
290
199
|
}
|
|
291
200
|
/**
|
|
@@ -9,7 +9,6 @@ exports.coreComponentMappings = {
|
|
|
9
9
|
"core::Animator": 1042,
|
|
10
10
|
"core::AssetLoad": 1213,
|
|
11
11
|
"core::AssetLoadLoadingState": 1214,
|
|
12
|
-
"core::AudioAnalysis": 1212,
|
|
13
12
|
"core::AudioEvent": 1105,
|
|
14
13
|
"core::AudioSource": 1020,
|
|
15
14
|
"core::AudioStream": 1021,
|
|
@@ -17,7 +16,6 @@ exports.coreComponentMappings = {
|
|
|
17
16
|
"core::AvatarBase": 1087,
|
|
18
17
|
"core::AvatarEmoteCommand": 1088,
|
|
19
18
|
"core::AvatarEquippedData": 1091,
|
|
20
|
-
"core::AvatarLocomotionSettings": 1211,
|
|
21
19
|
"core::AvatarModifierArea": 1070,
|
|
22
20
|
"core::AvatarShape": 1080,
|
|
23
21
|
"core::Billboard": 1090,
|