move persistent state into own file
This commit is contained in:
+8
-35
@@ -1,3 +1,4 @@
|
|||||||
|
mod persistent_state;
|
||||||
mod settings;
|
mod settings;
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
@@ -8,35 +9,16 @@ use crate::{
|
|||||||
force_close::ForceClose,
|
force_close::ForceClose,
|
||||||
launch::{Launch, Platform},
|
launch::{Launch, Platform},
|
||||||
},
|
},
|
||||||
gui::settings::Settings,
|
gui::{persistent_state::PersistentState, settings::Settings},
|
||||||
util::{
|
util::{
|
||||||
self,
|
self,
|
||||||
consts::{ENHANCED, GTA_WINDOW_TITLE, LEGACY},
|
consts::{ENHANCED, GTA_WINDOW_TITLE, LEGACY},
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
use eframe::egui;
|
use eframe::egui;
|
||||||
use serde::{Deserialize, Serialize};
|
use std::time::{Duration, Instant};
|
||||||
use std::{
|
|
||||||
fs::{self, File},
|
|
||||||
io::Write,
|
|
||||||
path::PathBuf,
|
|
||||||
sync::LazyLock,
|
|
||||||
time::{Duration, Instant},
|
|
||||||
};
|
|
||||||
|
|
||||||
const WINDOW_SIZE: [f32; 2] = [240.0, 240.0];
|
const WINDOW_SIZE: [f32; 2] = [240.0, 240.0];
|
||||||
static CONFIG_PATH: LazyLock<PathBuf> = LazyLock::new(|| {
|
|
||||||
dirs::config_local_dir()
|
|
||||||
.unwrap()
|
|
||||||
.join("GTA Tools")
|
|
||||||
.join("config.json")
|
|
||||||
});
|
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize)]
|
|
||||||
struct PersistentState {
|
|
||||||
launcher: Platform,
|
|
||||||
settings: Settings,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug, Default, PartialEq, Eq)]
|
#[derive(Debug, Default, PartialEq, Eq)]
|
||||||
pub enum Stage {
|
pub enum Stage {
|
||||||
@@ -396,14 +378,7 @@ impl Drop for App {
|
|||||||
launcher: self.launch.selected,
|
launcher: self.launch.selected,
|
||||||
settings: self.settings.clone(),
|
settings: self.settings.clone(),
|
||||||
};
|
};
|
||||||
let config_path = CONFIG_PATH.as_path();
|
persistent_state.set();
|
||||||
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();
|
|
||||||
// make sure we are not suspending game
|
// make sure we are not suspending game
|
||||||
features::empty_session::deactivate(self);
|
features::empty_session::deactivate(self);
|
||||||
}
|
}
|
||||||
@@ -438,12 +413,10 @@ fn app_creator(
|
|||||||
_cc: &eframe::CreationContext<'_>,
|
_cc: &eframe::CreationContext<'_>,
|
||||||
) -> Result<Box<dyn eframe::App>, Box<dyn std::error::Error + Send + Sync>> {
|
) -> Result<Box<dyn eframe::App>, Box<dyn std::error::Error + Send + Sync>> {
|
||||||
let mut app = Box::<App>::default();
|
let mut app = Box::<App>::default();
|
||||||
if let Ok(config) = fs::read_to_string(CONFIG_PATH.as_path()) {
|
if let Some(persistent_state) = PersistentState::get() {
|
||||||
if let Ok(persistent_state) = serde_json::from_str::<PersistentState>(&config) {
|
app.launch.selected = persistent_state.launcher;
|
||||||
app.launch.selected = persistent_state.launcher;
|
app.settings = persistent_state.settings.clone();
|
||||||
app.settings = persistent_state.settings;
|
};
|
||||||
}
|
|
||||||
}
|
|
||||||
if app.settings.start_elevated && !util::win::is_elevated() {
|
if app.settings.start_elevated && !util::win::is_elevated() {
|
||||||
util::win::elevate(util::win::ElevationExitMethod::Forced);
|
util::win::elevate(util::win::ElevationExitMethod::Forced);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,40 @@
|
|||||||
|
use crate::{features::launch::Platform, gui::settings::Settings};
|
||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
use std::{
|
||||||
|
fs::{self, File},
|
||||||
|
io::Write,
|
||||||
|
path::PathBuf,
|
||||||
|
sync::LazyLock,
|
||||||
|
};
|
||||||
|
|
||||||
|
static CONFIG_PATH: LazyLock<PathBuf> = LazyLock::new(|| {
|
||||||
|
dirs::config_local_dir()
|
||||||
|
.unwrap()
|
||||||
|
.join("GTA Tools")
|
||||||
|
.join("config.json")
|
||||||
|
});
|
||||||
|
|
||||||
|
#[derive(Serialize, Deserialize)]
|
||||||
|
pub struct PersistentState {
|
||||||
|
pub launcher: Platform,
|
||||||
|
pub settings: Settings,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl PersistentState {
|
||||||
|
pub fn get() -> Option<Self> {
|
||||||
|
fs::read_to_string(CONFIG_PATH.as_path())
|
||||||
|
.ok()
|
||||||
|
.and_then(|config| serde_json::from_str::<PersistentState>(&config).ok())
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn set(&self) {
|
||||||
|
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(&self).unwrap();
|
||||||
|
config_file.write_all(json.as_bytes()).unwrap();
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user