aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/moe/nea/caelo/optifine
diff options
context:
space:
mode:
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