Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cleanup EnumProperty arrays #43

Merged
merged 1 commit into from
Nov 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file modified resources/test/component8.sav
100755 → 100644
Empty file.
Empty file modified resources/test/enum_array.sav
100755 → 100644
Empty file.
11 changes: 6 additions & 5 deletions src/properties/enum_property.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::io::{Cursor, Read, Seek, Write};

use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt};
use unreal_helpers::UnrealWriteExt;

use crate::{
cursor_ext::{ReadExt, WriteExt},
Expand All @@ -13,16 +14,16 @@ use super::{impl_read_header, impl_write, impl_write_header_part, PropertyOption
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct EnumProperty {
enum_type: String,
enum_type: Option<String>,
value: String,
}

impl_write!(EnumProperty, (write_string, enum_type));
impl_write!(EnumProperty, (write_fstring, enum_type));

impl EnumProperty {
/// Creates a new `EnumProperty` instance.
#[inline]
pub fn new(enum_type: String, value: String) -> Self {
pub fn new(enum_type: Option<String>, value: String) -> Self {
EnumProperty { enum_type, value }
}

Expand All @@ -34,14 +35,14 @@ impl EnumProperty {
if include_header {
Self::read_header(cursor)
} else {
Self::read_body(cursor, String::from("<array>"))
Self::read_body(cursor, None)
}
}

impl_read_header!(enum_type);

#[inline]
fn read_body<R: Read + Seek>(cursor: &mut R, enum_type: String) -> Result<Self, Error> {
fn read_body<R: Read + Seek>(cursor: &mut R, enum_type: Option<String>) -> Result<Self, Error> {
let value = cursor.read_string()?;

Ok(EnumProperty { enum_type, value })
Expand Down
6 changes: 5 additions & 1 deletion src/properties/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ macro_rules! impl_read_header {
assert_eq!(separator, 0);

let start = reader.stream_position()?;
let result = Self::read_body(reader $(, $var)*)?;
let result = Self::read_body(reader $(, Some($var))*)?;
let end = reader.stream_position()?;
assert_eq!(end - start, length);

Expand Down Expand Up @@ -298,6 +298,10 @@ macro_rules! impl_write {
///
/// This macro is used inside the `impl_write!` macro to write individual parts of a property header.
macro_rules! impl_write_header_part {
($self:ident, $writer:ident, (write_fstring, $member:ident)) => {
$writer.write_fstring($self.$member.as_deref())?;
};

($self:ident, $writer:ident, ($write_fn:ident, $member:ident)) => {
$writer.$write_fn(&$self.$member)?;
};
Expand Down
2 changes: 1 addition & 1 deletion tests/test_property.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ test_property!(test_str, StrProperty, StrProperty::from("test string"));
test_property!(
test_enum,
EnumProperty,
EnumProperty::new(String::from("type"), String::from("value"))
EnumProperty::new(Some(String::from("type")), String::from("value"))
);

// StructProperty
Expand Down
Loading