Skip to content

Commit

Permalink
feat: improve json color fields
Browse files Browse the repository at this point in the history
  • Loading branch information
francisdb committed Apr 22, 2024
1 parent e4702d0 commit 7305b25
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 21 deletions.
5 changes: 5 additions & 0 deletions src/vpx/color.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ pub struct ColorNoAlpha {
b: u8,
}

impl ColorNoAlpha {
pub const RED: ColorNoAlpha = ColorNoAlpha { r: 255, g: 0, b: 0 };
pub const BLACK: ColorNoAlpha = ColorNoAlpha { r: 0, g: 0, b: 0 };
}

/// Serialize as a string in the format "#RRGGBB".
impl Serialize for ColorNoAlpha {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
Expand Down
32 changes: 11 additions & 21 deletions src/vpx/gamedata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use super::{
version::Version,
};
use crate::vpx::biff::{BiffRead, BiffWrite};
use crate::vpx::color::{Color, ColorJson, ColorNoAlpha};
use crate::vpx::color::ColorNoAlpha;
use crate::vpx::json::F32WithNanInf;
use crate::vpx::material::{Material, SaveMaterial, SavePhysicsMaterial};
use crate::vpx::math::{dequantize_u8, quantize_u8};
Expand Down Expand Up @@ -221,7 +221,7 @@ pub struct GameData {
pub fonts_size: u32, // SFNT 110
pub collections_size: u32, // SCOL 111
pub name: String, // NAME 112
pub custom_colors: [Color; 16], //[Color; 16], // CCUS 113
pub custom_colors: [ColorNoAlpha; 16], //[Color; 16], // CCUS 113
pub protection_data: Option<Vec<u8>>, // SECB (removed in ?)
pub code: StringWithEncoding, // CODE 114
/// TLCK (added in 10.8 for tournament mode?)
Expand Down Expand Up @@ -375,7 +375,7 @@ pub(crate) struct GameDataJson {
pub tone_mapper: Option<i32>,
pub bloom_strength: f32,
pub name: String,
pub custom_colors: [ColorJson; 16],
pub custom_colors: [ColorNoAlpha; 16],
pub protection_data: Option<Vec<u8>>,
//pub code: StringWithEncoding,
pub locked: Option<u32>,
Expand All @@ -385,14 +385,6 @@ pub(crate) struct GameDataJson {

impl GameDataJson {
pub fn to_game_data(&self) -> GameData {
let custom_colors: [Color; 16] = self
.custom_colors
.iter()
.map(|c| c.to_color())
.collect::<Vec<Color>>()
.try_into()
.unwrap();

GameData {
left: self.left,
top: self.top,
Expand Down Expand Up @@ -559,7 +551,7 @@ impl GameDataJson {
// this data is loaded from a separate file
collections_size: 0,
name: self.name.clone(),
custom_colors,
custom_colors: self.custom_colors.clone(),
protection_data: self.protection_data.clone(),
code: StringWithEncoding::empty(),
locked: self.locked,
Expand All @@ -568,8 +560,6 @@ impl GameDataJson {
}

pub fn from_game_data(game_data: &GameData) -> GameDataJson {
let custom_colors: [ColorJson; 16] =
game_data.custom_colors.map(|c| ColorJson::from_color(&c));
GameDataJson {
left: game_data.left,
top: game_data.top,
Expand Down Expand Up @@ -722,7 +712,7 @@ impl GameDataJson {
tone_mapper: game_data.tone_mapper,
bloom_strength: game_data.bloom_strength,
name: game_data.name.clone(),
custom_colors,
custom_colors: game_data.custom_colors.clone(),
protection_data: game_data.protection_data.clone(),
// code: game_data.code.clone(),
locked: game_data.locked,
Expand Down Expand Up @@ -871,7 +861,7 @@ impl Default for GameData {
fonts_size: 0,
collections_size: 0,
name: "Table1".to_string(), // seems to be the default name
custom_colors: [Color::BLACK; 16],
custom_colors: [ColorNoAlpha::BLACK; 16],
protection_data: None,
code: StringWithEncoding::empty(),
bg_view_horizontal_offset_desktop: None,
Expand Down Expand Up @@ -1477,19 +1467,19 @@ pub fn read_all_gamedata_records(input: &[u8], version: &Version) -> GameData {
gamedata
}

fn read_colors(data: Vec<u8>) -> [Color; 16] {
fn read_colors(data: Vec<u8>) -> [ColorNoAlpha; 16] {
// COLORREF: 0x00BBGGRR
// sizeof(COLORREF) * 16
let mut colors = Vec::new();
let mut buff = BytesMut::from(data.as_slice());
for _ in 0..16 {
let color = Color::new_bgr(buff.get_u32_le());
let color = ColorNoAlpha::new_bgr(buff.get_u32_le());
colors.push(color);
}
<[Color; 16]>::try_from(colors).unwrap()
<[ColorNoAlpha; 16]>::try_from(colors).unwrap()
}

fn write_colors(colors: &[Color; 16]) -> Vec<u8> {
fn write_colors(colors: &[ColorNoAlpha; 16]) -> Vec<u8> {
let mut bytes = BytesMut::new();
for color in colors {
bytes.put_u32_le(color.bgr());
Expand Down Expand Up @@ -1637,7 +1627,7 @@ mod tests {
materials: None,
render_probes: Some(vec![Faker.fake(), Faker.fake()]),
name: String::from("test name"),
custom_colors: [Color::RED; 16],
custom_colors: [ColorNoAlpha::RED; 16],
protection_data: None,
code: StringWithEncoding::from("test code wit some unicode: Ǣ"),
bg_view_horizontal_offset_desktop: None,
Expand Down

0 comments on commit 7305b25

Please sign in to comment.