From c820e7a406174883ecb1ae7ad6bd2e1638f81c69 Mon Sep 17 00:00:00 2001 From: zaxar163 Date: Sat, 27 Apr 2019 18:42:42 +0300 Subject: [PATCH] =?UTF-8?q?[FIX]=20=D0=9F=D0=BE=D0=BB=D1=83=D1=87=D0=B5?= =?UTF-8?q?=D0=BD=D0=B8=D0=B5=20RAM.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../dialog/overlay/settings/settings.js | 2 +- .../ru/gravit/launcher/LauncherAgent.java | 2 +- .../launcher/client/ClientLauncher.java | 2 +- .../launcher/client/FunctionalBridge.java | 5 +- .../launcher/hwid/OshiHWIDProvider.java | 2 +- .../ru/gravit/launcher/utils/DirWatcher.java | 1 - .../{managers => utils}/NativeJVMHalt.java | 2 +- .../gravit/utils/downloader/Downloader.java | 213 ------------------ .../ru/gravit/utils/helper/JVMHelper.java | 17 -- 9 files changed, 9 insertions(+), 237 deletions(-) rename Launcher/src/main/java/ru/gravit/launcher/{managers => utils}/NativeJVMHalt.java (96%) delete mode 100644 libLauncher/src/main/java/ru/gravit/utils/downloader/Downloader.java diff --git a/Launcher/runtime/dialog/overlay/settings/settings.js b/Launcher/runtime/dialog/overlay/settings/settings.js index 8429fd46..3e2fd32d 100644 --- a/Launcher/runtime/dialog/overlay/settings/settings.js +++ b/Launcher/runtime/dialog/overlay/settings/settings.js @@ -42,7 +42,7 @@ var settingsOverlay = { settingsOverlay.updateRAMLabel(); var ramSlider = holder.lookup("#ramSlider"); - ramSlider.setMax(JVMHelper.RAM); + ramSlider.setMax(FunctionalBridge.getTotalMemory()); ramSlider.setSnapToTicks(true); ramSlider.setShowTickMarks(true); ramSlider.setShowTickLabels(true); diff --git a/Launcher/src/main/java/ru/gravit/launcher/LauncherAgent.java b/Launcher/src/main/java/ru/gravit/launcher/LauncherAgent.java index 9b879f62..cb3f0ca4 100644 --- a/Launcher/src/main/java/ru/gravit/launcher/LauncherAgent.java +++ b/Launcher/src/main/java/ru/gravit/launcher/LauncherAgent.java @@ -8,7 +8,7 @@ import org.objectweb.asm.tree.InsnNode; import org.objectweb.asm.tree.MethodNode; -import ru.gravit.launcher.managers.NativeJVMHalt; +import ru.gravit.launcher.utils.NativeJVMHalt; import ru.gravit.utils.helper.LogHelper; import java.io.ByteArrayOutputStream; diff --git a/Launcher/src/main/java/ru/gravit/launcher/client/ClientLauncher.java b/Launcher/src/main/java/ru/gravit/launcher/client/ClientLauncher.java index 6a623aec..77fd11e9 100644 --- a/Launcher/src/main/java/ru/gravit/launcher/client/ClientLauncher.java +++ b/Launcher/src/main/java/ru/gravit/launcher/client/ClientLauncher.java @@ -327,7 +327,7 @@ public static Process launch( context.playerProfile = params.pp; context.args.add(javaBin.toString()); context.args.add(MAGICAL_INTEL_OPTION); - if (params.ram > 0 && params.ram <= JVMHelper.RAM) { + if (params.ram > 0 && params.ram <= FunctionalBridge.getTotalMemory()) { context.args.add("-Xms" + params.ram + 'M'); context.args.add("-Xmx" + params.ram + 'M'); } diff --git a/Launcher/src/main/java/ru/gravit/launcher/client/FunctionalBridge.java b/Launcher/src/main/java/ru/gravit/launcher/client/FunctionalBridge.java index 6639966d..6c61bf5c 100644 --- a/Launcher/src/main/java/ru/gravit/launcher/client/FunctionalBridge.java +++ b/Launcher/src/main/java/ru/gravit/launcher/client/FunctionalBridge.java @@ -30,6 +30,8 @@ public class FunctionalBridge { public static AtomicReference hwid = new AtomicReference<>(); @LauncherAPI public static Thread getHWID = null; + + private static long cachedMemorySize = -1; @LauncherAPI public static HashedDirRunnable offlineUpdateRequest(String dirName, Path dir, SignedObjectHolder hdir, FileNameMatcher matcher, boolean digest) { @@ -61,7 +63,8 @@ public static HWID getHWID() { @LauncherAPI public static long getTotalMemory() { - return hwidProvider.getTotalMemory() >> 20; + if (cachedMemorySize > 0) return cachedMemorySize; + return cachedMemorySize = hwidProvider.getTotalMemory() >> 20; } @LauncherAPI diff --git a/Launcher/src/main/java/ru/gravit/launcher/hwid/OshiHWIDProvider.java b/Launcher/src/main/java/ru/gravit/launcher/hwid/OshiHWIDProvider.java index 43003f1c..69f68802 100644 --- a/Launcher/src/main/java/ru/gravit/launcher/hwid/OshiHWIDProvider.java +++ b/Launcher/src/main/java/ru/gravit/launcher/hwid/OshiHWIDProvider.java @@ -83,7 +83,7 @@ public String getMacAddr() { } public long getTotalMemory() { - if (noHWID) return -1; + if (noHWID) return 1024>>20; if (hardware == null) hardware = systemInfo.getHardware(); return hardware.getMemory().getTotal(); } diff --git a/Launcher/src/main/java/ru/gravit/launcher/utils/DirWatcher.java b/Launcher/src/main/java/ru/gravit/launcher/utils/DirWatcher.java index f9157c35..9c0e30bf 100644 --- a/Launcher/src/main/java/ru/gravit/launcher/utils/DirWatcher.java +++ b/Launcher/src/main/java/ru/gravit/launcher/utils/DirWatcher.java @@ -6,7 +6,6 @@ import ru.gravit.launcher.hasher.HashedEntry; import ru.gravit.launcher.hasher.HashedFile; import ru.gravit.launcher.hasher.HashedEntry.Type; -import ru.gravit.launcher.managers.NativeJVMHalt; import ru.gravit.utils.helper.IOHelper; import ru.gravit.utils.helper.JVMHelper; import ru.gravit.utils.helper.JVMHelper.OS; diff --git a/Launcher/src/main/java/ru/gravit/launcher/managers/NativeJVMHalt.java b/Launcher/src/main/java/ru/gravit/launcher/utils/NativeJVMHalt.java similarity index 96% rename from Launcher/src/main/java/ru/gravit/launcher/managers/NativeJVMHalt.java rename to Launcher/src/main/java/ru/gravit/launcher/utils/NativeJVMHalt.java index cf166035..4401ceb1 100644 --- a/Launcher/src/main/java/ru/gravit/launcher/managers/NativeJVMHalt.java +++ b/Launcher/src/main/java/ru/gravit/launcher/utils/NativeJVMHalt.java @@ -1,4 +1,4 @@ -package ru.gravit.launcher.managers; +package ru.gravit.launcher.utils; import cpw.mods.fml.SafeExitJVMLegacy; import net.minecraftforge.fml.SafeExitJVM; diff --git a/libLauncher/src/main/java/ru/gravit/utils/downloader/Downloader.java b/libLauncher/src/main/java/ru/gravit/utils/downloader/Downloader.java deleted file mode 100644 index dd128305..00000000 --- a/libLauncher/src/main/java/ru/gravit/utils/downloader/Downloader.java +++ /dev/null @@ -1,213 +0,0 @@ -package ru.gravit.utils.downloader; - -import ru.gravit.utils.helper.IOHelper; -import ru.gravit.utils.helper.LogHelper; - -import javax.net.ssl.HttpsURLConnection; -import java.io.BufferedInputStream; -import java.io.File; -import java.io.FileOutputStream; -import java.io.IOException; -import java.net.HttpURLConnection; -import java.net.URL; -import java.security.cert.Certificate; -import java.util.Collections; -import java.util.HashMap; -import java.util.Map; -import java.util.concurrent.atomic.AtomicBoolean; -import java.util.concurrent.atomic.AtomicInteger; -import java.util.concurrent.atomic.AtomicReference; - -public class Downloader implements Runnable { - @FunctionalInterface - public interface Handler { - void check(Certificate[] certs) throws IOException; - } - - public static final Map requestClient = Collections.singletonMap("User-Agent", - "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.95 Safari/537.11"); - public static final int INTERVAL = 300; - - private final File file; - private final URL url; - private final String method; - public final Map requestProps; - public AtomicInteger writed = new AtomicInteger(0); - public final AtomicBoolean interrupt = new AtomicBoolean(false); - public final AtomicBoolean interrupted = new AtomicBoolean(false); - public AtomicReference ex = new AtomicReference<>(null); - private final int skip; - private final Handler handler; - - private HttpURLConnection connect = null; - - public Downloader(URL url, File file) { - this.requestProps = new HashMap<>(requestClient); - this.file = file; - this.url = url; - this.skip = 0; - this.handler = null; - this.method = null; - } - - public Downloader(URL url, File file, int skip) { - this.requestProps = new HashMap<>(requestClient); - this.file = file; - this.url = url; - this.skip = skip; - this.handler = null; - this.method = null; - } - - public Downloader(URL url, File file, Handler handler) { - this.requestProps = new HashMap<>(requestClient); - this.file = file; - this.url = url; - this.skip = 0; - this.handler = handler; - this.method = null; - } - - public Downloader(URL url, File file, int skip, Handler handler) { - this.requestProps = new HashMap<>(requestClient); - this.file = file; - this.url = url; - this.skip = skip; - this.handler = handler; - this.method = null; - } - - public Downloader(URL url, File file, int skip, Handler handler, Map requestProps) { - this.requestProps = new HashMap<>(requestProps); - this.file = file; - this.url = url; - this.skip = skip; - this.handler = handler; - this.method = null; - } - - public Downloader(URL url, File file, int skip, Handler handler, Map requestProps, String method) { - this.requestProps = new HashMap<>(requestProps); - this.file = file; - this.url = url; - this.skip = skip; - this.handler = handler; - this.method = method; - } - - public Downloader(URL url, File file, int skip, Handler handler, String method) { - this.requestProps = new HashMap<>(requestClient); - this.file = file; - this.url = url; - this.skip = skip; - this.handler = handler; - this.method = method; - } - - public Map getProps() { - return requestProps; - } - - public void addProp(String key, String value) { - requestProps.put(key, value); - } - - public File getFile() { - return file; - } - - public String getMethod() { - return method; - } - - public Handler getHandler() { - return handler; - } - - public void downloadFile() throws IOException { - if (!(url.getProtocol().equalsIgnoreCase("http") || url.getProtocol().equalsIgnoreCase("https"))) - throw new IOException("Invalid protocol."); - interrupted.set(false); - if (url.getProtocol().equalsIgnoreCase("http")) { - HttpURLConnection connect = (HttpURLConnection) (url).openConnection(); - this.connect = connect; - if (method != null) connect.setRequestMethod(method); - for (Map.Entry ent : requestProps.entrySet()) { - connect.setRequestProperty(ent.getKey(), ent.getValue()); - } - connect.setInstanceFollowRedirects(true); - if (!(connect.getResponseCode() >= 200 && connect.getResponseCode() < 300)) - throw new IOException(String.format("Invalid response of http server %d.", connect.getResponseCode())); - try (BufferedInputStream in = new BufferedInputStream(connect.getInputStream(), IOHelper.BUFFER_SIZE); - FileOutputStream fout = new FileOutputStream(file, skip != 0)) { - byte data[] = new byte[IOHelper.BUFFER_SIZE]; - int count = -1; - long timestamp = System.currentTimeMillis(); - int writed_local = 0; - in.skip(skip); - while ((count = in.read(data)) != -1) { - fout.write(data, 0, count); - writed_local += count; - if (System.currentTimeMillis() - timestamp > INTERVAL) { - writed.set(writed_local); - LogHelper.debug("Downloaded %d", writed_local); - if (interrupt.get()) { - break; - } - } - } - LogHelper.debug("Downloaded %d", writed_local); - writed.set(writed_local); - } - } else { - HttpsURLConnection connect = (HttpsURLConnection) (url).openConnection(); - this.connect = connect; - if (method != null) connect.setRequestMethod(method); - for (Map.Entry ent : requestProps.entrySet()) { - connect.setRequestProperty(ent.getKey(), ent.getValue()); - } - connect.setInstanceFollowRedirects(true); - if (handler != null) - handler.check(connect.getServerCertificates()); - if (!(connect.getResponseCode() >= 200 && connect.getResponseCode() < 300)) - throw new IOException(String.format("Invalid response of http server %d.", connect.getResponseCode())); - try (BufferedInputStream in = new BufferedInputStream(connect.getInputStream(), IOHelper.BUFFER_SIZE); - FileOutputStream fout = new FileOutputStream(file, skip != 0)) { - byte data[] = new byte[IOHelper.BUFFER_SIZE]; - int count = -1; - long timestamp = System.currentTimeMillis(); - int writed_local = 0; - in.skip(skip); - while ((count = in.read(data)) != -1) { - fout.write(data, 0, count); - writed_local += count; - if (System.currentTimeMillis() - timestamp > INTERVAL) { - writed.set(writed_local); - LogHelper.debug("Downloaded %d", writed_local); - if (interrupt.get()) { - break; - } - } - } - LogHelper.debug("Downloaded %d", writed_local); - writed.set(writed_local); - } - } - interrupted.set(true); - } - - @Override - public void run() { - try { - downloadFile(); - } catch (Throwable ex) { - this.ex.set(ex); - LogHelper.error(ex); - } - if (connect != null) - try { - connect.disconnect(); - } catch (Throwable ignored) { - } - } -} diff --git a/libLauncher/src/main/java/ru/gravit/utils/helper/JVMHelper.java b/libLauncher/src/main/java/ru/gravit/utils/helper/JVMHelper.java index e4e2510b..f0351c92 100644 --- a/libLauncher/src/main/java/ru/gravit/utils/helper/JVMHelper.java +++ b/libLauncher/src/main/java/ru/gravit/utils/helper/JVMHelper.java @@ -7,8 +7,6 @@ import java.lang.management.ManagementFactory; import java.lang.management.OperatingSystemMXBean; import java.lang.management.RuntimeMXBean; -import java.lang.reflect.InvocationTargetException; -import java.lang.reflect.Method; import java.net.MalformedURLException; import java.net.URL; import java.util.Arrays; @@ -52,8 +50,6 @@ public static OS byName(String name) { public static final int OS_BITS = getCorrectOSArch(); @LauncherAPI public static final int JVM_BITS = Integer.parseInt(System.getProperty("sun.arch.data.model")); - @LauncherAPI - public static final int RAM = getRAMAmount(); @LauncherAPI public static final SecurityManager SECURITY_MANAGER = System.getSecurityManager(); @@ -137,19 +133,6 @@ public static String getEnvPropertyCaseSensitive(String name) { return System.getenv().get(name); } - private static int getRAMAmount() { - int physicalRam = 1024; - try { - final Method getTotalPhysicalMemorySize = OPERATING_SYSTEM_MXBEAN.getClass().getDeclaredMethod("getTotalPhysicalMemorySize"); - getTotalPhysicalMemorySize.setAccessible(true); - physicalRam = (int) ((long)getTotalPhysicalMemorySize.invoke(OPERATING_SYSTEM_MXBEAN) >> 20); - } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException | NoSuchMethodException - | SecurityException e) { - throw new Error(e); - } - return Math.min(physicalRam, OS_BITS == 32 ? 1536 : 32768); // Limit 32-bit OS to 1536 MiB, and 64-bit OS to 32768 MiB (because it's enough) - } - @LauncherAPI public static boolean isJVMMatchesSystemArch() { return JVM_BITS == OS_BITS;