@dcl/ecs 7.20.4 → 7.20.5-22638270380.commit-3f48d5e
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 +5 -0
- package/dist/components/index.js +5 -2
- 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 -0
- package/dist/engine/component.d.ts +52 -1
- package/dist/engine/entity.js +2 -4
- 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 -1
- package/dist/index.js +1 -0
- 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/index.d.ts +5 -0
- package/dist-cjs/components/index.js +7 -3
- 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 -0
- package/dist-cjs/engine/component.d.ts +52 -1
- package/dist-cjs/engine/entity.js +2 -4
- 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 -1
- package/dist-cjs/index.js +2 -1
- 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/serialization/crdt/network/utils.d.ts +0 -9
- package/dist/serialization/crdt/network/utils.js +0 -60
- package/dist-cjs/serialization/crdt/network/utils.d.ts +0 -9
- package/dist-cjs/serialization/crdt/network/utils.js +0 -67
|
@@ -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
|
/**
|
|
@@ -19,6 +19,7 @@ import { InputModifierComponentDefinitionExtended } from './extended/InputModifi
|
|
|
19
19
|
import { LightSourceComponentDefinitionExtended } from './extended/LightSource';
|
|
20
20
|
import { TriggerAreaComponentDefinitionExtended } from './extended/TriggerArea';
|
|
21
21
|
import { TagsComponentDefinitionExtended } from './manual/Tags';
|
|
22
|
+
import { ICreatedByType } from './manual/CreatedBy';
|
|
22
23
|
export * from './generated/index.gen';
|
|
23
24
|
export type { GrowOnlyValueSetComponentDefinition, LastWriteWinElementSetComponentDefinition, LwwComponentGetter, GSetComponentGetter };
|
|
24
25
|
export declare const Transform: LwwComponentGetter<TransformComponentExtended>;
|
|
@@ -50,4 +51,8 @@ export declare const NetworkEntity: (engine: Pick<IEngine, 'defineComponent'>) =
|
|
|
50
51
|
* @alpha
|
|
51
52
|
*/
|
|
52
53
|
export declare const NetworkParent: (engine: Pick<IEngine, 'defineComponent'>) => LastWriteWinElementSetComponentDefinition<INetowrkParentType>;
|
|
54
|
+
/**
|
|
55
|
+
* @public
|
|
56
|
+
*/
|
|
57
|
+
export declare const CreatedBy: (engine: Pick<IEngine, 'defineComponent'>) => LastWriteWinElementSetComponentDefinition<ICreatedByType>;
|
|
53
58
|
export { MediaState };
|
|
@@ -17,7 +17,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
17
17
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
18
18
|
};
|
|
19
19
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
20
|
-
exports.MediaState = exports.NetworkParent = exports.NetworkEntity = exports.SyncComponents = exports.Tags = exports.Name = exports.TriggerArea = exports.LightSource = exports.InputModifier = exports.VirtualCamera = exports.Tween = exports.MeshCollider = exports.MeshRenderer = exports.AudioStream = exports.AudioSource = exports.Animator = exports.Material = exports.Transform = void 0;
|
|
20
|
+
exports.MediaState = exports.CreatedBy = exports.NetworkParent = exports.NetworkEntity = exports.SyncComponents = exports.Tags = exports.Name = exports.TriggerArea = exports.LightSource = exports.InputModifier = exports.VirtualCamera = exports.Tween = exports.MeshCollider = exports.MeshRenderer = exports.AudioStream = exports.AudioSource = exports.Animator = exports.Material = exports.Transform = void 0;
|
|
21
21
|
const Animator_1 = require("./extended/Animator");
|
|
22
22
|
const AudioSource_1 = require("./extended/AudioSource");
|
|
23
23
|
const Material_1 = require("./extended/Material");
|
|
@@ -37,6 +37,7 @@ const InputModifier_1 = require("./extended/InputModifier");
|
|
|
37
37
|
const LightSource_1 = require("./extended/LightSource");
|
|
38
38
|
const TriggerArea_1 = require("./extended/TriggerArea");
|
|
39
39
|
const Tags_1 = __importDefault(require("./manual/Tags"));
|
|
40
|
+
const CreatedBy_1 = __importDefault(require("./manual/CreatedBy"));
|
|
40
41
|
__exportStar(require("./generated/index.gen"), exports);
|
|
41
42
|
/* @__PURE__ */
|
|
42
43
|
const Transform = (engine) => (0, Transform_1.defineTransformComponent)(engine);
|
|
@@ -92,12 +93,15 @@ exports.SyncComponents = SyncComponents;
|
|
|
92
93
|
/**
|
|
93
94
|
* @alpha
|
|
94
95
|
*/
|
|
95
|
-
/* @__PURE__ */
|
|
96
96
|
const NetworkEntity = (engine) => (0, NetworkEntity_1.default)(engine);
|
|
97
97
|
exports.NetworkEntity = NetworkEntity;
|
|
98
98
|
/**
|
|
99
99
|
* @alpha
|
|
100
100
|
*/
|
|
101
|
-
/* @__PURE__ */
|
|
102
101
|
const NetworkParent = (engine) => (0, NetworkParent_1.default)(engine);
|
|
103
102
|
exports.NetworkParent = NetworkParent;
|
|
103
|
+
/**
|
|
104
|
+
* @public
|
|
105
|
+
*/
|
|
106
|
+
const CreatedBy = (engine) => (0, CreatedBy_1.default)(engine);
|
|
107
|
+
exports.CreatedBy = CreatedBy;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { IEngine, LastWriteWinElementSetComponentDefinition } from '../../engine/types';
|
|
2
|
+
export interface ICreatedByType {
|
|
3
|
+
address: string;
|
|
4
|
+
}
|
|
5
|
+
export type ICreatedBy = LastWriteWinElementSetComponentDefinition<ICreatedByType>;
|
|
6
|
+
declare function defineCreatedBy(engine: Pick<IEngine, 'defineComponent'>): import("../../engine/types").MapComponentDefinition<import("../..").MapResult<{
|
|
7
|
+
address: import("../../schemas").ISchema<string>;
|
|
8
|
+
}>>;
|
|
9
|
+
export default defineCreatedBy;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const schemas_1 = require("../../schemas");
|
|
4
|
+
function defineCreatedBy(engine) {
|
|
5
|
+
const CreatedBy = engine.defineComponent('core-schema::Created-By', {
|
|
6
|
+
address: schemas_1.Schemas.String
|
|
7
|
+
});
|
|
8
|
+
return CreatedBy;
|
|
9
|
+
}
|
|
10
|
+
exports.default = defineCreatedBy;
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { LastWriteWinElementSetComponentDefinition, IEngine } from '../../engine';
|
|
2
2
|
import { Entity } from '../../engine/entity';
|
|
3
|
+
import type { ISchema } from '../../schemas/ISchema';
|
|
3
4
|
/**
|
|
4
5
|
* @public
|
|
5
6
|
*/
|
|
@@ -11,6 +12,10 @@ export interface TransformComponentExtended extends TransformComponent {
|
|
|
11
12
|
create(entity: Entity, val?: TransformTypeWithOptionals): TransformType;
|
|
12
13
|
createOrReplace(entity: Entity, val?: TransformTypeWithOptionals): TransformType;
|
|
13
14
|
}
|
|
15
|
+
/**
|
|
16
|
+
* @public
|
|
17
|
+
*/
|
|
18
|
+
export declare const COMPONENT_ID = 1;
|
|
14
19
|
/**
|
|
15
20
|
* @public
|
|
16
21
|
*/
|
|
@@ -33,6 +38,10 @@ export type TransformType = {
|
|
|
33
38
|
};
|
|
34
39
|
parent?: Entity;
|
|
35
40
|
};
|
|
41
|
+
/** @public */
|
|
42
|
+
export declare const TRANSFORM_LENGTH = 44;
|
|
43
|
+
/** @public */
|
|
44
|
+
export declare const TransformSchema: ISchema<TransformType>;
|
|
36
45
|
/**
|
|
37
46
|
* @public
|
|
38
47
|
*/
|
|
@@ -2,12 +2,12 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.defineTransformComponent = exports.TransformSchema = exports.TRANSFORM_LENGTH = exports.COMPONENT_ID = void 0;
|
|
4
4
|
/**
|
|
5
|
-
* @
|
|
5
|
+
* @public
|
|
6
6
|
*/
|
|
7
7
|
exports.COMPONENT_ID = 1;
|
|
8
|
-
/** @
|
|
8
|
+
/** @public */
|
|
9
9
|
exports.TRANSFORM_LENGTH = 44;
|
|
10
|
-
/** @
|
|
10
|
+
/** @public */
|
|
11
11
|
exports.TransformSchema = {
|
|
12
12
|
serialize(value, builder) {
|
|
13
13
|
const ptr = builder.incrementWriteOffset(exports.TRANSFORM_LENGTH);
|
|
@@ -12,6 +12,7 @@ export type { TagsComponentDefinitionExtended, TagsType } from './manual/Tags';
|
|
|
12
12
|
export type { ISyncComponents, ISyncComponentsType } from './manual/SyncComponents';
|
|
13
13
|
export type { INetowrkEntity, INetowrkEntityType } from './manual/NetworkEntity';
|
|
14
14
|
export type { INetowrkParent, INetowrkParentType } from './manual/NetworkParent';
|
|
15
|
+
export type { ICreatedBy, ICreatedByType } from './manual/CreatedBy';
|
|
15
16
|
export type { InputModifierHelper, InputModifierComponentDefinitionExtended } from './extended/InputModifier';
|
|
16
17
|
export type { LightSourceHelper, LightSourceComponentDefinitionExtended } from './extended/LightSource';
|
|
17
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, PutComponentMessageBody } from '../serialization/crdt';
|
|
3
|
+
import { CrdtMessageBody, DeleteComponentMessageBody, ProcessMessageResultType, PutComponentMessageBody } from '../serialization/crdt';
|
|
4
4
|
import { Entity } from './entity';
|
|
5
5
|
import { DeepReadonly, DeepReadonlySet } from './readonly';
|
|
6
6
|
/**
|
|
@@ -70,7 +70,58 @@ 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;
|
|
73
117
|
}
|
|
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;
|
|
74
125
|
/**
|
|
75
126
|
* @public
|
|
76
127
|
*/
|
|
@@ -64,10 +64,8 @@ var EntityState;
|
|
|
64
64
|
*/
|
|
65
65
|
function createEntityContainer(opts) {
|
|
66
66
|
const reservedStaticEntities = opts?.reservedStaticEntities ?? exports.RESERVED_STATIC_ENTITIES;
|
|
67
|
-
//
|
|
68
|
-
|
|
69
|
-
const maxCompositeEntity = typeof DCL_MAX_COMPOSITE_ENTITY !== 'undefined' ? DCL_MAX_COMPOSITE_ENTITY : 0;
|
|
70
|
-
let entityCounter = Math.max(reservedStaticEntities, maxCompositeEntity > 0 ? maxCompositeEntity + 1 : 0);
|
|
67
|
+
// Local entities counter
|
|
68
|
+
let entityCounter = reservedStaticEntities;
|
|
71
69
|
const usedEntities = new Set();
|
|
72
70
|
let toRemoveEntities = [];
|
|
73
71
|
const removedEntities = (0, gset_1.createVersionGSet)();
|
|
@@ -5,6 +5,7 @@ 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';
|
|
8
9
|
function frozenError() {
|
|
9
10
|
throw new Error('The set is frozen');
|
|
10
11
|
}
|
|
@@ -25,6 +26,7 @@ function createValueSetComponentDefinitionFromSchema(componentName, componentId,
|
|
|
25
26
|
const dirtyIterator = new Set();
|
|
26
27
|
const queuedCommands = [];
|
|
27
28
|
const onChangeCallbacks = new Map();
|
|
29
|
+
const validateCallbacks = new Map();
|
|
28
30
|
// only sort the array if the latest (N) element has a timestamp <= N-1
|
|
29
31
|
function shouldSort(row) {
|
|
30
32
|
const len = row.raw.length;
|
|
@@ -83,8 +85,11 @@ function createValueSetComponentDefinitionFromSchema(componentName, componentId,
|
|
|
83
85
|
has(entity) {
|
|
84
86
|
return data.has(entity);
|
|
85
87
|
},
|
|
86
|
-
entityDeleted(entity) {
|
|
88
|
+
entityDeleted(entity, markAsDirty) {
|
|
87
89
|
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
|
+
}
|
|
88
93
|
},
|
|
89
94
|
get(entity) {
|
|
90
95
|
const values = data.get(entity);
|
|
@@ -155,6 +160,44 @@ function createValueSetComponentDefinitionFromSchema(componentName, componentId,
|
|
|
155
160
|
for (const cb of cbs) {
|
|
156
161
|
cb(value);
|
|
157
162
|
}
|
|
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];
|
|
158
201
|
}
|
|
159
202
|
};
|
|
160
203
|
return ret;
|
|
@@ -1,8 +1,10 @@
|
|
|
1
1
|
import { ISchema } from '../schemas';
|
|
2
2
|
import { ByteBuffer } from '../serialization/ByteBuffer';
|
|
3
|
-
import { PutComponentMessageBody, DeleteComponentMessageBody, CrdtMessageBody } from '../serialization/crdt';
|
|
3
|
+
import { PutComponentMessageBody, DeleteComponentMessageBody, ProcessMessageResultType, CrdtMessageBody, PutNetworkComponentMessageBody, DeleteComponentNetworkMessageBody, AuthoritativePutComponentMessageBody } 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>): (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];
|
|
7
9
|
export declare function createUpdateLwwFromCrdt(componentId: number, timestamps: Map<Entity, number>, schema: Pick<ISchema<any>, 'serialize' | 'deserialize'>, data: Map<Entity, unknown>): (msg: CrdtMessageBody) => [null | PutComponentMessageBody | DeleteComponentMessageBody, any];
|
|
8
10
|
export declare function createGetCrdtMessagesForLww(componentId: number, timestamps: Map<Entity, number>, dirtyIterator: Set<Entity>, schema: Pick<ISchema<any>, 'serialize'>, data: Map<Entity, unknown>): () => Generator<PutComponentMessageBody | DeleteComponentMessageBody, void, unknown>;
|