diff --git a/src/app_logic.rs b/src/app_logic.rs index 78bed54..ad65d33 100644 --- a/src/app_logic.rs +++ b/src/app_logic.rs @@ -3,12 +3,13 @@ use std::{sync::Arc, thread, time::Duration}; use crossbeam_channel::Receiver; use egui::{mutex::Mutex}; -use crate::omori_locator; +use crate::{config, omori_locator}; #[derive(Clone)] pub enum UiState { Loading, - KeyRequired(String) + KeyRequired(String), + Error(String) } #[derive(Clone)] @@ -33,10 +34,10 @@ impl AppThread { *state = s; } - fn get_key(&mut self, reason: String) { + fn get_key(&mut self, reason: String) -> anyhow::Result<()> { self.commit(UiState::KeyRequired(reason)); loop { - match self.ui_event_channel.recv().expect("The UI event channel was closed.") { + match self.ui_event_channel.recv()? { UiEvent::SetKey(key) => { self.decryption_key = key; break @@ -46,6 +47,8 @@ impl AppThread { } self.commit(UiState::Loading); + + Ok(()) } pub fn create(state_holder: &UiStateHolder, context: &egui::Context, ui_event_channel: &Receiver) -> AppThread { @@ -57,14 +60,24 @@ impl AppThread { } } - pub fn main(&mut self) { + fn real_main(&mut self) -> anyhow::Result<()> { self.commit(UiState::Loading); + let mut config_provider = config::ConfigProvider::new()?; + println!("{:?}", config_provider); + match omori_locator::get_omori_key() { Ok(key) => self.decryption_key = key, - Err(reason) => self.get_key(format!("{:?}", reason)), + Err(reason) => self.get_key(format!("{:?}", reason))?, } - println!("{:?}", self.decryption_key); + Ok(()) + } + + pub fn main(&mut self) { + match self.real_main() { + Ok(_) => {}, + Err(e) => self.commit(UiState::Error(format!("{:?}", e))), + } } } diff --git a/src/config.rs b/src/config.rs index 91fd61d..bb30304 100644 --- a/src/config.rs +++ b/src/config.rs @@ -1,3 +1,9 @@ +use std::{fs::{self, File}, path::PathBuf}; + +use anyhow::Context; +use serde::{de, Deserialize, Serialize}; + +#[derive(Deserialize, Serialize, Debug)] struct Config { key: Option> } @@ -6,4 +12,37 @@ impl Default for Config { fn default() -> Self { Self { key: Default::default() } } -} \ No newline at end of file +} + +#[derive(Debug)] +pub struct ConfigProvider { + config: Config, + base_path: PathBuf +} + +impl ConfigProvider { + pub fn new() -> anyhow::Result { + let mut base_path = dirs::preference_dir().with_context(|| "Failed to find your preference directory")?; + base_path.push(env!("CARGO_PKG_NAME")); + + fs::create_dir_all(&base_path)?; + + base_path.push("config.json"); + + if !fs::exists(&base_path)? { + let mut fd = File::create(&base_path)?; + serde_json::to_writer(&mut fd, &Config::default())?; + drop(fd); + } + + let mut fd = File::open(&base_path)?; + let config: Config = serde_json::from_reader(&mut fd)?; + drop(fd); + + Ok(ConfigProvider { config, base_path }) + } + + fn get_key(&self) -> Option> { + return self.config.key.clone(); + } +} diff --git a/src/main.rs b/src/main.rs index abf218e..168a006 100644 --- a/src/main.rs +++ b/src/main.rs @@ -74,6 +74,19 @@ impl eframe::App for Application { }); }); }, + UiState::Error(reason) => { + egui::TopBottomPanel::top("error_title_bar").show(ctx, |ui| { + ui.with_layout(Layout::top_down(Align::Center), |ui| { + ui.label(RichText::new("An error has occured :(").size(32.0)); + }); + }); + + egui::CentralPanel::default().show(ctx, |ui| { + center_vertical(ui,|ui| { + ui.label(RichText::new(reason).size(32.0)); + }); + }); + }, UiState::KeyRequired(reason) => { let mut is_valid = true; let hash = sha2::Sha256::digest(&self.key_input.as_bytes());