支持通过环境变量设置 HMCL 配置文件夹#5987
Conversation
There was a problem hiding this comment.
Pull request overview
该 PR 旨在支持通过环境变量(并新增一个 JVM 参数)覆盖 HMCL 的配置/数据目录与依赖目录位置,以便在不同打包/运行环境下更灵活地指定路径(#5981 依赖此改动)。
Changes:
- 允许使用环境变量作为
hmcl.home/hmcl.dir的回退来源来设置 HMCL 全局/当前数据目录 - 新增依赖目录可配置项:
hmcl.dependencies.dir或HMCL_DEPENDENCIES_DIR - 将
Paths.get(...)替换为Path.of(...)
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| 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() |
There was a problem hiding this comment.
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.
| 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) |
There was a problem hiding this comment.
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).
| 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"); |
There was a problem hiding this comment.
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.
#5981 依赖于本 PR。
另外我认为应该考虑重命名一下
HMCL_CURRENT_DIRECTORY和HMCL_GLOBAL_DIRECTORY,最好在名字里就能表现出它们的功能用途。