Skip to content

Commit

Permalink
Fixed #42
Browse files Browse the repository at this point in the history
  • Loading branch information
huanghongxun committed Mar 3, 2016
1 parent 5dc8120 commit afde411
Show file tree
Hide file tree
Showing 8 changed files with 89 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashSet;
import java.util.Map;
import org.jackhuang.hellominecraft.util.C;
Expand All @@ -30,8 +32,8 @@
import org.jackhuang.hellominecraft.util.StrUtils;
import org.jackhuang.hellominecraft.launcher.core.download.IDownloadProvider;
import org.jackhuang.hellominecraft.launcher.core.version.MinecraftVersion;
import org.jackhuang.hellominecraft.util.OverridableSwingWorker;
import org.jackhuang.hellominecraft.util.VersionNumber;
import org.jackhuang.hellominecraft.util.tasks.TaskInfo;

/**
*
Expand All @@ -44,19 +46,26 @@ public AssetsMojangLoader(String name) {
}

@Override
public OverridableSwingWorker<String[]> getList(final MinecraftVersion mv, final IMinecraftAssetService mp) {
return new OverridableSwingWorker<String[]>() {
public Task getList(final MinecraftVersion mv, final IMinecraftAssetService mp) {
if (mv == null)
throw new IllegalArgumentException("AssetsMojangLoader: null argument: MinecraftVersion");
String assetsId = mv.getAssetsIndex().getId();
File assets = mp.getAssets();
HMCLog.log("Gathering asset index: " + assetsId);
File f = IOUtils.tryGetCanonicalFile(new File(assets, "indexes/" + assetsId + ".json"));
return new TaskInfo("Gather asset index") {
@Override
protected void work() throws Exception {
if (mv == null)
throw new IllegalArgumentException("AssetsMojangLoader: null argument: MinecraftVersion");
String assetsId = mv.getAssetsIndex().getId();
File assets = mp.getAssets();
HMCLog.log("Gathering asset index: " + assetsId);
File f = IOUtils.tryGetCanonicalFile(new File(assets, "indexes/" + assetsId + ".json"));
if (!f.exists() && !mp.downloadMinecraftAssetsIndex(mv.getAssetsIndex()))
throw new IllegalStateException("Failed to get index json");
public Collection<Task> getDependTasks() {
if (!f.exists())
return Arrays.asList(mp.downloadMinecraftAssetsIndex(mv.getAssetsIndex()));
else
return null;
}

@Override
public void executeTask() throws Throwable {
if (!areDependTasksSucceeded)
throw new IllegalStateException("Failed to get asset index");
String result = FileUtils.readFileToString(f);
if (StrUtils.isBlank(result))
throw new IllegalStateException("Index json is empty, please redownload it!");
Expand All @@ -66,6 +75,7 @@ protected void work() throws Exception {
ArrayList<String> al = new ArrayList<>();
contents = new ArrayList<>();
HashSet<String> loadedHashes = new HashSet<>();
int pgs = 0;
if (o != null && o.getFileMap() != null)
for (Map.Entry<String, AssetsObject> e : o.getFileMap().entrySet()) {
if (loadedHashes.contains(e.getValue().getHash()))
Expand All @@ -79,9 +89,9 @@ protected void work() throws Exception {
assetsDownloadURLs.add(c.key);
assetsLocalNames.add(new File(assets, "objects" + File.separator + c.key.replace("/", File.separator)));
al.add(e.getKey());
if (ppl != null)
ppl.setProgress(this, ++pgs, o.getFileMap().size());
}

publish(al.toArray(new String[1]));
}
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public String getName() {
* @param mp Asset Service
* @param x finished event
*/
public abstract OverridableSwingWorker<String[]> getList(MinecraftVersion mv, IMinecraftAssetService mp);
public abstract Task getList(MinecraftVersion mv, IMinecraftAssetService mp);

