Skip to content

支持通过环境变量设置 HMCL 配置文件夹#5987

Merged
Glavo merged 4 commits intoHMCL-dev:mainfrom
Glavo:env-dir
Apr 22, 2026
Merged

支持通过环境变量设置 HMCL 配置文件夹#5987
Glavo merged 4 commits intoHMCL-dev:mainfrom
Glavo:env-dir

Conversation

@Glavo
Copy link
Copy Markdown
Member

@Glavo Glavo commented Apr 21, 2026

#5981 依赖于本 PR。

另外我认为应该考虑重命名一下 HMCL_CURRENT_DIRECTORYHMCL_GLOBAL_DIRECTORY,最好在名字里就能表现出它们的功能用途。

@Glavo Glavo changed the title 支持通过环境变量配置 HMCL 支持通过环境变量设置 HMCL 配置文件夹 Apr 21, 2026
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

该 PR 旨在支持通过环境变量(并新增一个 JVM 参数)覆盖 HMCL 的配置/数据目录与依赖目录位置,以便在不同打包/运行环境下更灵活地指定路径(#5981 依赖此改动)。

Changes:

  • 允许使用环境变量作为 hmcl.home / hmcl.dir 的回退来源来设置 HMCL 全局/当前数据目录
  • 新增依赖目录可配置项:hmcl.dependencies.dirHMCL_DEPENDENCIES_DIR
  • Paths.get(...) 替换为 Path.of(...)

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +69 to +92
String hmclHome = System.getProperty("hmcl.home", System.getenv("HMCL_USER_HOME"));
if (StringUtils.isBlank(hmclHome)) {
if (OperatingSystem.CURRENT_OS.isLinuxOrBSD()) {
String xdgData = System.getenv("XDG_DATA_HOME");
if (StringUtils.isNotBlank(xdgData)) {
HMCL_GLOBAL_DIRECTORY = Paths.get(xdgData, "hmcl").toAbsolutePath().normalize();
HMCL_GLOBAL_DIRECTORY = Path.of(xdgData, "hmcl").toAbsolutePath().normalize();
} else {
HMCL_GLOBAL_DIRECTORY = Paths.get(System.getProperty("user.home"), ".local", "share", "hmcl").toAbsolutePath().normalize();
HMCL_GLOBAL_DIRECTORY = Path.of(System.getProperty("user.home"), ".local", "share", "hmcl").toAbsolutePath().normalize();
}
} else {
HMCL_GLOBAL_DIRECTORY = OperatingSystem.getWorkingDirectory("hmcl");
}
} else {
HMCL_GLOBAL_DIRECTORY = Paths.get(hmclHome).toAbsolutePath().normalize();
HMCL_GLOBAL_DIRECTORY = Path.of(hmclHome).toAbsolutePath().normalize();
}

String hmclCurrentDir = System.getProperty("hmcl.dir");
HMCL_CURRENT_DIRECTORY = hmclCurrentDir != null
? Paths.get(hmclCurrentDir).toAbsolutePath().normalize()
String hmclCurrentDir = System.getProperty("hmcl.dir", System.getenv("HMCL_LOCAL_HOME"));
HMCL_CURRENT_DIRECTORY = StringUtils.isNotBlank(hmclCurrentDir)
? Path.of(hmclCurrentDir).toAbsolutePath().normalize()
: CURRENT_DIRECTORY.resolve(".hmcl");
DEPENDENCIES_DIRECTORY = HMCL_CURRENT_DIRECTORY.resolve("dependencies");

String hmclDependencies = System.getProperty("hmcl.dependencies.dir", System.getenv("HMCL_DEPENDENCIES_DIR"));
DEPENDENCIES_DIRECTORY = StringUtils.isNotBlank(hmclDependencies)
? Path.of(hmclDependencies).toAbsolutePath().normalize()
Copy link

Copilot AI Apr 22, 2026

Choose a reason for hiding this comment

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

New environment variables (HMCL_USER_HOME / HMCL_LOCAL_HOME / HMCL_DEPENDENCIES_DIR) and the new JVM property (hmcl.dependencies.dir) are introduced here, but the debug-options docs currently only mention -Dhmcl.home and -Dhmcl.dir (no env var equivalents). Please update the docs (e.g., docs/Contributing*.md) so users know the new knobs and the precedence rules.

Copilot uses AI. Check for mistakes.
Comment on lines +69 to +86
String hmclHome = System.getProperty("hmcl.home", System.getenv("HMCL_USER_HOME"));
if (StringUtils.isBlank(hmclHome)) {
if (OperatingSystem.CURRENT_OS.isLinuxOrBSD()) {
String xdgData = System.getenv("XDG_DATA_HOME");
if (StringUtils.isNotBlank(xdgData)) {
HMCL_GLOBAL_DIRECTORY = Paths.get(xdgData, "hmcl").toAbsolutePath().normalize();
HMCL_GLOBAL_DIRECTORY = Path.of(xdgData, "hmcl").toAbsolutePath().normalize();
} else {
HMCL_GLOBAL_DIRECTORY = Paths.get(System.getProperty("user.home"), ".local", "share", "hmcl").toAbsolutePath().normalize();
HMCL_GLOBAL_DIRECTORY = Path.of(System.getProperty("user.home"), ".local", "share", "hmcl").toAbsolutePath().normalize();
}
} else {
HMCL_GLOBAL_DIRECTORY = OperatingSystem.getWorkingDirectory("hmcl");
}
} else {
HMCL_GLOBAL_DIRECTORY = Paths.get(hmclHome).toAbsolutePath().normalize();
HMCL_GLOBAL_DIRECTORY = Path.of(hmclHome).toAbsolutePath().normalize();
}

String hmclCurrentDir = System.getProperty("hmcl.dir");
HMCL_CURRENT_DIRECTORY = hmclCurrentDir != null
? Paths.get(hmclCurrentDir).toAbsolutePath().normalize()
String hmclCurrentDir = System.getProperty("hmcl.dir", System.getenv("HMCL_LOCAL_HOME"));
HMCL_CURRENT_DIRECTORY = StringUtils.isNotBlank(hmclCurrentDir)
Copy link

Copilot AI Apr 22, 2026

Choose a reason for hiding this comment

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

The env var naming for these directory overrides doesn’t follow the property→env mapping used elsewhere (e.g., hmcl.uiScale ↔ HMCL_UI_SCALE in EntryPoint.java:97 and hmcl.april_fools ↔ HMCL_APRIL_FOOLS in AprilFools.java:52). For consistency/predictability, consider supporting HMCL_HOME for hmcl.home and HMCL_DIR for hmcl.dir (optionally keeping the new names as aliases if you need them).

Copilot uses AI. Check for mistakes.
Comment on lines +82 to +93
HMCL_GLOBAL_DIRECTORY = Path.of(hmclHome).toAbsolutePath().normalize();
}

