Move GSON const to JsonUtils

This commit is contained in:
yushijinhun
2018-09-22 12:37:03 +08:00
parent 19632ac162
commit b33e014f71
23 changed files with 84 additions and 81 deletions

View File

@@ -19,8 +19,8 @@ package org.jackhuang.hmcl.game;
import org.jackhuang.hmcl.mod.Modpack; import org.jackhuang.hmcl.mod.Modpack;
import org.jackhuang.hmcl.task.Task; import org.jackhuang.hmcl.task.Task;
import org.jackhuang.hmcl.util.Constants;
import org.jackhuang.hmcl.util.Logging; import org.jackhuang.hmcl.util.Logging;
import org.jackhuang.hmcl.util.gson.JsonUtils;
import org.jackhuang.hmcl.util.io.Zipper; import org.jackhuang.hmcl.util.io.Zipper;
import java.io.File; import java.io.File;
@@ -73,8 +73,8 @@ public class HMCLModpackExportTask extends Task {
Version mv = repository.getResolvedVersion(version); Version mv = repository.getResolvedVersion(version);
String gameVersion = GameVersion.minecraftVersion(repository.getVersionJar(version)) String gameVersion = GameVersion.minecraftVersion(repository.getVersionJar(version))
.orElseThrow(() -> new IllegalStateException("Cannot parse the version of " + version)); .orElseThrow(() -> new IllegalStateException("Cannot parse the version of " + version));
zip.putTextFile(Constants.GSON.toJson(mv.setJar(gameVersion)), "minecraft/pack.json"); // Making "jar" to gameVersion is to be compatible with old HMCL. zip.putTextFile(JsonUtils.GSON.toJson(mv.setJar(gameVersion)), "minecraft/pack.json"); // Making "jar" to gameVersion is to be compatible with old HMCL.
zip.putTextFile(Constants.GSON.toJson(modpack.setGameVersion(gameVersion)), "modpack.json"); // Newer HMCL only reads 'gameVersion' field. zip.putTextFile(JsonUtils.GSON.toJson(modpack.setGameVersion(gameVersion)), "modpack.json"); // Newer HMCL only reads 'gameVersion' field.
} }
} }
} }

View File

