Skip to content

Commit

Permalink
[PlaybackSerialiser] Switched flavor to pass by reference
Browse files Browse the repository at this point in the history
  • Loading branch information
ScribbleTAS committed May 10, 2024
1 parent e09a4d6 commit fdf1953
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 31 deletions.
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
package com.minecrafttas.tasmod.playback.tasfile;

import java.io.File;
import java.util.ArrayList;
import java.io.FileNotFoundException;
import java.util.List;

import com.dselent.bigarraylist.BigArrayList;
import com.minecrafttas.tasmod.playback.PlaybackControllerClient;
import com.minecrafttas.tasmod.playback.PlaybackControllerClient.TickInputContainer;
import com.minecrafttas.tasmod.playback.metadata.PlaybackMetadata;
import com.minecrafttas.tasmod.playback.tasfile.flavor.PlaybackFlavorBase;
import com.minecrafttas.tasmod.util.FileThread;
import com.minecrafttas.tasmod.util.TASmodRegistry;

/**
Expand All @@ -28,15 +29,16 @@ public class PlaybackSerialiser2 {
* @param file The file to save the serialised inputs to.
* @param controller
* @param flavor
* @throws FileNotFoundException
*/
public static void saveToFile(File file, PlaybackControllerClient controller, String flavorname) {
public static void saveToFile(File file, PlaybackControllerClient controller, String flavorname) throws FileNotFoundException {
if (controller == null) {
throw new NullPointerException("Save to file failed. No controller specified");
}
saveToFile(file, controller.getInputs(), flavorname);
}

public static void saveToFile(File file, BigArrayList<TickInputContainer> container, String flavorname) {
public static void saveToFile(File file, BigArrayList<TickInputContainer> container, String flavorname) throws FileNotFoundException {
if (file == null) {
throw new NullPointerException("Save to file failed. No file specified");
}
Expand All @@ -49,12 +51,22 @@ public static void saveToFile(File file, BigArrayList<TickInputContainer> contai
flavorname = defaultFlavor;
}

FileThread writerThread = new FileThread(file, false);

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

List<PlaybackMetadata> metadataList = TASmodRegistry.PLAYBACK_METADATA.handleOnStore();

List<String> outLines = new ArrayList<>();
outLines.addAll(flavor.serialiseHeader(metadataList));
for (String line : flavor.serialiseHeader(metadataList)) {
writerThread.addLine(line);
}

BigArrayList<String> tickLines = flavor.serialise(container);
for (long i = 0; i < tickLines.size(); i++) {
writerThread.addLine(tickLines.get(i));
}

writerThread.close();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,24 +18,29 @@ public abstract class PlaybackFlavorBase {
/**
* The current tick that is being serialised or deserialised
*/
private int currentTick;
protected int currentTick;

public abstract String flavorName();

public List<String> serialiseHeader(List<PlaybackMetadata> metadataList) {
List<String> out = new ArrayList<>();
out.add(serialiseFlavorName());
serialiseFlavorName(out);
// out.add(serializeExtensionNames());
out.addAll(serialiseMetadata(metadataList));
serialiseMetadata(out, metadataList);
return out;
}

protected String serialiseFlavorName() {
return "Flavor:" + flavorName();
protected void serialiseFlavorName(List<String> out) {
out.add("# Flavor:" + flavorName());
}

public List<String> serialiseMetadata(List<PlaybackMetadata> metadataList) {
return null;
protected void serialiseMetadata(List<String> out, List<PlaybackMetadata> metadataList) {
for (PlaybackMetadata metadata : metadataList) {
out.add("### " + metadata.getExtensionName());
for (String value : metadata.toStringList()) {
out.add("# " + value);
}
}
}

public BigArrayList<String> serialise(BigArrayList<TickInputContainer> inputs) {
Expand All @@ -44,43 +49,57 @@ public BigArrayList<String> serialise(BigArrayList<TickInputContainer> inputs) {
for (int i = 0; i < inputs.size(); i++) {
currentTick = i;
TickInputContainer container = inputs.get(i);
addAll(out, serialiseContainer(container));
serialiseContainer(out, container);
}
return out;
}

protected List<String> serialiseContainer(TickInputContainer container) {
protected void serialiseContainer(BigArrayList<String> out, TickInputContainer container) {
List<String> serialisedKeyboard = serialiseKeyboard(container.getKeyboard());
List<String> serialisedMouse = serialiseMouse(container.getMouse());
List<String> serialisedCameraAngle = serialiseCameraAngle(container.getCameraAngle());

return mergeInputs(serialisedKeyboard, serialisedMouse, serialisedCameraAngle);
mergeInputs(out, serialisedKeyboard, serialisedMouse, serialisedCameraAngle);
}

protected abstract List<String> serialiseKeyboard(VirtualKeyboard keyboard);

protected abstract List<String> serialiseMouse(VirtualMouse mouse);
protected List<String> serialiseKeyboard(VirtualKeyboard keyboard) {
List<String> out = new ArrayList<>();
for (VirtualKeyboard subtick : keyboard.getAll()) {
out.add(subtick.toString());
}
return out;
}

protected abstract List<String> serialiseCameraAngle(VirtualCameraAngle cameraAngle);
protected List<String> serialiseMouse(VirtualMouse mouse) {
List<String> out = new ArrayList<>();
for (VirtualMouse subtick : mouse.getAll()) {
out.add(subtick.toString());
}
return out;
}

protected List<String> mergeInputs(List<String> serialisedKeyboard, List<String> serialisedMouse, List<String> serialisedCameraAngle) {
protected List<String> serialiseCameraAngle(VirtualCameraAngle cameraAngle) {
List<String> out = new ArrayList<>();

for (VirtualCameraAngle subtick : cameraAngle.getAll()) {
out.add(subtick.toString());
}
return out;
}

protected void mergeInputs(BigArrayList<String> out, List<String> serialisedKeyboard, List<String> serialisedMouse, List<String> serialisedCameraAngle) {
Queue<String> keyboardQueue = new LinkedBlockingQueue<>(serialisedKeyboard);
Queue<String> mouseQueue = new LinkedBlockingQueue<>(serialisedMouse);
Queue<String> cameraAngleQueue = new LinkedBlockingQueue<>(serialisedCameraAngle);
while(!keyboardQueue.isEmpty() && !mouseQueue.isEmpty() && !cameraAngleQueue.isEmpty()) {

while (!keyboardQueue.isEmpty() || !mouseQueue.isEmpty() || !cameraAngleQueue.isEmpty()) {
String kb = getOrEmpty(keyboardQueue.poll());
String ms = getOrEmpty(mouseQueue.poll());
String ca = getOrEmpty(cameraAngleQueue.poll());

out.add(kb + ms + ca);
}

return out;
}

protected String getOrEmpty(String string) {
return string == null ? "" : string;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,17 @@ public String flavorName() {

@Override
protected List<String> serialiseKeyboard(VirtualKeyboard keyboard) {
// TODO Auto-generated method stub
return null;
}

@Override
protected List<String> serialiseMouse(VirtualMouse mouse) {
// TODO Auto-generated method stub
return null;

}

@Override
protected List<String> serialiseCameraAngle(VirtualCameraAngle cameraAngle) {
// TODO Auto-generated method stub
return null;
}

}

0 comments on commit fdf1953

Please sign in to comment.