aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/moe/nea/caelo/optifine
diff options
context:
space:
mode:
authorLinnea Gräf <nea@nea.moe>2024-06-12 19:49:10 +0200
committerLinnea Gräf <nea@nea.moe>2024-06-12 19:49:10 +0200
commitd1738866ab1101ea847d2fa1505e323d75c2b10a (patch)
tree36fd9e0888872475543181d03fc79626d76dd450 /src/main/java/moe/nea/caelo/optifine
parent056f85b223bc106197c0a7dfea6b8ba2a4bc8a93 (diff)
downloadveloxcaelo-d1738866ab1101ea847d2fa1505e323d75c2b10a.tar.gz
veloxcaelo-d1738866ab1101ea847d2fa1505e323d75c2b10a.tar.bz2
veloxcaelo-d1738866ab1101ea847d2fa1505e323d75c2b10a.zip
Add regex cache
Diffstat (limited to 'src/main/java/moe/nea/caelo/optifine')
-rw-r--r--src/main/java/moe/nea/caelo/optifine/OptifineCustomItemCache.kt1
-rw-r--r--src/main/java/moe/nea/caelo/optifine/OptifineRegexCache.kt50
2 files changed, 51 insertions, 0 deletions
diff --git a/src/main/java/moe/nea/caelo/optifine/OptifineCustomItemCache.kt b/src/main/java/moe/nea/caelo/optifine/OptifineCustomItemCache.kt
index 244eff3..f343958 100644
--- a/src/main/java/moe/nea/caelo/optifine/OptifineCustomItemCache.kt
+++ b/src/main/java/moe/nea/caelo/optifine/OptifineCustomItemCache.kt
@@ -24,6 +24,7 @@ object OptifineCustomItemCache {
MC.display("- Insertions: §b${cache.insertions}")
MC.display("- Evictions: §b${cache.removals}")
MC.display("- Cache Size: §b${cache.size}")
+ OptifineRegexCache.printStats()
}
}
diff --git a/src/main/java/moe/nea/caelo/optifine/OptifineRegexCache.kt b/src/main/java/moe/nea/caelo/optifine/OptifineRegexCache.kt
new file mode 100644
index 0000000..daded38
--- /dev/null
+++ b/src/main/java/moe/nea/caelo/optifine/OptifineRegexCache.kt
@@ -0,0 +1,50 @@
+package moe.nea.caelo.optifine
+
+import moe.nea.caelo.config.CConfig
+import moe.nea.caelo.event.ResourceReloadEvent
+import moe.nea.caelo.util.MC
+import net.minecraftforge.fml.common.eventhandler.SubscribeEvent
+import org.apache.logging.log4j.LogManager
+import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable
+import java.util.regex.Pattern
+
+object OptifineRegexCache {
+ val cache: MutableMap<String, Pattern> = mutableMapOf()
+ val illegalRegexes = mutableSetOf<String>()
+ val logger = LogManager.getLogger()
+ val neverRegex = Pattern.compile("$.")
+
+ @SubscribeEvent
+ fun onResourcePackReload(resourceReload: ResourceReloadEvent) {
+ cache.clear()
+ }
+
+ private fun compilePattern(regex: String): Pattern {
+ return try {
+ Pattern.compile(regex)
+ } catch (ex: Exception) {
+ logger.error("Invalid regex $regex in optifine resource pack", ex)
+ illegalRegexes.add(regex)
+ neverRegex
+ }
+ }
+
+ fun matchesRegex(str: String, regex: String, cir: CallbackInfoReturnable<Boolean>) {
+ if (!CConfig.config.optiCache.regexCache) return
+ val pattern = cache.computeIfAbsent(regex, ::compilePattern)
+ cir.returnValue = pattern.matcher(str).matches()
+ }
+
+ fun printStats() {
+ MC.display("Regex Stats:")
+ MC.display("- Cache Size: §a${cache.size}")
+ if (illegalRegexes.isNotEmpty()) {
+ MC.display("- Illegal Regexes:")
+ for (illegalRegex in illegalRegexes) {
+ MC.display(" - §c${illegalRegex}")
+ }
+ }
+ }
+
+
+} \ No newline at end of file