mirror of
https://codeberg.org/demostf/parser.git
synced 2026-10-01 08:44:05 +02:00
feature gate write support, use hash to compare game event definitions
This commit is contained in:
parent
6ef2b1cea8
commit
fbaca025b6
32 changed files with 6168 additions and 4363 deletions
|
|
@ -1,6 +1,6 @@
|
|||
use bitbuffer::{
|
||||
BitError, BitReadBuffer, BitReadStream, BitWrite, BitWriteSized, BitWriteStream, LittleEndian,
|
||||
};
|
||||
#[cfg(feature = "write")]
|
||||
use bitbuffer::{BitError, BitWrite, BitWriteSized, BitWriteStream};
|
||||
use bitbuffer::{BitReadBuffer, BitReadStream, LittleEndian};
|
||||
use num_traits::{PrimInt, Unsigned};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use snap::raw::{decompress_len, Decoder};
|
||||
|
|
@ -9,7 +9,9 @@ use crate::demo::lzss::decompress;
|
|||
use crate::demo::packet::stringtable::{
|
||||
ExtraData, FixedUserDataSize, StringTable, StringTableEntry,
|
||||
};
|
||||
use crate::demo::parser::{Encode, ParseBitSkip};
|
||||
#[cfg(feature = "write")]
|
||||
use crate::demo::parser::Encode;
|
||||
use crate::demo::parser::ParseBitSkip;
|
||||
use crate::{Parse, ParseError, ParserState, ReadResult, Result, Stream};
|
||||
use std::borrow::Cow;
|
||||
use std::cmp::min;
|
||||
|
|
@ -157,6 +159,7 @@ impl<'a> ParseBitSkip<'a> for CreateStringTableMessage<'a> {
|
|||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "write")]
|
||||
impl Encode for CreateStringTableMessage<'_> {
|
||||
fn encode(
|
||||
&self,
|
||||
|
|
@ -193,6 +196,7 @@ impl Encode for CreateStringTableMessage<'_> {
|
|||
}
|
||||
|
||||
#[test]
|
||||
#[cfg(feature = "write")]
|
||||
fn test_create_string_table_roundtrip() {
|
||||
let state = ParserState::new(24, |_| false, false);
|
||||
crate::test_roundtrip_encode(
|
||||
|
|
@ -274,6 +278,7 @@ impl<'a> ParseBitSkip<'a> for UpdateStringTableMessage<'a> {
|
|||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "write")]
|
||||
impl Encode for UpdateStringTableMessage<'_> {
|
||||
fn encode(&self, stream: &mut BitWriteStream<LittleEndian>, state: &ParserState) -> Result<()> {
|
||||
self.table_id.write_sized(stream, 5)?;
|
||||
|
|
@ -294,6 +299,7 @@ impl Encode for UpdateStringTableMessage<'_> {
|
|||
}
|
||||
|
||||
#[test]
|
||||
#[cfg(feature = "write")]
|
||||
fn test_update_string_table_roundtrip() {
|
||||
let mut state = ParserState::new(24, |_| false, false);
|
||||
state.string_tables = vec![StringTableMeta {
|
||||
|
|
@ -377,6 +383,7 @@ impl<'a> TableEntries<'a> {
|
|||
self.entries
|
||||
}
|
||||
|
||||
#[cfg(feature = "write")]
|
||||
pub fn find_best_history(&self, text: &str) -> Option<(usize, usize)> {
|
||||
let mut best_index = None;
|
||||
let mut best_count = 0;
|
||||
|
|
@ -394,6 +401,7 @@ impl<'a> TableEntries<'a> {
|
|||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "write")]
|
||||
fn count_similar_characters(a: &str, b: &str) -> usize {
|
||||
for (i, (a, b)) in a.bytes().zip(b.bytes()).enumerate() {
|
||||
if a != b {
|
||||
|
|
@ -429,6 +437,7 @@ pub fn parse_string_table_update<'a>(
|
|||
Ok(entries.into_entries())
|
||||
}
|
||||
|
||||
#[cfg(feature = "write")]
|
||||
pub fn write_string_table_update(
|
||||
entries: &[(u16, StringTableEntry)],
|
||||
stream: &mut BitWriteStream<LittleEndian>,
|
||||
|
|
@ -457,6 +466,7 @@ pub fn write_string_table_update(
|
|||
}
|
||||
|
||||
#[test]
|
||||
#[cfg(feature = "write")]
|
||||
fn test_table_update_roundtrip() {
|
||||
fn entry_roundtrip(
|
||||
entries: Vec<(u16, StringTableEntry)>,
|
||||
|
|
@ -601,6 +611,7 @@ fn read_table_entry<'a>(
|
|||
Ok(StringTableEntry { text, extra_data })
|
||||
}
|
||||
|
||||
#[cfg(feature = "write")]
|
||||
fn write_table_entry(
|
||||
entry: &StringTableEntry,
|
||||
stream: &mut BitWriteStream<LittleEndian>,
|
||||
|
|
@ -647,6 +658,7 @@ fn write_table_entry(
|
|||
}
|
||||
|
||||
#[test]
|
||||
#[cfg(feature = "write")]
|
||||
fn test_table_entry_roundtrip() {
|
||||
fn entry_roundtrip(entry: StringTableEntry, fixed_bits: Option<u8>) {
|
||||
let table_meta = StringTableMeta {
|
||||
|
|
@ -723,6 +735,7 @@ pub fn read_var_int(stream: &mut Stream) -> ReadResult<u32> {
|
|||
Ok(result)
|
||||
}
|
||||
|
||||
#[cfg(feature = "write")]
|
||||
pub fn write_var_int(mut int: u32, stream: &mut BitWriteStream<LittleEndian>) -> ReadResult<()> {
|
||||
while int > 0x7F {
|
||||
let byte: u8 = int as u8 & 0x7F;
|
||||
|
|
@ -734,6 +747,7 @@ pub fn write_var_int(mut int: u32, stream: &mut BitWriteStream<LittleEndian>) ->
|
|||
|
||||
// encode the int in such a way that it has a fixed size, but still decodes the same
|
||||
// result is the first 40 bits of the return value
|
||||
#[cfg(feature = "write")]
|
||||
pub fn encode_var_int_fixed(mut int: u32) -> u64 {
|
||||
let mut out = 0;
|
||||
for i in 0..4 {
|
||||
|
|
@ -746,6 +760,7 @@ pub fn encode_var_int_fixed(mut int: u32) -> u64 {
|
|||
}
|
||||
|
||||
#[test]
|
||||
#[cfg(feature = "write")]
|
||||
fn test_var_int_roundtrip() {
|
||||
fn var_int_roundtrip(int: u32) {
|
||||
let mut data = Vec::new();
|
||||
|
|
@ -768,6 +783,7 @@ fn test_var_int_roundtrip() {
|
|||
}
|
||||
|
||||
#[test]
|
||||
#[cfg(feature = "write")]
|
||||
fn test_var_int_fixed_roundtrip() {
|
||||
fn var_int_roundtrip(int: u32) {
|
||||
let mut data = Vec::new();
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue