Skip to content

Commit

Permalink
Merge branch 'dev' of https://github.com/PikminGuts92/grim into dev
Browse files Browse the repository at this point in the history
  • Loading branch information
PikminGuts92 committed Jul 16, 2023
2 parents 1ef5455 + 5107c3c commit ce0dcc9
Show file tree
Hide file tree
Showing 4 changed files with 119 additions and 0 deletions.
79 changes: 79 additions & 0 deletions core/grim/src/scene/color_palette/io.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
use crate::io::{BinaryStream, SeekFrom, Stream};
use crate::scene::*;
use crate::SystemInfo;
use grim_traits::scene::*;
use std::error::Error;
use thiserror::Error as ThisError;
use log::info;

#[derive(Debug, ThisError)]
pub enum ColorPaletteLoadError {
#[error("ColorPalette version {version} is not supported")]
ColorPaletteVersionNotSupported {
version: u32
},
}

fn is_version_supported(version: u32) -> bool {
match version {
1 => true, // RB1 and up, I don't think a v2 exists
_ => false
}
}

impl ObjectReadWrite for ColorPalette {
fn load(&mut self, stream: &mut dyn Stream, info: &SystemInfo) -> Result<(), Box<dyn Error>> {
let mut reader = Box::new(BinaryStream::from_stream_with_endian(stream, info.endian));

let version = reader.read_uint32()?;
if !is_version_supported(version) {
return Err(Box::new(ColorPaletteLoadError::ColorPaletteVersionNotSupported {
version
}));
}

load_object(self, &mut reader, info)?;

self.num_colors = reader.read_uint32()?;
self.colors = load_palette_colors(self.num_colors, &mut reader)?;

Ok(())
}

fn save(&self, stream: &mut dyn Stream, info: &SystemInfo) -> Result<(), Box<dyn Error>> {
let mut stream = Box::new(BinaryStream::from_stream_with_endian(stream, info.endian));

// TODO: Get version from system info
let version = 1;

stream.write_uint32(version)?;

save_object(self, &mut stream, info)?;

stream.write_uint32(self.num_colors)?;

for color in &self.colors {
stream.write_float32(color.red)?;
stream.write_float32(color.green)?;
stream.write_float32(color.blue)?;
stream.write_float32(color.unknown)?;
}

Ok(())
}
}

fn load_palette_colors(num_colors: u32, reader: &mut Box<BinaryStream>,) -> Result<Vec<PaletteColor>, Box<dyn Error>> {
let mut colors = Vec::new();

for _ in 0..num_colors {
let red = reader.read_float32()?;
let green = reader.read_float32()?;
let blue = reader.read_float32()?;
let unknown = reader.read_float32()?;

colors.push(PaletteColor {red, green, blue, unknown, })
}

Ok(colors)
}
33 changes: 33 additions & 0 deletions core/grim/src/scene/color_palette/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
mod io;

use grim_macros::*;
use grim_traits::scene::*;
pub use io::*;

pub struct PaletteColor {
pub red: f32,
pub green: f32,
pub blue: f32,
pub unknown: f32, // TODO: figure out what this is
}

#[milo]
pub struct ColorPalette {
pub num_colors: u32,
pub colors: Vec<PaletteColor>,
}

impl Default for ColorPalette {
fn default() -> Self {
Self {
// Base object
name: String::default(),
type2: String::default(),
note: String::default(),

// ColorPalette object
num_colors: 0,
colors: Vec::new(),
}
}
}
2 changes: 2 additions & 0 deletions core/grim/src/scene/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ mod char_driver;
mod char_weightable;
mod char_lip_sync;
mod character;
mod color_palette;
mod cube_tex;
mod draw;
mod group;
Expand Down Expand Up @@ -42,6 +43,7 @@ pub use char_driver::*;
pub use char_weightable::*;
pub use char_lip_sync::*;
pub use character::*;
pub use color_palette::*;
pub use cube_tex::*;
pub use draw::*;
pub use group::*;
Expand Down
5 changes: 5 additions & 0 deletions core/grim/src/scene/object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ pub enum Object {
Cam(CamObject),
CharClipSamples(CharClipSamples),
CharLipSync(CharLipSync),
ColorPalette(ColorPalette),
CubeTex(CubeTexObject),
Draw(DrawObject),
Group(GroupObject),
Expand Down Expand Up @@ -39,6 +40,7 @@ impl Object {
Object::Cam(cam) => &cam.name,
Object::CharClipSamples(ccs) => &ccs.name,
Object::CharLipSync(cls) => &cls.name,
Object::ColorPalette(color_palette) => &color_palette.name,
Object::CubeTex(cube) => &cube.name,
Object::Draw(draw) => &draw.name,
Object::Group(grp) => &grp.name,
Expand All @@ -63,6 +65,7 @@ impl Object {
Object::Cam(_) => "Cam",
Object::CharClipSamples(_) => "CharClipSamples",
Object::CharLipSync(_) => "CharLipSync",
Object::ColorPalette(_) => "ColorPalette",
Object::CubeTex(_) => "CubeTex",
Object::Draw(_) => "Draw",
Object::Group(_) => "Group",
Expand Down Expand Up @@ -96,6 +99,7 @@ impl Object {
Object::Anim(obj) => obj,
Object::BandPlacer(obj) => obj,
Object::Cam(obj) => obj,
Object::ColorPalette(obj) => obj,
Object::CubeTex(obj) => obj,
Object::Draw(obj) => obj,
Object::Group(obj) => obj,
Expand Down Expand Up @@ -135,6 +139,7 @@ impl Object {
"Cam" => unpack_object(packed, info).map(|o| Object::Cam(o)),
"CharClipSamples" => unpack_object(packed, info).map(|o| Object::CharClipSamples(o)),
"CharLipSync" => unpack_object(packed, info).map(|o| Object::CharLipSync(o)),
"ColorPalette" => unpack_object(packed, info).map(|o| Object::ColorPalette(o)),
"CubeTex" => unpack_object(packed, info).map(|o| Object::CubeTex(o)),
"Draw" => unpack_object(packed, info).map(|o| Object::Draw(o)),
"Group" => unpack_object(packed, info).map(|o| Object::Group(o)),
Expand Down

0 comments on commit ce0dcc9

Please sign in to comment.