From 4df2803fdb41182f9b27e165bee8523a7d780028 Mon Sep 17 00:00:00 2001 From: mykola2312 <49044616+mykola2312@users.noreply.github.com> Date: Thu, 31 Aug 2023 13:46:20 +0300 Subject: [PATCH] implement Display, PartialEq and Borrow for FString. May need to complete it so indexing by &str would be possible --- src/fot/esh.rs | 7 +++---- src/fot/fstring.rs | 22 +++++++++++++++++++++- src/fot/world.rs | 4 +++- 3 files changed, 27 insertions(+), 6 deletions(-) diff --git a/src/fot/esh.rs b/src/fot/esh.rs index 426a608..e5c1cee 100644 --- a/src/fot/esh.rs +++ b/src/fot/esh.rs @@ -5,8 +5,7 @@ use super::stream::{ReadStream, WriteStream}; use super::tag::Tag; use anyhow::Result; use indexmap::IndexMap; -use std::fmt::{self, write}; -use std::path::Display; +use std::fmt; #[derive(Debug)] pub struct ESHUnknown { @@ -221,8 +220,8 @@ impl fmt::Display for ESHValue { ESHValue::Bool(val) => write!(f, "{}", val), ESHValue::Float(val) => write!(f, "{}", val), ESHValue::Int(val) => write!(f, "{}", val), - ESHValue::String(str) => write!(f, "{}", str.str), - ESHValue::Sprite(spr) => write!(f, "{}", spr.str), + ESHValue::String(str) => write!(f, "{}", str), + ESHValue::Sprite(spr) => write!(f, "{}", spr), ESHValue::Binary(bin) => write!(f, "Binary, size {}", bin.len()), ESHValue::EntityFlags(val) => { write!(f, "entity {} flags {:x}", val.entity_id, val.flags) diff --git a/src/fot/fstring.rs b/src/fot/fstring.rs index 9a73006..04d9be0 100644 --- a/src/fot/fstring.rs +++ b/src/fot/fstring.rs @@ -4,6 +4,8 @@ use anyhow::Result; use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt}; use encoding_rs::WINDOWS_1251; use std::io::Cursor; +use std::borrow::Borrow; +use std::fmt; // FString - Fallout @@ -13,7 +15,7 @@ pub enum FStringEncoding { WCS2, } -#[derive(Debug, Hash, PartialEq, Eq, Clone)] +#[derive(Debug, Hash, Eq, Clone)] pub struct FString { pub encoding: FStringEncoding, pub enc_len: usize, @@ -79,3 +81,21 @@ impl Decoder for FString { } } } + +impl PartialEq for FString { + fn eq(&self, other: &Self) -> bool { + self.str == other.str + } +} + +impl Borrow for FString { + fn borrow(&self) -> &str { + self.str.as_str() + } +} + +impl fmt::Display for FString { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(f, "\"{}\"", self.str) + } +} \ No newline at end of file diff --git a/src/fot/world.rs b/src/fot/world.rs index bfe6be4..4b7cf5b 100644 --- a/src/fot/world.rs +++ b/src/fot/world.rs @@ -1,6 +1,7 @@ use super::decoder::Decoder; use super::esh::ESH; use super::fstring::FString; +use super::fstring::FStringEncoding; use super::raw::Raw; use super::sgd::SGD; use super::ssg::SSG; @@ -34,8 +35,9 @@ impl World { let mut rd = ReadStream::new(&self.data, 0x14AC); let esh: ESH = rd.read(0)?; for (name, value) in esh.props.iter() { - println!("{}\t{}", name.str, value); + println!("{}\t{}", name, value); } + dbg!(&esh.props["Display Name"]); Ok(()) }