From 8f870f092e6b808ef8aecd5a8cff23faa1026ba8 Mon Sep 17 00:00:00 2001 From: Gareth Coles Date: Thu, 11 May 2023 21:53:01 +0100 Subject: [PATCH] Logs: Java class version incompats --- .../logs/config/SimpleLogParserConfig.kt | 2 + .../JavaClassFileVersionProcessor.kt | 96 +++++++++++++++++++ 2 files changed, 98 insertions(+) create mode 100644 module-log-parser/src/main/kotlin/org/quiltmc/community/cozy/modules/logs/processors/JavaClassFileVersionProcessor.kt diff --git a/module-log-parser/src/main/kotlin/org/quiltmc/community/cozy/modules/logs/config/SimpleLogParserConfig.kt b/module-log-parser/src/main/kotlin/org/quiltmc/community/cozy/modules/logs/config/SimpleLogParserConfig.kt index 8227b3c8..35dde5cf 100644 --- a/module-log-parser/src/main/kotlin/org/quiltmc/community/cozy/modules/logs/config/SimpleLogParserConfig.kt +++ b/module-log-parser/src/main/kotlin/org/quiltmc/community/cozy/modules/logs/config/SimpleLogParserConfig.kt @@ -16,6 +16,7 @@ import org.quiltmc.community.cozy.modules.logs.parsers.launchers.ATLauncherParse import org.quiltmc.community.cozy.modules.logs.parsers.launchers.MMCLikeParser import org.quiltmc.community.cozy.modules.logs.parsers.launchers.TechnicParser import org.quiltmc.community.cozy.modules.logs.parsers.quilt.QuiltModsParser +import org.quiltmc.community.cozy.modules.logs.processors.JavaClassFileVersionProcessor import org.quiltmc.community.cozy.modules.logs.processors.MixinErrorProcessor import org.quiltmc.community.cozy.modules.logs.processors.PlayerIPProcessor import org.quiltmc.community.cozy.modules.logs.processors.UnknownModProcessor @@ -55,6 +56,7 @@ public class SimpleLogParserConfig(private val builder: Builder) : LogParserConf public var processors: MutableList = mutableListOf( FabricImplProcessor(), IncompatibleModProcessor(), + JavaClassFileVersionProcessor(), MixinErrorProcessor(), PlayerIPProcessor(), QuiltLibrariesVersionProcessor(), diff --git a/module-log-parser/src/main/kotlin/org/quiltmc/community/cozy/modules/logs/processors/JavaClassFileVersionProcessor.kt b/module-log-parser/src/main/kotlin/org/quiltmc/community/cozy/modules/logs/processors/JavaClassFileVersionProcessor.kt new file mode 100644 index 00000000..1489a6f0 --- /dev/null +++ b/module-log-parser/src/main/kotlin/org/quiltmc/community/cozy/modules/logs/processors/JavaClassFileVersionProcessor.kt @@ -0,0 +1,96 @@ +/* + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at https://mozilla.org/MPL/2.0/. + */ + +package org.quiltmc.community.cozy.modules.logs.processors + +import dev.kord.core.event.Event +import org.quiltmc.community.cozy.modules.logs.data.LoaderType +import org.quiltmc.community.cozy.modules.logs.data.Log +import org.quiltmc.community.cozy.modules.logs.data.Order +import org.quiltmc.community.cozy.modules.logs.types.LogProcessor + +private val ERROR_REGEX = ( + "has been compiled by a more recent version of the Java Runtime " + + "\\(class file version ([\\d\\.]+)\\), this version of the Java Runtime only recognizes class file versions " + + "up to ([\\d\\.]+)" + ).toRegex(RegexOption.IGNORE_CASE) + +// I'm not sure why it thinks these aren't constants, but okay... +@Suppress("MagicNumber") +private val VERSION_MAP = mutableMapOf( + 45 to "1.1", + 46 to "1.2", + 47 to "1.3", + 48 to "1.4", + 49 to "5", + 50 to "6", + 51 to "7", + 52 to "8", + 53 to "9", + 54 to "10", + 55 to "11", + 56 to "12", + 57 to "13", + 58 to "14", + 59 to "15", + 60 to "16", + 61 to "17", + 62 to "18", + 63 to "19", + 64 to "20", + 65 to "21", +) + +public class JavaClassFileVersionProcessor : LogProcessor() { + override val identifier: String = "piracy" + override val order: Order = Order.Earlier + + override suspend fun predicate(log: Log, event: Event): Boolean = + log.getLoaderVersion(LoaderType.Quilt) != null + + override suspend fun process(log: Log) { + val matches = ERROR_REGEX.findAll(log.content).toList() + + if (matches.isEmpty()) { + return + } + + // Required -> Current + val versions = matches.map { it.groupValues[1].toInt() to it.groupValues[2].toInt() } + val currentClass = versions.first().second + + var requiredClass: Int = 0 + + versions.forEach { requiredClass = maxOf(requiredClass, it.first) } + + val current = VERSION_MAP[currentClass] + val required = VERSION_MAP[requiredClass] + + log.addMessage( + buildString { + append("**It looks like you need to update Java.** ") + + append( + if (current != null) { + "You have Java `$current` installed, " + } else { + "You have a version of Java that supports up to class version `$currentClass`, " + } + ) + + append( + if (required != null) { + "but you need Java `$required` or later." + } else { + "but you need a version of java that supports class version `$requiredClass` or later." + } + ) + } + ) + + log.hasProblems = true + } +}