From ac61a86ce7760077dca8d9789c2d1308c82582e3 Mon Sep 17 00:00:00 2001 From: cubewhy Date: Sat, 6 Jan 2024 11:43:21 +0800 Subject: [PATCH] remove OperatingSystem.java --- .../celestial/utils}/ManagedProcess.java | 8 +- .../cubewhy/celestial/utils/SystemUtils.java | 5 - .../hmcl/util/platform/OperatingSystem.java | 367 ------------------ 3 files changed, 7 insertions(+), 373 deletions(-) rename src/main/java/org/{jackhuang/hmcl/util/platform => cubewhy/celestial/utils}/ManagedProcess.java (96%) delete mode 100644 src/main/java/org/jackhuang/hmcl/util/platform/OperatingSystem.java diff --git a/src/main/java/org/jackhuang/hmcl/util/platform/ManagedProcess.java b/src/main/java/org/cubewhy/celestial/utils/ManagedProcess.java similarity index 96% rename from src/main/java/org/jackhuang/hmcl/util/platform/ManagedProcess.java rename to src/main/java/org/cubewhy/celestial/utils/ManagedProcess.java index 4cfd6b88..c2ba5ead 100644 --- a/src/main/java/org/jackhuang/hmcl/util/platform/ManagedProcess.java +++ b/src/main/java/org/cubewhy/celestial/utils/ManagedProcess.java @@ -1,5 +1,11 @@ -package org.jackhuang.hmcl.util.platform; +/* + * Celestial Launcher + * License under GPLv3 + * Do NOT remove this note if you want to copy this file. + */ + +package org.cubewhy.celestial.utils; import lombok.Getter; diff --git a/src/main/java/org/cubewhy/celestial/utils/SystemUtils.java b/src/main/java/org/cubewhy/celestial/utils/SystemUtils.java index 12e0627c..bb28f7e2 100644 --- a/src/main/java/org/cubewhy/celestial/utils/SystemUtils.java +++ b/src/main/java/org/cubewhy/celestial/utils/SystemUtils.java @@ -10,14 +10,9 @@ import com.sun.tools.attach.VirtualMachine; import com.sun.tools.attach.VirtualMachineDescriptor; import lombok.extern.slf4j.Slf4j; -import org.cubewhy.celestial.Celestial; -import org.cubewhy.celestial.utils.lunar.LauncherData; -import org.jackhuang.hmcl.util.platform.ManagedProcess; import org.jetbrains.annotations.Nullable; import java.io.IOException; -import java.util.Arrays; -import java.util.List; // from hmcl launcher @Slf4j diff --git a/src/main/java/org/jackhuang/hmcl/util/platform/OperatingSystem.java b/src/main/java/org/jackhuang/hmcl/util/platform/OperatingSystem.java deleted file mode 100644 index 8990840b..00000000 --- a/src/main/java/org/jackhuang/hmcl/util/platform/OperatingSystem.java +++ /dev/null @@ -1,367 +0,0 @@ -/* - * Hello Minecraft! Launcher - * Copyright (C) 2021 huangyuhui and contributors - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ -package org.jackhuang.hmcl.util.platform; - -import java.io.BufferedReader; -import java.io.File; -import java.io.IOException; -import java.io.InputStreamReader; -import java.nio.charset.Charset; -import java.nio.charset.StandardCharsets; -import java.nio.charset.UnsupportedCharsetException; -import java.nio.file.Files; -import java.nio.file.Path; -import java.nio.file.Paths; -import java.util.Arrays; -import java.util.Locale; -import java.util.Optional; -import java.util.regex.Matcher; -import java.util.regex.Pattern; - -/** - * Represents the operating system. - * - * @author huangyuhui - */ -public enum OperatingSystem { - /** - * Microsoft Windows. - */ - WINDOWS("windows"), - /** - * Linux and Unix like OS, including Solaris. - */ - LINUX("linux"), - /** - * Mac OS X. - */ - OSX("osx"), - /** - * Unknown operating system. - */ - UNKNOWN("universal"); - - private final String checkedName; - - OperatingSystem(String checkedName) { - this.checkedName = checkedName; - } - - public String getCheckedName() { - return checkedName; - } - - /** - * The current operating system. - */ - public static final OperatingSystem CURRENT_OS = parseOSName(System.getProperty("os.name")); - - /** - * The total memory/MB this computer have. - */ - public static final int TOTAL_MEMORY; - - /** - * The suggested memory size/MB for Minecraft to allocate. - */ - public static final int SUGGESTED_MEMORY; - - 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. - */ - public static final Charset NATIVE_CHARSET; - - /** - * Windows system build number. - * When the version number is not recognized or on another system, the value will be -1. - */ - public static final int SYSTEM_BUILD_NUMBER; - - /** - * The name of current operating system. - */ - public static final String SYSTEM_NAME; - - /** - * The version of current operating system. - */ - public static final String SYSTEM_VERSION; - - public static final Pattern INVALID_RESOURCE_CHARACTERS; - private static final String[] INVALID_RESOURCE_BASENAMES; - private static final String[] INVALID_RESOURCE_FULLNAMES; - - private static final Pattern MEMINFO_PATTERN = Pattern.compile("^(?.*?):\\s+(?\\d+) kB?$"); - - static { - String nativeEncoding = System.getProperty("native.encoding"); - String hmclNativeEncoding = System.getProperty("hmcl.native.encoding"); - Charset nativeCharset = Charset.defaultCharset(); - - try { - if (hmclNativeEncoding != null) { - nativeCharset = Charset.forName(hmclNativeEncoding); - } else { - if (nativeEncoding != null && !nativeEncoding.equalsIgnoreCase(nativeCharset.name())) { - nativeCharset = Charset.forName(nativeEncoding); - } - - if (nativeCharset == StandardCharsets.UTF_8 || nativeCharset == StandardCharsets.US_ASCII) { - nativeCharset = StandardCharsets.UTF_8; - } else if ("GBK".equalsIgnoreCase(nativeCharset.name()) || "GB2312".equalsIgnoreCase(nativeCharset.name())) { - nativeCharset = Charset.forName("GB18030"); - } - } - } catch (UnsupportedCharsetException e) { - e.printStackTrace(); - } - NATIVE_CHARSET = nativeCharset; - - if (CURRENT_OS == WINDOWS) { - String versionNumber = null; - int buildNumber = -1; - - try { - Process process = Runtime.getRuntime().exec(new String[]{"cmd", "ver"}); - try (BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream(), NATIVE_CHARSET))) { - Matcher matcher = Pattern.compile("(?[0-9]+\\.[0-9]+\\.(?[0-9]+)(\\.[0-9]+)?)]$") - .matcher(reader.readLine().trim()); - - if (matcher.find()) { - versionNumber = matcher.group("version"); - buildNumber = Integer.parseInt(matcher.group("build")); - } - } - process.destroy(); - } catch (Throwable ignored) { - } - - if (versionNumber == null) { - versionNumber = System.getProperty("os.version"); - } - - String osName = System.getProperty("os.name"); - - // Java 17 or earlier recognizes Windows 11 as Windows 10 - if (osName.equals("Windows 10") && buildNumber >= 22000) { - osName = "Windows 11"; - } - - SYSTEM_NAME = osName; - SYSTEM_VERSION = versionNumber; - SYSTEM_BUILD_NUMBER = buildNumber; - } else { - SYSTEM_NAME = System.getProperty("os.name"); - SYSTEM_VERSION = System.getProperty("os.version"); - SYSTEM_BUILD_NUMBER = -1; - } - - TOTAL_MEMORY = getPhysicalMemoryStatus() - .map(physicalMemoryStatus -> (int) (physicalMemoryStatus.getTotal() / 1024 / 1024)) - .orElse(1024); - - SUGGESTED_MEMORY = TOTAL_MEMORY >= 32768 ? 8192 : (int) (Math.round(1.0 * TOTAL_MEMORY / 4.0 / 128.0) * 128); - - // setup the invalid names - if (CURRENT_OS == WINDOWS) { - // valid names and characters taken from http://msdn.microsoft.com/library/default.asp?url=/library/en-us/fileio/fs/naming_a_file.asp - INVALID_RESOURCE_CHARACTERS = Pattern.compile("[/\"<>|?*:\\\\]"); - INVALID_RESOURCE_BASENAMES = new String[]{"aux", "com1", "com2", "com3", "com4", - "com5", "com6", "com7", "com8", "com9", "con", "lpt1", "lpt2", - "lpt3", "lpt4", "lpt5", "lpt6", "lpt7", "lpt8", "lpt9", "nul", "prn"}; - Arrays.sort(INVALID_RESOURCE_BASENAMES); - //CLOCK$ may be used if an extension is provided - INVALID_RESOURCE_FULLNAMES = new String[]{"clock$"}; - } else { - //only front slash and null char are invalid on UNIXes - //taken from http://www.faqs.org/faqs/unix-faq/faq/part2/section-2.html - INVALID_RESOURCE_CHARACTERS = null; - INVALID_RESOURCE_BASENAMES = null; - INVALID_RESOURCE_FULLNAMES = null; - } - } - - public static OperatingSystem parseOSName(String name) { - if (name == null) { - return UNKNOWN; - } - - name = name.trim().toLowerCase(Locale.ROOT); - - if (name.contains("win")) - return WINDOWS; - else if (name.contains("mac")) - return OSX; - else if (name.contains("solaris") || name.contains("linux") || name.contains("unix") || name.contains("sunos")) - return LINUX; - else - return UNKNOWN; - } - - @SuppressWarnings("deprecation") - public static Optional getPhysicalMemoryStatus() { - if (CURRENT_OS == LINUX) { - try { - long free = 0, available = 0, total = 0; - for (String line : Files.readAllLines(Paths.get("/proc/meminfo"))) { - Matcher matcher = MEMINFO_PATTERN.matcher(line); - if (matcher.find()) { - String key = matcher.group("key"); - String value = matcher.group("value"); - if ("MemAvailable".equals(key)) { - available = Long.parseLong(value) * 1024; - } - if ("MemFree".equals(key)) { - free = Long.parseLong(value) * 1024; - } - if ("MemTotal".equals(key)) { - total = Long.parseLong(value) * 1024; - } - } - } - if (total > 0) { - return Optional.of(new PhysicalMemoryStatus(total, available > 0 ? available : free)); - } - } catch (IOException e) { - e.printStackTrace(); - } - } - - try { - java.lang.management.OperatingSystemMXBean bean = java.lang.management.ManagementFactory.getOperatingSystemMXBean(); - if (bean instanceof com.sun.management.OperatingSystemMXBean) { - com.sun.management.OperatingSystemMXBean sunBean = - (com.sun.management.OperatingSystemMXBean) - java.lang.management.ManagementFactory.getOperatingSystemMXBean(); - return Optional.of(new PhysicalMemoryStatus(sunBean.getTotalPhysicalMemorySize(), sunBean.getFreePhysicalMemorySize())); - } - } catch (NoClassDefFoundError ignored) { - } - return Optional.empty(); - } - - @SuppressWarnings("removal") - public static void forceGC() { - System.gc(); - try { - System.runFinalization(); - System.gc(); - } catch (NoSuchMethodError ignored) { - } - } - - public static Path getWorkingDirectory(String folder) { - String home = System.getProperty("user.home", "."); - switch (OperatingSystem.CURRENT_OS) { - case LINUX: - return Paths.get(home, "." + folder).toAbsolutePath(); - case WINDOWS: - String appdata = System.getenv("APPDATA"); - return Paths.get(appdata == null ? home : appdata, "." + folder).toAbsolutePath(); - case OSX: - return Paths.get(home, "Library", "Application Support", folder).toAbsolutePath(); - default: - return Paths.get(home, folder).toAbsolutePath(); - } - } - - /** - * Returns true if the given name is a valid file name on this operating system, - * and false otherwise. - */ - public static boolean isNameValid(String name) { - // empty filename is not allowed - if (name.isEmpty()) - return false; - // . and .. have special meaning on all platforms - if (name.equals(".")) - return false; - // \0 and / are forbidden on all platforms - if (name.indexOf('/') != -1 || name.indexOf('\0') != -1) - return false; - - if (CURRENT_OS == WINDOWS) { // Windows only - char lastChar = name.charAt(name.length() - 1); - // filenames ending in dot are not valid - if (lastChar == '.') - return false; - // file names ending with whitespace are truncated (bug 118997) - if (Character.isWhitespace(lastChar)) - return false; - int dot = name.indexOf('.'); - // on windows, filename suffixes are not relevant to name validity - String basename = dot == -1 ? name : name.substring(0, dot); - if (Arrays.binarySearch(INVALID_RESOURCE_BASENAMES, basename.toLowerCase(Locale.ROOT)) >= 0) - return false; - if (Arrays.binarySearch(INVALID_RESOURCE_FULLNAMES, name.toLowerCase(Locale.ROOT)) >= 0) - return false; - if (INVALID_RESOURCE_CHARACTERS.matcher(name).find()) - return false; - } - - return true; - } - - public static class PhysicalMemoryStatus { - private final long total; - private final long available; - - public PhysicalMemoryStatus(long total, long available) { - this.total = total; - this.available = available; - } - - public long getTotal() { - return total; - } - - public double getTotalGB() { - return toGigaBytes(total); - } - - public long getUsed() { - return hasAvailable() ? total - available : 0; - } - - public double getUsedGB() { - return toGigaBytes(getUsed()); - } - - public long getAvailable() { - return available; - } - - public double getAvailableGB() { - return toGigaBytes(available); - } - - public boolean hasAvailable() { - return available >= 0; - } - - public static double toGigaBytes(long bytes) { - return bytes / 1024. / 1024. / 1024.; - } - - public static final PhysicalMemoryStatus INVALID = new PhysicalMemoryStatus(0, -1); - } -}