veloren/voxygen/src/meta.rs

101 lines
3.2 KiB
Rust
Raw Normal View History

2020-01-20 13:37:29 +00:00
use common::comp;
use directories::ProjectDirs;
use serde::{Deserialize, Serialize};
use std::{fs, io::Write, path::PathBuf};
use tracing::warn;
2020-01-20 13:37:29 +00:00
2020-05-01 21:27:12 +00:00
const VALID_VERSION: u32 = 0; // Change this if you broke charsaves
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)]
2020-05-01 21:27:12 +00:00
//#[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,
2020-05-01 21:27:12 +00:00
pub version: u32,
2020-01-20 13:37:29 +00:00
}
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) {
2020-05-01 21:27:12 +00:00
match ron::de::from_reader::<_, Meta>(file) {
Ok(s) => {
if s.version == VALID_VERSION {
return s;
}
},
2020-01-20 13:37:29 +00:00
Err(e) => {
warn!(?e, ?file, "Failed to parse meta file! Fallback to default");
2020-01-20 13:37:29 +00:00
// Rename the corrupted settings file
let mut new_path = path.to_owned();
new_path.pop();
new_path.push("meta.invalid.ron");
if let Err(e) = std::fs::rename(path.clone(), new_path.clone()) {
warn!(?e, ?path, ?new_path, "Failed to rename meta file");
2020-01-20 13:37:29 +00:00
}
},
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!(?e, "Failed to save settings");
2020-01-20 13:37:29 +00:00
}
}
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(path) = std::env::var_os("VOXYGEN_CONFIG") {
let meta = PathBuf::from(path).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;
}
warn!(?path, "VOXYGEN_CONFIG points to invalid path.");
2020-01-20 13:37:29 +00:00
}
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
}
}