mirror of
https://github.com/demostf/demo.js
synced 2026-06-03 16:44:12 +02:00
add encoder for sayText2
This commit is contained in:
parent
2c8fe681d3
commit
cd5d0b0e86
4 changed files with 110 additions and 10 deletions
|
|
@ -109,7 +109,7 @@ export interface SayText2Packet extends BasePacket {
|
|||
packetType: 'sayText2';
|
||||
client: number;
|
||||
raw: number;
|
||||
kind: string;
|
||||
kind: 'TF_Chat_All' | 'TF_Chat_Team' | 'TF_Chat_AllDead';
|
||||
from: string;
|
||||
text: string;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
import {BitStream} from 'bit-buffer';
|
||||
import {UserMessagePacket} from '../../Data/Packet';
|
||||
import {SayText2} from '../UserMessage/SayText2';
|
||||
import {ParseSayText2} from '../UserMessage/SayText2';
|
||||
import {make} from './ParserGenerator';
|
||||
import {voidEncoder} from './Parser';
|
||||
|
||||
|
|
@ -66,7 +66,7 @@ enum UserMessageType {
|
|||
}
|
||||
|
||||
const userMessageParsers = {
|
||||
4: {parser: SayText2, voidEncoder},
|
||||
4: {parser: ParseSayText2, voidEncoder},
|
||||
5: make('textMsg', 'destType{8}text{s}'),
|
||||
};
|
||||
|
||||
|
|
|
|||
|
|
@ -1,15 +1,15 @@
|
|||
import {BitStream} from 'bit-buffer';
|
||||
import {SayText2Packet} from '../../Data/Packet';
|
||||
|
||||
export function SayText2(stream: BitStream): SayText2Packet { // 4: SayText2
|
||||
const client = stream.readBits(8);
|
||||
const raw = stream.readBits(8);
|
||||
export function ParseSayText2(stream: BitStream): SayText2Packet { // 4: ParseSayText2
|
||||
const client = stream.readUint8();
|
||||
const raw = stream.readUint8();
|
||||
const pos = stream.index;
|
||||
let from;
|
||||
let text;
|
||||
let kind;
|
||||
if (stream.readBits(8) === 1) {
|
||||
const first = stream.readBits(8);
|
||||
if (stream.readUint8() === 1) {
|
||||
const first = stream.readUint8();
|
||||
if (first === 7) {
|
||||
const color = stream.readUTF8String(6);
|
||||
} else {
|
||||
|
|
@ -29,8 +29,10 @@ export function SayText2(stream: BitStream): SayText2Packet { // 4: SayText2
|
|||
kind = stream.readUTF8String();
|
||||
from = stream.readUTF8String();
|
||||
text = stream.readUTF8String();
|
||||
stream.readASCIIString();
|
||||
stream.readASCIIString();
|
||||
// maybe always 2 null bytes?
|
||||
// stream.readASCIIString();
|
||||
// stream.readASCIIString();
|
||||
stream.readUint16();
|
||||
}
|
||||
// cleanup color codes
|
||||
text = text.replace(/\u0001/g, '');
|
||||
|
|
@ -40,6 +42,7 @@ export function SayText2(stream: BitStream): SayText2Packet { // 4: SayText2
|
|||
text = text.slice(0, stringPos) + text.slice(stringPos + 7);
|
||||
stringPos = text.indexOf('\u0007');
|
||||
}
|
||||
|
||||
return {
|
||||
packetType: 'sayText2',
|
||||
client,
|
||||
|
|
@ -49,3 +52,18 @@ export function SayText2(stream: BitStream): SayText2Packet { // 4: SayText2
|
|||
text,
|
||||
};
|
||||
}
|
||||
|
||||
export function EncodeSayText2(packet: SayText2Packet, stream: BitStream) {
|
||||
stream.writeUint8(packet.client);
|
||||
stream.writeUint8(packet.raw);
|
||||
|
||||
if (packet.kind === 'TF_Chat_AllDead') {
|
||||
const rawText = `*DEAD* \u0003${packet.from}\u0001: ${packet.text}`;
|
||||
stream.writeUTF8String(rawText);
|
||||
} else {
|
||||
stream.writeUTF8String(packet.kind);
|
||||
stream.writeUTF8String(packet.from);
|
||||
stream.writeUTF8String(packet.text);
|
||||
stream.writeUint16(0);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
82
src/tests/unit/Parser/UserMessage/SayText2Test.ts
Normal file
82
src/tests/unit/Parser/UserMessage/SayText2Test.ts
Normal file
|
|
@ -0,0 +1,82 @@
|
|||
import {BitStream} from 'bit-buffer';
|
||||
import {assertEncoder, assertParser, getStream} from '../Packet/PacketTest';
|
||||
import {EncodeSayText2, ParseSayText2} from '../../../../Parser/UserMessage/SayText2';
|
||||
|
||||
const data = [
|
||||
3,
|
||||
1,
|
||||
84,
|
||||
70,
|
||||
95,
|
||||
67,
|
||||
104,
|
||||
97,
|
||||
116,
|
||||
95,
|
||||
84,
|
||||
101,
|
||||
97,
|
||||
109,
|
||||
95,
|
||||
68,
|
||||
101,
|
||||
97,
|
||||
100,
|
||||
0,
|
||||
79,
|
||||
108,
|
||||
100,
|
||||
32,
|
||||
66,
|
||||
105,
|
||||
108,
|
||||
108,
|
||||
121,
|
||||
32,
|
||||
82,
|
||||
105,
|
||||
108,
|
||||
101,
|
||||
121,
|
||||
0,
|
||||
91,
|
||||
80,
|
||||
45,
|
||||
82,
|
||||
69,
|
||||
67,
|
||||
93,
|
||||
32,
|
||||
83,
|
||||
116,
|
||||
111,
|
||||
112,
|
||||
32,
|
||||
114,
|
||||
101,
|
||||
99,
|
||||
111,
|
||||
114,
|
||||
100,
|
||||
46,
|
||||
0,
|
||||
0,
|
||||
0];
|
||||
const expected = {
|
||||
packetType: 'sayText2',
|
||||
client: 3,
|
||||
raw: 1,
|
||||
kind: 'TF_Chat_Team_Dead',
|
||||
from: 'Old Billy Riley',
|
||||
text: '[P-REC] Stop record.'
|
||||
};
|
||||
|
||||
suite('SayText2', () => {
|
||||
test('Parse sayText2', () => {
|
||||
assertParser(ParseSayText2, getStream(data), expected, 472);
|
||||
});
|
||||
|
||||
test('Encode sayText2', () => {
|
||||
assertEncoder(ParseSayText2, EncodeSayText2, expected, 472);
|
||||
});
|
||||
});
|
||||
Loading…
Add table
Add a link
Reference in a new issue