From 240ddc7e8436ea7eaed40f3ed13d7cdf61f76e29 Mon Sep 17 00:00:00 2001 From: jake Date: Sun, 19 Nov 2023 00:03:50 -0700 Subject: [PATCH] move elseware characterclas/sectionid logic into libpso --- Cargo.toml | 4 +- src/character/character.rs | 90 +++++++++++++++++++++++++++++++++++++- src/character/mod.rs | 2 + 3 files changed, 93 insertions(+), 3 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index e0bc685..c827987 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -5,6 +5,8 @@ authors = ["Jake Probst "] edition = "2018" [dependencies] +psopacket = { path = "psopacket" } rand = "0.6.5" chrono = "*" -psopacket = { path = "psopacket" } +serde = { version = "1.0", features = ["derive"]} +strum = { version = "0.25.0", features = ["derive"] } diff --git a/src/character/character.rs b/src/character/character.rs index c3b914d..55b08e5 100644 --- a/src/character/character.rs +++ b/src/character/character.rs @@ -1,9 +1,11 @@ // TODO: techniques to enum use psopacket::PSOPacketData; use crate::{PSOPacketData, PacketParseError}; +use serde::{Serialize, Deserialize}; + #[repr(u8)] -#[derive(PSOPacketData, Debug, Copy, Clone, Hash, PartialEq, Eq)] +#[derive(PSOPacketData, Debug, Copy, Clone, Hash, PartialEq, Eq, strum::Display, strum::EnumString, Serialize, Deserialize)] pub enum CharacterClass { HUmar, HUnewearl, @@ -25,6 +27,7 @@ impl Default for CharacterClass { } } +// TODO: TryFrom impl std::convert::From for CharacterClass { fn from(f: u8) -> CharacterClass { match f { @@ -46,8 +49,54 @@ impl std::convert::From for CharacterClass { } } +impl From 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)] +#[derive(PSOPacketData, Debug, Copy, Clone, Hash, PartialEq, Eq, strum::Display, strum::EnumString, Serialize, Deserialize)] pub enum SectionID { Viridia, Greenill, @@ -67,6 +116,43 @@ impl Default for SectionID { } } +// TODO: TryFrom +impl From 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 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)] #[repr(C)] diff --git a/src/character/mod.rs b/src/character/mod.rs index d8668f3..3290cd0 100644 --- a/src/character/mod.rs +++ b/src/character/mod.rs @@ -2,3 +2,5 @@ pub mod settings; pub mod character; pub mod guildcard; + +pub use character::{SectionID, CharacterClass};