@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.
Files changed (53) hide show
  1. package/dist/components/index.d.ts +0 -5
  2. package/dist/components/index.js +2 -5
  3. package/dist/components/manual/Transform.d.ts +0 -9
  4. package/dist/components/manual/Transform.js +3 -3
  5. package/dist/components/types.d.ts +0 -1
  6. package/dist/engine/component.d.ts +1 -52
  7. package/dist/engine/entity.js +31 -4
  8. package/dist/engine/grow-only-value-set-component-definition.js +2 -45
  9. package/dist/engine/index.js +22 -3
  10. package/dist/engine/lww-element-set-component-definition.d.ts +1 -3
  11. package/dist/engine/lww-element-set-component-definition.js +13 -68
  12. package/dist/engine/types.d.ts +8 -1
  13. package/dist/index.d.ts +1 -2
  14. package/dist/index.js +0 -1
  15. package/dist/serialization/crdt/index.d.ts +0 -1
  16. package/dist/serialization/crdt/index.js +0 -1
  17. package/dist/serialization/crdt/network/utils.d.ts +9 -0
  18. package/dist/serialization/crdt/network/utils.js +60 -0
  19. package/dist/serialization/crdt/types.d.ts +3 -25
  20. package/dist/serialization/crdt/types.js +1 -3
  21. package/dist/systems/crdt/index.d.ts +1 -0
  22. package/dist/systems/crdt/index.js +146 -55
  23. package/dist-cjs/components/index.d.ts +0 -5
  24. package/dist-cjs/components/index.js +3 -7
  25. package/dist-cjs/components/manual/Transform.d.ts +0 -9
  26. package/dist-cjs/components/manual/Transform.js +3 -3
  27. package/dist-cjs/components/types.d.ts +0 -1
  28. package/dist-cjs/engine/component.d.ts +1 -52
  29. package/dist-cjs/engine/entity.js +31 -4
  30. package/dist-cjs/engine/grow-only-value-set-component-definition.js +1 -44
  31. package/dist-cjs/engine/index.js +21 -2
  32. package/dist-cjs/engine/lww-element-set-component-definition.d.ts +1 -3
  33. package/dist-cjs/engine/lww-element-set-component-definition.js +14 -71
  34. package/dist-cjs/engine/types.d.ts +8 -1
  35. package/dist-cjs/index.d.ts +1 -2
  36. package/dist-cjs/index.js +1 -2
  37. package/dist-cjs/serialization/crdt/index.d.ts +0 -1
  38. package/dist-cjs/serialization/crdt/index.js +0 -1
  39. package/dist-cjs/serialization/crdt/network/utils.d.ts +9 -0
  40. package/dist-cjs/serialization/crdt/network/utils.js +67 -0
  41. package/dist-cjs/serialization/crdt/types.d.ts +3 -25
  42. package/dist-cjs/serialization/crdt/types.js +1 -3
  43. package/dist-cjs/systems/crdt/index.d.ts +1 -0
  44. package/dist-cjs/systems/crdt/index.js +169 -55
  45. package/package.json +2 -2
  46. package/dist/components/manual/CreatedBy.d.ts +0 -9
  47. package/dist/components/manual/CreatedBy.js +0 -8
  48. package/dist/serialization/crdt/authoritativePutComponent.d.ts +0 -15
  49. package/dist/serialization/crdt/authoritativePutComponent.js +0 -47
  50. package/dist-cjs/components/manual/CreatedBy.d.ts +0 -9
  51. package/dist-cjs/components/manual/CreatedBy.js +0 -10
  52. package/dist-cjs/serialization/crdt/authoritativePutComponent.d.ts +0 -15
  53. package/dist-cjs/serialization/crdt/authoritativePutComponent.js +0 -50
@@ -23,7 +23,6 @@ import { TriggerAreaComponentDefinitionExtended } from './extended/TriggerArea';
23
23
  import { ParticleSystemComponentDefinitionExtended } from './extended/ParticleSystem';
24
24
  import { TouchScreenControlsComponentDefinitionExtended } from './extended/TouchScreenControls';
