mirror of
https://codeberg.org/demostf/parser.git
synced 2026-06-04 02:24:12 +02:00
some wip
This commit is contained in:
commit
4ac2a3cfc6
20 changed files with 907 additions and 0 deletions
23
src/demo/packet/consolecmd.rs
Normal file
23
src/demo/packet/consolecmd.rs
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
use crate::{Parse, ParseError, ParserState, Result, Stream};
|
||||
|
||||
pub struct ConsoleCmdPacket {
|
||||
tick: u32,
|
||||
command: String,
|
||||
}
|
||||
|
||||
impl<'a> Parse<'a> for ConsoleCmdPacket {
|
||||
fn parse(stream: &mut Stream, _state: &ParserState) -> Result<Self> {
|
||||
let tick = stream.read(32)?;
|
||||
let len = stream.read(32)?;
|
||||
Ok(ConsoleCmdPacket {
|
||||
tick,
|
||||
command: stream.read_string(Some(len))?,
|
||||
})
|
||||
}
|
||||
|
||||
fn skip(stream: &mut Stream) -> Result<()> {
|
||||
let _ = stream.skip(32)?;
|
||||
let len = stream.read(32)?;
|
||||
stream.skip(len).map_err(ParseError::from)
|
||||
}
|
||||
}
|
||||
46
src/demo/packet/mod.rs
Normal file
46
src/demo/packet/mod.rs
Normal file
|
|
@ -0,0 +1,46 @@
|
|||
use crate::{Parse, ParseError, ParserState, Result, Stream};
|
||||
use num_traits::FromPrimitive;
|
||||
use self::stop::Stop;
|
||||
|
||||
mod stop;
|
||||
mod synctick;
|
||||
mod consolecmd;
|
||||
mod usercmd;
|
||||
mod stringtable;
|
||||
|
||||
pub enum Packet {
|
||||
Stop(Stop)
|
||||
}
|
||||
|
||||
#[derive(Primitive)]
|
||||
pub enum PacketType {
|
||||
Sigon = 1,
|
||||
Packet = 2,
|
||||
SyncTick = 3,
|
||||
ConsoleCmd = 4,
|
||||
UserCmd = 5,
|
||||
DataTables = 6,
|
||||
Stop = 7,
|
||||
StringTables = 8,
|
||||
}
|
||||
|
||||
impl Packet {
|
||||
fn read_type(stream: &mut Stream) -> Result<PacketType> {
|
||||
let raw = stream.read(8)?;
|
||||
let packet_type: Option<PacketType> = PacketType::from_u8(raw);
|
||||
packet_type.ok_or(ParseError::InvalidPacketType(raw))
|
||||
}
|
||||
}
|
||||
|
||||
//impl Parse for Packet {
|
||||
// fn parse(stream: &mut Stream, _state: &ParserState) -> Result<Self> {
|
||||
// let packet_type = Packet::read_type(stream);
|
||||
// match packet_type {
|
||||
// Sigon => {}
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// fn skip(stream: &mut Stream) -> Result<()> {
|
||||
// Ok(())
|
||||
// }
|
||||
//}
|
||||
13
src/demo/packet/stop.rs
Normal file
13
src/demo/packet/stop.rs
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
use crate::{Parse, ParserState, Result, Stream};
|
||||
|
||||
pub struct Stop;
|
||||
|
||||
impl<'a> Parse<'a> for Stop {
|
||||
fn parse(_stream: &mut Stream, _state: &ParserState) -> Result<Self> {
|
||||
Ok(Stop)
|
||||
}
|
||||
|
||||
fn skip(_stream: &mut Stream) -> Result<()> {
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
91
src/demo/packet/stringtable.rs
Normal file
91
src/demo/packet/stringtable.rs
Normal file
|
|
@ -0,0 +1,91 @@
|
|||
use crate::{Parse, ParseError, ParserState, Result, Stream};
|
||||
|
||||
pub struct StringTable<'a> {
|
||||
name: String,
|
||||
entries: Vec<StringTableEntry<'a>>,
|
||||
max_entries: u32,
|
||||
fixed_userdata_size: Option<u32>,
|
||||
fixed_userdata_size_bits: Option<u32>,
|
||||
client_entries: Option<Vec<StringTableEntry<'a>>>,
|
||||
compressed: bool,
|
||||
}
|
||||
|
||||
impl<'a> StringTable<'a> {
|
||||
fn parse(stream: &mut Stream<'a>) -> Result<Self> {
|
||||
let name = stream.read_string(None)?;
|
||||
let entry_count: u32 = stream.read(16)?;
|
||||
let mut entries = Vec::with_capacity(entry_count as usize);
|
||||
for _ in 0..entry_count {
|
||||
entries.push(StringTableEntry::parse(stream)?);
|
||||
}
|
||||
|
||||
let client_entries = if stream.read_bool()? {
|
||||
let count = stream.read(16)?;
|
||||
let mut vec = Vec::with_capacity(count);
|
||||
for _ in 0..count {
|
||||
vec.push(StringTableEntry::parse(stream)?);
|
||||
}
|
||||
Some(vec)
|
||||
} else {
|
||||
None
|
||||
};
|
||||
|
||||
Ok(StringTable {
|
||||
name,
|
||||
entries,
|
||||
max_entries: entry_count,
|
||||
fixed_userdata_size: None,
|
||||
fixed_userdata_size_bits: None,
|
||||
client_entries,
|
||||
compressed: false,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
pub struct StringTableEntry<'a> {
|
||||
text: String,
|
||||
extra_data: Option<Stream<'a>>,
|
||||
}
|
||||
|
||||
impl<'a> StringTableEntry<'a> {
|
||||
fn parse(stream: &mut Stream<'a>) -> Result<Self> {
|
||||
let text = stream.read_string(None)?;
|
||||
let extra_data = if stream.read_bool()? {
|
||||
let byte_len: usize = stream.read(16)?;
|
||||
Some(stream.read_bits(byte_len * 8)?)
|
||||
} else {
|
||||
None
|
||||
};
|
||||
Ok(StringTableEntry {
|
||||
text,
|
||||
extra_data,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
pub struct StringTablePacket<'a> {
|
||||
tick: u32,
|
||||
tables: Vec<StringTable<'a>>,
|
||||
}
|
||||
|
||||
impl<'a> Parse<'a> for StringTablePacket<'a> {
|
||||
fn parse(stream: &mut Stream<'a>, _state: &ParserState<'a>) -> Result<Self> {
|
||||
let tick = stream.read(32)?;
|
||||
let length: usize = stream.read(32)?;
|
||||
let count: u32 = stream.read(8)?;
|
||||
let mut tables = Vec::with_capacity(count as usize);
|
||||
for _ in 0..count {
|
||||
tables.push(StringTable::parse(stream)?);
|
||||
}
|
||||
Ok(StringTablePacket {
|
||||
tick,
|
||||
tables,
|
||||
})
|
||||
}
|
||||
|
||||
fn skip(stream: &mut Stream) -> Result<()> {
|
||||
let _ = stream.skip(32)?;
|
||||
let length = stream.read(32)?;
|
||||
stream.skip(length).map_err(ParseError::from)
|
||||
}
|
||||
}
|
||||
17
src/demo/packet/synctick.rs
Normal file
17
src/demo/packet/synctick.rs
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
use crate::{Parse, ParseError, ParserState, Result, Stream};
|
||||
|
||||
pub struct SyncTickPacket {
|
||||
tick: u32
|
||||
}
|
||||
|
||||
impl<'a> Parse<'a> for SyncTickPacket {
|
||||
fn parse(stream: &mut Stream, _state: &ParserState) -> Result<Self> {
|
||||
Ok(SyncTickPacket {
|
||||
tick: stream.read(32)?
|
||||
})
|
||||
}
|
||||
|
||||
fn skip(stream: &mut Stream) -> Result<()> {
|
||||
stream.skip(32).map_err(ParseError::from)
|
||||
}
|
||||
}
|
||||
26
src/demo/packet/usercmd.rs
Normal file
26
src/demo/packet/usercmd.rs
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
use crate::{Parse, ParseError, ParserState, Result, Stream};
|
||||
|
||||
pub struct UserCmdPacket {
|
||||
tick: u32,
|
||||
command: String,
|
||||
sequence_out: u32
|
||||
}
|
||||
|
||||
impl<'a> Parse<'a> for UserCmdPacket {
|
||||
fn parse(stream: &mut Stream, _state: &ParserState) -> Result<Self> {
|
||||
let tick = stream.read(32)?;
|
||||
let sequence_out = stream.read(32)?;
|
||||
let len = stream.read(32)?;
|
||||
Ok(UserCmdPacket {
|
||||
tick,
|
||||
sequence_out,
|
||||
command: stream.read_string(Some(len))?,
|
||||
})
|
||||
}
|
||||
|
||||
fn skip(stream: &mut Stream) -> Result<()> {
|
||||
let _ = stream.skip(64)?;
|
||||
let len = stream.read(32)?;
|
||||
stream.skip(len).map_err(ParseError::from)
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue