mirror of
https://github.com/demostf/demo.js
synced 2026-06-04 00:54:14 +02:00
propper skipping of TempEntities
This commit is contained in:
parent
3b805be013
commit
1fbb61f252
11 changed files with 255 additions and 163 deletions
|
|
@ -17,6 +17,7 @@ export class Match {
|
|||
sendTables: SendTable[];
|
||||
instanceBaselines: SendProp[][][];
|
||||
staticBaseLines: any[];
|
||||
_classBits: number = 0
|
||||
|
||||
constructor() {
|
||||
this.tick = 0;
|
||||
|
|
|
|||
|
|
@ -10,8 +10,9 @@ export class SendPropDefinition {
|
|||
table: SendTable|null;
|
||||
numElements: number|null;
|
||||
arrayProperty: SendPropDefinition|null;
|
||||
ownerTableName: string;
|
||||
|
||||
constructor(type, name, flags) {
|
||||
constructor(type, name, flags, ownerTableName) {
|
||||
this.type = type;
|
||||
this.name = name;
|
||||
this.flags = flags;
|
||||
|
|
@ -22,6 +23,7 @@ export class SendPropDefinition {
|
|||
this.table = null;
|
||||
this.numElements = null;
|
||||
this.arrayProperty = null;
|
||||
this.ownerTableName = ownerTableName;
|
||||
}
|
||||
|
||||
hasFlag(flag: SendPropFlag) {
|
||||
|
|
@ -33,11 +35,22 @@ export class SendPropDefinition {
|
|||
}
|
||||
|
||||
inspect() {
|
||||
return {
|
||||
let data: any = {
|
||||
fromTable: this.ownerTableName,
|
||||
name: this.name,
|
||||
type: SendPropType[this.type],
|
||||
flags: SendPropDefinition.formatFlags(this.flags)
|
||||
flags: SendPropDefinition.formatFlags(this.flags),
|
||||
bitCount: this.bitCount
|
||||
};
|
||||
if (this.type === SendPropType.DPT_Float) {
|
||||
data.lowValue = this.lowValue;
|
||||
data.highValue = this.highValue;
|
||||
}
|
||||
if (this.type === SendPropType.DPT_DataTable && this.table) {
|
||||
data.tableName = this.table.name;
|
||||
}
|
||||
|
||||
return data;
|
||||
}
|
||||
|
||||
static formatFlags(flags: number) {
|
||||
|
|
|
|||
|
|
@ -16,7 +16,7 @@ export class SendTable {
|
|||
}
|
||||
|
||||
flatten() {
|
||||
let excludes = [];
|
||||
let excludes: SendPropDefinition[] = [];
|
||||
let props: SendPropDefinition[] = [];
|
||||
this.getAllProps(excludes, props);
|
||||
|
||||
|
|
@ -35,7 +35,7 @@ export class SendTable {
|
|||
this._flattenedProps = props;
|
||||
}
|
||||
|
||||
getAllProps(excludes: SendTable[], props: SendPropDefinition[]) {
|
||||
getAllProps(excludes: SendPropDefinition[], props: SendPropDefinition[]) {
|
||||
let localProps = [];
|
||||
this.getAllPropsIteratorProps(excludes, localProps, props);
|
||||
for (let i = 0; i < localProps.length; i++) {
|
||||
|
|
@ -43,19 +43,24 @@ export class SendTable {
|
|||
}
|
||||
}
|
||||
|
||||
getAllPropsIteratorProps(excludes: SendTable[], props: SendPropDefinition[], childProps: SendPropDefinition[]) {
|
||||
getAllPropsIteratorProps(excludes: SendPropDefinition[], props: SendPropDefinition[], childProps: SendPropDefinition[]) {
|
||||
for (let i = 0; i < this.props.length; i++) {
|
||||
const prop = this.props[i];
|
||||
if (prop.hasFlag(SendPropFlag.SPROP_EXCLUDE) || excludes.indexOf(prop) !== -1) {
|
||||
continue;
|
||||
}
|
||||
if (excludes.filter((exclude) => {
|
||||
return prop.table && exclude.name == prop.name && exclude.excludeDTName == prop.table.name;
|
||||
}).length > 0) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (prop.type === SendPropType.DPT_DataTable && prop.table) {
|
||||
if (prop.hasFlag(SendPropFlag.SPROP_EXCLUDE)) {
|
||||
excludes.push(prop.table);
|
||||
} else if (excludes.indexOf(this) === -1) {
|
||||
if (prop.hasFlag(SendPropFlag.SPROP_COLLAPSIBLE)) {
|
||||
prop.table.getAllPropsIteratorProps(excludes, props, childProps);
|
||||
} else {
|
||||
prop.table.getAllProps(excludes, childProps);
|
||||
}
|
||||
}
|
||||
} else if (!prop.hasFlag(SendPropFlag.SPROP_EXCLUDE)) {
|
||||
props.push(prop);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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 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;
|
||||
|
|
@ -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();
|
||||
|
|
@ -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,6 +107,7 @@ 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);
|
||||
|
|
@ -117,6 +119,7 @@ export function PacketEntities(stream: BitStream, events: GameEventDefinition[],
|
|||
match.instanceBaselines[baseLine][entityId] = newBaseLine;
|
||||
}
|
||||
entity.inPVS = true;
|
||||
// stream.readBits(1);
|
||||
} else if (pvs === PVS.PRESERVE) {
|
||||
const entity = match.entities[entityId];
|
||||
if (entity) {
|
||||
|
|
@ -159,11 +162,11 @@ 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 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;
|
||||
}
|
||||
|
|
@ -54,7 +54,7 @@ export class SendPropParser {
|
|||
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,11 +88,11 @@ 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)) {
|
||||
|
|
@ -104,7 +104,7 @@ export class SendPropParser {
|
|||
}
|
||||
}
|
||||
|
||||
static readBitCoord(stream: BitStream, isIntegral: boolean, isLowPrecision: boolean): number {
|
||||
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