Fix JsonParseException

This commit is contained in:
huanghongxun
2018-09-26 13:13:21 +08:00
parent 01ff1327f7
commit 518969d2f2
7 changed files with 19 additions and 17 deletions

View File

@@ -18,6 +18,7 @@
package org.jackhuang.hmcl.upgrade; package org.jackhuang.hmcl.upgrade;
import com.google.gson.Gson; import com.google.gson.Gson;
import com.google.gson.JsonParseException;
import javafx.application.Platform; import javafx.application.Platform;
import javafx.scene.layout.Region; import javafx.scene.layout.Region;
import org.jackhuang.hmcl.Launcher; import org.jackhuang.hmcl.Launcher;
@@ -239,18 +240,20 @@ public final class UpdateHandler {
} }
private static void breakForceUpdateFeature() { private static void breakForceUpdateFeature() {
try { Path hmclVersionJson = Launcher.HMCL_DIRECTORY.toPath().resolve("hmclver.json");
Path hmclVersionJson = Launcher.HMCL_DIRECTORY.toPath().resolve("hmclver.json"); if (Files.isRegularFile(hmclVersionJson)) {
if (Files.isRegularFile(hmclVersionJson)) { try {
Map<?, ?> content = new Gson().fromJson(new String(Files.readAllBytes(hmclVersionJson), UTF_8), Map.class); Map<?, ?> content = new Gson().fromJson(new String(Files.readAllBytes(hmclVersionJson), UTF_8), Map.class);
Object ver = content.get("ver"); Object ver = content.get("ver");
if (ver instanceof String && ((String) ver).startsWith("3.")) { if (ver instanceof String && ((String) ver).startsWith("3.")) {
Files.delete(hmclVersionJson); Files.delete(hmclVersionJson);
LOG.info("Successfully broke the force update feature"); LOG.info("Successfully broke the force update feature");
} }
} catch (IOException e) {
LOG.log(Level.WARNING, "Failed to break the force update feature", e);
} catch (JsonParseException e) {
hmclVersionJson.toFile().delete();
} }
} catch (IOException e) {
LOG.log(Level.WARNING, "Failed to break the force update feature", e);
} }
} }
// ==== // ====

View File

