This commit is contained in:
huangyuhui
2017-02-22 20:12:21 +08:00
parent f4d148b042
commit d1c4dee7f7
303 changed files with 15121 additions and 2180 deletions

View File

@@ -17,15 +17,10 @@
*/
package org.jackhuang.hmcl.util.sys;
import com.sun.management.OperatingSystemMXBean;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.lang.management.ManagementFactory;
import java.util.Locale;
import java.util.StringTokenizer;
import org.jackhuang.hmcl.util.code.Charsets;
import org.jackhuang.hmcl.api.HMCLog;
/**
@@ -68,56 +63,20 @@ public enum OS {
*/
public static long getTotalPhysicalMemory() {
try {
if (os() == LINUX)
return memoryInfoForLinux()[0] * 1024;
else {
OperatingSystemMXBean o = (OperatingSystemMXBean) ManagementFactory.getOperatingSystemMXBean();
return o.getTotalPhysicalMemorySize();
}
return ReflectionHelper.get(ManagementFactory.getOperatingSystemMXBean(), "getTotalPhysicalMemorySize");
} catch (Throwable t) {
HMCLog.warn("Failed to get total physical memory size", t);
return -1;
return 1024;
}
}
public static int getSuggestedMemorySize() {
long total = getTotalPhysicalMemory();
if (total == -1)
return 1024;
int memory = (int) (total / 1024 / 1024) / 4;
memory = Math.round((float) memory / 128.0f) * 128;
return memory;
}
public static long[] memoryInfoForLinux() throws IOException {
File file = new File("/proc/meminfo");
try (BufferedReader br = new BufferedReader(new InputStreamReader(FileUtils.openInputStream(file), Charsets.UTF_8))) {
long[] result = new long[4];
String str;
StringTokenizer token;
while ((str = br.readLine()) != null) {
token = new StringTokenizer(str);
if (!token.hasMoreTokens())
continue;
str = token.nextToken();
if (!token.hasMoreTokens())
continue;
if (str.equalsIgnoreCase("MemTotal:"))
result[0] = Long.parseLong(token.nextToken());
else if (str.equalsIgnoreCase("MemFree:"))
result[1] = Long.parseLong(token.nextToken());
else if (str.equalsIgnoreCase("SwapTotal:"))
result[2] = Long.parseLong(token.nextToken());
else if (str.equalsIgnoreCase("SwapFree:"))
result[3] = Long.parseLong(token.nextToken());
}
return result;
}
}
public static String getLinuxReleaseVersion() throws IOException {
return FileUtils.read(new File("/etc/issue"));
}

View File

