Fixed warning when renaming a game version

This commit is contained in:
huangyuhui 2017-04-09 20:09:11 +08:00
parent a68aa84e2f
commit 0d8bd2569f

View File

@ -43,6 +43,7 @@ import org.jackhuang.hmcl.util.MessageBox;
import org.jackhuang.hmcl.util.StrUtils; import org.jackhuang.hmcl.util.StrUtils;
import org.jackhuang.hmcl.api.func.Consumer; import org.jackhuang.hmcl.api.func.Consumer;
import org.jackhuang.hmcl.api.func.Predicate; import org.jackhuang.hmcl.api.func.Predicate;
import org.jackhuang.hmcl.util.sys.IOUtils;
import org.jackhuang.hmcl.util.ui.SwingUtils; import org.jackhuang.hmcl.util.ui.SwingUtils;
/** /**
@ -117,7 +118,7 @@ public class MinecraftVersionManager extends IMinecraftProvider {
} }
MinecraftVersion mcVersion; MinecraftVersion mcVersion;
try { try {
mcVersion = C.GSON.fromJson(FileUtils.read(jsonFile), MinecraftVersion.class); mcVersion = readJson(jsonFile);
if (mcVersion == null) if (mcVersion == null)
throw new JsonSyntaxException("Wrong json format, got null."); throw new JsonSyntaxException("Wrong json format, got null.");
} catch (JsonSyntaxException | IOException e) { } catch (JsonSyntaxException | IOException e) {
@ -125,7 +126,7 @@ public class MinecraftVersionManager extends IMinecraftProvider {
if (MessageBox.show(C.i18n("launcher.versions_json_not_formatted", id), MessageBox.YES_NO_OPTION) == MessageBox.YES_OPTION) { if (MessageBox.show(C.i18n("launcher.versions_json_not_formatted", id), MessageBox.YES_NO_OPTION) == MessageBox.YES_OPTION) {
TaskWindow.factory().execute(service.download().downloadMinecraftVersionJson(id)); TaskWindow.factory().execute(service.download().downloadMinecraftVersionJson(id));
try { try {
mcVersion = C.GSON.fromJson(FileUtils.read(jsonFile), MinecraftVersion.class); mcVersion = readJson(jsonFile);
if (mcVersion == null) if (mcVersion == null)
throw new JsonSyntaxException("Wrong json format, got null."); throw new JsonSyntaxException("Wrong json format, got null.");
} catch (IOException | JsonSyntaxException ex) { } catch (IOException | JsonSyntaxException ex) {
@ -165,7 +166,24 @@ public class MinecraftVersionManager extends IMinecraftProvider {
versions.remove(name); versions.remove(name);
return FileUtils.deleteDirectoryQuietly(version); return FileUtils.deleteDirectoryQuietly(version);
} }
/**
*
* @param id version id
* @return null if json syntax is wrong or cannot read the json file.
*/
public MinecraftVersion readJson(String id) {
try {
return readJson(new File(versionRoot(id), id + ".json"));
} catch(IOException | JsonSyntaxException e) {
return null;
}
}
public MinecraftVersion readJson(File file) throws IOException {
return C.GSON.fromJson(FileUtils.read(file, IOUtils.DEFAULT_CHARSET), MinecraftVersion.class);
}
@Override @Override
public boolean renameVersion(String from, String to) { public boolean renameVersion(String from, String to) {
try { try {
@ -176,7 +194,7 @@ public class MinecraftVersionManager extends IMinecraftProvider {
File toJar = new File(toDir, to + ".jar"); File toJar = new File(toDir, to + ".jar");
if (!new File(toDir, from + ".json").renameTo(toJson)) if (!new File(toDir, from + ".json").renameTo(toJson))
HMCLog.warn("MinecraftVersionManager.RenameVersion: Failed to rename json"); HMCLog.warn("MinecraftVersionManager.RenameVersion: Failed to rename json");
MinecraftVersion mcVersion = C.GSON.fromJson(FileUtils.read(toJson), MinecraftVersion.class); MinecraftVersion mcVersion = readJson(toJson);
mcVersion.id = to; mcVersion.id = to;
FileUtils.writeQuietly(toJson, C.GSON.toJson(mcVersion)); FileUtils.writeQuietly(toJson, C.GSON.toJson(mcVersion));
File oldJar = new File(toDir, from + ".jar"); File oldJar = new File(toDir, from + ".jar");
@ -203,7 +221,7 @@ public class MinecraftVersionManager extends IMinecraftProvider {
return false; return false;
if (callback != null) { if (callback != null) {
File mvt = new File(versionRoot(id), id + ".json"); File mvt = new File(versionRoot(id), id + ".json");
MinecraftVersion v = C.GSON.fromJson(FileUtils.readQuietly(mvt), MinecraftVersion.class); MinecraftVersion v = readJson(id);
if (v == null) if (v == null)
return false; return false;
callback.accept(v); callback.accept(v);