Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding helper function for getting Savefilepath #13

Merged
merged 6 commits into from
Oct 23, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 16 additions & 18 deletions plastic_tui/src/ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,32 +161,31 @@ impl Ui {
}

fn save_state(&mut self, slot: u8) {
if self.nes.is_empty() {
return;
if let Some(path) = self.get_save_state_path(slot) {
let file = fs::File::create(&path).unwrap();
self.nes.save_state(&file).unwrap();
}

let base_saved_states_dir = base_save_state_folder().unwrap();
let filename = self.nes.save_state_file_name(slot).unwrap();
let path = base_saved_states_dir.join(&filename);

let file = fs::File::create(&path).unwrap();

self.nes.save_state(&file).unwrap();
}

fn load_state(&mut self, slot: u8) {
if let Some(path) = self.get_save_state_path(slot) {
let file = fs::File::open(&path).unwrap();
self.nes.load_state(&file).unwrap();
}
}

fn get_save_state_path(&self, slot: u8) -> Option<std::path::PathBuf> {
if self.nes.is_empty() {
return;
return None;
}

let base_saved_states_dir = base_save_state_folder().unwrap();
let filename = self.nes.save_state_file_name(slot).unwrap();
let path = base_saved_states_dir.join(&filename);
let base_saved_states_dir = base_save_state_folder()?;
let filename = self.nes.save_state_file_name(slot)?;

Some(base_saved_states_dir.join(filename))
}

let file = fs::File::open(&path).unwrap();

self.nes.load_state(&file).unwrap();
}

fn reset_menu(&mut self) {
let mut save_state_items = Vec::with_capacity(10);
Expand Down Expand Up @@ -536,7 +535,6 @@ impl Ui {
thread::sleep(remaining);
}
}

disable_raw_mode().unwrap();
execute!(
io::stdout(),
Expand Down
Loading