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:
parent
51739e3aa8
commit
e9349d178e
9 changed files with 91 additions and 70 deletions
|
|
@ -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);
|
||||
|
|
|
|||
10
src/Demo.ts
10
src/Demo.ts
|
|
@ -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;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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");
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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,
|
||||
|
|
|
|||
|
|
@ -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,14 +10,16 @@ export abstract class Parser {
|
|||
stream: BitStream;
|
||||
length: number;
|
||||
match: Match;
|
||||
skippedPackets: PacketType[];
|
||||
|
||||
constructor(type, tick, stream, length, match) {
|
||||
this.type = type;
|
||||
this.tick = tick;
|
||||
this.stream = stream;
|
||||
this.length = length;//length in bytes
|
||||
this.match = 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;
|
||||
abstract parse(): Packet[]|string;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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,38 +74,41 @@ export function PacketEntities(stream: BitStream, match: Match): PacketEntitiesP
|
|||
let entityId = -1;
|
||||
|
||||
const receivedEntities: PacketEntity[] = [];
|
||||
for (let i = 0; i < updatedEntries; i++) {
|
||||
const diff = readUBitVar(stream);
|
||||
entityId += 1 + diff;
|
||||
const pvs = readPVSType(stream);
|
||||
if (pvs === PVS.ENTER) {
|
||||
const packetEntity = readEnterPVS(stream, entityId, match);
|
||||
applyEntityUpdate(packetEntity, match.getSendTable(packetEntity.serverClass.dataTable), stream);
|
||||
const removedEntityIds: number[] = [];
|
||||
|
||||
if (updatedBaseLine) {
|
||||
const newBaseLine: SendProp[] = [];
|
||||
newBaseLine.concat(packetEntity.props);
|
||||
match.baseLineCache[packetEntity.serverClass.id] = packetEntity.clone();
|
||||
}
|
||||
packetEntity.inPVS = true;
|
||||
receivedEntities.push(packetEntity);
|
||||
} else if (pvs === PVS.PRESERVE) {
|
||||
const packetEntity = getPacketEntityForExisting(entityId, match, pvs);
|
||||
applyEntityUpdate(packetEntity, match.getSendTable(packetEntity.serverClass.dataTable), stream);
|
||||
receivedEntities.push(packetEntity);
|
||||
} else {
|
||||
if(match.entityClasses[entityId]) {
|
||||
const packetEntity = getPacketEntityForExisting(entityId, match, pvs);
|
||||
if (!skip) {
|
||||
for (let i = 0; i < updatedEntries; i++) {
|
||||
const diff = readUBitVar(stream);
|
||||
entityId += 1 + diff;
|
||||
const pvs = readPVSType(stream);
|
||||
if (pvs === PVS.ENTER) {
|
||||
const packetEntity = readEnterPVS(stream, entityId, match);
|
||||
applyEntityUpdate(packetEntity, match.getSendTable(packetEntity.serverClass.dataTable), stream);
|
||||
|
||||
if (updatedBaseLine) {
|
||||
const newBaseLine: SendProp[] = [];
|
||||
newBaseLine.concat(packetEntity.props);
|
||||
match.baseLineCache[packetEntity.serverClass.id] = packetEntity.clone();
|
||||
}
|
||||
packetEntity.inPVS = true;
|
||||
receivedEntities.push(packetEntity);
|
||||
} else if (pvs === PVS.PRESERVE) {
|
||||
const packetEntity = getPacketEntityForExisting(entityId, match, pvs);
|
||||
applyEntityUpdate(packetEntity, match.getSendTable(packetEntity.serverClass.dataTable), stream);
|
||||
receivedEntities.push(packetEntity);
|
||||
} else {
|
||||
if (match.entityClasses[entityId]) {
|
||||
const packetEntity = getPacketEntityForExisting(entityId, match, pvs);
|
||||
receivedEntities.push(packetEntity);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const removedEntityIds: number[] = [];
|
||||
if (isDelta) {
|
||||
while (stream.readBoolean()) {
|
||||
const entityId = stream.readBits(11);
|
||||
removedEntityIds.push(entityId);
|
||||
if (isDelta) {
|
||||
while (stream.readBoolean()) {
|
||||
const entityId = stream.readBits(11);
|
||||
removedEntityIds.push(entityId);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -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};
|
||||
|
|
|
|||
|
|
@ -4,35 +4,37 @@ 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[] = [];
|
||||
for (let i = 0; i < entityCount; i++) {
|
||||
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];
|
||||
// no clue why the -1 but it works
|
||||
// maybe because world (id=0) can never be temp
|
||||
// but it's not like the -1 saves any space
|
||||
const sendTable = match.getSendTable(serverClass.dataTable);
|
||||
entity = new PacketEntity(serverClass, 0, PVS.ENTER);
|
||||
applyEntityUpdate(entity, sendTable, stream);
|
||||
entities.push(entity);
|
||||
} else {
|
||||
if (entity) {
|
||||
applyEntityUpdate(entity, match.getSendTable(entity.serverClass.dataTable), stream);
|
||||
if (!skip) {
|
||||
for (let i = 0; i < entityCount; i++) {
|
||||
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];
|
||||
// no clue why the -1 but it works
|
||||
// maybe because world (id=0) can never be temp
|
||||
// but it's not like the -1 saves any space
|
||||
const sendTable = match.getSendTable(serverClass.dataTable);
|
||||
entity = new PacketEntity(serverClass, 0, PVS.ENTER);
|
||||
applyEntityUpdate(entity, sendTable, stream);
|
||||
entities.push(entity);
|
||||
} else {
|
||||
throw new Error("no entity set to update");
|
||||
if (entity) {
|
||||
applyEntityUpdate(entity, match.getSendTable(entity.serverClass.dataTable), stream);
|
||||
} else {
|
||||
throw new Error("no entity set to update");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if (end - stream.index > 8) {
|
||||
throw new Error("unexpected content after TempEntities");
|
||||
if (end - stream.index > 8) {
|
||||
throw new Error("unexpected content after TempEntities");
|
||||
}
|
||||
}
|
||||
|
||||
stream.index = end;
|
||||
|
|
|
|||
|
|
@ -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');
|
||||
});
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue