1
0
Fork 0
mirror of https://github.com/demostf/demo.js synced 2026-06-04 09:04:13 +02:00

code style and add tslint

This commit is contained in:
Robin Appelman 2017-07-30 22:43:51 +02:00
commit 52a36ed8c8
61 changed files with 1708 additions and 872 deletions

View file

@ -1,11 +1,11 @@
import {BSPDecalPacket} from "../../Data/Packet";
import {BitStream} from 'bit-buffer';
import {Vector} from "../../Data/Vector";
import {BSPDecalPacket} from '../../Data/Packet';
import {Vector} from '../../Data/Vector';
const getCoord = function (stream) {
const hasInt = !!stream.readBits(1);
function getCoord(stream) {
const hasInt = !!stream.readBits(1);
const hasFract = !!stream.readBits(1);
let value = 0;
let value = 0;
if (hasInt || hasFract) {
const sign = !!stream.readBits(1);
if (hasInt) {
@ -19,34 +19,35 @@ const getCoord = function (stream) {
}
}
return value;
};
}
const getVecCoord = function (stream): Vector {
function getVecCoord(stream): Vector {
const hasX = !!stream.readBits(1);
const hasY = !!stream.readBits(1);
const hasZ = !!stream.readBits(1);
return {
x: hasX ? getCoord(stream) : 0,
y: hasY ? getCoord(stream) : 0,
z: hasZ ? getCoord(stream) : 0
}
};
z: hasZ ? getCoord(stream) : 0,
};
}
export function BSPDecal(stream: BitStream): BSPDecalPacket { // 21: BSPDecal
let modelIndex, entIndex;
const position = getVecCoord(stream);
let modelIndex = 0;
let entIndex = 0;
const position = getVecCoord(stream);
const textureIndex = stream.readBits(9);
if (stream.readBits(1)) {
entIndex = stream.readBits(11);
entIndex = stream.readBits(11);
modelIndex = stream.readBits(12);
}
const lowPriority = stream.readBoolean();
return {
packetType: 'bspDecal',
position: position,
textureIndex: textureIndex,
entIndex: entIndex,
modelIndex: modelIndex,
lowPriority: lowPriority
}
packetType: 'bspDecal',
position,
textureIndex,
entIndex,
modelIndex,
lowPriority,
};
}

View file

@ -1,26 +1,26 @@
import {ClassInfoPacket} from "../../Data/Packet";
import {BitStream} from 'bit-buffer';
import {ClassInfoPacket} from '../../Data/Packet';
import {logBase2} from '../../Math';
export function ClassInfo(stream: BitStream): ClassInfoPacket { // 10: classInfo
const number = stream.readBits(16);
const count = stream.readBits(16);
const create = stream.readBoolean();
let entries: any[] = [];
const entries: any[] = [];
if (!create) {
const bits = logBase2(number) + 1;
for (let i = 0; i < number; i++) {
const bits = logBase2(count) + 1;
for (let i = 0; i < count; i++) {
const entry = {
'classId': stream.readBits(bits),
'className': stream.readASCIIString(),
'dataTableName': stream.readASCIIString()
classId: stream.readBits(bits),
className: stream.readASCIIString(),
dataTableName: stream.readASCIIString(),
};
entries.push(entry);
}
}
return {
'packetType': 'classInfo',
number: number,
create: create,
entries: entries
}
packetType: 'classInfo',
number: count,
create,
entries,
};
}

View file

@ -1,13 +1,13 @@
import {CmdKeyValuesPacket} from "../../Data/Packet";
import {BitStream} from 'bit-buffer';
import {CmdKeyValuesPacket} from '../../Data/Packet';
export function CmdKeyValues(stream: BitStream): CmdKeyValuesPacket {
//'length{32}data{$length}'
// 'length{32}data{$length}'
const length = stream.readUint32();
const data = stream.readBitStream(length);
return {
packetType: 'cmdKeyValues',
length: length,
data: data
length,
data,
};
}

View file

