Fixed minor issues

This commit is contained in:
G2-Games 2024-07-11 22:53:04 -05:00
parent 3bd0980313
commit 7b9140ae22
3 changed files with 49 additions and 26 deletions

View file

@ -1,9 +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 std::path::PathBuf; use std::fs;
use eframe::{egui::{self, text::{LayoutJob, TextWrapping}, ColorImage, Image, Rgba, TextBuffer, TextureFilter, TextureHandle, TextureOptions}, epaint::Fonts}; use eframe::egui::{self, ColorImage, Image, TextureFilter, TextureHandle, TextureOptions};
use luca_pak::{entry::EntryType, header, Pak}; use luca_pak::{entry::EntryType, Pak};
fn main() -> eframe::Result { fn main() -> eframe::Result {
let options = eframe::NativeOptions { let options = eframe::NativeOptions {
@ -47,6 +47,7 @@ impl eframe::App for PakExplorer {
ctx.set_pixels_per_point(1.5); ctx.set_pixels_per_point(1.5);
ui.heading("PAK File Explorer"); ui.heading("PAK File Explorer");
ui.horizontal(|ui| {
if ui.button("Open file…").clicked() { if ui.button("Open file…").clicked() {
if let Some(path) = rfd::FileDialog::new().pick_file() { if let Some(path) = rfd::FileDialog::new().pick_file() {
let pak = Pak::open(&path).unwrap(); let pak = Pak::open(&path).unwrap();
@ -56,6 +57,17 @@ impl eframe::App for PakExplorer {
self.hex_string = None; self.hex_string = None;
} }
} }
if let Some(pak) = &self.open_file {
if ui.button("Save PAK…").clicked() {
if let Some(path) = rfd::FileDialog::new()
.set_file_name(pak.path().file_name().unwrap().to_string_lossy())
.save_file()
{
pak.save(&path).unwrap();
}
}
}
});
ui.separator(); ui.separator();
@ -92,6 +104,7 @@ impl eframe::App for PakExplorer {
} }
if let Some(entry) = &self.selected_entry { if let Some(entry) = &self.selected_entry {
ui.horizontal(|ui| {
if ui.button("Save entry…").clicked() { if ui.button("Save entry…").clicked() {
if let Some(path) = rfd::FileDialog::new() if let Some(path) = rfd::FileDialog::new()
.set_file_name(entry.display_name()) .set_file_name(entry.display_name())
@ -100,6 +113,16 @@ impl eframe::App for PakExplorer {
entry.save(&path).unwrap(); entry.save(&path).unwrap();
} }
} }
if let Some(pak) = &mut self.open_file.as_mut() {
if ui.button("Replace entry…").clicked() {
if let Some(path) = rfd::FileDialog::new().pick_file() {
let file_bytes = fs::read(path).unwrap();
pak.replace(entry.index(), &file_bytes).unwrap();
}
}
}
});
match entry.file_type() { match entry.file_type() {
EntryType::CZ0 | EntryType::CZ1 EntryType::CZ0 | EntryType::CZ1
| EntryType::CZ2 | EntryType::CZ3 | EntryType::CZ2 | EntryType::CZ3
@ -147,7 +170,7 @@ impl eframe::App for PakExplorer {
); );
}, },
} }
} else { } else if self.open_file.is_some() {
ui.centered_and_justified(|ui| ui.centered_and_justified(|ui|
ui.label("Select an Entry") ui.label("Select an Entry")
); );

View file

@ -1,5 +1,5 @@
use std::{ use std::{
error::Error, fmt, fs::File, io::{BufWriter, Write}, path::Path error::Error, fs::File, io::{BufWriter, Write}, path::Path
}; };
/// A single file entry in a PAK file /// A single file entry in a PAK file

View file

@ -213,7 +213,6 @@ impl Pak {
&self, &self,
mut output: &mut T mut output: &mut T
) -> Result<(), PakError> { ) -> Result<(), PakError> {
let mut block_offset = 0;
self.header.write_into(&mut output)?; self.header.write_into(&mut output)?;
// Write unknown data // Write unknown data
@ -254,21 +253,22 @@ impl Pak {
output.write_all(&self.unknown_post_header)?; output.write_all(&self.unknown_post_header)?;
block_offset += self.header().data_offset / self.header().block_size; //let mut block_offset = self.header().data_offset / self.header().block_size;
for entry in self.entries() { for entry in self.entries() {
let block_size = entry.data.len().div_ceil(self.header().block_size as usize); //let block_size = entry.data.len().div_ceil(self.header().block_size as usize);
let mut remainder = 2048 - entry.data.len().rem_euclid(self.header().block_size as usize); let mut remainder = 2048 - entry.data.len().rem_euclid(self.header().block_size as usize);
if remainder == 2048 { if remainder == 2048 {
remainder = 0; remainder = 0;
} }
output.write_all(&entry.data)?;
output.write_all(&vec![0u8; remainder])?;
//println!("entry len {}", entry.data.len()); //println!("entry len {}", entry.data.len());
//println!("remainder {}", remainder); //println!("remainder {}", remainder);
//println!("block_offset {} - expected offset {}", block_offset, entry.offset); //println!("block_offset {} - expected offset {}", block_offset, entry.offset);
output.write_all(&entry.data)?;
output.write_all(&vec![0u8; remainder])?; //block_offset += block_size as u32;
block_offset += block_size as u32;
} }
Ok(()) Ok(())