@hytopia.com/server-protocol 1.0.1 → 1.0.3
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/README.md +3 -0
- package/index.ts +7 -0
- package/package.json +1 -1
- package/packets/{PacketTypes.ts → PacketCore.ts} +24 -15
- package/packets/inbound/Heartbeat.ts +4 -4
- package/packets/inbound/Input.ts +4 -4
- package/packets/inbound/index.ts +2 -0
- package/packets/outbound/Block.ts +4 -4
- package/packets/outbound/BlockRegistry.ts +4 -4
- package/packets/outbound/BlockType.ts +4 -4
- package/packets/outbound/Chunk.ts +4 -4
- package/packets/outbound/Entity.ts +4 -4
- package/packets/outbound/PhysicsDebugRender.ts +4 -4
- package/packets/outbound/index.ts +6 -0
- package/schemas/index.ts +16 -0
- package/packets/index.ts +0 -26
package/README.md
ADDED
package/index.ts
ADDED
package/package.json
CHANGED
|
@@ -17,7 +17,7 @@ import type { JSONSchemaType, ValidateFunction } from 'ajv';
|
|
|
17
17
|
* in encoded format.
|
|
18
18
|
*/
|
|
19
19
|
|
|
20
|
-
export enum
|
|
20
|
+
export enum PacketIds {
|
|
21
21
|
// Standard Inbound Packet Types: 0 - 31 range
|
|
22
22
|
HEARTBEAT = 0,
|
|
23
23
|
INPUT = 1,
|
|
@@ -40,28 +40,37 @@ export enum PacketType {
|
|
|
40
40
|
* Generators for packet definitions and validators.
|
|
41
41
|
*/
|
|
42
42
|
|
|
43
|
-
export interface IPacketDefinition<TId extends
|
|
43
|
+
export interface IPacketDefinition<TId extends PacketIds, TSchema> {
|
|
44
44
|
id: TId;
|
|
45
|
-
schema: JSONSchemaType<
|
|
46
|
-
validate: ValidateFunction<
|
|
45
|
+
schema: JSONSchemaType<TSchema>;
|
|
46
|
+
validate: ValidateFunction<TSchema>;
|
|
47
47
|
}
|
|
48
48
|
|
|
49
|
-
export interface IPacket<TId extends
|
|
49
|
+
export interface IPacket<TId extends PacketIds, TSchema> {
|
|
50
50
|
id: TId;
|
|
51
|
-
payload:
|
|
51
|
+
payload: TSchema;
|
|
52
52
|
}
|
|
53
53
|
|
|
54
|
-
export
|
|
54
|
+
export type AnyPacket = IPacket<PacketIds, unknown>;
|
|
55
|
+
|
|
56
|
+
export type AnyPacketDefinition = IPacketDefinition<number, unknown>;
|
|
57
|
+
|
|
58
|
+
export const createPacket = <TId extends number, TSchema>(
|
|
59
|
+
packetDef: IPacketDefinition<TId, TSchema>,
|
|
60
|
+
payload: TSchema,
|
|
61
|
+
): IPacket<TId, TSchema> => {
|
|
62
|
+
if (!packetDef.validate(payload)) {
|
|
63
|
+
throw new Error(`createPacket(): Invalid payload for packet with id ${packetDef.id}, error: ${Ajv.instance.errorsText(packetDef.validate.errors)}`);
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
return { id: packetDef.id, payload };
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
export const definePacket = <TId extends PacketIds, TSchema>(
|
|
55
70
|
id: TId,
|
|
56
|
-
schema: JSONSchemaType<
|
|
57
|
-
): IPacketDefinition<TId,
|
|
71
|
+
schema: JSONSchemaType<TSchema>,
|
|
72
|
+
): IPacketDefinition<TId, TSchema> => ({
|
|
58
73
|
id,
|
|
59
74
|
schema,
|
|
60
75
|
validate: Ajv.instance.compile(schema),
|
|
61
76
|
});
|
|
62
|
-
|
|
63
|
-
// Represents any packet type
|
|
64
|
-
export type AnyPacket = IPacket<PacketType, unknown>;
|
|
65
|
-
|
|
66
|
-
// Represents any packet definition type
|
|
67
|
-
export type AnyPacketDefinition = IPacketDefinition<number, unknown>;
|
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
import { definePacket,
|
|
2
|
-
import type { IPacket } from '../
|
|
1
|
+
import { definePacket, PacketIds } from '../PacketCore';
|
|
2
|
+
import type { IPacket } from '../PacketCore';
|
|
3
3
|
import { heartbeatSchema } from '../../schemas/Heartbeat';
|
|
4
4
|
import type { Heartbeat } from '../../schemas/Heartbeat';
|
|
5
5
|
|
|
6
|
-
export type HeartbeatPacket = IPacket<typeof
|
|
6
|
+
export type HeartbeatPacket = IPacket<typeof PacketIds.HEARTBEAT, Heartbeat>;
|
|
7
7
|
|
|
8
8
|
export const heartbeatPacketDefinition = definePacket(
|
|
9
|
-
|
|
9
|
+
PacketIds.HEARTBEAT,
|
|
10
10
|
heartbeatSchema
|
|
11
11
|
);
|
package/packets/inbound/Input.ts
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
import { definePacket,
|
|
2
|
-
import type { IPacket } from '../
|
|
1
|
+
import { definePacket, PacketIds } from '../PacketCore';
|
|
2
|
+
import type { IPacket } from '../PacketCore';
|
|
3
3
|
import { inputSchema } from '../../schemas/Input';
|
|
4
4
|
import type { Input } from '../../schemas/Input';
|
|
5
5
|
|
|
6
|
-
export type InputPacket = IPacket<typeof
|
|
6
|
+
export type InputPacket = IPacket<typeof PacketIds.INPUT, Input>;
|
|
7
7
|
|
|
8
8
|
export const inputPacketDefinition = definePacket(
|
|
9
|
-
|
|
9
|
+
PacketIds.INPUT,
|
|
10
10
|
inputSchema
|
|
11
11
|
);
|
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
import { definePacket,
|
|
2
|
-
import type { IPacket } from '../
|
|
1
|
+
import { definePacket, PacketIds } from '../PacketCore';
|
|
2
|
+
import type { IPacket } from '../PacketCore';
|
|
3
3
|
import { blockSchema } from '../../schemas/Block';
|
|
4
4
|
import type { Block } from '../../schemas/Block';
|
|
5
5
|
|
|
6
|
-
export type BlockPacket = IPacket<typeof
|
|
6
|
+
export type BlockPacket = IPacket<typeof PacketIds.BLOCK, Block>;
|
|
7
7
|
|
|
8
8
|
export const blockPacketDefinition = definePacket(
|
|
9
|
-
|
|
9
|
+
PacketIds.BLOCK,
|
|
10
10
|
blockSchema,
|
|
11
11
|
);
|
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
import { definePacket,
|
|
2
|
-
import type { IPacket } from '../
|
|
1
|
+
import { definePacket, PacketIds } from '../PacketCore';
|
|
2
|
+
import type { IPacket } from '../PacketCore';
|
|
3
3
|
import { blockRegistrySchema } from '../../schemas/BlockRegistry';
|
|
4
4
|
import type { BlockRegistry } from '../../schemas/BlockRegistry';
|
|
5
5
|
|
|
6
|
-
export type BlockRegistryPacket = IPacket<typeof
|
|
6
|
+
export type BlockRegistryPacket = IPacket<typeof PacketIds.BLOCK_REGISTRY, BlockRegistry>;
|
|
7
7
|
|
|
8
8
|
export const blockRegistryPacketDefinition = definePacket(
|
|
9
|
-
|
|
9
|
+
PacketIds.BLOCK_REGISTRY,
|
|
10
10
|
blockRegistrySchema,
|
|
11
11
|
);
|
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
import { definePacket,
|
|
2
|
-
import type { IPacket } from '../
|
|
1
|
+
import { definePacket, PacketIds } from '../PacketCore';
|
|
2
|
+
import type { IPacket } from '../PacketCore';
|
|
3
3
|
import { blockTypeSchema } from '../../schemas/BlockType';
|
|
4
4
|
import type { BlockType } from '../../schemas/BlockType';
|
|
5
5
|
|
|
6
|
-
export type BlockTypePacket = IPacket<typeof
|
|
6
|
+
export type BlockTypePacket = IPacket<typeof PacketIds.BLOCK_TYPE, BlockType>;
|
|
7
7
|
|
|
8
8
|
export const blockTypePacketDefinition = definePacket(
|
|
9
|
-
|
|
9
|
+
PacketIds.BLOCK_TYPE,
|
|
10
10
|
blockTypeSchema,
|
|
11
11
|
);
|
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
import { definePacket,
|
|
2
|
-
import type { IPacket } from '../
|
|
1
|
+
import { definePacket, PacketIds } from '../PacketCore';
|
|
2
|
+
import type { IPacket } from '../PacketCore';
|
|
3
3
|
import { chunkSchema } from '../../schemas/Chunk';
|
|
4
4
|
import type { Chunk } from '../../schemas/Chunk';
|
|
5
5
|
|
|
6
|
-
export type ChunkPacket = IPacket<typeof
|
|
6
|
+
export type ChunkPacket = IPacket<typeof PacketIds.CHUNK, Chunk>;
|
|
7
7
|
|
|
8
8
|
export const chunkPacketDefinition = definePacket(
|
|
9
|
-
|
|
9
|
+
PacketIds.CHUNK,
|
|
10
10
|
chunkSchema,
|
|
11
11
|
);
|
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
import { definePacket,
|
|
2
|
-
import type { IPacket } from '../
|
|
1
|
+
import { definePacket, PacketIds } from '../PacketCore';
|
|
2
|
+
import type { IPacket } from '../PacketCore';
|
|
3
3
|
import { entitySchema } from '../../schemas/Entity';
|
|
4
4
|
import type { Entity } from '../../schemas/Entity';
|
|
5
5
|
|
|
6
|
-
export type EntityPacket = IPacket<typeof
|
|
6
|
+
export type EntityPacket = IPacket<typeof PacketIds.ENTITY, Entity>;
|
|
7
7
|
|
|
8
8
|
export const entityPacketDefinition = definePacket(
|
|
9
|
-
|
|
9
|
+
PacketIds.ENTITY,
|
|
10
10
|
entitySchema,
|
|
11
11
|
);
|
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
import { definePacket,
|
|
2
|
-
import type { IPacket } from '../
|
|
1
|
+
import { definePacket, PacketIds } from '../PacketCore';
|
|
2
|
+
import type { IPacket } from '../PacketCore';
|
|
3
3
|
import { physicsDebugRenderSchema } from '../../schemas/PhysicsDebugRender';
|
|
4
4
|
import type { PhysicsDebugRender } from '../../schemas/PhysicsDebugRender';
|
|
5
5
|
|
|
6
|
-
export type PhysicsDebugRenderPacket = IPacket<typeof
|
|
6
|
+
export type PhysicsDebugRenderPacket = IPacket<typeof PacketIds.PHYSICS_DEBUG_RENDER, PhysicsDebugRender>;
|
|
7
7
|
|
|
8
8
|
export const physicsDebugRenderPacketDefinition = definePacket(
|
|
9
|
-
|
|
9
|
+
PacketIds.PHYSICS_DEBUG_RENDER,
|
|
10
10
|
physicsDebugRenderSchema,
|
|
11
11
|
);
|
package/schemas/index.ts
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
export * from './Block';
|
|
2
|
+
export * from './BlockRegistry';
|
|
3
|
+
export * from './BlockType';
|
|
4
|
+
export * from './Chunk';
|
|
5
|
+
export * from './Collider';
|
|
6
|
+
export * from './ColliderDesc';
|
|
7
|
+
export * from './Entity';
|
|
8
|
+
export * from './Heartbeat';
|
|
9
|
+
export * from './Input';
|
|
10
|
+
export * from './PhysicsDebugRender';
|
|
11
|
+
export * from './Quaternion';
|
|
12
|
+
export * from './RigidBody';
|
|
13
|
+
export * from './RigidBodyDesc';
|
|
14
|
+
export * from './Shape';
|
|
15
|
+
export * from './Vector';
|
|
16
|
+
export * from './VectorBoolean';
|
package/packets/index.ts
DELETED
|
@@ -1,26 +0,0 @@
|
|
|
1
|
-
import fs from 'fs';
|
|
2
|
-
import path from 'path';
|
|
3
|
-
|
|
4
|
-
const inboundPackets: Record<string, any> = {};
|
|
5
|
-
const outboundPackets: Record<string, any> = {};
|
|
6
|
-
|
|
7
|
-
const importPackets = async () => {
|
|
8
|
-
for (const directory of ['inbound', 'outbound']) {
|
|
9
|
-
const directoryPath = path.join(__dirname, directory);
|
|
10
|
-
const files = fs.readdirSync(directoryPath);
|
|
11
|
-
|
|
12
|
-
for (const file of files) {
|
|
13
|
-
if (file.endsWith('.ts')) {
|
|
14
|
-
const name = path.basename(file, '.ts');
|
|
15
|
-
const module = await import(`./${directory}/${name}`);
|
|
16
|
-
const typePackets = directory === 'inbound' ? inboundPackets : outboundPackets;
|
|
17
|
-
const lcfName = name.charAt(0).toLowerCase() + name.slice(1);
|
|
18
|
-
typePackets[`${lcfName}PacketDefinition`] = module[`${lcfName}PacketDefinition`];
|
|
19
|
-
}
|
|
20
|
-
}
|
|
21
|
-
}
|
|
22
|
-
};
|
|
23
|
-
|
|
24
|
-
await importPackets();
|
|
25
|
-
|
|
26
|
-
export default { inboundPackets, outboundPackets };
|