Compare commits

..

3 commits

11 changed files with 181 additions and 101 deletions

View file

@ -43,7 +43,7 @@ pub enum CzVersion {
} }
impl TryFrom<u8> for CzVersion { impl TryFrom<u8> for CzVersion {
type Error = &'static str; type Error = String;
fn try_from(value: u8) -> Result<Self, Self::Error> { fn try_from(value: u8) -> Result<Self, Self::Error> {
let value = match value { let value = match value {
@ -53,7 +53,7 @@ impl TryFrom<u8> for CzVersion {
3 => Self::CZ3, 3 => Self::CZ3,
4 => Self::CZ4, 4 => Self::CZ4,
5 => Self::CZ5, 5 => Self::CZ5,
_ => return Err("Value is not a valid CZ version"), v => return Err(format!("{} is not a valid CZ version", v)),
}; };
Ok(value) Ok(value)
@ -61,7 +61,7 @@ impl TryFrom<u8> for CzVersion {
} }
impl TryFrom<char> for CzVersion { impl TryFrom<char> for CzVersion {
type Error = &'static str; type Error = String;
fn try_from(value: char) -> Result<Self, Self::Error> { fn try_from(value: char) -> Result<Self, Self::Error> {
let value = match value { let value = match value {
@ -71,7 +71,7 @@ impl TryFrom<char> for CzVersion {
'3' => Self::CZ3, '3' => Self::CZ3,
'4' => Self::CZ4, '4' => Self::CZ4,
'5' => Self::CZ5, '5' => Self::CZ5,
_ => return Err("Value is not a valid CZ version"), v => return Err(format!("{} is not a valid CZ version", v)),
}; };
Ok(value) Ok(value)

View file

@ -13,7 +13,7 @@ use crate::{
/// A CZ# interface which can open and save any CZ file type. /// A CZ# interface which can open and save any CZ file type.
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct DynamicCz { pub struct CzFile {
header_common: CommonHeader, header_common: CommonHeader,
header_extended: Option<ExtendedHeader>, header_extended: Option<ExtendedHeader>,
@ -24,7 +24,7 @@ pub struct DynamicCz {
bitmap: Vec<u8>, bitmap: Vec<u8>,
} }
impl DynamicCz { impl CzFile {
/// Decode a CZ# file from anything that implements [`Read`] and [`Seek`] /// Decode a CZ# file from anything that implements [`Read`] and [`Seek`]
/// ///
/// The input must begin with the /// The input must begin with the

View file

@ -15,7 +15,7 @@ pub fn decode<T: Seek + Read>(bytes: &mut T) -> Result<Vec<u8>, CzError> {
} }
pub fn encode<T: Write>(output: &mut T, bitmap: &[u8]) -> Result<(), CzError> { pub fn encode<T: Write>(output: &mut T, bitmap: &[u8]) -> Result<(), CzError> {
let (compressed_data, compressed_info) = compress2(&bitmap); let (compressed_data, compressed_info) = compress2(bitmap);
compressed_info.write_into(output)?; compressed_info.write_into(output)?;

View file

@ -17,14 +17,14 @@ use common::CzError;
use std::{io::BufReader, path::Path}; use std::{io::BufReader, path::Path};
/// Open a CZ# file from a path /// Open a CZ# file from a path
pub fn open<P: ?Sized + AsRef<Path>>(path: &P) -> Result<DynamicCz, CzError> { pub fn open<P: ?Sized + AsRef<Path>>(path: &P) -> Result<CzFile, CzError> {
let mut img_file = BufReader::new(std::fs::File::open(path)?); let mut img_file = BufReader::new(std::fs::File::open(path)?);
DynamicCz::decode(&mut img_file) CzFile::decode(&mut img_file)
} }
#[doc(inline)] #[doc(inline)]
pub use dynamic::DynamicCz; pub use dynamic::CzFile;
/* /*
#[doc(inline)] #[doc(inline)]

View file

@ -1,6 +1,6 @@
use std::io::Cursor; use std::io::Cursor;
use cz::{common::CzVersion, DynamicCz}; use cz::{common::CzVersion, CzFile};
const KODIM03: (u16, u16, &[u8]) = (128, 128, include_bytes!("test_images/kodim03.rgba")); const KODIM03: (u16, u16, &[u8]) = (128, 128, include_bytes!("test_images/kodim03.rgba"));
const KODIM23: (u16, u16, &[u8]) = (225, 225, include_bytes!("test_images/kodim23.rgba")); const KODIM23: (u16, u16, &[u8]) = (225, 225, include_bytes!("test_images/kodim23.rgba"));
@ -13,13 +13,13 @@ const TEST_IMAGES: &[TestImage] = &[KODIM03, KODIM23, SQPTEXT, DPFLOGO];
#[test] #[test]
fn cz0_round_trip() { fn cz0_round_trip() {
for image in TEST_IMAGES { for image in TEST_IMAGES {
let original_cz = DynamicCz::from_raw(CzVersion::CZ0, image.0, image.1, image.2.to_vec()); let original_cz = CzFile::from_raw(CzVersion::CZ0, image.0, image.1, image.2.to_vec());
let mut cz_bytes = Vec::new(); let mut cz_bytes = Vec::new();
original_cz.encode(&mut cz_bytes).unwrap(); original_cz.encode(&mut cz_bytes).unwrap();
let mut cz_bytes = Cursor::new(cz_bytes); let mut cz_bytes = Cursor::new(cz_bytes);
let decoded_cz = DynamicCz::decode(&mut cz_bytes).unwrap(); let decoded_cz = CzFile::decode(&mut cz_bytes).unwrap();
assert_eq!(original_cz.as_raw(), decoded_cz.as_raw()); assert_eq!(original_cz.as_raw(), decoded_cz.as_raw());
} }
@ -28,13 +28,13 @@ fn cz0_round_trip() {
#[test] #[test]
fn cz1_round_trip() { fn cz1_round_trip() {
for image in TEST_IMAGES { for image in TEST_IMAGES {
let original_cz = DynamicCz::from_raw(CzVersion::CZ1, image.0, image.1, image.2.to_vec()); let original_cz = CzFile::from_raw(CzVersion::CZ1, image.0, image.1, image.2.to_vec());
let mut cz_bytes = Vec::new(); let mut cz_bytes = Vec::new();
original_cz.encode(&mut cz_bytes).unwrap(); original_cz.encode(&mut cz_bytes).unwrap();
let mut cz_bytes = Cursor::new(cz_bytes); let mut cz_bytes = Cursor::new(cz_bytes);
let decoded_cz = DynamicCz::decode(&mut cz_bytes).unwrap(); let decoded_cz = CzFile::decode(&mut cz_bytes).unwrap();
assert_eq!(original_cz.as_raw(), decoded_cz.as_raw()); assert_eq!(original_cz.as_raw(), decoded_cz.as_raw());
} }
@ -42,32 +42,29 @@ fn cz1_round_trip() {
#[test] #[test]
fn cz2_round_trip() { fn cz2_round_trip() {
let mut i = 0;
for image in TEST_IMAGES { for image in TEST_IMAGES {
let original_cz = DynamicCz::from_raw(CzVersion::CZ2, image.0, image.1, image.2.to_vec()); let original_cz = CzFile::from_raw(CzVersion::CZ2, image.0, image.1, image.2.to_vec());
let mut cz_bytes = Vec::new(); let mut cz_bytes = Vec::new();
original_cz.encode(&mut cz_bytes).unwrap(); original_cz.encode(&mut cz_bytes).unwrap();
let mut cz_bytes = Cursor::new(cz_bytes); let mut cz_bytes = Cursor::new(cz_bytes);
let decoded_cz = DynamicCz::decode(&mut cz_bytes).unwrap(); let decoded_cz = CzFile::decode(&mut cz_bytes).unwrap();
assert_eq!(original_cz.as_raw(), decoded_cz.as_raw()); assert_eq!(original_cz.as_raw(), decoded_cz.as_raw());
i += 1;
} }
} }
#[test] #[test]
fn cz3_round_trip() { fn cz3_round_trip() {
for image in TEST_IMAGES { for image in TEST_IMAGES {
let original_cz = DynamicCz::from_raw(CzVersion::CZ3, image.0, image.1, image.2.to_vec()); let original_cz = CzFile::from_raw(CzVersion::CZ3, image.0, image.1, image.2.to_vec());
let mut cz_bytes = Vec::new(); let mut cz_bytes = Vec::new();
original_cz.encode(&mut cz_bytes).unwrap(); original_cz.encode(&mut cz_bytes).unwrap();
let mut cz_bytes = Cursor::new(cz_bytes); let mut cz_bytes = Cursor::new(cz_bytes);
let decoded_cz = DynamicCz::decode(&mut cz_bytes).unwrap(); let decoded_cz = CzFile::decode(&mut cz_bytes).unwrap();
assert_eq!(original_cz.as_raw(), decoded_cz.as_raw()); assert_eq!(original_cz.as_raw(), decoded_cz.as_raw());
} }
@ -76,13 +73,13 @@ fn cz3_round_trip() {
#[test] #[test]
fn cz4_round_trip() { fn cz4_round_trip() {
for image in TEST_IMAGES { for image in TEST_IMAGES {
let original_cz = DynamicCz::from_raw(CzVersion::CZ4, image.0, image.1, image.2.to_vec()); let original_cz = CzFile::from_raw(CzVersion::CZ4, image.0, image.1, image.2.to_vec());
let mut cz_bytes = Vec::new(); let mut cz_bytes = Vec::new();
original_cz.encode(&mut cz_bytes).unwrap(); original_cz.encode(&mut cz_bytes).unwrap();
let mut cz_bytes = Cursor::new(cz_bytes); let mut cz_bytes = Cursor::new(cz_bytes);
let decoded_cz = DynamicCz::decode(&mut cz_bytes).unwrap(); let decoded_cz = CzFile::decode(&mut cz_bytes).unwrap();
assert_eq!(original_cz.as_raw(), decoded_cz.as_raw()); assert_eq!(original_cz.as_raw(), decoded_cz.as_raw());
} }

View file

@ -58,6 +58,10 @@ impl Entry {
self.length as usize self.length as usize
} }
pub fn is_empty(&self) -> bool {
self.len() == 0
}
/// Get the raw byte data of an [`Entry`] /// Get the raw byte data of an [`Entry`]
pub fn as_bytes(&self) -> &Vec<u8> { pub fn as_bytes(&self) -> &Vec<u8> {
&self.data &self.data
@ -66,7 +70,7 @@ impl Entry {
pub fn display_name(&self) -> String { pub fn display_name(&self) -> String {
let mut name = self.name().clone().unwrap_or(self.id().to_string()); let mut name = self.name().clone().unwrap_or(self.id().to_string());
let entry_type = self.file_type(); let entry_type = self.file_type();
name.push_str(&entry_type.extension()); name.push_str(entry_type.extension());
name name
} }

View file

@ -1,7 +1,7 @@
pub mod entry; pub mod entry;
pub mod header; pub mod header;
use byteorder_lite::{LE, ReadBytesExt, WriteBytesExt}; use byteorder_lite::{ReadBytesExt, WriteBytesExt, LE};
use header::Header; use header::Header;
use log::{debug, info}; use log::{debug, info};
use std::{ use std::{

View file

@ -1,7 +1,9 @@
#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")] // hide console window on Windows in release #![cfg_attr(not(debug_assertions), windows_subsystem = "windows")] // hide console window on Windows in release
use colog; use colog;
use eframe::egui::{self, ColorImage, Image, TextureFilter, TextureHandle, TextureOptions, ThemePreference}; use eframe::egui::{
self, ColorImage, Image, TextureFilter, TextureHandle, TextureOptions, ThemePreference,
};
use log::error; use log::error;
use luca_pak::{entry::EntryType, Pak}; use luca_pak::{entry::EntryType, Pak};
use std::fs; use std::fs;
@ -156,9 +158,8 @@ impl eframe::App for PakExplorer {
.set_file_name(display_name) .set_file_name(display_name)
.save_file() .save_file()
{ {
let cz = cz::DynamicCz::decode(&mut std::io::Cursor::new( let cz =
entry.as_bytes(), cz::CzFile::decode(&mut std::io::Cursor::new(entry.as_bytes()))
))
.unwrap(); .unwrap();
image::save_buffer_with_format( image::save_buffer_with_format(
path, path,
@ -166,8 +167,9 @@ impl eframe::App for PakExplorer {
cz.header().width() as u32, cz.header().width() as u32,
cz.header().height() as u32, cz.header().height() as u32,
image::ColorType::Rgba8, image::ColorType::Rgba8,
image::ImageFormat::Png image::ImageFormat::Png,
).unwrap(); )
.unwrap();
} }
} }
@ -175,7 +177,7 @@ impl eframe::App for PakExplorer {
let texture: &TextureHandle = self.image_texture.get_or_insert_with(|| { let texture: &TextureHandle = self.image_texture.get_or_insert_with(|| {
let cz = let cz =
cz::DynamicCz::decode(&mut std::io::Cursor::new(entry.as_bytes())) cz::CzFile::decode(&mut std::io::Cursor::new(entry.as_bytes()))
.unwrap(); .unwrap();
let image = ColorImage::from_rgba_unmultiplied( let image = ColorImage::from_rgba_unmultiplied(
[cz.header().width() as usize, cz.header().height() as usize], [cz.header().width() as usize, cz.header().height() as usize],

View file

@ -8,10 +8,16 @@ fn main() {
let si = SysinfoBuilder::all_sysinfo().unwrap(); let si = SysinfoBuilder::all_sysinfo().unwrap();
Emitter::default() Emitter::default()
.add_instructions(&build).unwrap() .add_instructions(&build)
.add_instructions(&cargo).unwrap() .unwrap()
.add_instructions(&gitcl).unwrap() .add_instructions(&cargo)
.add_instructions(&rustc).unwrap() .unwrap()
.add_instructions(&si).unwrap() .add_instructions(&gitcl)
.emit().unwrap(); .unwrap()
.add_instructions(&rustc)
.unwrap()
.add_instructions(&si)
.unwrap()
.emit()
.unwrap();
} }

View file

@ -1,6 +1,8 @@
use clap::{error::ErrorKind, Error, Parser, Subcommand}; use clap::{error::ErrorKind, Error, Parser, Subcommand};
use cz::{common::CzVersion, CzFile};
use image::ColorType; use image::ColorType;
use lbee_utils::version; use lbee_utils::version;
use owo_colors::OwoColorize;
use std::{ use std::{
fs, fs,
path::{Path, PathBuf}, path::{Path, PathBuf},
@ -24,7 +26,7 @@ struct Cli {
#[derive(Subcommand)] #[derive(Subcommand)]
enum Commands { enum Commands {
/// Converts a CZ file to a PNG /// Decode a CZ file to a PNG
Decode { Decode {
/// Decode a whole folder, and output to another folder /// Decode a whole folder, and output to another folder
#[arg(short, long)] #[arg(short, long)]
@ -39,7 +41,26 @@ enum Commands {
output: Option<PathBuf>, output: Option<PathBuf>,
}, },
/// Replace a CZ file's image data /// Encode a PNG file to a CZ
Encode {
/// Input image to encode
#[arg(value_name = "INPUT")]
input: PathBuf,
/// Output CZ file location
#[arg(value_name = "OUTPUT")]
output: PathBuf,
/// Output CZ file version
#[arg(short, long, value_name = "CZ VERSION")]
version: Option<u8>,
/// Output CZ file bit depth
#[arg(short, long, value_name = "CZ BIT DEPTH")]
depth: Option<u16>,
},
/// Replace an existing CZ file's image data
Replace { Replace {
/// Replace a whole folder, and output to another folder, /// Replace a whole folder, and output to another folder,
/// using a folder of replacements /// using a folder of replacements
@ -89,28 +110,19 @@ fn main() {
batch, batch,
} => { } => {
if !input.exists() { if !input.exists() {
Error::raw( pretty_error("The input file/folder provided does not exist");
ErrorKind::ValueValidation, exit(1);
"The input file/folder provided does not exist\n",
)
.exit()
} }
if *batch { if *batch {
if input.is_file() { if input.is_file() {
Error::raw( pretty_error("Batch input must be a directory");
ErrorKind::ValueValidation, exit(1);
"Batch input must be a directory\n",
)
.exit()
} }
if output.is_none() || output.as_ref().unwrap().is_file() { if output.is_none() || output.as_ref().unwrap().is_file() {
Error::raw( pretty_error("Batch output must be a directory");
ErrorKind::ValueValidation, exit(1);
"Batch output must be a directory\n",
)
.exit()
} }
for entry in fs::read_dir(input).unwrap() { for entry in fs::read_dir(input).unwrap() {
@ -128,15 +140,10 @@ fn main() {
let cz = match cz::open(&path) { let cz = match cz::open(&path) {
Ok(cz) => cz, Ok(cz) => cz,
Err(_) => { Err(_) => {
Error::raw( pretty_error(&format!(
ErrorKind::ValueValidation,
format!(
"Could not open input as a CZ file: {}\n", "Could not open input as a CZ file: {}\n",
path.into_os_string().to_str().unwrap() path.into_os_string().to_str().unwrap()
), ));
)
.print()
.unwrap();
continue; continue;
} }
}; };
@ -147,8 +154,9 @@ fn main() {
cz.header().width() as u32, cz.header().width() as u32,
cz.header().height() as u32, cz.header().height() as u32,
ColorType::Rgba8, ColorType::Rgba8,
image::ImageFormat::Png image::ImageFormat::Png,
).unwrap(); )
.unwrap();
} }
} else { } else {
let cz = cz::open(input).unwrap(); let cz = cz::open(input).unwrap();
@ -160,8 +168,9 @@ fn main() {
cz.header().width() as u32, cz.header().width() as u32,
cz.header().height() as u32, cz.header().height() as u32,
ColorType::Rgba8, ColorType::Rgba8,
image::ImageFormat::Png image::ImageFormat::Png,
).unwrap(); )
.unwrap();
} else { } else {
let file_stem = PathBuf::from(input.file_name().unwrap()); let file_stem = PathBuf::from(input.file_name().unwrap());
image::save_buffer_with_format( image::save_buffer_with_format(
@ -170,8 +179,9 @@ fn main() {
cz.header().width() as u32, cz.header().width() as u32,
cz.header().height() as u32, cz.header().height() as u32,
ColorType::Rgba8, ColorType::Rgba8,
image::ImageFormat::Png image::ImageFormat::Png,
).unwrap(); )
.unwrap();
} }
} }
} }
@ -184,45 +194,30 @@ fn main() {
depth, depth,
} => { } => {
if !input.exists() { if !input.exists() {
Error::raw( pretty_error("The input file does not exist");
ErrorKind::ValueValidation, exit(1);
"The original file provided does not exist\n",
)
.exit()
} }
if !replacement.exists() { if !replacement.exists() {
Error::raw( pretty_error("The replacement file does not exist");
ErrorKind::ValueValidation, exit(1);
"The replacement file provided does not exist\n",
)
.exit()
} }
// If it's a batch replacement, we want directories to search // If it's a batch replacement, we want directories to search
if *batch { if *batch {
if !input.is_dir() { if !input.is_dir() {
Error::raw( pretty_error("Batch input must be a directory");
ErrorKind::ValueValidation, exit(1);
"Batch input location must be a directory\n",
)
.exit()
} }
if !replacement.is_dir() { if !replacement.is_dir() {
Error::raw( pretty_error("Batch replacement must be a directory");
ErrorKind::ValueValidation, exit(1);
"Batch replacement location must be a directory\n",
)
.exit()
} }
if !output.is_dir() { if !output.is_dir() {
Error::raw( pretty_error("Batch output location must be a directory");
ErrorKind::ValueValidation, exit(1);
"Batch output location must be a directory\n",
)
.exit()
} }
// Replace all the files within the directory and print errors for them // Replace all the files within the directory and print errors for them
@ -254,17 +249,89 @@ fn main() {
} }
} else { } else {
if !input.is_file() { if !input.is_file() {
Error::raw(ErrorKind::ValueValidation, "Input must be a file\n").exit() pretty_error("Input must be a file");
exit(1);
} }
if !replacement.is_file() { if !replacement.is_file() {
Error::raw(ErrorKind::ValueValidation, "Replacement must be a file\n").exit() pretty_error("Replacement must be a file");
exit(1);
} }
// Replace the input file with the new image // Replace the input file with the new image
replace_cz(&input, &output, &replacement, version, depth).unwrap(); replace_cz(&input, &output, &replacement, version, depth).unwrap();
} }
} }
Commands::Encode {
input,
output,
version,
depth,
} => {
if !input.exists() {
pretty_error("The original file provided does not exist");
exit(1);
}
let version = if let Some(v) = version {
match CzVersion::try_from(*v) {
Ok(v) => v,
Err(_) => {
pretty_error(&format!(
"Invalid CZ version {}; must be 0, 1, 2, 3, or 4",
v
));
exit(1);
}
}
} else if output
.extension()
.is_some_and(|e| e.to_ascii_lowercase().to_string_lossy().starts_with("cz"))
{
let ext_string = output.extension().unwrap().to_string_lossy();
let last_char = ext_string.chars().last().unwrap();
match CzVersion::try_from(last_char) {
Ok(v) => v,
Err(e) => {
pretty_error(&format!("Invalid CZ type: {}", e));
exit(1);
}
}
} else {
pretty_error("CZ version not specified or not parseable from file path");
exit(1);
};
let image = match image::open(input) {
Ok(i) => i,
Err(e) => {
pretty_error(&format!("Could not open input file: {e}"));
exit(1);
}
};
let image_depth = image.color();
let mut cz = CzFile::from_raw(
version,
image.width() as u16,
image.height() as u16,
image.to_rgba8().into_vec(),
);
if let Some(d) = *depth {
if !(d == 8 || d == 24 || d == 32) {
pretty_error(&format!(
"The color depth provided is not valid. Choose from: {}",
"8, 24, or 32".bright_magenta()
));
exit(1);
}
cz.header_mut().set_depth(d);
} else {
cz.header_mut().set_depth(image_depth.bits_per_pixel());
}
cz.save_as_cz(output).expect("Saving CZ file failed");
}
} }
} }
@ -286,7 +353,7 @@ fn replace_cz<P: ?Sized + AsRef<Path>>(
} }
// Open the replacement image and convert it to RGBA8 // Open the replacement image and convert it to RGBA8
let repl_img = image::open(&replacement_path)?.to_rgba8(); let repl_img = image::open(replacement_path)?.to_rgba8();
// Open the original CZ file // Open the original CZ file
let mut cz = cz::open(&path)?; let mut cz = cz::open(&path)?;
@ -310,3 +377,7 @@ fn replace_cz<P: ?Sized + AsRef<Path>>(
Ok(()) Ok(())
} }
fn pretty_error(message: &str) {
eprintln!("{}: {}", "Error".red().italic(), message);
}

View file

@ -2,8 +2,8 @@ use clap::{
error::{Error, ErrorKind}, error::{Error, ErrorKind},
Parser, Subcommand, Parser, Subcommand,
}; };
use luca_pak::Pak;
use lbee_utils::version; use lbee_utils::version;
use luca_pak::Pak;
use std::{fs, path::PathBuf, process::exit}; use std::{fs, path::PathBuf, process::exit};
/// Utility to maniuplate PAK archive files from the LUCA System game engine by /// Utility to maniuplate PAK archive files from the LUCA System game engine by