@ -1,12 +1,12 @@
import {StringTablePacket} from "../../Data/Packet";
import {BitStream} from 'bit-buffer';
import {logBase2} from "../../Math";
import {readVarInt} from "../readBitVar";
import {StringTablePacket} from '../../Data/Packet';
import {logBase2} from '../../Math';
import {readVarInt} from '../readBitVar';
import {uncompress} from "snappyjs";
import {StringTable} from "../../Data/StringTable";
import {parseStringTable} from "../StringTableParser";
import {Match} from "../../Data/Match";
import {uncompress} from 'snappyjs';
import {Match} from '../../Data/Match';
import {StringTable} from '../../Data/StringTable';
import {parseStringTable} from '../StringTableParser';
export function CreateStringTable(stream: BitStream, match: Match): StringTablePacket { // 12: createStringTable
const tableName = stream.readASCIIString();
@ -38,12 +38,12 @@ export function CreateStringTable(stream: BitStream, match: Match): StringTableP
const compressedData = data.readArrayBuffer(compressedByteSize - 4); // 4 magic bytes
if (magic !== 'SNAP') {
throw new Error("Unknown compressed stringtable format");
throw new Error('Unknown compressed stringtable format');
}
const decompressedData = uncompress(compressedData);
if (decompressedData.byteLength !== decompressedByteSize) {
throw new Error("Incorrect length of decompressed stringtable");
throw new Error('Incorrect length of decompressed stringtable');
}
data = new BitStream(decompressedData.buffer);
@ -52,15 +52,15 @@ export function CreateStringTable(stream: BitStream, match: Match): StringTableP
const table: StringTable = {
name: tableName,
entries: [],
maxEntries: maxEntries,
maxEntries,
fixedUserDataSize: userDataSize,
fixedUserDataSizeBits: userDataSizeBits
fixedUserDataSizeBits: userDataSizeBits,
};
parseStringTable(data, table, entityCount, match);
return {
packetType: 'stringTable',
tables: [table]
tables: [table],
};
}

View file

@ -1,12 +1,11 @@
import {make} from './ParserGenerator';
import {EntityMessagePacket} from "../../Data/Packet";
import {BitStream} from 'bit-buffer';
import {Match} from "../../Data/Match";
import {Match} from '../../Data/Match';
import {EntityMessagePacket} from '../../Data/Packet';
const baseParser = make('entityMessage', 'index{11}classId{9}length{11}data{$length}');
export function EntityMessage(stream: BitStream, match: Match): EntityMessagePacket { // 24: entityMessage
const result = <EntityMessagePacket>baseParser(stream); //todo parse data further?
return result;
};
return baseParser(stream) as EntityMessagePacket; // todo parse data further?
}

View file

@ -1,31 +1,30 @@
import {GameEventPacket} from "../../Data/Packet";
import {BitStream} from 'bit-buffer';
import {
GameEventType, GameEventValue, GameEventEntry, GameEventDefinition, GameEvent as IGameEvent,
GameEventValueMap, GameEventDefinitionMap
} from "../../Data/GameEvent";
import {Match} from "../../Data/Match";
GameEvent as IGameEvent, GameEventDefinition, GameEventDefinitionMap, GameEventEntry, GameEventType,
GameEventValue, GameEventValueMap,
} from '../../Data/GameEvent';
import {Match} from '../../Data/Match';
import {GameEventPacket} from '../../Data/Packet';
const parseGameEvent = function (eventId: number, stream: BitStream, events: GameEventDefinitionMap): IGameEvent {
function parseGameEvent(eventId: number, stream: BitStream, events: GameEventDefinitionMap): IGameEvent {
if (!events[eventId]) {
throw new Error('unknown event type')
throw new Error('unknown event type');
}
const eventDescription: GameEventDefinition = events[eventId];
const values: GameEventValueMap = {};
for (let i = 0; i < eventDescription.entries.length; i++) {
const entry: GameEventEntry = eventDescription.entries[i];
const value = getGameEventValue(stream, entry);
const values: GameEventValueMap = {};
for (const entry of eventDescription.entries) {
const value = getGameEventValue(stream, entry);
if (value) {
values[entry.name] = value;
}
}
return {
name: eventDescription.name,
values: values
name: eventDescription.name,
values,
};
};
}
const getGameEventValue = function (stream: BitStream, entry: GameEventEntry): GameEventValue|null {
function getGameEventValue(stream: BitStream, entry: GameEventEntry): GameEventValue | null {
switch (entry.type) {
case GameEventType.STRING:
return stream.readUTF8String();
@ -44,17 +43,16 @@ const getGameEventValue = function (stream: BitStream, entry: GameEventEntry): G
default:
throw new Error('invalid game event type');
}
};
}
export function GameEvent(stream: BitStream, match: Match): GameEventPacket { // 25: game event
const length = stream.readBits(11);
const end = stream.index + length;
const length = stream.readBits(11);
const end = stream.index + length;
const eventId = stream.readBits(9);
const event = parseGameEvent(eventId, stream, match.eventDefinitions);
const event = parseGameEvent(eventId, stream, match.eventDefinitions);
stream.index = end;
return {
packetType: 'gameEvent',
event: event
}
event,
};
}

