Supported 16w05a version json
This commit is contained in:
@@ -151,4 +151,11 @@ public final class Utils {
|
||||
if (o == null)
|
||||
throw new NullPointerException("Oh dear, there is a problem...");
|
||||
}
|
||||
|
||||
public static Object firstNonNull(Object... o) {
|
||||
for (Object s : o)
|
||||
if (s != null)
|
||||
return s;
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -17,6 +17,8 @@
|
||||
*/
|
||||
package org.jackhuang.hellominecraft.util.tasks;
|
||||
|
||||
import java.util.Collection;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author huangyuhui
|
||||
@@ -28,14 +30,14 @@ public interface DoingDoneListener<K> {
|
||||
*
|
||||
* @param k
|
||||
*/
|
||||
void onDone(K k);
|
||||
void onDone(K k, Collection<Task> tasks);
|
||||
|
||||
/**
|
||||
* Before task executing.
|
||||
*
|
||||
* @param k
|
||||
*/
|
||||
void onDoing(K k);
|
||||
void onDoing(K k, Collection<Task> tasks);
|
||||
|
||||
/**
|
||||
* Task failed.
|
||||
|
||||
@@ -87,11 +87,11 @@ public abstract class Task {
|
||||
|
||||
public abstract String getInfo();
|
||||
|
||||
public Collection<? extends Task> getDependTasks() {
|
||||
public Collection<Task> getDependTasks() {
|
||||
return null;
|
||||
}
|
||||
|
||||
public Collection<? extends Task> getAfterTasks() {
|
||||
public Collection<Task> getAfterTasks() {
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
@@ -88,7 +88,7 @@ public class TaskList extends Thread {
|
||||
static final Set<Task> TASK_POOL = Collections.synchronizedSet(new HashSet<Task>());
|
||||
|
||||
private void processTasks(Collection<? extends Task> c) {
|
||||
if (c == null)
|
||||
if (c == null || c.isEmpty())
|
||||
return;
|
||||
this.totTask += c.size();
|
||||
Set<InvokeThread> runningThread = Collections.synchronizedSet(new HashSet<InvokeThread>());
|
||||
@@ -112,13 +112,16 @@ public class TaskList extends Thread {
|
||||
private void executeTask(Task t) {
|
||||
if (!shouldContinue || t == null)
|
||||
return;
|
||||
processTasks(t.getDependTasks());
|
||||
|
||||
Collection<Task> c = t.getDependTasks();
|
||||
if (c == null)
|
||||
c = new HashSet<>();
|
||||
HMCLog.log("Executing task: " + t.getInfo());
|
||||
for (DoingDoneListener<Task> d : taskListener)
|
||||
d.onDoing(t);
|
||||
d.onDoing(t, c);
|
||||
for (DoingDoneListener<Task> d : t.getTaskListeners())
|
||||
d.onDoing(t);
|
||||
d.onDoing(t, c);
|
||||
processTasks(c);
|
||||
|
||||
boolean flag = true;
|
||||
try {
|
||||
@@ -129,11 +132,14 @@ public class TaskList extends Thread {
|
||||
}
|
||||
if (flag) {
|
||||
HMCLog.log((t.isAborted() ? "Task aborted: " : "Task finished: ") + t.getInfo());
|
||||
Collection<Task> at = t.getAfterTasks();
|
||||
if (at == null)
|
||||
at = new HashSet<>();
|
||||
for (DoingDoneListener<Task> d : taskListener)
|
||||
d.onDone(t);
|
||||
d.onDone(t, at);
|
||||
for (DoingDoneListener<Task> d : t.getTaskListeners())
|
||||
d.onDone(t);
|
||||
processTasks(t.getAfterTasks());
|
||||
d.onDone(t, at);
|
||||
processTasks(at);
|
||||
} else {
|
||||
HMCLog.err("Task failed: " + t.getInfo(), t.getFailReason());
|
||||
for (DoingDoneListener<Task> d : taskListener)
|
||||
|
||||
@@ -18,6 +18,7 @@
|
||||
package org.jackhuang.hellominecraft.util.tasks;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.LinkedList;
|
||||
import javax.swing.SwingUtilities;
|
||||
import org.jackhuang.hellominecraft.util.C;
|
||||
@@ -35,12 +36,12 @@ public class TaskWindow extends javax.swing.JDialog
|
||||
|
||||
private static final TaskWindow INSTANCE = new TaskWindow();
|
||||
|
||||
private static TaskWindow inst() {
|
||||
private static TaskWindow instance() {
|
||||
INSTANCE.clean();
|
||||
return INSTANCE;
|
||||
}
|
||||
|
||||
public static TaskWindowFactory getInstance() {
|
||||
public static TaskWindowFactory factory() {
|
||||
return new TaskWindowFactory();
|
||||
}
|
||||
|
||||
@@ -225,7 +226,7 @@ public class TaskWindow extends javax.swing.JDialog
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDoing(Task task) {
|
||||
public void onDoing(Task task, Collection<Task> taskCollection) {
|
||||
if (task == null)
|
||||
return;
|
||||
task.setProgressProviderListener(this);
|
||||
@@ -245,7 +246,7 @@ public class TaskWindow extends javax.swing.JDialog
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDone(Task task) {
|
||||
public void onDone(Task task, Collection<Task> taskCollection) {
|
||||
SwingUtilities.invokeLater(() -> {
|
||||
if (taskList == null || task == null)
|
||||
return;
|
||||
@@ -299,18 +300,18 @@ public class TaskWindow extends javax.swing.JDialog
|
||||
LinkedList<Task> ll = new LinkedList<>();
|
||||
boolean flag;
|
||||
|
||||
public TaskWindowFactory addTask(Task t) {
|
||||
public TaskWindowFactory append(Task t) {
|
||||
ll.add(t);
|
||||
return this;
|
||||
}
|
||||
|
||||
public boolean start() {
|
||||
public boolean create() {
|
||||
String stacktrace = StrUtils.getStackTrace(new Throwable());
|
||||
return SwingUtils.invokeAndWait(() -> {
|
||||
synchronized (INSTANCE) {
|
||||
if (INSTANCE.isVisible())
|
||||
return false;
|
||||
TaskWindow tw = inst();
|
||||
TaskWindow tw = instance();
|
||||
for (Task t : ll)
|
||||
tw.addTask(t);
|
||||
tw.lastStackTrace = tw.stackTrace;
|
||||
|
||||
@@ -21,10 +21,13 @@ import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.RandomAccessFile;
|
||||
import java.math.BigInteger;
|
||||
import java.net.HttpURLConnection;
|
||||
import java.net.URL;
|
||||
import java.security.MessageDigest;
|
||||
import java.util.ArrayList;
|
||||
import org.jackhuang.hellominecraft.util.C;
|
||||
import org.jackhuang.hellominecraft.util.code.DigestUtils;
|
||||
import org.jackhuang.hellominecraft.util.logging.HMCLog;
|
||||
import org.jackhuang.hellominecraft.util.tasks.Task;
|
||||
import org.jackhuang.hellominecraft.util.tasks.communication.PreviousResult;
|
||||
@@ -44,6 +47,7 @@ public class FileDownloadTask extends Task implements PreviousResult<File>, Prev
|
||||
protected URL url; // download URL
|
||||
protected int downloaded = 0; // number of bytes downloaded
|
||||
protected File filePath;
|
||||
protected String expectedHash;
|
||||
|
||||
public FileDownloadTask() {
|
||||
}
|
||||
@@ -56,10 +60,18 @@ public class FileDownloadTask extends Task implements PreviousResult<File>, Prev
|
||||
this(IOUtils.parseURL(url), filePath);
|
||||
}
|
||||
|
||||
// Constructor for Download.
|
||||
public FileDownloadTask(URL url, File filePath) {
|
||||
this(url, filePath, null);
|
||||
}
|
||||
|
||||
public FileDownloadTask(String url, File filePath, String hash) {
|
||||
this(IOUtils.parseURL(url), filePath, hash);
|
||||
}
|
||||
|
||||
public FileDownloadTask(URL url, File file, String hash) {
|
||||
this.url = url;
|
||||
this.filePath = filePath;
|
||||
this.filePath = file;
|
||||
this.expectedHash = hash;
|
||||
}
|
||||
|
||||
// Get this download's URL.
|
||||
@@ -130,6 +142,8 @@ public class FileDownloadTask extends Task implements PreviousResult<File>, Prev
|
||||
// Open file and seek to the end of it.
|
||||
file = new RandomAccessFile(tempFile, "rw");
|
||||
|
||||
MessageDigest digest = DigestUtils.getSha1Digest();
|
||||
|
||||
stream = connection.getInputStream();
|
||||
int lastDownloaded = 0;
|
||||
downloaded = 0;
|
||||
@@ -149,6 +163,8 @@ public class FileDownloadTask extends Task implements PreviousResult<File>, Prev
|
||||
if (read == -1)
|
||||
break;
|
||||
|
||||
digest.update(buffer, 0, read);
|
||||
|
||||
// Write buffer to file.
|
||||
file.write(buffer, 0, read);
|
||||
downloaded += read;
|
||||
@@ -169,6 +185,10 @@ public class FileDownloadTask extends Task implements PreviousResult<File>, Prev
|
||||
filePath.delete();
|
||||
tempFile.renameTo(filePath);
|
||||
}
|
||||
String hashCode = String.format("%1$040x", new Object[] { new BigInteger(1, digest.digest()) });
|
||||
if (expectedHash != null && !expectedHash.equals(hashCode))
|
||||
throw new IllegalStateException("Unexpected hash code: " + hashCode + ", expected: " + expectedHash);
|
||||
|
||||
if (ppl != null)
|
||||
ppl.onProgressProviderDone(this);
|
||||
return;
|
||||
|
||||
Reference in New Issue
Block a user