25
25
  import { TagsComponentDefinitionExtended } from './manual/Tags';
26
- import { ICreatedByType } from './manual/CreatedBy';
27
26
  export * from './generated/index.gen';
28
27
  export type { GrowOnlyValueSetComponentDefinition, LastWriteWinElementSetComponentDefinition, LwwComponentGetter, GSetComponentGetter };
29
28
  export declare const Transform: LwwComponentGetter<TransformComponentExtended>;
@@ -58,9 +57,5 @@ export declare const NetworkEntity: (engine: Pick<IEngine, 'defineComponent'>) =
58
57
  * @alpha
59
58
  */
60
59
  export declare const NetworkParent: (engine: Pick<IEngine, 'defineComponent'>) => LastWriteWinElementSetComponentDefinition<INetowrkParentType>;
61
- /**
62
- * @public
63
- */
64
- export declare const CreatedBy: (engine: Pick<IEngine, 'defineComponent'>) => LastWriteWinElementSetComponentDefinition<ICreatedByType>;
65
60
  export { MediaState };
66
61
  export type { AudioAnalysisView };
@@ -19,7 +19,6 @@ import { defineTriggerAreaComponent } from './extended/TriggerArea';
19
19
  import { defineParticleSystemComponent } from './extended/ParticleSystem';
20
20
  import { defineTouchScreenControlsComponent } from './extended/TouchScreenControls';
21
21
  import defineTagsComponent from './manual/Tags';
22
- import defineCreatedBy from './manual/CreatedBy';
23
22
  export * from './generated/index.gen';
24
23
  /* @__PURE__ */
25
24
  export const Transform = (engine) => defineTransformComponent(engine);
@@ -66,13 +65,11 @@ export const SyncComponents = (engine) => defineSyncComponent(engine);
66
65
  /**
67
66
  * @alpha
68
67
  */
68
+ /* @__PURE__ */
69
69
  export const NetworkEntity = (engine) => defineNetworkEntity(engine);
70
70
  /**
71
71
  * @alpha
72
72
  */
73
+ /* @__PURE__ */
73
74
  export const NetworkParent = (engine) => defineNetworkParent(engine);
74
- /**
75
- * @public
76
- */
77
- export const CreatedBy = (engine) => defineCreatedBy(engine);
78
75
  export { MediaState };
@@ -1,6 +1,5 @@
1
1
  import { LastWriteWinElementSetComponentDefinition, IEngine } from '../../engine';
2
2
  import { Entity } from '../../engine/entity';
3
- import type { ISchema } from '../../schemas/ISchema';
4
3
  import type { Vector3Type } from '../../schemas/custom/Vector3';
5
4
  /**
6
5
  * @public
@@ -25,10 +24,6 @@ export interface TransformComponentExtended extends TransformComponent {
25
24
  */
26
25
  localToWorldDirection(entity: Entity, localDirection: Vector3Type): Vector3Type;
27
26
  }
28
- /**
29
- * @public
30
- */
31
- export declare const COMPONENT_ID = 1;
32
27
  /**
33
28
  * @public
34
29
  */
@@ -51,10 +46,6 @@ export type TransformType = {
51
46
  };
52
47
  parent?: Entity;
53
48
  };
54
- /** @public */
55
- export declare const TRANSFORM_LENGTH = 44;
56
- /** @public */
57
- export declare const TransformSchema: ISchema<TransformType>;
58
49
  /**
59
50
  * @public
60
51
  */
@@ -3,12 +3,12 @@
3
3
  // getters via __importStar in CJS), so by the time methods are called all exports are available.
4
4
  import * as treeHelpers from '../../runtime/helpers/tree';
5
5
  /**
6
- * @public
6
+ * @internal
7
7
  */
8
8
  export const COMPONENT_ID = 1;
9
- /** @public */
9
+ /** @internal */
10
10
  export const TRANSFORM_LENGTH = 44;
