blob: daded38e90746ce5c4306f8fc3968e5d8a8bf44f (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
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}")
}
}
}
}
|