mirror of
https://github.com/G2-Games/lbee-utils.git
synced 2025-04-19 23:32:55 -05:00
Improved encode command
This commit is contained in:
parent
e68ca53ab5
commit
b74dc0344e
2 changed files with 43 additions and 63 deletions
|
@ -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)
|
||||||
|
|
|
@ -108,28 +108,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() {
|
||||||
|
@ -147,15 +138,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;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
@ -203,45 +189,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
|
||||||
|
@ -273,11 +244,13 @@ 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
|
||||||
|
@ -294,10 +267,6 @@ fn main() {
|
||||||
pretty_error("The original file provided does not exist");
|
pretty_error("The original file provided does not exist");
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
if output.exists() {
|
|
||||||
pretty_error("The output path already exists; not overwriting");
|
|
||||||
exit(1);
|
|
||||||
}
|
|
||||||
|
|
||||||
let version = if let Some(v) = version {
|
let version = if let Some(v) = version {
|
||||||
match CzVersion::try_from(*v) {
|
match CzVersion::try_from(*v) {
|
||||||
|
@ -313,7 +282,7 @@ fn main() {
|
||||||
match CzVersion::try_from(last_char) {
|
match CzVersion::try_from(last_char) {
|
||||||
Ok(v) => v,
|
Ok(v) => v,
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
pretty_error(&format!("Invalid CZ type {}", e));
|
pretty_error(&format!("Invalid CZ type: {}", e));
|
||||||
exit(1);
|
exit(1);
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
@ -323,19 +292,30 @@ fn main() {
|
||||||
};
|
};
|
||||||
|
|
||||||
let image = match image::open(input) {
|
let image = match image::open(input) {
|
||||||
Ok(i) => i.to_rgba8(),
|
Ok(i) => i,
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
pretty_error(&format!("Could not open input file: {e}"));
|
pretty_error(&format!("Could not open input file: {e}"));
|
||||||
exit(1);
|
exit(1);
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
let cz = CzFile::from_raw(
|
let image_depth = image.color();
|
||||||
|
|
||||||
|
let mut cz = CzFile::from_raw(
|
||||||
version,
|
version,
|
||||||
image.width() as u16,
|
image.width() as u16,
|
||||||
image.height() as u16,
|
image.height() as u16,
|
||||||
image.into_vec()
|
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");
|
cz.save_as_cz(output).expect("Saving CZ file failed");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue