mirror of
https://github.com/demostf/demo.js
synced 2026-06-04 00:54:14 +02:00
add encoders for variable length ints
This commit is contained in:
parent
a979cf6b83
commit
79cd277fed
9 changed files with 148 additions and 24 deletions
|
|
@ -32,7 +32,7 @@ export function ParseClassInfo(stream: BitStream): ClassInfoPacket { // 10: clas
|
|||
|
||||
export function EncodeClassInfo(packet: ClassInfoPacket, stream: BitStream) {
|
||||
stream.writeUint16(packet.number);
|
||||
stream.writeBoolean(packet.create ? 1 : 0);
|
||||
stream.writeBoolean(packet.create);
|
||||
if (!packet.create) {
|
||||
const bits = logBase2(packet.number) + 1;
|
||||
for (const entry of packet.entries) {
|
||||
|
|
|
|||
|
|
@ -1,5 +1,6 @@
|
|||
import {Packet} from '../../Data/Packet';
|
||||
import {PacketHandler} from './Parser';
|
||||
import {BitStream} from 'bit-buffer';
|
||||
|
||||
export function make(name: string, definition: string): PacketHandler {
|
||||
const parts = definition.split('}');
|
||||
|
|
@ -31,7 +32,7 @@ export function make(name: string, definition: string): PacketHandler {
|
|||
};
|
||||
}
|
||||
|
||||
function readItem(stream, description, data) {
|
||||
function readItem(stream: BitStream, description: string, data) {
|
||||
if (description[0] === 'b') {
|
||||
return stream.readBoolean();
|
||||
} else if (description[0] === 's') {
|
||||
|
|
@ -54,25 +55,25 @@ function readItem(stream, description, data) {
|
|||
}
|
||||
}
|
||||
|
||||
function writeItem(stream, description, data, value) {
|
||||
function writeItem(stream: BitStream, description: string, data, value: boolean | string | number) {
|
||||
if (description[0] === 'b') {
|
||||
return stream.writeBoolean(value);
|
||||
return stream.writeBoolean(value as boolean);
|
||||
} else if (description[0] === 's') {
|
||||
if (description.length === 1) {
|
||||
return stream.writeUTF8String(value);
|
||||
return stream.writeUTF8String(value as string);
|
||||
} else {
|
||||
const length = parseInt(description.substr(1), 10);
|
||||
return stream.writeUTF8String(value, length);
|
||||
return stream.writeUTF8String(value as string, length);
|
||||
}
|
||||
} else if (description === 'f32') {
|
||||
return stream.writeFloat32(value);
|
||||
return stream.writeFloat32(value as number);
|
||||
} else if (description[0] === 'u') {
|
||||
const length = parseInt(description.substr(1), 10);
|
||||
return stream.writeBits(value, length);
|
||||
return stream.writeBits(value as number, length);
|
||||
} else if (description[0] === '$') {
|
||||
const variable = description.substr(1);
|
||||
return stream.writeBits(value, data[variable]);
|
||||
return stream.writeBits(value as number, data[variable]);
|
||||
} else {
|
||||
return stream.writeBits(value, parseInt(description, 10), true);
|
||||
return stream.writeBits(value as number, parseInt(description, 10));
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,4 +1,15 @@
|
|||
import {BitStream} from 'bit-buffer';
|
||||
import {logBase2} from '../Math';
|
||||
|
||||
function makeUnsigned(value: number, signed?: boolean) {
|
||||
if (signed) {
|
||||
const signBit = value < 0 ? 1 : 0;
|
||||
return ((value ^ -signBit) << 1) + signBit;
|
||||
} else {
|
||||
return value;
|
||||
}
|
||||
}
|
||||
|
||||
export function readBitVar(stream: BitStream, signed?: boolean): number {
|
||||
const type = stream.readBits(2);
|
||||
switch (type) {
|
||||
|
|
@ -14,6 +25,28 @@ export function readBitVar(stream: BitStream, signed?: boolean): number {
|
|||
throw new Error('Invalid var bit');
|
||||
}
|
||||
|
||||
export function writeBitVar(value: number, stream: BitStream, signed?: boolean) {
|
||||
const bitsNeeded = (signed) ? logBase2(Math.abs(value)) + 2 : logBase2(value) + 1;
|
||||
if (signed) {
|
||||
const signBit = value < 0 ? 1 : 0;
|
||||
value = value ^ (-signBit << (bitsNeeded - 1)) + (signBit << (bitsNeeded - 1));
|
||||
}
|
||||
|
||||
if (bitsNeeded > 12) {
|
||||
stream.writeBits(3, 2);
|
||||
stream.writeBits(value, 32);
|
||||
} else if (bitsNeeded > 8) {
|
||||
stream.writeBits(2, 2);
|
||||
stream.writeBits(value, 12);
|
||||
} else if (bitsNeeded > 4) {
|
||||
stream.writeBits(1, 2);
|
||||
stream.writeBits(value, 8);
|
||||
} else {
|
||||
stream.writeBits(0, 2);
|
||||
stream.writeBits(value, 4);
|
||||
}
|
||||
}
|
||||
|
||||
export const readUBitVar = readBitVar;
|
||||
|
||||
export function readVarInt(stream: BitStream, signed: boolean = false) {
|
||||
|
|
@ -33,3 +66,14 @@ export function readVarInt(stream: BitStream, signed: boolean = false) {
|
|||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
export function writeVarInt(value: number, stream: BitStream, signed: boolean = false) {
|
||||
value = makeUnsigned(value, signed);
|
||||
|
||||
do {
|
||||
const byte = value & 0x7F;
|
||||
const resumeBit = (value > 128) ? 0x80 : 0;
|
||||
stream.writeUint8(byte | resumeBit);
|
||||
value = value >> 7;
|
||||
} while (value > 0);
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue