Skip to content

Commit

Permalink
cache preprocessors, delete cache of deleted scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
brachy84 committed Mar 5, 2024
1 parent bf1ab75 commit 1b05b2f
Show file tree
Hide file tree
Showing 7 changed files with 191 additions and 95 deletions.
8 changes: 6 additions & 2 deletions src/main/java/com/cleanroommc/groovyscript/GroovyScript.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
package com.cleanroommc.groovyscript;

import com.cleanroommc.groovyscript.api.GroovyBlacklist;
import com.cleanroommc.groovyscript.api.GroovyLog;
import com.cleanroommc.groovyscript.command.CustomClickAction;
import com.cleanroommc.groovyscript.command.GSCommand;
import com.cleanroommc.groovyscript.compat.content.GroovyResourcePack;
import com.cleanroommc.groovyscript.compat.mods.ModSupport;
import com.cleanroommc.groovyscript.compat.mods.tinkersconstruct.TinkersConstruct;
import com.cleanroommc.groovyscript.compat.vanilla.VanillaModule;
import com.cleanroommc.groovyscript.core.mixin.DefaultResourcePackAccessor;
import com.cleanroommc.groovyscript.documentation.linkgenerator.LinkGeneratorHooks;
import com.cleanroommc.groovyscript.documentation.Documentation;
import com.cleanroommc.groovyscript.documentation.linkgenerator.LinkGeneratorHooks;
import com.cleanroommc.groovyscript.event.EventHandler;
import com.cleanroommc.groovyscript.gameobjects.GameObjectHandlerManager;
import com.cleanroommc.groovyscript.helper.JsonHelper;
Expand Down Expand Up @@ -53,7 +54,6 @@
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
import net.minecraftforge.fml.common.gameevent.InputEvent;
import net.minecraftforge.fml.relauncher.FMLLaunchHandler;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.jetbrains.annotations.ApiStatus;
Expand Down Expand Up @@ -164,6 +164,10 @@ public static void initializeGroovyPreInit() {
@ApiStatus.Internal
public static long runGroovyScriptsInLoader(LoadStage loadStage) {
// called via mixin between fml post init and load complete
if (!getRunConfig().isLoaderConfigured(loadStage.getName())) {
GroovyLog.get().infoMC("Skipping load stage {}, since no scripts are configured!", loadStage.getName());
return -1;
}
if (scriptMod == null) scriptMod = Loader.instance().getIndexedModList().get(getRunConfig().getPackId());
ModContainer current = Loader.instance().activeModContainer();
Loader.instance().setActiveModContainer(scriptMod);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,15 @@ public boolean readData(File basePath) {
}
}

private File getDataFile(File basePath) {
public void deleteCache(File cachePath) {
try {
Files.deleteIfExists(getDataFile(cachePath).toPath());
} catch (IOException e) {
throw new RuntimeException(e);
}
}

protected File getDataFile(File basePath) {
return new File(basePath, this.name + ".clz");
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
package com.cleanroommc.groovyscript.sandbox;

import com.cleanroommc.groovyscript.api.GroovyLog;
import com.cleanroommc.groovyscript.helper.JsonHelper;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import groovy.lang.GroovyClassLoader;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.jetbrains.annotations.NotNull;

import java.io.File;
import java.util.ArrayList;
Expand All @@ -13,6 +18,7 @@ class CompiledScript extends CompiledClass {
final String path;
final List<CompiledClass> innerClasses = new ArrayList<>();
long lastEdited;
List<String> preprocessors = null;

public CompiledScript(String path, long lastEdited) {
this(path, null, lastEdited);
Expand Down Expand Up @@ -54,6 +60,64 @@ public void ensureLoaded(GroovyClassLoader classLoader, File basePath) {
}
}

@NotNull
public JsonObject toJson() {
JsonObject jsonEntry = new JsonObject();
jsonEntry.addProperty("name", this.name);
jsonEntry.addProperty("path", this.path);
jsonEntry.addProperty("lm", this.lastEdited);
if (!this.innerClasses.isEmpty()) {
JsonArray inner = new JsonArray();
for (CompiledClass comp : this.innerClasses) {
inner.add(comp.name);
}
jsonEntry.add("inner", inner);
}
if (this.preprocessors != null && !this.preprocessors.isEmpty()) {
JsonArray jsonPp = new JsonArray();
for (String pp : this.preprocessors) {
jsonPp.add(pp);
}
jsonEntry.add("preprocessors", jsonPp);
}
return jsonEntry;
}

public static CompiledScript fromJson(JsonObject json, File scriptRoot, File cacheRoot) {
CompiledScript cs = new CompiledScript(json.get("path").getAsString(), JsonHelper.getString(json, null, "name"), json.get("lm").getAsLong());
if (new File(scriptRoot, cs.path).exists()) {
if (json.has("inner")) {
for (JsonElement element : json.getAsJsonArray("inner")) {
cs.innerClasses.add(new CompiledClass(element.getAsString()));
}
}
if (json.has("preprocessors")) {
cs.preprocessors = new ArrayList<>();
for (JsonElement element : json.getAsJsonArray("preprocessors")) {
cs.preprocessors.add(element.getAsString());
}
}
return cs;
}
// script file no longer exists -> delete cache
cs.deleteCache(cacheRoot);
return null;
}

@Override
public void deleteCache(File cachePath) {
super.deleteCache(cachePath);
for (CompiledClass cc : this.innerClasses) {
cc.deleteCache(cachePath);
}
}

public boolean checkPreprocessors(File basePath) {
return this.preprocessors == null ||
this.preprocessors.isEmpty() ||
Preprocessor.validatePreprocessor(new File(basePath, this.path), this.preprocessors);
}

@Override
public String toString() {
return new ToStringBuilder(this)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ protected void startRunning() {

protected void stopRunning() {
this.running.set(false);
currentSandbox.set(null);
currentSandbox.remove();
}

protected GroovyScriptEngine createScriptEngine() {
Expand Down Expand Up @@ -126,6 +126,7 @@ protected void loadScripts(GroovyScriptEngine engine, Binding binding, Set<File>
for (File scriptFile : getScriptFiles()) {
if (!executedClasses.contains(scriptFile)) {
Class<?> clazz = loadScriptClass(engine, scriptFile);
if (clazz == GroovyLog.class) continue; // preprocessor returned false
if (clazz == null) {
GroovyLog.get().errorMC("Error loading script for {}", scriptFile.getPath());
GroovyLog.get().errorMC("Did you forget to register your class file in your run config?");
Expand All @@ -151,6 +152,7 @@ protected void loadClassScripts(GroovyScriptEngine engine, Binding binding, Set<
for (File classFile : getClassFiles()) {
if (executedClasses.contains(classFile)) continue;
Class<?> clazz = loadScriptClass(engine, classFile);
if (clazz == GroovyLog.class) continue; // preprocessor returned false
if (clazz == null) {
// loading script fails if the file is a script that depends on a class file that isn't loaded yet
// we cant determine if the file is a script or a class
Expand Down
Loading

0 comments on commit 1b05b2f

Please sign in to comment.