@@ -17,6 +17,7 @@
*/ */
package org.jackhuang.hmcl.download; package org.jackhuang.hmcl.download;
import com.google.gson.JsonParseException;
import org.jackhuang.hmcl.download.game.LibraryDownloadTask; import org.jackhuang.hmcl.download.game.LibraryDownloadTask;
import org.jackhuang.hmcl.game.Library; import org.jackhuang.hmcl.game.Library;
import org.jackhuang.hmcl.game.LibraryDownloadInfo; import org.jackhuang.hmcl.game.LibraryDownloadInfo;
@@ -62,7 +63,7 @@ public class DefaultCacheRepository extends CacheRepository {
index = JsonUtils.GSON.fromJson(FileUtils.readText(indexFile.toFile()), Index.class); index = JsonUtils.GSON.fromJson(FileUtils.readText(indexFile.toFile()), Index.class);
else else
index = new Index(); index = new Index();
} catch (IOException e) { } catch (IOException | JsonParseException e) {
Logging.LOG.log(Level.WARNING, "Unable to read index file", e); Logging.LOG.log(Level.WARNING, "Unable to read index file", e);
index = new Index(); index = new Index();
} finally { } finally {

View File

@@ -89,9 +89,7 @@ public final class ForgeInstallTask extends TaskResult<Version> {
if (stream == null) if (stream == null)
throw new IOException("Malformed forge installer file, install_profile.json does not exist."); throw new IOException("Malformed forge installer file, install_profile.json does not exist.");
String json = IOUtils.readFullyAsString(stream); String json = IOUtils.readFullyAsString(stream);
ForgeInstallProfile installProfile = JsonUtils.GSON.fromJson(json, ForgeInstallProfile.class); ForgeInstallProfile installProfile = JsonUtils.fromNonNullJson(json, ForgeInstallProfile.class);
if (installProfile == null)
throw new IOException("Malformed forge installer file, install_profile.json does not exist.");
// unpack the universal jar in the installer file. // unpack the universal jar in the installer file.
Library forgeLibrary = Library.fromName(installProfile.getInstall().getPath()); Library forgeLibrary = Library.fromName(installProfile.getInstall().getPath());

View File

@@ -18,7 +18,6 @@
package org.jackhuang.hmcl.game; package org.jackhuang.hmcl.game;
import com.google.gson.JsonParseException; import com.google.gson.JsonParseException;
import com.google.gson.JsonSyntaxException;
import org.jackhuang.hmcl.event.*; import org.jackhuang.hmcl.event.*;
import org.jackhuang.hmcl.task.Schedulers; import org.jackhuang.hmcl.task.Schedulers;
import org.jackhuang.hmcl.util.Logging; import org.jackhuang.hmcl.util.Logging;
@@ -108,11 +107,11 @@ public class DefaultGameRepository implements GameRepository {
return new File(getVersionRoot(id), id + ".json"); return new File(getVersionRoot(id), id + ".json");
} }
public Version readVersionJson(String id) throws IOException, JsonSyntaxException { public Version readVersionJson(String id) throws IOException, JsonParseException {
return readVersionJson(getVersionJson(id)); return readVersionJson(getVersionJson(id));
} }
public Version readVersionJson(File file) throws IOException, JsonSyntaxException { public Version readVersionJson(File file) throws IOException, JsonParseException {
return JsonUtils.GSON.fromJson(FileUtils.readText(file), Version.class); return JsonUtils.GSON.fromJson(FileUtils.readText(file), Version.class);
} }

View File

@@ -112,7 +112,7 @@ public class Datapack {
try { try {
PackMcMeta pack = JsonUtils.fromNonNullJson(FileUtils.readText(mcmeta), PackMcMeta.class); PackMcMeta pack = JsonUtils.fromNonNullJson(FileUtils.readText(mcmeta), PackMcMeta.class);
info.add(new Pack(path, FileUtils.getNameWithoutExtension(path), pack.getPackInfo().getDescription(), this)); info.add(new Pack(path, FileUtils.getNameWithoutExtension(path), pack.getPackInfo().getDescription(), this));
} catch (IOException e) { } catch (IOException | JsonParseException e) {
Logging.LOG.log(Level.WARNING, "Failed to read datapack " + path, e); Logging.LOG.log(Level.WARNING, "Failed to read datapack " + path, e);
} }
} else { } else {
@@ -147,7 +147,7 @@ public class Datapack {
PackMcMeta pack = enabled ? JsonUtils.fromNonNullJson(FileUtils.readText(mcmeta), PackMcMeta.class) PackMcMeta pack = enabled ? JsonUtils.fromNonNullJson(FileUtils.readText(mcmeta), PackMcMeta.class)
: JsonUtils.fromNonNullJson(FileUtils.readText(mcmetaDisabled), PackMcMeta.class); : JsonUtils.fromNonNullJson(FileUtils.readText(mcmetaDisabled), PackMcMeta.class);
info.add(new Pack(enabled ? mcmeta : mcmetaDisabled, FileUtils.getName(subDir), pack.getPackInfo().getDescription(), this)); info.add(new Pack(enabled ? mcmeta : mcmetaDisabled, FileUtils.getName(subDir), pack.getPackInfo().getDescription(), this));
} catch (IOException e) { } catch (IOException | JsonParseException e) {
Logging.LOG.log(Level.WARNING, "Failed to read datapack " + subDir, e); Logging.LOG.log(Level.WARNING, "Failed to read datapack " + subDir, e);
} }
} else if (Files.isRegularFile(subDir)) { } else if (Files.isRegularFile(subDir)) {
@@ -169,7 +169,7 @@ public class Datapack {
PackMcMeta pack = JsonUtils.fromNonNullJson(FileUtils.readText(mcmeta), PackMcMeta.class); PackMcMeta pack = JsonUtils.fromNonNullJson(FileUtils.readText(mcmeta), PackMcMeta.class);
info.add(new Pack(subDir, name, pack.getPackInfo().getDescription(), this)); info.add(new Pack(subDir, name, pack.getPackInfo().getDescription(), this));
} catch (IOException e) { } catch (IOException | JsonParseException e) {
Logging.LOG.log(Level.WARNING, "Failed to read datapack " + subDir, e); Logging.LOG.log(Level.WARNING, "Failed to read datapack " + subDir, e);
} }
} }

View File

@@ -63,8 +63,8 @@ public final class MultiMCManifest {
String json = IOUtils.readFullyAsString(Files.newInputStream(mmcPack)); String json = IOUtils.readFullyAsString(Files.newInputStream(mmcPack));
MultiMCManifest manifest = JsonUtils.fromNonNullJson(json, MultiMCManifest.class); MultiMCManifest manifest = JsonUtils.fromNonNullJson(json, MultiMCManifest.class);
if (manifest != null && manifest.getComponents() == null) if (manifest.getComponents() == null)
throw new IOException("mmc-pack.json malformed."); throw new IOException("mmc-pack.json malformed.");
return manifest; return manifest;
} }

View File

@@ -125,6 +125,7 @@ public final class MultiMCModpackInstallTask extends Task {
if (Files.exists(patches)) if (Files.exists(patches))
for (Path patchJson : Files.newDirectoryStream(patches)) { for (Path patchJson : Files.newDirectoryStream(patches)) {
if (patchJson.toString().endsWith(".json")) { if (patchJson.toString().endsWith(".json")) {
// If json is malformed, we should stop installing this modpack instead of skipping it.
MultiMCInstancePatch patch = JsonUtils.GSON.fromJson(IOUtils.readFullyAsString(Files.newInputStream(patchJson)), MultiMCInstancePatch.class); MultiMCInstancePatch patch = JsonUtils.GSON.fromJson(IOUtils.readFullyAsString(Files.newInputStream(patchJson)), MultiMCInstancePatch.class);
List<String> newArguments = new LinkedList<>(); List<String> newArguments = new LinkedList<>();
for (String arg : patch.getTweakers()) { for (String arg : patch.getTweakers()) {