read preferences from outfly.toml

This commit is contained in:
yuni 2024-04-30 22:26:54 +02:00
parent 4ea2f2d1e4
commit d65d3b2aec
5 changed files with 146 additions and 8 deletions

42
Cargo.lock generated
View file

@ -691,7 +691,7 @@ dependencies = [
"quote", "quote",
"rustc-hash", "rustc-hash",
"syn 2.0.52", "syn 2.0.52",
"toml_edit", "toml_edit 0.21.1",
] ]
[[package]] [[package]]
@ -2813,7 +2813,7 @@ dependencies = [
"regex", "regex",
"serde", "serde",
"serde_yaml", "serde_yaml",
"toml_edit", "toml_edit 0.22.12",
] ]
[[package]] [[package]]
@ -2997,7 +2997,7 @@ version = "3.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6d37c51ca738a55da99dc0c4a34860fd675453b8b36209178c2249bb13651284" checksum = "6d37c51ca738a55da99dc0c4a34860fd675453b8b36209178c2249bb13651284"
dependencies = [ dependencies = [
"toml_edit", "toml_edit 0.21.1",
] ]
[[package]] [[package]]
@ -3304,6 +3304,15 @@ dependencies = [
"serde", "serde",
] ]
[[package]]
name = "serde_spanned"
version = "0.6.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "eb3622f419d1296904700073ea6cc23ad690adbd66f13ea683df73298736f0c1"
dependencies = [
"serde",
]
[[package]] [[package]]
name = "serde_yaml" name = "serde_yaml"
version = "0.9.34+deprecated" version = "0.9.34+deprecated"
@ -3592,6 +3601,9 @@ name = "toml_datetime"
version = "0.6.5" version = "0.6.5"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3550f4e9685620ac18a50ed434eb3aec30db8ba93b0287467bca5826ea25baf1" checksum = "3550f4e9685620ac18a50ed434eb3aec30db8ba93b0287467bca5826ea25baf1"
dependencies = [
"serde",
]
[[package]] [[package]]
name = "toml_edit" name = "toml_edit"
@ -3601,7 +3613,20 @@ checksum = "6a8534fd7f78b5405e860340ad6575217ce99f38d4d5c8f2442cb5ecb50090e1"
dependencies = [ dependencies = [
"indexmap", "indexmap",
"toml_datetime", "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]] [[package]]
@ -4481,6 +4506,15 @@ dependencies = [
"memchr", "memchr",
] ]
[[package]]
name = "winnow"
version = "0.6.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "14b9415ee827af173ebb3f15f9083df5a122eb93572ec28741fb153356ea2578"
dependencies = [
"memchr",
]
[[package]] [[package]]
name = "winreg" name = "winreg"
version = "0.10.1" version = "0.10.1"

View file

@ -36,13 +36,11 @@ bevy_embedded_assets = { version = "0.10.2", optional = true }
fastrand = "2.0" fastrand = "2.0"
# For conversation specification files, which are in YAML # For conversation specification files, which are in YAML
serde = "1.0" serde = { version = "1.0", features = ["derive"] }
serde_yaml = "0.9" serde_yaml = "0.9"
# For reading/writing the player's configuration file. # For reading/writing the player's configuration file.
# Use the same version/features as the already included toml_edit, toml_edit = { version = "0.22", default-features = false, features = ["parse", "serde"] }
# in the hope of shaving off some kilobytes from the resulting binary
toml_edit = { version = "*", default-features = false, features = ["parse"] }
[build-dependencies] [build-dependencies]
embed-resource = "1.6.3" # embedding of .exe metadata embed-resource = "1.6.3" # embedding of .exe metadata

21
outfly.toml Normal file
View file

@ -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"

View file

@ -127,6 +127,8 @@ impl Plugin for OutFlyPlugin {
skeleton::SkeletonPlugin, skeleton::SkeletonPlugin,
world::WorldPlugin, world::WorldPlugin,
)); ));
var::load_prefs();
} }
} }

View file

@ -13,6 +13,11 @@
use bevy::prelude::*; use bevy::prelude::*;
use std::collections::HashMap; 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 = "$"; pub const SCOPE_SEPARATOR: &str = "$";
@ -26,6 +31,84 @@ pub const TOKEN_NEGATE: &str = "~";
pub const DEFAULT_CHAT_SPEED: f32 = 10.0; 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<String> {
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::<DocumentMut>() {
Ok(doc) => {
match from_document::<Preferences>(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)] #[derive(Resource)]
pub struct Settings { pub struct Settings {
pub dev_mode: bool, pub dev_mode: bool,