Skip to content

Commit 3b56842

Browse files
Merge 'origin/main'
2 parents 786ffbc + 97c1989 commit 3b56842

File tree

11 files changed

+120
-58
lines changed

11 files changed

+120
-58
lines changed

Cargo.lock

Lines changed: 49 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -145,9 +145,13 @@ cargo install ferium
145145
146146
### Program Configuration
147147

148-
Ferium stores profile and modpack information in its config file. By default, this is located at `~/.config/ferium/config.json`.
149-
You can change this in 2 ways, setting the `FERIUM_CONFIG_FILE` environment variable, or setting the `--config-file` global flag.
150-
The flag always takes precedence.
148+
Ferium stores profile and modpack information in its config file. By default, this is located at
149+
- `$XDG_CONFIG_HOME/ferium/config.json` or `~/.config/ferium/config.json` on Linux.
150+
- `%APPDATA%/ferium/config/config.json` on Windows.
151+
- `~/.config/ferium/config.json` on macOS.
152+
153+
You can change this in 2 ways; by setting the `FERIUM_CONFIG_FILE` environment variable,
154+
or the `--config-file` global flag. The flag takes precedence.
151155

152156
> [!CAUTION]
153157
> Be mindful of syntax when manually editing the config file

libium/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ serde = { version = "1.0", features = ["derive"] }
3232
url = { version = "2.5", features = ["serde"] }
3333
zip-extensions = "0.8"
3434
futures-util = "0.3"
35+
directories = "6.0"
3536
thiserror = "2.0"
3637
regex = "1.11"
3738
sha1 = "0.10"
38-
home = "0.5"
3939
zip = "2.5"

libium/src/config/mod.rs

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,11 @@
11
pub mod filters;
22
pub mod structs;
3-
43
use std::{
54
fs::{create_dir_all, File},
65
io::{BufReader, Result},
7-
path::{Path, PathBuf},
8-
sync::LazyLock,
6+
path::Path,
97
};
108

