Ensure that the save file is generated before overriding the old one

This commit is contained in:
Quentin Boyer 2023-11-12 16:27:53 +01:00
parent 68bace9004
commit 4261ab76ca

View file

@ -604,12 +604,13 @@ impl Application for Glaurung {
Message::FontLoaded(r) => r.expect("could not load font"), Message::FontLoaded(r) => r.expect("could not load font"),
Message::Event(ev) => { Message::Event(ev) => {
if let Event::Window(window::Event::CloseRequested) = ev { if let Event::Window(window::Event::CloseRequested) = ev {
let tmp_path = format!(".glaurung-{}", std::process::id());
let save_file = OpenOptions::new() let save_file = OpenOptions::new()
.create(true) .create(true)
.write(true) .write(true)
.truncate(true) .truncate(true)
.open(&self.save_file) .open(&tmp_path)
.expect("Can't open data file"); .expect("Can't open temp save file");
serde_json::to_writer( serde_json::to_writer(
save_file, save_file,
&SaveFile { &SaveFile {
@ -624,6 +625,8 @@ impl Application for Glaurung {
) )
.expect("could not write save file"); .expect("could not write save file");
std::fs::rename(tmp_path, &self.save_file).expect("could not save data");
return window::close(); return window::close();
} }
} }