mirror of
https://github.com/demostf/demo.js
synced 2026-06-04 09:04:13 +02:00
encoder for createStringTable
This commit is contained in:
parent
79cd277fed
commit
89b9c3b25c
10 changed files with 276 additions and 77 deletions
|
|
@ -1,27 +1,26 @@
|
|||
import {BitStream} from 'bit-buffer';
|
||||
import {StringTablePacket} from '../../Data/Packet';
|
||||
import {CreateStringTablePacket} from '../../Data/Packet';
|
||||
import {logBase2} from '../../Math';
|
||||
import {readVarInt} from '../readBitVar';
|
||||
import {readVarInt, writeVarInt} from '../readBitVar';
|
||||
|
||||
import {uncompress} from 'snappyjs';
|
||||
import {Match} from '../../Data/Match';
|
||||
import {StringTable} from '../../Data/StringTable';
|
||||
import {parseStringTable} from '../StringTableParser';
|
||||
import {encodeStringTableEntries, guessStringTableEntryLength, parseStringTableEntries} from '../StringTableParser';
|
||||
|
||||
export function ParseCreateStringTable(stream: BitStream, match: Match): StringTablePacket { // 12: createStringTable
|
||||
const tableName = stream.readASCIIString();
|
||||
const maxEntries = stream.readUint16();
|
||||
const encodeBits = logBase2(maxEntries);
|
||||
export function ParseCreateStringTable(stream: BitStream): CreateStringTablePacket { // 12: createStringTable
|
||||
const tableName = stream.readASCIIString();
|
||||
const maxEntries = stream.readUint16();
|
||||
const encodeBits = logBase2(maxEntries);
|
||||
const entityCount = stream.readBits(encodeBits + 1);
|
||||
|
||||
const bitCount = readVarInt(stream);
|
||||
|
||||
let userDataSize = 0;
|
||||
let userDataSize = 0;
|
||||
let userDataSizeBits = 0;
|
||||
|
||||
// userdata fixed size
|
||||
if (stream.readBoolean()) {
|
||||
userDataSize = stream.readBits(12);
|
||||
userDataSize = stream.readBits(12);
|
||||
userDataSizeBits = stream.readBits(4);
|
||||
}
|
||||
|
||||
|
|
@ -31,7 +30,7 @@ export function ParseCreateStringTable(stream: BitStream, match: Match): StringT
|
|||
|
||||
if (isCompressed) {
|
||||
const decompressedByteSize = data.readUint32();
|
||||
const compressedByteSize = data.readUint32();
|
||||
const compressedByteSize = data.readUint32();
|
||||
|
||||
const magic = data.readASCIIString(4);
|
||||
|
||||
|
|
@ -50,17 +49,45 @@ export function ParseCreateStringTable(stream: BitStream, match: Match): StringT
|
|||
}
|
||||
|
||||
const table: StringTable = {
|
||||
name: tableName,
|
||||
entries: [],
|
||||
name: tableName,
|
||||
entries: [],
|
||||
maxEntries,
|
||||
fixedUserDataSize: userDataSize,
|
||||
fixedUserDataSize: userDataSize,
|
||||
fixedUserDataSizeBits: userDataSizeBits,
|
||||
};
|
||||
|
||||
parseStringTable(data, table, entityCount, match);
|
||||
table.entries = parseStringTableEntries(data, table, entityCount);
|
||||
|
||||
return {
|
||||
packetType: 'stringTable',
|
||||
tables: [table],
|
||||
packetType: 'createStringTable',
|
||||
table: table,
|
||||
};
|
||||
}
|
||||
|
||||
export function EncodeCreateStringTable(packet: CreateStringTablePacket, stream: BitStream) {
|
||||
stream.writeASCIIString(packet.table.name);
|
||||
stream.writeUint16(packet.table.maxEntries);
|
||||
const encodeBits = logBase2(packet.table.maxEntries);
|
||||
stream.writeBits(packet.table.entries.length, encodeBits + 1);
|
||||
|
||||
const entryData = new BitStream(new ArrayBuffer(guessStringTableEntryLength(packet.table)));
|
||||
encodeStringTableEntries(entryData, packet.table);
|
||||
|
||||
const entryLength = entryData.index;
|
||||
entryData.index = 0;
|
||||
|
||||
writeVarInt(entryLength, stream);
|
||||
|
||||
if (packet.table.fixedUserDataSize && packet.table.fixedUserDataSizeBits) {
|
||||
stream.writeBoolean(true);
|
||||
stream.writeBits(packet.table.fixedUserDataSize, 12);
|
||||
stream.writeBits(packet.table.fixedUserDataSizeBits, 4);
|
||||
} else {
|
||||
stream.writeBoolean(false);
|
||||
}
|
||||
|
||||
// we never compress table data
|
||||
stream.writeBoolean(false);
|
||||
|
||||
stream.writeBitStream(entryData, entryLength);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,26 +1,32 @@
|
|||
import {BitStream} from 'bit-buffer';
|
||||
import {Match} from '../../Data/Match';
|
||||
import {StringTablePacket} from '../../Data/Packet';
|
||||
import {parseStringTable} from '../StringTableParser';
|
||||
import {UpdateStringTablePacket} from '../../Data/Packet';
|
||||
import {parseStringTableEntries} from '../StringTableParser';
|
||||
|
||||
export function ParseUpdateStringTable(stream: BitStream, match: Match): StringTablePacket { // 12: updateStringTable
|
||||
export function ParseUpdateStringTable(stream: BitStream, match: Match): UpdateStringTablePacket { // 12: updateStringTable
|
||||
const tableId = stream.readBits(5);
|
||||
|
||||
const multipleChanged = stream.readBoolean();
|
||||
const changedEntries = (multipleChanged) ? stream.readBits(16) : 1;
|
||||
const changedEntries = (multipleChanged) ? stream.readBits(16) : 1;
|
||||
|
||||
const bitCount = stream.readBits(20);
|
||||
const data = stream.readBitStream(bitCount);
|
||||
const data = stream.readBitStream(bitCount);
|
||||
|
||||
if (!match.stringTables[tableId]) {
|
||||
throw new Error('Table not found for update');
|
||||
}
|
||||
|
||||
const table = match.stringTables[tableId];
|
||||
parseStringTable(data, table, changedEntries, match);
|
||||
const updatedEntries = parseStringTableEntries(data, table, changedEntries, table.entries);
|
||||
|
||||
for (let i = 0; i < updatedEntries.length; i++) {
|
||||
if (updatedEntries[i]) {
|
||||
table.entries[i] = updatedEntries[i];
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
packetType: 'stringTable',
|
||||
tables: [table],
|
||||
packetType: 'updateStringTable',
|
||||
table: table,
|
||||
};
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue