最大限度的捕获异常,防止客户端崩溃

添加关闭推荐内容按钮
This commit is contained in:
南宫临风 2016-08-24 13:46:06 +08:00
parent 6db34fcdc7
commit 90451e6da9
6 changed files with 111 additions and 31 deletions

View File

@ -144,8 +144,7 @@ public class DynamicDownloadProvider extends MojangDownloadProvider {
try { try {
String providerInfo = NetUtils.get(PROVIDER_ADDR); String providerInfo = NetUtils.get(PROVIDER_ADDR);
Map<String, String> addrInfo = null; Map<String, String> addrInfo = null;
addrInfo = C.GSON.fromJson(providerInfo, new TypeToken<Map<String, String>>() { addrInfo = C.GSON.fromJson(providerInfo, new TypeToken<Map<String, String>>() {}.getType());
}.getType());
if (addrInfo != null) { if (addrInfo != null) {
setLibrariesAddr(getValue(addrInfo, "libraries")); setLibrariesAddr(getValue(addrInfo, "libraries"));
setAssetsAddr(getValue(addrInfo, "assets")); setAssetsAddr(getValue(addrInfo, "assets"));
@ -153,7 +152,7 @@ public class DynamicDownloadProvider extends MojangDownloadProvider {
setLauncherAddr(getValue(addrInfo, "launcher")); setLauncherAddr(getValue(addrInfo, "launcher"));
setVersionManifestAddr(getValue(addrInfo, "versionManifest")); setVersionManifestAddr(getValue(addrInfo, "versionManifest"));
} }
} catch (IOException ex) { } catch (Throwable t) {
} }
} }

View File

