From 627708e0d9ec940f953d55a8ab7d0482c1145bd6 Mon Sep 17 00:00:00 2001 From: user Date: Thu, 13 Jun 2024 12:06:45 +0200 Subject: [PATCH] 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())) + } +}