@@ -34,7 +34,7 @@ import org.jackhuang.hmcl.api.IProcess;
public class ProcessThread extends Thread {
IProcess p;
public final EventHandler<SimpleEvent<String>> printlnEvent = new EventHandler<>();
public final EventHandler<SimpleEvent<IProcess>> stopEvent = new EventHandler<>();

View File

@@ -0,0 +1,66 @@
/*
* Hello Minecraft! Launcher.
* Copyright (C) 2013 huangyuhui <huanghongxun2008@126.com>
*
* 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 {http://www.gnu.org/licenses/}.
*/
package org.jackhuang.hmcl.util.sys;
import java.lang.reflect.AccessibleObject;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.security.AccessController;
import java.security.PrivilegedActionException;
import java.security.PrivilegedExceptionAction;
import sun.misc.Unsafe;
/**
*
* @author huang
*/
public class ReflectionHelper {
private static Unsafe unsafe = null;
private static long objectFieldOffset;
static {
try {
unsafe = AccessController.doPrivileged(new PrivilegedExceptionAction<Unsafe> () {
@Override
public Unsafe run() throws Exception {
Field theUnsafe = Unsafe.class.getDeclaredField("theUnsafe");
theUnsafe.setAccessible(true);
return (Unsafe) theUnsafe.get(null);
}
});
Field overrideField = AccessibleObject.class.getDeclaredField("override");
objectFieldOffset = unsafe.objectFieldOffset(overrideField);
} catch (Throwable ex) {
}
}
private static void setAccessible(AccessibleObject obj) {
unsafe.putBoolean(obj, objectFieldOffset, true);
}
public static <T> T get(Object obj, String fieldName) {
try {
Method method = obj.getClass().getDeclaredMethod(fieldName);
setAccessible(method);
return (T) method.invoke(obj);
} catch (Throwable ex) {
return null;
}
}
}

View File

@@ -32,6 +32,17 @@ import javax.swing.Timer;
public class Page extends TopTabPage {
boolean selected = false;
public int id;
@Override
public int getId() {
return id;
}
@Override
public void setId(int id) {
this.id = id;
}
@Override
public boolean isSelected() {
@@ -39,9 +50,11 @@ public class Page extends TopTabPage {
}
@Override
public void onSelect() {
if (!selected)
public void onSelect(TopTabPage lastSelectedPage) {
if (!selected) {
lastPage = (Page) lastSelectedPage;
animate();
}
selected = true;
}
@@ -65,13 +78,13 @@ public class Page extends TopTabPage {
// -------------------
// Animation
// -------------------
private static final int ANIMATION_LENGTH = 10;
private static final int ANIMATION_LENGTH = 5;
public Page() {
timer = new Timer(1, (e) -> {
SwingUtilities.invokeLater(() -> {
Page.this.repaint();
offsetX += 0.15;
offsetX += 0.14;
if (offsetX >= ANIMATION_LENGTH) {
timer.stop();
Page.this.repaint();
@@ -80,7 +93,7 @@ public class Page extends TopTabPage {
});
}
BufferedImage cache = null;
BufferedImage cache = null, lastCache = null;
@Override
public void paint(Graphics g) {
@@ -88,33 +101,50 @@ public class Page extends TopTabPage {
super.paint(g);
return;
}
double pgs = 1 - Math.sin(Math.PI / 2 / ANIMATION_LENGTH * offsetX);
Graphics2D gg = (Graphics2D) g;
double pgs = Math.sin(Math.PI / 2 / ANIMATION_LENGTH * offsetX);
if (Math.abs(ANIMATION_LENGTH - offsetX) < 0.1) {
super.paint(g);
return;
}
if (offsetX == 0) {
cache = new BufferedImage(getWidth(), getHeight(), BufferedImage.TYPE_INT_ARGB);
Graphics2D g2d = cache.createGraphics();
if (isOpaque()) {
g2d.setColor(getBackground());
g2d.fillRect(0, 0, getWidth(), getHeight());
}
super.paint(g2d);
g2d.dispose();
cache = cacheImpl(this);
if (lastPage != null)
lastCache = cacheImpl(lastPage);
}
if (pgs > 1)
pgs = 1;
if (pgs < 0)
pgs = 0;
Graphics2D gg = (Graphics2D) g;
gg.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, (float) (1 - pgs)));
g.drawImage(cache, (int) (pgs * 50), 0, this);
int ori = lastPage != null ? (lastPage.getId() < getId() ? 1 : -1) : 1;
if (pgs >= 0.5)
animateImpl(gg, cache, (int) (((1 - (pgs - 0.5) * 2)) * totalOffset * ori), (float) ((pgs - 0.5) * 2));
else
animateImpl(gg, lastCache, (int) (((- pgs * 2)) * totalOffset * ori), (float) (1 - pgs * 2));
}
double offsetX = ANIMATION_LENGTH;
BufferedImage cacheImpl(Page page) {
BufferedImage image = new BufferedImage(page.getWidth(), page.getHeight(), BufferedImage.TYPE_INT_ARGB);
Graphics2D g2d = image.createGraphics();
if (isOpaque()) {
g2d.setColor(page.getBackground());
g2d.fillRect(0, 0, page.getWidth(), page.getHeight());
}
page.superPaint(g2d);
g2d.dispose();
return image;
}
void animateImpl(Graphics2D g, BufferedImage image, int left, float alpha) {
if (image == null)
return;
g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, alpha));
g.drawImage(image, left, 0, this);
}
protected void superPaint(Graphics2D g) {
super.paint(g);
}
double offsetX = ANIMATION_LENGTH, totalOffset = 20;
Page lastPage;
Timer timer;
protected boolean animationEnabled = true;