From d0ee6c9039103ed782574fe8c585eed395abcccd Mon Sep 17 00:00:00 2001 From: XTerPL Date: Tue, 21 May 2024 17:44:04 +0200 Subject: [PATCH 1/2] fix a goddamn stack overflow when serializing a dictionary also added missing methods to ScriptVariable and fixed wrong serializer used for booleans --- .../dfscript/script/ScriptManager.java | 4 +-- .../script/values/ScriptDictionaryValue.java | 4 +-- .../script/values/ScriptVariable.java | 33 ++++++++++++++++--- 3 files changed, 32 insertions(+), 9 deletions(-) diff --git a/src/main/java/io/github/techstreet/dfscript/script/ScriptManager.java b/src/main/java/io/github/techstreet/dfscript/script/ScriptManager.java index 788461e..509acdb 100644 --- a/src/main/java/io/github/techstreet/dfscript/script/ScriptManager.java +++ b/src/main/java/io/github/techstreet/dfscript/script/ScriptManager.java @@ -21,7 +21,6 @@ import io.github.techstreet.dfscript.script.options.ScriptNamedOption; import io.github.techstreet.dfscript.script.repetitions.ScriptBuiltinRepetition; import io.github.techstreet.dfscript.script.repetitions.ScriptWhile; -import io.github.techstreet.dfscript.script.util.ScriptValueJson; import io.github.techstreet.dfscript.script.values.*; import io.github.techstreet.dfscript.util.FileUtil; import io.github.techstreet.dfscript.util.chat.ChatType; @@ -77,7 +76,8 @@ public class ScriptManager implements Loadable { .registerTypeAdapter(ScriptTextValue.class, new ScriptTextValue.Serializer()) .registerTypeAdapter(ScriptListValue.class, new ScriptListValue.Serializer()) .registerTypeAdapter(ScriptDictionaryValue.class, new ScriptDictionaryValue.Serializer()) - .registerTypeAdapter(ScriptBoolValue.class, new ScriptDictionaryValue.Serializer()) + .registerTypeAdapter(ScriptBoolValue.class, new ScriptBoolValue.Serializer()) + .registerTypeAdapter(ScriptVariable.class, new ScriptVariable.Serializer()) .create(); public ScriptManager() { diff --git a/src/main/java/io/github/techstreet/dfscript/script/values/ScriptDictionaryValue.java b/src/main/java/io/github/techstreet/dfscript/script/values/ScriptDictionaryValue.java index cd52a00..26b7007 100644 --- a/src/main/java/io/github/techstreet/dfscript/script/values/ScriptDictionaryValue.java +++ b/src/main/java/io/github/techstreet/dfscript/script/values/ScriptDictionaryValue.java @@ -3,9 +3,7 @@ import com.google.gson.*; import java.lang.reflect.Type; -import java.util.ArrayList; import java.util.HashMap; -import java.util.List; import java.util.Map; public class ScriptDictionaryValue extends ScriptValue { @@ -89,7 +87,7 @@ public JsonElement serialize(ScriptDictionaryValue scriptValue, Type type, JsonS dictObj.add(key, context.serialize(dict.get(key))); } JsonObject obj = new JsonObject(); - obj.add("dict", obj); + obj.add("dict", dictObj); obj.addProperty("___objectType", "dict"); return obj; } diff --git a/src/main/java/io/github/techstreet/dfscript/script/values/ScriptVariable.java b/src/main/java/io/github/techstreet/dfscript/script/values/ScriptVariable.java index 6b22566..40c9c83 100644 --- a/src/main/java/io/github/techstreet/dfscript/script/values/ScriptVariable.java +++ b/src/main/java/io/github/techstreet/dfscript/script/values/ScriptVariable.java @@ -1,12 +1,10 @@ package io.github.techstreet.dfscript.script.values; -import io.github.techstreet.dfscript.script.values.ScriptUnknownValue; -import io.github.techstreet.dfscript.script.values.ScriptValue; +import com.google.gson.*; +import java.lang.reflect.Type; import java.util.HashMap; import java.util.List; -import java.util.Map; -import java.util.stream.Collectors; public class ScriptVariable extends ScriptValue { private ScriptValue value; @@ -24,6 +22,11 @@ public ScriptValue get() { return value; } + @Override + public String toString() { + return asText(); + } + public void set(ScriptValue value) { this.value = value; } @@ -62,4 +65,26 @@ String typeName() { public boolean valueEquals(ScriptValue other) { return get().valueEquals(other); } + + @Override + public ScriptValue getCompareValue() { + return get().getCompareValue(); + } + + @Override + public int compare(ScriptValue other) { + return get().compare(other); + } + + @Override + public String formatAsText() { + return get().formatAsText(); + } + + public static class Serializer implements JsonSerializer { + @Override + public JsonElement serialize(ScriptVariable scriptValue, Type type, JsonSerializationContext context) { + return context.serialize(scriptValue.get()); + } + } } From 8b5c2d663de4eaf9a0cf6c2167ad710062874e05 Mon Sep 17 00:00:00 2001 From: XTerPL Date: Tue, 21 May 2024 19:12:06 +0200 Subject: [PATCH 2/2] fixed the scroll not being saved in some instances in ScriptEditScreen --- .../techstreet/dfscript/screen/CScreen.java | 14 +++++++++++++- .../script/ScriptAddConfigValueScreen.java | 6 ++---- .../ScriptAddFunctionArgValueScreen.java | 5 ++--- .../script/ScriptAddSettingSubtypeScreen.java | 4 +--- .../screen/script/ScriptEditScreen.java | 19 ++++++++++++------- .../screen/script/ScriptSettingsScreen.java | 4 ++++ .../dfscript/script/ScriptSnippet.java | 16 ++++++++-------- 7 files changed, 42 insertions(+), 26 deletions(-) diff --git a/src/main/java/io/github/techstreet/dfscript/screen/CScreen.java b/src/main/java/io/github/techstreet/dfscript/screen/CScreen.java index bb5c533..8f4c455 100644 --- a/src/main/java/io/github/techstreet/dfscript/screen/CScreen.java +++ b/src/main/java/io/github/techstreet/dfscript/screen/CScreen.java @@ -9,7 +9,6 @@ import net.minecraft.client.gui.DrawContext; import net.minecraft.client.gui.screen.Screen; import net.minecraft.client.util.math.MatrixStack; -import net.minecraft.item.Items; import net.minecraft.text.Text; import org.jetbrains.annotations.NotNull; @@ -25,6 +24,10 @@ protected CScreen(int width, int height) { // DFScript.MC.keyboard.setRepeatEvents(true); } + public void changeScreen(Screen screen) { + DFScript.MC.setScreen(screen); + } + @Override public void render(@NotNull DrawContext context, int mouseX, int mouseY, float tickDelta) { //renderBackground(context, mouseX, mouseY, tickDelta); @@ -174,4 +177,13 @@ public boolean mouseDragged(double mouseX, double mouseY, int button, double del return super.mouseDragged(mouseX, mouseY, button, deltaX, deltaY); } + + public static CScreen getCurrent() { + Screen current = DFScript.MC.currentScreen; + + if(current instanceof CScreen c) { + return c; + } + return null; + } } diff --git a/src/main/java/io/github/techstreet/dfscript/screen/script/ScriptAddConfigValueScreen.java b/src/main/java/io/github/techstreet/dfscript/screen/script/ScriptAddConfigValueScreen.java index 8472c9d..b0e1edd 100644 --- a/src/main/java/io/github/techstreet/dfscript/screen/script/ScriptAddConfigValueScreen.java +++ b/src/main/java/io/github/techstreet/dfscript/screen/script/ScriptAddConfigValueScreen.java @@ -6,8 +6,6 @@ import io.github.techstreet.dfscript.screen.widget.CScrollPanel; import io.github.techstreet.dfscript.script.Script; import io.github.techstreet.dfscript.script.ScriptParametrizedPart; -import io.github.techstreet.dfscript.script.ScriptPart; -import io.github.techstreet.dfscript.script.action.ScriptAction; import io.github.techstreet.dfscript.script.argument.ScriptConfigArgument; import io.github.techstreet.dfscript.script.event.ScriptHeader; import io.github.techstreet.dfscript.script.options.ScriptNamedOption; @@ -19,8 +17,8 @@ public class ScriptAddConfigValueScreen extends CScreen { private final ScriptParametrizedPart action; private final int insertIndex; - private static int WIDTH = 200; - private static int HEIGHT = 94; + private static final int WIDTH = 200; + private static final int HEIGHT = 94; public ScriptAddConfigValueScreen(ScriptParametrizedPart action, Script script, int insertIndex, ScriptHeader header, String overwrite) { super(WIDTH, HEIGHT); diff --git a/src/main/java/io/github/techstreet/dfscript/screen/script/ScriptAddFunctionArgValueScreen.java b/src/main/java/io/github/techstreet/dfscript/screen/script/ScriptAddFunctionArgValueScreen.java index 2fe6b9a..dfb40e6 100644 --- a/src/main/java/io/github/techstreet/dfscript/screen/script/ScriptAddFunctionArgValueScreen.java +++ b/src/main/java/io/github/techstreet/dfscript/screen/script/ScriptAddFunctionArgValueScreen.java @@ -7,7 +7,6 @@ import io.github.techstreet.dfscript.script.Script; import io.github.techstreet.dfscript.script.ScriptParametrizedPart; import io.github.techstreet.dfscript.script.action.ScriptActionArgument; -import io.github.techstreet.dfscript.script.argument.ScriptConfigArgument; import io.github.techstreet.dfscript.script.argument.ScriptFunctionArgument; import io.github.techstreet.dfscript.script.event.ScriptFunction; import io.github.techstreet.dfscript.script.event.ScriptHeader; @@ -19,8 +18,8 @@ public class ScriptAddFunctionArgValueScreen extends CScreen { private final ScriptParametrizedPart action; private final int insertIndex; - private static int WIDTH = 200; - private static int HEIGHT = 94; + private static final int WIDTH = 200; + private static final int HEIGHT = 94; public ScriptAddFunctionArgValueScreen(ScriptParametrizedPart action, Script script, int insertIndex, ScriptHeader header, String overwrite) { super(WIDTH, HEIGHT); diff --git a/src/main/java/io/github/techstreet/dfscript/screen/script/ScriptAddSettingSubtypeScreen.java b/src/main/java/io/github/techstreet/dfscript/screen/script/ScriptAddSettingSubtypeScreen.java index b4610bd..5c06932 100644 --- a/src/main/java/io/github/techstreet/dfscript/screen/script/ScriptAddSettingSubtypeScreen.java +++ b/src/main/java/io/github/techstreet/dfscript/screen/script/ScriptAddSettingSubtypeScreen.java @@ -10,7 +10,6 @@ import io.github.techstreet.dfscript.script.options.ScriptOptionEnum; import io.github.techstreet.dfscript.util.chat.ChatUtil; import net.minecraft.client.gui.DrawContext; -import net.minecraft.client.util.math.MatrixStack; import net.minecraft.item.ItemStack; import net.minecraft.item.Items; import net.minecraft.text.Text; @@ -19,7 +18,6 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.List; -import java.util.stream.Collectors; public class ScriptAddSettingSubtypeScreen extends CScreen { @@ -33,7 +31,7 @@ public class ScriptAddSettingSubtypeScreen extends CScreen { private int optionPos; - private List subtypes; + private final List subtypes; static { primitiveTypes = Arrays.stream(ScriptOptionEnum.values()).filter(c -> c.getExtraTypes() == 0).toList(); diff --git a/src/main/java/io/github/techstreet/dfscript/screen/script/ScriptEditScreen.java b/src/main/java/io/github/techstreet/dfscript/screen/script/ScriptEditScreen.java index 2b315e8..54e2eea 100644 --- a/src/main/java/io/github/techstreet/dfscript/screen/script/ScriptEditScreen.java +++ b/src/main/java/io/github/techstreet/dfscript/screen/script/ScriptEditScreen.java @@ -2,7 +2,6 @@ import io.github.techstreet.dfscript.DFScript; import io.github.techstreet.dfscript.screen.CReloadableScreen; -import io.github.techstreet.dfscript.screen.CScreen; import io.github.techstreet.dfscript.screen.ContextMenuButton; import io.github.techstreet.dfscript.screen.widget.*; import io.github.techstreet.dfscript.script.Script; @@ -12,8 +11,8 @@ import io.github.techstreet.dfscript.script.event.ScriptHeader; import net.minecraft.client.font.TextRenderer; import net.minecraft.client.gui.DrawContext; +import net.minecraft.client.gui.screen.Screen; import net.minecraft.client.sound.PositionedSoundInstance; -import net.minecraft.client.util.math.MatrixStack; import net.minecraft.sound.SoundEvents; import net.minecraft.text.Text; import net.minecraft.util.Identifier; @@ -57,7 +56,7 @@ public void reload() panel.add(name); CButton settings = new CTexturedButton(120-8, y, 8, 8, DFScript.MOD_ID + ":settings.png", DFScript.MOD_ID + ":settings_highlight.png", () -> { - DFScript.MC.setScreen(new ScriptSettingsScreen(this.script, true)); + changeScreen(new ScriptSettingsScreen(this.script, true)); }); panel.add(settings); @@ -86,10 +85,10 @@ public boolean mouseClicked(double x, double y, int button) { if (button != 0) { CButton insertBefore = new CButton((int) x, (int) y, 40, 8, "Insert Before", () -> { - DFScript.MC.setScreen(new ScriptHeaderCategoryScreen(script, currentIndex)); + changeScreen(new ScriptHeaderCategoryScreen(script, currentIndex)); }); CButton insertAfter = new CButton((int) x, (int) y+8, 40, 8, "Insert After", () -> { - DFScript.MC.setScreen(new ScriptHeaderCategoryScreen(script, currentIndex + 1)); + changeScreen(new ScriptHeaderCategoryScreen(script, currentIndex + 1)); }); CButton delete = new CButton((int) x, (int) y+16, 40, 8, "Delete", () -> { script.getHeaders().remove(currentIndex); @@ -109,7 +108,7 @@ public boolean mouseClicked(double x, double y, int button) { } else { if(header instanceof ScriptFunction f) { - DFScript.MC.setScreen(new ScriptEditFunctionScreen(f, script)); + changeScreen(new ScriptEditFunctionScreen(f, script)); } } return true; @@ -121,7 +120,7 @@ public boolean mouseClicked(double x, double y, int button) { } CButton add = new CButton(37, y, 46, 8, "Add Header", () -> { - DFScript.MC.setScreen(new ScriptHeaderCategoryScreen(script, script.getHeaders().size())); + changeScreen(new ScriptHeaderCategoryScreen(script, script.getHeaders().size())); }); panel.add(add); @@ -129,6 +128,12 @@ public boolean mouseClicked(double x, double y, int button) { panel.setScroll(scroll); } + @Override + public void changeScreen(Screen screen) { + scroll = panel.getScroll(); + DFScript.MC.setScreen(screen); + } + public void createIndent(int indent, int y) { for (int i = 0; i < indent; i += 5) { diff --git a/src/main/java/io/github/techstreet/dfscript/screen/script/ScriptSettingsScreen.java b/src/main/java/io/github/techstreet/dfscript/screen/script/ScriptSettingsScreen.java index 789a8b9..47babb1 100644 --- a/src/main/java/io/github/techstreet/dfscript/screen/script/ScriptSettingsScreen.java +++ b/src/main/java/io/github/techstreet/dfscript/screen/script/ScriptSettingsScreen.java @@ -92,9 +92,11 @@ public boolean mouseClicked(double x, double y, int button) { if (button != 0) { CButton insertBefore = new CButton((int) x, (int) y, 40, 8, "Insert Before", () -> { + scroll = panel.getScroll(); DFScript.MC.setScreen(new ScriptAddSettingScreen(script, finalIndex)); }); CButton insertAfter = new CButton((int) x, (int) y+8, 40, 8, "Insert After", () -> { + scroll = panel.getScroll(); DFScript.MC.setScreen(new ScriptAddSettingScreen(script, finalIndex + 1)); }); CButton delete = new CButton((int) x, (int) y+16, 40, 8, "Delete", () -> { @@ -107,6 +109,7 @@ public boolean mouseClicked(double x, double y, int button) { } else { + scroll = panel.getScroll(); DFScript.MC.setScreen(new ScriptEditSettingScreen(script, option)); } return true; @@ -126,6 +129,7 @@ public boolean mouseClicked(double x, double y, int button) { if(owned) { CButton add = new CButton(37, y, 48, 9, "Add Option", () -> { + scroll = panel.getScroll(); DFScript.MC.setScreen(new ScriptAddSettingScreen(script, script.getOptions().size())); }); panel.add(add); diff --git a/src/main/java/io/github/techstreet/dfscript/script/ScriptSnippet.java b/src/main/java/io/github/techstreet/dfscript/script/ScriptSnippet.java index e3abf69..bb4f9fe 100644 --- a/src/main/java/io/github/techstreet/dfscript/script/ScriptSnippet.java +++ b/src/main/java/io/github/techstreet/dfscript/script/ScriptSnippet.java @@ -2,6 +2,7 @@ import com.google.gson.*; import io.github.techstreet.dfscript.DFScript; +import io.github.techstreet.dfscript.screen.CScreen; import io.github.techstreet.dfscript.screen.ContextMenuButton; import io.github.techstreet.dfscript.screen.script.ScriptEditPartScreen; import io.github.techstreet.dfscript.screen.script.ScriptEditScreen; @@ -17,7 +18,6 @@ import io.github.techstreet.dfscript.script.conditions.ScriptConditionType; import io.github.techstreet.dfscript.script.event.ScriptHeader; import io.github.techstreet.dfscript.script.execution.ScriptActionContext; -import io.github.techstreet.dfscript.script.execution.ScriptPosStackElement; import io.github.techstreet.dfscript.script.execution.ScriptTask; import io.github.techstreet.dfscript.script.render.ScriptPartRender; import io.github.techstreet.dfscript.script.repetitions.ScriptBuiltinRepetition; @@ -33,7 +33,7 @@ import java.lang.reflect.Type; import java.util.ArrayList; import java.util.List; -import java.util.function.Consumer; +import java.util.Objects; public class ScriptSnippet extends ArrayList { boolean hidden = false; @@ -129,16 +129,16 @@ public boolean mouseClicked(double x, double y, int button) { if (button == 0) { if(part instanceof ScriptParametrizedPart parametrizedPart) - DFScript.MC.setScreen(new ScriptEditPartScreen(parametrizedPart, script, header)); + CScreen.getCurrent().changeScreen(new ScriptEditPartScreen(parametrizedPart, script, header)); if(part instanceof ScriptComment) return false; } else { List contextMenu = new ArrayList<>(); contextMenu.add(new ContextMenuButton("Insert Before", () -> { - DFScript.MC.setScreen(new ScriptPartCategoryScreen(script, thisSnippet, currentIndex)); + CScreen.getCurrent().changeScreen(new ScriptPartCategoryScreen(script, thisSnippet, currentIndex)); }, false)); contextMenu.add(new ContextMenuButton("Insert After", () -> { - DFScript.MC.setScreen(new ScriptPartCategoryScreen(script, thisSnippet, currentIndex + 1)); + CScreen.getCurrent().changeScreen(new ScriptPartCategoryScreen(script, thisSnippet, currentIndex + 1)); }, false)); contextMenu.add(new ContextMenuButton("Delete", () -> { thisSnippet.remove(currentIndex); @@ -162,7 +162,7 @@ public boolean mouseClicked(double x, double y, int button) { ScriptPartRender.createIndent(panel, indent, y, 8); CButton add = new CButton((ScriptEditScreen.width-30)/2, y, 30, 8, "Add Part", () -> { - DFScript.MC.setScreen(new ScriptPartCategoryScreen(script, thisSnippet, thisSnippet.size())); + CScreen.getCurrent().changeScreen(new ScriptPartCategoryScreen(script, thisSnippet, thisSnippet.size())); }); panel.add(add); @@ -247,7 +247,7 @@ public void removeOption(String option) { public void replaceFunction(String oldFunction, String newFunction) { for(ScriptPart part : this) { if(part instanceof ScriptFunctionCall fc) { - if(fc.getFunctionName() == oldFunction) { + if(Objects.equals(fc.getFunctionName(), oldFunction)) { fc.setFunction(newFunction); } } @@ -263,7 +263,7 @@ public void removeFunction(String function) { while(index < this.size()) { ScriptPart part = this.get(index); if(part instanceof ScriptFunctionCall fc) { - if(fc.getFunctionName() == function) { + if(Objects.equals(fc.getFunctionName(), function)) { this.remove(index); continue; }