在 Linux/FreeBSD 平台读取 os-release (#3314)

* 简化 OperatingSystem.getPhysicalMemoryStatus

* Read os-release

* update

* update
This commit is contained in:
Glavo 2024-10-07 00:57:35 +08:00 committed by GitHub
parent 26224ae366
commit 4c1e607b8e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 39 additions and 19 deletions

View File

@ -222,7 +222,9 @@ public final class Launcher extends Application {
try {
LOG.info("*** " + Metadata.TITLE + " ***");
LOG.info("Operating System: " + OperatingSystem.SYSTEM_NAME + ' ' + OperatingSystem.SYSTEM_VERSION);
LOG.info("Operating System: " + (OperatingSystem.OS_RELEASE_PRETTY_NAME == null
? OperatingSystem.SYSTEM_NAME + ' ' + OperatingSystem.SYSTEM_VERSION
: OperatingSystem.OS_RELEASE_PRETTY_NAME + " (" + OperatingSystem.SYSTEM_NAME + ' ' + OperatingSystem.SYSTEM_VERSION + ')'));
LOG.info("System Architecture: " + Architecture.SYSTEM_ARCH_NAME);
LOG.info("Java Architecture: " + Architecture.CURRENT_ARCH_NAME);
LOG.info("Java Version: " + System.getProperty("java.version") + ", " + System.getProperty("java.vendor"));

View File

@ -396,7 +396,7 @@ public class HMCLGameRepository extends DefaultGameRepository {
.setOverrideJavaArguments(StringUtils.tokenize(vs.getJavaArgs()))
.setMaxMemory(vs.isNoJVMArgs() && vs.isAutoMemory() ? null : (int)(getAllocatedMemory(
vs.getMaxMemory() * 1024L * 1024L,
OperatingSystem.getPhysicalMemoryStatus().orElse(OperatingSystem.PhysicalMemoryStatus.INVALID).getAvailable(),
OperatingSystem.getPhysicalMemoryStatus().getAvailable(),
vs.isAutoMemory()
) / 1024 / 1024))
.setMinMemory(vs.getMinMemory())

View File

@ -20,8 +20,6 @@ package org.jackhuang.hmcl.ui;
import com.jfoenix.controls.JFXButton;
import javafx.beans.property.BooleanProperty;
import javafx.beans.property.SimpleBooleanProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Node;
@ -43,6 +41,7 @@ import org.jackhuang.hmcl.setting.Theme;
import org.jackhuang.hmcl.task.Schedulers;
import org.jackhuang.hmcl.task.Task;
import org.jackhuang.hmcl.ui.construct.TwoLineListItem;
import org.jackhuang.hmcl.util.Lang;
import org.jackhuang.hmcl.util.Log4jLevel;
import org.jackhuang.hmcl.util.logging.Logger;
import org.jackhuang.hmcl.util.Pair;
@ -77,8 +76,6 @@ public class GameCrashWindow extends Stage {
private final String total_memory;
private final String java;
private final LibraryAnalyzer analyzer;
private final StringProperty os = new SimpleStringProperty(OperatingSystem.SYSTEM_NAME);
private final StringProperty arch = new SimpleStringProperty(Architecture.SYSTEM_ARCH.getDisplayName());
private final TextFlow reasonTextFlow = new TextFlow(new Text(i18n("game.crash.reason.unknown")));
private final BooleanProperty loading = new SimpleBooleanProperty();
private final TextFlow feedbackTextFlow = new TextFlow();
@ -356,12 +353,12 @@ public class GameCrashWindow extends Stage {
TwoLineListItem os = new TwoLineListItem();
os.getStyleClass().setAll("two-line-item-second-large");
os.setTitle(i18n("system.operating_system"));
os.subtitleProperty().bind(GameCrashWindow.this.os);
os.setSubtitle(Lang.requireNonNullElse(OperatingSystem.OS_RELEASE_NAME, OperatingSystem.SYSTEM_NAME));
TwoLineListItem arch = new TwoLineListItem();
arch.getStyleClass().setAll("two-line-item-second-large");
arch.setTitle(i18n("system.architecture"));
arch.subtitleProperty().bind(GameCrashWindow.this.arch);
arch.setSubtitle(Architecture.SYSTEM_ARCH.getDisplayName());
infoPane.getChildren().setAll(launcher, version, total_memory, memory, java, os, arch);
}

View File

@ -457,7 +457,7 @@ public final class VersionSettingsPage extends StackPane implements DecoratorPag
}
private void initialize() {
memoryStatus.set(OperatingSystem.getPhysicalMemoryStatus().orElse(OperatingSystem.PhysicalMemoryStatus.INVALID));
memoryStatus.set(OperatingSystem.getPhysicalMemoryStatus());
enableSpecificSettings.addListener((a, b, newValue) -> {
if (versionId == null) return;

View File

@ -17,6 +17,8 @@
*/
package org.jackhuang.hmcl.util.platform;
import org.jackhuang.hmcl.util.KeyValuePairProperties;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
@ -28,8 +30,9 @@ import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.Collections;
import java.util.Locale;
import java.util.Optional;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
@ -118,6 +121,9 @@ public enum OperatingSystem {
*/
public static final String SYSTEM_VERSION;
public static final String OS_RELEASE_NAME;
public static final String OS_RELEASE_PRETTY_NAME;
public static final Pattern INVALID_RESOURCE_CHARACTERS;
private static final String[] INVALID_RESOURCE_BASENAMES;
private static final String[] INVALID_RESOURCE_FULLNAMES;
@ -144,7 +150,7 @@ public enum OperatingSystem {
}
}
} catch (UnsupportedCharsetException e) {
e.printStackTrace();
e.printStackTrace(System.err);
}
NATIVE_CHARSET = nativeCharset;
@ -187,9 +193,24 @@ public enum OperatingSystem {
SYSTEM_BUILD_NUMBER = -1;
}
TOTAL_MEMORY = getPhysicalMemoryStatus()
.map(physicalMemoryStatus -> (int) (physicalMemoryStatus.getTotal() / 1024 / 1024))
.orElse(1024);
Map<String, String> osRelease = Collections.emptyMap();
if (CURRENT_OS == LINUX || CURRENT_OS == FREEBSD) {
Path osReleaseFile = Paths.get("/etc/os-release");
if (Files.exists(osReleaseFile)) {
try {
osRelease = KeyValuePairProperties.load(osReleaseFile);
} catch (IOException e) {
e.printStackTrace(System.err);
}
}
}
OS_RELEASE_NAME = osRelease.get("NAME");
OS_RELEASE_PRETTY_NAME = osRelease.get("PRETTY_NAME");
PhysicalMemoryStatus physicalMemoryStatus = getPhysicalMemoryStatus();
TOTAL_MEMORY = physicalMemoryStatus != PhysicalMemoryStatus.INVALID
? (int) (physicalMemoryStatus.getTotal() / 1024 / 1024)
: 1024;
SUGGESTED_MEMORY = TOTAL_MEMORY >= 32768 ? 8192 : (int) (Math.round(1.0 * TOTAL_MEMORY / 4.0 / 128.0) * 128);
@ -256,7 +277,7 @@ public enum OperatingSystem {
}
@SuppressWarnings("deprecation")
public static Optional<PhysicalMemoryStatus> getPhysicalMemoryStatus() {
public static PhysicalMemoryStatus getPhysicalMemoryStatus() {
if (CURRENT_OS == LINUX) {
try {
long free = 0, available = 0, total = 0;
@ -277,10 +298,10 @@ public enum OperatingSystem {
}
}
if (total > 0) {
return Optional.of(new PhysicalMemoryStatus(total, available > 0 ? available : free));
return new PhysicalMemoryStatus(total, available > 0 ? available : free);
}
} catch (IOException e) {
e.printStackTrace();
e.printStackTrace(System.err);
}
}
@ -290,11 +311,11 @@ public enum OperatingSystem {
com.sun.management.OperatingSystemMXBean sunBean =
(com.sun.management.OperatingSystemMXBean)
java.lang.management.ManagementFactory.getOperatingSystemMXBean();
return Optional.of(new PhysicalMemoryStatus(sunBean.getTotalPhysicalMemorySize(), sunBean.getFreePhysicalMemorySize()));
return new PhysicalMemoryStatus(sunBean.getTotalPhysicalMemorySize(), sunBean.getFreePhysicalMemorySize());
}
} catch (NoClassDefFoundError ignored) {
}
return Optional.empty();
return PhysicalMemoryStatus.INVALID;
}
@SuppressWarnings("removal")