@ -21,8 +21,10 @@ import org.jackhuang.hellominecraft.launcher.core.download.DownloadType;
import com.google.gson.annotations.SerializedName; import com.google.gson.annotations.SerializedName;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.HashSet;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Set;
import java.util.TreeMap; import java.util.TreeMap;
import java.util.UUID; import java.util.UUID;
import org.jackhuang.hellominecraft.launcher.core.auth.IAuthenticator; import org.jackhuang.hellominecraft.launcher.core.auth.IAuthenticator;
@ -76,6 +78,16 @@ public final class Config implements Cloneable {
private Map<String, Map> auth; private Map<String, Map> auth;
@SerializedName("ignoreUpdateVersion") @SerializedName("ignoreUpdateVersion")
private String ignoreUpdateVersion; private String ignoreUpdateVersion;
@SerializedName("ignoreRecommend")
private Set<String> ignoreRecommend;
public Set<String> getIgnoreRecommend() {
if (ignoreRecommend == null) {
ignoreRecommend = new HashSet<>();
Settings.save();
}
return ignoreRecommend;
}
public List<JdkVersion> getJava() { public List<JdkVersion> getJava() {
return java == null ? java = new ArrayList<>() : java; return java == null ? java = new ArrayList<>() : java;

View File

@ -36,9 +36,15 @@ import java.util.ArrayList;
import java.util.concurrent.Executors; import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.SwingUtilities; import javax.swing.SwingUtilities;
import org.jackhuang.hellominecraft.launcher.Main;
import org.jackhuang.hellominecraft.launcher.setting.Settings;
import org.jackhuang.hellominecraft.util.C; import org.jackhuang.hellominecraft.util.C;
import org.jackhuang.hellominecraft.util.NetUtils; import org.jackhuang.hellominecraft.util.NetUtils;
import org.jackhuang.hellominecraft.util.StrUtils;
import org.jackhuang.hellominecraft.util.ui.SwingUtils; import org.jackhuang.hellominecraft.util.ui.SwingUtils;
/** /**
@ -49,12 +55,21 @@ public class RecommendPanel extends JPanel {
private static final int SWITCH_INTERVAL = 10; private static final int SWITCH_INTERVAL = 10;
private static final int SPACE = 10;
private static final int TOP_POSITION = 2;
private JButton closeButton;
private Image currImage; private Image currImage;
private String imageKey = null; private String imageKey = null;
private boolean ignoreSwitch = false;
private List<RecommendInfo> recommends; private List<RecommendInfo> recommends;
public ScheduledExecutorService scheduledexec = Executors.newScheduledThreadPool(1); public ScheduledExecutorService scheduledexec = Executors.newScheduledThreadPool(1);
public RecommendPanel() { public RecommendPanel() {
initComponents();
recommends = new ArrayList<RecommendInfo>(); recommends = new ArrayList<RecommendInfo>();
new LoadImages().execute(); new LoadImages().execute();
setCursor(new Cursor(Cursor.HAND_CURSOR)); setCursor(new Cursor(Cursor.HAND_CURSOR));
@ -67,6 +82,31 @@ public class RecommendPanel extends JPanel {
}); });
} }
private void initComponents() {
this.setLayout(null);
closeButton = new JButton(Main.getIcon("re_close.png"));
closeButton.setRolloverIcon(Main.getIcon("re_close_enter.png"));
closeButton.setBorder(BorderFactory.createEmptyBorder());
closeButton.setContentAreaFilled(false);
closeButton.addActionListener((e) -> {
synchronized(RecommendPanel.class) {
if (StrUtils.isNotBlank(imageKey)) {
Settings.getInstance().getIgnoreRecommend().add(imageKey);
Settings.save();
ignoreSwitch = true;
showNext();
}
}
});
closeButton.setCursor(new Cursor(Cursor.HAND_CURSOR));
closeButton.setFocusable(false);
closeButton.setBounds(0, 0, 12, 12);
closeButton.setVisible(false);
this.add(closeButton);
}
private void MouseClicked(MouseEvent evt) { private void MouseClicked(MouseEvent evt) {
if (imageKey == null) { if (imageKey == null) {
return; return;
@ -86,13 +126,14 @@ public class RecommendPanel extends JPanel {
@Override @Override
public void run() { public void run() {
for (RecommendInfo info : recommends) { for (RecommendInfo info : recommends) {
try { if (!ignoreShowUrl(info.url)) {
File tempFile = File.createTempFile("hmcl", "png"); try {
String tempPath = tempFile.getCanonicalPath(); File tempFile = File.createTempFile("hmcl", "png");
if (NetUtils.download(info.url, tempPath)) { String tempPath = tempFile.getCanonicalPath();
info.image = ImageIO.read(tempFile); if (NetUtils.download(info.url, tempPath)) {
} info.image = ImageIO.read(tempFile);
} catch (IOException ex) { }
} catch (Throwable t) { }
} }
} }
@ -105,9 +146,13 @@ public class RecommendPanel extends JPanel {
SwingUtilities.invokeLater(new Runnable() { SwingUtilities.invokeLater(new Runnable() {
@Override @Override
public void run() { public void run() {
int showIndex = getNextImageIndex(); synchronized(RecommendPanel.class) {
RecommendInfo info = recommends.get(showIndex); if (ignoreSwitch) {
RecommendPanel.this.setImage(info.url, info.image); ignoreSwitch = false;
} else {
showNext();
}
}
} }
}); });
} }
@ -142,25 +187,45 @@ public class RecommendPanel extends JPanel {
} }
return true; return true;
} }
private void showNext() {
if (getCanShowImageCount() == 0) {
setVisible(false);
} else {
int showIndex = getNextImageIndex();
RecommendInfo info = recommends.get(showIndex);
setImage(info.url, info.image);
}
}
private boolean ignoreShowUrl(String url) {
return Settings.getInstance().getIgnoreRecommend().contains(url);
}
public int getCanShowImageCount() {
int imageCount = 0;
for (RecommendInfo recommend : recommends) {
if (recommend.image != null && !ignoreShowUrl(recommend.url)) {
imageCount++;
}
}
return imageCount;
}
public int getNextImageIndex(int showIndex) { public int getNextImageIndex(int showIndex) {
if (showIndex >= recommends.size()) { if (showIndex >= recommends.size()) {
showIndex = 0; showIndex = 0;
} }
RecommendInfo info = recommends.get(showIndex); RecommendInfo info = recommends.get(showIndex);
if (info.image == null) { if (info.image == null || ignoreShowUrl(info.url)) {
showIndex = getNextImageIndex(++showIndex); showIndex = getNextImageIndex(++showIndex);
} }
return showIndex; return showIndex;
} }
public int getNextImageIndex() { public int getNextImageIndex() {
int showIndex = 0; int showIndex = getCurrentImageIndex();
if (imageKey != null) { if (++showIndex >= recommends.size()) {
showIndex = getCurrentImageIndex();
showIndex++;
}
if (showIndex >= recommends.size()) {
showIndex = 0; showIndex = 0;
} }
showIndex = getNextImageIndex(showIndex); showIndex = getNextImageIndex(showIndex);
@ -178,21 +243,26 @@ public class RecommendPanel extends JPanel {
} }
return currIndex; return currIndex;
} }
public void setImage(String key, Image image) { public void setImage(String key, Image image) {
this.imageKey = key; this.imageKey = key;
this.currImage = image; this.currImage = image;
//setToolTipText(C.i18n("ui.message.recommend_tip"));
setSize(image.getWidth(this), image.getHeight(this)); int btnWidth = closeButton.getWidth();
setSize(image.getWidth(this) + SPACE + btnWidth, image.getHeight(this));
closeButton.setLocation(getWidth() - btnWidth, TOP_POSITION);
closeButton.setVisible(true);
SwingUtilities.updateComponentTreeUI(this.getRootPane()); SwingUtilities.updateComponentTreeUI(this.getRootPane());
} }
@Override @Override
public void paintComponent(Graphics g) { public void paintComponent(Graphics g) {
if (null == currImage) { if (currImage != null) {
return; g.drawImage(currImage, 0, 0, currImage.getWidth(this), currImage.getHeight(this), this);
} }
g.drawImage(currImage, 0, 0, currImage.getWidth(this), currImage.getHeight(this), this); super.paintComponent(g);
} }
static class RecommendInfo { static class RecommendInfo {
@ -215,8 +285,7 @@ public class RecommendPanel extends JPanel {
} }
Map<String, Object> data = new Gson().fromJson(content, Map<String, Object> data = new Gson().fromJson(content,
new TypeToken<Map<String, Object>>() { new TypeToken<Map<String, Object>>() {}.getType());
}.getType());
if (data == null) { if (data == null) {
break; break;
} }

View File

@ -139,8 +139,8 @@ public class CrashReporter implements Thread.UncaughtExceptionHandler {
map.put("CrashReport", text); map.put("CrashReport", text);
try { try {
NetUtils.post(NetUtils.constantURL("http://huangyuhui.duapp.com/crash.php"), map); NetUtils.post(NetUtils.constantURL("http://huangyuhui.duapp.com/crash.php"), map);
} catch (IOException ex) { } catch (Throwable error) {
LOGGER.log(Level.SEVERE, "Failed to post HMCL server.", ex); LOGGER.log(Level.SEVERE, "Failed to post HMCL server.", error);
} }
}); });
t.setDaemon(true); t.setDaemon(true);

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.1 KiB