add: auto fix malformed asset index file

This commit is contained in:
huanghongxun
2020-02-02 23:35:47 +08:00
parent 1d579c52eb
commit 43428c054a
3 changed files with 34 additions and 2 deletions

View File

@@ -17,6 +17,7 @@
*/
package org.jackhuang.hmcl.download.game;
import com.google.gson.JsonSyntaxException;
import org.jackhuang.hmcl.download.AbstractDependencyManager;
import org.jackhuang.hmcl.game.AssetIndex;
import org.jackhuang.hmcl.game.AssetIndexInfo;
@@ -46,6 +47,8 @@ public final class GameAssetDownloadTask extends Task<Void> {
private final File assetIndexFile;
private final List<Task<?>> dependents = new LinkedList<>();
private final List<Task<?>> dependencies = new LinkedList<>();
private AssetIndex index;
private boolean retry = false;
/**
* Constructor.
@@ -72,10 +75,32 @@ public final class GameAssetDownloadTask extends Task<Void> {
public Collection<Task<?>> getDependencies() {
return dependencies;
}
@Override
public boolean doPreExecute() {
return true;
}
@Override
public void preExecute() throws Exception {
try {
index = JsonUtils.GSON.fromJson(FileUtils.readText(assetIndexFile), AssetIndex.class);
} catch (JsonSyntaxException e) {
dependents.add(new GameAssetIndexDownloadTask(dependencyManager, this.version));
retry = true;
}
}
@Override
public void execute() throws Exception {
AssetIndex index = JsonUtils.GSON.fromJson(FileUtils.readText(assetIndexFile), AssetIndex.class);
if (retry) {
try {
index = JsonUtils.GSON.fromJson(FileUtils.readText(assetIndexFile), AssetIndex.class);
} catch (JsonSyntaxException e) {
throw new GameAssetIndexDownloadTask.GameAssetIndexMalformedException();
}
}
int progress = 0;
if (index != null)
for (AssetObject assetObject : index.getObjects().values()) {

View File

@@ -25,6 +25,7 @@ import org.jackhuang.hmcl.task.Task;
import org.jackhuang.hmcl.util.io.NetworkUtils;
import java.io.File;
import java.io.IOException;
import java.util.LinkedList;
import java.util.List;
@@ -69,4 +70,7 @@ public final class GameAssetIndexDownloadTask extends Task<Void> {
).setCacheRepository(dependencyManager.getCacheRepository()));
}
public static class GameAssetIndexMalformedException extends IOException {
}
}