Reset the java directory when javadir is wrong.
This commit is contained in:
@@ -51,7 +51,7 @@ public final class C {
|
||||
try {
|
||||
return String.format(C.I18N.getString(a), format);
|
||||
} catch (Exception e) {
|
||||
HMCLog.warn("Failed to read localization lang: " + a, e);
|
||||
HMCLog.warn("Failed to read localization key: " + a, e);
|
||||
return a;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -22,7 +22,6 @@ import java.util.Collections;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import org.jackhuang.hellominecraft.utils.functions.NonConsumer;
|
||||
import org.jackhuang.hellominecraft.HMCLog;
|
||||
|
||||
/**
|
||||
@@ -32,7 +31,7 @@ import org.jackhuang.hellominecraft.HMCLog;
|
||||
public class TaskList extends Thread {
|
||||
|
||||
List<Task> taskQueue = Collections.synchronizedList(new ArrayList());
|
||||
ArrayList<NonConsumer> allDone = new ArrayList();
|
||||
ArrayList<Runnable> allDone = new ArrayList();
|
||||
ArrayList<DoingDoneListener<Task>> taskListener = new ArrayList();
|
||||
|
||||
int totTask;
|
||||
@@ -46,7 +45,7 @@ public class TaskList extends Thread {
|
||||
taskQueue.clear();
|
||||
}
|
||||
|
||||
public void addAllDoneListener(NonConsumer l) {
|
||||
public void addAllDoneListener(Runnable l) {
|
||||
allDone.add(l);
|
||||
}
|
||||
|
||||
@@ -100,8 +99,7 @@ public class TaskList extends Thread {
|
||||
try {
|
||||
if (this.isInterrupted()) return;
|
||||
Thread.sleep(1);
|
||||
} catch (InterruptedException ex) {
|
||||
HMCLog.warn("Failed to sleep task thread", ex);
|
||||
} catch (InterruptedException ignore) {
|
||||
}
|
||||
|
||||
}
|
||||
@@ -148,8 +146,8 @@ public class TaskList extends Thread {
|
||||
for (Task taskQueue1 : taskQueue)
|
||||
executeTask(taskQueue1);
|
||||
if (shouldContinue)
|
||||
for (NonConsumer d : allDone)
|
||||
d.onDone();
|
||||
for (Runnable d : allDone)
|
||||
d.run();
|
||||
}
|
||||
|
||||
public boolean isEmpty() {
|
||||
|
||||
@@ -19,7 +19,6 @@ package org.jackhuang.hellominecraft.tasks;
|
||||
import java.util.ArrayList;
|
||||
import javax.swing.SwingUtilities;
|
||||
import org.jackhuang.hellominecraft.C;
|
||||
import org.jackhuang.hellominecraft.utils.functions.NonConsumer;
|
||||
import org.jackhuang.hellominecraft.HMCLog;
|
||||
import org.jackhuang.hellominecraft.utils.system.MessageBox;
|
||||
import org.jackhuang.hellominecraft.utils.StrUtils;
|
||||
@@ -30,7 +29,7 @@ import org.jackhuang.hellominecraft.utils.SwingUtils;
|
||||
* @author huangyuhui
|
||||
*/
|
||||
public class TaskWindow extends javax.swing.JDialog
|
||||
implements ProgressProviderListener, NonConsumer, DoingDoneListener<Task> {
|
||||
implements ProgressProviderListener, Runnable, DoingDoneListener<Task> {
|
||||
|
||||
private static final TaskWindow instance = new TaskWindow();
|
||||
|
||||
@@ -202,7 +201,7 @@ public class TaskWindow extends javax.swing.JDialog
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDone() {
|
||||
public void run() {
|
||||
suc = true;
|
||||
this.dispose();
|
||||
HMCLog.log("Tasks are finished.");
|
||||
@@ -259,7 +258,9 @@ public class TaskWindow extends javax.swing.JDialog
|
||||
@Override
|
||||
public void setStatus(Task task, String sta) {
|
||||
SwingUtilities.invokeLater(() -> {
|
||||
SwingUtils.setValueAt(lstDownload, sta, lstDownload.getRowCount(), 0);
|
||||
int idx = tasks.indexOf(task);
|
||||
if (idx == -1) return;
|
||||
SwingUtils.setValueAt(lstDownload, task.getInfo() + ": " + sta, idx, 0);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -38,12 +38,14 @@ import org.jackhuang.hellominecraft.utils.system.IOUtils;
|
||||
public class FileDownloadTask extends Task implements PreviousResult<File>, PreviousResultRegistrar<String> {
|
||||
|
||||
// Max size of download buffer.
|
||||
private static final int MAX_BUFFER_SIZE = 2048;
|
||||
protected static final int MAX_BUFFER_SIZE = 2048;
|
||||
|
||||
private URL url; // download URL
|
||||
private int size; // size of download in bytes
|
||||
private int downloaded; // number of bytes downloaded
|
||||
private final File filePath;
|
||||
protected URL url; // download URL
|
||||
protected int downloaded = 0; // number of bytes downloaded
|
||||
protected File filePath;
|
||||
|
||||
public FileDownloadTask() {
|
||||
}
|
||||
|
||||
public FileDownloadTask(File filePath) {
|
||||
this((URL) null, filePath);
|
||||
@@ -56,8 +58,6 @@ public class FileDownloadTask extends Task implements PreviousResult<File>, Prev
|
||||
// Constructor for Download.
|
||||
public FileDownloadTask(URL url, File filePath) {
|
||||
this.url = url;
|
||||
size = -1;
|
||||
downloaded = 0;
|
||||
this.filePath = filePath;
|
||||
}
|
||||
|
||||
@@ -124,10 +124,6 @@ public class FileDownloadTask extends Task implements PreviousResult<File>, Prev
|
||||
return false;
|
||||
}
|
||||
|
||||
// Set the size for this download if it hasn't been already set.
|
||||
if (size == -1)
|
||||
size = contentLength;
|
||||
|
||||
filePath.getParentFile().mkdirs();
|
||||
|
||||
File tempFile = new File(filePath.getAbsolutePath() + ".hmd");
|
||||
@@ -135,10 +131,11 @@ public class FileDownloadTask extends Task implements PreviousResult<File>, Prev
|
||||
tempFile.createNewFile();
|
||||
|
||||
// Open file and seek to the end of it.
|
||||
file = new RandomAccessFile(tempFile, "rw");
|
||||
file.seek(downloaded);
|
||||
file = new RandomAccessFile(tempFile, "rwd");
|
||||
|
||||
stream = connection.getInputStream();
|
||||
int lastDownloaded = 0;
|
||||
long lastTime = System.currentTimeMillis();
|
||||
while (true) {
|
||||
// Size buffer according to how much of the file is left to download.
|
||||
if (!shouldContinue) {
|
||||
@@ -158,14 +155,21 @@ public class FileDownloadTask extends Task implements PreviousResult<File>, Prev
|
||||
file.write(buffer, 0, read);
|
||||
downloaded += read;
|
||||
|
||||
if (ppl != null)
|
||||
ppl.setProgress(this, downloaded, size);
|
||||
long now = System.currentTimeMillis();
|
||||
if (ppl != null && (now - lastTime) >= 1000) {
|
||||
ppl.setProgress(this, downloaded, contentLength);
|
||||
ppl.setStatus(this, (downloaded - lastDownloaded) / 1024 + "KB/s");
|
||||
lastDownloaded = downloaded;
|
||||
lastTime = now;
|
||||
}
|
||||
}
|
||||
closeFiles();
|
||||
if (aborted)
|
||||
tempFile.delete();
|
||||
else
|
||||
else {
|
||||
if (filePath.exists()) filePath.delete();
|
||||
tempFile.renameTo(filePath);
|
||||
}
|
||||
if (ppl != null)
|
||||
ppl.onProgressProviderDone(this);
|
||||
return true;
|
||||
@@ -184,6 +188,7 @@ public class FileDownloadTask extends Task implements PreviousResult<File>, Prev
|
||||
|
||||
@Override
|
||||
public boolean abort() {
|
||||
//for (Downloader d : downloaders) d.abort();
|
||||
shouldContinue = false;
|
||||
aborted = true;
|
||||
return true;
|
||||
|
||||
@@ -22,6 +22,10 @@ package org.jackhuang.hellominecraft.tasks.download;
|
||||
*/
|
||||
public class NetException extends RuntimeException {
|
||||
|
||||
public NetException(Exception message) {
|
||||
super(message);
|
||||
}
|
||||
|
||||
public NetException(String message) {
|
||||
super(message);
|
||||
}
|
||||
|
||||
@@ -19,7 +19,6 @@ package org.jackhuang.hellominecraft.utils;
|
||||
import java.util.Map;
|
||||
import org.jackhuang.hellominecraft.utils.system.MessageBox;
|
||||
import org.jackhuang.hellominecraft.C;
|
||||
import org.jackhuang.hellominecraft.utils.functions.NonConsumer;
|
||||
import org.jackhuang.hellominecraft.HMCLog;
|
||||
|
||||
/**
|
||||
@@ -30,16 +29,15 @@ public final class UpdateChecker extends Thread {
|
||||
|
||||
public static boolean OUT_DATED = false;
|
||||
public VersionNumber base;
|
||||
public String versionString;
|
||||
public String type;
|
||||
public boolean continueUpdate;
|
||||
public NonConsumer dl;
|
||||
public Map<String, String> download_link;
|
||||
public Runnable dl;
|
||||
public Map<String, String> download_link = null;
|
||||
|
||||
public UpdateChecker(VersionNumber base, String type, boolean continueUpdate, NonConsumer dl) {
|
||||
public UpdateChecker(VersionNumber base, String type, Runnable dl) {
|
||||
super("UpdateChecker");
|
||||
this.base = base;
|
||||
this.type = type;
|
||||
this.continueUpdate = continueUpdate;
|
||||
this.dl = dl;
|
||||
}
|
||||
|
||||
@@ -48,25 +46,16 @@ public final class UpdateChecker extends Thread {
|
||||
@Override
|
||||
public void run() {
|
||||
|
||||
String version;
|
||||
try {
|
||||
version = NetUtils.doGet("http://huangyuhui.duapp.com/info.php?type=" + type);
|
||||
versionString = NetUtils.doGet("http://huangyuhui.duapp.com/info.php?type=" + type);
|
||||
} catch (Exception e) {
|
||||
HMCLog.warn("Failed to get update url.", e);
|
||||
return;
|
||||
}
|
||||
value = VersionNumber.check(version);
|
||||
if (!continueUpdate)
|
||||
return;
|
||||
value = VersionNumber.check(versionString);
|
||||
process(false);
|
||||
if (OUT_DATED) {
|
||||
try {
|
||||
download_link = C.gson.fromJson(NetUtils.doGet("http://huangyuhui.duapp.com/update_link.php?type=" + type), Map.class);
|
||||
} catch (Exception e) {
|
||||
HMCLog.warn("Failed to get update link.", e);
|
||||
}
|
||||
dl.onDone();
|
||||
}
|
||||
if (OUT_DATED)
|
||||
dl.run();
|
||||
}
|
||||
|
||||
public void process(boolean showMessage) {
|
||||
@@ -74,14 +63,23 @@ public final class UpdateChecker extends Thread {
|
||||
HMCLog.warn("Failed to check update...");
|
||||
if (showMessage)
|
||||
MessageBox.Show(C.i18n("update.failed"));
|
||||
} else
|
||||
if (VersionNumber.isOlder(base, value)) {
|
||||
OUT_DATED = true;
|
||||
}
|
||||
} else if (VersionNumber.isOlder(base, value))
|
||||
OUT_DATED = true;
|
||||
}
|
||||
|
||||
public VersionNumber getNewVersion() {
|
||||
return value;
|
||||
}
|
||||
|
||||
public synchronized void requestDownloadLink(Runnable finish) {
|
||||
new Thread(() -> {
|
||||
if (download_link == null)
|
||||
try {
|
||||
download_link = C.gson.fromJson(NetUtils.doGet("http://huangyuhui.duapp.com/update_link.php?type=" + type), Map.class);
|
||||
} catch (Exception e) {
|
||||
HMCLog.warn("Failed to get update link.", e);
|
||||
}
|
||||
finish.run();
|
||||
}).start();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -344,11 +344,13 @@ public class FileUtils {
|
||||
return filename.substring(0, index);
|
||||
}
|
||||
|
||||
public static void writeQuietly(File file, CharSequence data) {
|
||||
public static boolean writeQuietly(File file, CharSequence data) {
|
||||
try {
|
||||
write(file, data);
|
||||
return true;
|
||||
} catch (IOException e) {
|
||||
HMCLog.warn("Failed to write data to file: " + file, e);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -38,23 +38,24 @@ public class JavaProcessMonitor {
|
||||
this.p = p;
|
||||
}
|
||||
|
||||
void start() {
|
||||
public void start() {
|
||||
Event<JavaProcess> event = (sender2, t) -> {
|
||||
processThreadStopped((ProcessThread) sender2, false);
|
||||
return true;
|
||||
};
|
||||
ProcessThread a = new ProcessThread(p, true, true);
|
||||
a.stopEvent.register((sender3, p1) -> {
|
||||
Event<JavaProcess> event2 = (sender3, p1) -> {
|
||||
if (p1.getExitCode() != 0 && p1.getStdErrLines().size() > 0 && StrUtils.containsOne(p1.getStdErrLines(), Arrays.asList("Could not create the Java Virtual Machine.",
|
||||
"Error occurred during initialization of VM",
|
||||
"A fatal exception has occurred. Program will exit."))) MessageBox.Show(C.i18n("launch.cannot_create_jvm"));
|
||||
processThreadStopped((ProcessThread) sender3, false);
|
||||
return true;
|
||||
});
|
||||
};
|
||||
ProcessThread a = new ProcessThread(p, true, true);
|
||||
a.stopEvent.register(event2);
|
||||
a.start();
|
||||
al.add(a);
|
||||
a = new ProcessThread(p, false, true);
|
||||
a.stopEvent.register(event);
|
||||
a.stopEvent.register(event2);
|
||||
a.start();
|
||||
al.add(a);
|
||||
a = new ProcessThread(p, false, false);
|
||||
|
||||
@@ -14,13 +14,33 @@
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program.
|
||||
*/
|
||||
package org.jackhuang.hellominecraft.utils.functions;
|
||||
package org.jackhuang.hellominecraft.utils.system;
|
||||
|
||||
import org.jackhuang.hellominecraft.utils.functions.Consumer;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author huangyuhui
|
||||
*/
|
||||
public interface NonConsumer {
|
||||
public class ThreadExecutor extends Thread {
|
||||
|
||||
public final Consumer<Throwable> c;
|
||||
public final Runnable r;
|
||||
|
||||
public ThreadExecutor(Consumer<Throwable> c, Runnable r) {
|
||||
super();
|
||||
this.c = c;
|
||||
this.r = r;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
r.run();
|
||||
c.accept(null);
|
||||
} catch (Throwable t) {
|
||||
c.accept(t);
|
||||
}
|
||||
}
|
||||
|
||||
void onDone();
|
||||
}
|
||||
@@ -17,7 +17,6 @@
|
||||
package org.jackhuang.hellominecraft.views;
|
||||
|
||||
import org.jackhuang.hellominecraft.C;
|
||||
import org.jackhuang.hellominecraft.utils.functions.NonConsumer;
|
||||
import org.jackhuang.hellominecraft.utils.functions.NonFunction;
|
||||
import org.jackhuang.hellominecraft.utils.DoubleOutputStream;
|
||||
import org.jackhuang.hellominecraft.utils.LauncherPrintStream;
|
||||
@@ -33,7 +32,7 @@ public class LogWindow extends javax.swing.JFrame {
|
||||
|
||||
boolean movingEnd;
|
||||
NonFunction<Boolean> listener;
|
||||
NonConsumer terminateGameListener;
|
||||
Runnable terminateGameListener;
|
||||
|
||||
/**
|
||||
* Creates new form LogWindow
|
||||
@@ -226,7 +225,7 @@ public class LogWindow extends javax.swing.JFrame {
|
||||
|
||||
private void btnTerminateGameActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnTerminateGameActionPerformed
|
||||
if (terminateGameListener != null)
|
||||
terminateGameListener.onDone();
|
||||
terminateGameListener.run();
|
||||
}//GEN-LAST:event_btnTerminateGameActionPerformed
|
||||
|
||||
private void btnGitHubActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnGitHubActionPerformed
|
||||
@@ -253,7 +252,7 @@ public class LogWindow extends javax.swing.JFrame {
|
||||
this.listener = exit;
|
||||
}
|
||||
|
||||
public void setTerminateGame(NonConsumer l) {
|
||||
public void setTerminateGame(Runnable l) {
|
||||
this.terminateGameListener = l;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user