11
- /** @public */
11
+ /** @internal */
12
12
  export const TransformSchema = {
13
13
  serialize(value, builder) {
14
14
  const ptr = builder.incrementWriteOffset(TRANSFORM_LENGTH);
@@ -13,7 +13,6 @@ export type { TagsComponentDefinitionExtended, TagsType } from './manual/Tags';
13
13
  export type { ISyncComponents, ISyncComponentsType } from './manual/SyncComponents';
14
14
  export type { INetowrkEntity, INetowrkEntityType } from './manual/NetworkEntity';
15
15
  export type { INetowrkParent, INetowrkParentType } from './manual/NetworkParent';
16
- export type { ICreatedBy, ICreatedByType } from './manual/CreatedBy';
17
16
  export type { InputModifierHelper, InputModifierComponentDefinitionExtended } from './extended/InputModifier';
18
17
  export type { LightSourceHelper, LightSourceComponentDefinitionExtended } from './extended/LightSource';
19
18
  export type { TriggerAreaComponentDefinitionExtended } from './extended/TriggerArea';
@@ -1,6 +1,6 @@
1
1
  import { ISchema } from '../schemas';
2
2
  import { ByteBuffer } from '../serialization/ByteBuffer';
3
- import { CrdtMessageBody, DeleteComponentMessageBody, ProcessMessageResultType, PutComponentMessageBody } from '../serialization/crdt';
3
+ import { CrdtMessageBody, DeleteComponentMessageBody, PutComponentMessageBody } from '../serialization/crdt';
4
4
  import { Entity } from './entity';
5
5
  import { DeepReadonly, DeepReadonlySet } from './readonly';
6
6
  /**
@@ -70,58 +70,7 @@ export interface BaseComponent<T> {
70
70
  * If the value is undefined, the component was deleted.
71
71
  */
72
72
  onChange(entity: Entity, cb: (value: T | undefined) => void): void;
73
- /**
74
- * @public
75
- *
76
- */
77
- validateBeforeChange(entity: Entity, cb: ValidateCallback<T>): void;
78
- validateBeforeChange(cb: ValidateCallback<T>): void;
79
- /**
80
- * Get the CRDT state for an entity (serialized data and timestamp)
81
- * @param entity - Entity to get the CRDT state for
82
- * @returns Object with serialized data and timestamp, or null if entity doesn't have the component
83
- * @public
84
- */
85
- getCrdtState(entity: Entity): {
86
- data: Uint8Array;
87
- timestamp: number;
88
- } | null;
89
- }
90
- /**
91
- * Internal component interface that exposes all internal methods for SDK use
92
- * This is not exposed to users, only for internal SDK operations
93
- */
94
- export interface InternalBaseComponent<T> extends BaseComponent<T> {
95
- /**
96
- * @public
97
- * Dry run update to check if a CRDT message would be accepted without actually applying it
98
- */
99
- __dry_run_updateFromCrdt(body: CrdtMessageBody): ProcessMessageResultType;
100
- /**
101
- * @public
102
- * Get the iterator to every entity has the component
103
- */
104
- iterator(): Iterable<[Entity, any]>;
105
- /**
106
- * @public
107
- */
108
- dirtyIterator(): Iterable<Entity>;
109
- /**
110
- * @public
111
- */
112
- __onChangeCallbacks(entity: Entity, value: T): void;
113
- /**
114
- * @public
115
- */
116
- __run_validateBeforeChange(entity: Entity, newValue: T | undefined, senderAddress: string, createdBy: string): boolean;
117
73
  }
118
- export type ValidateCallback<T> = (value: {
119
- entity: Entity;
120
- currentValue: T | undefined;
121
- newValue: T | undefined;
122
- senderAddress: string;
123
- createdBy: string;
124
- }) => boolean;
125
74
  /**
126
75
  * @public
127
76
  */
@@ -56,6 +56,16 @@ export var EntityState;
56
56
  */
57
57
  EntityState[EntityState["Reserved"] = 3] = "Reserved";
58
58
  })(EntityState || (EntityState = {}));
