diff --git a/experimental/src/main.rs b/experimental/src/main.rs index b473100..2387026 100644 --- a/experimental/src/main.rs +++ b/experimental/src/main.rs @@ -1,9 +1,9 @@ #![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 luca_pak::{entry::EntryType, header, Pak}; +use eframe::egui::{self, ColorImage, Image, TextureFilter, TextureHandle, TextureOptions}; +use luca_pak::{entry::EntryType, Pak}; fn main() -> eframe::Result { let options = eframe::NativeOptions { @@ -47,15 +47,27 @@ impl eframe::App for PakExplorer { ctx.set_pixels_per_point(1.5); ui.heading("PAK File Explorer"); - if ui.button("Open file…").clicked() { - if let Some(path) = rfd::FileDialog::new().pick_file() { - let pak = Pak::open(&path).unwrap(); - self.open_file = Some(pak); - self.selected_entry = None; - self.image_texture = None; - self.hex_string = None; + ui.horizontal(|ui| { + if ui.button("Open file…").clicked() { + if let Some(path) = rfd::FileDialog::new().pick_file() { + let pak = Pak::open(&path).unwrap(); + self.open_file = Some(pak); + self.selected_entry = None; + self.image_texture = 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(); @@ -92,14 +104,25 @@ impl eframe::App for PakExplorer { } if let Some(entry) = &self.selected_entry { - if ui.button("Save entry…").clicked() { - if let Some(path) = rfd::FileDialog::new() - .set_file_name(entry.display_name()) - .save_file() - { - entry.save(&path).unwrap(); + ui.horizontal(|ui| { + if ui.button("Save entry…").clicked() { + if let Some(path) = rfd::FileDialog::new() + .set_file_name(entry.display_name()) + .save_file() + { + 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() { EntryType::CZ0 | EntryType::CZ1 | 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.label("Select an Entry") ); diff --git a/luca_pak/src/entry.rs b/luca_pak/src/entry.rs index bb7d443..16b3273 100644 --- a/luca_pak/src/entry.rs +++ b/luca_pak/src/entry.rs @@ -1,5 +1,5 @@ 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 diff --git a/luca_pak/src/lib.rs b/luca_pak/src/lib.rs index 5e91ffa..0ae15d0 100644 --- a/luca_pak/src/lib.rs +++ b/luca_pak/src/lib.rs @@ -213,7 +213,6 @@ impl Pak { &self, mut output: &mut T ) -> Result<(), PakError> { - let mut block_offset = 0; self.header.write_into(&mut output)?; // Write unknown data @@ -254,21 +253,22 @@ impl Pak { 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() { - 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); if remainder == 2048 { remainder = 0; } + output.write_all(&entry.data)?; + output.write_all(&vec![0u8; remainder])?; //println!("entry len {}", entry.data.len()); //println!("remainder {}", remainder); //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(())