1
0
Fork 0
mirror of https://github.com/demostf/demo.js synced 2026-06-03 16:44:12 +02:00

Add tests for header parsing

This commit is contained in:
Robin Appelman 2017-11-21 17:58:00 +01:00
commit 3ab89caa74
4 changed files with 439 additions and 28 deletions

View file

@ -3,6 +3,7 @@ import {Header} from './Data/Header';
import {Message, MessageType} from './Data/Message';
import {ParserState} from './Data/ParserState';
import {messageHandlers} from './Parser';
import {encodeHeader} from './Parser/Header';
export class Encoder {
public readonly stream: BitStream;
@ -14,17 +15,7 @@ export class Encoder {
}
public encodeHeader(header: Header) {
this.stream.writeASCIIString(header.type, 8);
this.stream.writeUint32(header.version);
this.stream.writeUint32(header.protocol);
this.stream.writeASCIIString(header.server, 260);
this.stream.writeASCIIString(header.nick, 260);
this.stream.writeASCIIString(header.map, 260);
this.stream.writeASCIIString(header.game, 260);
this.stream.writeFloat32(header.duration);
this.stream.writeUint32(header.ticks);
this.stream.writeUint32(header.frames);
this.stream.writeUint32(header.sigon);
encodeHeader(header, this.stream);
}
public writeMessage(message: Message) {

View file

@ -10,6 +10,7 @@ import {StopHandler} from './Parser/Message/Stop';
import {StringTableHandler} from './Parser/Message/StringTable';
import {SyncTickHandler} from './Parser/Message/SyncTick';
import {UserCmdHandler} from './Parser/Message/UserCmd';
import {parseHeader} from './Parser/Header';
export const messageHandlers: Map<MessageType, MessageHandler<Message>> = new Map<MessageType, MessageHandler<Message>>([
[MessageType.Sigon, PacketMessageHandler],
@ -35,7 +36,7 @@ export class Parser {
public getHeader() {
if (!this.header) {
this.header = this.parseHeader(this.stream);
this.header = parseHeader(this.stream);
}
return this.header;
}
@ -70,22 +71,6 @@ export class Parser {
}
}
protected parseHeader(stream): Header {
return {
type: stream.readASCIIString(8),
version: stream.readInt32(),
protocol: stream.readInt32(),
server: stream.readASCIIString(260),
nick: stream.readASCIIString(260),
map: stream.readASCIIString(260),
game: stream.readASCIIString(260),
duration: stream.readFloat32(),
ticks: stream.readInt32(),
frames: stream.readInt32(),
sigon: stream.readInt32()
};
}
protected * handleMessage(message: Message): Iterable<Packet> {
this.parserState.handleMessage(message);
if (message.type === MessageType.Packet) {

32
src/Parser/Header.ts Normal file
View file

@ -0,0 +1,32 @@
import {Header} from '../Data/Header';
import {BitStream} from 'bit-buffer';
export function parseHeader(stream: BitStream): Header {
return {
type: stream.readASCIIString(8),
version: stream.readInt32(),
protocol: stream.readInt32(),
server: stream.readASCIIString(260),
nick: stream.readASCIIString(260),
map: stream.readASCIIString(260),
game: stream.readASCIIString(260),
duration: stream.readFloat32(),
ticks: stream.readInt32(),
frames: stream.readInt32(),
sigon: stream.readInt32()
};
}
export function encodeHeader(header: Header, stream: BitStream) {
stream.writeASCIIString(header.type, 8);
stream.writeUint32(header.version);
stream.writeUint32(header.protocol);
stream.writeASCIIString(header.server, 260);
stream.writeASCIIString(header.nick, 260);
stream.writeASCIIString(header.map, 260);
stream.writeASCIIString(header.game, 260);
stream.writeFloat32(header.duration);
stream.writeUint32(header.ticks);
stream.writeUint32(header.frames);
stream.writeUint32(header.sigon);
}

View file

@ -0,0 +1,403 @@
import {BitStream} from 'bit-buffer';
import {assertEncoder, assertParser, assertReEncode, getStream} from './Packet/PacketTest';
import {encodeHeader, parseHeader} from '../../../Parser/Header';
const data = [
72, 76, 50, 68,
69, 77, 79, 0,
3, 0, 0, 0,
24, 0, 0, 0,
70, 97, 107, 107,
101, 108, 66, 114,
105, 103, 97, 100,
101, 32, 35, 49,
32, 40, 35, 55,
49, 50, 49, 51,
51, 41, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0,
0, 0,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 32, 104,
101, 116, 32, 104,
101, 131, 180, 1,
0, 8, 6, 128,
0, 0, 0, 192,
255, 255, 255, 255,
92, 1, 38, 49,
4, 141, 223, 55,
168, 139, 18, 159,
223, 11, 239, 120,
214, 111, 0, 33,
143, 194, 117, 60,
108, 116, 102, 0,
99, 112, 95, 112,
114, 111, 108, 97,
110, 100, 115, 95,
98, 50, 99, 0,
115, 107, 121, 95,
98, 97, 100, 108,
97, 110, 100, 115,
95, 48, 49, 0,
70, 97, 107, 107,
101, 108, 66, 114,
105, 103, 97, 100,
101, 32, 35, 49,
32, 83, 84, 86,
32, 40, 112, 111,
114, 116, 32, 50,
55, 48, 50, 48,
41, 0, 6, 123,
80, 0, 0, 0,
0, 0, 0, 134,
236, 237, 206, 141,
237, 45, 140, 44,
76, 140, 173, 108,
14, 0, 0, 68,
0, 0, 31, 96,
109, 97, 112, 115,
92, 99, 112, 95,
112, 114, 111, 108,
97, 110, 100, 115,
95, 98, 50, 99,
46, 98, 115, 112,
0, 54, 247, 86,
231, 70, 246, 210,
150, 54, 55, 246,
18, 54, 134, 150,
86, 102, 87, 214,
86, 230, 70, 247,
85, 22, 38, 231,
86, 70, 230, 114,
23, 102, 7, 128,
105, 123, 35, 43,
99, 131, 147, 43,
27, 11, 27, 67,
43, 3, 0, 128,
168, 30, 248, 188,
5, 3, 64, 42,
74, 1, 0, 28,
175, 0, 0, 76,
57, 5, 65, 41,
150, 6, 192, 199,
14, 32, 107, 11,
131, 155, 123, 25,
131, 251, 130, 147,
123, 99, 11, 115,
35, 155, 251, 18,
147, 25, 115, 17
];
suite('Header', () => {
test('Parse header', () => {
assertParser(parseHeader, getStream(data), {
'type': 'HL2DEMO',
'version': 3,
'protocol': 24,
'server': 'FakkelBrigade #1 (#712133)',
'nick': 'SourceTV Demo',
'map': 'cp_prolands_b2c',
'game': 'tf',
'duration': 515.1900024414062,
'ticks': 34346,
'frames': 34334,
'sigon': 808977
}
, 8576);
});
test('Encode header', () => {
assertEncoder(parseHeader, encodeHeader, {
'type': 'HL2DEMO',
'version': 3,
'protocol': 24,
'server': 'FakkelBrigade #1 (#712133)',
'nick': 'SourceTV Demo',
'map': 'cp_prolands_b2c',
'game': 'tf',
'duration': 515.1900024414062,
'ticks': 34346,
'frames': 34334,
'sigon': 808977
}
, 8576);
});
test('Re-encode header', () => {
assertReEncode(parseHeader, encodeHeader, getStream(data));
});
});