From d65d3b2aec1d66d331fd583c909a78544ba7d2bc Mon Sep 17 00:00:00 2001 From: hut Date: Tue, 30 Apr 2024 22:26:54 +0200 Subject: [PATCH] read preferences from outfly.toml --- Cargo.lock | 42 ++++++++++++++++++++++++--- Cargo.toml | 6 ++-- outfly.toml | 21 ++++++++++++++ src/main.rs | 2 ++ src/var.rs | 83 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 146 insertions(+), 8 deletions(-) create mode 100644 outfly.toml diff --git a/Cargo.lock b/Cargo.lock index 3f0d8b1..f3b3884 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -691,7 +691,7 @@ dependencies = [ "quote", "rustc-hash", "syn 2.0.52", - "toml_edit", + "toml_edit 0.21.1", ] [[package]] @@ -2813,7 +2813,7 @@ dependencies = [ "regex", "serde", "serde_yaml", - "toml_edit", + "toml_edit 0.22.12", ] [[package]] @@ -2997,7 +2997,7 @@ version = "3.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6d37c51ca738a55da99dc0c4a34860fd675453b8b36209178c2249bb13651284" dependencies = [ - "toml_edit", + "toml_edit 0.21.1", ] [[package]] @@ -3304,6 +3304,15 @@ dependencies = [ "serde", ] +[[package]] +name = "serde_spanned" +version = "0.6.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "eb3622f419d1296904700073ea6cc23ad690adbd66f13ea683df73298736f0c1" +dependencies = [ + "serde", +] + [[package]] name = "serde_yaml" version = "0.9.34+deprecated" @@ -3592,6 +3601,9 @@ name = "toml_datetime" version = "0.6.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3550f4e9685620ac18a50ed434eb3aec30db8ba93b0287467bca5826ea25baf1" +dependencies = [ + "serde", +] [[package]] name = "toml_edit" @@ -3601,7 +3613,20 @@ checksum = "6a8534fd7f78b5405e860340ad6575217ce99f38d4d5c8f2442cb5ecb50090e1" dependencies = [ "indexmap", "toml_datetime", - "winnow", + "winnow 0.5.40", +] + +[[package]] +name = "toml_edit" +version = "0.22.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d3328d4f68a705b2a4498da1d580585d39a6510f98318a2cec3018a7ec61ddef" +dependencies = [ + "indexmap", + "serde", + "serde_spanned", + "toml_datetime", + "winnow 0.6.7", ] [[package]] @@ -4481,6 +4506,15 @@ dependencies = [ "memchr", ] +[[package]] +name = "winnow" +version = "0.6.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "14b9415ee827af173ebb3f15f9083df5a122eb93572ec28741fb153356ea2578" +dependencies = [ + "memchr", +] + [[package]] name = "winreg" version = "0.10.1" diff --git a/Cargo.toml b/Cargo.toml index cce9009..acf92de 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -36,13 +36,11 @@ bevy_embedded_assets = { version = "0.10.2", optional = true } fastrand = "2.0" # For conversation specification files, which are in YAML -serde = "1.0" +serde = { version = "1.0", features = ["derive"] } serde_yaml = "0.9" # For reading/writing the player's configuration file. -# Use the same version/features as the already included toml_edit, -# in the hope of shaving off some kilobytes from the resulting binary -toml_edit = { version = "*", default-features = false, features = ["parse"] } +toml_edit = { version = "0.22", default-features = false, features = ["parse", "serde"] } [build-dependencies] embed-resource = "1.6.3" # embedding of .exe metadata diff --git a/outfly.toml b/outfly.toml new file mode 100644 index 0000000..ba34066 --- /dev/null +++ b/outfly.toml @@ -0,0 +1,21 @@ +# ▄████████▄ + ███ + ▄█████████ ███ + +# ███▀ ▀███ + + ███ ███▀ + ███ + + +# ███ + ███ ███ ███ █████████ ███ ███ ███ ███ +# ███ +███ ███ ███ ███ ███▐██████ ███ ███ ███ +# ███ + ███ ███+ ███ +███ ███ + ███ ███ + ███ +# ███▄ ▄███ ███▄ ███ ███ + ███ + ███ ███▄ ███ +# ▀████████▀ + ▀███████ ███▄ ███▄ ▀████ ▀███████ +# + + + ███ +# + ▀████████████████████████████████████████████████████▀ +# +# User preferences for the game OutFly. +# See https://codeberg.org/hut/outfly + +# fullscreen_mode may be "borderless", "true", or "sized" +fullscreen_mode = "borderless" + +# window_mode may be "windowed", or "fullscreen" +window_mode = "fullscreen" + +# render_mode may be "vulkan" or "gl" +render_mode = "vulkan" diff --git a/src/main.rs b/src/main.rs index 18c6c12..1346a1a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -127,6 +127,8 @@ impl Plugin for OutFlyPlugin { skeleton::SkeletonPlugin, world::WorldPlugin, )); + + var::load_prefs(); } } diff --git a/src/var.rs b/src/var.rs index 74c30cc..a1e1978 100644 --- a/src/var.rs +++ b/src/var.rs @@ -13,6 +13,11 @@ use bevy::prelude::*; use std::collections::HashMap; +use serde::Deserialize; +use toml_edit::DocumentMut; +use toml_edit::de::from_document; +use std::env; +use std::fs; pub const SCOPE_SEPARATOR: &str = "$"; @@ -26,6 +31,84 @@ pub const TOKEN_NEGATE: &str = "~"; pub const DEFAULT_CHAT_SPEED: f32 = 10.0; +#[derive(Resource, Deserialize, Debug)] +#[serde(default)] +pub struct Preferences { + pub fullscreen_mode: String, + pub window_mode: String, + pub render_mode: String, +} + +impl Default for Preferences { + fn default() -> Self { + Self { + fullscreen_mode: "borderless".to_string(), + window_mode: "fullscreen".to_string(), + render_mode: "vulkan".to_string(), + } + } +} + +fn file_is_readable(file_path: &str) -> bool { + fs::metadata(file_path).map(|metadata| metadata.is_file()).unwrap_or(false) +} + +fn get_prefs_path() -> Option { + let test = "outfly.toml"; + if file_is_readable(test) { + return Some(test.to_string()); + } + if let Ok(basedir) = env::var("XDG_CONFIG_HOME") { + let test = basedir.to_string() + "/outfly/outfly.toml"; + if file_is_readable(test.as_str()) { + return Some(test); + } + } else if let Ok(basedir) = env::var("HOME") { + let test = basedir.to_string() + ".config/outfly/outfly.toml"; + if file_is_readable(test.as_str()) { + return Some(test); + } + } + return None; +} + +pub fn load_prefs() -> Preferences { + let prefs_path = match get_prefs_path() { + Some(path) => path, + None => { + warn!("Found no preference file, using default preferences."); + return Preferences::default(); + } + }; + let toml = fs::read_to_string(&prefs_path); + let toml = match toml { + Ok(toml) => toml, + Err(error) => { + error!("Failed to open preferences file '{prefs_path}': {error}"); + return Preferences::default(); + } + }; + match toml.parse::() { + Ok(doc) => { + match from_document::(doc) { + Ok(pref) => { + info!("Loaded preference file from {prefs_path}"); + dbg!(&pref); + return pref; + } + Err(error) => { + error!("Failed to read preference line: {error}"); + return Preferences::default(); + } + } + } + Err(error) => { + error!("Failed to open preferences: {error}"); + return Preferences::default(); + } + } +} + #[derive(Resource)] pub struct Settings { pub dev_mode: bool,