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