Skip to content

Commit 8969f55

Browse files
authored
[Bugfix] 使用 tomlj 代替 toml4j 以适配 TOML 标准 (#5863)
Resolves #5544
1 parent 312a285 commit 8969f55

4 files changed

Lines changed: 38 additions & 16 deletions

File tree

HMCL/src/main/resources/assets/about/deps.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,5 +93,10 @@
9393
"title": "LWJGL Unsafe Agent",
9494
"subtitle": "Copyright © 2026 Glavo.\nLicensed under the Apache 2.0 License.",
9595
"externalLink": "https://github.com/HMCL-dev/lwjgl-unsafe-agent"
96+
},
97+
{
98+
"title": "TomlJ",
99+
"subtitle": "Licensed under the Apache 2.0 License.",
100+
"externalLink": "https://github.com/tomlj/tomlj"
96101
}
97102
]

HMCLCore/build.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ dependencies {
1616
api(libs.kala.compress.tar)
1717
api(libs.simple.png.javafx)
1818
api(libs.gson)
19-
api(libs.toml)
19+
api(libs.tomlj)
2020
api(libs.xz)
2121
api(libs.lz4)
2222
api(libs.fx.gson)

HMCLCore/src/main/java/org/jackhuang/hmcl/mod/modinfo/ForgeNewModMetadata.java

Lines changed: 30 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
import com.google.gson.JsonParseException;
2525
import com.google.gson.JsonPrimitive;
2626
import com.google.gson.annotations.JsonAdapter;
27-
import com.moandjiezana.toml.Toml;
2827
import kala.compress.archivers.zip.ZipArchiveEntry;
2928
import org.jackhuang.hmcl.mod.LocalModFile;
3029
import org.jackhuang.hmcl.mod.ModLoaderType;
@@ -35,15 +34,19 @@
3534
import org.jackhuang.hmcl.util.gson.Validation;
3635
import org.jackhuang.hmcl.util.io.CompressingUtils;
3736
import org.jackhuang.hmcl.util.tree.ZipFileTree;
37+
import org.tomlj.Toml;
38+
import org.tomlj.TomlArray;
39+
import org.tomlj.TomlParseResult;
40+
import org.tomlj.TomlTable;
3841

3942
import java.io.IOException;
4043
import java.io.InputStream;
4144
import java.lang.reflect.Type;
4245
import java.nio.file.Files;
4346
import java.nio.file.Path;
4447
import java.util.ArrayList;
45-
import java.util.HashMap;
4648
import java.util.List;
49+
import java.util.Map;
4750
import java.util.StringJoiner;
4851
import java.util.jar.Attributes;
4952
import java.util.jar.Manifest;
@@ -208,10 +211,15 @@ private static LocalModFile fromFile0(
208211
ZipArchiveEntry modToml = tree.getEntry(tomlPath);
209212
if (modToml == null)
210213
throw new IOException("File " + modFile + " is not a Forge 1.13+ or NeoForge mod.");
211-
Toml toml = new Toml().read(tree.readTextEntry(modToml));
212-
ForgeNewModMetadata metadata = toml.to(ForgeNewModMetadata.class);
214+
TomlParseResult tomlParseResult = Toml.parse(tree.readTextEntry(modToml));
215+
if (tomlParseResult.hasErrors()) {
216+
var ioException = new IOException("Mod " + modFile + " `%s` is malformed..".formatted(modToml.getName()));
217+
tomlParseResult.errors().forEach(ioException::addSuppressed);
218+
throw ioException;
219+
}
220+
ForgeNewModMetadata metadata = JsonUtils.GSON.fromJson(tomlParseResult.toJson(), ForgeNewModMetadata.class);
213221
if (metadata == null || metadata.getMods().isEmpty())
214-
throw new IOException("Mod " + modFile + " `mods.toml` is malformed..");
222+
throw new IOException("Mod " + modFile + " `%s` is malformed..".formatted(modToml.getName()));
215223
Mod mod = metadata.getMods().get(0);
216224
ZipArchiveEntry manifestMF = tree.getEntry("META-INF/MANIFEST.MF");
217225
String jarVersion = "";
@@ -224,7 +232,7 @@ private static LocalModFile fromFile0(
224232
}
225233
}
226234

227-
ModLoaderType type = analyzeLoader(toml, mod.getModId(), modLoaderType);
235+
ModLoaderType type = analyzeLoader(tomlParseResult, mod.getModId(), modLoaderType);
228236

229237
return new LocalModFile(modManager, modManager.getLocalMod(mod.getModId(), type), modFile, mod.getDisplayName(), new LocalModFile.Description(mod.getDescription()),
230238
mod.getAuthors(), jarVersion == null ? mod.getVersion() : mod.getVersion().replace("${file.jarVersion}", jarVersion), "",
@@ -293,23 +301,32 @@ private static LocalModFile fromEmbeddedMod(ModManager modManager, Path modFile,
293301
throw new IOException();
294302
}
295303

296-
private static ModLoaderType analyzeLoader(Toml toml, String modID, ModLoaderType loader) {
297-
List<HashMap<String, Object>> dependencies = null;
304+
private static ModLoaderType analyzeLoader(TomlParseResult toml, String modID, ModLoaderType loader) {
305+
List<Map<String, Object>> dependencies = null;
298306
try {
299-
dependencies = toml.getList("dependencies." + modID);
307+
TomlArray tomlArray = toml.getArray("dependencies." + modID);
308+
if (tomlArray != null) {
309+
dependencies = tomlArray.toList().stream().map( o -> ((TomlTable) o).toMap()).toList();
310+
}
300311
} catch (ClassCastException ignored) { // https://github.com/HMCL-dev/HMCL/issues/5068
301312
}
302313

303314
if (dependencies == null) {
304315
try {
305-
dependencies = toml.getList("dependencies"); // ??? I have no idea why some of the Forge mods use [[dependencies]]
316+
TomlArray tomlArray = toml.getArray("dependencies"); // ??? I have no idea why some of the Forge mods use [[dependencies]]
317+
if (tomlArray != null) {
318+
dependencies = tomlArray.toList().stream().map( o -> ((TomlTable) o).toMap()).toList();
319+
}
306320
} catch (ClassCastException e) {
307321
try {
308-
Toml table = toml.getTable("dependencies");
322+
TomlTable table = toml.getTable("dependencies");
309323
if (table == null)
310324
return loader;
311325

312-
dependencies = table.getList(modID);
326+
TomlArray tomlArray = table.getArray(modID);
327+
if (tomlArray != null) {
328+
dependencies = tomlArray.toList().stream().map( o -> ((TomlTable) o).toMap()).toList();
329+
}
313330
} catch (Throwable ignored) {
314331
}
315332
}
@@ -321,7 +338,7 @@ private static ModLoaderType analyzeLoader(Toml toml, String modID, ModLoaderTyp
321338

322339
ModLoaderType result = null;
323340
loop:
324-
for (HashMap<String, Object> dependency : dependencies) {
341+
for (Map<String, Object> dependency : dependencies) {
325342
switch ((String) dependency.get("modId")) {
326343
case "forge":
327344
result = ModLoaderType.FORGE;

gradle/libs.versions.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ jetbrains-annotations = "26.1.0"
44
kala-compress = "1.27.1-3"
55
simple-png-javafx = "0.3.0"
66
gson = "2.13.2"
7-
toml4j = "0.7.2"
7+
tomlj = "1.1.1"
88
xz = "1.12"
99
lz4 = "1.10.4.1"
1010
fx-gson = "5.0.0"
@@ -38,7 +38,7 @@ kala-compress-zip = { module = "org.glavo.kala:kala-compress-archivers-zip", ver
3838
kala-compress-tar = { module = "org.glavo.kala:kala-compress-archivers-tar", version.ref = "kala-compress" }
3939
simple-png-javafx = { module = "org.glavo:simple-png-javafx", version.ref = "simple-png-javafx" }
4040
gson = { module = "com.google.code.gson:gson", version.ref = "gson" }
41-
toml = { module = "com.moandjiezana.toml:toml4j", version.ref = "toml4j" }
41+
tomlj = { module = "org.tomlj:tomlj", version.ref = "tomlj" }
4242
xz = { module = "org.tukaani:xz", version.ref = "xz" }
4343
lz4 = { module = "org.glavo:lz4-java", version.ref = "lz4" }
4444
fx-gson = { module = "org.hildan.fxgson:fx-gson", version.ref = "fx-gson" }

0 commit comments

Comments
 (0)