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

add fast mode

This commit is contained in:
Robin Appelman 2017-03-07 18:16:55 +01:00
commit e9349d178e
9 changed files with 91 additions and 70 deletions

View file

@ -26,7 +26,7 @@ var echo = function (data) {
fs.readFile(argv._[0], function (err, data) {
if (err) throw err;
var demo = Demo.fromNodeBuffer(data);
var parser = demo.getParser();
var parser = demo.getParser(true);
var head = parser.readHeader();
if (argv.head) {
echo(head);

View file

@ -2,6 +2,7 @@ import {Stream} from "stream";
import {BitStream} from 'bit-buffer';
import {Parser} from './Parser';
import {StreamParser} from './StreamParser';
import {PacketType} from "./Parser/Message/Packet";
export class Demo {
stream: BitStream;
@ -11,9 +12,14 @@ export class Demo {
this.stream = new BitStream(arrayBuffer);
}
getParser() {
getParser(fastMode: boolean = false) {
if (!this.parser) {
this.parser = new Parser(this.stream);
const skippedPackets = fastMode ? [
PacketType.packetEntities,
PacketType.tempEntities,
PacketType.entityMessage,
] : [];
this.parser = new Parser(this.stream, skippedPackets);
}
return this.parser;
}

View file

@ -1,4 +1,4 @@
import {Packet} from './Parser/Message/Packet';
import {Packet, PacketType} from './Parser/Message/Packet';
import {ConsoleCmd} from './Parser/Message/ConsoleCmd';
import {StringTable} from './Parser/Message/StringTable';
import {DataTable} from './Parser/Message/DataTable';
@ -12,12 +12,14 @@ import {Header} from "./Data/Header";
export class Parser extends EventEmitter {
stream: BitStream;
match: Match;
skipPackets: PacketType[];
constructor(stream: BitStream) {
constructor(stream: BitStream, skipPackets: PacketType[] = []) {
super();
this.stream = stream;
this.match = new Match();
this.on('packet', this.match.handlePacket.bind(this.match));
this.skipPackets = skipPackets;
}
readHeader() {
@ -62,15 +64,15 @@ export class Parser extends EventEmitter {
switch (type) {
case MessageType.Sigon:
case MessageType.Packet:
return new Packet(type, tick, data, length, match);
return new Packet(type, tick, data, length, match, this.skipPackets);
case MessageType.ConsoleCmd:
return new ConsoleCmd(type, tick, data, length, match);
return new ConsoleCmd(type, tick, data, length, match, this.skipPackets);
case MessageType.UserCmd:
return new UserCmd(type, tick, data, length, match);
return new UserCmd(type, tick, data, length, match, this.skipPackets);
case MessageType.DataTables:
return new DataTable(type, tick, data, length, match);
return new DataTable(type, tick, data, length, match, this.skipPackets);
case MessageType.StringTables:
return new StringTable(type, tick, data, length, match);
return new StringTable(type, tick, data, length, match, this.skipPackets);
default:
throw new Error("unknown message type");
}

View file

@ -37,7 +37,8 @@ export class Packet extends Parser {
const type = this.stream.readBits(6);
if (type !== 0) {
if (Packet.parsers[type]) {
let packet = Packet.parsers[type].call(this, this.stream, this.match);
const skip = this.skippedPackets.indexOf(type) !== -1;
const packet = Packet.parsers[type].call(this, this.stream, this.match, skip);
packets.push(packet);
} else {
throw new Error('Unknown packet type ' + type + " just parsed a " + PacketType[lastPacketType]);
@ -86,7 +87,7 @@ export class Packet extends Parser {
};
}
enum PacketType {
export enum PacketType {
file = 2,
netTick = 3,
stringCmd = 4,

View file

@ -1,7 +1,8 @@
import {BitStream} from 'bit-buffer';
import {Match} from '../../Data/Match';
import {Packet} from "../../Data/Packet";
import {SendTable} from "../../Data/SendTable";
import {MessageType} from "../../Parser";
import {PacketType} from "./Packet";
export abstract class Parser {
type: any;
@ -9,13 +10,15 @@ export abstract class Parser {
stream: BitStream;
length: number;
match: Match;
skippedPackets: PacketType[];
constructor(type, tick, stream, length, match) {
constructor(type: MessageType, tick: number, stream: BitStream, length: number, match: Match, skippedPacket: PacketType[] = []) {
this.type = type;
this.tick = tick;
this.stream = stream;
this.length = length;//length in bytes
this.match = match;
this.skippedPackets = skippedPacket;
}
abstract parse(): Packet[]|string;

View file

@ -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): 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
@ -74,6 +74,9 @@ export function PacketEntities(stream: BitStream, match: Match): PacketEntitiesP
let entityId = -1;
const receivedEntities: PacketEntity[] = [];
const removedEntityIds: number[] = [];
if (!skip) {
for (let i = 0; i < updatedEntries; i++) {
const diff = readUBitVar(stream);
entityId += 1 + diff;
@ -101,13 +104,13 @@ export function PacketEntities(stream: BitStream, match: Match): PacketEntitiesP
}
}
const removedEntityIds: number[] = [];
if (isDelta) {
while (stream.readBoolean()) {
const entityId = stream.readBits(11);
removedEntityIds.push(entityId);
}
}
}
stream.index = end;
return {

View file

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

View file

@ -4,13 +4,14 @@ import {Match} from "../../Data/Match";
import {PacketEntity, PVS} from "../../Data/PacketEntity";
import {applyEntityUpdate} from "../EntityDecoder";
export function TempEntities(stream: BitStream, match: Match): TempEntitiesPacket { // 10: classInfo
export function TempEntities(stream: BitStream, match: Match, skip: boolean = false): TempEntitiesPacket { // 10: classInfo
const entityCount = stream.readBits(8);
const length = readVarInt(stream);
const end = stream.index + length;
let entity: PacketEntity|null = null;
let entities: PacketEntity[] = [];
if (!skip) {
for (let i = 0; i < entityCount; i++) {
const delay = (stream.readBoolean()) ? stream.readUint8() / 100 : 0; //unused it seems
if (stream.readBoolean()) {
@ -34,6 +35,7 @@ export function TempEntities(stream: BitStream, match: Match): TempEntitiesPacke
if (end - stream.index > 8) {
throw new Error("unexpected content after TempEntities");
}
}
stream.index = end;
return {

View file

@ -2,11 +2,11 @@ import {readFileSync} from 'fs';
import {Demo} from "../../Demo";
import * as assert from 'assert';
function testDemo(name: string) {
function testDemo(name: string, fastMode: boolean = false) {
const target = JSON.parse(readFileSync(`${__dirname}/../data/${name}.json`, 'utf8'));
const source = readFileSync(`${__dirname}/../data/${name}.dem`);
const demo = Demo.fromNodeBuffer(source);
const parser = demo.getParser();
const parser = demo.getParser(fastMode);
parser.readHeader();
parser.parseBody();
const parsed = parser.match.getState();
@ -14,6 +14,10 @@ function testDemo(name: string) {
}
suite('Parse basic demo info', () => {
test('Fast mode', () => {
testDemo('snakewater', true);
});
test('Parse snakewater.dem', () => {
testDemo('snakewater');
});