Skip to content

Commit

Permalink
Update str for wz and packet
Browse files Browse the repository at this point in the history
  • Loading branch information
jon-zu committed Jan 12, 2024
1 parent 94afe35 commit 22fd754
Show file tree
Hide file tree
Showing 27 changed files with 836 additions and 207 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@
# Files generated during some tests
*.wav
*.png
*.pcm
*.pcm

3 changes: 3 additions & 0 deletions crates/shroom-crypto/src/ig_cipher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ impl IgContext {
a ^ self.shuffle(key[0])
}

/// Decrypt the given data with the given key
fn dec(&self, data: u8, key: u32) -> u8 {
let key = key.to_le_bytes();
let a = self.shuffle(key[0]) ^ data;
Expand All @@ -93,6 +94,7 @@ impl IgContext {
}
}

/// Hasher for the IG hash
pub struct IgHasher<'ctx> {
state: u32,
ctx: &'ctx IgContext,
Expand Down Expand Up @@ -120,6 +122,7 @@ impl<'ctx> Hasher for IgHasher<'ctx> {
}
}

/// IgCipher
pub struct IgCipher {
ctx: IgContext,
state: u32,
Expand Down
2 changes: 2 additions & 0 deletions crates/shroom-crypto/src/version.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,12 @@ impl From<ShroomVersion> for u16 {
}

impl ShroomVersion {
/// Creates a new version
pub const fn new(v: u16) -> Self {
Self(v)
}

/// Gets the raw version
pub fn raw(&self) -> u16 {
self.0
}
Expand Down
2 changes: 2 additions & 0 deletions crates/shroom-pkt/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ pub enum Error {
OutOfCapacity,
#[error("No opcode)")]
NoOpCode,
#[error("Invalid all bits")]
InvalidAllBits
}

impl Error {
Expand Down
2 changes: 1 addition & 1 deletion crates/shroom-pkt/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,6 @@ pub use writer::PacketWriter;
// Re-export proto
pub use proto::*;

pub use opcode::ShroomOpCode;
pub use opcode::{ShroomOpCode, HasOpCode};
pub use pkt::Packet;
pub use shroom_pkt_derive::*;
6 changes: 3 additions & 3 deletions crates/shroom-pkt/src/opcode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,16 @@ pub trait HasOpCode {
/// Helper macro to easily implment `HasOpCode` for a packet
/// Example ```packet_opcode!(PingPacket, SendOpCode::Ping);```
#[macro_export]
macro_rules! packet_opcode {
macro_rules! with_opcode {
($packet_ty:ty, $op:path, $ty:ty) => {
impl $crate::HasOpCode for $packet_ty {
impl $crate::opcode::HasOpCode for $packet_ty {
type OpCode = $ty;

const OPCODE: Self::OpCode = $op;
}
};
($packet_ty:ty, $ty:ident::$op:ident) => {
impl $crate::HasOpCode for $packet_ty {
impl $crate::opcode::HasOpCode for $packet_ty {
type OpCode = $ty;

const OPCODE: Self::OpCode = $ty::$op;
Expand Down
16 changes: 0 additions & 16 deletions crates/shroom-pkt/src/pkt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,6 @@ use bytes::{Bytes, BytesMut, BufMut};

use crate::{Error, PacketReader, ShroomOpCode, PacketWriter, EncodePacket, opcode::HasOpCode, DecodePacket};

#[macro_export]
macro_rules! packet_with_opcode {
($packet_ty:ty, $op:path, $ty:ty) => {
impl $crate::ShroomPacket for $packet_ty {
type OpCode = $ty;
const OPCODE: Self::OpCode = $op;
}
};
($packet_ty:ty, $ty:ident::$op:ident) => {
impl $crate::ShroomPacket for $packet_ty {
type OpCode = $ty;
const OPCODE: Self::OpCode = $ty::$op;
}
};
}

#[derive(Debug, Clone)]
pub struct Packet(Bytes);

Expand Down
2 changes: 1 addition & 1 deletion crates/shroom-pkt/src/proto/enum.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/// Mark an enum which implements TryFromPrimitive and Into<Primitive>
/// Mark an enum which implements TryFromPrimitive and `Into<Primitive>`
/// as packet encode/decode-able
#[macro_export]
macro_rules! mark_shroom_enum {
Expand Down
2 changes: 1 addition & 1 deletion crates/shroom-pkt/src/proto/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ pub mod conditional;
pub mod list;
pub mod option;
pub mod padding;
//pub mod partial;
pub mod partial;
pub mod primitive;
pub mod r#enum;
pub mod string;
Expand Down
28 changes: 26 additions & 2 deletions crates/shroom-pkt/src/proto/partial.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::marker::PhantomData;

use crate::{
DecodePacket, EncodePacket, PacketReader, PacketResult, PacketWriter,
SizeHint, packet_wrap,
SizeHint,
};

pub trait PartialData<'de>: Sized {
Expand All @@ -26,6 +26,30 @@ impl<Flags> Default for AllFlags<Flags> {
}
}

impl<T: bitflags::Flags> EncodePacket for AllFlags<T>
where T::Bits: EncodePacket {
const SIZE_HINT: SizeHint = SizeHint::NONE;

fn encode_len(&self) -> usize {
T::all().bits().encode_len()
}

fn encode<B: bytes::BufMut>(&self, pw: &mut PacketWriter<B>) -> PacketResult<()> {
T::all().bits().encode(pw)
}
}

impl<'de, T: bitflags::Flags> DecodePacket<'de> for AllFlags<T>
where T::Bits: DecodePacket<'de> {
fn decode(pr: &mut PacketReader<'de>) -> PacketResult<Self> {
let bits = T::Bits::decode(pr)?;
if bits != T::all().bits() {
return Err(crate::Error::InvalidAllBits);
}
Ok(Self(PhantomData))
}
}

/*
Expand All @@ -44,7 +68,7 @@ impl<Flags: bitflags::Flags> PacketTryWrapped for AllFlags<Flags> {
}
}*/

packet_wrap!(AllFlags<Flags>, Flags, Flags);
//packet_wrap!(AllFlags<Flags>, Flags, Flags);

#[derive(Debug, Clone, PartialEq)]
pub struct PartialFlag<Hdr, FlagData> {
Expand Down
2 changes: 1 addition & 1 deletion crates/shroom-pkt/src/util/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ impl SizeHint {
})
}

/// Sum two Option<usize>
/// Sum two usize Options
/// When const traits become stable Add can be implemented
pub const fn add(self, rhs: Self) -> Self {
Self(match (self.0, rhs.0) {
Expand Down
28 changes: 15 additions & 13 deletions crates/shroom-wz/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,16 @@ default = []
mmap = ["memmap2"]
webp = ["webp-animation"]

[dev-dependencies]
quickcheck = "1"
quickcheck_macros = "1"

[dependencies]
webp-animation = { version = "0.9", optional = true }
aes = "0.8"
anyhow = { version = "1" }
binrw = "0.13"
bit-struct = "0.3.2"
bytemuck = "1"
image = "0.24"
itoa = "1"
flate2 = { version = "1", features = ["zlib"] }
memmap2 = { version = "0.9", optional = true }
texpresso = "2"
serde = { version = "1", features = ["derive"] }
derive_more = { version = "1.0.0-beta.6", features = [
"from",
"into",
Expand All @@ -28,12 +26,16 @@ derive_more = { version = "1.0.0-beta.6", features = [
"try_into",
"into_iterator",
] }
uuid = { version = "1", features = ["v4"] }
indexmap = { version = "2", features = ["serde"] }
encoding_rs = "0.8"
indexmap = { version = "2", features = ["serde"] }
flate2 = { version = "1", features = ["zlib"] }
image = "0.24"
memmap2 = { version = "0.9", optional = true }
num_enum = "0.7.2"
rgb = "0.8.37"
serde = { version = "1", features = ["derive"] }
serde_json = "1"
texpresso = "2"
uuid = { version = "1", features = ["v4"] }
webp-animation = { version = "0.9", optional = true }
shroom-crypto = { version = "0.1.0", path = "../shroom-crypto" }
rgb = "0.8.37"
bit-struct = "0.3.2"
num_enum = "0.7.2"
derive_builder = "0.12.0"
Loading

0 comments on commit 22fd754

Please sign in to comment.