Skip to content

Commit

Permalink
Support ContentId in rbx_xml
Browse files Browse the repository at this point in the history
  • Loading branch information
Dekkonot committed Oct 7, 2024
1 parent 8973675 commit b72bcbf
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 14 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
---
source: rbx_xml/src/tests/formatting.rs
assertion_line: 174
expression: ser_str
---
<roblox version="4">
Expand Down Expand Up @@ -31,13 +32,13 @@ expression: ser_str
<B>125600</B>
</Color3>
<ColorSequence name="TestColorSequence">0 0 0.5 1 0 1 1 0.5 0 0 </ColorSequence>
<Content name="TestContent1">
<ContentId name="TestContent1">
<url>Wow!</url>
</Content>
<Content name="TestContent2">
</ContentId>
<ContentId name="TestContent2">
<null>
</null>
</Content>
</ContentId>
</Properties>
<Item class="TestClass" referent="1">
<Properties>
Expand Down
42 changes: 32 additions & 10 deletions rbx_xml/src/types/content.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ use crate::{
serializer_core::{XmlEventWriter, XmlWriteEvent},
};

// A Content type is serialized as either:
// A ContentId type is serialized as either:
// <null></null>, which indicates an empty content value
// <url>something</url>, where 'something' is a URL to use for content.
impl XmlType for Content {
const XML_TAG_NAME: &'static str = "Content";
const XML_TAG_NAME: &'static str = "ContentId";

fn write_xml<W: Write>(&self, writer: &mut XmlEventWriter<W>) -> Result<(), EncodeError> {
// FIXME: Content should have a method for this
Expand Down Expand Up @@ -66,6 +66,28 @@ impl XmlType for Content {
}
}

/// In release 645, Roblox changed `Content` to serialize as `ContentId`.
/// However, we still need to deserialize older models, so we have to support
/// `Content`.
///
/// This may need to be replaced in the future if Roblox implements a new
/// `Content` type, but right now they haven't.
#[derive(Debug, PartialEq, Eq)]
pub struct ContentDummy(pub Content);

impl XmlType for ContentDummy {
const XML_TAG_NAME: &'static str = "Content";

fn write_xml<W: Write>(&self, _writer: &mut XmlEventWriter<W>) -> Result<(), EncodeError> {
panic!("Content values are only read, never written.");
}

fn read_xml<R: Read>(reader: &mut XmlEventReader<R>) -> Result<Self, DecodeError> {
// We just want to use the same deserializer as ContentId
Content::read_xml(reader).map(ContentDummy)
}
}

#[cfg(test)]
mod test {
use super::*;
Expand All @@ -86,9 +108,9 @@ mod test {
fn deserialize_content_url() {
test_util::test_xml_deserialize(
r#"
<Content name="something">
<ContentId name="something">
<url>Some URL</url>
</Content>
</ContentId>
"#,
&Content::from("Some URL"),
);
Expand All @@ -98,9 +120,9 @@ mod test {
fn deserialize_content_null() {
test_util::test_xml_deserialize(
r#"
<Content name="something">
<ContentId name="something">
<null></null>
</Content>
</ContentId>
"#,
&Content::new(),
);
Expand All @@ -110,9 +132,9 @@ mod test {
fn serialize_content_url() {
test_util::test_xml_serialize(
r#"
<Content name="foo">
<ContentId name="foo">
<url>Some URL</url>
</Content>
</ContentId>
"#,
&Content::from("Some URL"),
);
Expand All @@ -122,9 +144,9 @@ mod test {
fn serialize_content_null() {
test_util::test_xml_serialize(
r#"
<Content name="foo">
<ContentId name="foo">
<null></null>
</Content>
</ContentId>
"#,
&Content::new(),
);
Expand Down
4 changes: 4 additions & 0 deletions rbx_xml/src/types/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,10 @@ macro_rules! declare_rbx_types {
let value = self::strings::ProtectedStringDummy::read_outer_xml(reader)?;
Ok(Some(Variant::String(value.0)))
},
self::content::ContentDummy::XML_TAG_NAME => {
let value = self::content::ContentDummy::read_outer_xml(reader)?;
Ok(Some(Variant::Content(value.0)))
}

self::referent::XML_TAG_NAME => Ok(Some(Variant::Ref(read_ref(reader, instance_id, property_name, state)?))),
self::shared_string::XML_TAG_NAME => read_shared_string(reader, instance_id, property_name, state).map(Some),
Expand Down

0 comments on commit b72bcbf

Please sign in to comment.