59
+ /**
60
+ * True when `entity`'s NUMBER falls in the renderer-reserved range, at any version.
61
+ *
62
+ * Masks rather than calling `EntityUtils.fromEntityId`, which allocates a tuple per call to
63
+ * read one number; this runs on every inbound CRDT message. `entity & MAX_U16` already lands
64
+ * in [0, 65535], so the `>>> 0` fromEntityId applies is a no-op here.
65
+ */
66
+ function isReservedEntity(entity, reservedStaticEntities) {
67
+ return (entity & MAX_U16) < reservedStaticEntities;
68
+ }
59
69
  /**
60
70
  * @public
61
71
  */
@@ -90,7 +100,11 @@ export function createEntityContainer(opts) {
90
100
  return generateNewEntity();
91
101
  }
92
102
  for (const [number, version] of removedEntities.getMap()) {
93
- if (version < MAX_U16) {
103
+ // Never recycle a renderer-reserved number: the renderer reissues those slots as
104
+ // toEntityId(number, version + 1) too, from the same stored version, so it would hand
105
+ // the scene an id belonging to a live remote player. Belt-and-braces โ€” every writer
106
+ // into `removedEntities` refuses reserved numbers, so this cannot fire today.
107
+ if (number >= reservedStaticEntities && version < MAX_U16) {
94
108
  const entity = EntityUtils.toEntityId(number, version + 1);
95
109
  // If the entity is not being used, we can re-use it
96
110
  // If the entity was removed in this tick, we're not counting for the usedEntities, but we have it in the toRemoveEntityArray
@@ -103,7 +117,7 @@ export function createEntityContainer(opts) {
103
117
  return generateNewEntity();
104
118
  }
105
119
  function removeEntity(entity) {
106
- if (entity < reservedStaticEntities)
120
+ if (isReservedEntity(entity, reservedStaticEntities))
107
121
  return false;
108
122
  if (usedEntities.has(entity)) {
109
123
  usedEntities.delete(entity);
@@ -126,6 +140,12 @@ export function createEntityContainer(opts) {
126
140
  return arr;
127
141
  }
128
142
  function updateRemovedEntity(entity) {
143
+ // Called for EVERY inbound DELETE_ENTITY, including the renderer's tombstones for
144
+ // departed remote players โ€” so this is the door reserved numbers would otherwise enter
145
+ // the free list through. They need no tombstone: getEntityState reports them Reserved
146
+ // before it consults `removedEntities`, so `Removed` is unreachable for them anyway.
147
+ if (isReservedEntity(entity, reservedStaticEntities))
148
+ return false;
129
149
  const [n, v] = EntityUtils.fromEntityId(entity);
130
150
  // Update the removed entities map
131
151
  removedEntities.addTo(n, v);
@@ -136,6 +156,11 @@ export function createEntityContainer(opts) {
136
156
  return true;
137
157
  }
138
158
  function updateUsedEntity(entity) {
159
+ // Same invariant as updateRemovedEntity. Unreachable from the CRDT path today
160
+ // (getEntityState returns `Reserved`, never `Unknown`), but the `v > 0` branch below
161
+ // would seed `removedEntities` with a reserved number.
162
+ if (isReservedEntity(entity, reservedStaticEntities))
163
+ return false;
139
164
  const [n, v] = EntityUtils.fromEntityId(entity);
140
165
  // if the entity was removed then abort fast
141
166
  if (removedEntities.has(n, v))
@@ -151,10 +176,12 @@ export function createEntityContainer(opts) {
151
176
  return true;
152
177
  }
153
178
  function getEntityState(entity) {
154
- const [n, v] = EntityUtils.fromEntityId(entity);
155
- if (n < reservedStaticEntities) {
179
+ // Same guard as the three above, so `Engine.removeEntity` โ€” which classifies via
180
+ // getEntityState โ€” cannot disagree with whether this container releases the id.
181
+ if (isReservedEntity(entity, reservedStaticEntities)) {
156
182
  return EntityState.Reserved;
157
183
  }
184
+ const [n, v] = EntityUtils.fromEntityId(entity);
158
185
  if (usedEntities.has(entity)) {
159
186
  return EntityState.UsedEntity;
160
187
  }
@@ -1,8 +1,7 @@
1
1
  import { ReadWriteByteBuffer } from '../serialization/ByteBuffer';
2
- import { AppendValueOperation, CrdtMessageType, ProcessMessageResultType } from '../serialization/crdt';
2
+ import { AppendValueOperation, CrdtMessageType } from '../serialization/crdt';
3
3
  import { __DEV__ } from '../runtime/invariant';
4
4
  const emptyReadonlySet = freezeSet(new Set());
5
- const __GLOBAL_ENTITY = '__GLOBAL_ENTITY';
6
5
  function frozenError() {
7
6
  throw new Error('The set is frozen');
8
7
  }
@@ -23,7 +22,6 @@ export function createValueSetComponentDefinitionFromSchema(componentName, compo
23
22
  const dirtyIterator = new Set();
24
23
  const queuedCommands = [];
25
24
  const onChangeCallbacks = new Map();
26
- const validateCallbacks = new Map();
27
25
  // only sort the array if the latest (N) element has a timestamp <= N-1
28
26
  function shouldSort(row) {
29
27
  const len = row.raw.length;
@@ -82,11 +80,8 @@ export function createValueSetComponentDefinitionFromSchema(componentName, compo
82
80
  has(entity) {
83
81
  return data.has(entity);
84
82
  },
85
- entityDeleted(entity, markAsDirty) {
83
+ entityDeleted(entity) {
86
84
  data.delete(entity);
87
- if (markAsDirty) {
88
- // For grow-only sets, we don't need to mark as dirty since deletion doesn't generate CRDT messages
89
- }
90
85
  },
91
86
  get(entity) {
92
87
  const values = data.get(entity);
@@ -157,44 +152,6 @@ export function createValueSetComponentDefinitionFromSchema(componentName, compo
157
152
  for (const cb of cbs) {
158
153
  cb(value);
159
154
  }
160
- },
161
- __dry_run_updateFromCrdt(_body) {
162
- return ProcessMessageResultType.StateUpdatedData;
163
- },
164
- validateBeforeChange(entityOrCb, cb) {
165
- if (arguments.length === 1) {
166
- // Second overload: just callback (global validation)
167
- validateCallbacks.set(__GLOBAL_ENTITY, entityOrCb);
168
- }
169
- else {
170
- if (cb) {
171
- validateCallbacks.set(entityOrCb, cb);
172
- }
173
- }
174
- },
175
- __run_validateBeforeChange(entity, newValue, senderAddress, createdBy) {
176
- const cb = entity && validateCallbacks.get(entity);
177
- const globalCb = validateCallbacks.get(__GLOBAL_ENTITY);
178
- const currentValue = [...this.get(entity).values()];
179
- const value = { entity, currentValue: currentValue, newValue, senderAddress, createdBy };
180
- const globalResult = globalCb?.(value) ?? true;
181
- const entityResult = (globalResult && cb?.(value)) ?? true;
182
- return globalResult && entityResult;
183
- },
184
- getCrdtState(entity) {
185
- const row = data.get(entity);
186
- if (!row || row.raw.length === 0) {
187
- return null;
188
- }
189
- // For GrowOnlySet, we need to return the complete CRDT messages for all values
190
- // This is complex because GrowOnlySet uses APPEND messages, not a single PUT
191
- // For now, return null to indicate this component type doesn't support simple corrections
192
- return null;
193
- },
194
- __forceUpdateFromCrdt(_msg) {
195
- // GrowOnlySet doesn't support authoritative corrections in the same way as LWW
196
- // since it uses APPEND_VALUE messages instead of PUT_COMPONENT messages
197
- return [null, undefined];
198
155
  }
199
156
  };
200
157
  return ret;
@@ -4,7 +4,7 @@ import { checkNotThenable } from '../runtime/invariant';
4
4
  import { Schemas } from '../schemas';
5
5
  import { crdtSceneSystem } from '../systems/crdt';
6
6
  import { createComponentDefinitionFromSchema } from './lww-element-set-component-definition';
7
- import { createEntityContainer } from './entity';
7
+ import { EntityState, EntityUtils, createEntityContainer } from './entity';
8
8
  import { SystemContainer, SYSTEMS_REGULAR_PRIORITY } from './systems';
9
9
  import { createValueSetComponentDefinitionFromSchema } from './grow-only-value-set-component-definition';
10
10
  import { removeEntityWithChildren as removeEntityWithChildrenEngine } from '../runtime/helpers/tree';
@@ -12,6 +12,8 @@ import { CrdtMessageType } from '../serialization/crdt';
12
12
  export * from './input';
13
13
  export * from './readonly';
14
14
  export * from './types';
15
+ /** RootEntity 0, PlayerEntity 1, CameraEntity 2 โ€” see the engineInstance fields below. */
16
+ const NAMED_STATIC_ENTITIES = 3;
15
17
  function preEngine(options) {
16
18
  const entityContainer = options?.entityContainer ?? createEntityContainer();
17
19
  const componentsDefinition = new Map();
@@ -28,6 +30,18 @@ function preEngine(options) {
28
30
  return entity;
29
31
  }
30
32
  function removeEntity(entity) {
33
+ // The renderer streams the avatar range and drops the scene's deletes there, so purging
34
+ // locally is permanent: a one-shot component like PlayerIdentityData is never re-sent, and
35
+ // the entity is left as a moving Transform with no identity. The three named static
36
+ // entities are reserved too, but the renderer DOES apply scene deletes on them โ€” that is
37
+ // how InputModifier.deleteFrom(engine.PlayerEntity) clears an input lock โ€” so they must
38
+ // still be purged. Asking the container keeps this in step with whether it releases the
39
+ // id, including for a custom container with a different reserved range.
40
+ const [entityNumber] = EntityUtils.fromEntityId(entity);
41
+ const isAvatarEntity = entityNumber >= NAMED_STATIC_ENTITIES && entityContainer.getEntityState(entity) === EntityState.Reserved;
42
+ const released = entityContainer.removeEntity(entity);
43
+ if (isAvatarEntity)
44
+ return released;
31
45
  for (const [, component] of componentsDefinition) {
32
46
  // TODO: hack for the moment.
33
47
  // We still need the NetworkEntity to forward this message to the SyncTransport.
@@ -36,7 +50,7 @@ function preEngine(options) {
36
50
  continue;
37
51
  component.entityDeleted(entity, true);
38
52
  }
39
- return entityContainer.removeEntity(entity);
53
+ return released;
40
54
  }
41
55
  function removeEntityWithChildren(entity) {
42
56
  return removeEntityWithChildrenEngine({ removeEntity, defineComponentFromSchema, getEntitiesWith, defineComponent }, entity);
@@ -260,7 +274,12 @@ export function Engine(options) {
260
274
  RootEntity: 0,
261
275
  PlayerEntity: 1,
262
276
  CameraEntity: 2,
263
- getEntityState: partialEngine.entityContainer.getEntityState,
277
+ // Delegated, not aliased. A detached `entityContainer.getEntityState` reference binds
278
+ // `this` to the engine, so a custom IEntityContainer that uses `this` silently reports the
279
+ // wrong state through the public API while the engine itself, which calls the container
280
+ // directly, reports the right one. Harmless for the built-in closure-based container, but
281
+ // removeEntity now classifies through getEntityState, so the two must never diverge.
282
+ getEntityState: (entity) => partialEngine.entityContainer.getEntityState(entity),
264
283
  addTransport: crdtSystem.addTransport,
265
284
  entityContainer: partialEngine.entityContainer
266
285
  };
@@ -1,10 +1,8 @@
1
1
  import { ISchema } from '../schemas';
2
2
  import { ByteBuffer } from '../serialization/ByteBuffer';
3
- import { PutComponentMessageBody, DeleteComponentMessageBody, ProcessMessageResultType, CrdtMessageBody, PutNetworkComponentMessageBody, DeleteComponentNetworkMessageBody, AuthoritativePutComponentMessageBody } from '../serialization/crdt';
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>;
@@ -30,12 +30,16 @@ export function createDumpLwwFunctionFromCrdt(componentId, timestamps, schema, d
30
30
  }
31
31
  };
32
32
  }
33
- const __GLOBAL_ENTITY = '__GLOBAL_ENTITY';
34
- export function createCrdtRuleValidator(timestamps, schema, data, lastSentData) {
33
+ export function createUpdateLwwFromCrdt(componentId, timestamps, schema, data, lastSentData) {
35
34
  /**
36
- * Shared CRDT conflict resolution logic
37
- * @public
38
- */
35
+ * Process the received message only if the lamport number recieved is higher
36
+ * than the stored one. If its lower, we spread it to the network to correct the peer.
37
+ * If they are equal, the bigger raw data wins.
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
+ */
39
43
  function crdtRuleForCurrentState(message) {
40
44
  const { entityId, timestamp } = message;
41
45
  const currentTimestamp = timestamps.get(entityId);
@@ -45,6 +49,7 @@ export function createCrdtRuleValidator(timestamps, schema, data, lastSentData)
45
49
  }
46
50
  // Outdated Message. Resend our state message through the wire.
47
51
  if (currentTimestamp > timestamp) {
52
+ // console.log('2', currentTimestamp, timestamp)
48
53
  return ProcessMessageResultType.StateOutdatedTimestamp;
49
54
  }
50
55
  // Deletes are idempotent
@@ -61,6 +66,7 @@ export function createCrdtRuleValidator(timestamps, schema, data, lastSentData)
61
66
  currentDataGreater = dataCompare(null, message.data);
62
67
  }
63
68
  // Same data, same timestamp. Weirdo echo message.
69
+ // console.log('3', currentDataGreater, writeBuffer.toBinary(), (message as any).data || null)
64
70
  if (currentDataGreater === 0) {
65
71
  return ProcessMessageResultType.NoChanges;
66
72
  }
@@ -69,35 +75,10 @@ export function createCrdtRuleValidator(timestamps, schema, data, lastSentData)
69
75
  return ProcessMessageResultType.StateOutdatedData;
70
76
  }
71
77
  else {
72
- // Current data is lower
78
+ // Curent data is lower
73
79
  return ProcessMessageResultType.StateUpdatedData;
74
80
  }
75
81
  }
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
- const buffer = new ReadWriteByteBuffer(msg.data);
84
- const deserializedValue = schema.deserialize(buffer);
85
- data.set(msg.entityId, deserializedValue);
86
- timestamps.set(msg.entityId, msg.timestamp);
87
- return [null, deserializedValue];
88
- };
89
- }
90
- export function createUpdateLwwFromCrdt(componentId, timestamps, schema, data, lastSentData) {
91
- /**
92
- * Process the received message only if the lamport number recieved is higher
93
- * than the stored one. If its lower, we spread it to the network to correct the peer.
94
- * If they are equal, the bigger raw data wins.
95
-
96
- * Returns the recieved data if the lamport number was bigger than ours.
97
- * If it was an outdated message, then we return void
98
- * @public
99
- */
100
- const crdtRuleForCurrentState = createCrdtRuleValidator(timestamps, schema, data, lastSentData);
101
82
  return (msg) => {
102
83
  /* istanbul ignore next */
103
84
  if (msg.type !== CrdtMessageType.PUT_COMPONENT &&
@@ -115,10 +96,7 @@ export function createUpdateLwwFromCrdt(componentId, timestamps, schema, data, l
115
96
  if (msg.type === CrdtMessageType.PUT_COMPONENT || msg.type === CrdtMessageType.PUT_COMPONENT_NETWORK) {
116
97
  const buf = new ReadWriteByteBuffer(msg.data);
117
98
  data.set(entity, schema.deserialize(buf));
118
- // do NOT seed lastSentData from an INCOMING message!!!!
119
- // seeding it from received bytes makes the engine
120
- // believe it already sent this state, which kills valid re-broadcasting ๐Ÿงจ
121
- lastSentData.delete(entity);
99
+ lastSentData.set(entity, new Uint8Array(msg.data));
122
100
  }
123
101
  else {
124
102
  data.delete(entity);
@@ -207,7 +185,6 @@ export function createComponentDefinitionFromSchema(componentName, componentId,
207
185
  const timestamps = new Map();
208
186
  const lastSentData = new Map();
209
187
  const onChangeCallbacks = new Map();
210
- const validateCallbacks = new Map();
211
188
  return {
212
189
  get componentId() {
213
190
  return componentId;
@@ -302,39 +279,7 @@ export function createComponentDefinitionFromSchema(componentName, componentId,
302
279
  },
303
280
  getCrdtUpdates: createGetCrdtMessagesForLww(componentId, timestamps, dirtyIterator, schema, data, lastSentData),
304
281
  updateFromCrdt: createUpdateLwwFromCrdt(componentId, timestamps, schema, data, lastSentData),
305
- __forceUpdateFromCrdt: createForceUpdateLwwFromCrdt(componentId, timestamps, schema, data),
306
- __dry_run_updateFromCrdt: createCrdtRuleValidator(timestamps, schema, data, lastSentData),
307
282
  dumpCrdtStateToBuffer: createDumpLwwFunctionFromCrdt(componentId, timestamps, schema, data),
308
- validateBeforeChange(entityOrCb, cb) {
309
- if (arguments.length === 1) {
310
- // Second overload: just callback (global validation)
311
- validateCallbacks.set(__GLOBAL_ENTITY, entityOrCb);
312
- }
313
- else {
314
- if (cb) {
315
- validateCallbacks.set(entityOrCb, cb);
316
- }
317
- }
318
- },
319
- __run_validateBeforeChange(entity, newValue, senderAddress, createdBy) {
320
- const cb = entity && validateCallbacks.get(entity);
321
- const globalCb = validateCallbacks.get(__GLOBAL_ENTITY);
322
- const currentValue = data.get(entity);
323
- const value = { entity, currentValue, newValue, senderAddress, createdBy };
324
- const globalResult = globalCb?.(value) ?? true;
325
- const entityResult = (globalResult && cb?.(value)) ?? true;
326
- return globalResult && entityResult;
327
- },
328
- getCrdtState(entity) {
329
- const componentData = data.get(entity);
330
- const timestamp = timestamps.get(entity);
331
- if (componentData && timestamp !== undefined) {
332
- const buffer = new ReadWriteByteBuffer();
333
- schema.serialize(deepReadonly(componentData), buffer);
334
- return { data: buffer.toBinary(), timestamp };
335
- }
336
- return null;
337
- },
338
283
  onChange(entity, cb) {
339
284
  const cbs = onChangeCallbacks.get(entity) ?? [];
340
285
  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): void;
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/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, ICreatedBy, TouchScreenControlsComponentDefinitionExtended } from './components/types';
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/index.js CHANGED
@@ -58,7 +58,6 @@ export const NetworkEntity = /* @__PURE__*/ components.NetworkEntity(engine);
58
58
  * Tag a entity to be syncronized through comms
59
59
  */
60
60
  export const NetworkParent = /* @__PURE__*/ components.NetworkParent(engine);
61
- export const CreatedBy = /* @__PURE__*/ components.CreatedBy(engine);
62
61
  // export components for global engine
63
62
  export * from './components/generated/global.gen';
64
63
  export * from './components/generated/types.gen';
@@ -7,4 +7,3 @@ export * from './network/deleteComponentNetwork';
7
7
  export * from './network/deleteEntityNetwork';
8
8
  export * from './types';
9
9
  export * from './crdtMessageProtocol';
10
- export * from './authoritativePutComponent';
@@ -7,4 +7,3 @@ export * from './network/deleteComponentNetwork';
7
7
  export * from './network/deleteEntityNetwork';
8
8
  export * from './types';
9
9
  export * from './crdtMessageProtocol';
10
- export * from './authoritativePutComponent';
@@ -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;