mirror of
https://github.com/demostf/demo.js
synced 2026-06-04 00:54:14 +02:00
add entity update encoder
This commit is contained in:
parent
7b9131b7fe
commit
c064439b4e
11 changed files with 1786 additions and 43 deletions
|
|
@ -1,39 +1,54 @@
|
|||
import {BitStream} from 'bit-buffer';
|
||||
import {PacketEntity} from '../Data/PacketEntity';
|
||||
import {SendProp} from '../Data/SendProp';
|
||||
import {SendTable} from '../Data/SendTable';
|
||||
import {readUBitVar} from './readBitVar';
|
||||
import {readBitVar, writeBitVar} from './readBitVar';
|
||||
import {SendPropParser} from './SendPropParser';
|
||||
import {SendPropEncoder} from './SendPropEncoder';
|
||||
|
||||
export function applyEntityUpdate(entity: PacketEntity, sendTable: SendTable, stream: BitStream): PacketEntity {
|
||||
export function getEntityUpdate(sendTable: SendTable, stream: BitStream): SendProp[] {
|
||||
let index = -1;
|
||||
const allProps = sendTable.flattenedProps;
|
||||
index = readFieldIndex(stream, index);
|
||||
while (index !== -1) {
|
||||
const props: SendProp[] = [];
|
||||
while (stream.readBoolean()) {
|
||||
index = readFieldIndex(stream, index);
|
||||
if (index >= 4096 || index > allProps.length) {
|
||||
throw new Error('prop index out of bounds while applying update for ' + sendTable.name + ' got ' + index
|
||||
+ ' property only has ' + allProps.length + ' properties');
|
||||
}
|
||||
|
||||
const propDefinition = allProps[index];
|
||||
const existingProp = entity.getPropByDefinition(propDefinition);
|
||||
|
||||
const prop = existingProp ? existingProp : new SendProp(propDefinition);
|
||||
const prop = new SendProp(propDefinition);
|
||||
prop.value = SendPropParser.decode(propDefinition, stream);
|
||||
|
||||
if (!existingProp) {
|
||||
entity.props.push(prop);
|
||||
}
|
||||
|
||||
index = readFieldIndex(stream, index);
|
||||
props.push(prop);
|
||||
}
|
||||
return entity;
|
||||
return props;
|
||||
}
|
||||
|
||||
export function encodeEntityUpdate(props: SendProp[], sendTable: SendTable, stream: BitStream) {
|
||||
const allProps = sendTable.flattenedProps;
|
||||
let lastIndex = -1;
|
||||
for (const prop of props) {
|
||||
stream.writeBoolean(true);
|
||||
const index = allProps.findIndex(propDef => propDef.fullName === prop.definition.fullName);
|
||||
if (index === -1) {
|
||||
throw new Error('Unknown definition for property');
|
||||
}
|
||||
writeFieldIndex(index, stream, lastIndex);
|
||||
lastIndex = index;
|
||||
|
||||
if (prop.value !== null) {
|
||||
SendPropEncoder.encode(prop.value, prop.definition, stream);
|
||||
}
|
||||
}
|
||||
stream.writeBoolean(false);
|
||||
}
|
||||
|
||||
function readFieldIndex(stream: BitStream, lastIndex: number): number {
|
||||
if (!stream.readBoolean()) {
|
||||
return -1;
|
||||
}
|
||||
const diff = readUBitVar(stream);
|
||||
const diff = readBitVar(stream);
|
||||
return lastIndex + diff + 1;
|
||||
}
|
||||
|
||||
function writeFieldIndex(index: number, stream: BitStream, lastIndex: number) {
|
||||
const diff = index - lastIndex - 1;
|
||||
writeBitVar(diff, stream);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -3,7 +3,7 @@ import {Match} from '../../Data/Match';
|
|||
import {PacketEntitiesPacket} from '../../Data/Packet';
|
||||
import {EntityId, PacketEntity, PVS} from '../../Data/PacketEntity';
|
||||
import {SendProp} from '../../Data/SendProp';
|
||||
import {applyEntityUpdate} from '../EntityDecoder';
|
||||
import {getEntityUpdate} from '../EntityDecoder';
|
||||
import {readUBitVar} from '../readBitVar';
|
||||
|
||||
const pvsMap = {
|
||||
|
|
@ -39,7 +39,7 @@ function readEnterPVS(stream: BitStream, entityId: EntityId, match: Match): Pack
|
|||
const staticBaseLine = match.staticBaseLines[serverClass.id];
|
||||
if (staticBaseLine) {
|
||||
staticBaseLine.index = 0;
|
||||
applyEntityUpdate(entity, sendTable, staticBaseLine);
|
||||
entity.props = getEntityUpdate(sendTable, staticBaseLine);
|
||||
match.baseLineCache.set(serverClass, entity.clone());
|
||||
// if (staticBaseLine.bitsLeft > 7) {
|
||||
// console.log(staticBaseLine.length, staticBaseLine.index);
|
||||
|
|
@ -85,7 +85,8 @@ export function ParsePacketEntities(stream: BitStream, match: Match, skip: boole
|
|||
const pvs = readPVSType(stream);
|
||||
if (pvs === PVS.ENTER) {
|
||||
const packetEntity = readEnterPVS(stream, entityId, match);
|
||||
applyEntityUpdate(packetEntity, match.getSendTable(packetEntity.serverClass.dataTable), stream);
|
||||
const updatedProps = getEntityUpdate(match.getSendTable(packetEntity.serverClass.dataTable), stream);
|
||||
packetEntity.applyPropUpdate(updatedProps);
|
||||
|
||||
if (updatedBaseLine) {
|
||||
const newBaseLine: SendProp[] = [];
|
||||
|
|
@ -96,7 +97,8 @@ export function ParsePacketEntities(stream: BitStream, match: Match, skip: boole
|
|||
receivedEntities.push(packetEntity);
|
||||
} else if (pvs === PVS.PRESERVE) {
|
||||
const packetEntity = getPacketEntityForExisting(entityId, match, pvs);
|
||||
applyEntityUpdate(packetEntity, match.getSendTable(packetEntity.serverClass.dataTable), stream);
|
||||
const updatedProps = getEntityUpdate(match.getSendTable(packetEntity.serverClass.dataTable), stream);
|
||||
packetEntity.applyPropUpdate(updatedProps);
|
||||
receivedEntities.push(packetEntity);
|
||||
} else if (match.entityClasses.has(entityId)) {
|
||||
const packetEntity = getPacketEntityForExisting(entityId, match, pvs);
|
||||
|
|
|
|||
|
|
@ -2,7 +2,8 @@ import {BitStream} from 'bit-buffer';
|
|||
import {Match} from '../../Data/Match';
|
||||
import {TempEntitiesPacket} from '../../Data/Packet';
|
||||
import {PacketEntity, PVS} from '../../Data/PacketEntity';
|
||||
import {applyEntityUpdate} from '../EntityDecoder';
|
||||
import {getEntityUpdate} from '../EntityDecoder';
|
||||
import {readVarInt} from '../readBitVar';
|
||||
|
||||
export function ParseTempEntities(stream: BitStream, match: Match, skip: boolean = false): TempEntitiesPacket { // 10: classInfo
|
||||
const entityCount = stream.readBits(8);
|
||||
|
|
@ -23,11 +24,12 @@ export function ParseTempEntities(stream: BitStream, match: Match, skip: boolean
|
|||
const sendTable = match.getSendTable(serverClass.dataTable);
|
||||
entity = new PacketEntity(serverClass, 0, PVS.ENTER);
|
||||
entity.delay = delay;
|
||||
applyEntityUpdate(entity, sendTable, stream);
|
||||
entity.props = getEntityUpdate(sendTable, stream);
|
||||
entities.push(entity);
|
||||
} else {
|
||||
if (entity) {
|
||||
applyEntityUpdate(entity, match.getSendTable(entity.serverClass.dataTable), stream);
|
||||
const updatedProps = getEntityUpdate(match.getSendTable(entity.serverClass.dataTable), stream);
|
||||
entity.applyPropUpdate(updatedProps);
|
||||
} else {
|
||||
throw new Error('no entity set to update');
|
||||
}
|
||||
|
|
@ -44,16 +46,3 @@ export function ParseTempEntities(stream: BitStream, match: Match, skip: boolean
|
|||
entities,
|
||||
};
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue