Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions maxima-lib/src/content/downloader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use crate::{
},
util::{
hash::hash_file_crc32,
native::{maxima_dir, NativeError, SafeParent, SafeStr},
native::{maxima_cache_dir, NativeError, SafeParent, SafeStr},
},
};
use async_compression::tokio::write::DeflateDecoder;
Expand All @@ -37,7 +37,7 @@ use tokio::{
use tokio_util::compat::FuturesAsyncReadCompatExt;

fn zstate_path(id: &str, path: &str) -> Result<PathBuf, DownloaderError> {
let mut path = maxima_dir()?.join("temp/downloader").join(id).join(path);
let mut path = maxima_cache_dir()?.join("downloader").join(id).join(path);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we want this to be in cache and not in the temp directory?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure about /tmp, since it's usually tmpfs so only tiny files can be put there; also might be more hassle:

Since /tmp/ is accessible to other users of the system, it is essential that files and subdirectories under this directory are only created with mkstemp(3), mkdtemp(3), and similar calls. For more details, see Using /tmp/ and /var/tmp/ Safely.

I'm not sure how big this particular folder is during downloads though, I see it has .eazstate files after it was finished at least. Might want to make Maxima wipe them after the download is finished regardless, while /tmp should be wiped on reboot (it will be if tmpfs), some people have really long uptime too... So removing them once they're not needed is the correct choice.

But if it's just these tiny files and you don't mind a third function like maxima_temp_dir(), then sure could do that (then we could put them in Windows's %TEMP% aka %APPDATA%\Local\Temp)

path.set_extension("eazstate");
std::fs::create_dir_all(path.safe_parent()?)?;
Ok(path)
Expand Down
6 changes: 3 additions & 3 deletions maxima-lib/src/content/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ use crate::{
service_layer::ServiceLayerError,
MaximaEvent,
},
util::native::{maxima_dir, NativeError},
util::native::{maxima_cache_dir, NativeError},
};

const QUEUE_FILE: &str = "download_queue.json";
Expand Down Expand Up @@ -93,7 +93,7 @@ pub enum DownloaderError {

impl DownloadQueue {
pub(crate) async fn load() -> Result<DownloadQueue, ContentManagerError> {
let file = maxima_dir()?.join(QUEUE_FILE);
let file = maxima_cache_dir()?.join(QUEUE_FILE);
if !file.exists() {
return Ok(Self::default());
}
Expand All @@ -108,7 +108,7 @@ impl DownloadQueue {
}

pub(crate) async fn save(&self) -> Result<(), ContentManagerError> {
let file = maxima_dir()?.join(QUEUE_FILE);
let file = maxima_cache_dir()?.join(QUEUE_FILE);
fs::write(file, serde_json::to_string(&self)?).await?;
Ok(())
}
Expand Down
4 changes: 2 additions & 2 deletions maxima-lib/src/core/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ use crate::{
content::manager::{ContentManager, ContentManagerError},
lsx::{self, service::LSXServerError, types::LSXRequestType},
rtm::client::{BasicPresence, RtmClient},
util::native::{maxima_dir, NativeError},
util::native::{maxima_cache_dir, NativeError},
};

#[derive(Clone, IntoStaticStr)]
Expand Down Expand Up @@ -401,7 +401,7 @@ impl Maxima {
width: u16,
height: u16,
) -> Result<PathBuf, NativeError> {
let dir = maxima_dir()?.join("cache/avatars");
let dir = maxima_cache_dir()?.join("avatars");
create_dir_all(&dir)?;

Ok(dir.join(format!("{}_{}x{}.jpg", id, width, height)))
Expand Down
16 changes: 8 additions & 8 deletions maxima-lib/src/unix/wine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ use xz2::read::XzDecoder;

use crate::util::{
github::{fetch_github_release, fetch_github_releases, github_download_asset, GithubRelease},
native::{maxima_dir, DownloadError, NativeError, SafeParent, SafeStr, WineError},
native::{maxima_dir, maxima_cache_dir, DownloadError, NativeError, SafeParent, SafeStr, WineError},
registry::RegistryError,
};

Expand Down Expand Up @@ -315,19 +315,19 @@ pub(crate) async fn install_wine() -> Result<(), NativeError> {
None => return Err(NativeError::Wine(WineError::Fetch)),
};

let dir = maxima_dir()?.join("downloads");
let dir = maxima_cache_dir()?.join("downloads");
create_dir_all(&dir)?;

let path = dir.join(&asset.name);
github_download_asset(asset, &path)?;
extract_wine(&path)?;
let archive_path = dir.join(&asset.name);
github_download_asset(asset, &archive_path)?;
extract_wine(&archive_path)?;

let mut versions = versions()?;
versions.proton = release.tag_name;
set_versions(versions)?;

if let Err(err) = remove_file(&path) {
warn!("Failed to delete {:?} - {:?}", path, err);
if let Err(err) = remove_file(&archive_path) {
warn!("Failed to delete {:?} - {:?}", archive_path, err);
}

let _ = run_wine_command("", None::<[&str; 0]>, None, false, CommandType::Run).await;
Expand Down Expand Up @@ -413,7 +413,7 @@ pub async fn setup_wine_registry() -> Result<(), NativeError> {
}
}

let path = maxima_dir()?.join("temp").join("wine.reg");
let path = maxima_cache_dir()?.join("wine.reg");
tokio::fs::create_dir_all(path.safe_parent()?).await?;

{
Expand Down
92 changes: 77 additions & 15 deletions maxima-lib/src/util/native.rs
Original file line number Diff line number Diff line change
Expand Up @@ -223,17 +223,14 @@ pub fn module_path() -> Result<PathBuf, NativeError> {

#[cfg(target_os = "linux")]
pub fn module_path() -> Result<PathBuf, NativeError> {
let path = std::fs::read_link("/proc/self/exe");

Ok(path?)
Ok(std::fs::read_link("/proc/self/exe")?)
}

#[cfg(target_os = "macos")]
pub fn module_path() -> Result<PathBuf, NativeError> {
Ok(env::current_exe()?)
}

#[cfg(not(unix))]
pub fn maxima_dir() -> Result<PathBuf, NativeError> {
use directories::ProjectDirs;

Expand All @@ -243,17 +240,11 @@ pub fn maxima_dir() -> Result<PathBuf, NativeError> {
Ok(path)
}

#[cfg(unix)]
pub fn maxima_dir() -> Result<PathBuf, NativeError> {
let home = if let Ok(home) = env::var("XDG_DATA_HOME") {
home
} else if let Ok(home) = env::var("HOME") {
format!("{}/.local/share", home)
} else {
return Err(NativeError::MissingEnvironmentVariable("HOME".to_string()));
};

let path = PathBuf::from(format!("{}/maxima", home));
pub fn maxima_cache_dir() -> Result<PathBuf, NativeError> {
use directories::ProjectDirs;

let dirs = ProjectDirs::from("com", "ArmchairDevelopers", "Maxima");
let path = dirs.unwrap().cache_dir().to_path_buf();
create_dir_all(&path)?;
Ok(path)
}
Expand All @@ -267,3 +258,74 @@ pub fn platform_path<P: AsRef<Path>>(path: P) -> PathBuf {
pub fn platform_path<P: AsRef<Path>>(path: P) -> PathBuf {
PathBuf::from(path.as_ref())
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_maxima_dir() -> Result<(), NativeError> {
let dir_expected: PathBuf = if cfg!(target_os = "linux") {
let home = if let Ok(home) = env::var("XDG_DATA_HOME") {
home
} else if let Ok(home) = env::var("HOME") {
format!("{}/.local/share", home)
} else {
return Err(NativeError::MissingEnvironmentVariable("HOME".to_string()));
};
PathBuf::from(format!("{}/maxima", home))
} else if cfg!(target_os = "macos") {
if let Ok(home) = env::var("HOME") {
PathBuf::from(format!("{}/Library/Application Support/com.ArmchairDevelopers.Maxima", home))
} else {
return Err(NativeError::MissingEnvironmentVariable("HOME".to_string()));
}
} else if cfg!(target_os = "windows") {
if let Ok(appdata) = env::var("APPDATA") {
PathBuf::from(format!("{}/ArmchairDevelopers/Maxima/data", appdata))
} else {
return Err(NativeError::MissingEnvironmentVariable("APPDATA".to_string()));
}
} else {
panic!("unsupported platform");
};

let dir = maxima_dir().unwrap();
println!("[test_maxima_dir] Expected: {:?} Got: {:?}", dir_expected, dir);
assert_eq!(dir, dir_expected);
Ok(())
}

#[test]
fn test_maxima_cache_dir() -> Result<(), NativeError> {
let dir_expected: PathBuf = if cfg!(target_os = "linux") {
let home = if let Ok(home) = env::var("XDG_CACHE_HOME") {
home
} else if let Ok(home) = env::var("HOME") {
format!("{}/.cache", home)
} else {
return Err(NativeError::MissingEnvironmentVariable("HOME".to_string()));
};
PathBuf::from(format!("{}/maxima", home))
} else if cfg!(target_os = "macos") {
if let Ok(home) = env::var("HOME") {
PathBuf::from(format!("{}/Library/Caches/com.ArmchairDevelopers.Maxima", home))
} else {
return Err(NativeError::MissingEnvironmentVariable("HOME".to_string()));
}
} else if cfg!(target_os = "windows") {
if let Ok(localappdata) = env::var("LOCALAPPDATA") {
PathBuf::from(format!("{}/ArmchairDevelopers/Maxima/cache", localappdata))
} else {
return Err(NativeError::MissingEnvironmentVariable("LOCALAPPDATA".to_string()));
}
} else {
panic!("unsupported platform");
};

let dir = maxima_cache_dir().unwrap();
println!("[test_maxima_cache_dir] Expected: {:?} Got: {:?}", dir_expected, dir);
assert_eq!(dir, dir_expected);
Ok(())
}
}
8 changes: 4 additions & 4 deletions maxima-ui/src/bridge/get_games.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use maxima::{
},
LockedMaxima,
},
util::native::maxima_dir,
util::native::maxima_cache_dir,
};
use std::{fs, sync::mpsc::Sender};

Expand Down Expand Up @@ -245,9 +245,9 @@ pub async fn get_games_request(
});
channel.send(res)?;

let bg = maxima_dir()?.join("cache/ui/images/").join(&slug).join("background.jpg");
let game_hero = maxima_dir()?.join("cache/ui/images/").join(&slug).join("hero.jpg");
let game_logo = maxima_dir()?.join("cache/ui/images/").join(&slug).join("logo.png");
let bg = maxima_cache_dir()?.join("ui/images/").join(&slug).join("background.jpg");
let game_hero = maxima_cache_dir()?.join("ui/images/").join(&slug).join("hero.jpg");
let game_logo = maxima_cache_dir()?.join("ui/images/").join(&slug).join("logo.png");
let has_hero = fs::metadata(&game_hero).is_ok();
let has_logo = fs::metadata(&game_logo).is_ok();
let has_background = fs::metadata(&bg).is_ok();
Expand Down
6 changes: 3 additions & 3 deletions maxima-ui/src/ui_image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use core::slice::SlicePattern;
use log::{debug, error, info};

use image::io::Reader as ImageReader;
use maxima::util::native::{maxima_dir, NativeError, SafeStr};
use maxima::util::native::{maxima_cache_dir, NativeError, SafeStr};

#[derive(Clone, PartialEq, Eq, Hash, std::fmt::Debug)]
pub enum UIImageType {
Expand Down Expand Up @@ -101,9 +101,9 @@ impl UIImageCache {
}

fn get_path_for_image(variant: &UIImageType) -> Result<PathBuf, NativeError> {
let image_cache_root = maxima_dir()?.join("cache/ui/images");
let image_cache_root = maxima_cache_dir()?.join("ui/images");
// lib should probably create the pfp path but we'll check both just in case we're first
let avatar_cache_root = maxima_dir()?.join("cache/avatars");
let avatar_cache_root = maxima_cache_dir()?.join("avatars");
match variant {
UIImageType::Hero(slug) => Ok(image_cache_root.join(&slug).join("hero.jpg")),
UIImageType::Logo(slug) => Ok(image_cache_root.join(&slug).join("logo.png")),
Expand Down