Skip to content

Commit

Permalink
fix for unknown material
Browse files Browse the repository at this point in the history
  • Loading branch information
francisdb committed Apr 21, 2024
1 parent c7ca4a2 commit c75101e
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 18 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,6 @@ Cargo.lock

# MSVC Windows builds of rustc generate these, which store debugging information
*.pdb

# output from example
basic.vpx
30 changes: 17 additions & 13 deletions src/vpx/gameitem/gate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,10 @@ impl Serialize for GateType {
S: Serializer,
{
match self {
GateType::WireW => serializer.serialize_str("WireW"),
GateType::WireRectangle => serializer.serialize_str("WireRectangle"),
GateType::Plate => serializer.serialize_str("Plate"),
GateType::LongPlate => serializer.serialize_str("LongPlate"),
GateType::WireW => serializer.serialize_str("wire_w"),
GateType::WireRectangle => serializer.serialize_str("wire_rectangle"),
GateType::Plate => serializer.serialize_str("plate"),
GateType::LongPlate => serializer.serialize_str("long_plate"),
}
}
}
Expand All @@ -54,13 +54,17 @@ impl<'de> Deserialize<'de> for GateType {
where
D: Deserializer<'de>,
{
let s = String::deserialize(deserializer)?;
match s.as_str() {
"WireW" => Ok(GateType::WireW),
"WireRectangle" => Ok(GateType::WireRectangle),
"Plate" => Ok(GateType::Plate),
"LongPlate" => Ok(GateType::LongPlate),
_ => Err(serde::de::Error::custom(format!("Unknown GateType: {}, expecting \"WireW\", \"WireRectangle\", \"Plate\" or \"LongPlate\"", s))),
let value = String::deserialize(deserializer)?;
let s = value.as_str();
match s {
"wire_w" => Ok(GateType::WireW),
"wire_rectangle" => Ok(GateType::WireRectangle),
"plate" => Ok(GateType::Plate),
"long_plate" => Ok(GateType::LongPlate),
_ => Err(serde::de::Error::unknown_variant(
s,
&["wire_w", "wire_rectangle", "plate", "long_plate"],
)),
}
}
}
Expand Down Expand Up @@ -456,13 +460,13 @@ mod tests {
fn test_gate_type_json() {
let gate_type = GateType::WireRectangle;
let json = serde_json::to_string(&gate_type).unwrap();
assert_eq!(json, "\"WireRectangle\"");
assert_eq!(json, "\"wire_rectangle\"");
let gate_type_read: GateType = serde_json::from_str(&json).unwrap();
assert_eq!(gate_type, gate_type_read);
}

#[test]
#[should_panic = "Error(\"Unknown GateType: Unknown, expecting \\\"WireW\\\", \\\"WireRectangle\\\", \\\"Plate\\\" or \\\"LongPlate\\\"\", line: 0, column: 0)"]
#[should_panic = "Error(\"unknown variant `Unknown`, expected one of `wire_w`, `wire_rectangle`, `plate`, `long_plate`\", line: 0, column: 0)"]
fn test_gate_type_json_panic() {
let json = Value::from("Unknown");
let _: GateType = serde_json::from_value(json).unwrap();
Expand Down
15 changes: 10 additions & 5 deletions src/vpx/material.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,23 +14,26 @@ const MAX_NAME_BUFFER: usize = 32;

#[derive(Dummy, Debug, Clone, PartialEq)]
pub enum MaterialType {
Unknown = -1, // found in Hot Line (Williams 1966) SG1bsoN.vpx
Basic = 0,
Metal = 1,
}

impl From<u32> for MaterialType {
fn from(value: u32) -> Self {
impl From<i32> for MaterialType {
fn from(value: i32) -> Self {
match value {
-1 => MaterialType::Unknown,
0 => MaterialType::Basic,
1 => MaterialType::Metal,
_ => panic!("Invalid MaterialType {}", value),
}
}
}

impl From<&MaterialType> for u32 {
impl From<&MaterialType> for i32 {
fn from(value: &MaterialType) -> Self {
match value {
MaterialType::Unknown => -1,
MaterialType::Basic => 0,
MaterialType::Metal => 1,
}
Expand All @@ -44,6 +47,7 @@ impl Serialize for MaterialType {
S: Serializer,
{
match self {
MaterialType::Unknown => serializer.serialize_str("unknown"),
MaterialType::Basic => serializer.serialize_str("basic"),
MaterialType::Metal => serializer.serialize_str("metal"),
}
Expand Down Expand Up @@ -71,6 +75,7 @@ impl<'de> Deserialize<'de> for MaterialType {
E: serde::de::Error,
{
match value.to_lowercase().as_str() {
"unknown" => Ok(MaterialType::Unknown),
"basic" => Ok(MaterialType::Basic),
"metal" => Ok(MaterialType::Metal),
_ => Err(serde::de::Error::unknown_variant(
Expand Down Expand Up @@ -633,7 +638,7 @@ impl BiffRead for Material {
let tag = reader.tag();
let tag_str = tag.as_str();
match tag_str {
"TYPE" => material.type_ = reader.get_u32().into(),
"TYPE" => material.type_ = reader.get_i32().into(),
"NAME" => material.name = reader.get_string(),
"WLIG" => material.wrap_lighting = reader.get_f32(),
"ROUG" => material.roughness = reader.get_f32(),
Expand Down Expand Up @@ -667,7 +672,7 @@ impl BiffRead for Material {

impl BiffWrite for Material {
fn biff_write(&self, writer: &mut BiffWriter) {
writer.write_tagged_u32("TYPE", (&self.type_).into());
writer.write_tagged_i32("TYPE", (&self.type_).into());
writer.write_tagged_string("NAME", &self.name);
writer.write_tagged_f32("WLIG", self.wrap_lighting);
writer.write_tagged_f32("ROUG", self.roughness);
Expand Down

0 comments on commit c75101e

Please sign in to comment.