From ae7810702ae6ef8020c6a823da35e8c96c3261e6 Mon Sep 17 00:00:00 2001 From: user Date: Mon, 27 Nov 2023 18:02:27 +0100 Subject: [PATCH 01/11] Initial commit for savefile migration --- backend/src/persist/migration.rs | 52 ++++++++++++++++++++++++++++++++ backend/src/persist/mod.rs | 4 ++- 2 files changed, 55 insertions(+), 1 deletion(-) create mode 100644 backend/src/persist/migration.rs diff --git a/backend/src/persist/migration.rs b/backend/src/persist/migration.rs new file mode 100644 index 0000000..33f21bb --- /dev/null +++ b/backend/src/persist/migration.rs @@ -0,0 +1,52 @@ +use std::collections::HashMap; + +use serde::{Deserialize, Serialize}; + +#[derive(Serialize, Deserialize)] +pub struct OldOnEventJson { + pub on_save: Option, + pub on_load: Option, + pub on_set: Option, + pub on_resume: Option, +} + +#[derive(Serialize, Deserialize)] +pub struct OldSettingsJson { + pub version: u64, + pub name: String, + pub persistent: bool, + pub cpus: Vec, + pub gpu: super::GpuJson, + pub battery: super::BatteryJson, + pub provider: Option, + pub events: Option, +} + +impl From<&OldSettingsJson> for super::SettingsJson { + fn from(old_settings: &OldSettingsJson) -> Self { + Self { + version: old_settings.version, + name: old_settings.name.clone(), + variant: 0, + persistent: old_settings.persistent, + cpus: old_settings.cpus.clone(), + gpu: old_settings.gpu.clone(), + battery: old_settings.battery.clone(), + provider: old_settings.provider.clone(), + } + } +} + +impl From<&OldSettingsJson> for super::FileJson { + fn from(old_settings: &OldSettingsJson) -> Self { + let mut variants = HashMap::new(); + let variant = super::SettingsJson::from(old_settings); + variants.insert(0, variant); + + Self { + version: 0, + name: old_settings.name.clone(), + variants, + } + } +} diff --git a/backend/src/persist/mod.rs b/backend/src/persist/mod.rs index 4c9a31b..d991a88 100644 --- a/backend/src/persist/mod.rs +++ b/backend/src/persist/mod.rs @@ -5,6 +5,7 @@ mod error; mod file; mod general; mod gpu; +mod migration; pub use battery::{BatteryEventJson, BatteryJson}; pub use cpu::CpuJson; @@ -12,5 +13,6 @@ pub use driver::DriverJson; pub use file::FileJson; pub use general::{MinMaxJson, SettingsJson}; pub use gpu::GpuJson; +pub use migration::OldSettingsJson; -pub use error::{SerdeError, RonError}; +pub use error::{RonError, SerdeError}; -- 2.43.4 From d22df0639abb668cdf4d0c021e6e26201cc82653 Mon Sep 17 00:00:00 2001 From: user Date: Mon, 27 Nov 2023 18:02:27 +0100 Subject: [PATCH 02/11] Initial commit for savefile migration --- backend/src/persist/migration.rs | 52 ++++++++++++++++++++++++++++++++ backend/src/persist/mod.rs | 6 ++-- 2 files changed, 55 insertions(+), 3 deletions(-) create mode 100644 backend/src/persist/migration.rs diff --git a/backend/src/persist/migration.rs b/backend/src/persist/migration.rs new file mode 100644 index 0000000..33f21bb --- /dev/null +++ b/backend/src/persist/migration.rs @@ -0,0 +1,52 @@ +use std::collections::HashMap; + +use serde::{Deserialize, Serialize}; + +#[derive(Serialize, Deserialize)] +pub struct OldOnEventJson { + pub on_save: Option, + pub on_load: Option, + pub on_set: Option, + pub on_resume: Option, +} + +#[derive(Serialize, Deserialize)] +pub struct OldSettingsJson { + pub version: u64, + pub name: String, + pub persistent: bool, + pub cpus: Vec, + pub gpu: super::GpuJson, + pub battery: super::BatteryJson, + pub provider: Option, + pub events: Option, +} + +impl From<&OldSettingsJson> for super::SettingsJson { + fn from(old_settings: &OldSettingsJson) -> Self { + Self { + version: old_settings.version, + name: old_settings.name.clone(), + variant: 0, + persistent: old_settings.persistent, + cpus: old_settings.cpus.clone(), + gpu: old_settings.gpu.clone(), + battery: old_settings.battery.clone(), + provider: old_settings.provider.clone(), + } + } +} + +impl From<&OldSettingsJson> for super::FileJson { + fn from(old_settings: &OldSettingsJson) -> Self { + let mut variants = HashMap::new(); + let variant = super::SettingsJson::from(old_settings); + variants.insert(0, variant); + + Self { + version: 0, + name: old_settings.name.clone(), + variants, + } + } +} diff --git a/backend/src/persist/mod.rs b/backend/src/persist/mod.rs index bfc0b2a..d991a88 100644 --- a/backend/src/persist/mod.rs +++ b/backend/src/persist/mod.rs @@ -5,6 +5,7 @@ mod error; mod file; mod general; mod gpu; +mod migration; pub use battery::{BatteryEventJson, BatteryJson}; pub use cpu::CpuJson; @@ -12,7 +13,6 @@ pub use driver::DriverJson; pub use file::FileJson; pub use general::{MinMaxJson, SettingsJson}; pub use gpu::GpuJson; +pub use migration::OldSettingsJson; -pub use error::SerdeError; - -pub const LATEST_VERSION: u64 = 0; +pub use error::{RonError, SerdeError}; -- 2.43.4 From 6379853307d7c0264c51e7bb42bbbdf61b964695 Mon Sep 17 00:00:00 2001 From: user Date: Mon, 27 May 2024 12:18:16 +0200 Subject: [PATCH 03/11] Added new struct fields into structs used for migration logic --- backend/src/persist/migration.rs | 18 +++++++++++++++++- backend/src/persist/mod.rs | 2 ++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/backend/src/persist/migration.rs b/backend/src/persist/migration.rs index 33f21bb..88122f4 100644 --- a/backend/src/persist/migration.rs +++ b/backend/src/persist/migration.rs @@ -1,3 +1,16 @@ +// - Commit that changed setting format: 3aa9680baedf850d7d576d0df28ce50cf2124ebe. +// - Commit that made settings variant use u64 instead of String: +// d481f1314409c99a0ed48b006a8e9b53f6caa8a5. +// - Add all back-end functionality for interacting with variants on the front-end: +// 4eaf6fae2bba0219a316d37499ee6e1549645862. +// - Improve memory clock selection for #140, fix dpm_performance enforcement check for GPU: +// a1c44cdea7ae27f7a09fd194c580c79b291d25d1. +// - Remove legacy charge mode and level functionality: 88d359e286bec2fdabde0f4f72b52fb8f4d5200e. +// - Add tags for persistent settings variants: ccf0c04020bfc5ae9f667801f99d1a31f939006a. + +// TODO: Check full git diff between data structures from version 1.4.0 and current, to see exactly +// what changed and how to migrate them to the current data structures. + use std::collections::HashMap; use serde::{Deserialize, Serialize}; @@ -20,6 +33,7 @@ pub struct OldSettingsJson { pub battery: super::BatteryJson, pub provider: Option, pub events: Option, + pub tags: Vec, } impl From<&OldSettingsJson> for super::SettingsJson { @@ -33,6 +47,7 @@ impl From<&OldSettingsJson> for super::SettingsJson { gpu: old_settings.gpu.clone(), battery: old_settings.battery.clone(), provider: old_settings.provider.clone(), + tags: todo!(), } } } @@ -44,9 +59,10 @@ impl From<&OldSettingsJson> for super::FileJson { variants.insert(0, variant); Self { - version: 0, + version: super::LATEST_VERSION, name: old_settings.name.clone(), variants, + app_id: todo!(), } } } diff --git a/backend/src/persist/mod.rs b/backend/src/persist/mod.rs index d991a88..561e6c1 100644 --- a/backend/src/persist/mod.rs +++ b/backend/src/persist/mod.rs @@ -16,3 +16,5 @@ pub use gpu::GpuJson; pub use migration::OldSettingsJson; pub use error::{RonError, SerdeError}; + +pub const LATEST_VERSION: u64 = 0; -- 2.43.4 From da5d2a9673591e44b2f4b203b214e11f394244c4 Mon Sep 17 00:00:00 2001 From: user Date: Thu, 30 May 2024 10:59:23 +0200 Subject: [PATCH 04/11] Create module for migration logic, add `GpuJson` migration logic --- backend/src/persist/migration.rs | 70 +---------------------- backend/src/persist/migration/gpu.rs | 26 +++++++++ backend/src/persist/migration/settings.rs | 58 +++++++++++++++++++ backend/src/persist/mod.rs | 2 +- 4 files changed, 87 insertions(+), 69 deletions(-) create mode 100644 backend/src/persist/migration/gpu.rs create mode 100644 backend/src/persist/migration/settings.rs diff --git a/backend/src/persist/migration.rs b/backend/src/persist/migration.rs index 88122f4..37c3f2e 100644 --- a/backend/src/persist/migration.rs +++ b/backend/src/persist/migration.rs @@ -1,68 +1,2 @@ -// - Commit that changed setting format: 3aa9680baedf850d7d576d0df28ce50cf2124ebe. -// - Commit that made settings variant use u64 instead of String: -// d481f1314409c99a0ed48b006a8e9b53f6caa8a5. -// - Add all back-end functionality for interacting with variants on the front-end: -// 4eaf6fae2bba0219a316d37499ee6e1549645862. -// - Improve memory clock selection for #140, fix dpm_performance enforcement check for GPU: -// a1c44cdea7ae27f7a09fd194c580c79b291d25d1. -// - Remove legacy charge mode and level functionality: 88d359e286bec2fdabde0f4f72b52fb8f4d5200e. -// - Add tags for persistent settings variants: ccf0c04020bfc5ae9f667801f99d1a31f939006a. - -// TODO: Check full git diff between data structures from version 1.4.0 and current, to see exactly -// what changed and how to migrate them to the current data structures. - -use std::collections::HashMap; - -use serde::{Deserialize, Serialize}; - -#[derive(Serialize, Deserialize)] -pub struct OldOnEventJson { - pub on_save: Option, - pub on_load: Option, - pub on_set: Option, - pub on_resume: Option, -} - -#[derive(Serialize, Deserialize)] -pub struct OldSettingsJson { - pub version: u64, - pub name: String, - pub persistent: bool, - pub cpus: Vec, - pub gpu: super::GpuJson, - pub battery: super::BatteryJson, - pub provider: Option, - pub events: Option, - pub tags: Vec, -} - -impl From<&OldSettingsJson> for super::SettingsJson { - fn from(old_settings: &OldSettingsJson) -> Self { - Self { - version: old_settings.version, - name: old_settings.name.clone(), - variant: 0, - persistent: old_settings.persistent, - cpus: old_settings.cpus.clone(), - gpu: old_settings.gpu.clone(), - battery: old_settings.battery.clone(), - provider: old_settings.provider.clone(), - tags: todo!(), - } - } -} - -impl From<&OldSettingsJson> for super::FileJson { - fn from(old_settings: &OldSettingsJson) -> Self { - let mut variants = HashMap::new(); - let variant = super::SettingsJson::from(old_settings); - variants.insert(0, variant); - - Self { - version: super::LATEST_VERSION, - name: old_settings.name.clone(), - variants, - app_id: todo!(), - } - } -} +pub mod gpu; +pub mod settings; diff --git a/backend/src/persist/migration/gpu.rs b/backend/src/persist/migration/gpu.rs new file mode 100644 index 0000000..6f71a4e --- /dev/null +++ b/backend/src/persist/migration/gpu.rs @@ -0,0 +1,26 @@ +use serde::{Deserialize, Serialize}; + +use crate::persist::{GpuJson, MinMaxJson}; + +#[derive(Serialize, Deserialize, Clone)] +pub struct OldGpuJson { + pub fast_ppt: Option, + pub slow_ppt: Option, + pub clock_limits: Option>, + pub slow_memory: bool, + pub root: Option, +} + +impl From for GpuJson { + fn from(old_gpu: OldGpuJson) -> Self { + Self { + fast_ppt: old_gpu.fast_ppt, + slow_ppt: old_gpu.slow_ppt, + tdp: None, + tdp_boost: None, + clock_limits: old_gpu.clock_limits.clone(), + memory_clock: None, + root: old_gpu.root.clone(), + } + } +} diff --git a/backend/src/persist/migration/settings.rs b/backend/src/persist/migration/settings.rs new file mode 100644 index 0000000..3d95bf6 --- /dev/null +++ b/backend/src/persist/migration/settings.rs @@ -0,0 +1,58 @@ +use std::collections::HashMap; + +use serde::{Deserialize, Serialize}; + +use crate::persist::{BatteryJson, CpuJson, DriverJson, FileJson, SettingsJson, LATEST_VERSION}; + +use super::gpu::OldGpuJson; + +#[derive(Serialize, Deserialize, Clone)] +pub struct OldOnEventJson { + pub on_save: Option, + pub on_load: Option, + pub on_set: Option, + pub on_resume: Option, +} + +#[derive(Serialize, Deserialize, Clone)] +pub struct OldSettingsJson { + pub version: u64, + pub name: String, + pub persistent: bool, + pub cpus: Vec, + pub gpu: OldGpuJson, + pub battery: BatteryJson, + pub provider: Option, + pub events: Option, +} + +impl From for SettingsJson { + fn from(old_settings: OldSettingsJson) -> Self { + Self { + version: old_settings.version, + name: old_settings.name.clone(), + variant: 0, + persistent: old_settings.persistent, + cpus: old_settings.cpus.clone(), + gpu: old_settings.gpu.clone().into(), + battery: old_settings.battery.clone(), + provider: old_settings.provider.clone(), + tags: Vec::new(), + } + } +} + +impl From for FileJson { + fn from(old_settings: OldSettingsJson) -> Self { + let mut variants = HashMap::new(); + let variant = SettingsJson::from(old_settings.clone()); + variants.insert(0, variant); + + Self { + version: LATEST_VERSION, + name: old_settings.name.clone(), + app_id: todo!(), + variants, + } + } +} diff --git a/backend/src/persist/mod.rs b/backend/src/persist/mod.rs index 561e6c1..d856fa8 100644 --- a/backend/src/persist/mod.rs +++ b/backend/src/persist/mod.rs @@ -13,7 +13,7 @@ pub use driver::DriverJson; pub use file::FileJson; pub use general::{MinMaxJson, SettingsJson}; pub use gpu::GpuJson; -pub use migration::OldSettingsJson; +pub use migration::{gpu::*, settings::*}; pub use error::{RonError, SerdeError}; -- 2.43.4 From 4816279f9e36aefe624aac07a5150a578dc88216 Mon Sep 17 00:00:00 2001 From: user Date: Thu, 30 May 2024 15:25:26 +0200 Subject: [PATCH 05/11] Add `APP_ID_UNKNOWN` sentinel value for required `app_id` field during savefile migration --- backend/src/persist/migration.rs | 2 ++ backend/src/persist/migration/settings.rs | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/backend/src/persist/migration.rs b/backend/src/persist/migration.rs index 37c3f2e..7931447 100644 --- a/backend/src/persist/migration.rs +++ b/backend/src/persist/migration.rs @@ -1,2 +1,4 @@ pub mod gpu; pub mod settings; + +pub const APP_ID_UNKNOWN: u64 = u64::MAX; diff --git a/backend/src/persist/migration/settings.rs b/backend/src/persist/migration/settings.rs index 3d95bf6..3f1b265 100644 --- a/backend/src/persist/migration/settings.rs +++ b/backend/src/persist/migration/settings.rs @@ -51,7 +51,7 @@ impl From for FileJson { Self { version: LATEST_VERSION, name: old_settings.name.clone(), - app_id: todo!(), + app_id: super::APP_ID_UNKNOWN, // `u64::MAX`, sentinel value. variants, } } -- 2.43.4 From 529664fadc8257681be3a08a8a743e5644fc4a16 Mon Sep 17 00:00:00 2001 From: user Date: Tue, 4 Jun 2024 10:04:25 +0200 Subject: [PATCH 06/11] cargo fmt --- backend/src/api/handler.rs | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/backend/src/api/handler.rs b/backend/src/api/handler.rs index 83a2b9b..22ab7b5 100644 --- a/backend/src/api/handler.rs +++ b/backend/src/api/handler.rs @@ -45,13 +45,21 @@ impl core::fmt::Display for ApiMessage { Self::OnChargeChange(x) => write!(f, "OnChargeChange({:?})", x), Self::PowerVibeCheck => write!(f, "PowerVibeCheck"), Self::WaitForEmptyQueue(_) => write!(f, "WaitForEmptyQueue"), - Self::LoadSettings(path, name, variant, variant_name) => write!(f, "LoadSettings({}, {}, {}, {})", path, name, variant, variant_name), - Self::LoadVariant(variant, variant_name) => write!(f, "LoadVariant({}, {})", variant, variant_name), + Self::LoadSettings(path, name, variant, variant_name) => write!( + f, + "LoadSettings({}, {}, {}, {})", + path, name, variant, variant_name + ), + Self::LoadVariant(variant, variant_name) => { + write!(f, "LoadVariant({}, {})", variant, variant_name) + } Self::LoadMainSettings => write!(f, "LoadMainSettings"), Self::LoadSystemSettings => write!(f, "LoadSystemSettings"), Self::GetLimits(_) => write!(f, "GetLimits"), Self::GetProvider(s, _) => write!(f, "GetProvider({})", s), - Self::UploadCurrentVariant(id, user) => write!(f, "UploadCurrentVariant(id: {}, user: {})", id, user), + Self::UploadCurrentVariant(id, user) => { + write!(f, "UploadCurrentVariant(id: {}, user: {})", id, user) + } } } } @@ -392,9 +400,7 @@ impl ApiMessageHandler { messages.push(msg.to_string()); dirty |= self.process(settings, msg); } - if dirty - || dirty_echo - { + if dirty || dirty_echo { dirty_echo = dirty; // echo only once print_messages(&messages); // run on_set -- 2.43.4 From 326a727a2ffe172b567380d263713c7b4ddfb97e Mon Sep 17 00:00:00 2001 From: user Date: Tue, 4 Jun 2024 10:47:07 +0200 Subject: [PATCH 07/11] Append "(migrated)" to `SettingsJson.name` during migration --- backend/src/persist/migration/settings.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/backend/src/persist/migration/settings.rs b/backend/src/persist/migration/settings.rs index 3f1b265..361afab 100644 --- a/backend/src/persist/migration/settings.rs +++ b/backend/src/persist/migration/settings.rs @@ -30,7 +30,7 @@ impl From for SettingsJson { fn from(old_settings: OldSettingsJson) -> Self { Self { version: old_settings.version, - name: old_settings.name.clone(), + name: format!("{} (migrated)", old_settings.name.clone()), variant: 0, persistent: old_settings.persistent, cpus: old_settings.cpus.clone(), -- 2.43.4 From bb9fa9f170c30538396854f309febeedca78d34e Mon Sep 17 00:00:00 2001 From: user Date: Tue, 4 Jun 2024 12:17:08 +0200 Subject: [PATCH 08/11] Plan out further migration steps --- backend/src/api/handler.rs | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/backend/src/api/handler.rs b/backend/src/api/handler.rs index 22ab7b5..e1e155a 100644 --- a/backend/src/api/handler.rs +++ b/backend/src/api/handler.rs @@ -505,6 +505,15 @@ impl ApiMessageHandler { false } ApiMessage::LoadSettings(id, name, variant_id, variant_name) => { + /* Migration steps: + 1. Modify to the frontend to send the game ID in this message (`id` here is the app + ID). + 2. Change game ID to app ID. + 3. (Create and) call function to merge OldSettingsJson with existing FileJson, or + use existing `From for FileJson` if this game doesn't have a + save in the new format yet. + 4. Let the rest of the code below work its magic. + */ let path = format!("{}.ron", id); if let Err(e) = settings.on_unload() { print_errors("LoadSettings on_unload()", e); -- 2.43.4 From 627708e0d9ec940f953d55a8ab7d0482c1145bd6 Mon Sep 17 00:00:00 2001 From: user Date: Thu, 13 Jun 2024 12:06:45 +0200 Subject: [PATCH 09/11] Add open method for legacy savefile, including shitty error handling --- backend/src/api/handler.rs | 19 +++++++++++++++++-- backend/src/persist/error.rs | 8 ++++++++ backend/src/persist/migration/settings.rs | 11 ++++++++++- 3 files changed, 35 insertions(+), 3 deletions(-) diff --git a/backend/src/api/handler.rs b/backend/src/api/handler.rs index e1e155a..e2b24ed 100644 --- a/backend/src/api/handler.rs +++ b/backend/src/api/handler.rs @@ -23,7 +23,7 @@ pub enum ApiMessage { OnChargeChange(f64), // battery fill amount: 0 = empty, 1 = full PowerVibeCheck, WaitForEmptyQueue(Callback<()>), - LoadSettings(u64, String, u64, String), // (path, name, variant, variant name) + LoadSettings(Option<(u64, String)>, u64, String, u64, String), // ((legacy game_id, legacy name), 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(id, name, variant_id, variant_name) => { + ApiMessage::LoadSettings(legacy_settings, id, name, variant_id, variant_name) => { /* Migration steps: 1. Modify to the frontend to send the game ID in this message (`id` here is the app ID). @@ -514,6 +514,21 @@ impl ApiMessageHandler { save in the new format yet. 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); + + // Load the file from disk and deserialize it. + let file = crate::persist::OldSettingsJson::open(legacy_file_path); + + // 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()`. + } + let path = format!("{}.ron", id); if let Err(e) = settings.on_unload() { print_errors("LoadSettings on_unload()", e); diff --git a/backend/src/persist/error.rs b/backend/src/persist/error.rs index 502d199..ca1b41e 100644 --- a/backend/src/persist/error.rs +++ b/backend/src/persist/error.rs @@ -36,6 +36,14 @@ impl From for RonError { } } +impl From for RonError { + fn from(_value: serde_json::Error) -> Self { + RonError::General(ron::Error::Message(String::from( + "TODO: make error handling in migration logic good. Specifically, make it so a json error can be used as a RonError instead of just returning this string", + ))) + } +} + impl From for RonError { fn from(value: ron::error::SpannedError) -> Self { Self::Spanned(value) diff --git a/backend/src/persist/migration/settings.rs b/backend/src/persist/migration/settings.rs index 361afab..0327a48 100644 --- a/backend/src/persist/migration/settings.rs +++ b/backend/src/persist/migration/settings.rs @@ -2,7 +2,9 @@ use std::collections::HashMap; use serde::{Deserialize, Serialize}; -use crate::persist::{BatteryJson, CpuJson, DriverJson, FileJson, SettingsJson, LATEST_VERSION}; +use crate::persist::{ + BatteryJson, CpuJson, DriverJson, FileJson, SerdeError, SettingsJson, LATEST_VERSION, +}; use super::gpu::OldGpuJson; @@ -56,3 +58,10 @@ impl From for FileJson { } } } + +impl OldSettingsJson { + pub fn open>(path: P) -> Result { + let mut file = std::fs::File::open(path).map_err(SerdeError::Io)?; + serde_json::from_reader(&mut file).map_err(|e| SerdeError::Serde(e.into())) + } +} -- 2.43.4 From db371d8d8f48660fafe67faeb3bca792042ae054 Mon Sep 17 00:00:00 2001 From: user Date: Tue, 18 Jun 2024 12:23:02 +0200 Subject: [PATCH 10/11] Rewrote migration initialization situation --- backend/src/api/handler.rs | 49 ++++++++++++++++++++++++++++---------- 1 file changed, 37 insertions(+), 12 deletions(-) diff --git a/backend/src/api/handler.rs b/backend/src/api/handler.rs index e2b24ed..faae7e5 100644 --- a/backend/src/api/handler.rs +++ b/backend/src/api/handler.rs @@ -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()`. + 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() { -- 2.43.4 From e13f87f81f3149fceeb2756e4883844c56575260 Mon Sep 17 00:00:00 2001 From: user Date: Fri, 21 Jun 2024 11:31:11 +0200 Subject: [PATCH 11/11] Add logic for creating new savefile from legacy savefile if new one isn't present --- backend/src/api/handler.rs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/backend/src/api/handler.rs b/backend/src/api/handler.rs index faae7e5..3f36ce0 100644 --- a/backend/src/api/handler.rs +++ b/backend/src/api/handler.rs @@ -547,7 +547,12 @@ impl ApiMessageHandler { } else { // A savefile in the current format doesn't exist yet. // TODO: Parse it to the new format and save it. + + let app_id_from_game_id: u64; + let new_path = format!("{app_id_from_game_id}.ron"); + let migrated_settings: crate::persist::FileJson = legacy_file.into(); + migrated_settings.save(new_path); } } else if current_settings.is_some() { // ... -- 2.43.4