From 1353fe3fdb146895bd74b498028d26faef7f0a3a Mon Sep 17 00:00:00 2001 From: "Luke D. Jones" Date: Fri, 17 May 2024 21:54:37 +1200 Subject: [PATCH] Rename and recreate the default Anime config if cache setup fails --- CHANGELOG.md | 4 ++++ asusd/src/ctrl_anime/mod.rs | 9 +++++++-- config-traits/src/lib.rs | 6 +----- rog-anime/src/diagonal.rs | 7 ++++++- rog-anime/src/gif.rs | 12 +++++++++--- rog-anime/src/image.rs | 6 +++++- 6 files changed, 32 insertions(+), 12 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5f426e58..9246658b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Add G513RS to laptop DB. +### Changed + +- Rename and recreate the default Anime config if cache setup fails + ## [v6.0.8] ### Added diff --git a/asusd/src/ctrl_anime/mod.rs b/asusd/src/ctrl_anime/mod.rs index d231cf81..fc9a6823 100644 --- a/asusd/src/ctrl_anime/mod.rs +++ b/asusd/src/ctrl_anime/mod.rs @@ -90,7 +90,7 @@ impl CtrlAnime { // } // } - let config = AnimeConfig::new().load(); + let mut config = AnimeConfig::new().load(); let mut anime_type = get_anime_type()?; if let AnimeType::Unknown = anime_type { if let Some(model) = config.model_override { @@ -101,7 +101,12 @@ impl CtrlAnime { info!("Device has an AniMe Matrix display: {anime_type:?}"); let mut cache = AnimeConfigCached::default(); - cache.init_from_config(&config, anime_type)?; + if let Err(e) = cache.init_from_config(&config, anime_type) { + error!("Trying to cache the Anime Config failed, will reset to default config: {e:?}"); + config.rename_file_old(); + config = AnimeConfig::new(); + config.write(); + } let ctrl = CtrlAnime { node, diff --git a/config-traits/src/lib.rs b/config-traits/src/lib.rs index e57c28fd..289a974c 100644 --- a/config-traits/src/lib.rs +++ b/config-traits/src/lib.rs @@ -146,11 +146,7 @@ where /// Renames the existing file to `-old` fn rename_file_old(&self) { - warn!( - "Renaming {} to {}-old and recreating config", - self.file_name(), - self.file_name() - ); + warn!("Renaming {} to {}-old", self.file_name(), self.file_name()); let mut cfg_old = self.file_path().to_string_lossy().to_string(); cfg_old.push_str("-old"); std::fs::rename(self.file_path(), cfg_old).unwrap_or_else(|err| { diff --git a/rog-anime/src/diagonal.rs b/rog-anime/src/diagonal.rs index b9758d65..a71f9f99 100644 --- a/rog-anime/src/diagonal.rs +++ b/rog-anime/src/diagonal.rs @@ -3,6 +3,8 @@ use std::path::Path; use std::time::Duration; +use log::error; + use crate::data::AnimeDataBuffer; use crate::error::{AnimeError, Result}; use crate::AnimeType; @@ -49,7 +51,10 @@ impl AnimeDiagonal { bright: f32, anime_type: AnimeType, ) -> Result { - let data = std::fs::read(path)?; + let data = std::fs::read(path).map_err(|e| { + error!("Could not open {path:?}: {e:?}"); + e + })?; let data = std::io::Cursor::new(data); let decoder = png_pong::Decoder::new(data)?.into_steps(); let png_pong::Step { raster, delay: _ } = decoder.last().ok_or(AnimeError::NoFrames)??; diff --git a/rog-anime/src/gif.rs b/rog-anime/src/gif.rs index 9437a749..3f01db4f 100644 --- a/rog-anime/src/gif.rs +++ b/rog-anime/src/gif.rs @@ -4,6 +4,7 @@ use std::path::Path; use std::time::Duration; use glam::Vec2; +use log::error; use serde_derive::{Deserialize, Serialize}; use crate::error::{AnimeError, Result}; @@ -107,7 +108,10 @@ impl AnimeGif { // Configure the decoder such that it will expand the image to RGBA. decoder.set_color_output(gif::ColorOutput::RGBA); // Read the file header - let file = File::open(file_name)?; + let file = File::open(file_name).map_err(|e| { + error!("Could not open {file_name:?}: {e:?}"); + e + })?; let mut decoder = decoder.read_info(file)?; let mut frames = Vec::default(); @@ -186,12 +190,14 @@ impl AnimeGif { anime_type: AnimeType, ) -> Result { let mut frames = Vec::new(); - let mut decoder = gif::DecodeOptions::new(); // Configure the decoder such that it will expand the image to RGBA. decoder.set_color_output(gif::ColorOutput::RGBA); // Read the file header - let file = File::open(file_name)?; + let file = File::open(file_name).map_err(|e| { + error!("Could not open {file_name:?}: {e:?}"); + e + })?; let mut decoder = decoder.read_info(file)?; let height = decoder.height(); diff --git a/rog-anime/src/image.rs b/rog-anime/src/image.rs index ddfa1ee0..a42e945b 100644 --- a/rog-anime/src/image.rs +++ b/rog-anime/src/image.rs @@ -3,6 +3,7 @@ use std::path::Path; pub use glam::Vec2; use glam::{Mat3, Vec3}; +use log::error; use crate::data::AnimeDataBuffer; use crate::error::{AnimeError, Result}; @@ -421,7 +422,10 @@ impl AnimeImage { bright: f32, anime_type: AnimeType, ) -> Result { - let data = std::fs::read(path)?; + let data = std::fs::read(path).map_err(|e| { + error!("Could not open {path:?}: {e:?}"); + e + })?; let data = std::io::Cursor::new(data); let decoder = png_pong::Decoder::new(data)?.into_steps(); let png_pong::Step { raster, delay: _ } = decoder.last().ok_or(AnimeError::NoFrames)??;