Skip to content

Commit

Permalink
waddy: can open bsp file to save as wad #13
Browse files Browse the repository at this point in the history
  • Loading branch information
khanghugo committed Sep 19, 2024
1 parent 70be49d commit 2ea3291
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 16 deletions.
29 changes: 20 additions & 9 deletions src/gui/programs/waddy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -737,7 +737,7 @@ impl WaddyGui {

if let Some(ext) = path.extension() {
// if new wad file is dropped, we open that wad file instead
if ext == "wad" {
if ext == "wad" || ext == "bsp" {
if let Err(err) = self.start_waddy_instance(ui, Some(path)) {
// TODO TOAST
println!("{}", err);
Expand Down Expand Up @@ -800,10 +800,19 @@ impl WaddyGui {

// FIXME: it is ram guzzler
fn start_waddy_instance(&mut self, ui: &mut Ui, path: Option<&Path>) -> eyre::Result<()> {
let waddy = if let Some(path) = path {
Waddy::from_file(path)?
// return is_changed here so that the user knows they need to save the file to have the file
let (waddy, is_changed) = if let Some(path) = path {
let ext = path.extension().unwrap();

if ext == "wad" {
(Waddy::from_wad_file(path)?, false)
} else if ext == "bsp" {
(Waddy::from_bsp_file(path)?, true)
} else {
unreachable!()
}
} else {
Waddy::new()
(Waddy::new(), true)
};

let texture_tiles = waddy
Expand Down Expand Up @@ -837,10 +846,10 @@ impl WaddyGui {
.collect::<Vec<TextureTile>>();

self.instances.push(WaddyInstance {
path: path.map(|path| path.to_owned()),
path: path.map(|path| path.with_extension("wad")),
waddy,
texture_tiles,
is_changed: false,
is_changed,
selected: vec![],
to_delete: vec![],
search: SearchBar::default(),
Expand All @@ -851,7 +860,9 @@ impl WaddyGui {

fn menu_open(&mut self, ui: &mut Ui) -> bool {
if let Some(path) = rfd::FileDialog::new().pick_file() {
if path.extension().unwrap().to_str().unwrap() == "wad" {
let ext = path.extension().unwrap();

if ext == "wad" || ext == "bsp" {
// todo toast
if let Err(err) = self.start_waddy_instance(ui, Some(path.as_path())) {
println!("{}", err);
Expand Down Expand Up @@ -1018,7 +1029,7 @@ impl WaddyGui {
});

ui.separator();
ui.label("Drag and drop a WAD file to start");
ui.label("Drag and drop a WAD file to start.\nYou can also drop a BSP file if you want.");

let ctx = ui.ctx();

Expand All @@ -1034,7 +1045,7 @@ impl WaddyGui {
}

if let Some(ext) = path.extension() {
if ext == "wad" {
if ext == "wad" || ext == "bsp" {
if let Err(err) = self.start_waddy_instance(ui, Some(path)) {
// TODO TOAST
println!("{}", err);
Expand Down
53 changes: 46 additions & 7 deletions src/modules/waddy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use std::{
str::from_utf8,
};

use bsp::Bsp;
use image::RgbaImage;
use rayon::prelude::*;

Expand All @@ -30,18 +31,43 @@ impl Waddy {
Self { wad: Wad::new() }
}

pub fn from_file(path: impl AsRef<Path> + Into<PathBuf> + AsRef<OsStr>) -> eyre::Result<Self> {
pub fn from_wad_file(
path: impl AsRef<Path> + Into<PathBuf> + AsRef<OsStr>,
) -> eyre::Result<Self> {
let wad = Wad::from_file(path)?;

Ok(Waddy { wad })
}

pub fn from_bytes(bytes: &[u8]) -> eyre::Result<Self> {
pub fn from_wad_bytes(bytes: &[u8]) -> eyre::Result<Self> {
let wad = Wad::from_bytes(bytes)?;

Ok(Waddy { wad })
}

pub fn from_bsp_file(
path: impl AsRef<Path> + Into<PathBuf> + AsRef<OsStr>,
) -> eyre::Result<Self> {
let mut res = Self::new();

let bsp = Bsp::from_file(path)?;
let textures = bsp.textures;

res.wad.entries = textures
.into_iter()
.map(|texture| {
let texture_name = texture.texture_name.get_string();

wad::types::Entry {
directory_entry: wad::types::DirectoryEntry::new(texture_name),
file_entry: wad::types::FileEntry::MipTex(texture),
}
})
.collect::<Vec<wad::types::Entry>>();

Ok(res)
}

fn log(&self, i: impl std::fmt::Display + AsRef<str>) {
println!("{}", i);
}
Expand Down Expand Up @@ -308,19 +334,19 @@ mod test {

#[test]
fn dump_info() {
let waddy = Waddy::from_file("/home/khang/gchimp/wad/test/surf_cyberwave.wad").unwrap();
let waddy = Waddy::from_wad_file("/home/khang/gchimp/wad/test/surf_cyberwave.wad").unwrap();
println!("{}", waddy.dump_info());
}

#[test]
fn dump_info2() {
let waddy = Waddy::from_file("/home/khang/map_compiler/cso_normal_pack.wad").unwrap();
let waddy = Waddy::from_wad_file("/home/khang/map_compiler/cso_normal_pack.wad").unwrap();
println!("{}", waddy.dump_info());
}

#[test]
fn dump_textures() {
let waddy = Waddy::from_file("/home/khang/gchimp/wad/test/surf_cyberwave.wad").unwrap();
let waddy = Waddy::from_wad_file("/home/khang/gchimp/wad/test/surf_cyberwave.wad").unwrap();
waddy
.dump_textures_to_files("/home/khang/gchimp/examples/waddy/")
.unwrap();
Expand All @@ -329,7 +355,8 @@ mod test {
#[test]
fn dump_textures2() {
{
let waddy = Waddy::from_file("/home/khang/map_compiler/cso_normal_pack.wad").unwrap();
let waddy =
Waddy::from_wad_file("/home/khang/map_compiler/cso_normal_pack.wad").unwrap();

waddy
.dump_textures_to_files("/home/khang/gchimp/examples/waddy/cso")
Expand All @@ -342,7 +369,8 @@ mod test {

#[test]
fn add_wad() {
let mut waddy = Waddy::from_file("/home/khang/gchimp/examples/waddy/wad_test.wad").unwrap();
let mut waddy =
Waddy::from_wad_file("/home/khang/gchimp/examples/waddy/wad_test.wad").unwrap();

// waddy
// .add_texture("/home/khang/map_compiler/my_textures/black.bmp")
Expand All @@ -360,4 +388,15 @@ mod test {
.save_to_file("/home/khang/gchimp/examples/waddy/wad_test_out.wad")
.unwrap();
}

#[test]
fn open_bsp() {
let waddy = Waddy::from_bsp_file("/home/khang/map/bsp/bsp_compile.bsp").unwrap();
// println!("{}", waddy.wad.entries)
waddy
.wad
.entries
.iter()
.for_each(|what| println!("{}", what.texture_name()));
}
}

0 comments on commit 2ea3291

Please sign in to comment.