From b805a9d261414e6a8edfe8093b3e16c7328d411b Mon Sep 17 00:00:00 2001 From: Scribble Date: Tue, 24 Dec 2024 20:59:40 +0100 Subject: [PATCH] [Flavor] Add keyboard and mouse serialisation to AlphaFlavor - [PlaybackSersialiser] Switched from using toString() in keyboard and mouse to the same implementation. Reason is that toString in VirtualKeyboard might change in the fututure while the SerialiserFlavorBase is supposed to be backwards compatible --- .../tasfile/flavor/SerialiserFlavorBase.java | 17 ++- .../tasfile/flavor/builtin/AlphaFlavor.java | 120 ++++++++++++++++++ 2 files changed, 135 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/minecrafttas/tasmod/playback/tasfile/flavor/SerialiserFlavorBase.java b/src/main/java/com/minecrafttas/tasmod/playback/tasfile/flavor/SerialiserFlavorBase.java index 5d50abb8..703ed1e3 100644 --- a/src/main/java/com/minecrafttas/tasmod/playback/tasfile/flavor/SerialiserFlavorBase.java +++ b/src/main/java/com/minecrafttas/tasmod/playback/tasfile/flavor/SerialiserFlavorBase.java @@ -10,6 +10,9 @@ import java.util.concurrent.LinkedBlockingQueue; import java.util.regex.Matcher; import java.util.regex.Pattern; +import java.util.stream.Collectors; + +import org.apache.commons.lang3.StringUtils; import com.dselent.bigarraylist.BigArrayList; import com.minecrafttas.mctcommon.registry.Registerable; @@ -372,7 +375,7 @@ protected List serialiseKeyboard(VirtualKeyboard keyboard) { pruneListEndEmptySubtickable(subticks); for (VirtualKeyboard subtick : subticks) { - out.add(subtick.toString2()); + out.add(String.format("%s;%s", String.join(",", subtick.getCurrentPresses()), charListToString(subtick.getCharList()))); } return out; } @@ -384,7 +387,7 @@ protected List serialiseMouse(VirtualMouse mouse) { pruneListEndEmptySubtickable(subticks); for (VirtualMouse subtick : subticks) { - out.add(subtick.toString2()); + out.add(String.format("%s;%s,%s,%s", String.join(",", subtick.getCurrentPresses()), subtick.getScrollWheel(), subtick.getCursorX(), subtick.getCursorY())); } return out; } @@ -1303,4 +1306,14 @@ public boolean equals(Object obj) { public void setProcessExtensions(boolean processExtensions) { this.processExtensions = processExtensions; } + + protected String charListToString(List charList) { + String charString = ""; + if (!charList.isEmpty()) { + charString = charList.stream().map(Object::toString).collect(Collectors.joining()); + charString = StringUtils.replace(charString, "\r", "\\n"); + charString = StringUtils.replace(charString, "\n", "\\n"); + } + return charString; + } } diff --git a/src/main/java/com/minecrafttas/tasmod/playback/tasfile/flavor/builtin/AlphaFlavor.java b/src/main/java/com/minecrafttas/tasmod/playback/tasfile/flavor/builtin/AlphaFlavor.java index 0925880d..6c3b945b 100644 --- a/src/main/java/com/minecrafttas/tasmod/playback/tasfile/flavor/builtin/AlphaFlavor.java +++ b/src/main/java/com/minecrafttas/tasmod/playback/tasfile/flavor/builtin/AlphaFlavor.java @@ -18,6 +18,10 @@ import com.minecrafttas.tasmod.playback.metadata.PlaybackMetadata; import com.minecrafttas.tasmod.playback.tasfile.flavor.SerialiserFlavorBase; import com.minecrafttas.tasmod.registries.TASmodAPIRegistry; +import com.minecrafttas.tasmod.virtual.VirtualCameraAngle; +import com.minecrafttas.tasmod.virtual.VirtualKey; +import com.minecrafttas.tasmod.virtual.VirtualKeyboard; +import com.minecrafttas.tasmod.virtual.VirtualMouse; public class AlphaFlavor extends SerialiserFlavorBase { @@ -106,6 +110,122 @@ public boolean checkFlavorName(List headerLines) { return false; } + @Override + protected List serialiseKeyboard(VirtualKeyboard keyboard) { + /* + * Old code from when I did not know String.join exists, + * kept relatively unaltered because I want to. + */ + List out = new ArrayList<>(); + + List stringy = keyboard.getCurrentPresses(); + String keyString = ""; + if (!stringy.isEmpty()) { + String seperator = ","; + for (int i = 0; i < stringy.size(); i++) { + if (i == stringy.size() - 1) { + seperator = ""; + } + keyString = keyString.concat(stringy.get(i) + seperator); + } + } + List charList = keyboard.getCharList(); + String charString = ""; + if (!charList.isEmpty()) { + for (int i = 0; i < charList.size(); i++) { + charString = charString.concat(Character.toString(charList.get(i))); + } + charString = charString.replace("\r", "\\n"); + charString = charString.replace("\n", "\\n"); + } + + out.add("Keyboard:" + keyString + ";" + charString); // Keyboard didn't support subticks, only the current key is processed + + return out; + } + + @Override + protected List serialiseMouse(VirtualMouse mouse) { + /* + * Old code from when I did not know String.join exists, + * kept relatively unaltered because I want to. + */ + List out = new ArrayList<>(); + List stringy = mouse.getCurrentPresses(); + String keyString = ""; + if (!stringy.isEmpty()) { + String seperator = ","; + for (int i = 0; i < stringy.size(); i++) { + if (i == stringy.size() - 1) { + seperator = ""; + } + keyString = keyString.concat(stringy.get(i) + seperator); + } + } + + List path = new ArrayList<>(mouse.getSubticks()); // I previously called subticks "paths" as it was mainly used for the mouse... +// pruneListEndEmptySubtickable(path); + + /* + * The mouse supported subticks, + * but it was handled differently in alpha... + * The subticks where added in square brackets, seperated by a "->" + * Not the best solution in hindsight, + * but that was apparently the first thing that came to my mind back then... + */ + String pathString = ""; + if (!path.isEmpty()) { + String seperator = "->"; + for (int i = 0; i < path.size(); i++) { + if (i == path.size() - 1) { + seperator = ""; + } + + VirtualMouse singlePath = path.get(i); + + pathString = pathString.concat("[" + serialisePath(singlePath) + "]" + seperator); + } + } + out.add("Mouse:" + keyString + ";" + pathString); + return out; + } + + protected String serialisePath(VirtualMouse path) { + String keyString = ""; + List strings = new ArrayList(); + + path.getPressedKeys().forEach((virtualkeys) -> { + strings.add(VirtualKey.getName(virtualkeys)); + }); + if (!strings.isEmpty()) { + String seperator = ","; + for (int i = 0; i < strings.size(); i++) { + if (i == strings.size() - 1) { + seperator = ""; + } + keyString = keyString.concat(strings.get(i) + seperator); + } + } + if (keyString.isEmpty()) { + return "MOUSEMOVED," + path.getScrollWheel() + "," + path.getCursorX() + "," + path.getCursorY(); + } else { + return keyString + "," + path.getScrollWheel() + "," + path.getCursorX() + "," + path.getCursorY(); + } + } + + @Override + protected List serialiseCameraAngle(VirtualCameraAngle subticks) { + List out = new ArrayList<>(); + + /* + * The camera was called "subticks" in previous iterations of this code. + * To honor this fact, it is also called subticks here, even though + * actual subticks were not supported + */ + out.add("Camera:" + subticks.getPitch() + ";" + subticks.getYaw()); + return out; + } + @Override protected void deserialiseMetadata(List headerLines) { String author = "Insert author here";