mirror of
https://github.com/demostf/demo.js
synced 2026-06-04 09:04:13 +02:00
propper skipping of TempEntities
This commit is contained in:
parent
3b805be013
commit
1fbb61f252
11 changed files with 255 additions and 163 deletions
|
|
@ -8,22 +8,22 @@ export class DataTable extends Parser {
|
|||
// https://github.com/LestaD/SourceEngine2007/blob/43a5c90a5ada1e69ca044595383be67f40b33c61/src_main/engine/dt_common_eng.cpp#L356
|
||||
// https://github.com/LestaD/SourceEngine2007/blob/43a5c90a5ada1e69ca044595383be67f40b33c61/src_main/engine/dt_recv_eng.cpp#L310
|
||||
// https://github.com/PazerOP/DemoLib/blob/master/DemoLib/Commands/DemoDataTablesCommand.cs
|
||||
var tables:SendTable[] = [];
|
||||
var i, j;
|
||||
let tables:SendTable[] = [];
|
||||
let i, j;
|
||||
while (this.stream.readBoolean()) {
|
||||
var needsDecoder = this.stream.readBoolean();
|
||||
var tableName = this.stream.readASCIIString();
|
||||
var numProps = this.stream.readBits(10);
|
||||
var table = new SendTable(tableName);
|
||||
const needsDecoder = this.stream.readBoolean();
|
||||
const tableName = this.stream.readASCIIString();
|
||||
const numProps = this.stream.readBits(10);
|
||||
const table = new SendTable(tableName);
|
||||
|
||||
// get props metadata
|
||||
var arrayElementProp;
|
||||
let arrayElementProp;
|
||||
for (i = 0; i < numProps; i++) {
|
||||
var propType = this.stream.readBits(5);
|
||||
var propName = this.stream.readASCIIString();
|
||||
var nFlagsBits = 16; // might be 11 (old?), 13 (new?), 16(networked) or 17(??)
|
||||
var flags = this.stream.readBits(nFlagsBits);
|
||||
var prop = new SendPropDefinition(propType, propName, flags);
|
||||
const propType = this.stream.readBits(5);
|
||||
const propName = this.stream.readASCIIString();
|
||||
const nFlagsBits = 16; // might be 11 (old?), 13 (new?), 16(networked) or 17(??)
|
||||
const flags = this.stream.readBits(nFlagsBits);
|
||||
const prop = new SendPropDefinition(propType, propName, flags, tableName);
|
||||
if (propType === SendPropType.DPT_DataTable) {
|
||||
prop.excludeDTName = this.stream.readASCIIString();
|
||||
} else {
|
||||
|
|
@ -57,6 +57,12 @@ export class DataTable extends Parser {
|
|||
}
|
||||
|
||||
if (prop.hasFlag(SendPropFlag.SPROP_INSIDEARRAY)) {
|
||||
if (arrayElementProp) {
|
||||
throw new Error("array element already set");
|
||||
}
|
||||
if (prop.hasFlag(SendPropFlag.SPROP_CHANGES_OFTEN)) {
|
||||
throw new Error("unexpected CHANGES_OFTEN prop in array");
|
||||
}
|
||||
arrayElementProp = prop;
|
||||
} else {
|
||||
table.addProp(prop);
|
||||
|
|
@ -76,23 +82,23 @@ export class DataTable extends Parser {
|
|||
}
|
||||
}
|
||||
|
||||
var serverClasses = this.stream.readUint16(); // short
|
||||
const serverClasses = this.stream.readUint16(); // short
|
||||
if (serverClasses <= 0) {
|
||||
throw "expected one or more serverclasses";
|
||||
}
|
||||
|
||||
for (i = 0; i < serverClasses; i++) {
|
||||
var classId = this.stream.readUint16();
|
||||
const classId = this.stream.readUint16();
|
||||
if (classId > serverClasses) {
|
||||
throw "invalid class id";
|
||||
}
|
||||
var className = this.stream.readASCIIString();
|
||||
var dataTable = this.stream.readASCIIString();
|
||||
const className = this.stream.readASCIIString();
|
||||
const dataTable = this.stream.readASCIIString();
|
||||
this.match.serverClasses.push(new ServerClass(classId, className, dataTable));
|
||||
}
|
||||
|
||||
var bitsLeft = (this.length * 8) - this.stream._index;
|
||||
if (bitsLeft > 7) {
|
||||
const bitsLeft = (this.length * 8) - this.stream._index;
|
||||
if (bitsLeft > 7 || bitsLeft < 0) {
|
||||
throw "unexpected remaining data in datatable (" + bitsLeft + " bits)";
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -13,8 +13,11 @@ import {SetConVar} from '../Packet/SetConVar';
|
|||
import {UpdateStringTable} from '../Packet/UpdateStringTable';
|
||||
import {UserMessage} from '../Packet/UserMessage';
|
||||
import {PacketParserMap} from '../Packet/Parser'
|
||||
import {TempEntities} from '../Packet/TempEntities'
|
||||
|
||||
import {GameEventDefinitionMap} from "../../Data/GameEvent";
|
||||
|
||||
|
||||
import {Packet as IPacket} from '../../Data/Packet';
|
||||
|
||||
// https://code.google.com/p/coldemoplayer/source/browse/branches/2.0/compLexity+Demo+Player/CDP.Source/Messages/?r=219
|
||||
|
|
@ -23,15 +26,10 @@ import {Packet as IPacket} from '../../Data/Packet';
|
|||
// https://github.com/LestaD/SourceEngine2007/blob/master/src_main/common/netmessages.cpp
|
||||
|
||||
export class Packet extends Parser {
|
||||
viewOrigin: any;
|
||||
|
||||
parse() {
|
||||
//var table = new PacketStringTable(this.stream);
|
||||
//table.searchIds();
|
||||
//return [];
|
||||
|
||||
let packets: IPacket[] = [];
|
||||
let entities = [];
|
||||
let entities = [];
|
||||
let lastPacketType = 0;
|
||||
while (this.bitsLeft > 6) { // last 6 bits for NOOP
|
||||
const type = this.stream.readBits(6);
|
||||
if (type !== 0) {
|
||||
|
|
@ -39,8 +37,9 @@ export class Packet extends Parser {
|
|||
let packet = Packet.parsers[type].call(this, this.stream, Packet.gameEventMap, entities, this.match);
|
||||
packets.push(packet);
|
||||
} else {
|
||||
throw 'Unknown packet type ' + type;
|
||||
throw new Error('Unknown packet type ' + type + " just parsed a " + PacketType[lastPacketType]);
|
||||
}
|
||||
lastPacketType = type;
|
||||
}
|
||||
}
|
||||
return packets;
|
||||
|
|
@ -51,13 +50,13 @@ export class Packet extends Parser {
|
|||
}
|
||||
|
||||
static parsers: PacketParserMap = {
|
||||
2: ParserGenerator.make('file', 'transferId{32}fileName{s}requested{b}'),
|
||||
3: ParserGenerator.make('netTick', 'tick{32}frameTime{16}stdDev{16}'),
|
||||
4: ParserGenerator.make('stringCmd', 'command{s}'),
|
||||
5: SetConVar,
|
||||
6: ParserGenerator.make('sigOnState', 'state{8}count{32}'),
|
||||
7: ParserGenerator.make('print', 'value{s}'),
|
||||
8: ParserGenerator.make('serverInfo',
|
||||
2: ParserGenerator.make('file', 'transferId{32}fileName{s}requested{b}'),
|
||||
3: ParserGenerator.make('netTick', 'tick{32}frameTime{16}stdDev{16}'),
|
||||
4: ParserGenerator.make('stringCmd', 'command{s}'),
|
||||
5: SetConVar,
|
||||
6: ParserGenerator.make('sigOnState', 'state{8}count{32}'),
|
||||
7: ParserGenerator.make('print', 'value{s}'),
|
||||
8: ParserGenerator.make('serverInfo',
|
||||
'version{16}serverCount{32}stv{b}dedicated{b}maxCrc{32}maxClasses{16}' +
|
||||
'mapHash{128}playerCount{8}maxPlayerCount{8}intervalPerTick{f32}platform{s1}' +
|
||||
'game{s}map{s}skybox{s}serverName{s}replay{b}'),
|
||||
|
|
@ -75,7 +74,7 @@ export class Packet extends Parser {
|
|||
24: EntityMessage,
|
||||
25: GameEvent,
|
||||
26: PacketEntities,
|
||||
27: ParserGenerator.make('tempEntities', 'count{8}length{17}_{$length}'),
|
||||
27: TempEntities,
|
||||
28: ParserGenerator.make('preFetch', 'index{14}'),
|
||||
29: ParserGenerator.make('menu', 'type{16}length{16}_{$length}_{$length}_{$length}_{$length}_{$length}_{$length}_{$length}'),//length*8
|
||||
30: GameEventList,
|
||||
|
|
@ -85,3 +84,33 @@ export class Packet extends Parser {
|
|||
|
||||
static gameEventMap: GameEventDefinitionMap = {};
|
||||
}
|
||||
|
||||
enum PacketType {
|
||||
file = 2,
|
||||
netTick = 3,
|
||||
stringSmd = 4,
|
||||
setConVar = 5,
|
||||
sigOnState = 6,
|
||||
print = 7,
|
||||
serverInfo = 8,
|
||||
classInfo = 10,
|
||||
setPause = 11,
|
||||
createStringTable = 12,
|
||||
updateStringTable = 13,
|
||||
voiceInit = 14,
|
||||
voiceData = 15,
|
||||
parseSounds = 17,
|
||||
setView = 18,
|
||||
fixAngle = 19,
|
||||
bspDecal = 21,
|
||||
userMessage = 23,
|
||||
entityMessage = 24,
|
||||
gameEvent = 25,
|
||||
packetEntities = 26,
|
||||
tempEntities = 27,
|
||||
preFetch = 28,
|
||||
menu = 29,
|
||||
gameEventList = 30,
|
||||
getCvarValue = 30,
|
||||
cmdKeyValues = 32
|
||||
}
|
||||
|
|
|
|||
|
|
@ -41,7 +41,7 @@ export function BSPDecal(stream: BitStream): Packet { // 21: BSPDecal
|
|||
}
|
||||
const lowPriority = !!stream.readBits(1);
|
||||
return {
|
||||
packetType: 'BSPDecal',
|
||||
packetType: 'bspDecal',
|
||||
position: position,
|
||||
textureIndex: textureIndex,
|
||||
entIndex: entIndex,
|
||||
|
|
|
|||
|
|
@ -5,6 +5,7 @@ import {Packet} from "../../Data/Packet";
|
|||
import {BitStream} from 'bit-buffer';
|
||||
import {GameEventDefinition} from "../../Data/GameEvent";
|
||||
import {Match} from "../../Data/Match";
|
||||
import {readUBitVar} from "../readBitVar";
|
||||
|
||||
enum PVS {
|
||||
PRESERVE = 0,
|
||||
|
|
@ -13,7 +14,7 @@ enum PVS {
|
|||
DELETE = 4
|
||||
}
|
||||
|
||||
function readPVSType(stream: BitStream): number {
|
||||
function readPVSType(stream: BitStream): PVS {
|
||||
// https://github.com/skadistats/smoke/blob/a2954fbe2fa3936d64aee5b5567be294fef228e6/smoke/io/stream/entity.pyx#L24
|
||||
let pvs;
|
||||
const hi = stream.readBoolean();
|
||||
|
|
@ -30,7 +31,7 @@ function readPVSType(stream: BitStream): number {
|
|||
return pvs;
|
||||
}
|
||||
|
||||
function readEnterPVS(stream: BitStream, entityId: number, match: Match, baseLine: number):Entity {
|
||||
function readEnterPVS(stream: BitStream, entityId: number, match: Match, baseLine: number): Entity {
|
||||
// https://github.com/PazerOP/DemoLib/blob/5f9467650f942a4a70f9ec689eadcd3e0a051956/TF2Net/NetMessages/NetPacketEntitiesMessage.cs#L198
|
||||
const serverClass = match.serverClasses[stream.readBits(match.classBits)];
|
||||
console.log(serverClass);
|
||||
|
|
@ -84,7 +85,7 @@ export function PacketEntities(stream: BitStream, events: GameEventDefinition[],
|
|||
const length = stream.readBits(20);
|
||||
const updatedBaseLine = stream.readBoolean();
|
||||
const end = stream._index + length;
|
||||
let entityId = -1
|
||||
let entityId = -1;
|
||||
|
||||
stream._index = end;
|
||||
return {
|
||||
|
|
@ -106,23 +107,25 @@ export function PacketEntities(stream: BitStream, events: GameEventDefinition[],
|
|||
const diff = readUBitVar(stream);
|
||||
entityId += 1 + diff;
|
||||
const pvs = readPVSType(stream);
|
||||
console.log("entity: " + entityId, ", pvs " + PVS[pvs]);
|
||||
if (pvs === PVS.ENTER) {
|
||||
const entity = readEnterPVS(stream, entityId, match, baseLine);
|
||||
applyEntityUpdate(entity, stream);
|
||||
match.entities[entityId] = entity;
|
||||
|
||||
if (updatedBaseLine) {
|
||||
const newBaseLine:SendProp[] = [];
|
||||
const newBaseLine: SendProp[] = [];
|
||||
newBaseLine.concat(entity.props);
|
||||
match.instanceBaselines[baseLine][entityId] = newBaseLine;
|
||||
}
|
||||
entity.inPVS = true;
|
||||
// stream.readBits(1);
|
||||
} else if (pvs === PVS.PRESERVE) {
|
||||
const entity = match.entities[entityId];
|
||||
if (entity) {
|
||||
applyEntityUpdate(entity, stream);
|
||||
} else {
|
||||
console.log(entityId, match.entities.length);
|
||||
console.log( entityId, match.entities.length);
|
||||
throw new Error("unknown entity");
|
||||
}
|
||||
} else {
|
||||
|
|
@ -157,13 +160,13 @@ const readFieldIndex = function (stream: BitStream, lastIndex: number): number {
|
|||
};
|
||||
|
||||
const applyEntityUpdate = function (entity: Entity, stream: BitStream): Entity {
|
||||
let index = -1;
|
||||
const allProps = entity.sendTable.flattenedProps;
|
||||
let index = -1;
|
||||
const allProps = entity.sendTable.flattenedProps;
|
||||
let changedProps: SendProp[] = [];
|
||||
while ((index = readFieldIndex(stream, index)) != -1) {
|
||||
if (index > 4096) {
|
||||
throw new Error('prop index out of bounds');
|
||||
}
|
||||
console.log(index);
|
||||
const propDefinition = allProps[index];
|
||||
const existingProp = entity.getPropByDefinition(propDefinition);
|
||||
let prop;
|
||||
|
|
@ -172,26 +175,18 @@ const applyEntityUpdate = function (entity: Entity, stream: BitStream): Entity {
|
|||
} else {
|
||||
prop = new SendProp(propDefinition);
|
||||
}
|
||||
prop.value = SendPropParser.decode(propDefinition, stream);
|
||||
console.log(prop);
|
||||
// prop.value = SendPropParser.decode(propDefinition, stream);
|
||||
// console.log(prop);
|
||||
changedProps.push(prop);
|
||||
|
||||
if (!existingProp) {
|
||||
entity.props.push(prop);
|
||||
}
|
||||
}
|
||||
for (let i = 0; i < changedProps.length; i++) {
|
||||
const prop = changedProps[i];
|
||||
prop.value = SendPropParser.decode(prop.definition, stream);
|
||||
console.log(prop);
|
||||
}
|
||||
return entity;
|
||||
};
|
||||
|
||||
const readUBitVar = function (stream: BitStream): number {
|
||||
switch (stream.readBits(2)) {
|
||||
case 0:
|
||||
return stream.readBits(4);
|
||||
case 1:
|
||||
return stream.readBits(8);
|
||||
case 2:
|
||||
return stream.readBits(12);
|
||||
case 3:
|
||||
return stream.readBits(32);
|
||||
}
|
||||
throw new Error('Invalid var bit');
|
||||
};
|
||||
|
|
|
|||
|
|
@ -1,32 +1,32 @@
|
|||
import {Parser} from './Parser';
|
||||
|
||||
export function make(name: string, definition: string): Parser {
|
||||
var parts = definition.substr(0, definition.length - 1).split('}');//remove leading } to prevent empty part
|
||||
var items = parts.map(function (part) {
|
||||
const parts = definition.substr(0, definition.length - 1).split('}');//remove leading } to prevent empty part
|
||||
const items = parts.map(function (part) {
|
||||
return part.split('{');
|
||||
});
|
||||
return function (stream) {
|
||||
var result = {
|
||||
let result = {
|
||||
'packetType': name
|
||||
};
|
||||
try {
|
||||
for (var i = 0; i < items.length; i++) {
|
||||
var value = readItem(stream, items[i][1], result);
|
||||
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;
|
||||
}
|
||||
}
|
||||
} catch (e) {
|
||||
throw 'Failed reading pattern ' + definition + '. ' + e;
|
||||
throw new Error('Failed reading pattern ' + definition + '. ' + e);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
const readItem = function (stream, description, data) {
|
||||
var length;
|
||||
let length;
|
||||
if (description[0] === 'b') {
|
||||
return !!stream.readBits(1);
|
||||
return stream.readBoolean();
|
||||
} else if (description[0] === 's') {
|
||||
if (description.length === 1) {
|
||||
return stream.readUTF8String();
|
||||
|
|
@ -40,8 +40,8 @@ const readItem = function (stream, description, data) {
|
|||
length = parseInt(description.substr(1), 10);
|
||||
return stream.readBits(length);
|
||||
} else if (description[0] === '$') {
|
||||
var variable = description.substr(1);
|
||||
return stream.readBits(variable);
|
||||
const variable = description.substr(1);
|
||||
return stream.readBits(data[variable]);
|
||||
} else {
|
||||
return stream.readBits(parseInt(description, 10), true);
|
||||
}
|
||||
|
|
|
|||
26
src/Parser/Packet/TempEntities.ts
Normal file
26
src/Parser/Packet/TempEntities.ts
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
import {Packet} from "../../Data/Packet";
|
||||
import {BitStream} from 'bit-buffer';
|
||||
|
||||
export function TempEntities(stream: BitStream): Packet { // 10: classInfo
|
||||
const entityCount = stream.readBits(8);
|
||||
const length = readVarInt(stream);
|
||||
console.log(length);
|
||||
stream._index += length;
|
||||
|
||||
return {
|
||||
'packetType': 'tempEntities'
|
||||
}
|
||||
}
|
||||
|
||||
function readVarInt(stream: BitStream) {
|
||||
let result = 0;
|
||||
for (let run = 0; run < 35; run += 7) {
|
||||
const byte = stream.readUint8();
|
||||
result |= ((byte & 0x7F) << run);
|
||||
|
||||
if ((byte >> 7) == 0) {
|
||||
return result;
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
|
@ -47,14 +47,14 @@ export class SendPropParser {
|
|||
|
||||
static readArray(propDefinition: SendPropDefinition, stream: BitStream): SendPropArrayValue[] {
|
||||
let maxElements = propDefinition.numElements;
|
||||
let numBits = 1;
|
||||
let numBits = 1;
|
||||
while ((maxElements >>= 1) != 0)
|
||||
numBits++;
|
||||
|
||||
const count = stream.readBits(numBits);
|
||||
const count = stream.readBits(numBits);
|
||||
const values: SendPropArrayValue[] = [];
|
||||
if (!propDefinition.arrayProperty) {
|
||||
throw new Error('Array of undefniend type');
|
||||
throw new Error('Array of undefined type');
|
||||
}
|
||||
for (let i = 0; i < count; i++) {
|
||||
const value = SendPropParser.decode(propDefinition.arrayProperty, stream);
|
||||
|
|
@ -88,24 +88,24 @@ export class SendPropParser {
|
|||
if (propDefinition.hasFlag(SendPropFlag.SPROP_COORD)) {
|
||||
throw new Error("not implemented");
|
||||
} else if (propDefinition.hasFlag(SendPropFlag.SPROP_COORD_MP)) {
|
||||
return SendPropParser.readBitCoord(stream, false, false);
|
||||
return SendPropParser.readBitCoord(propDefinition, stream, false, false);
|
||||
} else if (propDefinition.hasFlag(SendPropFlag.SPROP_COORD_MP_LOWPRECISION)) {
|
||||
return SendPropParser.readBitCoord(stream, false, true);
|
||||
return SendPropParser.readBitCoord(propDefinition, stream, false, true);
|
||||
} else if (propDefinition.hasFlag(SendPropFlag.SPROP_COORD_MP_INTEGRAL)) {
|
||||
return SendPropParser.readBitCoord(stream, true, false);
|
||||
return SendPropParser.readBitCoord(propDefinition, stream, true, false);
|
||||
} else if (propDefinition.hasFlag(SendPropFlag.SPROP_NOSCALE)) {
|
||||
return stream.readFloat32();
|
||||
} else if (propDefinition.hasFlag(SendPropFlag.SPROP_NORMAL)) {
|
||||
throw new Error("not implemented");
|
||||
} else {
|
||||
const raw = stream.readBits(propDefinition.bitCount);
|
||||
const raw = stream.readBits(propDefinition.bitCount);
|
||||
const percentage = raw / ((1 << propDefinition.bitCount) - 1);
|
||||
return propDefinition.lowValue + (propDefinition.highValue - propDefinition.lowValue) * percentage;
|
||||
}
|
||||
}
|
||||
|
||||
static readBitCoord(stream: BitStream, isIntegral: boolean, isLowPrecision: boolean): number {
|
||||
let value = 0;
|
||||
static readBitCoord(propDefinition: SendPropDefinition, stream: BitStream, isIntegral: boolean, isLowPrecision: boolean): number {
|
||||
let value = 0;
|
||||
let isNegative = false;
|
||||
const inBounds = stream.readBoolean();
|
||||
|
||||
|
|
@ -131,6 +131,7 @@ export class SendPropParser {
|
|||
} else {
|
||||
value = stream.readBits(14) + 1;
|
||||
if (value < (1 << 11)) {
|
||||
console.log(propDefinition, value);
|
||||
throw new Error("Something's fishy...");
|
||||
}
|
||||
}
|
||||
|
|
|
|||
16
src/Parser/readBitVar.ts
Normal file
16
src/Parser/readBitVar.ts
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
import {BitStream} from "bit-buffer";
|
||||
export function readBitVar(stream: BitStream, signed?: boolean): number {
|
||||
switch (stream.readBits(2)) {
|
||||
case 0:
|
||||
return stream.readBits(4, signed);
|
||||
case 1:
|
||||
return stream.readBits(8, signed);
|
||||
case 2:
|
||||
return stream.readBits(12, signed);
|
||||
case 3:
|
||||
return stream.readBits(32, signed);
|
||||
}
|
||||
throw new Error('Invalid var bit');
|
||||
}
|
||||
|
||||
export const readUBitVar = readBitVar;
|
||||
Loading…
Add table
Add a link
Reference in a new issue