Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 13 additions & 14 deletions src/main/java/zone/rong/mixinbooter/service/ModDiscoverer.java
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,8 @@ public static void discover() {
return;
}
discovered = true;

long startTime = System.currentTimeMillis();
Gson gson;
try {
gson = new GsonBuilder().setLenient().create();
Expand All @@ -275,7 +277,7 @@ public static void discover() {
} catch (URISyntaxException ignored) { }
}

LOGGER.info("Finished gathering {} mods...", modIdToFiles.keySet().size());
LOGGER.info("Finished gathering {} mods, took {} ms.", modIdToFiles.keySet().size(), System.currentTimeMillis() - startTime);
LOGGER.debug("Mods gathered: {}", String.join(", ", modIdToFiles.keySet()));
}

Expand Down Expand Up @@ -349,12 +351,13 @@ private static void scanJar(Gson gson, File jar) {
}
}
ZipEntry entry = jarFile.getEntry("mcmod.info");
List<String> modIds = entry != null ? parseMcmodInfo(gson, jarFile.getInputStream(entry)) : Collections.emptyList();
if (modIds.isEmpty()) {
String modId = scanModAnnotation(jarFile);
if (modId != null) {
modIds = Collections.singletonList(modId);
}
Set<String> modIds = new HashSet<>();
if (entry != null) {
parseMcmodInfo(gson, jarFile.getInputStream(entry), modIds);
}
String modAnnotationId = scanModAnnotation(jarFile);
if (modAnnotationId != null) {
modIds.add(modAnnotationId);
}
for (String modId : modIds) {
recordMod(modId, jar);
Expand All @@ -374,7 +377,7 @@ private static void recordMod(String modId, File source) {
/**
* Logs if a mod jar bundles its own Mixin engine of any fork variety.
*/
private static void checkIfJarBundlesMixin(File jar, JarFile jarFile, List<String> modIds) {
private static void checkIfJarBundlesMixin(File jar, JarFile jarFile, Set<String> modIds) {
if (modIds.isEmpty() || modIds.contains("mixinbooter")) {
return;
}
Expand All @@ -383,9 +386,8 @@ private static void checkIfJarBundlesMixin(File jar, JarFile jarFile, List<Strin
}
}

private static List<String> parseMcmodInfo(Gson gson, InputStream stream) {
private static void parseMcmodInfo(Gson gson, InputStream stream, Set<String> ids) {
try {
List<String> ids = new ArrayList<>();
JsonElement root = gson.fromJson(new InputStreamReader(stream, StandardCharsets.UTF_8), JsonElement.class);
if (root.isJsonArray()) {
for (JsonElement element : root.getAsJsonArray()) {
Expand All @@ -400,19 +402,16 @@ private static List<String> parseMcmodInfo(Gson gson, InputStream stream) {
}
}
}
return ids;
} catch (Throwable t) {
LOGGER.error("Failed to parse mcmod.info", t);
} finally {
IOUtils.closeQuietly(stream);
}
return Collections.emptyList();
}

/**
* Scans the jar's classes for the first {@code @Mod} annotation and returns its {@code modid}
* or {@code null} if none declares one. This is the fallback for mods that declare their id via
* the annotation rather than {@code mcmod.info}.
* or {@code null} if none declares one.
* Reads bytecode only and unreadable entries are skipped, the walk stops at the first match.
*/
private static String scanModAnnotation(JarFile jar) {
Expand Down
Loading