@dcl/ecs 7.26.1-31714079767.commit-96e9a29 → 7.26.1-32157237851.commit-e712ef7
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 +0 -5
- package/dist/components/index.js +2 -5
- package/dist/components/manual/Transform.d.ts +0 -9
- package/dist/components/manual/Transform.js +3 -3
- package/dist/components/types.d.ts +0 -1
- package/dist/engine/component.d.ts +1 -52
- package/dist/engine/entity.js +31 -4
- package/dist/engine/grow-only-value-set-component-definition.js +2 -45
- package/dist/engine/index.js +22 -3
- package/dist/engine/lww-element-set-component-definition.d.ts +1 -3
- package/dist/engine/lww-element-set-component-definition.js +13 -68
- package/dist/engine/types.d.ts +8 -1
- package/dist/index.d.ts +1 -2
- package/dist/index.js +0 -1
- package/dist/serialization/crdt/index.d.ts +0 -1
- package/dist/serialization/crdt/index.js +0 -1
- package/dist/serialization/crdt/network/utils.d.ts +9 -0
- package/dist/serialization/crdt/network/utils.js +60 -0
- package/dist/serialization/crdt/types.d.ts +3 -25
- package/dist/serialization/crdt/types.js +1 -3
- package/dist/systems/crdt/index.d.ts +1 -0
- package/dist/systems/crdt/index.js +146 -55
- package/dist-cjs/components/index.d.ts +0 -5
- package/dist-cjs/components/index.js +3 -7
- package/dist-cjs/components/manual/Transform.d.ts +0 -9
- package/dist-cjs/components/manual/Transform.js +3 -3
- package/dist-cjs/components/types.d.ts +0 -1
- package/dist-cjs/engine/component.d.ts +1 -52
- package/dist-cjs/engine/entity.js +31 -4
- package/dist-cjs/engine/grow-only-value-set-component-definition.js +1 -44
- package/dist-cjs/engine/index.js +21 -2
- package/dist-cjs/engine/lww-element-set-component-definition.d.ts +1 -3
- package/dist-cjs/engine/lww-element-set-component-definition.js +14 -71
- package/dist-cjs/engine/types.d.ts +8 -1
- package/dist-cjs/index.d.ts +1 -2
- package/dist-cjs/index.js +1 -2
- package/dist-cjs/serialization/crdt/index.d.ts +0 -1
- package/dist-cjs/serialization/crdt/index.js +0 -1
- package/dist-cjs/serialization/crdt/network/utils.d.ts +9 -0
- package/dist-cjs/serialization/crdt/network/utils.js +67 -0
- package/dist-cjs/serialization/crdt/types.d.ts +3 -25
- package/dist-cjs/serialization/crdt/types.js +1 -3
- package/dist-cjs/systems/crdt/index.d.ts +1 -0
- package/dist-cjs/systems/crdt/index.js +169 -55
- package/package.json +2 -2
- package/dist/components/manual/CreatedBy.d.ts +0 -9
- package/dist/components/manual/CreatedBy.js +0 -8
- package/dist/serialization/crdt/authoritativePutComponent.d.ts +0 -15
- package/dist/serialization/crdt/authoritativePutComponent.js +0 -47
- package/dist-cjs/components/manual/CreatedBy.d.ts +0 -9
- package/dist-cjs/components/manual/CreatedBy.js +0 -10
- package/dist-cjs/serialization/crdt/authoritativePutComponent.d.ts +0 -15
- package/dist-cjs/serialization/crdt/authoritativePutComponent.js +0 -50
|
@@ -59,6 +59,16 @@ var EntityState;
|
|
|
59
59
|
*/
|
|
60
60
|
EntityState[EntityState["Reserved"] = 3] = "Reserved";
|
|
61
61
|
})(EntityState = exports.EntityState || (exports.EntityState = {}));
|
|
62
|
+
/**
|
|
63
|
+
* True when `entity`'s NUMBER falls in the renderer-reserved range, at any version.
|
|
64
|
+
*
|
|
65
|
+
* Masks rather than calling `EntityUtils.fromEntityId`, which allocates a tuple per call to
|
|
66
|
+
* read one number; this runs on every inbound CRDT message. `entity & MAX_U16` already lands
|
|
67
|
+
* in [0, 65535], so the `>>> 0` fromEntityId applies is a no-op here.
|
|
68
|
+
*/
|
|
69
|
+
function isReservedEntity(entity, reservedStaticEntities) {
|
|
70
|
+
return (entity & exports.MAX_U16) < reservedStaticEntities;
|
|
71
|
+
}
|
|
62
72
|
/**
|
|
63
73
|
* @public
|
|
64
74
|
*/
|
|
@@ -93,7 +103,11 @@ function createEntityContainer(opts) {
|
|
|
93
103
|
return generateNewEntity();
|
|
94
104
|
}
|
|
95
105
|
for (const [number, version] of removedEntities.getMap()) {
|
|
96
|
-
|
|
106
|
+
// Never recycle a renderer-reserved number: the renderer reissues those slots as
|
|
107
|
+
// toEntityId(number, version + 1) too, from the same stored version, so it would hand
|
|
108
|
+
// the scene an id belonging to a live remote player. Belt-and-braces — every writer
|
|
109
|
+
// into `removedEntities` refuses reserved numbers, so this cannot fire today.
|
|
110
|
+
if (number >= reservedStaticEntities && version < exports.MAX_U16) {
|
|
97
111
|
const entity = EntityUtils.toEntityId(number, version + 1);
|
|
98
112
|
// If the entity is not being used, we can re-use it
|
|
99
113
|
// If the entity was removed in this tick, we're not counting for the usedEntities, but we have it in the toRemoveEntityArray
|
|
@@ -106,7 +120,7 @@ function createEntityContainer(opts) {
|
|
|
106
120
|
return generateNewEntity();
|
|
107
121
|
}
|
|
108
122
|
function removeEntity(entity) {
|
|
109
|
-
if (entity
|
|
123
|
+
if (isReservedEntity(entity, reservedStaticEntities))
|
|
110
124
|
return false;
|
|
111
125
|
if (usedEntities.has(entity)) {
|
|
112
126
|
usedEntities.delete(entity);
|
|
@@ -129,6 +143,12 @@ function createEntityContainer(opts) {
|
|
|
129
143
|
return arr;
|
|
130
144
|
}
|
|
131
145
|
function updateRemovedEntity(entity) {
|
|
146
|
+
// Called for EVERY inbound DELETE_ENTITY, including the renderer's tombstones for
|
|
147
|
+
// departed remote players — so this is the door reserved numbers would otherwise enter
|
|
148
|
+
// the free list through. They need no tombstone: getEntityState reports them Reserved
|
|
149
|
+
// before it consults `removedEntities`, so `Removed` is unreachable for them anyway.
|
|
150
|
+
if (isReservedEntity(entity, reservedStaticEntities))
|
|
151
|
+
return false;
|
|
132
152
|
const [n, v] = EntityUtils.fromEntityId(entity);
|
|
133
153
|
// Update the removed entities map
|
|
134
154
|
removedEntities.addTo(n, v);
|
|
@@ -139,6 +159,11 @@ function createEntityContainer(opts) {
|
|
|
139
159
|
return true;
|
|
140
160
|
}
|
|
141
161
|
function updateUsedEntity(entity) {
|
|
162
|
+
// Same invariant as updateRemovedEntity. Unreachable from the CRDT path today
|
|
163
|
+
// (getEntityState returns `Reserved`, never `Unknown`), but the `v > 0` branch below
|
|
164
|
+
// would seed `removedEntities` with a reserved number.
|
|
165
|
+
if (isReservedEntity(entity, reservedStaticEntities))
|
|
166
|
+
return false;
|
|
142
167
|
const [n, v] = EntityUtils.fromEntityId(entity);
|
|
143
168
|
// if the entity was removed then abort fast
|
|
144
169
|
if (removedEntities.has(n, v))
|
|
@@ -154,10 +179,12 @@ function createEntityContainer(opts) {
|
|
|
154
179
|
return true;
|
|
155
180
|
}
|
|
156
181
|
function getEntityState(entity) {
|
|
157
|
-
|
|
158
|
-
|
|
182
|
+
// Same guard as the three above, so `Engine.removeEntity` — which classifies via
|
|
183
|
+
// getEntityState — cannot disagree with whether this container releases the id.
|
|
184
|
+
if (isReservedEntity(entity, reservedStaticEntities)) {
|
|
159
185
|
return EntityState.Reserved;
|
|
160
186
|
}
|
|
187
|
+
const [n, v] = EntityUtils.fromEntityId(entity);
|
|
161
188
|
if (usedEntities.has(entity)) {
|
|
162
189
|
return EntityState.UsedEntity;
|
|
163
190
|
}
|
|
@@ -5,7 +5,6 @@ const ByteBuffer_1 = require("../serialization/ByteBuffer");
|
|
|
5
5
|
const crdt_1 = require("../serialization/crdt");
|
|
6
6
|
const invariant_1 = require("../runtime/invariant");
|
|
7
7
|
const emptyReadonlySet = freezeSet(new Set());
|
|
8
|
-
const __GLOBAL_ENTITY = '__GLOBAL_ENTITY';
|
|
9
8
|
function frozenError() {
|
|
10
9
|
throw new Error('The set is frozen');
|
|
11
10
|
}
|
|
@@ -26,7 +25,6 @@ function createValueSetComponentDefinitionFromSchema(componentName, componentId,
|
|
|
26
25
|
const dirtyIterator = new Set();
|
|
27
26
|
const queuedCommands = [];
|
|
28
27
|
const onChangeCallbacks = new Map();
|
|
29
|
-
const validateCallbacks = new Map();
|
|
30
28
|
// only sort the array if the latest (N) element has a timestamp <= N-1
|
|
31
29
|
function shouldSort(row) {
|
|
32
30
|
const len = row.raw.length;
|
|
@@ -85,11 +83,8 @@ function createValueSetComponentDefinitionFromSchema(componentName, componentId,
|
|
|
85
83
|
has(entity) {
|
|
86
84
|
return data.has(entity);
|
|
87
85
|
},
|
|
88
|
-
entityDeleted(entity
|
|
86
|
+
entityDeleted(entity) {
|
|
89
87
|
data.delete(entity);
|
|
90
|
-
if (markAsDirty) {
|
|
91
|
-
// For grow-only sets, we don't need to mark as dirty since deletion doesn't generate CRDT messages
|
|
92
|
-
}
|
|
93
88
|
},
|
|
94
89
|
get(entity) {
|
|
95
90
|
const values = data.get(entity);
|
|
@@ -160,44 +155,6 @@ function createValueSetComponentDefinitionFromSchema(componentName, componentId,
|
|
|
160
155
|
for (const cb of cbs) {
|
|
161
156
|
cb(value);
|
|
162
157
|
}
|
|
163
|
-
},
|
|
164
|
-
__dry_run_updateFromCrdt(_body) {
|
|
165
|
-
return crdt_1.ProcessMessageResultType.StateUpdatedData;
|
|
166
|
-
},
|
|
167
|
-
validateBeforeChange(entityOrCb, cb) {
|
|
168
|
-
if (arguments.length === 1) {
|
|
169
|
-
// Second overload: just callback (global validation)
|
|
170
|
-
validateCallbacks.set(__GLOBAL_ENTITY, entityOrCb);
|
|
171
|
-
}
|
|
172
|
-
else {
|
|
173
|
-
if (cb) {
|
|
174
|
-
validateCallbacks.set(entityOrCb, cb);
|
|
175
|
-
}
|
|
176
|
-
}
|
|
177
|
-
},
|
|
178
|
-
__run_validateBeforeChange(entity, newValue, senderAddress, createdBy) {
|
|
179
|
-
const cb = entity && validateCallbacks.get(entity);
|
|
180
|
-
const globalCb = validateCallbacks.get(__GLOBAL_ENTITY);
|
|
181
|
-
const currentValue = [...this.get(entity).values()];
|
|
182
|
-
const value = { entity, currentValue: currentValue, newValue, senderAddress, createdBy };
|
|
183
|
-
const globalResult = globalCb?.(value) ?? true;
|
|
184
|
-
const entityResult = (globalResult && cb?.(value)) ?? true;
|
|
185
|
-
return globalResult && entityResult;
|
|
186
|
-
},
|
|
187
|
-
getCrdtState(entity) {
|
|
188
|
-
const row = data.get(entity);
|
|
189
|
-
if (!row || row.raw.length === 0) {
|
|
190
|
-
return null;
|
|
191
|
-
}
|
|
192
|
-
// For GrowOnlySet, we need to return the complete CRDT messages for all values
|
|
193
|
-
// This is complex because GrowOnlySet uses APPEND messages, not a single PUT
|
|
194
|
-
// For now, return null to indicate this component type doesn't support simple corrections
|
|
195
|
-
return null;
|
|
196
|
-
},
|
|
197
|
-
__forceUpdateFromCrdt(_msg) {
|
|
198
|
-
// GrowOnlySet doesn't support authoritative corrections in the same way as LWW
|
|
199
|
-
// since it uses APPEND_VALUE messages instead of PUT_COMPONENT messages
|
|
200
|
-
return [null, undefined];
|
|
201
158
|
}
|
|
202
159
|
};
|
|
203
160
|
return ret;
|
package/dist-cjs/engine/index.js
CHANGED
|
@@ -41,6 +41,8 @@ const crdt_2 = require("../serialization/crdt");
|
|
|
41
41
|
__exportStar(require("./input"), exports);
|
|
42
42
|
__exportStar(require("./readonly"), exports);
|
|
43
43
|
__exportStar(require("./types"), exports);
|
|
44
|
+
/** RootEntity 0, PlayerEntity 1, CameraEntity 2 — see the engineInstance fields below. */
|
|
45
|
+
const NAMED_STATIC_ENTITIES = 3;
|
|
44
46
|
function preEngine(options) {
|
|
45
47
|
const entityContainer = options?.entityContainer ?? (0, entity_1.createEntityContainer)();
|
|
46
48
|
const componentsDefinition = new Map();
|
|
@@ -57,6 +59,18 @@ function preEngine(options) {
|
|
|
57
59
|
return entity;
|
|
58
60
|
}
|
|
59
61
|
function removeEntity(entity) {
|
|
62
|
+
// The renderer streams the avatar range and drops the scene's deletes there, so purging
|
|
63
|
+
// locally is permanent: a one-shot component like PlayerIdentityData is never re-sent, and
|
|
64
|
+
// the entity is left as a moving Transform with no identity. The three named static
|
|
65
|
+
// entities are reserved too, but the renderer DOES apply scene deletes on them — that is
|
|
66
|
+
// how InputModifier.deleteFrom(engine.PlayerEntity) clears an input lock — so they must
|
|
67
|
+
// still be purged. Asking the container keeps this in step with whether it releases the
|
|
68
|
+
// id, including for a custom container with a different reserved range.
|
|
69
|
+
const [entityNumber] = entity_1.EntityUtils.fromEntityId(entity);
|
|
70
|
+
const isAvatarEntity = entityNumber >= NAMED_STATIC_ENTITIES && entityContainer.getEntityState(entity) === entity_1.EntityState.Reserved;
|
|
71
|
+
const released = entityContainer.removeEntity(entity);
|
|
72
|
+
if (isAvatarEntity)
|
|
73
|
+
return released;
|
|
60
74
|
for (const [, component] of componentsDefinition) {
|
|
61
75
|
// TODO: hack for the moment.
|
|
62
76
|
// We still need the NetworkEntity to forward this message to the SyncTransport.
|
|
@@ -65,7 +79,7 @@ function preEngine(options) {
|
|
|
65
79
|
continue;
|
|
66
80
|
component.entityDeleted(entity, true);
|
|
67
81
|
}
|
|
68
|
-
return
|
|
82
|
+
return released;
|
|
69
83
|
}
|
|
70
84
|
function removeEntityWithChildren(entity) {
|
|
71
85
|
return (0, tree_1.removeEntityWithChildren)({ removeEntity, defineComponentFromSchema, getEntitiesWith, defineComponent }, entity);
|
|
@@ -289,7 +303,12 @@ function Engine(options) {
|
|
|
289
303
|
RootEntity: 0,
|
|
290
304
|
PlayerEntity: 1,
|
|
291
305
|
CameraEntity: 2,
|
|
292
|
-
|
|
306
|
+
// Delegated, not aliased. A detached `entityContainer.getEntityState` reference binds
|
|
307
|
+
// `this` to the engine, so a custom IEntityContainer that uses `this` silently reports the
|
|
308
|
+
// wrong state through the public API while the engine itself, which calls the container
|
|
309
|
+
// directly, reports the right one. Harmless for the built-in closure-based container, but
|
|
310
|
+
// removeEntity now classifies through getEntityState, so the two must never diverge.
|
|
311
|
+
getEntityState: (entity) => partialEngine.entityContainer.getEntityState(entity),
|
|
293
312
|
addTransport: crdtSystem.addTransport,
|
|
294
313
|
entityContainer: partialEngine.entityContainer
|
|
295
314
|
};
|
|
@@ -1,10 +1,8 @@
|
|
|
1
1
|
import { ISchema } from '../schemas';
|
|
2
2
|
import { ByteBuffer } from '../serialization/ByteBuffer';
|
|
3
|
-
import { PutComponentMessageBody, DeleteComponentMessageBody,
|
|
3
|
+
import { PutComponentMessageBody, DeleteComponentMessageBody, CrdtMessageBody } from '../serialization/crdt';
|
|
4
4
|
import { Entity } from './entity';
|
|
5
5
|
export declare function incrementTimestamp(entity: Entity, timestamps: Map<Entity, number>): number;
|
|
6
6
|
export declare function createDumpLwwFunctionFromCrdt(componentId: number, timestamps: Map<Entity, number>, schema: Pick<ISchema<any>, 'serialize' | 'deserialize'>, data: Map<Entity, unknown>): (buffer: ByteBuffer, filterEntity?: ((entity: Entity) => boolean) | undefined) => void;
|
|
7
|
-
export declare function createCrdtRuleValidator(timestamps: Map<Entity, number>, schema: Pick<ISchema<any>, 'serialize' | 'deserialize'>, data: Map<Entity, unknown>, lastSentData: Map<Entity, Uint8Array>): (message: PutComponentMessageBody | DeleteComponentMessageBody | PutNetworkComponentMessageBody | DeleteComponentNetworkMessageBody) => ProcessMessageResultType;
|
|
8
|
-
export declare function createForceUpdateLwwFromCrdt(componentId: number, timestamps: Map<Entity, number>, schema: Pick<ISchema<any>, 'serialize' | 'deserialize'>, data: Map<Entity, unknown>): (msg: AuthoritativePutComponentMessageBody) => [null, any];
|
|
9
7
|
export declare function createUpdateLwwFromCrdt(componentId: number, timestamps: Map<Entity, number>, schema: Pick<ISchema<any>, 'serialize' | 'deserialize'>, data: Map<Entity, unknown>, lastSentData: Map<Entity, Uint8Array>): (msg: CrdtMessageBody) => [null | PutComponentMessageBody | DeleteComponentMessageBody, any];
|
|
10
8
|
export declare function createGetCrdtMessagesForLww(componentId: number, timestamps: Map<Entity, number>, dirtyIterator: Set<Entity>, schema: Pick<ISchema<any>, 'serialize'>, data: Map<Entity, unknown>, lastSentData: Map<Entity, Uint8Array>): () => Generator<PutComponentMessageBody | DeleteComponentMessageBody, void, unknown>;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.createComponentDefinitionFromSchema = exports.createGetCrdtMessagesForLww = exports.createUpdateLwwFromCrdt = exports.
|
|
3
|
+
exports.createComponentDefinitionFromSchema = exports.createGetCrdtMessagesForLww = exports.createUpdateLwwFromCrdt = exports.createDumpLwwFunctionFromCrdt = exports.incrementTimestamp = void 0;
|
|
4
4
|
const ByteBuffer_1 = require("../serialization/ByteBuffer");
|
|
5
5
|
const crdt_1 = require("../serialization/crdt");
|
|
6
6
|
const utils_1 = require("../systems/crdt/utils");
|
|
@@ -35,12 +35,16 @@ function createDumpLwwFunctionFromCrdt(componentId, timestamps, schema, data) {
|
|
|
35
35
|
};
|
|
36
36
|
}
|
|
37
37
|
exports.createDumpLwwFunctionFromCrdt = createDumpLwwFunctionFromCrdt;
|
|
38
|
-
|
|
39
|
-
function createCrdtRuleValidator(timestamps, schema, data, lastSentData) {
|
|
38
|
+
function createUpdateLwwFromCrdt(componentId, timestamps, schema, data, lastSentData) {
|
|
40
39
|
/**
|
|
41
|
-
*
|
|
42
|
-
*
|
|
43
|
-
|
|
40
|
+
* Process the received message only if the lamport number recieved is higher
|
|
41
|
+
* than the stored one. If its lower, we spread it to the network to correct the peer.
|
|
42
|
+
* If they are equal, the bigger raw data wins.
|
|
43
|
+
|
|
44
|
+
* Returns the recieved data if the lamport number was bigger than ours.
|
|
45
|
+
* If it was an outdated message, then we return void
|
|
46
|
+
* @public
|
|
47
|
+
*/
|
|
44
48
|
function crdtRuleForCurrentState(message) {
|
|
45
49
|
const { entityId, timestamp } = message;
|
|
46
50
|
const currentTimestamp = timestamps.get(entityId);
|
|
@@ -50,6 +54,7 @@ function createCrdtRuleValidator(timestamps, schema, data, lastSentData) {
|
|
|
50
54
|
}
|
|
51
55
|
// Outdated Message. Resend our state message through the wire.
|
|
52
56
|
if (currentTimestamp > timestamp) {
|
|
57
|
+
// console.log('2', currentTimestamp, timestamp)
|
|
53
58
|
return crdt_1.ProcessMessageResultType.StateOutdatedTimestamp;
|
|
54
59
|
}
|
|
55
60
|
// Deletes are idempotent
|
|
@@ -66,6 +71,7 @@ function createCrdtRuleValidator(timestamps, schema, data, lastSentData) {
|
|
|
66
71
|
currentDataGreater = (0, utils_1.dataCompare)(null, message.data);
|
|
67
72
|
}
|
|
68
73
|
// Same data, same timestamp. Weirdo echo message.
|
|
74
|
+
// console.log('3', currentDataGreater, writeBuffer.toBinary(), (message as any).data || null)
|
|
69
75
|
if (currentDataGreater === 0) {
|
|
70
76
|
return crdt_1.ProcessMessageResultType.NoChanges;
|
|
71
77
|
}
|
|
@@ -74,37 +80,10 @@ function createCrdtRuleValidator(timestamps, schema, data, lastSentData) {
|
|
|
74
80
|
return crdt_1.ProcessMessageResultType.StateOutdatedData;
|
|
75
81
|
}
|
|
76
82
|
else {
|
|
77
|
-
//
|
|
83
|
+
// Curent data is lower
|
|
78
84
|
return crdt_1.ProcessMessageResultType.StateUpdatedData;
|
|
79
85
|
}
|
|
80
86
|
}
|
|
81
|
-
return crdtRuleForCurrentState;
|
|
82
|
-
}
|
|
83
|
-
exports.createCrdtRuleValidator = createCrdtRuleValidator;
|
|
84
|
-
function createForceUpdateLwwFromCrdt(componentId, timestamps, schema, data) {
|
|
85
|
-
/**
|
|
86
|
-
* Force update component state regardless of timestamp - used for server authoritative messages
|
|
87
|
-
*/
|
|
88
|
-
return (msg) => {
|
|
89
|
-
const buffer = new ByteBuffer_1.ReadWriteByteBuffer(msg.data);
|
|
90
|
-
const deserializedValue = schema.deserialize(buffer);
|
|
91
|
-
data.set(msg.entityId, deserializedValue);
|
|
92
|
-
timestamps.set(msg.entityId, msg.timestamp);
|
|
93
|
-
return [null, deserializedValue];
|
|
94
|
-
};
|
|
95
|
-
}
|
|
96
|
-
exports.createForceUpdateLwwFromCrdt = createForceUpdateLwwFromCrdt;
|
|
97
|
-
function createUpdateLwwFromCrdt(componentId, timestamps, schema, data, lastSentData) {
|
|
98
|
-
/**
|
|
99
|
-
* Process the received message only if the lamport number recieved is higher
|
|
100
|
-
* than the stored one. If its lower, we spread it to the network to correct the peer.
|
|
101
|
-
* If they are equal, the bigger raw data wins.
|
|
102
|
-
|
|
103
|
-
* Returns the recieved data if the lamport number was bigger than ours.
|
|
104
|
-
* If it was an outdated message, then we return void
|
|
105
|
-
* @public
|
|
106
|
-
*/
|
|
107
|
-
const crdtRuleForCurrentState = createCrdtRuleValidator(timestamps, schema, data, lastSentData);
|
|
108
87
|
return (msg) => {
|
|
109
88
|
/* istanbul ignore next */
|
|
110
89
|
if (msg.type !== crdt_1.CrdtMessageType.PUT_COMPONENT &&
|
|
@@ -122,10 +101,7 @@ function createUpdateLwwFromCrdt(componentId, timestamps, schema, data, lastSent
|
|
|
122
101
|
if (msg.type === crdt_1.CrdtMessageType.PUT_COMPONENT || msg.type === crdt_1.CrdtMessageType.PUT_COMPONENT_NETWORK) {
|
|
123
102
|
const buf = new ByteBuffer_1.ReadWriteByteBuffer(msg.data);
|
|
124
103
|
data.set(entity, schema.deserialize(buf));
|
|
125
|
-
|
|
126
|
-
// seeding it from received bytes makes the engine
|
|
127
|
-
// believe it already sent this state, which kills valid re-broadcasting 🧨
|
|
128
|
-
lastSentData.delete(entity);
|
|
104
|
+
lastSentData.set(entity, new Uint8Array(msg.data));
|
|
129
105
|
}
|
|
130
106
|
else {
|
|
131
107
|
data.delete(entity);
|
|
@@ -216,7 +192,6 @@ function createComponentDefinitionFromSchema(componentName, componentId, schema)
|
|
|
216
192
|
const timestamps = new Map();
|
|
217
193
|
const lastSentData = new Map();
|
|
218
194
|
const onChangeCallbacks = new Map();
|
|
219
|
-
const validateCallbacks = new Map();
|
|
220
195
|
return {
|
|
221
196
|
get componentId() {
|
|
222
197
|
return componentId;
|
|
@@ -311,39 +286,7 @@ function createComponentDefinitionFromSchema(componentName, componentId, schema)
|
|
|
311
286
|
},
|
|
312
287
|
getCrdtUpdates: createGetCrdtMessagesForLww(componentId, timestamps, dirtyIterator, schema, data, lastSentData),
|
|
313
288
|
updateFromCrdt: createUpdateLwwFromCrdt(componentId, timestamps, schema, data, lastSentData),
|
|
314
|
-
__forceUpdateFromCrdt: createForceUpdateLwwFromCrdt(componentId, timestamps, schema, data),
|
|
315
|
-
__dry_run_updateFromCrdt: createCrdtRuleValidator(timestamps, schema, data, lastSentData),
|
|
316
289
|
dumpCrdtStateToBuffer: createDumpLwwFunctionFromCrdt(componentId, timestamps, schema, data),
|
|
317
|
-
validateBeforeChange(entityOrCb, cb) {
|
|
318
|
-
if (arguments.length === 1) {
|
|
319
|
-
// Second overload: just callback (global validation)
|
|
320
|
-
validateCallbacks.set(__GLOBAL_ENTITY, entityOrCb);
|
|
321
|
-
}
|
|
322
|
-
else {
|
|
323
|
-
if (cb) {
|
|
324
|
-
validateCallbacks.set(entityOrCb, cb);
|
|
325
|
-
}
|
|
326
|
-
}
|
|
327
|
-
},
|
|
328
|
-
__run_validateBeforeChange(entity, newValue, senderAddress, createdBy) {
|
|
329
|
-
const cb = entity && validateCallbacks.get(entity);
|
|
330
|
-
const globalCb = validateCallbacks.get(__GLOBAL_ENTITY);
|
|
331
|
-
const currentValue = data.get(entity);
|
|
332
|
-
const value = { entity, currentValue, newValue, senderAddress, createdBy };
|
|
333
|
-
const globalResult = globalCb?.(value) ?? true;
|
|
334
|
-
const entityResult = (globalResult && cb?.(value)) ?? true;
|
|
335
|
-
return globalResult && entityResult;
|
|
336
|
-
},
|
|
337
|
-
getCrdtState(entity) {
|
|
338
|
-
const componentData = data.get(entity);
|
|
339
|
-
const timestamp = timestamps.get(entity);
|
|
340
|
-
if (componentData && timestamp !== undefined) {
|
|
341
|
-
const buffer = new ByteBuffer_1.ReadWriteByteBuffer();
|
|
342
|
-
schema.serialize((0, readonly_1.deepReadonly)(componentData), buffer);
|
|
343
|
-
return { data: buffer.toBinary(), timestamp };
|
|
344
|
-
}
|
|
345
|
-
return null;
|
|
346
|
-
},
|
|
347
290
|
onChange(entity, cb) {
|
|
348
291
|
const cbs = onChangeCallbacks.get(entity) ?? [];
|
|
349
292
|
cbs.push(cb);
|
|
@@ -55,11 +55,18 @@ export interface IEngine {
|
|
|
55
55
|
* @public
|
|
56
56
|
* Remove all components of an entity
|
|
57
57
|
* @param entity - entity
|
|
58
|
+
* @returns whether the entity id was released for reuse. Ids in the renderer-reserved range
|
|
59
|
+
* are never released, at any version. Components are still purged for
|
|
60
|
+
* RootEntity/PlayerEntity/CameraEntity, but not for the avatar range.
|
|
58
61
|
*/
|
|
59
|
-
removeEntity(entity: Entity):
|
|
62
|
+
removeEntity(entity: Entity): boolean;
|
|
60
63
|
/**
|
|
61
64
|
* Remove all components of each entity in the tree made with Transform parenting
|
|
62
65
|
* @param entity - the root entity of the tree
|
|
66
|
+
*
|
|
67
|
+
* May complete only partially and does not report it: nodes in the renderer-reserved range
|
|
68
|
+
* are refused by `removeEntity`, so a reserved node anywhere in the tree survives while its
|
|
69
|
+
* descendants are removed — leaving its `Transform.parent` pointing at a removed entity.
|
|
63
70
|
*/
|
|
64
71
|
removeEntityWithChildren(entity: Entity): void;
|
|
65
72
|
/**
|
package/dist-cjs/index.d.ts
CHANGED
|
@@ -16,7 +16,7 @@ export * from './systems/triggerArea';
|
|
|
16
16
|
export * from './systems/physics';
|
|
17
17
|
export * from './engine/entity';
|
|
18
18
|
export * from './components/types';
|
|
19
|
-
import { MaterialComponentDefinitionExtended, MeshColliderComponentDefinitionExtended, MeshRendererComponentDefinitionExtended, TransformComponentExtended, AnimatorComponentDefinitionExtended, AudioSourceComponentDefinitionExtended, AudioAnalysisComponentDefinitionExtended, AudioStreamComponentDefinitionExtended, ISyncComponents, TweenComponentDefinitionExtended, INetowrkEntity, INetowrkParent, VirtualCameraComponentDefinitionExtended, InputModifierComponentDefinitionExtended, LightSourceComponentDefinitionExtended, TriggerAreaComponentDefinitionExtended, ParticleSystemComponentDefinitionExtended,
|
|
19
|
+
import { MaterialComponentDefinitionExtended, MeshColliderComponentDefinitionExtended, MeshRendererComponentDefinitionExtended, TransformComponentExtended, AnimatorComponentDefinitionExtended, AudioSourceComponentDefinitionExtended, AudioAnalysisComponentDefinitionExtended, AudioStreamComponentDefinitionExtended, ISyncComponents, TweenComponentDefinitionExtended, INetowrkEntity, INetowrkParent, VirtualCameraComponentDefinitionExtended, InputModifierComponentDefinitionExtended, LightSourceComponentDefinitionExtended, TriggerAreaComponentDefinitionExtended, ParticleSystemComponentDefinitionExtended, TouchScreenControlsComponentDefinitionExtended } from './components/types';
|
|
20
20
|
import { NameComponent } from './components/manual/Name';
|
|
21
21
|
import { TagsComponentDefinitionExtended } from './components/manual/Tags';
|
|
22
22
|
export declare const Transform: TransformComponentExtended;
|
|
@@ -52,7 +52,6 @@ export declare const NetworkEntity: INetowrkEntity;
|
|
|
52
52
|
* Tag a entity to be syncronized through comms
|
|
53
53
|
*/
|
|
54
54
|
export declare const NetworkParent: INetowrkParent;
|
|
55
|
-
export declare const CreatedBy: ICreatedBy;
|
|
56
55
|
export * from './components/generated/global.gen';
|
|
57
56
|
export * from './components/generated/types.gen';
|
|
58
57
|
export * from './serialization/crdt';
|
package/dist-cjs/index.js
CHANGED
|
@@ -26,7 +26,7 @@ var __importStar = (this && this.__importStar) || function (mod) {
|
|
|
26
26
|
return result;
|
|
27
27
|
};
|
|
28
28
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
29
|
-
exports.
|
|
29
|
+
exports.NetworkParent = exports.NetworkEntity = exports.SyncComponents = exports.TouchScreenControls = exports.ParticleSystem = exports.TriggerArea = exports.LightSource = exports.InputModifier = exports.VirtualCamera = exports.Tween = exports.Tags = exports.Name = exports.MeshCollider = exports.MeshRenderer = exports.Material = exports.AudioStream = exports.AudioAnalysis = exports.AudioSource = exports.Animator = exports.Transform = exports.components = exports.cyclicParentingChecker = void 0;
|
|
30
30
|
// The order of the following imports matters. Please do not auto-sort
|
|
31
31
|
__exportStar(require("./engine"), exports);
|
|
32
32
|
__exportStar(require("./schemas"), exports);
|
|
@@ -87,7 +87,6 @@ exports.NetworkEntity = components.NetworkEntity(initialization_1.engine);
|
|
|
87
87
|
* Tag a entity to be syncronized through comms
|
|
88
88
|
*/
|
|
89
89
|
exports.NetworkParent = components.NetworkParent(initialization_1.engine);
|
|
90
|
-
exports.CreatedBy = components.CreatedBy(initialization_1.engine);
|
|
91
90
|
// export components for global engine
|
|
92
91
|
__exportStar(require("./components/generated/global.gen"), exports);
|
|
93
92
|
__exportStar(require("./components/generated/types.gen"), exports);
|
|
@@ -23,4 +23,3 @@ __exportStar(require("./network/deleteComponentNetwork"), exports);
|
|
|
23
23
|
__exportStar(require("./network/deleteEntityNetwork"), exports);
|
|
24
24
|
__exportStar(require("./types"), exports);
|
|
25
25
|
__exportStar(require("./crdtMessageProtocol"), exports);
|
|
26
|
-
__exportStar(require("./authoritativePutComponent"), exports);
|
|
@@ -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,67 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.fixTransformParent = exports.localMessageToNetwork = exports.networkMessageToLocal = exports.isNetworkMessage = void 0;
|
|
4
|
+
const ByteBuffer_1 = require("../../ByteBuffer");
|
|
5
|
+
const putComponent_1 = require("../putComponent");
|
|
6
|
+
const types_1 = require("../types");
|
|
7
|
+
const deleteComponent_1 = require("../deleteComponent");
|
|
8
|
+
const deleteEntity_1 = require("../deleteEntity");
|
|
9
|
+
const putComponentNetwork_1 = require("./putComponentNetwork");
|
|
10
|
+
const deleteComponentNetwork_1 = require("./deleteComponentNetwork");
|
|
11
|
+
const deleteEntityNetwork_1 = require("./deleteEntityNetwork");
|
|
12
|
+
const Transform_1 = require("../../../components/manual/Transform");
|
|
13
|
+
/* istanbul ignore next */
|
|
14
|
+
function isNetworkMessage(message) {
|
|
15
|
+
return [
|
|
16
|
+
types_1.CrdtMessageType.DELETE_COMPONENT_NETWORK,
|
|
17
|
+
types_1.CrdtMessageType.DELETE_ENTITY_NETWORK,
|
|
18
|
+
types_1.CrdtMessageType.PUT_COMPONENT_NETWORK
|
|
19
|
+
].includes(message.type);
|
|
20
|
+
}
|
|
21
|
+
exports.isNetworkMessage = isNetworkMessage;
|
|
22
|
+
/* istanbul ignore next */
|
|
23
|
+
function networkMessageToLocal(message, localEntityId, buffer, destinationBuffer) {
|
|
24
|
+
const offset = buffer.currentWriteOffset();
|
|
25
|
+
if (message.type === types_1.CrdtMessageType.PUT_COMPONENT_NETWORK) {
|
|
26
|
+
putComponent_1.PutComponentOperation.write(localEntityId, message.timestamp, message.componentId, message.data, buffer);
|
|
27
|
+
}
|
|
28
|
+
else if (message.type === types_1.CrdtMessageType.DELETE_COMPONENT_NETWORK) {
|
|
29
|
+
deleteComponent_1.DeleteComponent.write(localEntityId, message.componentId, message.timestamp, buffer);
|
|
30
|
+
}
|
|
31
|
+
else if (message.type === types_1.CrdtMessageType.DELETE_ENTITY_NETWORK) {
|
|
32
|
+
deleteEntity_1.DeleteEntity.write(localEntityId, buffer);
|
|
33
|
+
}
|
|
34
|
+
destinationBuffer.writeBuffer(buffer.buffer().subarray(offset, buffer.currentWriteOffset()), false);
|
|
35
|
+
}
|
|
36
|
+
exports.networkMessageToLocal = networkMessageToLocal;
|
|
37
|
+
/* istanbul ignore next */
|
|
38
|
+
function localMessageToNetwork(message, network, buffer, destinationBuffer) {
|
|
39
|
+
const offset = buffer.currentWriteOffset();
|
|
40
|
+
if (message.type === types_1.CrdtMessageType.PUT_COMPONENT) {
|
|
41
|
+
putComponentNetwork_1.PutNetworkComponentOperation.write(network.entityId, message.timestamp, message.componentId, network.networkId, message.data, buffer);
|
|
42
|
+
}
|
|
43
|
+
else if (message.type === types_1.CrdtMessageType.DELETE_COMPONENT) {
|
|
44
|
+
deleteComponentNetwork_1.DeleteComponentNetwork.write(network.entityId, message.componentId, message.timestamp, network.networkId, buffer);
|
|
45
|
+
}
|
|
46
|
+
else if (message.type === types_1.CrdtMessageType.DELETE_ENTITY) {
|
|
47
|
+
deleteEntityNetwork_1.DeleteEntityNetwork.write(network.entityId, network.networkId, buffer);
|
|
48
|
+
}
|
|
49
|
+
destinationBuffer.writeBuffer(buffer.buffer().subarray(offset, buffer.currentWriteOffset()), false);
|
|
50
|
+
}
|
|
51
|
+
exports.localMessageToNetwork = localMessageToNetwork;
|
|
52
|
+
const buffer = new ByteBuffer_1.ReadWriteByteBuffer();
|
|
53
|
+
/* istanbul ignore next */
|
|
54
|
+
function fixTransformParent(message, transformValue, parent) {
|
|
55
|
+
buffer.resetBuffer();
|
|
56
|
+
let transform = transformValue;
|
|
57
|
+
if (!transform && 'data' in message) {
|
|
58
|
+
transform = Transform_1.TransformSchema.deserialize(new ByteBuffer_1.ReadWriteByteBuffer(message.data));
|
|
59
|
+
}
|
|
60
|
+
if (!transform)
|
|
61
|
+
throw new Error('Invalid parent transform');
|
|
62
|
+
// Generate new transform raw data with the parent
|
|
63
|
+
const newTransform = { ...transform, parent };
|
|
64
|
+
Transform_1.TransformSchema.serialize(newTransform, buffer);
|
|
65
|
+
return buffer.toBinary();
|
|
66
|
+
}
|
|
67
|
+
exports.fixTransformParent = fixTransformParent;
|
|
@@ -11,8 +11,7 @@ export declare enum CrdtMessageType {
|
|
|
11
11
|
PUT_COMPONENT_NETWORK = 5,
|
|
12
12
|
DELETE_COMPONENT_NETWORK = 6,
|
|
13
13
|
DELETE_ENTITY_NETWORK = 7,
|
|
14
|
-
|
|
15
|
-
MAX_MESSAGE_TYPE = 9
|
|
14
|
+
MAX_MESSAGE_TYPE = 8
|
|
16
15
|
}
|
|
17
16
|
/**
|
|
18
17
|
* Min length = 8 bytes
|
|
@@ -49,23 +48,6 @@ export type PutNetworkComponentMessageBody = Omit<PutComponentMessageBody, 'type
|
|
|
49
48
|
type: CrdtMessageType.PUT_COMPONENT_NETWORK;
|
|
50
49
|
networkId: number;
|
|
51
50
|
};
|
|
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
|
-
};
|
|
69
51
|
/**
|
|
70
52
|
* Min. length = header (8 bytes) + 16 bytes = 24 bytes
|
|
71
53
|
*
|
|
@@ -132,10 +114,6 @@ export type AppendValueMessage = CrdtMessageHeader & AppendValueMessageBody;
|
|
|
132
114
|
* @public
|
|
133
115
|
*/
|
|
134
116
|
export type PutComponentMessage = CrdtMessageHeader & PutComponentMessageBody;
|
|
135
|
-
/**
|
|
136
|
-
* @public
|
|
137
|
-
*/
|
|
138
|
-
export type AuthoritativePutComponentMessage = CrdtMessageHeader & AuthoritativePutComponentMessageBody;
|
|
139
117
|
/**
|
|
140
118
|
* @public
|
|
141
119
|
*/
|
|
@@ -159,7 +137,7 @@ export type DeleteEntityNetworkMessage = CrdtMessageHeader & DeleteEntityNetwork
|
|
|
159
137
|
/**
|
|
160
138
|
* @public
|
|
161
139
|
*/
|
|
162
|
-
export type CrdtMessage = PutComponentMessage |
|
|
140
|
+
export type CrdtMessage = PutComponentMessage | DeleteComponentMessage | AppendValueMessage | DeleteEntityMessage | PutNetworkComponentMessage | DeleteComponentNetworkMessage | DeleteEntityNetworkMessage;
|
|
163
141
|
/**
|
|
164
142
|
* @public
|
|
165
143
|
*/
|
|
@@ -167,7 +145,7 @@ export type CrdtNetworkMessageBody = PutNetworkComponentMessageBody | DeleteComp
|
|
|
167
145
|
/**
|
|
168
146
|
* @public
|
|
169
147
|
*/
|
|
170
|
-
export type CrdtMessageBody = PutComponentMessageBody |
|
|
148
|
+
export type CrdtMessageBody = PutComponentMessageBody | DeleteComponentMessageBody | DeleteEntityMessageBody | AppendValueMessageBody | CrdtNetworkMessageBody;
|
|
171
149
|
export declare enum ProcessMessageResultType {
|
|
172
150
|
/**
|
|
173
151
|
* Typical message and new state set.
|
|
@@ -16,9 +16,7 @@ var CrdtMessageType;
|
|
|
16
16
|
CrdtMessageType[CrdtMessageType["PUT_COMPONENT_NETWORK"] = 5] = "PUT_COMPONENT_NETWORK";
|
|
17
17
|
CrdtMessageType[CrdtMessageType["DELETE_COMPONENT_NETWORK"] = 6] = "DELETE_COMPONENT_NETWORK";
|
|
18
18
|
CrdtMessageType[CrdtMessageType["DELETE_ENTITY_NETWORK"] = 7] = "DELETE_ENTITY_NETWORK";
|
|
19
|
-
|
|
20
|
-
CrdtMessageType[CrdtMessageType["AUTHORITATIVE_PUT_COMPONENT"] = 8] = "AUTHORITATIVE_PUT_COMPONENT";
|
|
21
|
-
CrdtMessageType[CrdtMessageType["MAX_MESSAGE_TYPE"] = 9] = "MAX_MESSAGE_TYPE";
|
|
19
|
+
CrdtMessageType[CrdtMessageType["MAX_MESSAGE_TYPE"] = 8] = "MAX_MESSAGE_TYPE";
|
|
22
20
|
})(CrdtMessageType = exports.CrdtMessageType || (exports.CrdtMessageType = {}));
|
|
23
21
|
/**
|
|
24
22
|
* @public
|