GameFile now just stores the underlying file and constructs the GameFile whenever required.

This commit is contained in:
Filipe Rodrigues 2021-05-31 05:27:16 +01:00
parent 72c74b89d6
commit ac0813edc4
4 changed files with 22 additions and 26 deletions

View File

@ -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<fs::File>,
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<fs::File>) -> Result<Self, anyhow::Error> {
pub fn new(mut file: fs::File) -> Result<Self, anyhow::Error> {
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<fs::File> {
&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))
}
}

View File

@ -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)),
}

View File

@ -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<Box<[_]>> = (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")?;

View File

@ -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")?;