[FIX] Получение RAM.

This commit is contained in:
zaxar163 2019-04-27 18:42:42 +03:00
parent b1e0fb65df
commit c820e7a406
No known key found for this signature in database
GPG key ID: E3B309DD3852DE06
9 changed files with 9 additions and 237 deletions

View file

@ -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);

View file

@ -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;

View file

@ -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');
}

View file

@ -30,6 +30,8 @@ public class FunctionalBridge {
public static AtomicReference<HWID> hwid = new AtomicReference<>();
@LauncherAPI
public static Thread getHWID = null;
private static long cachedMemorySize = -1;
@LauncherAPI
public static HashedDirRunnable offlineUpdateRequest(String dirName, Path dir, SignedObjectHolder<HashedDir> 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

View file

@ -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();
}

View file

@ -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;

View file

@ -1,4 +1,4 @@
package ru.gravit.launcher.managers;
package ru.gravit.launcher.utils;
import cpw.mods.fml.SafeExitJVMLegacy;
import net.minecraftforge.fml.SafeExitJVM;

View file

@ -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<String, String> 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<String, String> requestProps;
public AtomicInteger writed = new AtomicInteger(0);
public final AtomicBoolean interrupt = new AtomicBoolean(false);
public final AtomicBoolean interrupted = new AtomicBoolean(false);
public AtomicReference<Throwable> 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<String, String> 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<String, String> 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<String, String> 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<String, String> 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<String, String> 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) {
}
}
}

View file

@ -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;