@@ -27,7 +27,7 @@ import org.jackhuang.hmcl.mod.ModpackConfiguration;
import org.jackhuang.hmcl.mod.ModpackInstallTask; import org.jackhuang.hmcl.mod.ModpackInstallTask;
import org.jackhuang.hmcl.setting.Profile; import org.jackhuang.hmcl.setting.Profile;
import org.jackhuang.hmcl.task.Task; import org.jackhuang.hmcl.task.Task;
import org.jackhuang.hmcl.util.Constants; import org.jackhuang.hmcl.util.gson.JsonUtils;
import org.jackhuang.hmcl.util.io.CompressingUtils; import org.jackhuang.hmcl.util.io.CompressingUtils;
import org.jackhuang.hmcl.util.io.FileUtils; import org.jackhuang.hmcl.util.io.FileUtils;
@@ -65,7 +65,7 @@ public final class HMCLModpackInstallTask extends Task {
ModpackConfiguration<Modpack> config = null; ModpackConfiguration<Modpack> config = null;
try { try {
if (json.exists()) { if (json.exists()) {
config = Constants.GSON.fromJson(FileUtils.readText(json), new TypeToken<ModpackConfiguration<Modpack>>() { config = JsonUtils.GSON.fromJson(FileUtils.readText(json), new TypeToken<ModpackConfiguration<Modpack>>() {
}.getType()); }.getType());
if (!MODPACK_TYPE.equals(config.getType())) if (!MODPACK_TYPE.equals(config.getType()))
@@ -89,7 +89,7 @@ public final class HMCLModpackInstallTask extends Task {
@Override @Override
public void execute() throws Exception { public void execute() throws Exception {
String json = CompressingUtils.readTextZipEntry(zipFile, "minecraft/pack.json"); String json = CompressingUtils.readTextZipEntry(zipFile, "minecraft/pack.json");
Version version = Constants.GSON.fromJson(json, Version.class).setId(name).setJar(null); Version version = JsonUtils.GSON.fromJson(json, Version.class).setId(name).setJar(null);
dependencies.add(new VersionJsonSaveTask(repository, version)); dependencies.add(new VersionJsonSaveTask(repository, version));
dependencies.add(new MinecraftInstanceTask<>(zipFile, "/minecraft", modpack, MODPACK_TYPE, repository.getModpackConfiguration(name))); dependencies.add(new MinecraftInstanceTask<>(zipFile, "/minecraft", modpack, MODPACK_TYPE, repository.getModpackConfiguration(name)));
} }

View File

@@ -25,8 +25,8 @@ import org.jackhuang.hmcl.setting.Profile;
import org.jackhuang.hmcl.setting.VersionSetting; import org.jackhuang.hmcl.setting.VersionSetting;
import org.jackhuang.hmcl.task.FinalizedCallback; import org.jackhuang.hmcl.task.FinalizedCallback;
import org.jackhuang.hmcl.task.Task; import org.jackhuang.hmcl.task.Task;
import org.jackhuang.hmcl.util.Constants;
import org.jackhuang.hmcl.util.Lang; import org.jackhuang.hmcl.util.Lang;
import org.jackhuang.hmcl.util.gson.JsonUtils;
import org.jackhuang.hmcl.util.io.FileUtils; import org.jackhuang.hmcl.util.io.FileUtils;
import java.io.File; import java.io.File;
@@ -64,7 +64,7 @@ public final class ModpackHelper {
throw new FileNotFoundException(file.getPath()); throw new FileNotFoundException(file.getPath());
else else
try { try {
return Constants.GSON.fromJson(FileUtils.readText(file), new TypeToken<ModpackConfiguration<?>>() { return JsonUtils.GSON.fromJson(FileUtils.readText(file), new TypeToken<ModpackConfiguration<?>>() {
}.getType()); }.getType());
} catch (JsonParseException e) { } catch (JsonParseException e) {
throw new IOException("Malformed modpack configuration"); throw new IOException("Malformed modpack configuration");

View File

@@ -21,6 +21,7 @@ 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;
import org.jackhuang.hmcl.util.*; import org.jackhuang.hmcl.util.*;
import org.jackhuang.hmcl.util.gson.JsonUtils;
import org.jackhuang.hmcl.util.io.FileUtils; import org.jackhuang.hmcl.util.io.FileUtils;
import org.jackhuang.hmcl.util.platform.OperatingSystem; import org.jackhuang.hmcl.util.platform.OperatingSystem;
@@ -58,7 +59,7 @@ public class DefaultCacheRepository extends CacheRepository {
lock.writeLock().lock(); lock.writeLock().lock();
try { try {
if (Files.isRegularFile(indexFile)) if (Files.isRegularFile(indexFile))
index = Constants.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 e) {
@@ -193,7 +194,7 @@ public class DefaultCacheRepository extends CacheRepository {
private void saveIndex() { private void saveIndex() {
if (indexFile == null || index == null) return; if (indexFile == null || index == null) return;
try { try {
FileUtils.writeText(indexFile.toFile(), Constants.GSON.toJson(index)); FileUtils.writeText(indexFile.toFile(), JsonUtils.GSON.toJson(index));
} catch (IOException e) { } catch (IOException e) {
Logging.LOG.log(Level.SEVERE, "Unable to save index.json", e); Logging.LOG.log(Level.SEVERE, "Unable to save index.json", e);
} }

View File

@@ -22,8 +22,8 @@ import org.jackhuang.hmcl.game.Version;
import org.jackhuang.hmcl.task.ParallelTask; import org.jackhuang.hmcl.task.ParallelTask;
import org.jackhuang.hmcl.task.Task; import org.jackhuang.hmcl.task.Task;
import org.jackhuang.hmcl.util.AutoTypingMap; import org.jackhuang.hmcl.util.AutoTypingMap;
import org.jackhuang.hmcl.util.Constants;
import org.jackhuang.hmcl.util.function.ExceptionalFunction; import org.jackhuang.hmcl.util.function.ExceptionalFunction;
import org.jackhuang.hmcl.util.gson.JsonUtils;
/** /**
* *
@@ -50,7 +50,7 @@ public class DefaultGameBuilder extends GameBuilder {
@Override @Override
public Task buildAsync() { public Task buildAsync() {
return new VersionJsonDownloadTask(gameVersion, dependencyManager).then(variables -> { return new VersionJsonDownloadTask(gameVersion, dependencyManager).then(variables -> {
Version version = Constants.GSON.fromJson(variables.<String>get(VersionJsonDownloadTask.ID), Version.class); Version version = JsonUtils.GSON.fromJson(variables.<String>get(VersionJsonDownloadTask.ID), Version.class);
version = version.setId(name).setJar(null); version = version.setId(name).setJar(null);
variables.set("version", version); variables.set("version", version);
Task result = downloadGameAsync(gameVersion, version).then(new ParallelTask( Task result = downloadGameAsync(gameVersion, version).then(new ParallelTask(

View File

@@ -24,7 +24,7 @@ import org.jackhuang.hmcl.game.Version;
import org.jackhuang.hmcl.task.FileDownloadTask; import org.jackhuang.hmcl.task.FileDownloadTask;
import org.jackhuang.hmcl.task.Task; import org.jackhuang.hmcl.task.Task;
import org.jackhuang.hmcl.task.TaskResult; import org.jackhuang.hmcl.task.TaskResult;
import org.jackhuang.hmcl.util.Constants; import org.jackhuang.hmcl.util.gson.JsonUtils;
import org.jackhuang.hmcl.util.io.FileUtils; import org.jackhuang.hmcl.util.io.FileUtils;
import org.jackhuang.hmcl.util.io.IOUtils; import org.jackhuang.hmcl.util.io.IOUtils;
import org.jackhuang.hmcl.util.io.NetworkUtils; import org.jackhuang.hmcl.util.io.NetworkUtils;
@@ -89,7 +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 = Constants.GSON.fromJson(json, ForgeInstallProfile.class); ForgeInstallProfile installProfile = JsonUtils.GSON.fromJson(json, ForgeInstallProfile.class);
if (installProfile == null) if (installProfile == 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.");

View File

@@ -21,8 +21,8 @@ import org.jackhuang.hmcl.download.DownloadProvider;
import org.jackhuang.hmcl.download.VersionList; import org.jackhuang.hmcl.download.VersionList;
import org.jackhuang.hmcl.task.GetTask; import org.jackhuang.hmcl.task.GetTask;
import org.jackhuang.hmcl.task.Task; import org.jackhuang.hmcl.task.Task;
import org.jackhuang.hmcl.util.Constants;
import org.jackhuang.hmcl.util.StringUtils; import org.jackhuang.hmcl.util.StringUtils;
import org.jackhuang.hmcl.util.gson.JsonUtils;
import org.jackhuang.hmcl.util.io.NetworkUtils; import org.jackhuang.hmcl.util.io.NetworkUtils;
import org.jackhuang.hmcl.util.versioning.VersionNumber; import org.jackhuang.hmcl.util.versioning.VersionNumber;
@@ -60,7 +60,7 @@ public final class ForgeVersionList extends VersionList<ForgeRemoteVersion> {
lock.writeLock().lock(); lock.writeLock().lock();
try { try {
ForgeVersionRoot root = Constants.GSON.fromJson(task.getResult(), ForgeVersionRoot.class); ForgeVersionRoot root = JsonUtils.GSON.fromJson(task.getResult(), ForgeVersionRoot.class);
if (root == null) if (root == null)
return; return;
versions.clear(); versions.clear();

View File

@@ -24,10 +24,10 @@ import org.jackhuang.hmcl.game.AssetObject;
import org.jackhuang.hmcl.game.Version; import org.jackhuang.hmcl.game.Version;
import org.jackhuang.hmcl.task.FileDownloadTask; import org.jackhuang.hmcl.task.FileDownloadTask;
import org.jackhuang.hmcl.task.Task; import org.jackhuang.hmcl.task.Task;
import org.jackhuang.hmcl.util.Constants;
import org.jackhuang.hmcl.util.io.FileUtils; import org.jackhuang.hmcl.util.io.FileUtils;
import org.jackhuang.hmcl.util.io.NetworkUtils; import org.jackhuang.hmcl.util.io.NetworkUtils;
import org.jackhuang.hmcl.util.CacheRepository; import org.jackhuang.hmcl.util.CacheRepository;
import org.jackhuang.hmcl.util.gson.JsonUtils;
import java.io.File; import java.io.File;
import java.util.Collection; import java.util.Collection;
@@ -75,7 +75,7 @@ public final class GameAssetDownloadTask extends Task {
@Override @Override
public void execute() throws Exception { public void execute() throws Exception {
AssetIndex index = Constants.GSON.fromJson(FileUtils.readText(assetIndexFile), AssetIndex.class); AssetIndex index = JsonUtils.GSON.fromJson(FileUtils.readText(assetIndexFile), AssetIndex.class);
int progress = 0; int progress = 0;
if (index != null) if (index != null)
for (AssetObject assetObject : index.getObjects().values()) { for (AssetObject assetObject : index.getObjects().values()) {

View File

@@ -21,8 +21,8 @@ import org.jackhuang.hmcl.download.DownloadProvider;
import org.jackhuang.hmcl.download.VersionList; import org.jackhuang.hmcl.download.VersionList;
import org.jackhuang.hmcl.task.GetTask; import org.jackhuang.hmcl.task.GetTask;
import org.jackhuang.hmcl.task.Task; import org.jackhuang.hmcl.task.Task;
import org.jackhuang.hmcl.util.Constants;
import org.jackhuang.hmcl.util.StringUtils; import org.jackhuang.hmcl.util.StringUtils;
import org.jackhuang.hmcl.util.gson.JsonUtils;
import org.jackhuang.hmcl.util.io.NetworkUtils; import org.jackhuang.hmcl.util.io.NetworkUtils;
import java.util.Collection; import java.util.Collection;
@@ -70,7 +70,7 @@ public final class GameVersionList extends VersionList<GameRemoteVersion> {
try { try {
versions.clear(); versions.clear();
GameRemoteVersions root = Constants.GSON.fromJson(task.getResult(), GameRemoteVersions.class); GameRemoteVersions root = JsonUtils.GSON.fromJson(task.getResult(), GameRemoteVersions.class);
for (GameRemoteVersionInfo remoteVersion : root.getVersions()) { for (GameRemoteVersionInfo remoteVersion : root.getVersions()) {
versions.put(remoteVersion.getGameVersion(), new GameRemoteVersion( versions.put(remoteVersion.getGameVersion(), new GameRemoteVersion(
remoteVersion.getGameVersion(), remoteVersion.getGameVersion(),

View File

@@ -21,8 +21,8 @@ import org.jackhuang.hmcl.game.CompatibilityRule;
import org.jackhuang.hmcl.game.Library; import org.jackhuang.hmcl.game.Library;
import org.jackhuang.hmcl.game.Version; import org.jackhuang.hmcl.game.Version;
import org.jackhuang.hmcl.task.TaskResult; import org.jackhuang.hmcl.task.TaskResult;
import org.jackhuang.hmcl.util.Constants;
import org.jackhuang.hmcl.util.SimpleMultimap; import org.jackhuang.hmcl.util.SimpleMultimap;
import org.jackhuang.hmcl.util.gson.JsonUtils;
import org.jackhuang.hmcl.util.versioning.VersionNumber; import org.jackhuang.hmcl.util.versioning.VersionNumber;
import java.util.ArrayList; import java.util.ArrayList;
@@ -53,7 +53,7 @@ public class LibrariesUniqueTask extends TaskResult<Version> {
for (Library library : libraries) { for (Library library : libraries) {
String id = library.getGroupId() + ":" + library.getArtifactId(); String id = library.getGroupId() + ":" + library.getArtifactId();
VersionNumber number = VersionNumber.asVersion(library.getVersion()); VersionNumber number = VersionNumber.asVersion(library.getVersion());
String serialized = Constants.GSON.toJson(library); String serialized = JsonUtils.GSON.toJson(library);
if (multimap.containsKey(id)) { if (multimap.containsKey(id)) {
boolean duplicate = false; boolean duplicate = false;
@@ -68,7 +68,7 @@ public class LibrariesUniqueTask extends TaskResult<Version> {
} else if (number.compareTo(otherNumber) == 0) { // same library id. } else if (number.compareTo(otherNumber) == 0) { // same library id.
// prevent from duplicated libraries // prevent from duplicated libraries
if (library.equals(otherLibrary)) { if (library.equals(otherLibrary)) {
String otherSerialized = Constants.GSON.toJson(otherLibrary); String otherSerialized = JsonUtils.GSON.toJson(otherLibrary);
// A trick, the library that has more information is better, which can be // A trick, the library that has more information is better, which can be
// considered whose serialized JSON text will be longer. // considered whose serialized JSON text will be longer.
if (serialized.length() > otherSerialized.length()) { if (serialized.length() > otherSerialized.length()) {

View File

@@ -20,7 +20,7 @@ package org.jackhuang.hmcl.download.game;
import org.jackhuang.hmcl.game.DefaultGameRepository; import org.jackhuang.hmcl.game.DefaultGameRepository;
import org.jackhuang.hmcl.game.Version; import org.jackhuang.hmcl.game.Version;
import org.jackhuang.hmcl.task.Task; import org.jackhuang.hmcl.task.Task;
import org.jackhuang.hmcl.util.Constants; import org.jackhuang.hmcl.util.gson.JsonUtils;
import org.jackhuang.hmcl.util.io.FileUtils; import org.jackhuang.hmcl.util.io.FileUtils;
import java.io.File; import java.io.File;
@@ -54,6 +54,6 @@ public final class VersionJsonSaveTask extends Task {
File json = repository.getVersionJson(version.getId()).getAbsoluteFile(); File json = repository.getVersionJson(version.getId()).getAbsoluteFile();
if (!FileUtils.makeFile(json)) if (!FileUtils.makeFile(json))
throw new IOException("Cannot create file " + json); throw new IOException("Cannot create file " + json);
FileUtils.writeText(json, Constants.GSON.toJson(version)); FileUtils.writeText(json, JsonUtils.GSON.toJson(version));
} }
} }

View File

@@ -21,7 +21,7 @@ import org.jackhuang.hmcl.download.DownloadProvider;
import org.jackhuang.hmcl.download.VersionList; import org.jackhuang.hmcl.download.VersionList;
import org.jackhuang.hmcl.task.GetTask; import org.jackhuang.hmcl.task.GetTask;
import org.jackhuang.hmcl.task.Task; import org.jackhuang.hmcl.task.Task;
import org.jackhuang.hmcl.util.Constants; import org.jackhuang.hmcl.util.gson.JsonUtils;
import org.jackhuang.hmcl.util.io.NetworkUtils; import org.jackhuang.hmcl.util.io.NetworkUtils;
import org.jackhuang.hmcl.util.versioning.VersionNumber; import org.jackhuang.hmcl.util.versioning.VersionNumber;
import org.w3c.dom.Document; import org.w3c.dom.Document;
@@ -65,7 +65,7 @@ public final class LiteLoaderBMCLVersionList extends VersionList<LiteLoaderRemot
lock.writeLock().lock(); lock.writeLock().lock();
try { try {
LiteLoaderVersionsRoot root = Constants.GSON.fromJson(task.getResult(), LiteLoaderVersionsRoot.class); LiteLoaderVersionsRoot root = JsonUtils.GSON.fromJson(task.getResult(), LiteLoaderVersionsRoot.class);
versions.clear(); versions.clear();
for (Map.Entry<String, LiteLoaderGameVersions> entry : root.getVersions().entrySet()) { for (Map.Entry<String, LiteLoaderGameVersions> entry : root.getVersions().entrySet()) {

View File

@@ -21,7 +21,7 @@ import org.jackhuang.hmcl.download.DownloadProvider;
import org.jackhuang.hmcl.download.VersionList; import org.jackhuang.hmcl.download.VersionList;
import org.jackhuang.hmcl.task.GetTask; import org.jackhuang.hmcl.task.GetTask;
import org.jackhuang.hmcl.task.Task; import org.jackhuang.hmcl.task.Task;
import org.jackhuang.hmcl.util.Constants; import org.jackhuang.hmcl.util.gson.JsonUtils;
import org.jackhuang.hmcl.util.io.NetworkUtils; import org.jackhuang.hmcl.util.io.NetworkUtils;
import org.jackhuang.hmcl.util.versioning.VersionNumber; import org.jackhuang.hmcl.util.versioning.VersionNumber;
import org.w3c.dom.Document; import org.w3c.dom.Document;
@@ -65,7 +65,7 @@ public final class LiteLoaderVersionList extends VersionList<LiteLoaderRemoteVer
lock.writeLock().lock(); lock.writeLock().lock();
try { try {
LiteLoaderVersionsRoot root = Constants.GSON.fromJson(task.getResult(), LiteLoaderVersionsRoot.class); LiteLoaderVersionsRoot root = JsonUtils.GSON.fromJson(task.getResult(), LiteLoaderVersionsRoot.class);
versions.clear(); versions.clear();
for (Map.Entry<String, LiteLoaderGameVersions> entry : root.getVersions().entrySet()) { for (Map.Entry<String, LiteLoaderGameVersions> entry : root.getVersions().entrySet()) {

View File

@@ -22,8 +22,8 @@ import org.jackhuang.hmcl.download.DownloadProvider;
import org.jackhuang.hmcl.download.VersionList; import org.jackhuang.hmcl.download.VersionList;
import org.jackhuang.hmcl.task.GetTask; import org.jackhuang.hmcl.task.GetTask;
import org.jackhuang.hmcl.task.Task; import org.jackhuang.hmcl.task.Task;
import org.jackhuang.hmcl.util.Constants;
import org.jackhuang.hmcl.util.StringUtils; import org.jackhuang.hmcl.util.StringUtils;
import org.jackhuang.hmcl.util.gson.JsonUtils;
import org.jackhuang.hmcl.util.io.NetworkUtils; import org.jackhuang.hmcl.util.io.NetworkUtils;
import org.jackhuang.hmcl.util.versioning.VersionNumber; import org.jackhuang.hmcl.util.versioning.VersionNumber;
@@ -58,7 +58,7 @@ public final class OptiFineBMCLVersionList extends VersionList<OptiFineRemoteVer
public void execute() { public void execute() {
versions.clear(); versions.clear();
Set<String> duplicates = new HashSet<>(); Set<String> duplicates = new HashSet<>();
List<OptiFineVersion> root = Constants.GSON.fromJson(task.getResult(), new TypeToken<List<OptiFineVersion>>() { List<OptiFineVersion> root = JsonUtils.GSON.fromJson(task.getResult(), new TypeToken<List<OptiFineVersion>>() {
}.getType()); }.getType());
for (OptiFineVersion element : root) { for (OptiFineVersion element : root) {
String version = element.getType() + "_" + element.getPatch(); String version = element.getType() + "_" + element.getPatch();

View File

@@ -21,9 +21,9 @@ import com.google.gson.JsonParseException;
import com.google.gson.JsonSyntaxException; 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.Constants;
import org.jackhuang.hmcl.util.Logging; import org.jackhuang.hmcl.util.Logging;
import org.jackhuang.hmcl.util.ToStringBuilder; import org.jackhuang.hmcl.util.ToStringBuilder;
import org.jackhuang.hmcl.util.gson.JsonUtils;
import org.jackhuang.hmcl.util.io.FileUtils; import org.jackhuang.hmcl.util.io.FileUtils;
import java.io.File; import java.io.File;
@@ -113,7 +113,7 @@ public class DefaultGameRepository implements GameRepository {
} }
public Version readVersionJson(File file) throws IOException, JsonSyntaxException { public Version readVersionJson(File file) throws IOException, JsonSyntaxException {
return Constants.GSON.fromJson(FileUtils.readText(file), Version.class); return JsonUtils.GSON.fromJson(FileUtils.readText(file), Version.class);
} }
@Override @Override
@@ -142,7 +142,7 @@ public class DefaultGameRepository implements GameRepository {
if (fromVersion.getId().equals(fromVersion.getJar())) if (fromVersion.getId().equals(fromVersion.getJar()))
fromVersion = fromVersion.setJar(to); fromVersion = fromVersion.setJar(to);
FileUtils.writeText(toJson, Constants.GSON.toJson(fromVersion.setId(to))); FileUtils.writeText(toJson, JsonUtils.GSON.toJson(fromVersion.setId(to)));
return true; return true;
} catch (IOException | JsonParseException | VersionNotFoundException e) { } catch (IOException | JsonParseException | VersionNotFoundException e) {
return false; return false;
@@ -229,7 +229,7 @@ public class DefaultGameRepository implements GameRepository {
if (!id.equals(version.getId())) { if (!id.equals(version.getId())) {
version = version.setId(id); version = version.setId(id);
try { try {
FileUtils.writeText(json, Constants.GSON.toJson(version)); FileUtils.writeText(json, JsonUtils.GSON.toJson(version));
} catch (Exception e) { } catch (Exception e) {
Logging.LOG.log(Level.WARNING, "Ignoring version " + id + " because wrong id " + version.getId() + " is set and cannot correct it."); Logging.LOG.log(Level.WARNING, "Ignoring version " + id + " because wrong id " + version.getId() + " is set and cannot correct it.");
return Stream.empty(); return Stream.empty();
@@ -269,7 +269,7 @@ public class DefaultGameRepository implements GameRepository {
@Override @Override
public AssetIndex getAssetIndex(String version, String assetId) throws IOException { public AssetIndex getAssetIndex(String version, String assetId) throws IOException {
try { try {
return Objects.requireNonNull(Constants.GSON.fromJson(FileUtils.readText(getIndexFile(version, assetId)), AssetIndex.class)); return Objects.requireNonNull(JsonUtils.GSON.fromJson(FileUtils.readText(getIndexFile(version, assetId)), AssetIndex.class));
} catch (JsonParseException | NullPointerException e) { } catch (JsonParseException | NullPointerException e) {
throw new IOException("Asset index file malformed", e); throw new IOException("Asset index file malformed", e);
} }
@@ -329,7 +329,7 @@ public class DefaultGameRepository implements GameRepository {
return assetsDir; return assetsDir;
String assetIndexContent = FileUtils.readText(indexFile); String assetIndexContent = FileUtils.readText(indexFile);
AssetIndex index = Constants.GSON.fromJson(assetIndexContent, AssetIndex.class); AssetIndex index = JsonUtils.GSON.fromJson(assetIndexContent, AssetIndex.class);
if (index == null) if (index == null)
return assetsDir; return assetsDir;

View File

@@ -22,6 +22,7 @@ import org.jackhuang.hmcl.game.GameRepository;
import org.jackhuang.hmcl.task.FileDownloadTask; import org.jackhuang.hmcl.task.FileDownloadTask;
import org.jackhuang.hmcl.task.Task; import org.jackhuang.hmcl.task.Task;
import org.jackhuang.hmcl.util.*; import org.jackhuang.hmcl.util.*;
import org.jackhuang.hmcl.util.gson.JsonUtils;
import org.jackhuang.hmcl.util.io.FileUtils; import org.jackhuang.hmcl.util.io.FileUtils;
import org.jackhuang.hmcl.util.io.NetworkUtils; import org.jackhuang.hmcl.util.io.NetworkUtils;
@@ -76,7 +77,7 @@ public final class CurseCompletionTask extends Task {
try { try {
File manifestFile = new File(repository.getVersionRoot(version), "manifest.json"); File manifestFile = new File(repository.getVersionRoot(version), "manifest.json");
if (manifestFile.exists()) if (manifestFile.exists())
this.manifest = Constants.GSON.fromJson(FileUtils.readText(manifestFile), CurseManifest.class); this.manifest = JsonUtils.GSON.fromJson(FileUtils.readText(manifestFile), CurseManifest.class);
} catch (Exception e) { } catch (Exception e) {
Logging.LOG.log(Level.WARNING, "Unable to read CurseForge modpack manifest.json", e); Logging.LOG.log(Level.WARNING, "Unable to read CurseForge modpack manifest.json", e);
} }
@@ -121,7 +122,7 @@ public final class CurseCompletionTask extends Task {
return file; return file;
}) })
.collect(Collectors.toList())); .collect(Collectors.toList()));
FileUtils.writeText(new File(root, "manifest.json"), Constants.GSON.toJson(newManifest)); FileUtils.writeText(new File(root, "manifest.json"), JsonUtils.GSON.toJson(newManifest));
for (CurseManifestFile file : newManifest.getFiles()) for (CurseManifestFile file : newManifest.getFiles())
if (StringUtils.isNotBlank(file.getFileName())) { if (StringUtils.isNotBlank(file.getFileName())) {

View File

@@ -24,6 +24,7 @@ import org.jackhuang.hmcl.download.GameBuilder;
import org.jackhuang.hmcl.game.DefaultGameRepository; import org.jackhuang.hmcl.game.DefaultGameRepository;
import org.jackhuang.hmcl.task.Task; import org.jackhuang.hmcl.task.Task;
import org.jackhuang.hmcl.util.Constants; import org.jackhuang.hmcl.util.Constants;
import org.jackhuang.hmcl.util.gson.JsonUtils;
import org.jackhuang.hmcl.util.io.FileUtils; import org.jackhuang.hmcl.util.io.FileUtils;
import java.io.File; import java.io.File;
@@ -83,7 +84,7 @@ public final class CurseInstallTask extends Task {
ModpackConfiguration<CurseManifest> config = null; ModpackConfiguration<CurseManifest> config = null;
try { try {
if (json.exists()) { if (json.exists()) {
config = Constants.GSON.fromJson(FileUtils.readText(json), new TypeToken<ModpackConfiguration<CurseManifest>>() { config = JsonUtils.GSON.fromJson(FileUtils.readText(json), new TypeToken<ModpackConfiguration<CurseManifest>>() {
}.getType()); }.getType());
if (!MODPACK_TYPE.equals(config.getType())) if (!MODPACK_TYPE.equals(config.getType()))
@@ -118,7 +119,7 @@ public final class CurseInstallTask extends Task {
} }
File root = repository.getVersionRoot(name); File root = repository.getVersionRoot(name);
FileUtils.writeText(new File(root, "manifest.json"), Constants.GSON.toJson(manifest)); FileUtils.writeText(new File(root, "manifest.json"), JsonUtils.GSON.toJson(manifest));
dependencies.add(new CurseCompletionTask(dependencyManager, name, manifest)); dependencies.add(new CurseCompletionTask(dependencyManager, name, manifest));
dependencies.add(new MinecraftInstanceTask<>(zipFile, manifest.getOverrides(), manifest, MODPACK_TYPE, repository.getModpackConfiguration(name))); dependencies.add(new MinecraftInstanceTask<>(zipFile, manifest.getOverrides(), manifest, MODPACK_TYPE, repository.getModpackConfiguration(name)));

View File

@@ -21,6 +21,7 @@ import com.google.gson.JsonParseException;
import com.google.gson.annotations.SerializedName; import com.google.gson.annotations.SerializedName;
import com.google.gson.reflect.TypeToken; import com.google.gson.reflect.TypeToken;
import org.jackhuang.hmcl.util.*; import org.jackhuang.hmcl.util.*;
import org.jackhuang.hmcl.util.gson.JsonUtils;
import org.jackhuang.hmcl.util.io.CompressingUtils; import org.jackhuang.hmcl.util.io.CompressingUtils;
import org.jackhuang.hmcl.util.io.IOUtils; import org.jackhuang.hmcl.util.io.IOUtils;
@@ -118,7 +119,7 @@ public final class ForgeModMetadata {
Path mcmod = fs.getPath("mcmod.info"); Path mcmod = fs.getPath("mcmod.info");
if (Files.notExists(mcmod)) if (Files.notExists(mcmod))
throw new IOException("File " + modFile + " is not a Forge mod."); throw new IOException("File " + modFile + " is not a Forge mod.");
List<ForgeModMetadata> modList = Constants.GSON.fromJson(IOUtils.readFullyAsString(Files.newInputStream(mcmod)), List<ForgeModMetadata> modList = JsonUtils.GSON.fromJson(IOUtils.readFullyAsString(Files.newInputStream(mcmod)),
new TypeToken<List<ForgeModMetadata>>() { new TypeToken<List<ForgeModMetadata>>() {
}.getType()); }.getType());
if (modList == null || modList.isEmpty()) if (modList == null || modList.isEmpty())

View File

@@ -18,8 +18,9 @@
package org.jackhuang.hmcl.mod; package org.jackhuang.hmcl.mod;
import com.google.gson.JsonParseException; import com.google.gson.JsonParseException;
import org.jackhuang.hmcl.util.Constants;
import org.jackhuang.hmcl.util.Immutable; import org.jackhuang.hmcl.util.Immutable;
import org.jackhuang.hmcl.util.gson.JsonUtils;
import org.jackhuang.hmcl.util.io.IOUtils; import org.jackhuang.hmcl.util.io.IOUtils;
import java.io.File; import java.io.File;
@@ -112,7 +113,7 @@ public final class LiteModMetadata {
ZipEntry entry = zipFile.getEntry("litemod.json"); ZipEntry entry = zipFile.getEntry("litemod.json");
if (entry == null) if (entry == null)
throw new IOException("File " + modFile + "is not a LiteLoader mod."); throw new IOException("File " + modFile + "is not a LiteLoader mod.");
LiteModMetadata metadata = Constants.GSON.fromJson(IOUtils.readFullyAsString(zipFile.getInputStream(entry)), LiteModMetadata.class); LiteModMetadata metadata = JsonUtils.GSON.fromJson(IOUtils.readFullyAsString(zipFile.getInputStream(entry)), LiteModMetadata.class);
if (metadata == null) if (metadata == null)
throw new IOException("Mod " + modFile + " `litemod.json` is malformed."); throw new IOException("Mod " + modFile + " `litemod.json` is malformed.");
return new ModInfo(modFile, metadata.getName(), metadata.getDescription(), metadata.getAuthor(), metadata.getVersion(), metadata.getGameVersion(), metadata.getUpdateURI()); return new ModInfo(modFile, metadata.getName(), metadata.getDescription(), metadata.getAuthor(), metadata.getVersion(), metadata.getGameVersion(), metadata.getUpdateURI());

View File

@@ -18,7 +18,7 @@
package org.jackhuang.hmcl.mod; package org.jackhuang.hmcl.mod;
import org.jackhuang.hmcl.task.Task; import org.jackhuang.hmcl.task.Task;
import org.jackhuang.hmcl.util.Constants; import org.jackhuang.hmcl.util.gson.JsonUtils;
import org.jackhuang.hmcl.util.io.CompressingUtils; import org.jackhuang.hmcl.util.io.CompressingUtils;
import org.jackhuang.hmcl.util.io.FileUtils; import org.jackhuang.hmcl.util.io.FileUtils;
@@ -69,6 +69,6 @@ public final class MinecraftInstanceTask<T> extends Task {
}); });
} }
FileUtils.writeText(jsonFile, Constants.GSON.toJson(new ModpackConfiguration<>(manifest, type, overrides))); FileUtils.writeText(jsonFile, JsonUtils.GSON.toJson(new ModpackConfiguration<>(manifest, type, overrides)));
} }
} }

View File

@@ -27,6 +27,7 @@ import org.jackhuang.hmcl.game.DefaultGameRepository;
import org.jackhuang.hmcl.game.Version; import org.jackhuang.hmcl.game.Version;
import org.jackhuang.hmcl.task.Task; import org.jackhuang.hmcl.task.Task;
import org.jackhuang.hmcl.util.*; import org.jackhuang.hmcl.util.*;
import org.jackhuang.hmcl.util.gson.JsonUtils;
import org.jackhuang.hmcl.util.io.CompressingUtils; import org.jackhuang.hmcl.util.io.CompressingUtils;
import org.jackhuang.hmcl.util.io.FileUtils; import org.jackhuang.hmcl.util.io.FileUtils;
import org.jackhuang.hmcl.util.io.IOUtils; import org.jackhuang.hmcl.util.io.IOUtils;
@@ -90,7 +91,7 @@ public final class MultiMCModpackInstallTask extends Task {
ModpackConfiguration<MultiMCInstanceConfiguration> config = null; ModpackConfiguration<MultiMCInstanceConfiguration> config = null;
try { try {
if (json.exists()) { if (json.exists()) {
config = Constants.GSON.fromJson(FileUtils.readText(json), new TypeToken<ModpackConfiguration<MultiMCInstanceConfiguration>>() { config = JsonUtils.GSON.fromJson(FileUtils.readText(json), new TypeToken<ModpackConfiguration<MultiMCInstanceConfiguration>>() {
}.getType()); }.getType());
if (!MODPACK_TYPE.equals(config.getType())) if (!MODPACK_TYPE.equals(config.getType()))
@@ -124,7 +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")) {
MultiMCInstancePatch patch = Constants.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()) {
newArguments.add("--tweakClass"); newArguments.add("--tweakClass");

View File

@@ -17,29 +17,15 @@
*/ */
package org.jackhuang.hmcl.util; package org.jackhuang.hmcl.util;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import javafx.beans.binding.Bindings; import javafx.beans.binding.Bindings;
import javafx.beans.binding.ObjectBinding; import javafx.beans.binding.ObjectBinding;
import javafx.scene.image.Image; import javafx.scene.image.Image;
import org.jackhuang.hmcl.game.Argument;
import org.jackhuang.hmcl.game.Library;
import org.jackhuang.hmcl.game.RuledArgument;
import org.jackhuang.hmcl.game.StringArgument;
import org.jackhuang.hmcl.task.Schedulers; import org.jackhuang.hmcl.task.Schedulers;
import org.jackhuang.hmcl.util.gson.DateTypeAdapter;
import org.jackhuang.hmcl.util.gson.FileTypeAdapter;
import org.jackhuang.hmcl.util.gson.LowerCaseEnumTypeAdapterFactory;
import org.jackhuang.hmcl.util.gson.UUIDTypeAdapter;
import org.jackhuang.hmcl.util.gson.ValidationTypeAdapterFactory;
import org.jackhuang.hmcl.util.platform.OperatingSystem; import org.jackhuang.hmcl.util.platform.OperatingSystem;
import org.jackhuang.hmcl.util.platform.Platform;
import java.awt.*; import java.awt.*;
import java.io.File;
import java.nio.charset.Charset; import java.nio.charset.Charset;
import java.util.Date;
import java.util.UUID;
import java.util.function.Consumer; import java.util.function.Consumer;
import java.util.function.Predicate; import java.util.function.Predicate;
@@ -78,21 +64,6 @@ public final class Constants {
// lazy loading // lazy loading
public static final ObjectBinding<Image> DEFAULT_ICON = Bindings.createObjectBinding(() -> new Image("/assets/img/icon.png")); public static final ObjectBinding<Image> DEFAULT_ICON = Bindings.createObjectBinding(() -> new Image("/assets/img/icon.png"));
public static final Gson GSON = new GsonBuilder()
.enableComplexMapKeySerialization()
.setPrettyPrinting()
.registerTypeAdapter(Library.class, Library.Serializer.INSTANCE)
.registerTypeAdapter(Argument.class, Argument.Serializer.INSTANCE)
.registerTypeAdapter(StringArgument.class, Argument.Serializer.INSTANCE)
.registerTypeAdapter(RuledArgument.class, RuledArgument.Serializer.INSTANCE)
.registerTypeAdapter(Date.class, DateTypeAdapter.INSTANCE)
.registerTypeAdapter(UUID.class, UUIDTypeAdapter.INSTANCE)
.registerTypeAdapter(Platform.class, Platform.Serializer.INSTANCE)
.registerTypeAdapter(File.class, FileTypeAdapter.INSTANCE)
.registerTypeAdapterFactory(ValidationTypeAdapterFactory.INSTANCE)
.registerTypeAdapterFactory(LowerCaseEnumTypeAdapterFactory.INSTANCE)
.create();
public static <T> Predicate<T> truePredicate() { public static <T> Predicate<T> truePredicate() {
return s -> true; return s -> true;
} }

View File

@@ -17,8 +17,18 @@
*/ */
package org.jackhuang.hmcl.util.gson; package org.jackhuang.hmcl.util.gson;
import org.jackhuang.hmcl.util.Constants; import java.io.File;
import java.util.Date;
import java.util.UUID;
import org.jackhuang.hmcl.game.Argument;
import org.jackhuang.hmcl.game.Library;
import org.jackhuang.hmcl.game.RuledArgument;
import org.jackhuang.hmcl.game.StringArgument;
import org.jackhuang.hmcl.util.platform.Platform;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonParseException; import com.google.gson.JsonParseException;
/** /**
@@ -26,10 +36,26 @@ import com.google.gson.JsonParseException;
*/ */
public final class JsonUtils { public final class JsonUtils {
private JsonUtils() {} public static final Gson GSON = new GsonBuilder()
.enableComplexMapKeySerialization()
.setPrettyPrinting()
.registerTypeAdapter(Library.class, Library.Serializer.INSTANCE)
.registerTypeAdapter(Argument.class, Argument.Serializer.INSTANCE)
.registerTypeAdapter(StringArgument.class, Argument.Serializer.INSTANCE)
.registerTypeAdapter(RuledArgument.class, RuledArgument.Serializer.INSTANCE)
.registerTypeAdapter(Date.class, DateTypeAdapter.INSTANCE)
.registerTypeAdapter(UUID.class, UUIDTypeAdapter.INSTANCE)
.registerTypeAdapter(Platform.class, Platform.Serializer.INSTANCE)
.registerTypeAdapter(File.class, FileTypeAdapter.INSTANCE)
.registerTypeAdapterFactory(ValidationTypeAdapterFactory.INSTANCE)
.registerTypeAdapterFactory(LowerCaseEnumTypeAdapterFactory.INSTANCE)
.create();
private JsonUtils() {
}
public static <T> T fromNonNullJson(String json, Class<T> classOfT) throws JsonParseException { public static <T> T fromNonNullJson(String json, Class<T> classOfT) throws JsonParseException {
T parsed = Constants.GSON.fromJson(json, classOfT); T parsed = GSON.fromJson(json, classOfT);
if (parsed == null) if (parsed == null)
throw new JsonParseException("Json object cannot be null."); throw new JsonParseException("Json object cannot be null.");
return parsed; return parsed;