mirror of
https://github.com/demostf/demo.js
synced 2026-06-03 16:44:12 +02:00
fix hydrate array
This commit is contained in:
parent
36b0444e7d
commit
fb00dc6ea1
3 changed files with 55 additions and 33 deletions
|
|
@ -11,32 +11,32 @@ export class SendPropEncoder {
|
|||
switch (propDefinition.type) {
|
||||
case SendPropType.DPT_Int:
|
||||
if (typeof value !== 'number') {
|
||||
throw new Error('Invalid value for DPT_Int');
|
||||
throw new Error(`Invalid value for DPT_Int ${JSON.stringify(value)}`);
|
||||
}
|
||||
return SendPropEncoder.writeInt(value, propDefinition, stream);
|
||||
case SendPropType.DPT_Vector:
|
||||
if (!(value instanceof Vector)) {
|
||||
throw new Error('Invalid value for DPT_Vector');
|
||||
throw new Error(`Invalid value for DPT_Vector ${JSON.stringify(value)}`);
|
||||
}
|
||||
return SendPropEncoder.writeVector(value, propDefinition, stream);
|
||||
return SendPropEncoder.writeVector(value as Vector, propDefinition, stream);
|
||||
case SendPropType.DPT_VectorXY:
|
||||
if (!(value instanceof Vector)) {
|
||||
throw new Error('Invalid value for DPT_Vector');
|
||||
throw new Error(`Invalid value for DPT_VectorXY ${JSON.stringify(value)}`);
|
||||
}
|
||||
return SendPropEncoder.writeVectorXY(value, propDefinition, stream);
|
||||
case SendPropType.DPT_Float:
|
||||
if (typeof value !== 'number') {
|
||||
throw new Error('Invalid value for DPT_Int');
|
||||
throw new Error(`Invalid value for DPT_Float ${JSON.stringify(value)}`);
|
||||
}
|
||||
return SendPropEncoder.writeFloat(value, propDefinition, stream);
|
||||
case SendPropType.DPT_String:
|
||||
if (typeof value !== 'string') {
|
||||
throw new Error('Invalid value for DPT_Int');
|
||||
throw new Error(`Invalid value for DPT_String ${JSON.stringify(value)}`);
|
||||
}
|
||||
return SendPropEncoder.writeString(value, stream);
|
||||
case SendPropType.DPT_Array:
|
||||
if (!Array.isArray(value)) {
|
||||
throw new Error('Invalid value for DPT_Int');
|
||||
throw new Error(`Invalid value for DPT_Array ${JSON.stringify(value)}`);
|
||||
}
|
||||
return SendPropEncoder.writeArray(value, propDefinition, stream);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -119,30 +119,30 @@ const sunEntityData = {
|
|||
};
|
||||
|
||||
suite('PacketEntities', () => {
|
||||
// test('Parse packetEntities', () => {
|
||||
// const length = 130435;
|
||||
// const stream = getStream(data);
|
||||
// const start = stream.index;
|
||||
// const resultPacket = parse(stream);
|
||||
// assert.equal(stream.index - start, length, 'Unexpected number of bits consumed from stream');
|
||||
//
|
||||
// for (let i = 0; i < resultPacket.entities.length; i++) {
|
||||
// const resultEntity = resultPacket.entities[i];
|
||||
// const expectedEntity = expected.entities[i];
|
||||
// if (!deepEqual(resultEntity, expectedEntity)) {
|
||||
// for (let i = 0; i < expectedEntity.props.length; i++) {
|
||||
// assert.deepEqual(resultEntity.props[i], expectedEntity.props[i], `invalid property #${i} for ${resultEntity.serverClass.name}`);
|
||||
// }
|
||||
// assert.equal(resultEntity.props.length, expectedEntity.props.length, `Unexpected number of props for ${resultEntity.serverClass.name}`);
|
||||
// assert(false, 'Invalid entity ' + resultEntity.serverClass.name);
|
||||
// }
|
||||
// }
|
||||
// });
|
||||
test('Parse packetEntities', () => {
|
||||
const length = 130435;
|
||||
const stream = getStream(data);
|
||||
const start = stream.index;
|
||||
const resultPacket = parse(stream);
|
||||
assert.equal(stream.index - start, length, 'Unexpected number of bits consumed from stream');
|
||||
|
||||
for (let i = 0; i < resultPacket.entities.length; i++) {
|
||||
const resultEntity = resultPacket.entities[i];
|
||||
const expectedEntity = expected.entities[i];
|
||||
if (!deepEqual(resultEntity, expectedEntity)) {
|
||||
for (let i = 0; i < expectedEntity.props.length; i++) {
|
||||
assert.deepEqual(resultEntity.props[i], expectedEntity.props[i], `invalid property #${i} for ${resultEntity.serverClass.name}`);
|
||||
}
|
||||
assert.equal(resultEntity.props.length, expectedEntity.props.length, `Unexpected number of props for ${resultEntity.serverClass.name}`);
|
||||
assert(false, 'Invalid entity ' + resultEntity.serverClass.name);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
// test('Encode packetEntities', () => {
|
||||
// assertEncoder(parse, encode, expected, Math.ceil(data.length / 8));
|
||||
// });
|
||||
//
|
||||
|
||||
|
||||
test('Encode small packetEntities', () => {
|
||||
assertEncoder(parse, encode, {
|
||||
|
|
@ -167,4 +167,16 @@ suite('PacketEntities', () => {
|
|||
entities: [hydrateEntity(sunEntityData)]
|
||||
}, 259);
|
||||
});
|
||||
|
||||
test('Encode packetEntities only removed', () => {
|
||||
assertEncoder(parse, encode, {
|
||||
packetType: 'packetEntities',
|
||||
removedEntities: [10, 11],
|
||||
updatedBaseLine: false,
|
||||
baseLine: 0,
|
||||
delta: 0,
|
||||
maxEntries: 16,
|
||||
entities: []
|
||||
}, 102);
|
||||
});
|
||||
});
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
import {SendProp} from '../../../../Data/SendProp';
|
||||
import {SendProp, SendPropValue} from '../../../../Data/SendProp';
|
||||
import {PacketEntity} from '../../../../Data/PacketEntity';
|
||||
import {SendPropDefinition, SendPropType} from '../../../../Data/SendPropDefinition';
|
||||
import {Vector} from '../../../../Data/Vector';
|
||||
|
|
@ -8,11 +8,7 @@ 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;
|
||||
}
|
||||
prop.value = hydrateProp(propData.value, prop.definition);
|
||||
return prop;
|
||||
});
|
||||
entity.inPVS = entityData.inPVS;
|
||||
|
|
@ -25,6 +21,20 @@ export function hydrateEntity(entityData): PacketEntity {
|
|||
return entity;
|
||||
}
|
||||
|
||||
function hydrateProp(value: any, definition: SendPropDefinition): SendPropValue {
|
||||
if (Array.isArray(value)) {
|
||||
const arrayProp = definition.arrayProperty;
|
||||
if (arrayProp === null) {
|
||||
throw new Error('arrayProperty not set for array property');
|
||||
}
|
||||
return value.map(arrayValue => hydrateProp(arrayValue, arrayProp)) as SendPropValue;
|
||||
} else if (definition.type === SendPropType.DPT_Vector || definition.type === SendPropType.DPT_VectorXY) {
|
||||
return new Vector(value.x, value.y, value.z);
|
||||
} else {
|
||||
return value;
|
||||
}
|
||||
}
|
||||
|
||||
export function propDataDefinition(propData): SendPropDefinition {
|
||||
const prop = new SendPropDefinition(propData.type, propData.name, propData.flags, propData.ownerTableName);
|
||||
prop.arrayProperty = propData.arrayProperty ? propDataDefinition(propData.arrayProperty) : null;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue