清理 OperatingSystem (#3925)

This commit is contained in:
Glavo 2025-05-20 19:03:48 +08:00 committed by GitHub
parent 0900ad3e5a
commit c5f56eb5c4
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
9 changed files with 12 additions and 30 deletions

View File

@ -296,7 +296,7 @@ public final class Launcher extends Application {
Controllers.getStage().close(); Controllers.getStage().close();
Schedulers.shutdown(); Schedulers.shutdown();
Controllers.shutdown(); Controllers.shutdown();
Lang.executeDelayed(OperatingSystem::forceGC, TimeUnit.SECONDS, 5, true); Lang.executeDelayed(System::gc, TimeUnit.SECONDS, 5, true);
}); });
} }

View File

@ -41,6 +41,7 @@ import org.jackhuang.hmcl.util.versioning.GameVersionNumber;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
import java.io.BufferedReader; import java.io.BufferedReader;
import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.nio.file.*; import java.nio.file.*;
import java.util.*; import java.util.*;
@ -411,7 +412,7 @@ public final class JavaManager {
// Search in PATH. // Search in PATH.
if (System.getenv("PATH") != null) { if (System.getenv("PATH") != null) {
String[] paths = System.getenv("PATH").split(OperatingSystem.PATH_SEPARATOR); String[] paths = System.getenv("PATH").split(File.pathSeparator);
for (String path : paths) { for (String path : paths) {
try { try {
tryAddJavaExecutable(javaRuntimes, Paths.get(path, OperatingSystem.CURRENT_OS.getJavaExecutable())); tryAddJavaExecutable(javaRuntimes, Paths.get(path, OperatingSystem.CURRENT_OS.getJavaExecutable()));
@ -421,7 +422,7 @@ public final class JavaManager {
} }
if (System.getenv("HMCL_JRES") != null) { if (System.getenv("HMCL_JRES") != null) {
String[] paths = System.getenv("HMCL_JRES").split(OperatingSystem.PATH_SEPARATOR); String[] paths = System.getenv("HMCL_JRES").split(File.pathSeparator);
for (String path : paths) { for (String path : paths) {
try { try {
tryAddJavaHome(javaRuntimes, Paths.get(path)); tryAddJavaHome(javaRuntimes, Paths.get(path));

View File

@ -265,7 +265,7 @@ public class GameCrashWindow extends Stage {
Path logFile = Paths.get("minecraft-exported-crash-info-" + LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH-mm-ss")) + ".zip").toAbsolutePath(); Path logFile = Paths.get("minecraft-exported-crash-info-" + LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH-mm-ss")) + ".zip").toAbsolutePath();
CompletableFuture.supplyAsync(() -> CompletableFuture.supplyAsync(() ->
logs.stream().map(Log::getLog).collect(Collectors.joining(OperatingSystem.LINE_SEPARATOR))) logs.stream().map(Log::getLog).collect(Collectors.joining("\n")))
.thenComposeAsync(logs -> .thenComposeAsync(logs ->
LogExporter.exportLogs(logFile, repository, launchOptions.getVersionName(), logs, new CommandBuilder().addAll(managedProcess.getCommands()).toString())) LogExporter.exportLogs(logFile, repository, launchOptions.getVersionName(), logs, new CommandBuilder().addAll(managedProcess.getCommands()).toString()))
.handleAsync((result, exception) -> { .handleAsync((result, exception) -> {

View File

@ -41,7 +41,6 @@ import org.jackhuang.hmcl.util.io.CompressingUtils;
import org.jackhuang.hmcl.util.io.FileUtils; import org.jackhuang.hmcl.util.io.FileUtils;
import org.jackhuang.hmcl.util.platform.CommandBuilder; import org.jackhuang.hmcl.util.platform.CommandBuilder;
import org.jackhuang.hmcl.java.JavaRuntime; import org.jackhuang.hmcl.java.JavaRuntime;
import org.jackhuang.hmcl.util.platform.OperatingSystem;
import org.jackhuang.hmcl.util.platform.SystemUtils; import org.jackhuang.hmcl.util.platform.SystemUtils;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
@ -140,7 +139,7 @@ public class ForgeNewInstallTask extends Task<Version> {
classpath.add(file.toString()); classpath.add(file.toString());
} }
classpath.add(jar.toString()); classpath.add(jar.toString());
command.add(String.join(OperatingSystem.PATH_SEPARATOR, classpath)); command.add(String.join(File.pathSeparator, classpath));
command.add(mainClass); command.add(mainClass);

View File

@ -37,7 +37,6 @@ import org.jackhuang.hmcl.util.io.CompressingUtils;
import org.jackhuang.hmcl.util.io.FileUtils; import org.jackhuang.hmcl.util.io.FileUtils;
import org.jackhuang.hmcl.util.platform.CommandBuilder; import org.jackhuang.hmcl.util.platform.CommandBuilder;
import org.jackhuang.hmcl.java.JavaRuntime; import org.jackhuang.hmcl.java.JavaRuntime;
import org.jackhuang.hmcl.util.platform.OperatingSystem;
import org.jackhuang.hmcl.util.platform.SystemUtils; import org.jackhuang.hmcl.util.platform.SystemUtils;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
@ -136,7 +135,7 @@ public class NeoForgeOldInstallTask extends Task<Version> {
classpath.add(file.toString()); classpath.add(file.toString());
} }
classpath.add(jar.toString()); classpath.add(jar.toString());
command.add(String.join(OperatingSystem.PATH_SEPARATOR, classpath)); command.add(String.join(File.pathSeparator, classpath));
command.add(mainClass); command.add(mainClass);

View File

@ -255,7 +255,7 @@ public class DefaultLauncher extends Launcher {
// Provided Minecraft arguments // Provided Minecraft arguments
Path gameAssets = repository.getActualAssetDirectory(version.getId(), version.getAssetIndex().getId()); Path gameAssets = repository.getActualAssetDirectory(version.getId(), version.getAssetIndex().getId());
Map<String, String> configuration = getConfigurations(); Map<String, String> configuration = getConfigurations();
configuration.put("${classpath}", String.join(OperatingSystem.PATH_SEPARATOR, classpath)); configuration.put("${classpath}", String.join(File.pathSeparator, classpath));
configuration.put("${game_assets}", gameAssets.toAbsolutePath().toString()); configuration.put("${game_assets}", gameAssets.toAbsolutePath().toString());
configuration.put("${assets_root}", gameAssets.toAbsolutePath().toString()); configuration.put("${assets_root}", gameAssets.toAbsolutePath().toString());
@ -434,7 +434,7 @@ public class DefaultLauncher extends Launcher {
pair("${resolution_width}", options.getWidth().toString()), pair("${resolution_width}", options.getWidth().toString()),
pair("${resolution_height}", options.getHeight().toString()), pair("${resolution_height}", options.getHeight().toString()),
pair("${library_directory}", repository.getLibrariesDirectory(version).getAbsolutePath()), pair("${library_directory}", repository.getLibrariesDirectory(version).getAbsolutePath()),
pair("${classpath_separator}", OperatingSystem.PATH_SEPARATOR), pair("${classpath_separator}", File.pathSeparator),
pair("${primary_jar}", repository.getVersionJar(version).getAbsolutePath()), pair("${primary_jar}", repository.getVersionJar(version).getAbsolutePath()),
pair("${language}", Locale.getDefault().toString()), pair("${language}", Locale.getDefault().toString()),
@ -444,7 +444,7 @@ public class DefaultLauncher extends Launcher {
// when we propose this placeholder. // when we propose this placeholder.
pair("${libraries_directory}", repository.getLibrariesDirectory(version).getAbsolutePath()), pair("${libraries_directory}", repository.getLibrariesDirectory(version).getAbsolutePath()),
// file_separator is used in -DignoreList // file_separator is used in -DignoreList
pair("${file_separator}", OperatingSystem.FILE_SEPARATOR), pair("${file_separator}", File.separator),
pair("${primary_jar_name}", FileUtils.getName(repository.getVersionJar(version).toPath())) pair("${primary_jar_name}", FileUtils.getName(repository.getVersionJar(version).toPath()))
); );
} }

View File

@ -17,8 +17,6 @@
*/ */
package org.jackhuang.hmcl.util; package org.jackhuang.hmcl.util;
import org.jackhuang.hmcl.util.platform.OperatingSystem;
import java.io.PrintWriter; import java.io.PrintWriter;
import java.io.StringWriter; import java.io.StringWriter;
import java.util.*; import java.util.*;
@ -42,7 +40,7 @@ public final class StringUtils {
public static String getStackTrace(StackTraceElement[] elements) { public static String getStackTrace(StackTraceElement[] elements) {
StringBuilder builder = new StringBuilder(); StringBuilder builder = new StringBuilder();
for (StackTraceElement element : elements) for (StackTraceElement element : elements)
builder.append("\tat ").append(element).append(OperatingSystem.LINE_SEPARATOR); builder.append("\tat ").append(element).append(System.lineSeparator());
return builder.toString(); return builder.toString();
} }

View File

@ -22,7 +22,6 @@ import org.jackhuang.hmcl.util.platform.windows.Kernel32;
import org.jackhuang.hmcl.util.platform.windows.WinTypes; import org.jackhuang.hmcl.util.platform.windows.WinTypes;
import java.io.BufferedReader; import java.io.BufferedReader;
import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.io.InputStreamReader; import java.io.InputStreamReader;
import java.nio.charset.Charset; import java.nio.charset.Charset;
@ -85,10 +84,6 @@ public enum OperatingSystem {
*/ */
public static final OperatingSystem CURRENT_OS = parseOSName(System.getProperty("os.name")); public static final OperatingSystem CURRENT_OS = parseOSName(System.getProperty("os.name"));
public static final String PATH_SEPARATOR = File.pathSeparator;
public static final String FILE_SEPARATOR = File.separator;
public static final String LINE_SEPARATOR = System.lineSeparator();
/** /**
* The system default charset. * The system default charset.
*/ */
@ -296,16 +291,6 @@ public enum OperatingSystem {
return IS_WINDOWS_7_OR_LATER; return IS_WINDOWS_7_OR_LATER;
} }
@SuppressWarnings("removal")
public static void forceGC() {
System.gc();
try {
System.runFinalization();
System.gc();
} catch (NoSuchMethodError ignored) {
}
}
public static Path getWorkingDirectory(String folder) { public static Path getWorkingDirectory(String folder) {
String home = System.getProperty("user.home", "."); String home = System.getProperty("user.home", ".");
switch (OperatingSystem.CURRENT_OS) { switch (OperatingSystem.CURRENT_OS) {

View File

@ -47,7 +47,7 @@ public final class SystemUtils {
return null; return null;
try { try {
for (String item : path.split(OperatingSystem.PATH_SEPARATOR)) { for (String item : path.split(File.pathSeparator)) {
try { try {
Path program = Paths.get(item, command); Path program = Paths.get(item, command);
if (Files.isExecutable(program)) if (Files.isExecutable(program))