From ac0813edc4790732715bb23055200c98c1399670 Mon Sep 17 00:00:00 2001 From: Filipe Rodrigues Date: Mon, 31 May 2021 05:27:16 +0100 Subject: [PATCH] `GameFile` now just stores the underlying file and constructs the `GameFile` whenever required. --- dcb-tools/dcb-file-editor/src/game_file.rs | 29 ++++++++++--------- dcb-tools/dcb-file-editor/src/main.rs | 5 +--- .../dcb-file-editor/src/preview_panel.rs | 12 +++----- dcb-tools/dcb-file-editor/src/swap_window.rs | 2 +- 4 files changed, 22 insertions(+), 26 deletions(-) diff --git a/dcb-tools/dcb-file-editor/src/game_file.rs b/dcb-tools/dcb-file-editor/src/game_file.rs index 267e59c..c1f6916 100644 --- a/dcb-tools/dcb-file-editor/src/game_file.rs +++ b/dcb-tools/dcb-file-editor/src/game_file.rs @@ -5,13 +5,14 @@ use crate::{ swap_window::SwapWindow, }; use anyhow::Context; +use dcb_cdrom_xa::CdRomCursor; use eframe::egui; use std::fs; /// Game file pub struct GameFile { /// Game file - game_file: dcb_io::GameFile, + file: fs::File, /// `A` drive tree a_tree: DrvTree, @@ -37,7 +38,8 @@ pub struct GameFile { impl GameFile { /// Creates a new game - pub fn new(mut game_file: dcb_io::GameFile) -> Result { + pub fn new(mut file: fs::File) -> Result { + let mut game_file = dcb_io::GameFile::new(CdRomCursor::new(&mut file)); let mut a_reader = game_file.a_drv().context("Unable to get `a` drive")?; let a_tree = DrvTree::new(&mut a_reader).context("Unable to load `a` drive")?; let mut b_reader = game_file.b_drv().context("Unable to get `b` drive")?; @@ -54,7 +56,7 @@ impl GameFile { let p_tree = DrvTree::new(&mut p_reader).context("Unable to load `p` drive")?; Ok(Self { - game_file, + file, a_tree, b_tree, c_tree, @@ -67,26 +69,27 @@ impl GameFile { /// Reloads the game pub fn reload(&mut self) -> Result<(), anyhow::Error> { + let mut game_file = dcb_io::GameFile::new(CdRomCursor::new(&mut self.file)); self.a_tree - .reload(&mut self.game_file.a_drv().context("Unable to get `A` drive")?) + .reload(&mut game_file.a_drv().context("Unable to get `A` drive")?) .context("Unable to reload `A` drive")?; self.b_tree - .reload(&mut self.game_file.b_drv().context("Unable to get `B` drive")?) + .reload(&mut game_file.b_drv().context("Unable to get `B` drive")?) .context("Unable to reload `B` drive")?; self.c_tree - .reload(&mut self.game_file.c_drv().context("Unable to get `C` drive")?) + .reload(&mut game_file.c_drv().context("Unable to get `C` drive")?) .context("Unable to reload `C` drive")?; self.e_tree - .reload(&mut self.game_file.e_drv().context("Unable to get `E` drive")?) + .reload(&mut game_file.e_drv().context("Unable to get `E` drive")?) .context("Unable to reload `E` drive")?; self.f_tree - .reload(&mut self.game_file.f_drv().context("Unable to get `F` drive")?) + .reload(&mut game_file.f_drv().context("Unable to get `F` drive")?) .context("Unable to reload `F` drive")?; self.g_tree - .reload(&mut self.game_file.g_drv().context("Unable to get `G` drive")?) + .reload(&mut game_file.g_drv().context("Unable to get `G` drive")?) .context("Unable to reload `G` drive")?; self.p_tree - .reload(&mut self.game_file.p_drv().context("Unable to get `P` drive")?) + .reload(&mut game_file.p_drv().context("Unable to get `P` drive")?) .context("Unable to reload `P` drive")?; Ok(()) @@ -121,9 +124,9 @@ impl GameFile { DisplayResults { preview_path } } - /// Returns a mutable reference to the underlying game file - pub fn game_file_mut(&mut self) -> &mut dcb_io::GameFile { - &mut self.game_file + /// Returns a game file + pub fn game_file(&mut self) -> dcb_io::GameFile<&mut fs::File> { + dcb_io::GameFile::new(CdRomCursor::new(&mut self.file)) } } diff --git a/dcb-tools/dcb-file-editor/src/main.rs b/dcb-tools/dcb-file-editor/src/main.rs index 2e11946..b7e000f 100644 --- a/dcb-tools/dcb-file-editor/src/main.rs +++ b/dcb-tools/dcb-file-editor/src/main.rs @@ -19,7 +19,6 @@ pub mod swap_window; // Imports use anyhow::Context; -use dcb_cdrom_xa::CdRomCursor; use dcb_util::{task, MutexPoison}; use eframe::{egui, epi, NativeOptions}; use game_file::GameFile; @@ -140,8 +139,6 @@ impl epi::App for FileEditor { .write(true) .open(file_path) .context("Unable to open file")?; - let cdrom = CdRomCursor::new(file); - let file = dcb_io::GameFile::new(cdrom); GameFile::new(file).context("Unable to load game") })); @@ -197,7 +194,7 @@ impl epi::App for FileEditor { // Flush the file if we have it if let Some(game_file) = &mut self.game_file { - match game_file.lock_unwrap().game_file_mut().cdrom().flush() { + match game_file.lock_unwrap().game_file().cdrom().flush() { Ok(()) => (), Err(err) => self::alert_error(&format!("Unable to flush file tod isk: {:?}", err)), } diff --git a/dcb-tools/dcb-file-editor/src/preview_panel.rs b/dcb-tools/dcb-file-editor/src/preview_panel.rs index dc08012..224be54 100644 --- a/dcb-tools/dcb-file-editor/src/preview_panel.rs +++ b/dcb-tools/dcb-file-editor/src/preview_panel.rs @@ -154,10 +154,8 @@ impl PreviewPanelBuilder { // Deserialize the tim let path = Path::from_ascii(&path).context("Unable to create path")?; let mut game_file = game_file.lock_unwrap(); - let mut file = game_file - .game_file_mut() - .open_file(path) - .context("Unable to open file")?; + let mut game_file = game_file.game_file(); + let mut file = game_file.open_file(path).context("Unable to open file")?; let tim = Tim::deserialize(&mut file).context("Unable to parse file")?; let pallette_pixels: Vec> = (0..tim.pallettes()) @@ -180,10 +178,8 @@ impl PreviewPanelBuilder { // Deserialize the tis let path = Path::from_ascii(&path).context("Unable to create path")?; let mut game_file = game_file.lock_unwrap(); - let file = game_file - .game_file_mut() - .open_file(path) - .context("Unable to open file")?; + let mut game_file = game_file.game_file(); + let file = game_file.open_file(path).context("Unable to open file")?; let mut file = BufReader::new(file); let tis: Tis = Tis::deserialize(&mut file).context("Unable to parse file")?; diff --git a/dcb-tools/dcb-file-editor/src/swap_window.rs b/dcb-tools/dcb-file-editor/src/swap_window.rs index 0b97732..95decda 100644 --- a/dcb-tools/dcb-file-editor/src/swap_window.rs +++ b/dcb-tools/dcb-file-editor/src/swap_window.rs @@ -52,7 +52,7 @@ impl SwapWindow { let res: Result<_, anyhow::Error> = try { game_file - .game_file_mut() + .game_file() .swap_files(lhs, rhs) .context("Unable to swap files")?;