Skip to content

Commit

Permalink
[PlaybackMetadata] Adding handleOnLoad and handleOnSave
Browse files Browse the repository at this point in the history
Changed constructor of PlaybackMetadata to accept PlaybackMetadataExtension.
This is to encourage users to write `new PlaybackMetadata(this)` to ensure,
that the metadata extension name is also in the PlaybackMetadata.

- Added PlaybackMetadata and PlaybackMetadataRegistry tests
  • Loading branch information
ScribbleTAS committed Apr 6, 2024
1 parent f8cf266 commit be74f5e
Show file tree
Hide file tree
Showing 6 changed files with 204 additions and 64 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public class PlaybackControllerClient implements ClientPacketHandler, EventVirtu
private TASstate state = TASstate.NONE;

/**
* The state of the controller when the state is paused
* The state of the controller when the {@link #state} is paused
*/
private TASstate tempPause = TASstate.NONE;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import com.minecrafttas.tasmod.util.FileThread;
import com.minecrafttas.tasmod.util.LoggerMarkers;
import com.minecrafttas.tasmod.virtual.VirtualCameraAngle;
import com.minecrafttas.tasmod.virtual.VirtualKey;
import com.minecrafttas.tasmod.virtual.VirtualKeyboard;
import com.minecrafttas.tasmod.virtual.VirtualMouse;
import com.mojang.realmsclient.util.Pair;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,29 +7,30 @@
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.minecrafttas.tasmod.playback.metadata.PlaybackMetadataRegistry.PlaybackMetadataExtension;

