@hytopia.com/server-protocol 1.0.27 → 1.0.29
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/package.json +1 -1
- package/packets/PacketCore.ts +15 -9
- package/packets/PacketDefinitions.ts +4 -3
- package/packets/outbound/Block.ts +2 -2
- package/packets/outbound/BlockType.ts +2 -2
- package/packets/outbound/BlockTypeRegistry.ts +3 -2
- package/packets/outbound/Chunk.ts +2 -2
- package/packets/outbound/Entity.ts +2 -2
- package/packets/outbound/PhysicsDebugRender.ts +2 -2
- package/packets/outbound/World.ts +2 -2
package/package.json
CHANGED
package/packets/PacketCore.ts
CHANGED
|
@@ -49,10 +49,11 @@ export interface IPacketDefinition<TId extends PacketId, TSchema> {
|
|
|
49
49
|
validate: ValidateFunction<TSchema>;
|
|
50
50
|
}
|
|
51
51
|
|
|
52
|
-
export type
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
]
|
|
52
|
+
export type WorldTick = number;
|
|
53
|
+
|
|
54
|
+
export type IPacket<TId extends PacketId, TSchema, TRequireWorldTick extends boolean = false> = TRequireWorldTick extends true
|
|
55
|
+
? [TId, TSchema, WorldTick]
|
|
56
|
+
: [TId, TSchema];
|
|
56
57
|
|
|
57
58
|
export type AnyPacket = IPacket<PacketId, unknown>;
|
|
58
59
|
|
|
@@ -64,18 +65,23 @@ export interface Serializable {
|
|
|
64
65
|
serialize(): AnySchema;
|
|
65
66
|
}
|
|
66
67
|
|
|
67
|
-
export function createPacket<TId extends
|
|
68
|
+
export function createPacket<TId extends PacketId, TSchema, TRequireWorldTick extends boolean = false>(
|
|
68
69
|
packetDef: IPacketDefinition<TId, TSchema>,
|
|
69
70
|
data: TSchema,
|
|
70
|
-
|
|
71
|
+
worldTick?: TRequireWorldTick extends true ? WorldTick : WorldTick | undefined
|
|
72
|
+
): IPacket<TId, TSchema, TRequireWorldTick> {
|
|
71
73
|
if (!packetDef.validate(data)) {
|
|
72
|
-
throw new Error(`
|
|
74
|
+
throw new Error(`Invalid payload for packet with id ${packetDef.id}. Error: ${Ajv.instance.errorsText(packetDef.validate.errors)}`);
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
if (worldTick === undefined) {
|
|
78
|
+
return [packetDef.id, data] as IPacket<TId, TSchema, TRequireWorldTick>;
|
|
73
79
|
}
|
|
74
80
|
|
|
75
|
-
return [
|
|
81
|
+
return [packetDef.id, data, worldTick] as IPacket<TId, TSchema, TRequireWorldTick>;
|
|
76
82
|
}
|
|
77
83
|
|
|
78
|
-
export function definePacket<TId extends PacketId, TSchema>(
|
|
84
|
+
export function definePacket<TId extends PacketId, TSchema, TRequireWorldTick extends boolean = false>(
|
|
79
85
|
id: TId,
|
|
80
86
|
schema: JSONSchemaType<TSchema>,
|
|
81
87
|
): IPacketDefinition<TId, TSchema> {
|
|
@@ -22,10 +22,11 @@ for (const packet of Object.values(allPackets)) {
|
|
|
22
22
|
|
|
23
23
|
export function isValidPacket(packet: unknown): packet is AnyPacket {
|
|
24
24
|
if (
|
|
25
|
-
|
|
26
|
-
packet
|
|
25
|
+
!Array.isArray(packet) ||
|
|
26
|
+
packet.length < 2 || packet.length > 3 ||
|
|
27
27
|
typeof packet[0] !== 'number' || packet[0] < 0 ||
|
|
28
|
-
packet[1] === undefined
|
|
28
|
+
packet[1] === undefined ||
|
|
29
|
+
(packet.length === 3 && typeof packet[2] !== 'number')
|
|
29
30
|
) {
|
|
30
31
|
return false;
|
|
31
32
|
}
|
|
@@ -3,9 +3,9 @@ import type { IPacket } from '../PacketCore';
|
|
|
3
3
|
import { blockSchema } from '../../schemas/Block';
|
|
4
4
|
import type { BlockSchema } from '../../schemas/Block';
|
|
5
5
|
|
|
6
|
-
export type BlockPacket = IPacket<typeof PacketId.BLOCK, BlockSchema>;
|
|
6
|
+
export type BlockPacket = IPacket<typeof PacketId.BLOCK, BlockSchema, true>;
|
|
7
7
|
|
|
8
|
-
export const blockPacketDefinition = definePacket(
|
|
8
|
+
export const blockPacketDefinition = definePacket<typeof PacketId.BLOCK, BlockSchema, true>(
|
|
9
9
|
PacketId.BLOCK,
|
|
10
10
|
blockSchema,
|
|
11
11
|
);
|
|
@@ -3,9 +3,9 @@ import type { IPacket } from '../PacketCore';
|
|
|
3
3
|
import { blockTypeSchema } from '../../schemas/BlockType';
|
|
4
4
|
import type { BlockTypeSchema } from '../../schemas/BlockType';
|
|
5
5
|
|
|
6
|
-
export type BlockTypePacket = IPacket<typeof PacketId.BLOCK_TYPE, BlockTypeSchema>;
|
|
6
|
+
export type BlockTypePacket = IPacket<typeof PacketId.BLOCK_TYPE, BlockTypeSchema, true>;
|
|
7
7
|
|
|
8
|
-
export const blockTypePacketDefinition = definePacket(
|
|
8
|
+
export const blockTypePacketDefinition = definePacket<typeof PacketId.BLOCK_TYPE, BlockTypeSchema, true>(
|
|
9
9
|
PacketId.BLOCK_TYPE,
|
|
10
10
|
blockTypeSchema,
|
|
11
11
|
);
|
|
@@ -2,10 +2,11 @@ import { definePacket, PacketId } from '../PacketCore';
|
|
|
2
2
|
import type { IPacket } from '../PacketCore';
|
|
3
3
|
import { blockTypeRegistrySchema } from '../../schemas/BlockTypeRegistry';
|
|
4
4
|
import type { BlockTypeRegistrySchema } from '../../schemas/BlockTypeRegistry';
|
|
5
|
+
import type { WorldTick } from '../PacketCore';
|
|
5
6
|
|
|
6
|
-
export type BlockTypeRegistryPacket = IPacket<typeof PacketId.BLOCK_TYPE_REGISTRY, BlockTypeRegistrySchema>;
|
|
7
|
+
export type BlockTypeRegistryPacket = IPacket<typeof PacketId.BLOCK_TYPE_REGISTRY, BlockTypeRegistrySchema, true>;
|
|
7
8
|
|
|
8
|
-
export const blockTypeRegistryPacketDefinition = definePacket(
|
|
9
|
+
export const blockTypeRegistryPacketDefinition = definePacket<typeof PacketId.BLOCK_TYPE_REGISTRY, BlockTypeRegistrySchema, true>(
|
|
9
10
|
PacketId.BLOCK_TYPE_REGISTRY,
|
|
10
11
|
blockTypeRegistrySchema,
|
|
11
12
|
);
|
|
@@ -3,9 +3,9 @@ import type { IPacket } from '../PacketCore';
|
|
|
3
3
|
import { chunkSchema } from '../../schemas/Chunk';
|
|
4
4
|
import type { ChunkSchema } from '../../schemas/Chunk';
|
|
5
5
|
|
|
6
|
-
export type ChunkPacket = IPacket<typeof PacketId.CHUNK, ChunkSchema>;
|
|
6
|
+
export type ChunkPacket = IPacket<typeof PacketId.CHUNK, ChunkSchema, true>;
|
|
7
7
|
|
|
8
|
-
export const chunkPacketDefinition = definePacket(
|
|
8
|
+
export const chunkPacketDefinition = definePacket<typeof PacketId.CHUNK, ChunkSchema, true>(
|
|
9
9
|
PacketId.CHUNK,
|
|
10
10
|
chunkSchema,
|
|
11
11
|
);
|
|
@@ -3,9 +3,9 @@ import type { IPacket } from '../PacketCore';
|
|
|
3
3
|
import { entitySchema } from '../../schemas/Entity';
|
|
4
4
|
import type { EntitySchema } from '../../schemas/Entity';
|
|
5
5
|
|
|
6
|
-
export type EntityPacket = IPacket<typeof PacketId.ENTITY, EntitySchema>;
|
|
6
|
+
export type EntityPacket = IPacket<typeof PacketId.ENTITY, EntitySchema, true>;
|
|
7
7
|
|
|
8
|
-
export const entityPacketDefinition = definePacket(
|
|
8
|
+
export const entityPacketDefinition = definePacket<typeof PacketId.ENTITY, EntitySchema, true>(
|
|
9
9
|
PacketId.ENTITY,
|
|
10
10
|
entitySchema,
|
|
11
11
|
);
|
|
@@ -3,9 +3,9 @@ import type { IPacket } from '../PacketCore';
|
|
|
3
3
|
import { physicsDebugRenderSchema } from '../../schemas/PhysicsDebugRender';
|
|
4
4
|
import type { PhysicsDebugRenderSchema } from '../../schemas/PhysicsDebugRender';
|
|
5
5
|
|
|
6
|
-
export type PhysicsDebugRenderPacket = IPacket<typeof PacketId.PHYSICS_DEBUG_RENDER, PhysicsDebugRenderSchema>;
|
|
6
|
+
export type PhysicsDebugRenderPacket = IPacket<typeof PacketId.PHYSICS_DEBUG_RENDER, PhysicsDebugRenderSchema, true>;
|
|
7
7
|
|
|
8
|
-
export const physicsDebugRenderPacketDefinition = definePacket(
|
|
8
|
+
export const physicsDebugRenderPacketDefinition = definePacket<typeof PacketId.PHYSICS_DEBUG_RENDER, PhysicsDebugRenderSchema, true>(
|
|
9
9
|
PacketId.PHYSICS_DEBUG_RENDER,
|
|
10
10
|
physicsDebugRenderSchema,
|
|
11
11
|
);
|
|
@@ -3,9 +3,9 @@ import type { IPacket } from '../PacketCore';
|
|
|
3
3
|
import { worldSchema } from '../../schemas/World';
|
|
4
4
|
import type { WorldSchema } from '../../schemas/World';
|
|
5
5
|
|
|
6
|
-
export type WorldPacket = IPacket<typeof PacketId.WORLD, WorldSchema>;
|
|
6
|
+
export type WorldPacket = IPacket<typeof PacketId.WORLD, WorldSchema, true>;
|
|
7
7
|
|
|
8
|
-
export const worldPacketDefinition = definePacket(
|
|
8
|
+
export const worldPacketDefinition = definePacket<typeof PacketId.WORLD, WorldSchema, true>(
|
|
9
9
|
PacketId.WORLD,
|
|
10
10
|
worldSchema,
|
|
11
11
|
);
|