Skip to content

Commit

Permalink
修复modpackinfo.json NPE异常崩溃的问题
Browse files Browse the repository at this point in the history
  • Loading branch information
Wulian233 authored Feb 2, 2025
1 parent ac5570f commit 0ca6ea7
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 16 deletions.
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
package top.vmctcn.vmtranslationupdate.modpack;

public class ModpackInfo {
private Modpack modpack;
Modpack modpack;

public Modpack getModpack() {
return modpack;
}

public static class Modpack {
private String name;
private String version;
private Translation translation;
String name;
String version;
Translation translation;

public String getName() {
return name;
Expand All @@ -26,11 +26,11 @@ public Translation getTranslation() {
}

public static class Translation {
private String url;
private String language;
private String version;
private String updateCheckUrl;
private String resourcePackName;
String url;
String language;
String version;
String updateCheckUrl;
String resourcePackName;

public String getUrl() {
return url;
Expand All @@ -52,4 +52,4 @@ public String getResourcePackName() {
return resourcePackName;
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,33 +1,72 @@
package top.vmctcn.vmtranslationupdate.modpack;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import top.vmctcn.vmtranslationupdate.VMTranslationUpdate;
import top.vmctcn.vmtucore.ModPlatform;

import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardOpenOption;

public class ModpackInfoReader {
private static final Gson GSON = new Gson();
private static final Gson GSON = new GsonBuilder().setPrettyPrinting().create();
private static ModpackInfo modpackInfo;
private static final Path gamePath = ModPlatform.INSTANCE.getGameDir();
private static final Path modpackInfoPath = gamePath.resolve("modpackinfo.json");

static {
init();
}

public static void init() {
if (gamePath.resolve("modpackinfo.json") != null) {
try (Reader reader = new FileReader(gamePath.resolve("modpackinfo.json").toString())) {
if (Files.exists(modpackInfoPath)) {
try (Reader reader = Files.newBufferedReader(modpackInfoPath, StandardCharsets.UTF_8)) {
modpackInfo = GSON.fromJson(reader, ModpackInfo.class);
if (modpackInfo == null) {
VMTranslationUpdate.LOGGER.warn("modpackinfo.json is empty or invalid, generating default file.");
generateDefaultModpackInfo();
}
} catch (Exception e) {
VMTranslationUpdate.LOGGER.warn("Error getting modpack info index: ", e);
VMTranslationUpdate.LOGGER.warn("Error reading modpackinfo.json, generating default file.", e);
generateDefaultModpackInfo();
}
} else {
VMTranslationUpdate.LOGGER.warn("modpackinfo.json does not exist, generating default file.");
generateDefaultModpackInfo();
}
}

private static void generateDefaultModpackInfo() {
modpackInfo = new ModpackInfo();
modpackInfo.modpack = new ModpackInfo.Modpack();
modpackInfo.modpack.name = "ExampleModpack";
modpackInfo.modpack.version = "v0.1.0";

modpackInfo.modpack.translation = new ModpackInfo.Translation();
modpackInfo.modpack.translation.url = "https://vmct-cn.top/modpacks/example/";
modpackInfo.modpack.translation.language = "zh_cn";
modpackInfo.modpack.translation.version = "1.0.0";
modpackInfo.modpack.translation.updateCheckUrl = "https://gitee.com/Wulian233/vmtu/raw/main/update/example.txt";
modpackInfo.modpack.translation.resourcePackName = "VM汉化组模组汉化包1.18";

try {
Files.writeString(modpackInfoPath, GSON.toJson(modpackInfo), StandardCharsets.UTF_8, StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING);
VMTranslationUpdate.LOGGER.info("Default modpackinfo.json generated.");

// 再次读取以确保正确加载
try (Reader reader = Files.newBufferedReader(modpackInfoPath, StandardCharsets.UTF_8)) {
modpackInfo = GSON.fromJson(reader, ModpackInfo.class);
}
} catch (IOException e) {
VMTranslationUpdate.LOGGER.error("Failed to generate default modpackinfo.json", e);
}
}

public static ModpackInfo getModpackInfo() {
return modpackInfo;
}
}
}

0 comments on commit 0ca6ea7

Please sign in to comment.