1
0
Fork 0
mirror of https://github.com/demostf/demo.js synced 2026-06-04 00:54:14 +02:00

add encoder for tempEntities

This commit is contained in:
Robin Appelman 2017-09-06 00:05:44 +02:00
commit 450300c1b0
12 changed files with 667 additions and 68 deletions

View file

@ -0,0 +1,17 @@
import * as assert from 'assert';
import {DynamicBitStream} from '../../DynamicBitStream';
suite('DynamitcBitStream', () => {
test('write grow', () => {
const stream = new DynamicBitStream(2);
stream.writeBits(0, 15);
stream.writeBits(17, 16);
assert.equal(stream.length, 32);
assert.equal(stream.index, 31);
stream.index = 18;
assert.equal(stream.readBits(2), 2);
});
});

View file

@ -1,14 +1,12 @@
import {BitStream} from 'bit-buffer';
import {assertEncoder, assertParser, getStream} from './PacketTest';
import {readFileSync} from 'fs';
import {SendTable} from '../../../../Data/SendTable';
import {encodeEntityUpdate, getEntityUpdate} from '../../../../Parser/EntityDecoder';
import {SendProp, SendPropValue} from '../../../../Data/SendProp';
import {SendPropDefinition, SendPropType} from '../../../../Data/SendPropDefinition';
import {PacketEntity} from '../../../../Data/PacketEntity';
import {Vector} from '../../../../Data/Vector';
import {SendPropType} from '../../../../Data/SendPropDefinition';
import {SendPropEncoder} from '../../../../Parser/SendPropEncoder';
import {SendPropParser} from '../../../../Parser/SendPropParser';
import {hydrateEntity, hydrateTable} from './hydrate';
const data = [
9, 128, 64, 64, 64, 64, 0, 0, 0, 0, 0, 16, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 8, 36, 0, 64, 0, 1, 128, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 64, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 32, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 16, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 136, 0, 128, 0, 0, 8, 0, 128, 0, 0, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 4, 2, 64, 0, 32, 0, 16, 0, 32, 240, 255, 255, 255, 31, 0, 2, 32, 48, 0, 128, 0, 0, 4, 254, 255, 127, 224, 255, 255, 7, 254, 255, 127, 0, 8, 64, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 128, 0, 64, 64, 64, 64, 0, 32, 224, 136, 10, 248, 91, 2, 63, 18, 8, 40, 38, 3, 250, 163, 192, 126, 7, 2, 2, 0, 0, 0, 0, 1, 0, 0, 0, 128, 0, 0, 0, 224, 111, 0, 0, 0, 0, 32, 192, 129, 172, 140, 46, 44, 141, 237, 133, 172, 140, 46, 44, 141, 109, 14, 78, 46, 141, 174, 108, 238, 107, 46, 236, 174, 45, 141, 141, 45, 0];
@ -18,46 +16,15 @@ const sendTableData = JSON.parse(readFileSync(__dirname + '/../../../data/sendTa
const sendTable = hydrateTable(sendTableData);
const entity = hydrateEntity(entityData);
function hydrateEntity(entityData): PacketEntity {
const entity = new PacketEntity(entityData.serverClass, entityData.entityIndex, entityData.pvs);
entity.props = entityData.props.map(propData => {
const prop = new SendProp(propDataDefinition(propData.definition));
if (prop.definition.type === SendPropType.DPT_Vector || prop.definition.type === SendPropType.DPT_VectorXY) {
prop.value = new Vector(propData.value.x, propData.value.y, propData.value.z);
} else {
prop.value = propData.value;
}
return prop;
});
return entity;
}
function propDataDefinition(propData): SendPropDefinition {
const prop = new SendPropDefinition(propData.type, propData.name, propData.flags, propData.ownerTableName);
prop.arrayProperty = propData.arrayProperty;
prop.numElements = propData.numElements;
prop.bitCount = propData.bitCount;
prop.excludeDTName = propData.excludeDTName;
prop.lowValue = propData.lowValue;
prop.highValue = propData.highValue;
prop.table = propData.table ? hydrateTable(propData.table) : null;
return prop;
}
function hydrateTable(tableData): SendTable {
const table = new SendTable(tableData.name);
table.props = tableData.props.map(propDataDefinition);
return table;
}
function decodeUpdate(stream: BitStream) {
export function decodeUpdate(stream: BitStream) {
return getEntityUpdate(sendTable, stream);
}
function encodeUpdate(props: SendProp[], stream: BitStream) {
export function encodeUpdate(props: SendProp[], stream: BitStream) {
encodeEntityUpdate(props, sendTable, stream);
}
function encodeProp(prop: SendProp) {
return function (value: SendPropValue, stream: BitStream) {
return SendPropEncoder.encode(value, prop.definition, stream);

View file

@ -0,0 +1,514 @@
import {BitStream} from 'bit-buffer';
import {assertEncoder, assertParser, getStream} from './PacketTest';
import {EncodeTempEntities, ParseTempEntities} from '../../../../Parser/Packet/TempEntities';
import {Match} from '../../../../Data/Match';
import {hydrateEntity, hydrateTable} from './hydrate';
import {ServerClass} from '../../../../Data/ServerClass';
import {TempEntitiesPacket} from '../../../../Data/Packet';
const data = [
2,
142,
1,
150,
10,
68,
56,
43,
176,
245,
5,
254,
253,
192,
96,
20,
194,
14,
8,
252,
95];
const entityData = [
{
'serverClass': {
'id': 164,
'name': 'CTEPlayerAnimEvent',
'dataTable': 'DT_TEPlayerAnimEvent'
},
'entityIndex': 0,
'props': [
{
'definition': {
'type': 0,
'name': 'm_iPlayerIndex',
'flags': 1,
'excludeDTName': null,
'lowValue': 0,
'highValue': 0,
'bitCount': 7,
'table': null,
'numElements': 0,
'arrayProperty': null,
'ownerTableName': 'DT_TEPlayerAnimEvent'
},
'value': 17
}
],
'inPVS': false,
'pvs': 1,
'delay': 0
},
{
'serverClass': {
'id': 178,
'name': 'CTETFParticleEffect',
'dataTable': 'DT_TETFParticleEffect'
},
'entityIndex': 0,
'props': [
{
'definition': {
'type': 1,
'name': 'm_vecOrigin[0]',
'flags': 32772,
'excludeDTName': null,
'lowValue': 0,
'highValue': 0,
'bitCount': 32,
'table': null,
'numElements': 0,
'arrayProperty': null,
'ownerTableName': 'DT_TETFParticleEffect'
},
'value': 1004
},
{
'definition': {
'type': 1,
'name': 'm_vecOrigin[1]',
'flags': 32772,
'excludeDTName': null,
'lowValue': 0,
'highValue': 0,
'bitCount': 32,
'table': null,
'numElements': 0,
'arrayProperty': null,
'ownerTableName': 'DT_TETFParticleEffect'
},
'value': -2016
},
{
'definition': {
'type': 1,
'name': 'm_vecOrigin[2]',
'flags': 32772,
'excludeDTName': null,
'lowValue': 0,
'highValue': 0,
'bitCount': 32,
'table': null,
'numElements': 0,
'arrayProperty': null,
'ownerTableName': 'DT_TETFParticleEffect'
},
'value': 561
},
{
'definition': {
'type': 0,
'name': 'm_iParticleSystemIndex',
'flags': 1,
'excludeDTName': null,
'lowValue': 0,
'highValue': 0,
'bitCount': 16,
'table': null,
'numElements': 0,
'arrayProperty': null,
'ownerTableName': 'DT_TETFParticleEffect'
},
'value': 472
},
{
'definition': {
'type': 0,
'name': 'entindex',
'flags': 1,
'excludeDTName': null,
'lowValue': 0,
'highValue': 0,
'bitCount': 11,
'table': null,
'numElements': 0,
'arrayProperty': null,
'ownerTableName': 'DT_TETFParticleEffect'
},
'value': 2047
}
],
'inPVS': false,
'pvs': 1,
'delay': 0
}
];
const sendTableData = {
'name': 'DT_TEPlayerAnimEvent',
'props': [
{
'type': 0,
'name': 'm_iPlayerIndex',
'flags': 1,
'excludeDTName': null,
'lowValue': 0,
'highValue': 0,
'bitCount': 7,
'table': null,
'numElements': 0,
'arrayProperty': null,
'ownerTableName': 'DT_TEPlayerAnimEvent'
},
{
'type': 0,
'name': 'm_iEvent',
'flags': 1,
'excludeDTName': null,
'lowValue': 0,
'highValue': 0,
'bitCount': 6,
'table': null,
'numElements': 0,
'arrayProperty': null,
'ownerTableName': 'DT_TEPlayerAnimEvent'
},
{
'type': 0,
'name': 'm_nData',
'flags': 0,
'excludeDTName': null,
'lowValue': 0,
'highValue': 0,
'bitCount': 12,
'table': null,
'numElements': 0,
'arrayProperty': null,
'ownerTableName': 'DT_TEPlayerAnimEvent'
}
],
'cachedFlattenedProps': []
};
const sendTableData2 = {
'name': 'DT_TETFParticleEffect',
'props': [
{
'type': 6,
'name': 'baseclass',
'flags': 4096,
'excludeDTName': null,
'lowValue': 0,
'highValue': 0,
'bitCount': 0,
'table': {
'name': 'DT_BaseTempEntity',
'props': [],
'cachedFlattenedProps': []
},
'numElements': 0,
'arrayProperty': null,
'ownerTableName': 'DT_TETFParticleEffect'
},
{
'type': 1,
'name': 'm_vecOrigin[0]',
'flags': 32772,
'excludeDTName': null,
'lowValue': 0,
'highValue': 0,
'bitCount': 32,
'table': null,
'numElements': 0,
'arrayProperty': null,
'ownerTableName': 'DT_TETFParticleEffect'
},
{
'type': 1,
'name': 'm_vecOrigin[1]',
'flags': 32772,
'excludeDTName': null,
'lowValue': 0,
'highValue': 0,
'bitCount': 32,
'table': null,
'numElements': 0,
'arrayProperty': null,
'ownerTableName': 'DT_TETFParticleEffect'
},
{
'type': 1,
'name': 'm_vecOrigin[2]',
'flags': 32772,
'excludeDTName': null,
'lowValue': 0,
'highValue': 0,
'bitCount': 32,
'table': null,
'numElements': 0,
'arrayProperty': null,
'ownerTableName': 'DT_TETFParticleEffect'
},
{
'type': 1,
'name': 'm_vecStart[0]',
'flags': 32772,
'excludeDTName': null,
'lowValue': 0,
'highValue': 0,
'bitCount': 32,
'table': null,
'numElements': 0,
'arrayProperty': null,
'ownerTableName': 'DT_TETFParticleEffect'
},
{
'type': 1,
'name': 'm_vecStart[1]',
'flags': 32772,
'excludeDTName': null,
'lowValue': 0,
'highValue': 0,
'bitCount': 32,
'table': null,
'numElements': 0,
'arrayProperty': null,
'ownerTableName': 'DT_TETFParticleEffect'
},
{
'type': 1,
'name': 'm_vecStart[2]',
'flags': 32772,
'excludeDTName': null,
'lowValue': 0,
'highValue': 0,
'bitCount': 32,
'table': null,
'numElements': 0,
'arrayProperty': null,
'ownerTableName': 'DT_TETFParticleEffect'
},
{
'type': 2,
'name': 'm_vecAngles',
'flags': 0,
'excludeDTName': null,
'lowValue': 0,
'highValue': 360,
'bitCount': 7,
'table': null,
'numElements': 0,
'arrayProperty': null,
'ownerTableName': 'DT_TETFParticleEffect'
},
{
'type': 0,
'name': 'm_iParticleSystemIndex',
'flags': 1,
'excludeDTName': null,
'lowValue': 0,
'highValue': 0,
'bitCount': 16,
'table': null,
'numElements': 0,
'arrayProperty': null,
'ownerTableName': 'DT_TETFParticleEffect'
},
{
'type': 0,
'name': 'entindex',
'flags': 1,
'excludeDTName': null,
'lowValue': 0,
'highValue': 0,
'bitCount': 11,
'table': null,
'numElements': 0,
'arrayProperty': null,
'ownerTableName': 'DT_TETFParticleEffect'
},
{
'type': 0,
'name': 'm_iAttachType',
'flags': 1,
'excludeDTName': null,
'lowValue': 0,
'highValue': 0,
'bitCount': 5,
'table': null,
'numElements': 0,
'arrayProperty': null,
'ownerTableName': 'DT_TETFParticleEffect'
},
{
'type': 0,
'name': 'm_iAttachmentPointIndex',
'flags': 0,
'excludeDTName': null,
'lowValue': 0,
'highValue': 0,
'bitCount': 32,
'table': null,
'numElements': 0,
'arrayProperty': null,
'ownerTableName': 'DT_TETFParticleEffect'
},
{
'type': 0,
'name': 'm_bResetParticles',
'flags': 1,
'excludeDTName': null,
'lowValue': 0,
'highValue': 0,
'bitCount': 1,
'table': null,
'numElements': 0,
'arrayProperty': null,
'ownerTableName': 'DT_TETFParticleEffect'
},
{
'type': 0,
'name': 'm_bCustomColors',
'flags': 1,
'excludeDTName': null,
'lowValue': 0,
'highValue': 0,
'bitCount': 1,
'table': null,
'numElements': 0,
'arrayProperty': null,
'ownerTableName': 'DT_TETFParticleEffect'
},
{
'type': 2,
'name': 'm_CustomColors.m_vecColor1',
'flags': 0,
'excludeDTName': null,
'lowValue': 0,
'highValue': 1,
'bitCount': 8,
'table': null,
'numElements': 0,
'arrayProperty': null,
'ownerTableName': 'DT_TETFParticleEffect'
},
{
'type': 2,
'name': 'm_CustomColors.m_vecColor2',
'flags': 0,
'excludeDTName': null,
'lowValue': 0,
'highValue': 1,
'bitCount': 8,
'table': null,
'numElements': 0,
'arrayProperty': null,
'ownerTableName': 'DT_TETFParticleEffect'
},
{
'type': 0,
'name': 'm_bControlPoint1',
'flags': 1,
'excludeDTName': null,
'lowValue': 0,
'highValue': 0,
'bitCount': 1,
'table': null,
'numElements': 0,
'arrayProperty': null,
'ownerTableName': 'DT_TETFParticleEffect'
},
{
'type': 0,
'name': 'm_ControlPoint1.m_eParticleAttachment',
'flags': 1,
'excludeDTName': null,
'lowValue': 0,
'highValue': 0,
'bitCount': 5,
'table': null,
'numElements': 0,
'arrayProperty': null,
'ownerTableName': 'DT_TETFParticleEffect'
},
{
'type': 1,
'name': 'm_ControlPoint1.m_vecOffset[0]',
'flags': 8196,
'excludeDTName': null,
'lowValue': 0,
'highValue': 0,
'bitCount': 32,
'table': null,
'numElements': 0,
'arrayProperty': null,
'ownerTableName': 'DT_TETFParticleEffect'
},
{
'type': 1,
'name': 'm_ControlPoint1.m_vecOffset[1]',
'flags': 8196,
'excludeDTName': null,
'lowValue': 0,
'highValue': 0,
'bitCount': 32,
'table': null,
'numElements': 0,
'arrayProperty': null,
'ownerTableName': 'DT_TETFParticleEffect'
},
{
'type': 1,
'name': 'm_ControlPoint1.m_vecOffset[2]',
'flags': 8196,
'excludeDTName': null,
'lowValue': 0,
'highValue': 0,
'bitCount': 32,
'table': null,
'numElements': 0,
'arrayProperty': null,
'ownerTableName': 'DT_TETFParticleEffect'
}
],
'cachedFlattenedProps': []
};
const sendTable = hydrateTable(sendTableData);
const sendTable2 = hydrateTable(sendTableData2);
const expected = {
packetType: 'tempEntities',
entities: entityData.map(hydrateEntity)
};
const match = new Match();
match.serverClasses.length = 348;
match.serverClasses[164] = new ServerClass(164, 'CTEPlayerAnimEvent', 'DT_TEPlayerAnimEvent');
match.serverClasses[178] = new ServerClass(178, 'CTETFParticleEffect', 'DT_TETFParticleEffect');
match.sendTables.set(sendTable.name, sendTable);
match.sendTables.set(sendTable2.name, sendTable2);
function parse(stream: BitStream) {
return ParseTempEntities(stream, match);
}
function encode(value: TempEntitiesPacket, stream: BitStream) {
EncodeTempEntities(value, stream, match);
}
suite('TempEntities', () => {
test('Parse tempEntities', () => {
assertParser(parse, getStream(data), expected, 166);
});
test('Encode tempEntities', () => {
assertEncoder(parse, encode, expected, 166);
});
});

View file

@ -0,0 +1,40 @@
import {SendProp} from '../../../../Data/SendProp';
import {PacketEntity} from '../../../../Data/PacketEntity';
import {SendPropDefinition, SendPropType} from '../../../../Data/SendPropDefinition';
import {Vector} from '../../../../Data/Vector';
import {SendTable} from '../../../../Data/SendTable';
export function hydrateEntity(entityData): PacketEntity {
const entity = new PacketEntity(entityData.serverClass, entityData.entityIndex, entityData.pvs);
entity.props = entityData.props.map(propData => {
const prop = new SendProp(propDataDefinition(propData.definition));
if (prop.definition.type === SendPropType.DPT_Vector || prop.definition.type === SendPropType.DPT_VectorXY) {
prop.value = new Vector(propData.value.x, propData.value.y, propData.value.z);
} else {
prop.value = propData.value;
}
return prop;
});
if (typeof entityData.delay !== 'undefined') {
entity.delay = entityData.delay;
}
return entity;
}
export function propDataDefinition(propData): SendPropDefinition {
const prop = new SendPropDefinition(propData.type, propData.name, propData.flags, propData.ownerTableName);
prop.arrayProperty = propData.arrayProperty;
prop.numElements = propData.numElements;
prop.bitCount = propData.bitCount;
prop.excludeDTName = propData.excludeDTName;
prop.lowValue = propData.lowValue;
prop.highValue = propData.highValue;
prop.table = propData.table ? hydrateTable(propData.table) : null;
return prop;
}
export function hydrateTable(tableData): SendTable {
const table = new SendTable(tableData.name);
table.props = tableData.props.map(propDataDefinition);
return table;
}