|
4 | 4 | import com.google.gson.JsonArray; |
5 | 5 | import com.google.gson.JsonObject; |
6 | 6 | import com.google.gson.JsonParser; |
| 7 | +import com.moandjiezana.toml.Toml; |
7 | 8 | import jakarta.annotation.Nonnull; |
8 | 9 | import jakarta.enterprise.context.ApplicationScoped; |
9 | 10 | import jakarta.inject.Inject; |
|
27 | 28 | import java.io.ByteArrayInputStream; |
28 | 29 | import java.util.ArrayList; |
29 | 30 | import java.util.List; |
| 31 | +import java.util.Map; |
| 32 | +import java.util.Objects; |
| 33 | +import java.util.function.Supplier; |
30 | 34 | import java.util.jar.Manifest; |
31 | 35 |
|
32 | 36 | /** |
@@ -192,7 +196,59 @@ private boolean detectLowVersionForgeMod(@Nonnull WorkspaceResource resource) { |
192 | 196 | private boolean detectHighVersionForgeMod(@Nonnull WorkspaceResource resource) { |
193 | 197 | FileInfo forgeFileInfo = resource.getFileBundle().get("META-INF/mods.toml"); |
194 | 198 | if (forgeFileInfo != null) { |
| 199 | + loaderName = Lang.getBinding("service.analysis.is-forge-mod").get(); |
| 200 | + |
195 | 201 | // reference: https://mcforge.readthedocs.io/en/latest/gettingstarted/modfiles/ |
| 202 | + // 1. find modId in the [[mods]] section |
| 203 | + String modId = ""; |
| 204 | + Toml toml = new Toml(); |
| 205 | + try { |
| 206 | + toml.read(forgeFileInfo.asTextFile().getText()); |
| 207 | + List<Object> mods = toml.getList("mods"); |
| 208 | + if (mods != null && !mods.isEmpty()) { |
| 209 | + Object firstMod = mods.getFirst(); |
| 210 | + if (firstMod instanceof Map<?, ?> map) { |
| 211 | + if (map.containsKey("modId")) { |
| 212 | + modId = map.get("modId").toString(); |
| 213 | + } |
| 214 | + } |
| 215 | + } |
| 216 | + } catch (Exception e) { |
| 217 | + // Ignore TOML parsing errors |
| 218 | + } |
| 219 | + |
| 220 | + // 2. find versionRange in the [[dependencies.<modId>]] section and the modId of this must is "minecraft" |
| 221 | + if (!modId.isEmpty()) { |
| 222 | + try { |
| 223 | + List<Object> dependencies = toml.getList("dependencies." + modId); |
| 224 | + if (dependencies != null && !dependencies.isEmpty()) { |
| 225 | + for (Object dep : dependencies) { |
| 226 | + if (dep instanceof Map<?, ?> map) { |
| 227 | + if (map.containsKey("modId") && map.get("modId").toString().equals("minecraft")) { |
| 228 | + if (map.containsKey("versionRange")) { |
| 229 | + mcVersion = map.get("versionRange").toString(); |
| 230 | + break; |
| 231 | + } |
| 232 | + } |
| 233 | + } |
| 234 | + } |
| 235 | + } |
| 236 | + } catch (Exception e) { |
| 237 | + // Ignore TOML parsing errors |
| 238 | + } |
| 239 | + } |
| 240 | + |
| 241 | + // we can not find main class in mods.toml, so we scan all class files for @Mod annotation |
| 242 | + resource.jvmClassBundleStream().forEach(bundle -> { |
| 243 | + bundle.forEach(cls -> { |
| 244 | + Supplier<JvmClassInfo> classLookup = () -> Objects.requireNonNullElse(bundle.get(cls.getName()), cls); |
| 245 | + classLookup.get().getAnnotations().forEach(annotationInfo -> { |
| 246 | + if (annotationInfo.getDescriptor().equals("Lnet/minecraftforge/fml/common/Mod;")) { |
| 247 | + mainClasses.add(cls.getName()); |
| 248 | + } |
| 249 | + }); |
| 250 | + }); |
| 251 | + }); |
196 | 252 |
|
197 | 253 | return true; |
198 | 254 | } |
|
0 commit comments