From 67352bfe56a31cf388a88207a5a439c8269b3388 Mon Sep 17 00:00:00 2001 From: Rongmario Date: Fri, 10 Nov 2023 02:57:19 +0000 Subject: [PATCH 1/2] Catch exceptions, store and increment for cleanliness purposes --- .../sandbox/GroovyScriptSandbox.java | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/src/main/java/com/cleanroommc/groovyscript/sandbox/GroovyScriptSandbox.java b/src/main/java/com/cleanroommc/groovyscript/sandbox/GroovyScriptSandbox.java index f17208f6e..78793b980 100644 --- a/src/main/java/com/cleanroommc/groovyscript/sandbox/GroovyScriptSandbox.java +++ b/src/main/java/com/cleanroommc/groovyscript/sandbox/GroovyScriptSandbox.java @@ -14,6 +14,7 @@ import groovy.util.GroovyScriptEngine; import groovy.util.ResourceException; import groovy.util.ScriptException; +import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap; import it.unimi.dsi.fastutil.objects.ObjectOpenHashSet; import net.minecraft.util.math.MathHelper; import net.minecraftforge.common.MinecraftForge; @@ -25,13 +26,13 @@ import java.io.File; import java.io.IOException; import java.net.URL; -import java.util.Collection; -import java.util.Objects; -import java.util.Set; +import java.util.*; +import java.util.concurrent.atomic.AtomicInteger; public class GroovyScriptSandbox extends GroovySandbox { private final ImportCustomizer importCustomizer = new ImportCustomizer(); + private final Map, AtomicInteger> storedExceptions; private LoadStage currentLoadStage; @@ -69,6 +70,7 @@ public GroovyScriptSandbox(URL... scriptEnvironment) { "net.minecraft.util.ResourceLocation", "net.minecraftforge.fml.common.eventhandler.EventPriority", "com.cleanroommc.groovyscript.event.EventBusType"); + this.storedExceptions = new Object2ObjectOpenHashMap<>(); } public void checkSyntax() { @@ -109,9 +111,12 @@ public T runClosure(Closure closure, Object... args) { T result = null; try { result = closure.call(args); - } catch (Exception e) { - GroovyLog.get().error("An exception occurred while running a closure!"); - GroovyLog.get().exception(e); + } catch (Throwable t) { + this.storedExceptions.computeIfAbsent(Arrays.asList(t.getStackTrace()), k -> { + GroovyLog.get().error("An exception occurred while running a closure!"); + GroovyLog.get().exception(t); + return new AtomicInteger(1); + }).addAndGet(1); } finally { stopRunning(); } From 54525c73596269e65857332d2570ae810f43aaed Mon Sep 17 00:00:00 2001 From: Rongmario Date: Fri, 10 Nov 2023 02:58:19 +0000 Subject: [PATCH 2/2] Start at 0 for the exception counter as it is incremented straight after --- .../cleanroommc/groovyscript/sandbox/GroovyScriptSandbox.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/com/cleanroommc/groovyscript/sandbox/GroovyScriptSandbox.java b/src/main/java/com/cleanroommc/groovyscript/sandbox/GroovyScriptSandbox.java index 78793b980..156f13060 100644 --- a/src/main/java/com/cleanroommc/groovyscript/sandbox/GroovyScriptSandbox.java +++ b/src/main/java/com/cleanroommc/groovyscript/sandbox/GroovyScriptSandbox.java @@ -115,7 +115,7 @@ public T runClosure(Closure closure, Object... args) { this.storedExceptions.computeIfAbsent(Arrays.asList(t.getStackTrace()), k -> { GroovyLog.get().error("An exception occurred while running a closure!"); GroovyLog.get().exception(t); - return new AtomicInteger(1); + return new AtomicInteger(); }).addAndGet(1); } finally { stopRunning();