11-
pub static DEFAULT_CONFIG_PATH: LazyLock<PathBuf> = LazyLock::new(|| {
12-
crate::HOME
13-
.join(".config")
14-
.join("ferium")
15-
.join("config.json")
16-
});
17-
189
/// Open the config file at `path` and deserialise it into a config struct
1910
pub fn read_config(path: impl AsRef<Path>) -> Result<structs::Config> {
2011
if !path.as_ref().exists() {

libium/src/lib.rs

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ pub mod version_ext;
99
pub use add::add;
1010
pub use scan::scan;
1111

12+
use directories::{BaseDirs, ProjectDirs};
1213
use std::{path::PathBuf, sync::LazyLock};
1314

1415
pub static GITHUB_API: LazyLock<octocrab::Octocrab> = LazyLock::new(|| {
@@ -34,24 +35,27 @@ pub static MODRINTH_API: LazyLock<ferinth::Ferinth<()>> = LazyLock::new(|| {
3435
)
3536
});
3637

37-
pub static HOME: LazyLock<PathBuf> =
38-
LazyLock::new(|| home::home_dir().expect("Could not get user's home directory"));
38+
pub static BASE_DIRS: LazyLock<BaseDirs> =
39+
LazyLock::new(|| BaseDirs::new().expect("Could not get OS specific directories"));
40+
41+
pub static PROJECT_DIRS: LazyLock<ProjectDirs> = LazyLock::new(|| {
42+
ProjectDirs::from("", "", "ferium").expect("Could not get OS specific directories")
43+
});
3944

4045
/// Gets the default Minecraft instance directory based on the current compilation `target_os`
41-
///
42-
/// If the `target_os` doesn't match `"macos"`, `"linux"`, or `"windows"`, this function will not compile.
4346
pub fn get_minecraft_dir() -> PathBuf {
44-
#[cfg(target_os = "windows")]
45-
return HOME.join("AppData").join("Roaming").join(".minecraft");
46-
4747
#[cfg(target_os = "macos")]
48-
return HOME
49-
.join("Library")
50-
.join("Application Support")
51-
.join("minecraft");
52-
53-
#[cfg(target_os = "linux")]
54-
return HOME.join(".minecraft");
48+
{
49+
BASE_DIRS.data_dir().join("minecraft")
50+
}
51+
#[cfg(target_os = "windows")]
52+
{
53+
BASE_DIRS.data_dir().join(".minecraft")
54+
}
55+
#[cfg(not(any(target_os = "macos", target_os = "windows")))]
56+
{
57+
BASE_DIRS.home_dir().join(".minecraft")
58+
}
5559
}
5660

5761
/// Read `source` and return the data as a string

libium/src/upgrade/modpack_downloadable.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,19 @@
11
use super::{from_mr_version, try_from_cf_file, DistributionDeniedError};
2-
use crate::{config::structs::ModpackIdentifier, CURSEFORGE_API, HOME, MODRINTH_API};
2+
use crate::{config::structs::ModpackIdentifier, CURSEFORGE_API, MODRINTH_API, PROJECT_DIRS};
33
use reqwest::Client;
44
use std::{fs::create_dir_all, path::PathBuf};
55

66
#[derive(Debug, thiserror::Error)]
77
#[error(transparent)]
88
pub enum Error {
9-
/// The user can manually download the modpack zip file and place it in `~/.config/ferium/.cache/` to mitigate this.
9+
/// The user can manually download the modpack zip file and place it in
10+
/// `<ferium cache path>/downloaded` to mitigate this.
1011
/// However, they will have to manually update the modpack file.
12+
///
13+
/// Ferium cache path:
14+
/// - Windows: `%LOCALAPPDATA%/ferium/cache`
15+
/// - Linux: `${XDG_CACHE_HOME}/ferium` or `~/.cache/ferium`
16+
/// - MacOS: `~/Library/Caches/ferium`
1117
DistributionDenied(#[from] DistributionDeniedError),
1218
ModrinthError(#[from] ferinth::Error),
1319
CurseForgeError(#[from] furse::Error),
@@ -32,7 +38,7 @@ impl ModpackIdentifier {
3238
}
3339
};
3440

35-
let cache_dir = HOME.join(".config").join("ferium").join(".cache");
41+
let cache_dir = PROJECT_DIRS.cache_dir().join("downloaded");
3642
let modpack_path = cache_dir.join(&download_data.output);
3743
if !modpack_path.exists() {
3844
create_dir_all(&cache_dir)?;

src/file_picker.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use libium::HOME;
1+
use libium::BASE_DIRS;
22
use std::{
33
io::Result,
44
path::{Path, PathBuf},
@@ -38,7 +38,7 @@ pub fn pick_folder(
3838
.components()
3939
.map(|c| {
4040
if c.as_os_str() == "~" {
41-
HOME.as_os_str()
41+
BASE_DIRS.home_dir().as_os_str()
4242
} else {
4343
c.as_os_str()
4444
}

src/main.rs

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ use libium::{
3535
self,
3636
filters::ProfileParameters as _,
3737
structs::{Config, ModIdentifier, Modpack, Profile},
38-
DEFAULT_CONFIG_PATH,
3938
},
4039
iter_ext::IterExt as _,
4140
};
@@ -157,10 +156,31 @@ async fn actual_main(mut cli_app: Ferium) -> Result<()> {
157156

158157
let _ = SEMAPHORE.set(Semaphore::new(cli_app.parallel_tasks));
159158

159+
let old_default_config_path = libium::BASE_DIRS
160+
.home_dir()
161+
.join(".config")
162+
.join("ferium")
163+
.join("config.json");
160164
let config_path = &cli_app
161165
.config_file
162166
.or_else(|| var_os("FERIUM_CONFIG_FILE").map(Into::into))
163-
.unwrap_or(DEFAULT_CONFIG_PATH.clone());
167+
.unwrap_or({
168+
#[cfg(target_os = "macos")]
169+
{
170+
old_default_config_path
171+
}
172+
#[cfg(not(target_os = "macos"))]
173+
{
174+
libium::PROJECT_DIRS.config_dir().join("config.json")
175+
}
176+
});
177+
178+
// Handle old configs which may be in a different path
179+
if !config_path.exists() && old_default_config_path.exists() {
180+
std::fs::rename(old_default_config_path, config_path)
181+
.context("Failed to relocate config file to the new path, try doing so manually.")?;
182+
}
183+
164184
let mut config = config::read_config(config_path)?;
165185

166186
let mut did_add_fail = false;

src/subcommands/modpack/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use crate::file_picker::pick_folder;
1414
use anyhow::{ensure, Context as _, Result};
1515
use fs_extra::dir::{copy, CopyOptions};
1616
use inquire::Confirm;
17-
use libium::HOME;
17+
use libium::BASE_DIRS;
1818
use std::{fs::read_dir, path::Path};
1919

2020
pub fn check_output_directory(output_dir: &Path) -> Result<()> {
@@ -44,7 +44,7 @@ pub fn check_output_directory(output_dir: &Path) -> Result<()> {
4444
.unwrap_or_default()
4545
{
4646
let backup_dir = pick_folder(
47-
&*HOME,
47+
BASE_DIRS.home_dir(),
4848
"Where should the backup be made?",
4949
"Output Directory",
5050
)?

src/subcommands/modpack/upgrade.rs

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use libium::{
1313
read_file_from_zip, zip_extract,
1414
},
1515
upgrade::{from_modpack_file, try_from_cf_file, DistributionDeniedError, DownloadData},
16-
CURSEFORGE_API, HOME,
16+
CURSEFORGE_API,
1717
};
1818
use std::{
1919
fs::File,
@@ -113,10 +113,9 @@ pub async fn upgrade(modpack: &'_ Modpack) -> Result<()> {
113113
);
114114

115115
if modpack.install_overrides {
116-
let tmp_dir = HOME
117-
.join(".config")
118-
.join("ferium")
119-
.join(".tmp")
116+
let tmp_dir = libium::PROJECT_DIRS
117+
.cache_dir()
118+
.join("extracted")
120119
.join(manifest.name);
121120
zip_extract(&modpack_filepath, &tmp_dir)?;
122121
to_install = read_overrides(&tmp_dir.join(manifest.overrides))?;
@@ -142,10 +141,9 @@ pub async fn upgrade(modpack: &'_ Modpack) -> Result<()> {
142141
);
143142

144143
if modpack.install_overrides {
145-
let tmp_dir = HOME
146-
.join(".config")
147-
.join("ferium")
148-
.join(".tmp")
144+
let tmp_dir = libium::PROJECT_DIRS
145+
.cache_dir()
146+
.join("extracted")
149147
.join(metadata.name);
150148
zip_extract(&modpack_filepath, &tmp_dir)?;
151149
to_install = read_overrides(&tmp_dir.join("overrides"))?;

0 commit comments

Comments
 (0)