|
| 1 | +package com.ysm.modelengine.molang; |
| 2 | + |
| 3 | +import gg.moonflower.molangcompiler.api.GlobalMolangCompiler; |
| 4 | +import gg.moonflower.molangcompiler.api.MolangCompiler; |
| 5 | +import gg.moonflower.molangcompiler.api.MolangExpression; |
| 6 | +import gg.moonflower.molangcompiler.api.exception.MolangException; |
| 7 | + |
| 8 | +import java.util.Map; |
| 9 | +import java.util.concurrent.ConcurrentHashMap; |
| 10 | + |
| 11 | +final class MolangCompilerFacade { |
| 12 | + private final MolangCompiler compiler = GlobalMolangCompiler.get(); |
| 13 | + private final Map<String, MolangExpression> cache = new ConcurrentHashMap<>(); |
| 14 | + private final int maxEntries; |
| 15 | + private final RateLimitedDiagnostics diagnostics; |
| 16 | + |
| 17 | + MolangCompilerFacade(int maxEntries, RateLimitedDiagnostics diagnostics) { |
| 18 | + this.maxEntries = Math.max(64, maxEntries); |
| 19 | + this.diagnostics = diagnostics; |
| 20 | + } |
| 21 | + |
| 22 | + MolangExpression compile(String source) { |
| 23 | + String normalized = normalize(source); |
| 24 | + if (normalized.isBlank()) { |
| 25 | + throw new IllegalArgumentException("Molang expression cannot be blank"); |
| 26 | + } |
| 27 | + |
| 28 | + MolangExpression cached = cache.get(normalized); |
| 29 | + if (cached != null) return cached; |
| 30 | + |
| 31 | + MolangExpression expression = compileUncached(normalized); |
| 32 | + if (cache.size() >= maxEntries) { |
| 33 | + diagnostics.warn("cache-full", "Molang expression cache is full; using uncached expression: " + normalized); |
| 34 | + return expression; |
| 35 | + } |
| 36 | + |
| 37 | + MolangExpression existing = cache.putIfAbsent(normalized, expression); |
| 38 | + return existing == null ? expression : existing; |
| 39 | + } |
| 40 | + |
| 41 | + void clear() { |
| 42 | + cache.clear(); |
| 43 | + } |
| 44 | + |
| 45 | + int size() { |
| 46 | + return cache.size(); |
| 47 | + } |
| 48 | + |
| 49 | + private MolangExpression compileUncached(String source) { |
| 50 | + try { |
| 51 | + return compiler.compile(source); |
| 52 | + } catch (MolangException | RuntimeException exception) { |
| 53 | + diagnostics.warn("compile:" + source, |
| 54 | + "Unable to compile Molang expression '" + source + "': " + exception.getMessage()); |
| 55 | + throw new IllegalArgumentException("Invalid Molang expression: " + source, exception); |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + static String normalize(String source) { |
| 60 | + if (source == null) return ""; |
| 61 | + String normalized = source.trim(); |
| 62 | + if (normalized.startsWith("molang:")) normalized = normalized.substring("molang:".length()).trim(); |
| 63 | + normalized = normalized.replace("ysm.head_yaw", "query.head_y_rotation"); |
| 64 | + normalized = normalized.replace("ysm.head_pitch", "query.head_x_rotation"); |
| 65 | + normalized = normalized.replace("ysm.body_yaw", "query.body_y_rotation"); |
| 66 | + normalized = normalized.replace("variable.", "v."); |
| 67 | + normalized = normalized.replace("ctrl.", "v.ctrl_"); |
| 68 | + return normalized; |
| 69 | + } |
| 70 | +} |
0 commit comments