mirror of
https://github.com/G2-Games/lbee-utils.git
synced 2025-04-19 23:32:55 -05:00
Compare commits
No commits in common. "01fdb340fa95d41576d24a73b92be403f34ecf98" and "a7a486999d5ec8311b4e9a39aaf8391ad4b75677" have entirely different histories.
01fdb340fa
...
a7a486999d
11 changed files with 101 additions and 181 deletions
|
@ -43,7 +43,7 @@ pub enum CzVersion {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl TryFrom<u8> for CzVersion {
|
impl TryFrom<u8> for CzVersion {
|
||||||
type Error = String;
|
type Error = &'static str;
|
||||||
|
|
||||||
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,
|
||||||
v => return Err(format!("{} is not a valid CZ version", v)),
|
_ => return Err("Value is not a valid CZ version"),
|
||||||
};
|
};
|
||||||
|
|
||||||
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 = String;
|
type Error = &'static str;
|
||||||
|
|
||||||
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,
|
||||||
v => return Err(format!("{} is not a valid CZ version", v)),
|
_ => return Err("Value is not a valid CZ version"),
|
||||||
};
|
};
|
||||||
|
|
||||||
Ok(value)
|
Ok(value)
|
||||||
|
|
|
@ -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 CzFile {
|
pub struct DynamicCz {
|
||||||
header_common: CommonHeader,
|
header_common: CommonHeader,
|
||||||
header_extended: Option<ExtendedHeader>,
|
header_extended: Option<ExtendedHeader>,
|
||||||
|
|
||||||
|
@ -24,7 +24,7 @@ pub struct CzFile {
|
||||||
bitmap: Vec<u8>,
|
bitmap: Vec<u8>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl CzFile {
|
impl DynamicCz {
|
||||||
/// 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
|
||||||
|
|
|
@ -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)?;
|
||||||
|
|
||||||
|
|
|
@ -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<CzFile, CzError> {
|
pub fn open<P: ?Sized + AsRef<Path>>(path: &P) -> Result<DynamicCz, CzError> {
|
||||||
let mut img_file = BufReader::new(std::fs::File::open(path)?);
|
let mut img_file = BufReader::new(std::fs::File::open(path)?);
|
||||||
|
|
||||||
CzFile::decode(&mut img_file)
|
DynamicCz::decode(&mut img_file)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[doc(inline)]
|
#[doc(inline)]
|
||||||
pub use dynamic::CzFile;
|
pub use dynamic::DynamicCz;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
#[doc(inline)]
|
#[doc(inline)]
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
use std::io::Cursor;
|
use std::io::Cursor;
|
||||||
|
|
||||||
use cz::{common::CzVersion, CzFile};
|
use cz::{common::CzVersion, DynamicCz};
|
||||||
|
|
||||||
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 = CzFile::from_raw(CzVersion::CZ0, image.0, image.1, image.2.to_vec());
|
let original_cz = DynamicCz::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 = CzFile::decode(&mut cz_bytes).unwrap();
|
let decoded_cz = DynamicCz::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 = CzFile::from_raw(CzVersion::CZ1, image.0, image.1, image.2.to_vec());
|
let original_cz = DynamicCz::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 = CzFile::decode(&mut cz_bytes).unwrap();
|
let decoded_cz = DynamicCz::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,29 +42,32 @@ 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 = CzFile::from_raw(CzVersion::CZ2, image.0, image.1, image.2.to_vec());
|
let original_cz = DynamicCz::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 = CzFile::decode(&mut cz_bytes).unwrap();
|
let decoded_cz = DynamicCz::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 = CzFile::from_raw(CzVersion::CZ3, image.0, image.1, image.2.to_vec());
|
let original_cz = DynamicCz::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 = CzFile::decode(&mut cz_bytes).unwrap();
|
let decoded_cz = DynamicCz::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());
|
||||||
}
|
}
|
||||||
|
@ -73,13 +76,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 = CzFile::from_raw(CzVersion::CZ4, image.0, image.1, image.2.to_vec());
|
let original_cz = DynamicCz::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 = CzFile::decode(&mut cz_bytes).unwrap();
|
let decoded_cz = DynamicCz::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());
|
||||||
}
|
}
|
||||||
|
|
|
@ -58,10 +58,6 @@ 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
|
||||||
|
@ -70,7 +66,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
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
pub mod entry;
|
pub mod entry;
|
||||||
pub mod header;
|
pub mod header;
|
||||||
|
|
||||||
use byteorder_lite::{ReadBytesExt, WriteBytesExt, LE};
|
use byteorder_lite::{LE, ReadBytesExt, WriteBytesExt};
|
||||||
use header::Header;
|
use header::Header;
|
||||||
use log::{debug, info};
|
use log::{debug, info};
|
||||||
use std::{
|
use std::{
|
||||||
|
|
|
@ -1,9 +1,7 @@
|
||||||
#![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::{
|
use eframe::egui::{self, ColorImage, Image, TextureFilter, TextureHandle, TextureOptions, ThemePreference};
|
||||||
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;
|
||||||
|
@ -158,18 +156,18 @@ impl eframe::App for PakExplorer {
|
||||||
.set_file_name(display_name)
|
.set_file_name(display_name)
|
||||||
.save_file()
|
.save_file()
|
||||||
{
|
{
|
||||||
let cz =
|
let cz = cz::DynamicCz::decode(&mut std::io::Cursor::new(
|
||||||
cz::CzFile::decode(&mut std::io::Cursor::new(entry.as_bytes()))
|
entry.as_bytes(),
|
||||||
.unwrap();
|
))
|
||||||
|
.unwrap();
|
||||||
image::save_buffer_with_format(
|
image::save_buffer_with_format(
|
||||||
path,
|
path,
|
||||||
cz.as_raw(),
|
cz.as_raw(),
|
||||||
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();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -177,7 +175,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::CzFile::decode(&mut std::io::Cursor::new(entry.as_bytes()))
|
cz::DynamicCz::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],
|
||||||
|
|
|
@ -8,16 +8,10 @@ fn main() {
|
||||||
let si = SysinfoBuilder::all_sysinfo().unwrap();
|
let si = SysinfoBuilder::all_sysinfo().unwrap();
|
||||||
|
|
||||||
Emitter::default()
|
Emitter::default()
|
||||||
.add_instructions(&build)
|
.add_instructions(&build).unwrap()
|
||||||
.unwrap()
|
.add_instructions(&cargo).unwrap()
|
||||||
.add_instructions(&cargo)
|
.add_instructions(&gitcl).unwrap()
|
||||||
.unwrap()
|
.add_instructions(&rustc).unwrap()
|
||||||
.add_instructions(&gitcl)
|
.add_instructions(&si).unwrap()
|
||||||
.unwrap()
|
.emit().unwrap();
|
||||||
.add_instructions(&rustc)
|
|
||||||
.unwrap()
|
|
||||||
.add_instructions(&si)
|
|
||||||
.unwrap()
|
|
||||||
.emit()
|
|
||||||
.unwrap();
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,8 +1,6 @@
|
||||||
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},
|
||||||
|
@ -26,7 +24,7 @@ struct Cli {
|
||||||
|
|
||||||
#[derive(Subcommand)]
|
#[derive(Subcommand)]
|
||||||
enum Commands {
|
enum Commands {
|
||||||
/// Decode a CZ file to a PNG
|
/// Converts 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)]
|
||||||
|
@ -41,26 +39,7 @@ enum Commands {
|
||||||
output: Option<PathBuf>,
|
output: Option<PathBuf>,
|
||||||
},
|
},
|
||||||
|
|
||||||
/// Encode a PNG file to a CZ
|
/// Replace a CZ file's image data
|
||||||
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
|
||||||
|
@ -110,19 +89,28 @@ fn main() {
|
||||||
batch,
|
batch,
|
||||||
} => {
|
} => {
|
||||||
if !input.exists() {
|
if !input.exists() {
|
||||||
pretty_error("The input file/folder provided does not exist");
|
Error::raw(
|
||||||
exit(1);
|
ErrorKind::ValueValidation,
|
||||||
|
"The input file/folder provided does not exist\n",
|
||||||
|
)
|
||||||
|
.exit()
|
||||||
}
|
}
|
||||||
|
|
||||||
if *batch {
|
if *batch {
|
||||||
if input.is_file() {
|
if input.is_file() {
|
||||||
pretty_error("Batch input must be a directory");
|
Error::raw(
|
||||||
exit(1);
|
ErrorKind::ValueValidation,
|
||||||
|
"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() {
|
||||||
pretty_error("Batch output must be a directory");
|
Error::raw(
|
||||||
exit(1);
|
ErrorKind::ValueValidation,
|
||||||
|
"Batch output must be a directory\n",
|
||||||
|
)
|
||||||
|
.exit()
|
||||||
}
|
}
|
||||||
|
|
||||||
for entry in fs::read_dir(input).unwrap() {
|
for entry in fs::read_dir(input).unwrap() {
|
||||||
|
@ -140,10 +128,15 @@ fn main() {
|
||||||
let cz = match cz::open(&path) {
|
let cz = match cz::open(&path) {
|
||||||
Ok(cz) => cz,
|
Ok(cz) => cz,
|
||||||
Err(_) => {
|
Err(_) => {
|
||||||
pretty_error(&format!(
|
Error::raw(
|
||||||
"Could not open input as a CZ file: {}\n",
|
ErrorKind::ValueValidation,
|
||||||
path.into_os_string().to_str().unwrap()
|
format!(
|
||||||
));
|
"Could not open input as a CZ file: {}\n",
|
||||||
|
path.into_os_string().to_str().unwrap()
|
||||||
|
),
|
||||||
|
)
|
||||||
|
.print()
|
||||||
|
.unwrap();
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
@ -154,9 +147,8 @@ 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();
|
||||||
|
@ -168,9 +160,8 @@ 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(
|
||||||
|
@ -179,9 +170,8 @@ 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();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -194,30 +184,45 @@ fn main() {
|
||||||
depth,
|
depth,
|
||||||
} => {
|
} => {
|
||||||
if !input.exists() {
|
if !input.exists() {
|
||||||
pretty_error("The input file does not exist");
|
Error::raw(
|
||||||
exit(1);
|
ErrorKind::ValueValidation,
|
||||||
|
"The original file provided does not exist\n",
|
||||||
|
)
|
||||||
|
.exit()
|
||||||
}
|
}
|
||||||
|
|
||||||
if !replacement.exists() {
|
if !replacement.exists() {
|
||||||
pretty_error("The replacement file does not exist");
|
Error::raw(
|
||||||
exit(1);
|
ErrorKind::ValueValidation,
|
||||||
|
"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() {
|
||||||
pretty_error("Batch input must be a directory");
|
Error::raw(
|
||||||
exit(1);
|
ErrorKind::ValueValidation,
|
||||||
|
"Batch input location must be a directory\n",
|
||||||
|
)
|
||||||
|
.exit()
|
||||||
}
|
}
|
||||||
|
|
||||||
if !replacement.is_dir() {
|
if !replacement.is_dir() {
|
||||||
pretty_error("Batch replacement must be a directory");
|
Error::raw(
|
||||||
exit(1);
|
ErrorKind::ValueValidation,
|
||||||
|
"Batch replacement location must be a directory\n",
|
||||||
|
)
|
||||||
|
.exit()
|
||||||
}
|
}
|
||||||
|
|
||||||
if !output.is_dir() {
|
if !output.is_dir() {
|
||||||
pretty_error("Batch output location must be a directory");
|
Error::raw(
|
||||||
exit(1);
|
ErrorKind::ValueValidation,
|
||||||
|
"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
|
||||||
|
@ -249,89 +254,17 @@ fn main() {
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if !input.is_file() {
|
if !input.is_file() {
|
||||||
pretty_error("Input must be a file");
|
Error::raw(ErrorKind::ValueValidation, "Input must be a file\n").exit()
|
||||||
exit(1);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if !replacement.is_file() {
|
if !replacement.is_file() {
|
||||||
pretty_error("Replacement must be a file");
|
Error::raw(ErrorKind::ValueValidation, "Replacement must be a file\n").exit()
|
||||||
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");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -353,7 +286,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)?;
|
||||||
|
@ -377,7 +310,3 @@ fn replace_cz<P: ?Sized + AsRef<Path>>(
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn pretty_error(message: &str) {
|
|
||||||
eprintln!("{}: {}", "Error".red().italic(), message);
|
|
||||||
}
|
|
||||||
|
|
|
@ -2,8 +2,8 @@ use clap::{
|
||||||
error::{Error, ErrorKind},
|
error::{Error, ErrorKind},
|
||||||
Parser, Subcommand,
|
Parser, Subcommand,
|
||||||
};
|
};
|
||||||
use lbee_utils::version;
|
|
||||||
use luca_pak::Pak;
|
use luca_pak::Pak;
|
||||||
|
use lbee_utils::version;
|
||||||
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
|
||||||
|
|
Loading…
Reference in a new issue