Skip to content

Commit

Permalink
increase cache version
Browse files Browse the repository at this point in the history
  • Loading branch information
brachy84 committed Jul 24, 2024
1 parent 84a8e0c commit e8a7443
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 15 deletions.
2 changes: 1 addition & 1 deletion examples/runConfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,4 @@
"default": "normal",
"integratePackmodeMod": false
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<List<StackTraceElement>, 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<String, CompiledScript> index = new Object2ObjectOpenHashMap<>();

public static final boolean ENABLE_CACHE = true;

private LoadStage currentLoadStage;

public GroovyScriptSandbox(File scriptRoot, File cacheRoot) throws MalformedURLException {
Expand Down Expand Up @@ -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;
}
Expand All @@ -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<String, CompiledScript> entry : this.index.entrySet()) {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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()) {
Expand Down

0 comments on commit e8a7443

Please sign in to comment.