BitReader¶
Low-level bit-stream primitives for reading LSB-first bits, varints, and Dota-specific coordinate/angle types.
See also: The .dem Format
gem.reader.BitReader
¶
Reads bits and structured values from a byte buffer in LSB-first order.
The internal bit buffer accumulates bytes on demand, consuming them in least-significant-bit order. Byte-aligned reads use fast paths via struct.unpack to avoid per-bit overhead.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
buf
|
bytes
|
The raw bytes to read from. |
required |
Source code in src/gem/reader.py
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 | |
read_bits(n: int) -> int
¶
Read n bits from the buffer in LSB-first order.
Refills the bit buffer in 4-byte chunks using struct.unpack_from when possible to reduce Python loop iterations.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n
|
int
|
Number of bits to read (0 ≤ n ≤ 32). |
required |
Returns:
| Name | Type | Description |
|---|---|---|
int |
int
|
The unsigned integer value of the n bits read. |
Raises:
| Type | Description |
|---|---|
BufferError
|
If the buffer is exhausted before n bits are read. |
Source code in src/gem/reader.py
read_boolean() -> bool
¶
Read a single bit as a boolean.
Returns:
| Name | Type | Description |
|---|---|---|
bool |
bool
|
True if the bit is 1, False if 0. |
Source code in src/gem/reader.py
read_bytes(n: int) -> bytes
¶
Read exactly n bytes from the buffer.
Uses a zero-copy slice when bit-aligned, otherwise reads byte by byte.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n
|
int
|
Number of bytes to read. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
bytes |
bytes
|
The n bytes read. |
Raises:
| Type | Description |
|---|---|
BufferError
|
If fewer than n bytes remain. |
Source code in src/gem/reader.py
read_bits_as_bytes(n: int) -> bytes
¶
Read n bits, returning them packed into bytes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n
|
int
|
Number of bits to read (need not be a multiple of 8). |
required |
Returns:
| Name | Type | Description |
|---|---|---|
bytes |
bytes
|
The bits packed into ceil(n/8) bytes. |
Source code in src/gem/reader.py
read_le_uint32() -> int
¶
Read a little-endian unsigned 32-bit integer.
Returns:
| Name | Type | Description |
|---|---|---|
int |
int
|
The decoded uint32 value. |
read_le_uint64() -> int
¶
Read a little-endian unsigned 64-bit integer.
Returns:
| Name | Type | Description |
|---|---|---|
int |
int
|
The decoded uint64 value. |
read_varuint32() -> int
¶
Read an unsigned 32-bit variable-length integer.
Uses a continuation-bit scheme: the low 7 bits of each byte contribute to the value; the high bit signals more bytes follow. Stops after 5 bytes (35 bits) to prevent overflow.
Returns:
| Name | Type | Description |
|---|---|---|
int |
int
|
The decoded unsigned 32-bit integer. |
Raises:
| Type | Description |
|---|---|
BufferError
|
If the buffer is exhausted mid-varint. |
Source code in src/gem/reader.py
read_varint32() -> int
¶
Read a signed 32-bit variable-length integer using zigzag encoding.
Zigzag maps: 0→0, -1→1, 1→2, -2→3, 2→4, …
Returns:
| Name | Type | Description |
|---|---|---|
int |
int
|
The decoded signed 32-bit integer. |
Source code in src/gem/reader.py
read_varuint64() -> int
¶
Read an unsigned 64-bit variable-length integer.
Returns:
| Name | Type | Description |
|---|---|---|
int |
int
|
The decoded unsigned 64-bit integer. |
Raises:
| Type | Description |
|---|---|
BufferError
|
If the buffer is exhausted mid-varint. |
OverflowError
|
If the encoded value exceeds uint64 range. |
Source code in src/gem/reader.py
read_varint64() -> int
¶
Read a signed 64-bit variable-length integer using zigzag encoding.
Returns:
| Name | Type | Description |
|---|---|---|
int |
int
|
The decoded signed 64-bit integer. |
read_ubit_var() -> int
¶
Read a variable-length uint32 using a 6-bit group with 2-bit size hint.
The top 2 bits of the initial 6-bit read encode how many more bits follow for the value: - 0b00 → no extension (value fits in low 4 bits) - 0b01 → 4 more bits - 0b10 → 8 more bits - 0b11 → 28 more bits
Returns:
| Name | Type | Description |
|---|---|---|
int |
int
|
The decoded unsigned integer. |
Source code in src/gem/reader.py
read_ubit_var_fp() -> int
¶
Read a variable-length uint32 using field-path encoding.
Reads progressively more bits until a terminating condition, choosing among 2, 4, 10, 17, or 31-bit encodings.
Returns:
| Name | Type | Description |
|---|---|---|
int |
int
|
The decoded unsigned integer. |
Source code in src/gem/reader.py
read_float() -> float
¶
Read an IEEE 754 single-precision float (little-endian).
Returns:
| Name | Type | Description |
|---|---|---|
float |
float
|
The decoded float32 value. |
read_coord() -> float
¶
Read a Source Engine network coordinate as a float.
Coordinates are encoded as integer + fractional parts with a sign bit. An integer part of n is stored as (n - 1), giving a range of 1–16384. The fractional part provides 1/32 precision over 5 bits.
Returns:
| Name | Type | Description |
|---|---|---|
float |
float
|
The decoded coordinate value. |
Source code in src/gem/reader.py
read_angle(n: int) -> float
¶
Read a bit-encoded angle of n bits, mapping to [0, 360) degrees.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n
|
int
|
Bit width of the encoded angle. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
float |
float
|
The angle in degrees. |
Source code in src/gem/reader.py
read_normal() -> float
¶
Read a normalised float in the range [-1, 1] using 12 bits.
Encoded as a sign bit followed by an 11-bit magnitude.
Returns:
| Name | Type | Description |
|---|---|---|
float |
float
|
The normalised float value. |
Source code in src/gem/reader.py
read_3bit_normal() -> list[float]
¶
Read a 3-component normal vector using compressed encoding.
X and Y are each optionally present (1-bit flags), Z is derived from the constraint |v|=1. A final sign bit negates Z if set.
Returns:
| Type | Description |
|---|---|
list[float]
|
list[float]: A [x, y, z] unit vector. |
Source code in src/gem/reader.py
read_string() -> str
¶
Read a null-terminated UTF-8 string.
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
The decoded string, without the null terminator. |
Source code in src/gem/reader.py
read_string_n(n: int) -> str
¶
Read exactly n bytes and return them as a string.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n
|
int
|
Number of bytes to read. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
The decoded string (may contain null bytes). |
Source code in src/gem/reader.py
peek_bits(n: int) -> int
¶
Read n bits without advancing the position.
Refills the internal bit buffer exactly as read_bits does, so the reader is left in a state where a subsequent skip_bits(n) or read_bits(n) will consume the same bits.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n
|
int
|
Number of bits to peek (0 ≤ n ≤ 32). |
required |
Returns:
| Name | Type | Description |
|---|---|---|
int |
int
|
The unsigned integer value of the next n bits. |
Raises:
| Type | Description |
|---|---|
BufferError
|
If fewer than n bits remain. |
Source code in src/gem/reader.py
skip_bits(n: int) -> None
¶
Discard n bits that have already been loaded into the bit buffer.
Must only be called after a peek_bits(n) that has already refilled the buffer. Does not refill — callers must ensure n ≤ _bit_count.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n
|
int
|
Number of bits to skip. |
required |
Source code in src/gem/reader.py
rem_bits() -> int
¶
Return the number of unread bits remaining in the buffer.
Returns:
| Name | Type | Description |
|---|---|---|
int |
int
|
Remaining bits count. |
position() -> str
¶
Return a human-readable position string for debugging.
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
Position as 'byte' or 'byte.bit_offset'. |