@esengine/network 2.2.0 → 3.0.0

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.
@@ -0,0 +1,173 @@
1
+ /**
2
+ * @zh 组件同步系统
3
+ * @en Component Sync System
4
+ *
5
+ * @zh 基于 @sync 装饰器的组件状态同步,与 ecs-framework 的 Sync 模块集成
6
+ * @en Component state synchronization based on @sync decorator, integrated with ecs-framework Sync module
7
+ */
8
+ import { EntitySystem, type Entity, type DecodeSnapshotResult, type DecodeSpawnResult } from '@esengine/ecs-framework';
9
+ /**
10
+ * @zh 组件同步事件类型
11
+ * @en Component sync event type
12
+ */
13
+ export type ComponentSyncEventType = 'entitySpawned' | 'entityDespawned' | 'stateUpdated';
14
+ /**
15
+ * @zh 组件同步事件
16
+ * @en Component sync event
17
+ */
18
+ export interface ComponentSyncEvent {
19
+ type: ComponentSyncEventType;
20
+ entityId: number;
21
+ prefabType?: string;
22
+ }
23
+ /**
24
+ * @zh 组件同步事件监听器
25
+ * @en Component sync event listener
26
+ */
27
+ export type ComponentSyncEventListener = (event: ComponentSyncEvent) => void;
28
+ /**
29
+ * @zh 组件同步配置
30
+ * @en Component sync configuration
31
+ */
32
+ export interface ComponentSyncConfig {
33
+ /**
34
+ * @zh 是否启用增量同步
35
+ * @en Whether to enable delta sync
36
+ */
37
+ enableDeltaSync: boolean;
38
+ /**
39
+ * @zh 同步间隔(毫秒)
40
+ * @en Sync interval in milliseconds
41
+ */
42
+ syncInterval: number;
43
+ }
44
+ /**
45
+ * @zh 组件同步系统
46
+ * @en Component sync system
47
+ *
48
+ * @zh 基于 @sync 装饰器自动同步组件状态
49
+ * @en Automatically syncs component state based on @sync decorator
50
+ *
51
+ * @example
52
+ * ```typescript
53
+ * // Server-side: broadcast state
54
+ * const syncSystem = scene.getSystem(ComponentSyncSystem);
55
+ * const data = syncSystem.encodeAllEntities(false); // delta
56
+ * broadcast(data);
57
+ *
58
+ * // Client-side: receive state
59
+ * const syncSystem = scene.getSystem(ComponentSyncSystem);
60
+ * syncSystem.applySnapshot(data);
61
+ * ```
62
+ */
63
+ export declare class ComponentSyncSystem extends EntitySystem {
64
+ private readonly _config;
65
+ private readonly _syncEntityMap;
66
+ private readonly _syncListeners;
67
+ private _lastSyncTime;
68
+ private _isServer;
69
+ constructor(config?: Partial<ComponentSyncConfig>, isServer?: boolean);
70
+ /**
71
+ * @zh 设置是否为服务端模式
72
+ * @en Set whether in server mode
73
+ */
74
+ set isServer(value: boolean);
75
+ /**
76
+ * @zh 获取是否为服务端模式
77
+ * @en Get whether in server mode
78
+ */
79
+ get isServer(): boolean;
80
+ /**
81
+ * @zh 获取配置
82
+ * @en Get configuration
83
+ */
84
+ get config(): Readonly<ComponentSyncConfig>;
85
+ /**
86
+ * @zh 添加同步事件监听器
87
+ * @en Add sync event listener
88
+ */
89
+ addSyncListener(listener: ComponentSyncEventListener): void;
90
+ /**
91
+ * @zh 移除同步事件监听器
92
+ * @en Remove sync event listener
93
+ */
94
+ removeSyncListener(listener: ComponentSyncEventListener): void;
95
+ /**
96
+ * @zh 注册同步组件类型
97
+ * @en Register sync component type
98
+ *
99
+ * @zh 客户端需要调用此方法注册所有需要同步的组件类型
100
+ * @en Client needs to call this to register all component types to be synced
101
+ */
102
+ registerComponent<T extends new () => any>(componentClass: T): void;
103
+ /**
104
+ * @zh 编码所有实体状态
105
+ * @en Encode all entities state
106
+ *
107
+ * @param fullSync - @zh 是否完整同步(首次连接时使用)@en Whether to do full sync (for initial connection)
108
+ * @returns @zh 编码后的二进制数据 @en Encoded binary data
109
+ */
110
+ encodeAllEntities(fullSync?: boolean): Uint8Array;
111
+ /**
112
+ * @zh 编码有变更的实体
113
+ * @en Encode entities with changes
114
+ *
115
+ * @returns @zh 编码后的二进制数据,如果没有变更返回 null @en Encoded binary data, or null if no changes
116
+ */
117
+ encodeDelta(): Uint8Array | null;
118
+ /**
119
+ * @zh 编码实体生成消息
120
+ * @en Encode entity spawn message
121
+ */
122
+ encodeSpawn(entity: Entity, prefabType?: string): Uint8Array;
123
+ /**
124
+ * @zh 编码实体销毁消息
125
+ * @en Encode entity despawn message
126
+ */
127
+ encodeDespawn(entityId: number): Uint8Array;
128
+ /**
129
+ * @zh 应用状态快照
130
+ * @en Apply state snapshot
131
+ *
132
+ * @param data - @zh 二进制数据 @en Binary data
133
+ * @returns @zh 解码结果 @en Decode result
134
+ */
135
+ applySnapshot(data: Uint8Array): DecodeSnapshotResult;
136
+ /**
137
+ * @zh 应用实体生成消息
138
+ * @en Apply entity spawn message
139
+ *
140
+ * @param data - @zh 二进制数据 @en Binary data
141
+ * @returns @zh 解码结果,如果不是 SPAWN 消息返回 null @en Decode result, or null if not a SPAWN message
142
+ */
143
+ applySpawn(data: Uint8Array): DecodeSpawnResult | null;
144
+ /**
145
+ * @zh 应用实体销毁消息
146
+ * @en Apply entity despawn message
147
+ *
148
+ * @param data - @zh 二进制数据 @en Binary data
149
+ * @returns @zh 销毁的实体 ID 列表 @en List of despawned entity IDs
150
+ */
151
+ applyDespawn(data: Uint8Array): number[];
152
+ /**
153
+ * @zh 通过网络 ID 获取实体
154
+ * @en Get entity by network ID
155
+ */
156
+ getEntityById(entityId: number): Entity | undefined;
157
+ /**
158
+ * @zh 获取所有匹配的实体
159
+ * @en Get all matching entities
160
+ */
161
+ getMatchingEntities(): Entity[];
162
+ protected process(entities: readonly Entity[]): void;
163
+ private _hasChanges;
164
+ private _clearChangeTrackers;
165
+ private _emitEvent;
166
+ protected onDestroy(): void;
167
+ }
168
+ /**
169
+ * @zh 创建组件同步系统
170
+ * @en Create component sync system
171
+ */
172
+ export declare function createComponentSyncSystem(config?: Partial<ComponentSyncConfig>, isServer?: boolean): ComponentSyncSystem;
173
+ //# sourceMappingURL=ComponentSync.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ComponentSync.d.ts","sourceRoot":"","sources":["../../src/sync/ComponentSync.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,EACH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IACI,iBAAiB,CAAC,CAAC,SAAS,UAAU,GAAG,EAAE,cAAc,EAAE,CAAC,GAAG,IAAI;IAW1E;;;;;;OAMG;IACI,iBAAiB,CAAC,QAAQ,GAAE,OAAe,GAAG,UAAU;IAc/D;;;;;OAKG;IACI,WAAW,IAAI,UAAU,GAAG,IAAI;IAcvC;;;OAGG;IACI,WAAW,CAAC,MAAM,EAAE,MAAM,EAAE,UAAU,CAAC,EAAE,MAAM,GAAG,UAAU;IAInE;;;OAGG;IACI,aAAa,CAAC,QAAQ,EAAE,MAAM,GAAG,UAAU;IAQlD;;;;;;OAMG;IACI,aAAa,CAAC,IAAI,EAAE,UAAU,GAAG,oBAAoB;IAyB5D;;;;;;OAMG;IACI,UAAU,CAAC,IAAI,EAAE,UAAU,GAAG,iBAAiB,GAAG,IAAI;IAkB7D;;;;;;OAMG;IACI,YAAY,CAAC,IAAI,EAAE,UAAU,GAAG,MAAM,EAAE;IAqB/C;;;OAGG;IACI,aAAa,CAAC,QAAQ,EAAE,MAAM,GAAG,MAAM,GAAG,SAAS;IAI1D;;;OAGG;IACI,mBAAmB,IAAI,MAAM,EAAE;cAQnB,OAAO,CAAC,QAAQ,EAAE,SAAS,MAAM,EAAE,GAAG,IAAI;IAoB7D,OAAO,CAAC,WAAW;IAUnB,OAAO,CAAC,oBAAoB;IAW5B,OAAO,CAAC,UAAU;cAUC,SAAS,IAAI,IAAI;CAIvC;AAED;;;GAGG;AACH,wBAAgB,yBAAyB,CACrC,MAAM,CAAC,EAAE,OAAO,CAAC,mBAAmB,CAAC,EACrC,QAAQ,GAAE,OAAe,GAC1B,mBAAmB,CAErB"}
@@ -14,4 +14,6 @@ export type { IInputSnapshot, IPredictedState, IPredictor, ClientPredictionConfi
14
14
  export { ClientPrediction, createClientPrediction } from './ClientPrediction';
15
15
  export type { EntityDeltaState, DeltaSyncData, DeltaCompressionConfig } from './StateDelta';
16
16
  export { DeltaFlags, StateDeltaCompressor, createStateDeltaCompressor } from './StateDelta';
17
+ export type { ComponentSyncEventType, ComponentSyncEvent, ComponentSyncEventListener, ComponentSyncConfig } from './ComponentSync';
18
+ export { ComponentSyncSystem, createComponentSyncSystem } from './ComponentSync';
17
19
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/sync/index.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAMH,YAAY,EACR,cAAc,EACd,eAAe,EACf,2BAA2B,EAC3B,qBAAqB,EACrB,eAAe,EAClB,MAAM,kBAAkB,CAAC;AAE1B,OAAO,EAAE,cAAc,EAAE,oBAAoB,EAAE,MAAM,kBAAkB,CAAC;AAMxE,YAAY,EAAE,aAAa,EAAE,aAAa,EAAE,MAAM,iBAAiB,CAAC;AACpE,OAAO,EAAE,IAAI,EAAE,SAAS,EAAE,UAAU,EAAE,MAAM,iBAAiB,CAAC;AAE9D,OAAO,EACH,qBAAqB,EACrB,4BAA4B,EAC5B,2BAA2B,EAC3B,kCAAkC,EACrC,MAAM,yBAAyB,CAAC;AAMjC,YAAY,EACR,cAAc,EACd,eAAe,EACf,UAAU,EACV,sBAAsB,EACzB,MAAM,oBAAoB,CAAC;AAE5B,OAAO,EAAE,gBAAgB,EAAE,sBAAsB,EAAE,MAAM,oBAAoB,CAAC;AAM9E,YAAY,EACR,gBAAgB,EAChB,aAAa,EACb,sBAAsB,EACzB,MAAM,cAAc,CAAC;AAEtB,OAAO,EACH,UAAU,EACV,oBAAoB,EACpB,0BAA0B,EAC7B,MAAM,cAAc,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/sync/index.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAMH,YAAY,EACR,cAAc,EACd,eAAe,EACf,2BAA2B,EAC3B,qBAAqB,EACrB,eAAe,EAClB,MAAM,kBAAkB,CAAC;AAE1B,OAAO,EAAE,cAAc,EAAE,oBAAoB,EAAE,MAAM,kBAAkB,CAAC;AAMxE,YAAY,EAAE,aAAa,EAAE,aAAa,EAAE,MAAM,iBAAiB,CAAC;AACpE,OAAO,EAAE,IAAI,EAAE,SAAS,EAAE,UAAU,EAAE,MAAM,iBAAiB,CAAC;AAE9D,OAAO,EACH,qBAAqB,EACrB,4BAA4B,EAC5B,2BAA2B,EAC3B,kCAAkC,EACrC,MAAM,yBAAyB,CAAC;AAMjC,YAAY,EACR,cAAc,EACd,eAAe,EACf,UAAU,EACV,sBAAsB,EACzB,MAAM,oBAAoB,CAAC;AAE5B,OAAO,EAAE,gBAAgB,EAAE,sBAAsB,EAAE,MAAM,oBAAoB,CAAC;AAM9E,YAAY,EACR,gBAAgB,EAChB,aAAa,EACb,sBAAsB,EACzB,MAAM,cAAc,CAAC;AAEtB,OAAO,EACH,UAAU,EACV,oBAAoB,EACpB,0BAA0B,EAC7B,MAAM,cAAc,CAAC;AAMtB,YAAY,EACR,sBAAsB,EACtB,kBAAkB,EAClB,0BAA0B,EAC1B,mBAAmB,EACtB,MAAM,iBAAiB,CAAC;AAEzB,OAAO,EACH,mBAAmB,EACnB,yBAAyB,EAC5B,MAAM,iBAAiB,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@esengine/network",
3
- "version": "2.2.0",
3
+ "version": "3.0.0",
4
4
  "description": "Network synchronization for multiplayer games",
5
5
  "esengine": {
6
6
  "plugin": true,
@@ -24,12 +24,21 @@
24
24
  "dependencies": {
25
25
  "@esengine/rpc": "1.1.1"
26
26
  },
27
+ "peerDependencies": {
28
+ "@esengine/ecs-framework": "2.5.0",
29
+ "@esengine/blueprint": "2.0.0"
30
+ },
31
+ "peerDependenciesMeta": {
32
+ "@esengine/blueprint": {
33
+ "optional": true
34
+ }
35
+ },
27
36
  "devDependencies": {
28
37
  "rimraf": "^5.0.5",
29
38
  "tsup": "^8.0.0",
30
39
  "typescript": "^5.3.3",
31
- "@esengine/blueprint": "1.0.2",
32
- "@esengine/ecs-framework": "2.4.4",
40
+ "@esengine/blueprint": "2.0.0",
41
+ "@esengine/ecs-framework": "2.5.0",
33
42
  "@esengine/build-config": "1.0.0"
34
43
  },
35
44
  "keywords": [