Skip to content

Commit 6a0604e

Browse files
committed
fix: 无法识别部分旧版本 Forge 模组
1 parent e4731c7 commit 6a0604e

File tree

2 files changed

+45
-3
lines changed

2 files changed

+45
-3
lines changed

FCLCore/src/main/java/com/tungsten/fclcore/mod/modinfo/ForgeOldModMetadata.java

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
import com.google.gson.JsonParseException;
2121
import com.google.gson.annotations.SerializedName;
2222
import com.google.gson.reflect.TypeToken;
23+
import com.google.gson.stream.JsonReader;
24+
import com.google.gson.stream.JsonToken;
2325
import com.tungsten.fclcore.mod.LocalModFile;
2426
import com.tungsten.fclcore.mod.ModLoaderType;
2527
import com.tungsten.fclcore.mod.ModManager;
@@ -119,11 +121,26 @@ public static LocalModFile fromFile(ModManager modManager, Path modFile, FileSys
119121
Path mcmod = fs.getPath("mcmod.info");
120122
if (Files.notExists(mcmod))
121123
throw new IOException("File " + modFile + " is not a Forge mod.");
122-
List<ForgeOldModMetadata> modList = JsonUtils.GSON.fromJson(FileUtils.readText(mcmod),
123-
new TypeToken<List<ForgeOldModMetadata>>() {
124+
List<ForgeOldModMetadata> modList;
125+
126+
try (var reader = Files.newBufferedReader(mcmod);
127+
var jsonReader = new JsonReader(reader)) {
128+
JsonToken firstToken = jsonReader.peek();
129+
130+
if (firstToken == JsonToken.BEGIN_ARRAY)
131+
modList = JsonUtils.GSON.fromJson(jsonReader, new TypeToken<List<ForgeOldModMetadata>>() {
124132
}.getType());
133+
else if (firstToken == JsonToken.BEGIN_OBJECT) {
134+
ForgeOldModMetadataLst list = JsonUtils.GSON.fromJson(jsonReader, ForgeOldModMetadataLst.class);
135+
if (list == null)
136+
throw new IOException("Mod " + modFile + " `mcmod.info` is malformed");
137+
modList = list.getModList();
138+
} else {
139+
throw new JsonParseException("Unexpected first token: " + firstToken);
140+
}
141+
}
125142
if (modList == null || modList.isEmpty())
126-
throw new IOException("Mod " + modFile + " `mcmod.info` is malformed..");
143+
throw new IOException("Mod " + modFile + " `mcmod.info` is malformed");
127144
ForgeOldModMetadata metadata = modList.get(0);
128145
String authors = metadata.getAuthor();
129146
if (StringUtils.isBlank(authors) && metadata.getAuthors().length > 0)
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.tungsten.fclcore.mod.modinfo;
2+
3+
import java.util.List;
4+
5+
/// @author Glavo
6+
public class ForgeOldModMetadataLst {
7+
private int modListVersion;
8+
private List<ForgeOldModMetadata> modList;
9+
10+
public int getModListVersion() {
11+
return modListVersion;
12+
}
13+
14+
public void setModListVersion(int modListVersion) {
15+
this.modListVersion = modListVersion;
16+
}
17+
18+
public List<ForgeOldModMetadata> getModList() {
19+
return modList;
20+
}
21+
22+
public void setModList(List<ForgeOldModMetadata> modList) {
23+
this.modList = modList;
24+
}
25+
}

0 commit comments

Comments
 (0)