String hmclCurrentDir = System.getProperty("hmcl.dir");
HMCL_CURRENT_DIRECTORY = hmclCurrentDir != null
? Paths.get(hmclCurrentDir).toAbsolutePath().normalize()
String hmclCurrentDir = System.getProperty("hmcl.dir", System.getenv("HMCL_LOCAL_HOME"));
HMCL_CURRENT_DIRECTORY = StringUtils.isNotBlank(hmclCurrentDir)
? Path.of(hmclCurrentDir).toAbsolutePath().normalize()
: CURRENT_DIRECTORY.resolve(".hmcl");
DEPENDENCIES_DIRECTORY = HMCL_CURRENT_DIRECTORY.resolve("dependencies");

String hmclDependencies = System.getProperty("hmcl.dependencies.dir", System.getenv("HMCL_DEPENDENCIES_DIR"));
DEPENDENCIES_DIRECTORY = StringUtils.isNotBlank(hmclDependencies)
? Path.of(hmclDependencies).toAbsolutePath().normalize()
: HMCL_CURRENT_DIRECTORY.resolve("dependencies");
Copy link

Copilot AI Apr 22, 2026

Choose a reason for hiding this comment

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

Path.of(...) on values sourced from system properties/env vars can throw InvalidPathException during class initialization, which would crash the launcher very early. Since these inputs are user-controlled, it’d be safer to catch InvalidPathException (per-path), log a warning, and fall back to the default directory instead of failing class init.

Copilot uses AI. Check for mistakes.
@Glavo Glavo merged commit 3dcc7ab into HMCL-dev:main Apr 22, 2026
6 checks passed
@Glavo Glavo deleted the env-dir branch April 22, 2026 13:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants