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

dataTable bitcount improvements

This commit is contained in:
Robin Appelman 2017-11-24 22:02:15 +01:00
commit 686f240c05
4 changed files with 37 additions and 13 deletions

View file

@ -23,6 +23,7 @@ export class SendPropDefinition {
public lowValue: number;
public highValue: number;
public bitCount: number;
public originalBitCount: number | null = null;
public table: SendTable | null;
public numElements: number;
public arrayProperty: SendPropDefinition | null;
@ -63,7 +64,7 @@ export class SendPropDefinition {
data.highValue = this.highValue;
}
if (this.type === SendPropType.DPT_DataTable && this.table) {
data.tableName = this.table.name;
data.excludeDTName = this.table.name;
}
return data;

View file

@ -1,7 +1,7 @@
import {BitStream} from 'bit-buffer';
import {DataTablesMessage, MessageHandler, MessageType} from '../../Data/Message';
import {SendPropDefinition, SendPropFlag, SendPropType} from '../../Data/SendPropDefinition';
import {SendTable} from '../../Data/SendTable';
import {SendTable, SendTableName} from '../../Data/SendTable';
import {ServerClass} from '../../Data/ServerClass';
export const DataTableHandler: MessageHandler<DataTablesMessage> = {
@ -15,7 +15,7 @@ export const DataTableHandler: MessageHandler<DataTablesMessage> = {
// 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
const tables: SendTable[] = [];
const tableMap: {[key: string]: SendTable} = {};
const tableMap: Map<SendTableName, SendTable> = new Map();
while (messageStream.readBoolean()) {
const needsDecoder = messageStream.readBoolean();
const tableName = messageStream.readASCIIString();
@ -47,9 +47,11 @@ export const DataTableHandler: MessageHandler<DataTablesMessage> = {
if (prop.hasFlag(SendPropFlag.SPROP_NOSCALE)) {
if (prop.type === SendPropType.DPT_Float) {
prop.originalBitCount = prop.bitCount;
prop.bitCount = 32;
} else if (prop.type === SendPropType.DPT_Vector) {
if (!prop.hasFlag(SendPropFlag.SPROP_NORMAL)) {
prop.originalBitCount = prop.bitCount;
prop.bitCount = 32 * 3;
}
}
@ -76,7 +78,7 @@ export const DataTableHandler: MessageHandler<DataTablesMessage> = {
}
}
tables.push(table);
tableMap[table.name] = table;
tableMap.set(table.name, table);
}
// link referenced tables
@ -84,7 +86,11 @@ export const DataTableHandler: MessageHandler<DataTablesMessage> = {
for (const prop of table.props) {
if (prop.type === SendPropType.DPT_DataTable) {
if (prop.excludeDTName) {
prop.table = tableMap[prop.excludeDTName];
const referencedTable = tableMap.get(prop.excludeDTName);
if (!referencedTable) {
throw new Error(`Unknown referenced table ${prop.excludeDTName}`);
}
prop.table = referencedTable;
prop.excludeDTName = null;
}
}
@ -192,13 +198,23 @@ function encodeSendPropDefinition(stream: BitStream, definition: SendPropDefinit
} else {
stream.writeFloat32(definition.lowValue);
stream.writeFloat32(definition.highValue);
if (definition.hasFlag(SendPropFlag.SPROP_NOSCALE) &&
(
definition.type == SendPropType.DPT_Float ||
(definition.type == SendPropType.DPT_Vector && !definition.hasFlag(SendPropFlag.SPROP_NORMAL))
// if (definition.originalBitCount === null || typeof definition.originalBitCount === 'undefined') {
// stream.writeBits(definition.bitCount, 7);
// } else {
// stream.writeBits(definition.originalBitCount, 7);
// }
if (
definition.hasFlag(SendPropFlag.SPROP_NOSCALE) && (
definition.type === SendPropType.DPT_Float ||
(definition.type === SendPropType.DPT_Vector && !definition.hasFlag(SendPropFlag.SPROP_NORMAL))
)
) {
stream.writeBits(0, 7);
if (definition.originalBitCount === null || typeof definition.originalBitCount === 'undefined') {
stream.writeBits(definition.bitCount / 3, 7);
} else {
stream.writeBits(definition.originalBitCount, 7);
}
} else {
stream.writeBits(definition.bitCount, 7);
}

View file

@ -1,13 +1,13 @@
import * as assert from 'assert';
import {BitStream} from 'bit-buffer';
import {readFileSync} from 'fs';
import {readFileSync, writeFileSync} from 'fs';
import {gunzipSync} from 'zlib';
import {DataTablesMessage} from '../../../../Data/Message';
import {ParserState} from '../../../../Data/ParserState';
import {ServerClass} from '../../../../Data/ServerClass';
import {DataTableHandler} from '../../../../Parser/Message/DataTable';
import {hydrateTable} from '../Packet/hydrate';
import {assertEncoder, assertParser, getStream} from '../Packet/PacketTest';
import {assertEncoder, assertParser, assertReEncode, getStream} from '../Packet/PacketTest';
const data = Array.from(readFileSync(__dirname + '/../../../data/dataTableData.bin').values());
const expectedRaw = JSON.parse(
@ -46,6 +46,9 @@ suite('DataTable', () => {
});
test('Encode DataTable message', () => {
const source = getStream(data);
const expected = parser(source);
const length = 947888;
const stream = new BitStream(new ArrayBuffer(length + 64000));
@ -64,10 +67,14 @@ suite('DataTable', () => {
assert.deepEqual(result.tick, expected.tick, 'Re-decoded value not equal to original value');
assert.deepEqual(result.type, expected.type, 'Re-decoded value not equal to original value');
for (let i = 0; i < result.tables.length; i++) {
const resultTable = JSON.parse(JSON.stringify(result.tables[i]));
const resultTable = result.tables[i];
const expectedTable = expectedRaw.tables[i];
assert.deepEqual(resultTable, expectedTable, 'Re-decoded value not equal to original value');
}
assert.equal(stream.index, pos, 'Number of bits used for encoding and parsing not equal');
});
test('Re-encode DataTable message', () => {
assertReEncode(parser, encoder, getStream(data));
});
});