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 {
type Error = &'static str;
type Error = String;
fn try_from(value: u8) -> Result<Self, Self::Error> {
let value = match value {
@ -53,7 +53,7 @@ impl TryFrom<u8> for CzVersion {
3 => Self::CZ3,
4 => Self::CZ4,
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)
@ -61,7 +61,7 @@ impl TryFrom<u8> for CzVersion {
}
impl TryFrom<char> for CzVersion {
type Error = &'static str;
type Error = String;
fn try_from(value: char) -> Result<Self, Self::Error> {
let value = match value {
@ -71,7 +71,7 @@ impl TryFrom<char> for CzVersion {
'3' => Self::CZ3,
'4' => Self::CZ4,
'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)

View file

@ -13,7 +13,7 @@ use crate::{
/// A CZ# interface which can open and save any CZ file type.
#[derive(Debug, Clone)]
pub struct DynamicCz {
pub struct CzFile {
header_common: CommonHeader,
header_extended: Option<ExtendedHeader>,
@ -24,7 +24,7 @@ pub struct DynamicCz {
bitmap: Vec<u8>,
}
impl DynamicCz {
impl CzFile {
/// Decode a CZ# file from anything that implements [`Read`] and [`Seek`]
///
/// 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> {
let (compressed_data, compressed_info) = compress2(&bitmap);
let (compressed_data, compressed_info) = compress2(bitmap);
compressed_info.write_into(output)?;

View file

@ -17,14 +17,14 @@ use common::CzError;
use std::{io::BufReader, path::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)?);
DynamicCz::decode(&mut img_file)
CzFile::decode(&mut img_file)
}
#[doc(inline)]
pub use dynamic::DynamicCz;
pub use dynamic::CzFile;
/*
#[doc(inline)]

View file

@ -1,6 +1,6 @@
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 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]
fn cz0_round_trip() {
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();
original_cz.encode(&mut cz_bytes).unwrap();
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());
}
@ -28,13 +28,13 @@ fn cz0_round_trip() {
#[test]
fn cz1_round_trip() {
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();
original_cz.encode(&mut cz_bytes).unwrap();
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());
}
@ -42,32 +42,29 @@ fn cz1_round_trip() {
#[test]
fn cz2_round_trip() {
let mut i = 0;
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();
original_cz.encode(&mut cz_bytes).unwrap();
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());
i += 1;
}
}
#[test]
fn cz3_round_trip() {
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();
original_cz.encode(&mut cz_bytes).unwrap();
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());
}
@ -76,13 +73,13 @@ fn cz3_round_trip() {
#[test]
fn cz4_round_trip() {
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();
original_cz.encode(&mut cz_bytes).unwrap();
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());
}

View file

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

View file

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

View file

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

View file

@ -8,10 +8,16 @@ fn main() {
let si = SysinfoBuilder::all_sysinfo().unwrap();
Emitter::default()
.add_instructions(&build).unwrap()
.add_instructions(&cargo).unwrap()
.add_instructions(&gitcl).unwrap()
.add_instructions(&rustc).unwrap()
.add_instructions(&si).unwrap()
.emit().unwrap();
.add_instructions(&build)
.unwrap()
.add_instructions(&cargo)
.unwrap()
.add_instructions(&gitcl)
.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 cz::{common::CzVersion, CzFile};
use image::ColorType;
use lbee_utils::version;
use owo_colors::OwoColorize;
use std::{
fs,
path::{Path, PathBuf},
@ -24,7 +26,7 @@ struct Cli {
#[derive(Subcommand)]
enum Commands {
/// Converts a CZ file to a PNG
/// Decode a CZ file to a PNG
Decode {
/// Decode a whole folder, and output to another folder
#[arg(short, long)]
@ -39,7 +41,26 @@ enum Commands {
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 a whole folder, and output to another folder,
/// using a folder of replacements
@ -89,28 +110,19 @@ fn main() {
batch,
} => {
if !input.exists() {
Error::raw(
ErrorKind::ValueValidation,
"The input file/folder provided does not exist\n",
)
.exit()
pretty_error("The input file/folder provided does not exist");
exit(1);
}
if *batch {
if input.is_file() {
Error::raw(
ErrorKind::ValueValidation,
"Batch input must be a directory\n",
)
.exit()
pretty_error("Batch input must be a directory");
exit(1);
}
if output.is_none() || output.as_ref().unwrap().is_file() {
Error::raw(
ErrorKind::ValueValidation,
"Batch output must be a directory\n",
)
.exit()
pretty_error("Batch output must be a directory");
exit(1);
}
for entry in fs::read_dir(input).unwrap() {
@ -128,15 +140,10 @@ fn main() {
let cz = match cz::open(&path) {
Ok(cz) => cz,
Err(_) => {
Error::raw(
ErrorKind::ValueValidation,
format!(
"Could not open input as a CZ file: {}\n",
path.into_os_string().to_str().unwrap()
),
)
.print()
.unwrap();
pretty_error(&format!(
"Could not open input as a CZ file: {}\n",
path.into_os_string().to_str().unwrap()
));
continue;
}
};
@ -147,8 +154,9 @@ fn main() {
cz.header().width() as u32,
cz.header().height() as u32,
ColorType::Rgba8,
image::ImageFormat::Png
).unwrap();
image::ImageFormat::Png,
)
.unwrap();
}
} else {
let cz = cz::open(input).unwrap();
@ -160,8 +168,9 @@ fn main() {
cz.header().width() as u32,
cz.header().height() as u32,
ColorType::Rgba8,
image::ImageFormat::Png
).unwrap();
image::ImageFormat::Png,
)
.unwrap();
} else {
let file_stem = PathBuf::from(input.file_name().unwrap());
image::save_buffer_with_format(
@ -170,8 +179,9 @@ fn main() {
cz.header().width() as u32,
cz.header().height() as u32,
ColorType::Rgba8,
image::ImageFormat::Png
).unwrap();
image::ImageFormat::Png,
)
.unwrap();
}
}
}
@ -184,45 +194,30 @@ fn main() {
depth,
} => {
if !input.exists() {
Error::raw(
ErrorKind::ValueValidation,
"The original file provided does not exist\n",
)
.exit()
pretty_error("The input file does not exist");
exit(1);
}
if !replacement.exists() {
Error::raw(
ErrorKind::ValueValidation,
"The replacement file provided does not exist\n",
)
.exit()
pretty_error("The replacement file does not exist");
exit(1);
}
// If it's a batch replacement, we want directories to search
if *batch {
if !input.is_dir() {
Error::raw(
ErrorKind::ValueValidation,
"Batch input location must be a directory\n",
)
.exit()
pretty_error("Batch input must be a directory");
exit(1);
}
if !replacement.is_dir() {
Error::raw(
ErrorKind::ValueValidation,
"Batch replacement location must be a directory\n",
)
.exit()
pretty_error("Batch replacement must be a directory");
exit(1);
}
if !output.is_dir() {
Error::raw(
ErrorKind::ValueValidation,
"Batch output location must be a directory\n",
)
.exit()
pretty_error("Batch output location must be a directory");
exit(1);
}
// Replace all the files within the directory and print errors for them
@ -254,17 +249,89 @@ fn main() {
}
} else {
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() {
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_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
let repl_img = image::open(&replacement_path)?.to_rgba8();
let repl_img = image::open(replacement_path)?.to_rgba8();
// Open the original CZ file
let mut cz = cz::open(&path)?;
@ -310,3 +377,7 @@ fn replace_cz<P: ?Sized + AsRef<Path>>(
Ok(())
}
fn pretty_error(message: &str) {
eprintln!("{}: {}", "Error".red().italic(), message);
}

View file

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