Skip to content

Commit

Permalink
[PlaybackSerialiser] Fixed "equals" in TickContainer
Browse files Browse the repository at this point in the history
Unsurprisingly, this broke all tests and I'm glad I caught this mistake early
- Cleaned up and added some tests
  • Loading branch information
ScribbleTAS committed Jun 19, 2024
1 parent 144b45f commit 62d85db
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -593,7 +593,7 @@ public static class TickContainer implements Serializable {
private CommentContainer comments;

public TickContainer(VirtualKeyboard keyboard, VirtualMouse mouse, VirtualCameraAngle subticks) {
this(keyboard, mouse, subticks, null);
this(keyboard, mouse, subticks, new CommentContainer());
}

public TickContainer(VirtualKeyboard keyboard, VirtualMouse mouse, VirtualCameraAngle camera, CommentContainer comments) {
Expand Down Expand Up @@ -639,7 +639,7 @@ public TickContainer clone() {
public boolean equals(Object other) {
if (other instanceof TickContainer) {
TickContainer container = (TickContainer) other;
return keyboard.equals(container.keyboard) || mouse.equals(container.mouse) || cameraAngle.equals(container.cameraAngle);
return keyboard.equals(container.keyboard) && mouse.equals(container.mouse) && cameraAngle.equals(container.cameraAngle) && comments.equals(container.comments);
}
return super.equals(other);
}
Expand Down Expand Up @@ -707,6 +707,15 @@ public List<String> getInlineComments() {
public List<String> getEndlineComments() {
return endlineComments;
}

@Override
public boolean equals(Object obj) {
if(obj instanceof CommentContainer) {
CommentContainer other = (CommentContainer) obj;
return inlineComments.equals(other.inlineComments) && endlineComments.equals(other.endlineComments);
}
return super.equals(obj);
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,10 +118,12 @@ public PlaybackFileCommandContainer(List<List<PlaybackFileCommand>> list) {
List<PlaybackFileCommand> val = entry.getValue();

boolean valuePresent = false;
for (PlaybackFileCommand command : lists) {
if (key.equals(command.getName())) {
valuePresent = true;
val.add(command);
if(lists!=null) {
for (PlaybackFileCommand command : lists) {
if (key.equals(command.getName())) {
valuePresent = true;
val.add(command);
}
}
}
if (!valuePresent) {
Expand Down Expand Up @@ -180,6 +182,17 @@ public List<List<PlaybackFileCommand>> valuesBySubtick() {

@Override
public boolean equals(Object o) {
if(o instanceof PlaybackFileCommandContainer) {
PlaybackFileCommandContainer other = (PlaybackFileCommandContainer) o;
for (java.util.Map.Entry<String, List<PlaybackFileCommand>> entry : other.entrySet()) {
String key = entry.getKey();
List<PlaybackFileCommand> val = entry.getValue();

if(!this.containsKey(key) && !this.get(key).equals(val))
return false;
}
return true;
}
return super.equals(o);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Queue;

import com.minecrafttas.mctcommon.registry.AbstractRegistry;
import com.minecrafttas.tasmod.TASmod;
Expand Down Expand Up @@ -122,11 +121,6 @@ public PlaybackFileCommandContainer handleOnSerialiseEndline(long currentTick, T
return out;
}

@FunctionalInterface
private interface OnSerialise {
Queue<PlaybackFileCommand> accept(PlaybackFileCommandExtension extension, long currentTick, TickContainer container);
}

public void handleOnDeserialiseInline(long currentTick, TickContainer deserialisedContainer, List<List<PlaybackFileCommand>> inlineFileCommands) {
PlaybackFileCommandContainer fileCommandContainer = new PlaybackFileCommandContainer(inlineFileCommands);
for (PlaybackFileCommandExtension extension : enabledExtensions) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,9 @@ public static void saveToFile(File file, BigArrayList<TickContainer> container,
writerThread.start();

SerialiserFlavorBase flavor = TASmodRegistry.SERIALISER_FLAVOR.getFlavor(flavorname);

List<PlaybackMetadata> metadataList = TASmodRegistry.PLAYBACK_METADATA.handleOnStore();
List<PlaybackFileCommandExtension> filecommandextensionList = TASmodRegistry.PLAYBACK_FILE_COMMAND.getEnabled();

for (String line : flavor.serialiseHeader(metadataList, filecommandextensionList)) {
List<String> header = flavor.serialiseHeader();
for (String line : header) {
writerThread.addLine(line);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ protected String headerEnd() {
==============================================*/

public List<String> serialiseHeader(List<PlaybackMetadata> metadataList, List<PlaybackFileCommandExtension> extensionList) {
public List<String> serialiseHeader() {
List<String> out = new ArrayList<>();
out.add(headerStart());
serialiseFlavorName(out);
Expand Down Expand Up @@ -641,6 +641,7 @@ protected String deserialiseFileCommands(String comment, List<PlaybackFileComman
String[] args = matcher.group(2).split(", ?");
deserialisedFileCommands.add(new PlaybackFileCommand(name, args));
comment = matcher.replaceFirst("");
matcher.reset(comment);
}

return comment;
Expand Down Expand Up @@ -802,7 +803,6 @@ protected float deserialiseRelativeFloat(String name, String floatstring, Float
protected void splitInputs(List<String> lines, List<String> serialisedKeyboard, List<String> serialisedMouse, List<String> serialisedCameraAngle, List<String> commentsAtEnd, List<List<PlaybackFileCommand>> endlineFileCommands) {

for (String line : lines) {
List<PlaybackFileCommand> deserialisedFileCommands = new ArrayList<>();

Matcher tickMatcher = extract("^\\t?\\d+\\|(.*?)\\|(.*?)\\|(\\S*)\\s?", line);
if (tickMatcher.find()) {
Expand All @@ -815,15 +815,19 @@ protected void splitInputs(List<String> lines, List<String> serialisedKeyboard,
if (!tickMatcher.group(3).isEmpty()) {
serialisedCameraAngle.add(tickMatcher.group(3));
}

List<PlaybackFileCommand> deserialisedFileCommands = new ArrayList<>();

String endlineComment = line.substring(tickMatcher.group(0).length());
commentsAtEnd.add(deserialiseEndlineComment(endlineComment, deserialisedFileCommands));

if (deserialisedFileCommands.isEmpty())
deserialisedFileCommands = null;

endlineFileCommands.add(deserialisedFileCommands);
}


if (deserialisedFileCommands.isEmpty())
deserialisedFileCommands = null;

endlineFileCommands.add(deserialisedFileCommands);
}
}

Expand Down
26 changes: 21 additions & 5 deletions src/test/java/tasmod/playback/tasfile/PlaybackSerialiserTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ public void onClear() {
private static class TestFileCommand extends PlaybackFileCommandExtension {

List<PlaybackFileCommandContainer> inline = new ArrayList<>();
List<PlaybackFileCommandContainer> endline = new ArrayList<>();

@Override
public String name() {
Expand All @@ -94,12 +95,17 @@ public String name() {

@Override
public void onDeserialiseInlineComment(long tick, TickContainer container, PlaybackFileCommandContainer fileCommandContainer) {
inline.add(fileCommandContainer);
inline.add(fileCommandContainer.split("testKey"));
}

@Override
public void onDeserialiseEndlineComment(long tick, TickContainer container, PlaybackFileCommandContainer fileCommandContainer) {
endline.add(fileCommandContainer.split("endlineKey"));
}

@Override
public String[] getFileCommandNames() {
return new String[]{"testKey"};
return new String[]{"testKey", "endlineKey"};
}
}

Expand All @@ -119,6 +125,7 @@ static void register() {
@AfterEach
void afterEach() {
testFileCommand.inline.clear();
testFileCommand.endline.clear();
}

@AfterAll
Expand Down Expand Up @@ -200,9 +207,11 @@ void testDeserialiser() throws PlaybackLoadException, IOException {
lines.add("### Test");
lines.add("TestKey: Wat");
lines.add("##################################################");
lines.add("// This is a regular comment");
lines.add("");
lines.add("// $testKey(test);");
lines.add("1|W;w|| // test");
lines.add("\t1|W,T;t||");
lines.add("\t1|W,T;t|| // $testKey(test);$endlineKey();");

File file = new File("src/test/resources/serialiser/PlaybackSerialiserTest2.mctas");
try {
Expand Down Expand Up @@ -230,11 +239,18 @@ void testDeserialiser() throws PlaybackLoadException, IOException {
List<PlaybackFileCommandContainer> fclist = new ArrayList<>();
PlaybackFileCommandContainer fccontainer = new PlaybackFileCommandContainer();
fccontainer.add("testKey", new PlaybackFileCommand("testKey", "test"));

fclist.add(fccontainer);

assertIterableEquals(fclist, testFileCommand.inline);

List<PlaybackFileCommandContainer> fclistEnd = new ArrayList<>();
PlaybackFileCommandContainer fccontainerEnd = new PlaybackFileCommandContainer();
fccontainerEnd.add("endlineKey", null);
fccontainerEnd.add("endlineKey", new PlaybackFileCommand("endlineKey"));

fclistEnd.add(fccontainerEnd);
assertIterableEquals(fclistEnd, testFileCommand.endline);

file.delete();
}

Expand Down
11 changes: 11 additions & 0 deletions src/test/resources/serialiser/PlaybackSerialiserTest2.mctas
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
TASFile
FileCommand-Extensions: tasmod_testFileExtension
Flavor: Test
### Test
TestKey: Wat
##################################################
// This is a regular comment

// $testKey(test);
1|W;w|| // test
1|W,T;t|| // $testKey(test);$endlineKey();

0 comments on commit 62d85db

Please sign in to comment.