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

44 unable to open save file from journey to the savage planet #45

Closed
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
Binary file added resources/test/text_property_noarray.bin
Binary file not shown.
74 changes: 37 additions & 37 deletions src/properties/text_property.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@ use std::{
};

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

use crate::{
cursor_ext::{ReadExt, WriteExt},
error::{DeserializeError, Error},
};

use super::{impl_write, PropertyOptions, PropertyTrait};
use super::{impl_read, impl_read_header, impl_write, PropertyOptions, PropertyTrait};

/// A property that stores GVAS Text.
#[derive(Clone, PartialEq, Eq, Hash)]
Expand All @@ -21,12 +22,14 @@ pub enum TextProperty {
// Workaround for https://github.com/serde-rs/json/issues/664
[u8; 0],
),
/// A triple `TextProperty`.
Triple(Option<String>, Option<String>, Option<String>),
/// A rich `TextProperty`.
Rich(RichText),
/// A simple `TextProperty`.
Simple(Vec<String>),
/// `TextProperty` type 8.
Type8(String, String, String),
Type8(Option<String>, String, String),
}

/// A struct describing a rich `TextProperty`.
Expand Down Expand Up @@ -81,47 +84,31 @@ impl TextProperty {
}
}

impl_read!(options);
impl_read_header!(options);

