From c71ecb48fa4368035a852e2d06869a21382a6876 Mon Sep 17 00:00:00 2001 From: troido Date: Tue, 18 Feb 2020 01:11:49 +0100 Subject: Players are now saved/loaded too --- src/persistence.rs | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) (limited to 'src/persistence.rs') diff --git a/src/persistence.rs b/src/persistence.rs index 1808652..7c15ec0 100644 --- a/src/persistence.rs +++ b/src/persistence.rs @@ -1,6 +1,7 @@ use std::path::PathBuf; use std::fs; +use std::env; use serde_json; use serde_json::Value; use crate::{ @@ -29,9 +30,27 @@ pub struct FileStorage { } impl FileStorage { - pub fn new(path: &str) -> Self { + pub fn new(path: PathBuf) -> Self { Self { - directory: PathBuf::from(path) + directory: path + } + } + + pub fn savedir() -> Option { + if let Some(pathname) = env::var_os("ASCIIFARM_SAVE_DIR") { + Some(PathBuf::from(pathname)) + } else if let Some(pathname) = env::var_os("XDG_DATA_HOME") { + let mut path = PathBuf::from(pathname); + path.push("asciifarm"); + path.push("saves"); + Some(path) + } else if let Some(pathname) = env::var_os("HOME") { + let mut path = PathBuf::from(pathname); + path.push(".asciifarm"); + path.push("saves"); + Some(path) + } else { + None } } } -- cgit