View file

@ -1,7 +1,7 @@
import {GameEventListPacket} from "../../Data/Packet";
import {BitStream} from 'bit-buffer';
import {GameEventEntry, GameEventDefinitionMap} from "../../Data/GameEvent";
import {Match} from "../../Data/Match";
import {GameEventDefinitionMap, GameEventEntry} from '../../Data/GameEvent';
import {Match} from '../../Data/Match';
import {GameEventListPacket} from '../../Data/Packet';
export function GameEventList(stream: BitStream, match: Match): GameEventListPacket { // 30: gameEventList
// list of game events and parameters
@ -15,19 +15,19 @@ export function GameEventList(stream: BitStream, match: Match): GameEventListPac
const entries: GameEventEntry[] = [];
while (type !== 0) {
entries.push({
type: type,
name: stream.readASCIIString()
type,
name: stream.readASCIIString(),
});
type = stream.readBits(3);
}
eventList[id] = {
id: id,
name: name,
entries: entries
id,
name,
entries,
};
}
return {
packetType: 'gameEventList',
eventList: eventList
}
eventList,
};
}

View file

@ -1,15 +1,14 @@
import {MenuPacket} from "../../Data/Packet";
import {BitStream} from 'bit-buffer';
import {MenuPacket} from '../../Data/Packet';
export function Menu(stream: BitStream): MenuPacket {
//'type{16}length{16}_{$length}_{$length}_{$length}_{$length}_{$length}_{$length}_{$length}'
const type = stream.readUint16();
const length = stream.readUint16();
const data = stream.readBitStream(length * 8); //length is in bytes
const data = stream.readBitStream(length * 8); // length is in bytes
return {
packetType: 'menu',
type: type,
length: length,
data: data
type,
length,
data,
};
}

View file

