Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
TheLazySquid
GitHub Repository: TheLazySquid/GimkitCheat
Path: blob/main/src/network/schemaDecode.ts
4172 views
1
import { Reflection, type Iterator, decode } from "@colyseus/schema";
2
import { Protocol } from "../../node_modules/colyseus.js/dist/colyseus.js";
3
import { utf8Read, utf8Length } from "../../node_modules/colyseus.js/lib/Protocol.js";
4
import * as msgpack from '../../node_modules/colyseus.js/lib/msgpack/index.js';
5
6
class SchemaSerializer extends EventTarget {
7
state: any;
8
9
setState(rawState: any) {
10
this.state.decode(rawState);
11
this.dispatchEvent(new CustomEvent('patch', { detail: null }))
12
this.dispatchEvent(new Event('load'))
13
}
14
15
getState() {
16
return this.state;
17
}
18
19
patch(patches) {
20
let res = this.state.decode(patches);
21
for (let change of res) {
22
if(change.field != 'gameTime') {
23
this.dispatchEvent(new CustomEvent('patch', { detail: res }));
24
break;
25
}
26
}
27
}
28
29
teardown() {
30
this.state?.['$changes']?.root.clearRefs();
31
}
32
33
handshake(bytes: number[], it?: Iterator) {
34
if (this.state) {
35
const reflection = new Reflection();
36
reflection.decode(bytes, it);
37
} else {
38
// initialize reflected state from server
39
this.state = Reflection.decode(bytes, it) as any;
40
}
41
this.dispatchEvent(new CustomEvent('patch', { detail: null }));
42
}
43
}
44
45
export const serializer = new SchemaSerializer();
46
47
export function onMessage(event: MessageEvent) {
48
const bytes = Array.from(new Uint8Array(event.data));
49
const code = bytes[0];
50
51
if (code === Protocol.JOIN_ROOM) {
52
let offset = 1;
53
54
const reconnectionToken = utf8Read(bytes, offset);
55
offset += utf8Length(reconnectionToken);
56
57
const serializerId = utf8Read(bytes, offset);
58
offset += utf8Length(serializerId);
59
60
console.log(reconnectionToken, serializerId, offset)
61
62
if (bytes.length > offset) {
63
serializer.handshake(bytes, { offset });
64
}
65
} else if (code === Protocol.ROOM_DATA_SCHEMA) {
66
// don't think this matters
67
// const it = { offset: 1 };
68
69
// const context = (this.serializer.getState() as any).constructor._context;
70
// const type = context.get(decode.number(bytes, it));
71
72
// const message = new (type as any)();
73
// message.decode(bytes, it);
74
75
// this.dispatchMessage(type, message);
76
} else if (code === Protocol.ROOM_STATE) {
77
bytes.shift(); // drop `code` byte
78
serializer.setState(bytes);
79
} else if (code === Protocol.ROOM_STATE_PATCH) {
80
bytes.shift(); // drop `code` byte
81
serializer.patch(bytes);
82
} else if (code === Protocol.ROOM_DATA) {
83
const it: decode.Iterator = { offset: 1 };
84
85
const type = (decode.stringCheck(bytes, it))
86
? decode.string(bytes, it)
87
: decode.number(bytes, it);
88
89
const message = (bytes.length > it.offset)
90
? msgpack.decode(event.data, it.offset)
91
: undefined;
92
93
return { type, message }
94
} else if (code === Protocol.ROOM_DATA_BYTES) {
95
const it: decode.Iterator = { offset: 1 };
96
97
const type = (decode.stringCheck(bytes, it))
98
? decode.string(bytes, it)
99
: decode.number(bytes, it);
100
101
return { type, message: new Uint8Array(bytes.slice(it.offset)) }
102
}
103
104
return null;
105
}
106