Rewrote migration initialization situation

This commit is contained in:
user 2024-06-18 12:23:02 +02:00
parent 627708e0d9
commit db371d8d8f

View file

@ -23,7 +23,7 @@ pub enum ApiMessage {
OnChargeChange(f64), // battery fill amount: 0 = empty, 1 = full
PowerVibeCheck,
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
LoadMainSettings,
LoadSystemSettings,
@ -504,7 +504,7 @@ impl ApiMessageHandler {
self.on_empty.push(callback);
false
}
ApiMessage::LoadSettings(legacy_settings, id, name, variant_id, variant_name) => {
ApiMessage::LoadSettings(legacy_settings, current_settings) => {
/* Migration steps:
1. Modify to the frontend to send the game ID in this message (`id` here is the app
ID).
@ -515,19 +515,44 @@ impl ApiMessageHandler {
4. Let the rest of the code below work its magic.
*/
// If `legacy_settings` is `Some`, there is a legacy savefile present.
if let Some((legacy_game_id, legacy_name)) = legacy_settings {
// Create path to the legacy settings file.
let legacy_file_path = format!("{}.json", legacy_game_id);
// ===== migration logic =====
if legacy_settings.is_some() {
let (legacy_game_id, legacy_name) = legacy_settings.unwrap();
// Load the file from disk and deserialize it.
let file = crate::persist::OldSettingsJson::open(legacy_file_path);
let legacy_file_path = format!("{legacy_game_id}.json");
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
// `FileJson` using `FileJson::merge_legacy_settings()`. Else, create a new
// FileJson with this as the only variant, using
// `FileJson::from<OldSettingsJson>()`.
if current_settings.is_some() {
// A savefile in both the legacy and current format exist.
let (id, name, variant_id, variant_name) = current_settings.unwrap();
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);
if let Err(e) = settings.on_unload() {