@ -1,16 +1,16 @@
import {BitStream} from 'bit-buffer';
import {Match} from '../../Data/Match';
import {PacketEntitiesPacket} from '../../Data/Packet';
import {PacketEntity, PVS} from '../../Data/PacketEntity';
import {SendProp} from '../../Data/SendProp';
import {PacketEntitiesPacket} from "../../Data/Packet";
import {BitStream} from 'bit-buffer';
import {Match} from "../../Data/Match";
import {readUBitVar} from "../readBitVar";
import {applyEntityUpdate} from "../EntityDecoder";
import {applyEntityUpdate} from '../EntityDecoder';
import {readUBitVar} from '../readBitVar';
const pvsMap = {
0: PVS.PRESERVE,
2: PVS.ENTER,
1: PVS.LEAVE,
3: PVS.LEAVE + PVS.DELETE
3: PVS.LEAVE + PVS.DELETE,
};
function readPVSType(stream: BitStream): PVS {
@ -58,7 +58,7 @@ function getPacketEntityForExisting(entityId: number, match: Match, pvs: PVS) {
return new PacketEntity(serverClass, entityId, pvs);
}
export function PacketEntities(stream: BitStream, match: Match, skip: boolean = false): PacketEntitiesPacket { //26: packetEntities
export function PacketEntities(stream: BitStream, match: Match, skip: boolean = false): PacketEntitiesPacket { // 26: packetEntities
// https://github.com/skadistats/smoke/blob/master/smoke/replay/handler/svc_packetentities.pyx
// https://github.com/StatsHelix/demoinfo/blob/3d28ea917c3d44d987b98bb8f976f1a3fcc19821/DemoInfo/DP/Handler/PacketEntitesHandler.cs
// https://github.com/StatsHelix/demoinfo/blob/3d28ea917c3d44d987b98bb8f976f1a3fcc19821/DemoInfo/DP/Entity.cs
@ -106,8 +106,7 @@ export function PacketEntities(stream: BitStream, match: Match, skip: boolean =
if (isDelta) {
while (stream.readBoolean()) {
const entityId = stream.readBits(11);
removedEntityIds.push(entityId);
removedEntityIds.push(stream.readBits(11));
}
}
}
@ -117,12 +116,12 @@ export function PacketEntities(stream: BitStream, match: Match, skip: boolean =
packetType: 'packetEntities',
entities: receivedEntities,
removedEntities: removedEntityIds,
maxEntries: maxEntries,
isDelta: isDelta,
delta: delta,
baseLine: baseLine,
updatedEntries: updatedEntries,
length: length,
updatedBaseLine: updatedBaseLine
maxEntries,
isDelta,
delta,
baseLine,
updatedEntries,
length,
updatedBaseLine,
};
}

View file

@ -1,5 +1,5 @@
import {ParseSoundsPacket} from "../../Data/Packet";
import {BitStream} from 'bit-buffer';
import {ParseSoundsPacket} from '../../Data/Packet';
export function ParseSounds(stream: BitStream): ParseSoundsPacket { // 17: parseSounds
const reliable = stream.readBoolean();
@ -8,8 +8,8 @@ export function ParseSounds(stream: BitStream): ParseSoundsPacket { // 17: parse
stream.index += length;
return {
packetType: 'parseSounds',
reliable: reliable,
num: num,
length: length
}
reliable,
num,
length,
};
}

View file

@ -1,6 +1,9 @@
import {Packet} from "../../Data/Packet";
import {BitStream} from 'bit-buffer';
import {Match} from "../../Data/Match";
import {Match} from '../../Data/Match';
import {Packet} from '../../Data/Packet';
export type Parser = (stream: BitStream, match?: Match, skip?: boolean) => Packet;
export type PacketParserMap = {[id: number]: Parser};
export interface PacketParserMap {
[id: number]: Parser;
}

View file

@ -1,30 +1,30 @@
import {Packet} from '../../Data/Packet';
import {Parser} from './Parser';
import {Packet} from "../../Data/Packet";
export function make(name: string, definition: string): Parser {
const parts = definition.substr(0, definition.length - 1).split('}');//remove leading } to prevent empty part
const items = parts.map(function (part) {
const parts = definition.substr(0, definition.length - 1).split('}'); // remove leading } to prevent empty part
const items = parts.map((part) => {
return part.split('{');
});
return function (stream):Packet {
let result = {
'packetType': name
return (stream) => {
const result = {
packetType: name,
};
try {
for (let i = 0; i < items.length; i++) {
const value = readItem(stream, items[i][1], result);
if (items[i][0] !== '_') {
result[items[i][0]] = value;
for (const group of items) {
const value = readItem(stream, group[1], result);
if (group[0] !== '_') {
result[group[0]] = value;
}
}
} catch (e) {
throw new Error('Failed reading pattern ' + definition + '. ' + e);
}
return <Packet>result;
}
return result as Packet;
};
}
const readItem = function (stream, description, data) {
function readItem(stream, description, data) {
let length;
if (description[0] === 'b') {
return stream.readBoolean();
@ -46,4 +46,4 @@ const readItem = function (stream, description, data) {
} else {
return stream.readBits(parseInt(description, 10), true);
}
};
}

View file

@ -1,14 +1,14 @@
import {SetConVarPacket} from "../../Data/Packet";
import {BitStream} from 'bit-buffer';
import {SetConVarPacket} from '../../Data/Packet';
export function SetConVar(stream: BitStream): SetConVarPacket { // 5: setconvar
const count = stream.readBits(8);
let vars: {[key: string]: string} = {};
const vars: {[key: string]: string} = {};
for (let i = 0; i < count; i++) {
vars[stream.readUTF8String()] = stream.readUTF8String();
}
return {
packetType: 'setConVar',
vars: vars
}
vars,
};
}

View file

@ -1,8 +1,8 @@
import {TempEntitiesPacket} from "../../Data/Packet";
import {BitStream} from 'bit-buffer';
import {Match} from "../../Data/Match";
import {PacketEntity, PVS} from "../../Data/PacketEntity";
import {applyEntityUpdate} from "../EntityDecoder";
import {Match} from '../../Data/Match';
import {TempEntitiesPacket} from '../../Data/Packet';
import {PacketEntity, PVS} from '../../Data/PacketEntity';
import {applyEntityUpdate} from '../EntityDecoder';
export function TempEntities(stream: BitStream, match: Match, skip: boolean = false): TempEntitiesPacket { // 10: classInfo
const entityCount = stream.readBits(8);
@ -10,10 +10,10 @@ export function TempEntities(stream: BitStream, match: Match, skip: boolean = fa
const end = stream.index + length;
let entity: PacketEntity|null = null;
let entities: PacketEntity[] = [];
const entities: PacketEntity[] = [];
if (!skip) {
for (let i = 0; i < entityCount; i++) {
const delay = (stream.readBoolean()) ? stream.readUint8() / 100 : 0; //unused it seems
const delay = (stream.readBoolean()) ? stream.readUint8() / 100 : 0; // unused it seems
if (stream.readBoolean()) {
const classId = stream.readBits(match.classBits);
const serverClass = match.serverClasses[classId - 1];
@ -28,20 +28,20 @@ export function TempEntities(stream: BitStream, match: Match, skip: boolean = fa
if (entity) {
applyEntityUpdate(entity, match.getSendTable(entity.serverClass.dataTable), stream);
} else {
throw new Error("no entity set to update");
throw new Error('no entity set to update');
}
}
}
if (end - stream.index > 8) {
throw new Error("unexpected content after TempEntities");
throw new Error('unexpected content after TempEntities');
}
}
stream.index = end;
return {
packetType: 'tempEntities',
entities: entities
}
entities,
};
}
function readVarInt(stream: BitStream) {
@ -50,7 +50,7 @@ function readVarInt(stream: BitStream) {
const byte = stream.readUint8();
result |= ((byte & 0x7F) << run);
if ((byte >> 7) == 0) {
if ((byte >> 7) === 0) {
return result;
}
}

View file

@ -1,7 +1,7 @@
import {StringTablePacket} from "../../Data/Packet";
import {BitStream} from 'bit-buffer';
import {Match} from "../../Data/Match";
import {parseStringTable} from "../StringTableParser";
import {Match} from '../../Data/Match';
import {StringTablePacket} from '../../Data/Packet';
import {parseStringTable} from '../StringTableParser';
export function UpdateStringTable(stream: BitStream, match: Match): StringTablePacket { // 12: updateStringTable
const tableId = stream.readBits(5);
@ -21,6 +21,6 @@ export function UpdateStringTable(stream: BitStream, match: Match): StringTableP
return {
packetType: 'stringTable',
tables: [table]
tables: [table],
};
}

View file

@ -1,9 +1,9 @@
import {UserMessagePacket} from "../../Data/Packet";
import {BitStream} from 'bit-buffer';
import {make} from './ParserGenerator';
import {UserMessagePacket} from '../../Data/Packet';
import {SayText2} from '../UserMessage/SayText2';
import {make} from './ParserGenerator';
enum UserMessageType{
enum UserMessageType {
Geiger = 0,
Train = 1,
HudText = 2,
@ -61,12 +61,12 @@ enum UserMessageType{
HapPunch = 54,
HapSetDrag = 55,
HapSet = 56,
HapMeleeContact = 57
HapMeleeContact = 57,
}
const userMessageParsers = {
4: SayText2,
5: make('textMsg', 'destType{8}text{s}')
5: make('textMsg', 'destType{8}text{s}'),
};
export function UserMessage(stream: BitStream): UserMessagePacket { // 23: user message
@ -79,7 +79,7 @@ export function UserMessage(stream: BitStream): UserMessagePacket { // 23: user
} else {
result = {
packetType: 'unknownUserMessage',
type: type
type,
};
}
stream.index = pos + length;

View file

@ -1,5 +1,5 @@
import {VoiceDataPacket} from "../../Data/Packet";
import {BitStream} from 'bit-buffer';
import {VoiceDataPacket} from '../../Data/Packet';
export function VoiceData(stream: BitStream): VoiceDataPacket {
// 'client{8}proximity{8}length{16}_{$length}'
@ -9,9 +9,9 @@ export function VoiceData(stream: BitStream): VoiceDataPacket {
const data = stream.readBitStream(length);
return {
packetType: 'voiceData',
client: client,
proximity: proximity,
length: length,
data: data
client,
proximity,
length,
data,
};
}

View file

@ -1,5 +1,5 @@
import {VoiceInitPacket} from "../../Data/Packet";
import {BitStream} from 'bit-buffer';
import {VoiceInitPacket} from '../../Data/Packet';
export function VoiceInit(stream: BitStream): VoiceInitPacket {
const codec = stream.readASCIIString();
@ -8,8 +8,8 @@ export function VoiceInit(stream: BitStream): VoiceInitPacket {
const extraData = (codec === 'vaudio_celt' && quality === 255) ? stream.readUint16() : 0;
return {
packetType: 'voiceInit',
codec: codec,
quality: quality,
extraData: extraData
codec,
quality,
extraData,
};
}