-
Notifications
You must be signed in to change notification settings - Fork 0
/
local.ts
164 lines (139 loc) · 4.09 KB
/
local.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
import chalk from 'chalk';
import { PublicEventHandler } from '../../util/events.js';
import { type Packet } from '../../util/packet.js';
import { type AsyncVoid } from '../../util/types.js';
import { createChannel } from '../../worker/parent.js';
import proxy from '../internal.proxy/local.js';
import { PACKET_PREDICTION } from './settings.js';
import {
type GlobalMessage,
type LocalMessage,
type ServerMessage,
type ServerPlayerMessage,
type ServerSystemMessage,
} from './shared.js';
type i64 = [number, number];
interface ClientChatMessage {
message: string;
timestamp: i64;
salt: i64;
signature: Uint8Array | undefined;
offset: number;
acknowledged: Uint8Array;
}
interface ClientChatCommand {
command: string;
timestamp: i64;
salt: i64;
argumentSignatures: Uint8Array[];
messageCount: number;
acknowledged: Uint8Array;
}
interface UpstreamEventMap {
message: (packet: Packet<ClientChatMessage>) => AsyncVoid;
command: (packet: Packet<ClientChatCommand>) => AsyncVoid;
}
// TODO: Make server messages cancelable
interface DownstreamEventMap {
player: (
message: ServerPlayerMessage,
possiblePacket?: Packet<ServerPlayerMessage>,
) => AsyncVoid;
system: (
message: ServerSystemMessage,
possiblePacket?: Packet<ServerSystemMessage>,
) => AsyncVoid;
}
// ? Should I export the channel
export const channel = createChannel<LocalMessage, GlobalMessage>('chat');
export const chat = {
upstream: new PublicEventHandler<UpstreamEventMap>(),
downstream: new PublicEventHandler<DownstreamEventMap>(),
toClient(message: unknown) {
proxy.writeDownstream('system_chat', {
content: JSON.stringify(message),
isActionBar: false,
});
},
toClientActionBar(message: unknown) {
proxy.writeDownstream('system_chat', {
content: JSON.stringify(message),
isActionBar: true,
});
},
toServer(message: string) {
channel.write(message);
},
} as const;
proxy.upstream.on('chat_message', async (packet) => {
await chat.upstream.emit('message', packet as Packet<ClientChatMessage>);
});
proxy.upstream.on('chat_command', async (packet) => {
await chat.upstream.emit('command', packet as Packet<ClientChatCommand>);
});
interface PredictedPacket<T extends ServerMessage = ServerMessage> {
packet: Packet<T>;
callback: () => void;
}
let predictedPacket: PredictedPacket | undefined;
if (PACKET_PREDICTION.enabled) {
for (const chat of ['player', 'system'] as const) {
const event = `${chat}_chat` as const;
proxy.downstream.on(event, async (packet) => {
if (predictedPacket !== undefined) {
predictedPacket.callback();
}
await new Promise<void>((resolve) => {
predictedPacket = {
callback: resolve,
packet: packet as Packet<ServerMessage>,
};
});
});
}
}
channel.subscribe((message) => {
const type = message.type;
void (async () => {
let packet = predictedPacket?.packet;
const callback = predictedPacket?.callback;
if (packet !== undefined && packet.name !== `${type}_chat`) {
const message = `Expected ${type}_chat packet, got ${JSON.stringify(packet.name)} packet`;
switch (PACKET_PREDICTION.onIncorrectPacket.log) {
case 'none':
break;
case 'warn':
console.warn(chalk.yellow(message));
break;
case 'error':
throw new Error(message);
}
if (!PACKET_PREDICTION.onIncorrectPacket.emit) {
console.info("Since packet was incorrect, won't be emitted");
packet = undefined;
}
}
switch (type) {
case 'player':
await chat.downstream.emit(
'player',
message.message,
packet as Packet<ServerPlayerMessage> | undefined,
);
break;
case 'system':
await chat.downstream.emit(
'system',
message.message,
packet as Packet<ServerSystemMessage> | undefined,
);
break;
default:
throw new Error(`Invalid type: ${type as any}`);
}
if (callback !== undefined) {
callback();
}
})();
});
export default chat;