2021-07-19 02:57:14 +00:00
|
|
|
use crate::hud;
|
2021-12-16 18:37:15 +00:00
|
|
|
use common::character::CharacterId;
|
2020-06-16 13:55:37 +00:00
|
|
|
use hashbrown::HashMap;
|
2020-10-05 08:35:24 +00:00
|
|
|
use serde::{Deserialize, Serialize};
|
2021-07-19 02:57:14 +00:00
|
|
|
use std::{
|
|
|
|
fs,
|
|
|
|
io::Write,
|
|
|
|
path::{Path, PathBuf},
|
|
|
|
};
|
2020-06-21 10:22:26 +00:00
|
|
|
use tracing::warn;
|
2020-06-16 13:55:37 +00:00
|
|
|
|
|
|
|
/// Represents a character in the profile.
|
|
|
|
#[derive(Clone, Debug, Serialize, Deserialize)]
|
|
|
|
#[serde(default)]
|
|
|
|
pub struct CharacterProfile {
|
|
|
|
/// Array representing a character's hotbar.
|
|
|
|
pub hotbar_slots: [Option<hud::HotbarSlotContents>; 10],
|
|
|
|
}
|
|
|
|
|
2021-01-08 19:12:09 +00:00
|
|
|
const fn default_slots() -> [Option<hud::HotbarSlotContents>; 10] {
|
2021-12-16 18:37:15 +00:00
|
|
|
[None, None, None, None, None, None, None, None, None, None]
|
2021-01-08 19:12:09 +00:00
|
|
|
}
|
2020-11-14 19:32:39 +00:00
|
|
|
|
2020-06-16 13:55:37 +00:00
|
|
|
impl Default for CharacterProfile {
|
|
|
|
fn default() -> Self {
|
|
|
|
CharacterProfile {
|
2021-01-08 19:12:09 +00:00
|
|
|
hotbar_slots: default_slots(),
|
2020-06-16 13:55:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Represents a server in the profile.
|
|
|
|
#[derive(Clone, Debug, Serialize, Deserialize)]
|
|
|
|
#[serde(default)]
|
|
|
|
pub struct ServerProfile {
|
|
|
|
/// A map of character's by id to their CharacterProfile.
|
2020-09-17 23:02:14 +00:00
|
|
|
pub characters: HashMap<CharacterId, CharacterProfile>,
|
2020-11-14 19:32:39 +00:00
|
|
|
// Selected character in the chararacter selection screen
|
|
|
|
pub selected_character: Option<CharacterId>,
|
2020-06-16 13:55:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for ServerProfile {
|
|
|
|
fn default() -> Self {
|
|
|
|
ServerProfile {
|
|
|
|
characters: HashMap::new(),
|
2020-11-14 19:32:39 +00:00
|
|
|
selected_character: None,
|
2020-06-16 13:55:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// `Profile` contains everything that can be configured in the profile.ron
|
|
|
|
///
|
|
|
|
/// Initially it is just for persisting things that don't belong in
|
2020-08-25 12:21:25 +00:00
|
|
|
/// settings.ron - like the state of hotbar and any other character level
|
2020-06-16 13:55:37 +00:00
|
|
|
/// configuration.
|
|
|
|
#[derive(Clone, Debug, Serialize, Deserialize)]
|
|
|
|
#[serde(default)]
|
|
|
|
pub struct Profile {
|
|
|
|
pub servers: HashMap<String, ServerProfile>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for Profile {
|
|
|
|
fn default() -> Self {
|
|
|
|
Profile {
|
|
|
|
servers: HashMap::new(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Profile {
|
|
|
|
/// Load the profile.ron file from the standard path or create it.
|
2021-07-19 02:57:14 +00:00
|
|
|
pub fn load(config_dir: &Path) -> Self {
|
|
|
|
let path = Profile::get_path(config_dir);
|
2020-06-16 13:55:37 +00:00
|
|
|
|
|
|
|
if let Ok(file) = fs::File::open(&path) {
|
|
|
|
match ron::de::from_reader(file) {
|
|
|
|
Ok(profile) => return profile,
|
|
|
|
Err(e) => {
|
2020-06-21 10:22:26 +00:00
|
|
|
warn!(
|
2020-06-21 21:47:49 +00:00
|
|
|
?e,
|
|
|
|
?path,
|
|
|
|
"Failed to parse profile file! Falling back to default."
|
2020-06-16 13:55:37 +00:00
|
|
|
);
|
|
|
|
// Rename the corrupted profile file.
|
|
|
|
let new_path = path.with_extension("invalid.ron");
|
2020-06-21 21:47:49 +00:00
|
|
|
if let Err(e) = std::fs::rename(path.clone(), new_path.clone()) {
|
|
|
|
warn!(?e, ?path, ?new_path, "Failed to rename profile file.");
|
2020-06-16 13:55:37 +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_profile = Self::default();
|
2021-07-19 02:57:14 +00:00
|
|
|
default_profile.save_to_file_warn(config_dir);
|
2020-06-16 13:55:37 +00:00
|
|
|
default_profile
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Save the current profile to disk, warn on failure.
|
2021-07-19 02:57:14 +00:00
|
|
|
pub fn save_to_file_warn(&self, config_dir: &Path) {
|
|
|
|
if let Err(e) = self.save_to_file(config_dir) {
|
2020-06-21 21:47:49 +00:00
|
|
|
warn!(?e, "Failed to save profile");
|
2020-06-16 13:55:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Get the hotbar_slots for the requested character_id.
|
|
|
|
///
|
2020-11-14 19:32:39 +00:00
|
|
|
/// If the server or character does not exist then the default hotbar_slots
|
|
|
|
/// (empty) is returned.
|
2020-06-16 13:55:37 +00:00
|
|
|
///
|
|
|
|
/// # Arguments
|
|
|
|
///
|
|
|
|
/// * server - current server the character is on.
|
|
|
|
/// * character_id - id of the character.
|
|
|
|
pub fn get_hotbar_slots(
|
2020-11-14 19:32:39 +00:00
|
|
|
&self,
|
2020-06-16 13:55:37 +00:00
|
|
|
server: &str,
|
2020-09-17 23:02:14 +00:00
|
|
|
character_id: CharacterId,
|
2020-06-16 13:55:37 +00:00
|
|
|
) -> [Option<hud::HotbarSlotContents>; 10] {
|
|
|
|
self.servers
|
2020-11-14 19:32:39 +00:00
|
|
|
.get(server)
|
|
|
|
.and_then(|s| s.characters.get(&character_id))
|
2021-12-16 18:37:15 +00:00
|
|
|
.map(|c| c.hotbar_slots.clone())
|
2021-01-08 19:12:09 +00:00
|
|
|
.unwrap_or_else(default_slots)
|
2020-06-16 13:55:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Set the hotbar_slots for the requested character_id.
|
|
|
|
///
|
|
|
|
/// If the server or character does not exist then the appropriate fields
|
|
|
|
/// will be initialised and the slots added.
|
|
|
|
///
|
|
|
|
/// # Arguments
|
|
|
|
///
|
|
|
|
/// * server - current server the character is on.
|
|
|
|
/// * character_id - id of the character.
|
|
|
|
/// * slots - array of hotbar_slots to save.
|
|
|
|
pub fn set_hotbar_slots(
|
|
|
|
&mut self,
|
|
|
|
server: &str,
|
2020-09-17 23:02:14 +00:00
|
|
|
character_id: CharacterId,
|
2020-06-16 13:55:37 +00:00
|
|
|
slots: [Option<hud::HotbarSlotContents>; 10],
|
|
|
|
) {
|
|
|
|
self.servers
|
|
|
|
.entry(server.to_string())
|
|
|
|
.or_insert(ServerProfile::default())
|
|
|
|
// Get or update the CharacterProfile.
|
|
|
|
.characters
|
|
|
|
.entry(character_id)
|
|
|
|
.or_insert(CharacterProfile::default())
|
|
|
|
.hotbar_slots = slots;
|
|
|
|
}
|
|
|
|
|
2020-11-14 19:32:39 +00:00
|
|
|
/// Get the selected_character for the provided server.
|
|
|
|
///
|
|
|
|
/// if the server does not exist then the default selected_character (None)
|
|
|
|
/// is returned.
|
|
|
|
///
|
|
|
|
/// # Arguments
|
|
|
|
///
|
|
|
|
/// * server - current server the character is on.
|
|
|
|
pub fn get_selected_character(&self, server: &str) -> Option<CharacterId> {
|
|
|
|
self.servers
|
|
|
|
.get(server)
|
|
|
|
.map(|s| s.selected_character)
|
|
|
|
.unwrap_or_default()
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Set the selected_character for the provided server.
|
|
|
|
///
|
|
|
|
/// If the server does not exist then the appropriate fields
|
|
|
|
/// will be initialised and the selected_character added.
|
|
|
|
///
|
|
|
|
/// # Arguments
|
|
|
|
///
|
|
|
|
/// * server - current server the character is on.
|
|
|
|
/// * selected_character - option containing selected character ID
|
|
|
|
pub fn set_selected_character(
|
|
|
|
&mut self,
|
|
|
|
server: &str,
|
|
|
|
selected_character: Option<CharacterId>,
|
|
|
|
) {
|
|
|
|
self.servers
|
|
|
|
.entry(server.to_string())
|
|
|
|
.or_insert(ServerProfile::default())
|
|
|
|
.selected_character = selected_character;
|
|
|
|
}
|
|
|
|
|
2020-06-16 13:55:37 +00:00
|
|
|
/// Save the current profile to disk.
|
2021-07-19 02:57:14 +00:00
|
|
|
fn save_to_file(&self, config_dir: &Path) -> std::io::Result<()> {
|
|
|
|
let path = Profile::get_path(config_dir);
|
2020-06-16 13:55:37 +00:00
|
|
|
if let Some(dir) = path.parent() {
|
|
|
|
fs::create_dir_all(dir)?;
|
|
|
|
}
|
|
|
|
let mut config_file = fs::File::create(path)?;
|
|
|
|
|
|
|
|
let s: &str = &ron::ser::to_string_pretty(self, ron::ser::PrettyConfig::default()).unwrap();
|
|
|
|
config_file.write_all(s.as_bytes()).unwrap();
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2021-07-19 02:57:14 +00:00
|
|
|
fn get_path(config_dir: &Path) -> PathBuf { config_dir.join("profile.ron") }
|
2020-06-16 13:55:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_get_slots_with_empty_profile() {
|
2020-11-14 19:32:39 +00:00
|
|
|
let profile = Profile::default();
|
2020-06-16 13:55:37 +00:00
|
|
|
let slots = profile.get_hotbar_slots("TestServer", 12345);
|
2021-12-16 18:37:15 +00:00
|
|
|
assert_eq!(slots, [(); 10].map(|()| None))
|
2020-06-16 13:55:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_set_slots_with_empty_profile() {
|
|
|
|
let mut profile = Profile::default();
|
2021-12-16 18:37:15 +00:00
|
|
|
let slots = [(); 10].map(|()| None);
|
2020-06-16 13:55:37 +00:00
|
|
|
profile.set_hotbar_slots("TestServer", 12345, slots);
|
|
|
|
}
|
|
|
|
}
|