mirror of
https://github.com/demostf/demo.js
synced 2026-06-04 00:54:14 +02:00
Better encoding of stringtable updates
This commit is contained in:
parent
9eaf826814
commit
3bf5111108
3 changed files with 19 additions and 11 deletions
|
|
@ -1,7 +1,7 @@
|
|||
import {BitStream} from 'bit-buffer';
|
||||
import {UpdateStringTablePacket} from '../../Data/Packet';
|
||||
import {ParserState} from '../../Data/ParserState';
|
||||
import {encodeStringTableEntries, guessStringTableEntryLength, parseStringTableEntries} from '../StringTableParser';
|
||||
import {encodeStringTableEntries, parseStringTableEntries} from '../StringTableParser';
|
||||
|
||||
export function ParseUpdateStringTable(stream: BitStream, state: ParserState): UpdateStringTablePacket { // 12: updateStringTable
|
||||
const tableId = stream.readBits(5);
|
||||
|
|
@ -41,13 +41,17 @@ export function EncodeUpdateStringTable(packet: UpdateStringTablePacket, stream:
|
|||
if (!state.stringTables[packet.tableId]) {
|
||||
throw new Error(`Table not found for update: ${packet.tableId}`);
|
||||
}
|
||||
|
||||
const lengthStart = stream.index;
|
||||
stream.index += 20;
|
||||
const lengthEnd = stream.index;
|
||||
|
||||
const table = state.stringTables[packet.tableId];
|
||||
const entryData = new BitStream(new ArrayBuffer(guessStringTableEntryLength(table, packet.entries)));
|
||||
encodeStringTableEntries(entryData, table, packet.entries);
|
||||
|
||||
const entryLength = entryData.index;
|
||||
entryData.index = 0;
|
||||
encodeStringTableEntries(stream, table, packet.entries, table.entries);
|
||||
|
||||
const dataEnd = stream.index;
|
||||
stream.index = lengthStart;
|
||||
const entryLength = dataEnd - lengthEnd;
|
||||
stream.writeBits(entryLength, 20);
|
||||
stream.writeBitStream(entryData, entryLength);
|
||||
stream.index = dataEnd;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -95,7 +95,7 @@ export function guessStringTableEntryLength(table: StringTable, entries: StringT
|
|||
}, 1);
|
||||
}
|
||||
|
||||
export function encodeStringTableEntries(stream: BitStream, table: StringTable, entries: StringTableEntry[]) {
|
||||
export function encodeStringTableEntries(stream: BitStream, table: StringTable, entries: StringTableEntry[], oldEntries: StringTableEntry[] = []) {
|
||||
const entryBits = logBase2(table.maxEntries);
|
||||
const lastIndex = -1;
|
||||
for (let i = 0; i < entries.length; i++) {
|
||||
|
|
@ -108,7 +108,7 @@ export function encodeStringTableEntries(stream: BitStream, table: StringTable,
|
|||
stream.writeBoolean(true);
|
||||
}
|
||||
|
||||
if (typeof entry.text !== 'undefined') {
|
||||
if (typeof entry.text !== 'undefined' && !(oldEntries[i] && entry.text === oldEntries[i].text)) {
|
||||
stream.writeBoolean(true);
|
||||
// we don't encode substring optimizations
|
||||
stream.writeBoolean(false);
|
||||
|
|
|
|||
|
|
@ -3,7 +3,7 @@ import {UpdateStringTablePacket} from '../../../../Data/Packet';
|
|||
import {createParserState} from '../../../../Data/ParserState';
|
||||
import {StringTable} from '../../../../Data/StringTable';
|
||||
import {EncodeUpdateStringTable, ParseUpdateStringTable} from '../../../../Parser/Packet/UpdateStringTable';
|
||||
import {assertEncoder, assertParser, getStream} from './PacketTest';
|
||||
import {assertEncoder, assertParser, assertReEncode, getStream} from './PacketTest';
|
||||
|
||||
const exampleData = [200, 3, 0, 48, 130, 53];
|
||||
|
||||
|
|
@ -61,7 +61,11 @@ suite('UpdateStringTable', () => {
|
|||
});
|
||||
|
||||
test('Encode updateStringTable', () => {
|
||||
assertEncoder(ParseUpdate, EncodeUpdate, examplePacket, 266);
|
||||
assertEncoder(ParseUpdate, EncodeUpdate, examplePacket, 41);
|
||||
assertEncoder(ParseUpdate, EncodeUpdate, examplePacket2, 299);
|
||||
});
|
||||
|
||||
test('Re-encode updateStringTable', () => {
|
||||
assertReEncode(ParseUpdate, EncodeUpdate, getStream(exampleData));
|
||||
});
|
||||
});
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue