Skip to content

Commit

Permalink
to_x -> as_x
Browse files Browse the repository at this point in the history
  • Loading branch information
set authored and Corecii committed Mar 12, 2023
1 parent bf6b91e commit 735af5c
Show file tree
Hide file tree
Showing 5 changed files with 151 additions and 11 deletions.
24 changes: 20 additions & 4 deletions rbx_binary/src/deserializer/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -401,7 +401,14 @@ impl<'a, R: Read> DeserializerState<'a, R> {
for referent in &type_info.referents {
let instance = self.instances_by_ref.get_mut(referent).unwrap();
let value = chunk.read_bool()?;
instance.builder.add_property(&canonical_name, value);
if canonical_name == "IgnoreGuiInset" {
instance.builder.add_property(
"ScreenInsets",
Enum::from_u32(if value { 1 } else { 2 }),
)
} else {
instance.builder.add_property(&canonical_name, value);
}
}
}
invalid_type => {
Expand Down Expand Up @@ -802,9 +809,18 @@ impl<'a, R: Read> DeserializerState<'a, R> {

for (value, referent) in values.into_iter().zip(&type_info.referents) {
let instance = self.instances_by_ref.get_mut(referent).unwrap();
instance
.builder
.add_property(&canonical_name, Enum::from_u32(value));
if canonical_name == "Font" {
let font_value = Font::from_font_enum(value);
if let Some(font_value) = font_value {
instance.builder.add_property(&canonical_name, font_value);
} else {
log::warn!("Invalid value for Font enum {}, ignoring", value);
}
} else {
instance
.builder
.add_property(&canonical_name, Enum::from_u32(value));
}
}
}
invalid_type => {
Expand Down
4 changes: 2 additions & 2 deletions rbx_binary/src/serializer/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -713,8 +713,8 @@ impl<'dom, W: Write> SerializerState<'dom, W> {
for (i, rbx_value) in values {
if let Variant::Font(value) = rbx_value.as_ref() {
chunk.write_string(&value.family)?;
chunk.write_le_u16(value.weight.to_u16())?;
chunk.write_u8(value.style.to_u8())?;
chunk.write_le_u16(value.weight.as_u16())?;
chunk.write_u8(value.style.as_u8())?;
chunk.write_string(
&value.cached_face_id.clone().unwrap_or_default(),
)?;
Expand Down
104 changes: 102 additions & 2 deletions rbx_types/src/font.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ impl FontWeight {
_ => FontWeight::Regular,
}
}
pub fn to_u16(self) -> u16 {
pub fn as_u16(self) -> u16 {
match self {
FontWeight::Thin => 100,
FontWeight::ExtraLight => 200,
Expand Down Expand Up @@ -70,7 +70,7 @@ impl FontStyle {
}
}

pub fn to_u8(self) -> u8 {
pub fn as_u8(self) -> u8 {
match self {
FontStyle::Normal => 0,
FontStyle::Italic => 1,
Expand Down Expand Up @@ -102,3 +102,103 @@ impl Default for Font {
}
}
}

impl Font {
pub fn new(family: &str, weight: FontWeight, style: FontStyle) -> Self {
Self {
family: family.to_owned(),
weight,
style,
cached_face_id: None,
}
}
pub fn regular(family: &str) -> Self {
Self {
family: family.to_owned(),
..Default::default()
}
}
pub fn from_font_enum(value: u32) -> Option<Font> {
return Some(match value {
0 => Font::regular(&"rbxasset://fonts/families/LegacyArial.json"),
1 => Font::regular(&"rbxasset://fonts/families/Arial.json"),
2 => Font::new(
&"rbxasset://fonts/families/Arial.json",
FontWeight::Bold,
FontStyle::Normal,
),
3 => Font::regular(&"rbxasset://fonts/families/SourceSansPro.json"),
4 => Font::new(
&"rbxasset://fonts/families/SourceSansPro.json",
FontWeight::Bold,
FontStyle::Normal,
),
16 => Font::new(
&"rbxasset://fonts/families/SourceSansPro.json",
FontWeight::SemiBold,
FontStyle::Normal,
),
5 => Font::new(
&"rbxasset://fonts/families/SourceSansPro.json",
FontWeight::Light,
FontStyle::Normal,
),
6 => Font::new(
&"rbxasset://fonts/families/SourceSansPro.json",
FontWeight::Regular,
FontStyle::Italic,
),
7 => Font::regular(&"rbxasset://fonts/families/AccanthisADFStd.json"),
8 => Font::regular(&"rbxasset://fonts/families/Guru.json"),
9 => Font::regular(&"rbxasset://fonts/families/ComicNeueAngular.json"),
10 => Font::regular(&"rbxasset://fonts/families/Inconsolata.json"),
11 => Font::regular(&"rbxasset://fonts/families/HighwayGothic.json"),
12 => Font::regular(&"rbxasset://fonts/families/Zekton.json"),
13 => Font::regular(&"rbxasset://fonts/families/PressStart2P.json"),
14 => Font::regular(&"rbxasset://fonts/families/Balthazar.json"),
15 => Font::regular(&"rbxasset://fonts/families/RomanAntique.json"),
17 => Font::regular(&"rbxasset://fonts/families/GothamSSm.json"),
18 => Font::new(
&"rbxasset://fonts/families/GothamSSm.json",
FontWeight::Medium,
FontStyle::Normal,
),
19 => Font::new(
&"rbxasset://fonts/families/GothamSSm.json",
FontWeight::Bold,
FontStyle::Normal,
),
20 => Font::new(
&"rbxasset://fonts/families/GothamSSm.json",
FontWeight::Heavy,
FontStyle::Normal,
),
21 => Font::regular(&"rbxasset://fonts/families/AmaticSC.json"),
22 => Font::regular(&"rbxasset://fonts/families/Bangers.json"),
23 => Font::regular(&"rbxasset://fonts/families/Creepster.json"),
24 => Font::regular(&"rbxasset://fonts/families/DenkOne.json"),
25 => Font::regular(&"rbxasset://fonts/families/Fondamento.json"),
26 => Font::regular(&"rbxasset://fonts/families/FredokaOne.json"),
27 => Font::regular(&"rbxasset://fonts/families/GrenzeGotisch.json"),
28 => Font::regular(&"rbxasset://fonts/families/IndieFlower.json"),
29 => Font::regular(&"rbxasset://fonts/families/JosefinSans.json"),
30 => Font::regular(&"rbxasset://fonts/families/Jura.json"),
31 => Font::regular(&"rbxasset://fonts/families/Kalam.json"),
32 => Font::regular(&"rbxasset://fonts/families/LuckiestGuy.json"),
33 => Font::regular(&"rbxasset://fonts/families/Merriweather.json"),
34 => Font::regular(&"rbxasset://fonts/families/Michroma.json"),
35 => Font::regular(&"rbxasset://fonts/families/Nunito.json"),
36 => Font::regular(&"rbxasset://fonts/families/Oswald.json"),
37 => Font::regular(&"rbxasset://fonts/families/PatrickHand.json"),
38 => Font::regular(&"rbxasset://fonts/families/PermanentMarker.json"),
39 => Font::regular(&"rbxasset://fonts/families/Roboto.json"),
40 => Font::regular(&"rbxasset://fonts/families/RobotoCondensed.json"),
41 => Font::regular(&"rbxasset://fonts/families/RobotoMono.json"),
42 => Font::regular(&"rbxasset://fonts/families/Sarpanch.json"),
43 => Font::regular(&"rbxasset://fonts/families/SpecialElite.json"),
44 => Font::regular(&"rbxasset://fonts/families/TitilliumWeb.json"),
45 => Font::regular(&"rbxasset://fonts/families/Ubuntu.json"),
_ => return None,
});
}
}
28 changes: 26 additions & 2 deletions rbx_xml/src/deserializer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::{

use log::trace;
use rbx_dom_weak::{
types::{Ref, SharedString, Variant, VariantType},
types::{Enum, Font, Ref, SharedString, Variant, VariantType},
InstanceBuilder, WeakDom,
};
use rbx_reflection::DataType;
Expand Down Expand Up @@ -607,7 +607,31 @@ fn deserialize_properties<R: Read>(
}
};

props.insert(descriptor.name.to_string(), value);
match descriptor.name.as_ref() {
"Font" => match value {
Variant::Enum(value) => match Font::from_font_enum(value.to_u32()) {
Some(value) => {
props.insert(descriptor.name.to_string(), value.into());
}
None => {
log::warn!("Invalid value for Font enum {}, ignoring", value.to_u32())
}
},
_ => log::warn!("Invalid type for Font enum {:?}, ignoring", value),
},
"IgnoreGuiInset" => match value {
Variant::Bool(value) => {
props.insert(
"ScreenInsets".to_string(),
Enum::from_u32(if value { 1 } else { 2 }).into(),
);
}
_ => log::warn!("Invalid type for IgnoreGuiInset bool {:?}, ignoring", value),
},
_ => {
props.insert(descriptor.name.to_string(), value);
}
};
} else {
match state.options.property_behavior {
DecodePropertyBehavior::IgnoreUnknown => {
Expand Down
2 changes: 1 addition & 1 deletion rbx_xml/src/types/font.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ impl XmlType for Font {
fn write_xml<W: Write>(&self, writer: &mut XmlEventWriter<W>) -> Result<(), EncodeError> {
write_content(writer, &self.family, "Family")?;

writer.write_value_in_tag(&self.weight.to_u16(), "Weight")?;
writer.write_value_in_tag(&self.weight.as_u16(), "Weight")?;

let style = match self.style {
FontStyle::Normal => "Normal",
Expand Down

0 comments on commit 735af5c

Please sign in to comment.