3 Commits

  1. 4
      Cargo.toml
  2. 146
      src/character/character.rs
  3. 2
      src/character/mod.rs
  4. 29
      src/lib.rs

4
Cargo.toml

@ -5,6 +5,8 @@ authors = ["Jake Probst <jake.probst@gmail.com>"]
edition = "2018"
[dependencies]
psopacket = { path = "psopacket" }
rand = "0.6.5"
chrono = "*"
psopacket = { path = "psopacket" }
serde = "*"
strum = { version = "0.25.0", features = ["derive"] }

146
src/character/character.rs

@ -1,12 +1,12 @@
// TODO: ch_class to CharacterClass enum
// TODO: section_id to SectionId enum
// TODO: techniques to enum
use psopacket::PSOPacketData;
use crate::{PSOPacketData, PacketParseError};
use serde::{Serialize, Deserialize};
#[repr(u32)]
#[derive(Copy, Clone, Hash, PartialEq, Eq)]
pub enum Class {
#[repr(u8)]
#[derive(PSOPacketData, Debug, Copy, Clone, Hash, PartialEq, Eq, strum::Display, strum::EnumString, Serialize, Deserialize)]
pub enum CharacterClass {
HUmar,
HUnewearl,
HUcast,
@ -21,28 +21,82 @@ pub enum Class {
FOnewearl,
}
impl std::convert::From<u8> for Class {
fn from(f: u8) -> Class {
impl Default for CharacterClass {
fn default() -> CharacterClass {
CharacterClass::HUmar
}
}
// TODO: TryFrom
impl std::convert::From<u8> for CharacterClass {
fn from(f: u8) -> CharacterClass {
match f {
0 => Class::HUmar,
1 => Class::HUnewearl,
2 => Class::HUcast,
3 => Class::RAmar,
4 => Class::RAcast,
5 => Class::RAcaseal,
6 => Class::FOmarl,
7 => Class::FOnewm,
8 => Class::FOnewearl,
9 => Class::HUcaseal,
10 => Class::RAmarl,
11 => Class::FOmar,
0 => CharacterClass::HUmar,
1 => CharacterClass::HUnewearl,
2 => CharacterClass::HUcast,
3 => CharacterClass::RAmar,
4 => CharacterClass::RAcast,
5 => CharacterClass::RAcaseal,
6 => CharacterClass::FOmarl,
7 => CharacterClass::FOnewm,
8 => CharacterClass::FOnewearl,
9 => CharacterClass::HUcaseal,
10 => CharacterClass::RAmarl,
11 => CharacterClass::FOmar,
_ => panic!("unknown class")
}
}
}
impl From<CharacterClass> for u8 {
fn from(other: CharacterClass) -> u8 {
match other {
CharacterClass::HUmar => 0,
CharacterClass::HUnewearl => 1,
CharacterClass::HUcast => 2,
CharacterClass::RAmar => 3,
CharacterClass::RAcast => 4,
CharacterClass::RAcaseal => 5,
CharacterClass::FOmarl => 6,
CharacterClass::FOnewm => 7,
CharacterClass::FOnewearl => 8,
CharacterClass::HUcaseal => 9,
CharacterClass::FOmar => 10,
CharacterClass::RAmarl => 11,
}
}
}
impl CharacterClass {
pub fn is_human(&self) -> bool {
matches!(self,
CharacterClass::HUmar |
CharacterClass::RAmar |
CharacterClass::RAmarl |
CharacterClass::FOmar |
CharacterClass::FOmarl)
}
pub fn is_newman(&self) -> bool {
matches!(self,
CharacterClass::HUnewearl |
CharacterClass::FOnewm |
CharacterClass::FOnewearl)
}
pub fn is_android(&self) -> bool {
matches!(self,
CharacterClass::HUcast |
CharacterClass::HUcaseal |
CharacterClass::RAcast |
CharacterClass::RAcaseal)
}
}
#[repr(u8)]
#[derive(PSOPacketData, Debug, Copy, Clone, Hash, PartialEq, Eq, strum::Display, strum::EnumString, Serialize, Deserialize)]
pub enum SectionID {
Viridia,
Greenill,
@ -56,6 +110,48 @@ pub enum SectionID {
Whitill,
}
impl Default for SectionID {
fn default() -> SectionID {
SectionID::Viridia
}
}
// TODO: TryFrom
impl From<u8> for SectionID {
fn from(id: u8) -> SectionID {
match id {
0 => SectionID::Viridia,
1 => SectionID::Greenill,
2 => SectionID::Skyly,
3 => SectionID::Bluefull,
4 => SectionID::Purplenum,
5 => SectionID::Pinkal,
6 => SectionID::Redria,
7 => SectionID::Oran,
8 => SectionID::Yellowboze,
9 => SectionID::Whitill,
_ => panic!(),
}
}
}
impl From<SectionID> for u8 {
fn from(other: SectionID) -> u8 {
match other {
SectionID::Viridia => 0,
SectionID::Greenill => 1,
SectionID::Skyly => 2,
SectionID::Bluefull => 3,
SectionID::Purplenum => 4,
SectionID::Pinkal => 5,
SectionID::Redria => 6,
SectionID::Oran => 7,
SectionID::Yellowboze => 8,
SectionID::Whitill => 9,
}
}
}
#[derive(PSOPacketData, Copy, Clone)]
@ -80,8 +176,8 @@ pub struct Character {
pub _unused: [u8; 11],
pub play_time: u32,
pub name_color_checksum: u32,
pub section_id: u8,
pub ch_class: u8,
pub section_id: SectionID,
pub ch_class: CharacterClass,
pub v2flags: u8,
pub version: u8,
pub v1flags: u32,
@ -149,8 +245,8 @@ pub struct SelectScreenCharacter {
pub model: u8,
pub _unused: [u8; 15],
pub name_color_checksum: u32,
pub section_id: u8,
pub ch_class: u8,
pub section_id: SectionID,
pub ch_class: CharacterClass,
pub v2flags: u8,
pub version: u8,
pub v1flags: u32,
@ -301,8 +397,8 @@ pub struct FullCharacter {
pub guildcard_desc: [u16; 88],
pub _reserved1: u8,
pub _reserved2: u8,
pub section_id: u8,
pub char_class: u8,
pub section_id: SectionID,
pub char_class: CharacterClass,
pub _unknown2: u32,
pub symbol_chats: [u8; 0x4E0],
pub shortcuts: [u8; 2624],

2
src/character/mod.rs

@ -2,3 +2,5 @@
pub mod settings;
pub mod character;
pub mod guildcard;
pub use character::{SectionID, CharacterClass};

29
src/lib.rs

@ -624,4 +624,33 @@ mod test {
d: vec![9,9,9,8],
});
}
#[test]
fn test_derive_pso_packet_data_on_simple_enum() {
#[repr(u8)]
#[derive(PSOPacketData, Eq, PartialEq)]
enum Q {
A,
B,
C,
}
let q = Q::A.as_bytes();
assert!(q == vec![0]);
let q = Q::B.as_bytes();
assert!(q == vec![1]);
let q = Q::C.as_bytes();
assert!(q == vec![2]);
let q = Q::from_bytes(&mut std::io::Cursor::new(vec![0])).unwrap();
assert!(q == Q::A);
let q = Q::from_bytes(&mut std::io::Cursor::new(vec![1])).unwrap();
assert!(q == Q::B);
let q = Q::from_bytes(&mut std::io::Cursor::new(vec![2])).unwrap();
assert!(q == Q::C);
}
}
Loading…
Cancel
Save