/**
* Stores a section of<br>
* <br>
*/
public class PlaybackMetadata {
/**
* Debug extension name
*/
private String extensionName;
private LinkedHashMap<String, String> metadata;

private static String SEPERATOR = ":";

public PlaybackMetadata() {
this.metadata = new LinkedHashMap<String, String>();
public PlaybackMetadata(PlaybackMetadataExtension extension) {
this(extension.getExtensionName());
}

public PlaybackMetadata(String extensionName) {
this();
private PlaybackMetadata(String extensionName) {
this.extensionName = extensionName;
this.metadata = new LinkedHashMap<String, String>();
}

public void setValue(String key, String value) {
if (key.contains("=")) {
throw new IllegalArgumentException(String.format("%sKeyname %s can't contain =", extensionName != null ? extensionName + ": " : "", key));
if (key.contains(SEPERATOR)) {
throw new IllegalArgumentException(String.format("%sKeyname %s can't contain %s", extensionName != null ? extensionName + ": " : "", key, SEPERATOR));
}
metadata.put(key, value);
}
Expand All @@ -43,7 +44,7 @@ public String toString() {
String out = "";
for (String key : metadata.keySet()) {
String value = getValue(key);
out += (String.format("%s=%s\n", key, value));
out += (String.format("%s%s%s\n", key, SEPERATOR, value));
}
return out;
}
Expand All @@ -53,7 +54,7 @@ public List<String> toStringList() {
for (Object keyObj : metadata.keySet()) {
String key = (String) keyObj;
String value = getValue(key);
out.add(String.format("%s=%s\n", key, value));
out.add(String.format("%s%s%s\n", key, SEPERATOR, value));
}
return out;
}
Expand All @@ -76,13 +77,9 @@ public boolean equals(Object obj) {
}

public static PlaybackMetadata fromStringList(String extensionName, List<String> list) {
return fromStringList(list);
}

public static PlaybackMetadata fromStringList(List<String> list) {
PlaybackMetadata out = new PlaybackMetadata();
PlaybackMetadata out = new PlaybackMetadata(extensionName);

final Pattern pattern = Pattern.compile("(\\w+)=(.+)");
final Pattern pattern = Pattern.compile("(\\w+)\\"+SEPERATOR+"(.+)");

for (String data : list) {
Matcher matcher = pattern.matcher(data);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import com.minecrafttas.mctcommon.MCTCommon;
import com.minecrafttas.tasmod.TASmod;

/**
* Registry for registering custom metadata that is stored in the TASFile.<br>
Expand All @@ -16,7 +18,7 @@
*/
public class PlaybackMetadataRegistry {

private static final ArrayList<PlaybackMetadataExtension> METADATA_EXTENSION = new ArrayList<>();
private static final Map<String, PlaybackMetadataExtension> METADATA_EXTENSION = new LinkedHashMap<>();

/**
* Registers a new class as a metadata extension
Expand All @@ -29,41 +31,55 @@ public static void register(PlaybackMetadataExtension extension) {
}

if (containsClass(extension)) {
MCTCommon.LOGGER.warn("Trying to register the playback extension {}, but another instance of this class is already registered!", extension.getClass().getName());
TASmod.LOGGER.warn("Trying to register the playback extension {}, but another instance of this class is already registered!", extension.getClass().getName());
return;
}

if (!METADATA_EXTENSION.contains(extension)) {
METADATA_EXTENSION.add(extension);
} else {
MCTCommon.LOGGER.warn("Trying to register the playback extension {}, but it is already registered!", extension.getClass().getName());
if(METADATA_EXTENSION.containsKey(extension.getExtensionName())) {
TASmod.LOGGER.warn("Trying to register the playback extension {}, but an extension with the same name is already registered!", extension.getExtensionName());
return;
}

METADATA_EXTENSION.put(extension.getExtensionName(), extension);
}

public static void unregister(PlaybackMetadataExtension extension) {
if (extension == null) {
throw new NullPointerException("Tried to unregister an extension with value null");
}
if (METADATA_EXTENSION.contains(extension)) {
METADATA_EXTENSION.remove(extension);
if (METADATA_EXTENSION.containsKey(extension.getExtensionName())) {
METADATA_EXTENSION.remove(extension.getExtensionName());
} else {
MCTCommon.LOGGER.warn("Trying to unregister the playback extension {}, but it was not registered!", extension.getClass().getName());
TASmod.LOGGER.warn("Trying to unregister the playback extension {}, but it was not registered!", extension.getClass().getName());
}
}

public static void handleOnCreate() {

}

public static LinkedHashMap<String, PlaybackMetadata> handleOnLoad() {
return null;
public static List<PlaybackMetadata> handleOnStore() {
List<PlaybackMetadata> metadataList = new ArrayList<>();
for(PlaybackMetadataExtension extension : METADATA_EXTENSION.values()) {
metadataList.add(extension.onStore());
}
return metadataList;
}

public static void handleOnStore(LinkedHashMap<String, PlaybackMetadata> metadata) {
public static void handleOnLoad(List<PlaybackMetadata> meta) {
for(PlaybackMetadata metadata : meta) {
if(METADATA_EXTENSION.containsKey(metadata.getExtensionName())) {
PlaybackMetadataExtension extension = METADATA_EXTENSION.get(metadata.getExtensionName());

extension.onLoad(metadata);
} else {
TASmod.LOGGER.warn("The metadata extension {} was not found while loading the TASFile. Things might not be correctly loaded!", metadata.getExtensionName());
}
}
}

private static boolean containsClass(PlaybackMetadataExtension newExtension) {
for (PlaybackMetadataExtension extension : METADATA_EXTENSION) {
for (PlaybackMetadataExtension extension : METADATA_EXTENSION.values()) {
if (extension.getClass().equals(newExtension.getClass())) {
return true;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
package tasmod.playback.metadata;

import org.apache.commons.io.FileUtils;
import org.junit.jupiter.api.Test;

import com.minecrafttas.tasmod.playback.metadata.PlaybackMetadata;
import com.minecrafttas.tasmod.playback.metadata.PlaybackMetadataRegistry;
import com.minecrafttas.tasmod.playback.metadata.PlaybackMetadataRegistry.PlaybackMetadataExtension;

import static org.junit.jupiter.api.Assertions.*;

import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;


public class PlaybackMetadataRegistryTest {

class Test1 implements PlaybackMetadataExtension{

private String actual;

public String getActual() {
return actual;
}

@Override
public String getExtensionName() {
return "Test1";
}

@Override
public void onCreate() {
}

@Override
public PlaybackMetadata onStore() {
PlaybackMetadata data = new PlaybackMetadata(this);
data.setValue("Test", "Testing 1");
return data;
}

@Override
public void onLoad(PlaybackMetadata metadata) {
actual = metadata.getValue("Test");
}

}

File file = new File("src/test/resources/run/MetadataRegistry.txt");

void store() {
List<PlaybackMetadata> list = PlaybackMetadataRegistry.handleOnStore();
List<String> out = new ArrayList<>();

list.forEach(data -> {
out.addAll(data.toStringList());
});

try {
FileUtils.writeLines(file, out);
} catch (IOException e) {
e.printStackTrace();
}
}

void load() {
List<String> loaded = null;
try {
loaded = FileUtils.readLines(file, StandardCharsets.UTF_8);
} catch (IOException e) {
e.printStackTrace();
}


List<PlaybackMetadata> meta = new ArrayList<>();

meta.add(PlaybackMetadata.fromStringList("Test1", loaded));

PlaybackMetadataRegistry.handleOnLoad(meta);
}

/**
* Register, store and read metadata
*/
@Test
void testRegistry() {
Test1 actual = new Test1();
PlaybackMetadataRegistry.register(actual);

store();
load();

assertEquals("Testing 1", actual.getActual());
if(file.exists()) {
file.delete();
}
}
}
Loading

0 comments on commit be74f5e

Please sign in to comment.