Skip to content

Commit 9a37d41

Browse files
authored
使用工作路径下的 .hmcl 文件夹存放所有配置文件 (#3875)
1 parent dc5ebcf commit 9a37d41

16 files changed

Lines changed: 94 additions & 57 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,3 +52,4 @@ minecraft-exported-crash-info*
5252
# test
5353
/hmcl.json
5454
/.hmcl.json
55+
/.hmcl/

HMCL/src/main/java/org/jackhuang/hmcl/Launcher.java

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ public void start(Stage primaryStage) {
100100
return;
101101
}
102102

103-
if (Metadata.HMCL_DIRECTORY.toString().indexOf('=') >= 0) {
103+
if (Metadata.HMCL_CURRENT_DIRECTORY.toString().indexOf('=') >= 0) {
104104
Main.showWarningAndContinue(i18n("fatal.illegal_char"));
105105
}
106106

@@ -186,8 +186,10 @@ private static void checkConfigOwner() {
186186

187187
ArrayList<String> files = new ArrayList<>();
188188
files.add(ConfigHolder.configLocation().toString());
189-
if (Files.exists(Metadata.HMCL_DIRECTORY))
190-
files.add(Metadata.HMCL_DIRECTORY.toString());
189+
if (Files.exists(Metadata.HMCL_GLOBAL_DIRECTORY))
190+
files.add(Metadata.HMCL_GLOBAL_DIRECTORY.toString());
191+
if (Files.exists(Metadata.HMCL_CURRENT_DIRECTORY))
192+
files.add(Metadata.HMCL_CURRENT_DIRECTORY.toString());
191193

192194
Path mcDir = Paths.get(".minecraft").toAbsolutePath().normalize();
193195
if (Files.exists(mcDir))
@@ -238,8 +240,9 @@ public static void main(String[] args) {
238240
LOG.info("Java Version: " + System.getProperty("java.version") + ", " + System.getProperty("java.vendor"));
239241
LOG.info("Java VM Version: " + System.getProperty("java.vm.name") + " (" + System.getProperty("java.vm.info") + "), " + System.getProperty("java.vm.vendor"));
240242
LOG.info("Java Home: " + System.getProperty("java.home"));
241-
LOG.info("Current Directory: " + System.getProperty("user.dir"));
242-
LOG.info("HMCL Directory: " + Metadata.HMCL_DIRECTORY);
243+
LOG.info("Current Directory: " + Metadata.CURRENT_DIRECTORY);
244+
LOG.info("HMCL Global Directory: " + Metadata.HMCL_GLOBAL_DIRECTORY);
245+
LOG.info("HMCL Current Directory: " + Metadata.HMCL_CURRENT_DIRECTORY);
243246
LOG.info("HMCL Jar Path: " + Lang.requireNonNullElse(JarUtils.thisJarPath(), "Not Found"));
244247
LOG.info("HMCL Log File: " + Lang.requireNonNullElse(LOG.getLogFile(), "In Memory"));
245248
LOG.info("Memory: " + Runtime.getRuntime().maxMemory() / 1024 / 1024 + "MB");

HMCL/src/main/java/org/jackhuang/hmcl/Main.java

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,8 @@ public static void main(String[] args) {
5858
System.getProperties().putIfAbsent("javafx.autoproxy.disable", "true");
5959
System.getProperties().putIfAbsent("http.agent", "HMCL/" + Metadata.VERSION);
6060

61-
LOG.start(Metadata.HMCL_DIRECTORY.resolve("logs"));
61+
createHMCLCurrentDirectory();
62+
LOG.start(Metadata.HMCL_CURRENT_DIRECTORY.resolve("logs"));
6263

6364
checkDirectoryPath();
6465

@@ -82,6 +83,24 @@ public static void exit(int exitCode) {
8283
System.exit(exitCode);
8384
}
8485

86+
private static void createHMCLCurrentDirectory() {
87+
if (!Files.isDirectory(Metadata.HMCL_CURRENT_DIRECTORY)) {
88+
try {
89+
Files.createDirectories(Metadata.HMCL_CURRENT_DIRECTORY);
90+
if (OperatingSystem.CURRENT_OS == OperatingSystem.WINDOWS) {
91+
try {
92+
Files.setAttribute(Metadata.HMCL_CURRENT_DIRECTORY, "dos:hidden", true);
93+
} catch (IOException e) {
94+
LOG.warning("Failed to set hidden attribute of " + Metadata.HMCL_CURRENT_DIRECTORY, e);
95+
}
96+
}
97+
} catch (IOException e) {
98+
e.printStackTrace(System.err);
99+
showErrorAndExit(i18n("fatal.create_hmcl_current_directory_failure", Metadata.HMCL_CURRENT_DIRECTORY));
100+
}
101+
}
102+
}
103+
85104
private static void initIcon() {
86105
java.awt.Image image = java.awt.Toolkit.getDefaultToolkit().getImage(Main.class.getResource("/assets/img/icon-mac.png"));
87106
AwtUtils.setAppleIcon(image);

HMCL/src/main/java/org/jackhuang/hmcl/Metadata.java

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@
2828
* Stores metadata about this application.
2929
*/
3030
public final class Metadata {
31-
private Metadata() {}
31+
private Metadata() {
32+
}
3233

3334
public static final String NAME = "HMCL";
3435
public static final String FULL_NAME = "Hello Minecraft! Launcher";
@@ -51,25 +52,34 @@ private Metadata() {}
5152
public static final String BUILD_CHANNEL = JarUtils.getManifestAttribute("Build-Channel", "nightly");
5253
public static final String GITHUB_SHA = JarUtils.getManifestAttribute("GitHub-SHA", null);
5354

55+
public static final Path CURRENT_DIRECTORY = Paths.get(System.getProperty("user.dir")).toAbsolutePath().normalize();
5456
public static final Path MINECRAFT_DIRECTORY = OperatingSystem.getWorkingDirectory("minecraft");
55-
public static final Path HMCL_DIRECTORY;
57+
public static final Path HMCL_GLOBAL_DIRECTORY;
58+
public static final Path HMCL_CURRENT_DIRECTORY;
59+
public static final Path DEPENDENCIES_DIRECTORY;
5660

5761
static {
5862
String hmclHome = System.getProperty("hmcl.home");
5963
if (hmclHome == null) {
6064
if (OperatingSystem.CURRENT_OS.isLinuxOrBSD()) {
6165
String xdgData = System.getenv("XDG_DATA_HOME");
6266
if (StringUtils.isNotBlank(xdgData)) {
63-
HMCL_DIRECTORY = Paths.get(xdgData, "hmcl").toAbsolutePath();
67+
HMCL_GLOBAL_DIRECTORY = Paths.get(xdgData, "hmcl").toAbsolutePath().normalize();
6468
} else {
65-
HMCL_DIRECTORY = Paths.get(System.getProperty("user.home", "."), ".local", "share", "hmcl").toAbsolutePath();
69+
HMCL_GLOBAL_DIRECTORY = Paths.get(System.getProperty("user.home"), ".local", "share", "hmcl").toAbsolutePath().normalize();
6670
}
6771
} else {
68-
HMCL_DIRECTORY = OperatingSystem.getWorkingDirectory("hmcl");
72+
HMCL_GLOBAL_DIRECTORY = OperatingSystem.getWorkingDirectory("hmcl");
6973
}
7074
} else {
71-
HMCL_DIRECTORY = Paths.get(hmclHome).toAbsolutePath().normalize();
75+
HMCL_GLOBAL_DIRECTORY = Paths.get(hmclHome).toAbsolutePath().normalize();
7276
}
77+
78+
String hmclCurrentDir = System.getProperty("hmcl.dir");
79+
HMCL_CURRENT_DIRECTORY = hmclCurrentDir != null
80+
? Paths.get(hmclCurrentDir).toAbsolutePath().normalize()
81+
: CURRENT_DIRECTORY.resolve(".hmcl");
82+
DEPENDENCIES_DIRECTORY = HMCL_CURRENT_DIRECTORY.resolve("dependencies");
7383
}
7484

7585
public static boolean isStable() {

HMCL/src/main/java/org/jackhuang/hmcl/game/TexturesLoader.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ public Map<String, String> getMetadata() {
8787
}
8888

8989
private static final ThreadPoolExecutor POOL = threadPool("TexturesDownload", true, 2, 10, TimeUnit.SECONDS);
90-
private static final Path TEXTURES_DIR = Metadata.HMCL_DIRECTORY.resolve("skins");
90+
private static final Path TEXTURES_DIR = Metadata.HMCL_GLOBAL_DIRECTORY.resolve("skins");
9191

9292
private static Path getTexturePath(Texture texture) {
9393
String url = texture.getUrl();

HMCL/src/main/java/org/jackhuang/hmcl/java/JavaManager.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public final class JavaManager {
5858
private JavaManager() {
5959
}
6060

61-
public static final HMCLJavaRepository REPOSITORY = new HMCLJavaRepository(Metadata.HMCL_DIRECTORY.resolve("java"));
61+
public static final HMCLJavaRepository REPOSITORY = new HMCLJavaRepository(Metadata.HMCL_GLOBAL_DIRECTORY.resolve("java"));
6262

6363
public static String getMojangJavaPlatform(Platform platform) {
6464
if (platform.getOperatingSystem() == OperatingSystem.WINDOWS) {

HMCL/src/main/java/org/jackhuang/hmcl/setting/Accounts.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ private static void updateAccountStorages() {
175175
}
176176

177177
private static void loadGlobalAccountStorages() {
178-
Path globalAccountsFile = Metadata.HMCL_DIRECTORY.resolve("accounts.json");
178+
Path globalAccountsFile = Metadata.HMCL_GLOBAL_DIRECTORY.resolve("accounts.json");
179179
if (Files.exists(globalAccountsFile)) {
180180
try (Reader reader = Files.newBufferedReader(globalAccountsFile)) {
181181
globalAccountStorages.setAll(Config.CONFIG_GSON.fromJson(reader, listTypeOf(mapTypeOf(Object.class, Object.class))));
@@ -388,7 +388,7 @@ private static AuthlibInjectorArtifactProvider createAuthlibInjectorArtifactProv
388388
String authlibinjectorLocation = System.getProperty("hmcl.authlibinjector.location");
389389
if (authlibinjectorLocation == null) {
390390
return new AuthlibInjectorDownloader(
391-
Metadata.HMCL_DIRECTORY.resolve("authlib-injector.jar"),
391+
Metadata.DEPENDENCIES_DIRECTORY.resolve("authlib-injector.jar"),
392392
DownloadProviders::getDownloadProvider) {
393393
@Override
394394
public Optional<AuthlibInjectorArtifactInfo> getArtifactInfoImmediately() {
@@ -397,7 +397,7 @@ public Optional<AuthlibInjectorArtifactInfo> getArtifactInfoImmediately() {
397397
return local;
398398
}
399399
// search authlib-injector.jar in current directory, it's used as a fallback
400-
return parseArtifact(Paths.get("authlib-injector.jar"));
400+
return parseArtifact(Metadata.CURRENT_DIRECTORY.resolve("authlib-injector.jar"));
401401
}
402402
};
403403
} else {

HMCL/src/main/java/org/jackhuang/hmcl/setting/ConfigHolder.java

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ private ConfigHolder() {
3939

4040
public static final String CONFIG_FILENAME = "hmcl.json";
4141
public static final String CONFIG_FILENAME_LINUX = ".hmcl.json";
42-
public static final Path GLOBAL_CONFIG_PATH = Metadata.HMCL_DIRECTORY.resolve("config.json");
42+
public static final Path GLOBAL_CONFIG_PATH = Metadata.HMCL_GLOBAL_DIRECTORY.resolve("config.json");
4343

4444
private static Path configLocation;
4545
private static Config configInstance;
@@ -93,19 +93,9 @@ public static void init() throws IOException {
9393
LOG.setLogRetention(globalConfig().getLogRetention());
9494
Settings.init();
9595

96-
if (newlyCreated) {
96+
if (newlyCreated)
9797
saveConfigSync();
9898

99-
// hide the config file on windows
100-
if (OperatingSystem.CURRENT_OS == OperatingSystem.WINDOWS) {
101-
try {
102-
Files.setAttribute(configLocation, "dos:hidden", true);
103-
} catch (IOException e) {
104-
LOG.warning("Failed to set hidden attribute of " + configLocation, e);
105-
}
106-
}
107-
}
108-
10999
if (!Files.isWritable(configLocation)) {
110100
if (OperatingSystem.CURRENT_OS == OperatingSystem.WINDOWS
111101
&& configLocation.getFileSystem() == FileSystems.getDefault()
@@ -121,12 +111,14 @@ public static void init() throws IOException {
121111
}
122112

123113
private static Path locateConfig() {
124-
Path exePath = Paths.get("").toAbsolutePath();
114+
Path defaultConfigFile = Metadata.HMCL_CURRENT_DIRECTORY.resolve(CONFIG_FILENAME);
115+
if (Files.isRegularFile(defaultConfigFile))
116+
return defaultConfigFile;
117+
125118
try {
126119
Path jarPath = JarUtils.thisJarPath();
127120
if (jarPath != null && Files.isRegularFile(jarPath) && Files.isWritable(jarPath)) {
128121
jarPath = jarPath.getParent();
129-
exePath = jarPath;
130122

131123
Path config = jarPath.resolve(CONFIG_FILENAME);
132124
if (Files.isRegularFile(config))
@@ -149,7 +141,7 @@ private static Path locateConfig() {
149141
return dotConfig;
150142

151143
// create new
152-
return exePath.resolve(OperatingSystem.CURRENT_OS == OperatingSystem.WINDOWS ? CONFIG_FILENAME : CONFIG_FILENAME_LINUX);
144+
return defaultConfigFile;
153145
}
154146

155147
private static Config loadConfig() throws IOException {

HMCL/src/main/java/org/jackhuang/hmcl/setting/Theme.java

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@
3737
import java.nio.charset.StandardCharsets;
3838
import java.nio.file.Files;
3939
import java.nio.file.Path;
40-
import java.nio.file.Paths;
4140
import java.util.Base64;
4241
import java.util.Locale;
4342
import java.util.Objects;
@@ -59,11 +58,11 @@ public class Theme {
5958
Color.web("#B71C1C") // red
6059
};
6160

62-
private static final Lazy<Font> FONT = new Lazy<>(() -> {
61+
private static Font tryLoadDefaultFont(Path dir) {
6362
String[] fileNames = {"font.ttf", "font.otf", "font.woff"};
6463

6564
for (String fileName : fileNames) {
66-
Path path = Paths.get(fileName).toAbsolutePath();
65+
Path path = dir.resolve(fileName);
6766
if (Files.isRegularFile(path)) {
6867
try {
6968
Font font = Font.loadFont(path.toUri().toURL().toExternalForm(), 0);
@@ -77,22 +76,19 @@ public class Theme {
7776
}
7877
}
7978

80-
for (String fileName : fileNames) {
81-
Path path = Metadata.HMCL_DIRECTORY.resolve(fileName);
82-
if (Files.isRegularFile(path)) {
83-
try {
84-
Font font = Font.loadFont(path.toUri().toURL().toExternalForm(), 0);
85-
if (font != null) {
86-
return font;
87-
}
88-
} catch (MalformedURLException ignored) {
89-
}
79+
return null;
80+
}
9081

91-
LOG.warning("Failed to load font " + path);
92-
}
93-
}
82+
private static final Lazy<Font> FONT = new Lazy<>(() -> {
83+
Font font = tryLoadDefaultFont(Metadata.HMCL_CURRENT_DIRECTORY);
84+
if (font != null)
85+
return font;
9486

95-
return null;
87+
font = tryLoadDefaultFont(Metadata.CURRENT_DIRECTORY);
88+
if (font != null)
89+
return font;
90+
91+
return tryLoadDefaultFont(Metadata.HMCL_GLOBAL_DIRECTORY);
9692
});
9793

9894
public static Theme getTheme() {

HMCL/src/main/java/org/jackhuang/hmcl/ui/decorator/DecoratorController.java

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
import javafx.stage.Stage;
4141
import javafx.util.Duration;
4242
import org.jackhuang.hmcl.Launcher;
43+
import org.jackhuang.hmcl.Metadata;
4344
import org.jackhuang.hmcl.auth.authlibinjector.AuthlibInjectorDnD;
4445
import org.jackhuang.hmcl.setting.EnumBackgroundImage;
4546
import org.jackhuang.hmcl.task.Schedulers;
@@ -235,12 +236,22 @@ private Background getBackground() {
235236
* Load background image from bg/, background.png, background.jpg, background.gif
236237
*/
237238
private Image loadDefaultBackgroundImage() {
238-
Image image = randomImageIn(Paths.get("bg"));
239+
Image image = randomImageIn(Metadata.HMCL_CURRENT_DIRECTORY.resolve("background"));
239240
if (image != null)
240241
return image;
241242

242243
for (String extension : FXUtils.IMAGE_EXTENSIONS) {
243-
image = tryLoadImage(Paths.get("background." + extension));
244+
image = tryLoadImage(Metadata.HMCL_CURRENT_DIRECTORY.resolve("background." + extension));
245+
if (image != null)
246+
return image;
247+
}
248+
249+
image = randomImageIn(Metadata.CURRENT_DIRECTORY.resolve("bg"));
250+
if (image != null)
251+
return image;
252+
253+
for (String extension : FXUtils.IMAGE_EXTENSIONS) {
254+
image = tryLoadImage(Metadata.CURRENT_DIRECTORY.resolve("background." + extension));
244255
if (image != null)
245256
return image;
246257
}

0 commit comments

Comments
 (0)