From 807cb5242f3ff4f8299705de536901d39aba739c Mon Sep 17 00:00:00 2001 From: futile Date: Tue, 8 Apr 2025 08:35:29 +0100 Subject: [PATCH] implement some persistent state (namely selected game launcher) --- Cargo.lock | 65 ++++++++++++++++++++++++++++++++++++++++++ Cargo.toml | 3 ++ src/features/launch.rs | 3 +- src/gui.rs | 41 +++++++++++++++++++++++++- 4 files changed, 110 insertions(+), 2 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 1583d9c..f7f10ab 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -734,6 +734,27 @@ dependencies = [ "crypto-common", ] +[[package]] +name = "dirs" +version = "6.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c3e8aa94d75141228480295a7d0e7feb620b1a5ad9f12bc40be62411e38cce4e" +dependencies = [ + "dirs-sys", +] + +[[package]] +name = "dirs-sys" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e01a3366d27ee9890022452ee61b2b63a67e6f13f58900b651ff5665f0bb1fab" +dependencies = [ + "libc", + "option-ext", + "redox_users", + "windows-sys 0.59.0", +] + [[package]] name = "dispatch" version = "0.2.0" @@ -1348,10 +1369,13 @@ name = "gta-tools" version = "0.2.0" dependencies = [ "catppuccin-egui", + "dirs", "eframe", "egui_extras", "image", "open", + "serde", + "serde_json", "static_vcruntime", "sysinfo", "windows 0.61.1", @@ -1591,6 +1615,12 @@ dependencies = [ "once_cell", ] +[[package]] +name = "itoa" +version = "1.0.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4a5f13b858c8d314ee3e8f639011f7ccefe71f97f96e50151fb991f267928e2c" + [[package]] name = "jni" version = "0.21.1" @@ -2212,6 +2242,12 @@ dependencies = [ "pathdiff", ] +[[package]] +name = "option-ext" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "04744f49eae99ab78e0d5c0b603ab218f515ea8cfe5a456d7629ad883a3b6e7d" + [[package]] name = "orbclient" version = "0.3.48" @@ -2538,6 +2574,17 @@ dependencies = [ "bitflags 2.9.0", ] +[[package]] +name = "redox_users" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dd6f9d3d47bdd2ad6945c5015a226ec6155d0bcdfd8f7cd29f86b71f8de99d2b" +dependencies = [ + "getrandom 0.2.15", + "libredox", + "thiserror 2.0.12", +] + [[package]] name = "renderdoc-sys" version = "1.1.0" @@ -2582,6 +2629,12 @@ version = "1.0.20" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "eded382c5f5f786b989652c49544c4877d9f015cc22e145a5ea8ea66c2921cd2" +[[package]] +name = "ryu" +version = "1.0.20" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "28d3b2b1366ec20994f1fd18c3c594f05c5dd4bc44d8bb0c1c632c8d6829481f" + [[package]] name = "same-file" version = "1.0.6" @@ -2636,6 +2689,18 @@ dependencies = [ "syn", ] +[[package]] +name = "serde_json" +version = "1.0.140" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "20068b6e96dc6c9bd23e01df8827e6c7e1f2fddd43c21810382803c136b99373" +dependencies = [ + "itoa", + "memchr", + "ryu", + "serde", +] + [[package]] name = "serde_repr" version = "0.1.20" diff --git a/Cargo.toml b/Cargo.toml index 1513667..fb0dbeb 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,10 +7,13 @@ edition = "2024" catppuccin-egui = { version = "5.5.0", default-features = false, features = [ "egui31", ] } +dirs = "6.0.0" eframe = "0.31.1" egui_extras = { version = "0.31.1", features = ["image"] } image = { version = "0.25.6", default-features = false, features = ["png"] } open = "5.3.2" +serde = { version = "1.0.219", features = ["derive"] } +serde_json = "1.0.140" sysinfo = "0.34.2" windows = { version = "0.61.1", features = [ "Win32_UI_Input_KeyboardAndMouse", diff --git a/src/features/launch.rs b/src/features/launch.rs index 2c4c762..af26883 100644 --- a/src/features/launch.rs +++ b/src/features/launch.rs @@ -1,7 +1,8 @@ +use serde::{Deserialize, Serialize}; use std::{fmt::Display, path::PathBuf, process::Command}; use winreg::{RegKey, enums::HKEY_LOCAL_MACHINE}; -#[derive(Default, Debug, PartialEq, Eq)] +#[derive(Clone, Default, Debug, PartialEq, Eq, Serialize, Deserialize)] pub enum Platform { #[default] Steam, diff --git a/src/gui.rs b/src/gui.rs index c48f477..38ad144 100644 --- a/src/gui.rs +++ b/src/gui.rs @@ -12,11 +12,29 @@ use crate::{ }, }; use eframe::egui; -use std::time::{Duration, Instant}; +use serde::{Deserialize, Serialize}; +use std::{ + fs::{self, File}, + io::Write, + path::PathBuf, + sync::LazyLock, + time::{Duration, Instant}, +}; use sysinfo::System; use windows::Win32::Foundation::HANDLE; const THEME: catppuccin_egui::Theme = catppuccin_egui::MOCHA; +static CONFIG_PATH: LazyLock = LazyLock::new(|| { + let mut config_path = dirs::config_local_dir().unwrap(); + config_path.push("GTA Tools"); + config_path.push("config.json"); + config_path +}); + +#[derive(Serialize, Deserialize)] +struct PersistentState { + launcher: Platform, +} #[derive(Debug, Default, PartialEq, Eq)] pub enum Stage { @@ -64,6 +82,11 @@ impl eframe::App for App { if !self.initialized { catppuccin_egui::set_theme(ctx, THEME); egui_extras::install_image_loaders(ctx); + if let Ok(config) = fs::read_to_string(CONFIG_PATH.as_path()) { + if let Ok(persistent_state) = serde_json::from_str::(&config) { + self.launch.selected = persistent_state.launcher; + } + } self.initialized = true; } self.run_timers(); @@ -299,6 +322,22 @@ impl App { } } +impl Drop for App { + fn drop(&mut self) { + let persistent_state = PersistentState { + launcher: self.launch.selected.clone(), + }; + let config_path = CONFIG_PATH.as_path(); + let config_path_parent = config_path.parent().unwrap(); + if !config_path_parent.exists() { + fs::create_dir(config_path_parent).unwrap(); + } + let mut config_file = File::create(config_path).unwrap(); + let json = serde_json::to_string_pretty(&persistent_state).unwrap(); + config_file.write_all(json.as_bytes()).unwrap(); + } +} + fn load_icon() -> eframe::egui::IconData { let icon = include_bytes!("../assets/icon.png"); let image = image::load_from_memory(icon).unwrap().into_rgba8();