move Preferences struct under Settings struct

This commit is contained in:
yuni 2024-04-30 23:42:48 +02:00
parent 3a0b3dc1c3
commit 2d97ed1416

View file

@ -31,102 +31,6 @@ 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, Default)]
#[serde(default)]
pub struct Preferences {
pub fullscreen_mode: String,
pub window_mode: String,
pub render_mode: String,
#[serde(skip)]
pub source_file: Option<String>,
}
impl Preferences {
pub fn get_fullscreen_mode(&self) -> WindowMode {
match self.fullscreen_mode.as_str() {
"legacy" => WindowMode::Fullscreen,
"sized" => WindowMode::SizedFullscreen,
_ => WindowMode::BorderlessFullscreen,
}
}
pub fn get_window_mode(&self) -> WindowMode {
match self.window_mode.as_str() {
"fullscreen" => self.get_fullscreen_mode(),
_ => WindowMode::Windowed,
}
}
pub fn render_mode_is_gl(&self) -> bool {
return self.render_mode == "gl";
}
}
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 (toml, path) = match get_prefs_path() {
Some(path) => {
let toml = fs::read_to_string(&path);
match toml {
Ok(toml) => (toml, Some(path)),
Err(error) => {
error!("Failed to open preferences file '{path}': {error}");
return Preferences::default();
}
}
}
None => {
warn!("Found no preference file, using default preferences.");
(include_str!("data/outfly.toml").to_string(), None)
}
};
match toml.parse::<DocumentMut>() {
Ok(doc) => {
match toml_edit::de::from_document::<Preferences>(doc) {
Ok(mut pref) => {
if let Some(path) = &path {
info!("Loaded preference file from {path}");
} else {
info!("Loaded preferences from internal defaults");
}
pref.source_file = 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,
@ -360,6 +264,102 @@ impl Settings {
} }
} }
#[derive(Resource, Deserialize, Debug, Default)]
#[serde(default)]
pub struct Preferences {
pub fullscreen_mode: String,
pub window_mode: String,
pub render_mode: String,
#[serde(skip)]
pub source_file: Option<String>,
}
impl Preferences {
pub fn get_fullscreen_mode(&self) -> WindowMode {
match self.fullscreen_mode.as_str() {
"legacy" => WindowMode::Fullscreen,
"sized" => WindowMode::SizedFullscreen,
_ => WindowMode::BorderlessFullscreen,
}
}
pub fn get_window_mode(&self) -> WindowMode {
match self.window_mode.as_str() {
"fullscreen" => self.get_fullscreen_mode(),
_ => WindowMode::Windowed,
}
}
pub fn render_mode_is_gl(&self) -> bool {
return self.render_mode == "gl";
}
}
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 (toml, path) = match get_prefs_path() {
Some(path) => {
let toml = fs::read_to_string(&path);
match toml {
Ok(toml) => (toml, Some(path)),
Err(error) => {
error!("Failed to open preferences file '{path}': {error}");
return Preferences::default();
}
}
}
None => {
warn!("Found no preference file, using default preferences.");
(include_str!("data/outfly.toml").to_string(), None)
}
};
match toml.parse::<DocumentMut>() {
Ok(doc) => {
match toml_edit::de::from_document::<Preferences>(doc) {
Ok(mut pref) => {
if let Some(path) = &path {
info!("Loaded preference file from {path}");
} else {
info!("Loaded preferences from internal defaults");
}
pref.source_file = 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 GameVars { pub struct GameVars {
pub db: HashMap<String, String>, pub db: HashMap<String, String>,