-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
55 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 45 additions & 6 deletions
51
common/src/main/java/top/vmctcn/vmtranslationupdate/modpack/ModpackInfoReader.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |