From e8a7443c5a1a56cc01bbd12883b489792b0f0179 Mon Sep 17 00:00:00 2001 From: brachy84 Date: Wed, 24 Jul 2024 20:53:07 +0200 Subject: [PATCH] increase cache version --- examples/runConfig.json | 2 +- .../sandbox/GroovyScriptSandbox.java | 37 ++++++++++++------- 2 files changed, 24 insertions(+), 15 deletions(-) diff --git a/examples/runConfig.json b/examples/runConfig.json index ffc859430..6c7e947fd 100644 --- a/examples/runConfig.json +++ b/examples/runConfig.json @@ -23,4 +23,4 @@ "default": "normal", "integratePackmodeMod": false } -} \ No newline at end of file +} diff --git a/src/main/java/com/cleanroommc/groovyscript/sandbox/GroovyScriptSandbox.java b/src/main/java/com/cleanroommc/groovyscript/sandbox/GroovyScriptSandbox.java index 279c0e6b9..784fb05e1 100644 --- a/src/main/java/com/cleanroommc/groovyscript/sandbox/GroovyScriptSandbox.java +++ b/src/main/java/com/cleanroommc/groovyscript/sandbox/GroovyScriptSandbox.java @@ -41,19 +41,28 @@ public class GroovyScriptSandbox extends GroovySandbox { + /** + * Changing this number will force the cache to be deleted and every script has to be recompiled. + * Useful when changes to the compilation process were made. + */ + public static final int CACHE_VERSION = 2; + /** + * Setting this to false will cause compiled classes to never be cached. + * As a side effect some compilation behaviour might change. Can be useful for debugging. + */ + public static final boolean ENABLE_CACHE = true; + /** + * Setting this to true will cause the cache to be deleted before each script run. + * Useful for debugging. + */ + public static final boolean DELETE_CACHE_ON_RUN = false; + private final File cacheRoot; private final File scriptRoot; private final ImportCustomizer importCustomizer = new ImportCustomizer(); private final Map, AtomicInteger> storedExceptions; - /** - * Version of the cache. Used to auto delete current cache if changes to the cache system were made. - * 1: Default - */ - private int cacheVersion = 1; private final Map index = new Object2ObjectOpenHashMap<>(); - public static final boolean ENABLE_CACHE = true; - private LoadStage currentLoadStage; public GroovyScriptSandbox(File scriptRoot, File cacheRoot) throws MalformedURLException { @@ -105,9 +114,9 @@ private void readIndex() { JsonElement jsonElement = JsonHelper.loadJson(new File(this.cacheRoot, "_index.json")); if (jsonElement == null || !jsonElement.isJsonObject()) return; JsonObject json = jsonElement.getAsJsonObject(); - this.cacheVersion = json.get("version").getAsInt(); - if (this.cacheVersion != 1) { - // only version 1 allowed currently + int cacheVersion = json.get("version").getAsInt(); + if (cacheVersion != CACHE_VERSION) { + // cache version changed -> force delete cache deleteScriptCache(); return; } @@ -125,7 +134,7 @@ private void writeIndex() { if (!ENABLE_CACHE) return; JsonObject json = new JsonObject(); json.addProperty("!DANGER!", "DO NOT EDIT THIS FILE!!!"); - json.addProperty("version", this.cacheVersion); + json.addProperty("version", CACHE_VERSION); JsonArray index = new JsonArray(); json.add("index", index); for (Map.Entry entry : this.index.entrySet()) { @@ -241,9 +250,8 @@ public void onCompileClass(SourceUnit su, String path, Class clazz, byte[] co } /** - * Called via mixin when a script class needs to be recompiled. This happens when a script was loaded because - * another script depends on it. Groovy will then try to compile the script again. If we already compiled the class - * we just stop the compilation process. + * Called via mixin when a script class needs to be recompiled. This happens when a script was loaded because another script depends on + * it. Groovy will then try to compile the script again. If we already compiled the class we just stop the compilation process. */ @ApiStatus.Internal public Class onRecompileClass(GroovyClassLoader classLoader, URL source, String className) { @@ -321,6 +329,7 @@ protected void initEngine(GroovyScriptEngine engine, CompilerConfiguration confi @Override protected void preRun() { + if (DELETE_CACHE_ON_RUN) deleteScriptCache(); // first clear all added events GroovyEventManager.INSTANCE.reset(); if (this.currentLoadStage.isReloadable() && !ReloadableRegistryManager.isFirstLoad()) {