Skip to content

Commit 3ad05b6

Browse files
committed
stage2/ml9: Improve workaround for incorrectly reported versions
There are really two independent issues, the `toString` and the fact that it returns no version, which may happen independently. As such, this commit also unwraps the `toString` result of non-empty `Optional`s, and runs the fallback version reading code when the raw version reported is already `null`.
1 parent b4b0a7e commit 3ad05b6

File tree

1 file changed

+15
-7
lines changed

1 file changed

+15
-7
lines changed

stage2/modlauncher9/src/main/java/gg/essential/loader/stage2/util/SortedJarOrPathList.java

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -56,15 +56,22 @@ private ArtifactVersion getVersion(Object pathOrJar) {
5656
JarMetadata metadata = getMetadata(jar);
5757
if (metadata == null) return FALLBACK_VERSION;
5858
String version = metadata.version();
59-
if (version == null) return FALLBACK_VERSION;
60-
// ModLauncher somehow manages to report the version of some jars (really unsure which ones, best guess
61-
// right now is all those without a `FMLModType` manifest attribute? seemingly completely irregardless of
62-
// whether they have an `Implementation-Version` attribute!)
63-
// as "Optional.empty" (yes, that's a String, somewhere someone must have blindly `toString`ed).
64-
// We'll just go fetch it ourselves then I guess.
65-
if (version.equals("Optional.empty")) {
59+
60+
// Some revisions of ModLauncher have a bug where they simply call `toString` on `Optional<String>`, resulting
61+
// in versions being reported as the string "Optional.empty" or "Optional[1.2.3]" instead of `null` or "1.2.3".
62+
// See https://github.com/McModLauncher/securejarhandler/blob/7cd8481364d73bacecf2b608479c6b903bff7f6c/src/main/java/cpw/mods/jarhandling/impl/ModuleJarMetadata.java#L137
63+
// We need to unwrap those to get at the real version.
64+
if (version != null && version.equals("Optional.empty")) {
6665
version = null;
66+
} else if (version != null && version.startsWith("Optional[")) {
67+
version = version.substring("Optional[".length(), version.length() - 1);
6768
}
69+
70+
// Additionally, when ModuleJarMetadata is used (not entirely sure when that's the case, at the very least the
71+
// jar must have a `module-info.class` but mods may also use ModJarMetadata instead), ModLauncher only looks at
72+
// the version declared in the `module-info.class`. For most jars that version is `null` though because it
73+
// requires extra setup in Gradle which most people don't do.
74+
// We need a version for correct sorting though, so we'll try to find one ourselves.
6875
if (version == null) {
6976
Manifest manifest = compatibilityLayer.getManifest(jar);
7077
if (manifest != null) {
@@ -79,6 +86,7 @@ private ArtifactVersion getVersion(Object pathOrJar) {
7986
version = name.substring(name.lastIndexOf("-") + 1, name.length() - ".jar".length());
8087
}
8188
}
89+
8290
if (version == null) {
8391
return FALLBACK_VERSION;
8492
}

0 commit comments

Comments
 (0)