#[inline]
pub(crate) fn read<R: Read + Seek>(
pub(crate) fn read_body<R: Read + Seek>(
cursor: &mut R,
include_header: bool,
options: &mut PropertyOptions,
) -> Result<Self, Error> {
validate!(
cursor,
!include_header,
"TextProperty only supported in arrays"
);

let component_type = cursor.read_u32::<LittleEndian>()?;
validate!(
cursor,
component_type <= 2 || component_type == 8,
"Unexpected component {component_type}"
);

let expect_indicator = match component_type {
1 => 3,
8 => 0,
_ => 255,
};
let indicator = cursor.read_u8()?;
validate!(
cursor,
indicator == expect_indicator,
"Unexpected indicator {} for component {}, expected {}",
indicator,
component_type,
expect_indicator
);

if component_type == 0 {

if component_type == 0 && indicator == 255 {
// Empty text
let count = cursor.read_u32::<LittleEndian>()?;
validate!(cursor, count == 0, "Unexpected count {count}");

Ok(TextProperty::Empty([]))
} else if component_type == 1 {
} else if component_type == 0 && indicator == 0 {
// Triple text
let string1 = cursor.read_fstring()?;
let string2 = cursor.read_fstring()?;
let string3 = cursor.read_fstring()?;

Ok(TextProperty::Triple(string1, string2, string3))
} else if component_type == 1 && indicator == 3 {
// Rich text
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the problematic case:

00009fe0:                               11 00 00 00 53 68            ....Sh
00009ff0: 6f 72 74 44 65 73 63 72 69 70 74 69 6f 6e 00 0d  ortDescription..
0000a000: 00 00 00 54 65 78 74 50 72 6f 70 65 72 74 79 00  ...TextProperty.
0000a010: a2 00 00 00 00 00 00 00 00 01 00 00 00 03 02 00  ................
0000a020: 00 00 ff 01 00 00 00 53 00 00 00 55 73 65 20 79  .......S...Use y
0000a030: 6f 75 72 20 73 63 61 6e 6e 65 72 20 74 6f 20 64  our scanner to d
0000a040: 6f 63 75 6d 65 6e 74 20 74 68 65 20 70 6c 61 6e  ocument the plan
0000a050: 65 74 27 73 20 65 63 6f 73 79 73 74 65 6d 0a 0a  et's ecosystem..
0000a060: 4b 69 6e 64 65 78 20 70 72 6f 67 72 65 73 73 3a  Kindex progress:
0000a070: 20 7b 70 72 6f 67 72 65 73 73 7d 20 25 00 01 00   {progress} %...
0000a080: 00 00 09 00 00 00 70 72 6f 67 72 65 73 73 00 04  ......progress..
0000a090: 01 00 00 00 04 02 bc 9c 82 42 01 00 00 00 00 00  .........B......
0000a0a0: 00 00 01 00 00 00 04 01 00 00 00 44 01 00 00 00  ...........D....
0000a0b0: 00 00 00 00 00 00 00 00 00 00 00                 ...........

let num_flags = cursor.read_u8()?;
validate!(cursor, num_flags == 8, "Unexpected num_flags {num_flags}");
Expand Down Expand Up @@ -170,7 +157,7 @@ impl TextProperty {
pattern,
text_format,
}))
} else if component_type == 2 {
} else if component_type == 2 && indicator == 255 {
// Simple text
let count = cursor.read_u32::<LittleEndian>()?;

Expand All @@ -181,16 +168,16 @@ impl TextProperty {
}

Ok(TextProperty::Simple(strings))
} else if component_type == 8 {
let unknown = cursor.read_string()?;
} else if component_type == 8 && indicator == 0 {
let unknown = cursor.read_fstring()?;
let guid = cursor.read_string()?;
let value = cursor.read_string()?;

Ok(TextProperty::Type8(unknown, guid, value))
} else {
// Unknown text
Err(DeserializeError::InvalidProperty(
format!("Unexpected component_type {}", component_type),
format!("Unexpected component_type {component_type}, indicator {indicator}"),
cursor.stream_position()?,
))?
}
Expand All @@ -209,6 +196,14 @@ impl TextProperty {
cursor.write_u32::<LittleEndian>(0)?;
}

TextProperty::Triple(string1, string2, string3) => {
cursor.write_u32::<LittleEndian>(0)?;
cursor.write_u8(0)?;
cursor.write_fstring(string1.as_deref())?;
cursor.write_fstring(string2.as_deref())?;
cursor.write_fstring(string3.as_deref())?;
}

TextProperty::Rich(value) => {
cursor.write_u32::<LittleEndian>(1)?;
cursor.write_u8(3)?;
Expand Down Expand Up @@ -246,7 +241,7 @@ impl TextProperty {
TextProperty::Type8(unknown, guid, value) => {
cursor.write_u32::<LittleEndian>(8)?;
cursor.write_u8(0)?;
cursor.write_string(unknown)?;
cursor.write_fstring(unknown.as_deref())?;
cursor.write_string(guid)?;
cursor.write_string(value)?;
}
Expand All @@ -263,6 +258,11 @@ impl Debug for TextProperty {
TextProperty::Simple(values) => f.debug_list().entries(values).finish(),
TextProperty::Empty(_) => f.write_str("Empty"),
TextProperty::Type8(_, _, value) => value.fmt(f),
TextProperty::Triple(string1, string2, string3) => {
string1.fmt(f)?;
string2.fmt(f)?;
string3.fmt(f)
}
}
}
}
Expand Down
47 changes: 47 additions & 0 deletions tests/text_property_noarray.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
use gvas::GvasFile;
use std::{
fs::File,
io::{Cursor, Read},
path::Path,
};

#[test]
fn read_text_property_noarray() {
let path =
Path::new(env!("CARGO_MANIFEST_DIR")).join("resources/test/text_property_noarray.bin");
let mut file = File::open(path).expect("Failed to open test asset");

// Read the file in to a GvasFile
let _gvas = GvasFile::read(&mut file).expect("Failed to parse gvas file");
}

#[test]
fn write_text_property_noarray() {
let path =
Path::new(env!("CARGO_MANIFEST_DIR")).join("resources/test/text_property_noarray.bin");
let mut file = File::open(path).expect("Failed to open test asset");

// Read the file in to a Vec<u8>
let mut data = Vec::new();
file.read_to_end(&mut data)
.expect("Failed to read test asset");

// Convert the Vec<u8> to a GvasFile
let mut cursor = Cursor::new(data);
let file = GvasFile::read(&mut cursor).expect("Failed to parse gvas file");

// Convert the GvasFile back to a Vec<u8>
let mut writer = Cursor::new(Vec::new());
file.write(&mut writer)
.expect("Failed to serialize gvas file");

// Compare the two Vec<u8>s
assert_eq!(cursor.get_ref(), writer.get_ref());

// Read the file back in again
let mut reader = Cursor::new(writer.get_ref().to_owned());
let read_back = GvasFile::read(&mut reader).expect("Failed to parse serialized save file");

// Compare the two GvasFiles
assert_eq!(file, read_back);
}
Loading