WIP: Initial commit for savefile migration #133

Draft
not_a_segfault wants to merge 12 commits from not_a_segfault/PowerTools:dev into dev
Showing only changes of commit db371d8d8f - Show all commits

View file

@ -23,7 +23,7 @@ pub enum ApiMessage {
OnChargeChange(f64), // battery fill amount: 0 = empty, 1 = full OnChargeChange(f64), // battery fill amount: 0 = empty, 1 = full
PowerVibeCheck, PowerVibeCheck,
WaitForEmptyQueue(Callback<()>), WaitForEmptyQueue(Callback<()>),
LoadSettings(Option<(u64, String)>, u64, String, u64, String), // ((legacy game_id, legacy name), path, name, variant, variant name) LoadSettings(Option<(u64, String)>, Option<(u64, String, u64, String)>), // (legacy(game_id, name), current(path, name, variant, variant name))
LoadVariant(u64, String), // (variant, variant name) -- path and name assumed to be for current profile LoadVariant(u64, String), // (variant, variant name) -- path and name assumed to be for current profile
LoadMainSettings, LoadMainSettings,
LoadSystemSettings, LoadSystemSettings,
@ -504,7 +504,7 @@ impl ApiMessageHandler {
self.on_empty.push(callback); self.on_empty.push(callback);
false false
} }
ApiMessage::LoadSettings(legacy_settings, id, name, variant_id, variant_name) => { ApiMessage::LoadSettings(legacy_settings, current_settings) => {
/* Migration steps: /* Migration steps:
1. Modify to the frontend to send the game ID in this message (`id` here is the app 1. Modify to the frontend to send the game ID in this message (`id` here is the app
ID). ID).
@ -515,19 +515,44 @@ impl ApiMessageHandler {
4. Let the rest of the code below work its magic. 4. Let the rest of the code below work its magic.
*/ */
// If `legacy_settings` is `Some`, there is a legacy savefile present. // ===== migration logic =====
if let Some((legacy_game_id, legacy_name)) = legacy_settings { if legacy_settings.is_some() {
// Create path to the legacy settings file. let (legacy_game_id, legacy_name) = legacy_settings.unwrap();
let legacy_file_path = format!("{}.json", legacy_game_id);
// Load the file from disk and deserialize it. let legacy_file_path = format!("{legacy_game_id}.json");
let file = crate::persist::OldSettingsJson::open(legacy_file_path); let legacy_file = crate::persist::OldSettingsJson::open(legacy_file_path)
.expect("should be able to deserialzie legacy savefile format"); // TODO: don't panic on fail.
// If a file in the modern format exists, add it as a new variant to if current_settings.is_some() {
// `FileJson` using `FileJson::merge_legacy_settings()`. Else, create a new // A savefile in both the legacy and current format exist.
// FileJson with this as the only variant, using let (id, name, variant_id, variant_name) = current_settings.unwrap();
// `FileJson::from<OldSettingsJson>()`. let path = format!("{}.ron", id);
// 1. Parse `legacy_file` to into a settings variant.
let migrated_settings_variant: crate::persist::SettingsJson =
legacy_file.into();
// 2. Insert the variant into the current settings file.
match settings.load_file(
path.into(),
id,
name,
variant_id,
variant_name,
false,
) {
Ok(success) => log::info!("Loaded settings file? {}", success),
Err(e) => log::warn!("Load file err: {}", e),
} }
settings.add_variant() //TODO: This.
} else {
// A savefile in the current format doesn't exist yet.
// TODO: Parse it to the new format and save it.
let migrated_settings: crate::persist::FileJson = legacy_file.into();
}
} else if current_settings.is_some() {
// ...
}
// ===========================
let path = format!("{}.ron", id); let path = format!("{}.ron", id);
if let Err(e) = settings.on_unload() { if let Err(e) = settings.on_unload() {