diff --git a/src/main.rs b/src/main.rs index ce57c26..a3189a7 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,8 +1,30 @@ #![windows_subsystem = "windows"] -use iced::advanced::Application; -use pumpbin::Pumpbin; +use std::{fs, ops::Not}; -fn main() -> iced::Result { - Pumpbin::run(Pumpbin::settings()) +use anyhow::anyhow; +use dirs::data_dir; +use iced::advanced::Application; +use pumpbin::{plugin::CONFIG_FILE_PATH, Pumpbin}; + +fn main() -> anyhow::Result<()> { + let mut config_path = data_dir().ok_or(anyhow!("Get data_dir failed."))?; + config_path.push("PumpBin"); + config_path.push("plugins"); + + if let Some(parent) = config_path.parent() { + if parent.exists().not() { + fs::create_dir_all(parent)?; + } else if parent.is_dir().not() { + fs::remove_file(parent)?; + fs::create_dir_all(parent)?; + } + } + + CONFIG_FILE_PATH + .set(config_path) + .map_err(|_| anyhow!("Set CONFIG_FILE_PATH failed."))?; + + Pumpbin::run(Pumpbin::settings())?; + Ok(()) } diff --git a/src/plugin.rs b/src/plugin.rs index 31b7329..b323183 100644 --- a/src/plugin.rs +++ b/src/plugin.rs @@ -1,9 +1,14 @@ -use std::{collections::HashMap, fmt::Display, fs, ops::Not, path::Path}; +use std::{ + collections::HashMap, + fmt::Display, + fs, + path::{Path, PathBuf}, + sync::OnceLock, +}; use aes_gcm::{aead::Aead, Aes256Gcm, Key, KeyInit, Nonce}; use anyhow::anyhow; use bincode::{decode_from_slice, encode_to_vec, Decode, Encode}; -use dirs::data_dir; // 500 MiB const LIMIT: usize = 1024 * 1024 * 500; @@ -14,6 +19,8 @@ pub const BINCODE_PLUGIN_CONFIG: bincode::config::Configuration< > = bincode::config::standard().with_limit(); const BINCODE_PLUGINS_CONFIG: bincode::config::Configuration = bincode::config::standard(); +pub static CONFIG_FILE_PATH: OnceLock = OnceLock::new(); + #[derive(Debug, Clone, Encode, Decode, PartialEq, Eq)] pub struct AesGcmPass { key_holder: Vec, @@ -194,31 +201,25 @@ pub struct Plugins(pub HashMap); impl Plugins { pub fn reade_plugins() -> anyhow::Result { - let mut plugins_path = data_dir().ok_or(anyhow::anyhow!("data_dir is none."))?; - plugins_path.push("pumpbin"); - plugins_path.push("plugins"); + let plugins_path = CONFIG_FILE_PATH + .get() + .ok_or(anyhow!("Get config file path failed."))?; - if plugins_path.exists() && plugins_path.is_file() { - let buf = fs::read(plugins_path)?; - let (plugins, _) = decode_from_slice(buf.as_slice(), BINCODE_PLUGINS_CONFIG)?; - Ok(plugins) - } else { - anyhow::bail!("file not exists.") - } + let buf = fs::read(plugins_path)?; + let (plugins, _) = decode_from_slice(buf.as_slice(), BINCODE_PLUGINS_CONFIG)?; + Ok(plugins) } pub fn uptade_plugins(&self) -> anyhow::Result<()> { let buf = encode_to_vec(self, BINCODE_PLUGINS_CONFIG)?; + let plugins_path = CONFIG_FILE_PATH + .get() + .ok_or(anyhow!("Get config file path failed."))?; - let mut plugins_path = data_dir().ok_or(anyhow::anyhow!("data_dir is none."))?; - plugins_path.push("pumpbin"); - if plugins_path.exists().not() { - fs::create_dir_all(&plugins_path)?; - } else if plugins_path.exists() && plugins_path.is_dir().not() { - fs::remove_file(&plugins_path)?; - fs::create_dir_all(&plugins_path)?; + if plugins_path.is_dir() { + fs::remove_dir(plugins_path)?; } - plugins_path.push("plugins"); + fs::write(plugins_path, buf)?; Ok(())