/**
* Will be invoked when the user invoked "Download all assets".
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import com.google.gson.JsonSyntaxException;
import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashSet;
import java.util.Map;
Expand Down Expand Up @@ -58,15 +59,17 @@ public Task downloadAssets(final MinecraftVersion mv) {
Collection<Task> afters = new HashSet<>();

@Override
public void executeTask() throws Throwable {
IAssetsHandler type = IAssetsHandler.ASSETS_HANDLER;
type.getList(mv, service.asset()).justDo();
afters.add(type.getDownloadTask(service.getDownloadType().getProvider()));
public Collection<Task> getDependTasks() {
return Arrays.asList(IAssetsHandler.ASSETS_HANDLER.getList(mv, service.asset()));
}

@Override
public void executeTask() {
}

@Override
public Collection<Task> getAfterTasks() {
return afters;
return Arrays.asList(IAssetsHandler.ASSETS_HANDLER.getDownloadTask(service.getDownloadType().getProvider()));
}
};
}
Expand All @@ -80,7 +83,7 @@ public boolean refreshAssetsIndex(String id) {
}

@Override
public boolean downloadMinecraftAssetsIndex(AssetIndexDownloadInfo assets) {
public Task downloadMinecraftAssetsIndex(AssetIndexDownloadInfo assets) {
File assetsLocation = getAssets();
if (!assetsLocation.exists() && !assetsLocation.mkdirs())
HMCLog.warn("Failed to make directories: " + assetsLocation);
Expand All @@ -91,14 +94,22 @@ public boolean downloadMinecraftAssetsIndex(AssetIndexDownloadInfo assets) {
if (assetsIndex.renameTo(renamed))
HMCLog.warn("Failed to rename " + assetsIndex + " to " + renamed);
}
if (new FileDownloadTask(assets.getUrl(service.getDownloadType()), IOUtils.tryGetCanonicalFile(assetsIndex), assets.sha1).setTag(assets.getId() + ".json").run()) {
if (renamed != null && !renamed.delete())
HMCLog.warn("Failed to delete " + renamed + ", maybe you should do it.");
return true;
}
if (renamed != null && !renamed.renameTo(assetsIndex))
HMCLog.warn("Failed to rename " + renamed + " to " + assetsIndex);
return false;
File renamedFinal = renamed;
return new TaskInfo("Download Asset Index") {
@Override
public Collection<Task> getDependTasks() {
return Arrays.asList(new FileDownloadTask(assets.getUrl(service.getDownloadType()), IOUtils.tryGetCanonicalFile(assetsIndex), assets.sha1).setTag(assets.getId() + ".json"));
}

@Override
public void executeTask() throws Throwable {
if (areDependTasksSucceeded) {
if (renamedFinal != null && !renamedFinal.delete())
HMCLog.warn("Failed to delete " + renamedFinal + ", maybe you should do it.");
} else if (renamedFinal != null && !renamedFinal.renameTo(assetsIndex))
HMCLog.warn("Failed to rename " + renamedFinal + " to " + assetsIndex);
}
};
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public IMinecraftAssetService(IMinecraftService service) {

public abstract boolean downloadMinecraftAssetsIndexAsync(AssetIndexDownloadInfo assetsId);

public abstract boolean downloadMinecraftAssetsIndex(AssetIndexDownloadInfo assetsId);
public abstract Task downloadMinecraftAssetsIndex(AssetIndexDownloadInfo assetsId);

public abstract File getAssetObject(String assetVersion, String name) throws IOException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public Profile(String name, Profile v) {
this();
if (v == null)
return;
name = v.name;
this.name = name;
gameDir = v.gameDir;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
import org.jackhuang.hellominecraft.util.system.IOUtils;
import org.jackhuang.hellominecraft.util.ui.SwingUtils;
import org.jackhuang.hellominecraft.util.system.Java;
import org.jackhuang.hellominecraft.util.tasks.TaskWindow;
import org.jackhuang.hellominecraft.util.ui.LogWindow;

/**
Expand Down Expand Up @@ -975,7 +976,7 @@ private void btnModifyMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:e

private void btnDownloadAllAssetsActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDownloadAllAssetsActionPerformed
if (mcVersion != null)
Settings.getLastProfile().service().asset().downloadAssets(mcVersion).run();
TaskWindow.execute(Settings.getLastProfile().service().asset().downloadAssets(mcVersion));
}//GEN-LAST:event_btnDownloadAllAssetsActionPerformed

private void txtGameDirFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_txtGameDirFocusLost
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,8 @@ public Collection<Task> getAfterTasks() {
return null;
}

public boolean areDependTasksSucceeded;

protected ProgressProviderListener ppl;

public Task setProgressProviderListener(ProgressProviderListener p) {
Expand All @@ -110,9 +112,21 @@ public Task before(Task t) {
return new DoubleTask(t, this);
}

public void runWithException() throws Throwable {
Collection<Task> c = getDependTasks();
if (c != null)
for (Task t : c)
t.runWithException();
executeTask();
c = getAfterTasks();
if (c != null)
for (Task t : c)
t.runWithException();
}

public boolean run() {
try {
executeTask();
runWithException();
return true;
} catch (Throwable t) {
HMCLog.err("Failed to execute task", t);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.atomic.AtomicBoolean;
import org.jackhuang.hellominecraft.util.logging.HMCLog;

/**
Expand Down Expand Up @@ -72,15 +73,18 @@ private class Invoker implements Runnable {

Task task;
Set<Invoker> s;
AtomicBoolean bool;

public Invoker(Task task, Set<Invoker> ss) {
public Invoker(Task task, Set<Invoker> ss, AtomicBoolean bool) {
this.task = task;
s = ss;
this.bool = bool;
}

@Override
public void run() {
executeTask(task);
if (!executeTask(task))
bool.set(false);
s.remove(this);
}

Expand All @@ -90,32 +94,35 @@ public void run() {
HashMap<Invoker, Future<?>> futures = new HashMap<>();
HashSet<Invoker> invokers = new HashSet<>();

private void processTasks(Collection<? extends Task> c) {
private boolean processTasks(Collection<? extends Task> c) {
if (c == null || c.isEmpty())
return;
return true;
this.totTask += c.size();
AtomicBoolean bool = new AtomicBoolean(true);
Set<Invoker> runningThread = Collections.synchronizedSet(new HashSet<Invoker>());
for (Task t2 : c) {
t2.setParallelExecuting(true);
Invoker thread = new Invoker(t2, runningThread);
Invoker thread = new Invoker(t2, runningThread, bool);
runningThread.add(thread);
invokers.add(thread);
if (!EXECUTOR_SERVICE.isTerminated())
if (!EXECUTOR_SERVICE.isShutdown() && !EXECUTOR_SERVICE.isTerminated())
futures.put(thread, EXECUTOR_SERVICE.submit(thread));
}
while (!runningThread.isEmpty())
try {
if (this.isInterrupted())
return;
return false;
Thread.sleep(1);
} catch (InterruptedException ignore) {
}

return bool.get();
}

private void executeTask(Task t) {
if (!shouldContinue || t == null)
return;
private boolean executeTask(Task t) {
if (!shouldContinue)
return false;
if (t == null)
return true;

Collection<Task> c = t.getDependTasks();
if (c == null)
Expand All @@ -125,7 +132,7 @@ private void executeTask(Task t) {
d.onDoing(t, c);
for (DoingDoneListener<Task> d : taskListener)
d.onDoing(t, c);
processTasks(c);
t.areDependTasksSucceeded = processTasks(c);

boolean flag = true;
try {
Expand All @@ -151,6 +158,7 @@ private void executeTask(Task t) {
for (DoingDoneListener<Task> d : t.getTaskListeners())
d.onFailed(t);
}
return flag;
}

@Override
Expand Down

0 comments on commit afde411

Please sign in to comment.