mirror of
https://github.com/demostf/demo.js
synced 2026-06-04 00:54:14 +02:00
More precise baseline handling wip
This commit is contained in:
parent
f0c7460007
commit
357b725994
10 changed files with 90 additions and 53 deletions
|
|
@ -23,6 +23,7 @@ import {Chat} from './Chat';
|
|||
import {Packet} from './Packet';
|
||||
import {GameEventType} from './GameEventTypes';
|
||||
import {ParserState} from './ParserState';
|
||||
import {SendProp} from './SendProp';
|
||||
|
||||
export class Match implements ParserState {
|
||||
public tick: number = 0;
|
||||
|
|
@ -33,6 +34,7 @@ export class Match implements ParserState {
|
|||
public startTick: number = 0;
|
||||
public intervalPerTick: number = 0;
|
||||
public staticBaseLines: Map<ServerClassId, BitStream> = new Map();
|
||||
public staticBaselineCache: Map<ServerClassId, SendProp[]> = new Map();
|
||||
public eventDefinitions: Map<number, GameEventDefinition<GameEventType>> = new Map();
|
||||
public world: World = {
|
||||
boundaryMin: {x: 0, y: 0, z: 0},
|
||||
|
|
@ -41,7 +43,7 @@ export class Match implements ParserState {
|
|||
public playerEntityMap: Map<EntityId, Player> = new Map();
|
||||
public entityClasses: Map<EntityId, ServerClass> = new Map();
|
||||
public sendTables: Map<SendTableName, SendTable> = new Map();
|
||||
public baseLineCache: Map<ServerClass, PacketEntity> = new Map();
|
||||
public instanceBaselines: [Map<EntityId, SendProp[]>, Map<EntityId, SendProp[]>] = [new Map(), new Map()];
|
||||
public weaponMap: Map<EntityId, Weapon> = new Map();
|
||||
public outerMap: Map<number, EntityId> = new Map();
|
||||
public teams: Map<TeamNumber, Team> = new Map();
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
import {SendProp} from './SendProp';
|
||||
import {SendProp, SendPropValue} from './SendProp';
|
||||
import {SendPropDefinition} from './SendPropDefinition';
|
||||
import {ServerClass} from './ServerClass';
|
||||
|
||||
|
|
@ -28,15 +28,19 @@ export class PacketEntity {
|
|||
this.pvs = pvs;
|
||||
}
|
||||
|
||||
public getPropByDefinition(definition: SendPropDefinition) {
|
||||
for (const prop of this.props) {
|
||||
if (prop.definition.fullName === definition.fullName) {
|
||||
private static getPropByFullName(props: SendProp[], fullName: string): SendProp | null {
|
||||
for (const prop of props) {
|
||||
if (prop.definition.fullName === fullName) {
|
||||
return prop;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public getPropByDefinition(definition: SendPropDefinition) {
|
||||
return PacketEntity.getPropByFullName(this.props, definition.fullName);
|
||||
}
|
||||
|
||||
public getProperty(originTable: string, name: string) {
|
||||
for (const prop of this.props) {
|
||||
if (prop.definition.ownerTableName === originTable && prop.definition.name === name) {
|
||||
|
|
@ -51,6 +55,9 @@ export class PacketEntity {
|
|||
for (const prop of this.props) {
|
||||
result.props.push(prop.clone());
|
||||
}
|
||||
result.serialNumber = this.serialNumber;
|
||||
result.delay = this.delay;
|
||||
result.inPVS = this.inPVS;
|
||||
return result;
|
||||
}
|
||||
|
||||
|
|
@ -65,10 +72,15 @@ export class PacketEntity {
|
|||
}
|
||||
}
|
||||
|
||||
public diffFromBaseLine(baseline: PacketEntity): SendProp[] {
|
||||
public diffFromBaseLine(baselineProps: SendProp[]): SendProp[] {
|
||||
return this.props.filter(prop => {
|
||||
const baseProp = baseline.getPropByDefinition(prop.definition);
|
||||
const baseProp = PacketEntity.getPropByFullName(baselineProps, prop.definition.fullName);
|
||||
return (!baseProp || prop.value !== baseProp.value);
|
||||
});
|
||||
}
|
||||
|
||||
public getPropValue(fullName: string): SendPropValue | null {
|
||||
const prop = PacketEntity.getPropByFullName(this.props, fullName);
|
||||
return prop ? prop.value : null;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -5,16 +5,18 @@ import {SendTable, SendTableName} from './SendTable';
|
|||
import {ServerClass, ServerClassId} from './ServerClass';
|
||||
import {StringTable} from './StringTable';
|
||||
import {GameEventType} from './GameEventTypes';
|
||||
import {SendProp} from './SendProp';
|
||||
|
||||
export interface ParserState {
|
||||
staticBaseLines: Map<ServerClassId, BitStream>;
|
||||
staticBaselineCache: Map<ServerClassId, SendProp[]>;
|
||||
eventDefinitions: Map<number, GameEventDefinition<GameEventType>>;
|
||||
entityClasses: Map<EntityId, ServerClass>;
|
||||
sendTables: Map<SendTableName, SendTable>;
|
||||
version: number;
|
||||
stringTables: StringTable[];
|
||||
serverClasses: ServerClass[];
|
||||
baseLineCache: Map<ServerClass, PacketEntity>;
|
||||
instanceBaselines: [Map<EntityId, SendProp[]>, Map<EntityId, SendProp[]>];
|
||||
}
|
||||
|
||||
export function getClassBits(state: ParserState) {
|
||||
|
|
@ -32,13 +34,14 @@ export function getSendTable(state: ParserState, dataTable: string): SendTable {
|
|||
export function createParserState(): ParserState {
|
||||
return {
|
||||
staticBaseLines: new Map(),
|
||||
staticBaselineCache: new Map(),
|
||||
eventDefinitions: new Map(),
|
||||
entityClasses: new Map(),
|
||||
sendTables: new Map(),
|
||||
version: 0,
|
||||
stringTables: [],
|
||||
serverClasses: [],
|
||||
baseLineCache: new Map()
|
||||
instanceBaselines: [new Map(), new Map()]
|
||||
};
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -60,7 +60,9 @@ function saveUserData(userData: StringTableEntry, match: Match) {
|
|||
|
||||
function saveInstanceBaseLine(entry: StringTableEntry, match: Match) {
|
||||
if (entry.extraData) {
|
||||
match.staticBaseLines.set(parseInt(entry.text, 10), entry.extraData);
|
||||
const serverClassId = parseInt(entry.text, 10);
|
||||
match.staticBaselineCache.delete(serverClassId);
|
||||
match.staticBaseLines.set(serverClassId, entry.extraData);
|
||||
} else {
|
||||
throw new Error('Missing baseline');
|
||||
}
|
||||
|
|
|
|||
|
|
@ -35,7 +35,7 @@ export function encodeEntityUpdate(props: SendProp[], sendTable: SendTable, stre
|
|||
}
|
||||
|
||||
if (index < lastIndex) {
|
||||
throw new Error(`Property index not incremental while encoding ${prop.definition.fullName} in ${sendTable.name} (current: ${index}, last: ${lastIndex})`);
|
||||
throw new Error(`Property index not incremental while encoding ${prop.definition.fullName} after ${allProps[lastIndex].fullName} in ${sendTable.name} (current: ${index}, last: ${lastIndex})`);
|
||||
}
|
||||
writeFieldIndex(index, stream, lastIndex);
|
||||
lastIndex = index;
|
||||
|
|
|
|||
|
|
@ -34,7 +34,7 @@ function writePVSType(pvs: PVS, stream: BitStream) {
|
|||
stream.writeBits(raw, 2);
|
||||
}
|
||||
|
||||
function readEnterPVS(stream: BitStream, entityId: EntityId, state: ParserState): PacketEntity {
|
||||
function readEnterPVS(stream: BitStream, entityId: EntityId, state: ParserState, baseLine: number): PacketEntity {
|
||||
// https://github.com/PazerOP/DemoLib/blob/5f9467650f942a4a70f9ec689eadcd3e0a051956/TF2Net/NetMessages/NetPacketEntitiesMessage.cs#L198
|
||||
const classBits = getClassBits(state);
|
||||
const serverClass = state.serverClasses[stream.readBits(classBits)];
|
||||
|
|
@ -42,31 +42,36 @@ function readEnterPVS(stream: BitStream, entityId: EntityId, state: ParserState)
|
|||
|
||||
const sendTable = getSendTable(state, serverClass.dataTable);
|
||||
|
||||
const cachedBaseLine = state.baseLineCache.get(serverClass);
|
||||
if (cachedBaseLine) {
|
||||
const result = cachedBaseLine.clone();
|
||||
result.entityIndex = entityId;
|
||||
result.serialNumber = serial;
|
||||
const instanceBaseline = state.instanceBaselines[baseLine].get(entityId);
|
||||
const entity = new PacketEntity(serverClass, entityId, PVS.ENTER);
|
||||
entity.serialNumber = serial;
|
||||
if (instanceBaseline) {
|
||||
const result = entity.clone();
|
||||
result.applyPropUpdate(instanceBaseline);
|
||||
return result;
|
||||
} else {
|
||||
const entity = new PacketEntity(serverClass, entityId, PVS.ENTER);
|
||||
const staticBaseLine = state.staticBaseLines.get(serverClass.id);
|
||||
if (staticBaseLine) {
|
||||
staticBaseLine.index = 0;
|
||||
const props = getEntityUpdate(sendTable, staticBaseLine);
|
||||
entity.applyPropUpdate(props);
|
||||
state.baseLineCache.set(serverClass, entity.clone());
|
||||
const cachedBaseline = state.staticBaselineCache.get(serverClass.id);
|
||||
if (cachedBaseline) {
|
||||
entity.applyPropUpdate(cachedBaseline);
|
||||
} else {
|
||||
staticBaseLine.index = 0;
|
||||
const props = getEntityUpdate(sendTable, staticBaseLine);
|
||||
entity.applyPropUpdate(props);
|
||||
// TODO: cache
|
||||
// state.staticBaselineCache.set(serverClass.id, props.map(prop => prop.clone()));
|
||||
}
|
||||
// if (staticBaseLine.bitsLeft > 7) {
|
||||
// console.log(staticBaseLine.length, staticBaseLine.index);
|
||||
// throw new Error('Unexpected data left at the end of staticBaseline, ' + staticBaseLine.bitsLeft + ' bits left');
|
||||
// }
|
||||
}
|
||||
entity.serialNumber = serial;
|
||||
return entity;
|
||||
}
|
||||
}
|
||||
|
||||
function writeEnterPVS(entity: PacketEntity, stream: BitStream, state: ParserState) {
|
||||
function writeEnterPVS(entity: PacketEntity, stream: BitStream, state: ParserState, baseLine: number) {
|
||||
const serverClassId = state.serverClasses.findIndex(serverClass => serverClass && entity.serverClass.id === serverClass.id);
|
||||
if (serverClassId === -1) {
|
||||
throw new Error(`Unknown server class ${entity.serverClass.name}(${entity.serverClass.id})`);
|
||||
|
|
@ -79,19 +84,23 @@ function writeEnterPVS(entity: PacketEntity, stream: BitStream, state: ParserSta
|
|||
|
||||
const sendTable = getSendTable(state, serverClass.dataTable);
|
||||
|
||||
let cachedBaseLine = state.baseLineCache.get(serverClass);
|
||||
if (!cachedBaseLine) {
|
||||
let instanceBaseLine = state.instanceBaselines[baseLine].get(entity.entityIndex);
|
||||
if (!instanceBaseLine) {
|
||||
const staticBaseLine = state.staticBaseLines.get(serverClass.id);
|
||||
if (staticBaseLine) {
|
||||
cachedBaseLine = new PacketEntity(serverClass, entity.entityIndex, PVS.ENTER);
|
||||
staticBaseLine.index = 0;
|
||||
const props = getEntityUpdate(sendTable, staticBaseLine);
|
||||
cachedBaseLine.applyPropUpdate(props);
|
||||
state.baseLineCache.set(serverClass, cachedBaseLine.clone());
|
||||
instanceBaseLine = getEntityUpdate(sendTable, staticBaseLine);
|
||||
// state.instanceBaselines.set(serverClass, instanceBaseLine.clone());
|
||||
}
|
||||
}
|
||||
|
||||
const propsToEncode = cachedBaseLine ? entity.diffFromBaseLine(cachedBaseLine) : entity.props;
|
||||
const propsToEncode = instanceBaseLine ? entity.diffFromBaseLine(instanceBaseLine) : entity.props;
|
||||
|
||||
// console.log(propsToEncode.map(prop => `${prop.definition.name}: ${prop.value}`));
|
||||
|
||||
const allProps = sendTable.flattenedProps;
|
||||
propsToEncode.sort((a, b) => allProps.findIndex(propDef => propDef.fullName === a.definition.fullName) -
|
||||
allProps.findIndex(propDef => propDef.fullName === b.definition.fullName));
|
||||
|
||||
encodeEntityUpdate(propsToEncode, sendTable, stream);
|
||||
}
|
||||
|
|
@ -126,22 +135,24 @@ export function ParsePacketEntities(stream: BitStream, state: ParserState, skip:
|
|||
const receivedEntities: PacketEntity[] = [];
|
||||
const removedEntityIds: EntityId[] = [];
|
||||
|
||||
if (true) {
|
||||
if (!skip) {
|
||||
if (updatedBaseLine) {
|
||||
state.instanceBaselines[1 - baseLine] = new Map(state.instanceBaselines[baseLine]);
|
||||
// state.instanceBaselines[baseLine] = new Map();
|
||||
}
|
||||
|
||||
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, state);
|
||||
const packetEntity = readEnterPVS(stream, entityId, state, baseLine);
|
||||
const sendTable = getSendTable(state, packetEntity.serverClass.dataTable);
|
||||
const updatedProps = getEntityUpdate(sendTable, stream);
|
||||
packetEntity.applyPropUpdate(updatedProps);
|
||||
|
||||
if (updatedBaseLine) {
|
||||
// console.log('updated baseline', packetEntity.serverClass.name);
|
||||
const newBaseLine: SendProp[] = [];
|
||||
newBaseLine.concat(packetEntity.props);
|
||||
state.baseLineCache.set(packetEntity.serverClass, packetEntity.clone());
|
||||
state.instanceBaselines[1 - baseLine].set(entityId, packetEntity.clone().props);
|
||||
}
|
||||
packetEntity.inPVS = true;
|
||||
receivedEntities.push(packetEntity);
|
||||
|
|
@ -199,6 +210,11 @@ export function EncodePacketEntities(packet: PacketEntitiesPacket, stream: BitSt
|
|||
|
||||
let lastEntityId = -1;
|
||||
|
||||
if (packet.updatedBaseLine) {
|
||||
state.instanceBaselines[1 - packet.baseLine] = new Map(state.instanceBaselines[packet.baseLine]);
|
||||
// state.instanceBaselines[baseLine] = new Map();
|
||||
}
|
||||
|
||||
for (const entity of packet.entities) {
|
||||
const diff = entity.entityIndex - lastEntityId;
|
||||
lastEntityId = entity.entityIndex;
|
||||
|
|
@ -206,7 +222,11 @@ export function EncodePacketEntities(packet: PacketEntitiesPacket, stream: BitSt
|
|||
writePVSType(entity.pvs, stream);
|
||||
|
||||
if (entity.pvs === PVS.ENTER) {
|
||||
writeEnterPVS(entity, stream, state);
|
||||
if (packet.updatedBaseLine) {
|
||||
state.instanceBaselines[1 - packet.baseLine].set(entity.entityIndex, entity.clone().props);
|
||||
}
|
||||
|
||||
writeEnterPVS(entity, stream, state, packet.baseLine);
|
||||
} else if (entity.pvs === PVS.PRESERVE) {
|
||||
const sendTable = getSendTable(state, entity.serverClass.dataTable);
|
||||
encodeEntityUpdate(entity.props, sendTable, stream);
|
||||
|
|
|
|||
Binary file not shown.
|
|
@ -38,7 +38,6 @@ const definition2 = propDataDefinition({
|
|||
|
||||
suite('PacketEntity', () => {
|
||||
test('baseLine diff', () => {
|
||||
const baseLine = new PacketEntity(serverClass, 123, PVS.ENTER);
|
||||
const entity = new PacketEntity(serverClass, 123, PVS.ENTER);
|
||||
const prop1 = new SendProp(definition1);
|
||||
prop1.value = 0.03125023842039809;
|
||||
|
|
@ -46,16 +45,15 @@ suite('PacketEntity', () => {
|
|||
prop2.value = 0;
|
||||
const prop3 = new SendProp(definition2);
|
||||
prop3.value = 0.03125023842039809;
|
||||
baseLine.props = [prop1, prop2];
|
||||
entity.props = [prop1, prop3];
|
||||
|
||||
assert.deepEqual(entity.diffFromBaseLine(baseLine), [prop3]);
|
||||
assert.deepEqual(entity.diffFromBaseLine([prop1, prop2]), [prop3]);
|
||||
});
|
||||
|
||||
test('baseLine diff player', () => {
|
||||
const baseLine = hydrateEntity(playerBaseLineData);
|
||||
const entity = hydrateEntity(playerEntityData);
|
||||
|
||||
assert.deepEqual(entity.diffFromBaseLine(baseLine).length, 75);
|
||||
assert.deepEqual(entity.diffFromBaseLine(baseLine.props).length, 75);
|
||||
});
|
||||
});
|
||||
|
|
|
|||
|
|
@ -148,13 +148,13 @@ suite('PacketEntities', () => {
|
|||
// }
|
||||
// });
|
||||
|
||||
// test('Encode packetEntities', () => {
|
||||
// const toEncode = {...expected};
|
||||
// const entity = toEncode.entities[1];
|
||||
// // entity.props = [entity.props[0]];
|
||||
// toEncode.entities = [entity];
|
||||
// assertEncoder(parse, encode, toEncode, 11266);
|
||||
// });
|
||||
test('Encode packetEntities', () => {
|
||||
const toEncode = {...expected};
|
||||
// entity.props.map(prop => console.log(`${prop.definition.fullName}: ${prop.value}`));
|
||||
// entity.props = [entity.props[0]];
|
||||
toEncode.entities = toEncode.entities.slice(281, 282);
|
||||
assertEncoder(parse, encode, toEncode, 0);
|
||||
});
|
||||
|
||||
|
||||
test('Encode small packetEntities', () => {
|
||||
|
|
@ -166,7 +166,7 @@ suite('PacketEntities', () => {
|
|||
delta: 0,
|
||||
maxEntries: 16,
|
||||
entities: [hydrateEntity(sunEntityData)]
|
||||
}, 202);
|
||||
}, 124);
|
||||
});
|
||||
|
||||
test('Encode small packetEntities with removed', () => {
|
||||
|
|
@ -178,7 +178,7 @@ suite('PacketEntities', () => {
|
|||
delta: 0,
|
||||
maxEntries: 16,
|
||||
entities: [hydrateEntity(sunEntityData)]
|
||||
}, 259);
|
||||
}, 181);
|
||||
});
|
||||
|
||||
test('Encode packetEntities only removed', () => {
|
||||
|
|
@ -204,7 +204,7 @@ suite('PacketEntities', () => {
|
|||
delta: 0,
|
||||
maxEntries: 16,
|
||||
entities: [hydrateEntity(sunEntityData), hydrateEntity(secondEntity)]
|
||||
}, 351);
|
||||
}, 195);
|
||||
});
|
||||
|
||||
test('Encode player packetEntities', () => {
|
||||
|
|
|
|||
|
|
@ -17,7 +17,7 @@ export function getStream(data: string | number[]) {
|
|||
export type Encoder = (data: any, stream: BitStream) => void;
|
||||
|
||||
export function assertEncoder(parser: Parser, encoder: Encoder, data: any, length: number = 0, message: string = '') {
|
||||
const stream = new BitStream(new ArrayBuffer(Math.max(64000, (length + 1) * 8)));
|
||||
const stream = new BitStream(new ArrayBuffer(length + 64000));
|
||||
|
||||
encoder(data as Packet, stream);
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue