veloren/voxygen/src/meta.rs

95 lines
3.0 KiB
Rust
Raw Normal View History

2020-01-20 13:37:29 +00:00
use common::comp;
use directories::ProjectDirs;
use log::warn;
use serde_derive::{Deserialize, Serialize};
use std::{fs, io::Write, path::PathBuf};
2020-01-20 13:37:29 +00:00
#[derive(Clone, Debug, Serialize, Deserialize)]
2020-01-20 14:21:06 +00:00
#[repr(C)]
2020-01-20 13:37:29 +00:00
pub struct CharacterData {
pub name: String,
pub body: comp::Body,
pub tool: Option<String>,
2020-01-20 13:37:29 +00:00
}
#[derive(Clone, Debug, Default, Serialize, Deserialize)]
#[serde(default)]
2020-01-20 14:21:06 +00:00
#[repr(C)]
2020-01-20 13:37:29 +00:00
pub struct Meta {
pub characters: Vec<CharacterData>,
pub selected_character: usize,
}
impl Meta {
pub fn delete_character(&mut self, index: usize) {
self.characters.remove(index);
if index < self.selected_character {
self.selected_character -= 1;
}
}
pub fn add_character(&mut self, data: CharacterData) -> usize {
self.characters.push(data);
// return new character's index
self.characters.len() - 1
}
2020-01-20 13:37:29 +00:00
pub fn load() -> Self {
let path = Self::get_meta_path();
if let Ok(file) = fs::File::open(&path) {
match ron::de::from_reader(file) {
2020-01-20 13:37:29 +00:00
Ok(s) => return s,
Err(e) => {
log::warn!("Failed to parse meta file! Fallback to default. {}", e);
// Rename the corrupted settings file
let mut new_path = path.to_owned();
new_path.pop();
new_path.push("meta.invalid.ron");
2020-01-20 13:37:29 +00:00
if let Err(err) = std::fs::rename(path, new_path) {
log::warn!("Failed to rename meta file. {}", err);
}
},
2020-01-20 13:37:29 +00:00
}
}
// This is reached if either:
// - The file can't be opened (presumably it doesn't exist)
// - Or there was an error parsing the file
let default = Self::default();
default.save_to_file_warn();
default
}
pub fn save_to_file_warn(&self) {
if let Err(err) = self.save_to_file() {
warn!("Failed to save settings: {:?}", err);
}
}
pub fn save_to_file(&self) -> std::io::Result<()> {
let path = Self::get_meta_path();
if let Some(dir) = path.parent() {
fs::create_dir_all(dir)?;
}
let mut meta_file = fs::File::create(path)?;
let s: &str = &ron::ser::to_string_pretty(self, ron::ser::PrettyConfig::default()).unwrap();
meta_file.write_all(s.as_bytes()).unwrap();
2020-01-20 13:37:29 +00:00
Ok(())
}
pub fn get_meta_path() -> PathBuf {
if let Some(val) = std::env::var_os("VOXYGEN_CONFIG") {
let meta = PathBuf::from(val).join("meta.ron");
2020-01-20 13:37:29 +00:00
if meta.exists() || meta.parent().map(|x| x.exists()).unwrap_or(false) {
return meta;
}
log::warn!("VOXYGEN_CONFIG points to invalid path.");
}
let proj_dirs = ProjectDirs::from("net", "veloren", "voxygen")
.expect("System's $HOME directory path not found!");
proj_dirs.config_dir().join("meta").with_extension("ron")
2020-01-20 13:37:29 +00:00
}
}