Skip to content

Commit

Permalink
[Flavor] Add header deserialisation to alpha flavor
Browse files Browse the repository at this point in the history
- Add test for alpha flavor
- Made some metadata values protected
  • Loading branch information
ScribbleTAS committed Dec 18, 2024
1 parent b1d6def commit fac5bda
Show file tree
Hide file tree
Showing 10 changed files with 390 additions and 56 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@
public class PlaybackFileCommandsRegistry extends AbstractRegistry<PlaybackFileCommandExtension> implements EventPlaybackClient.EventRecordTick, EventPlaybackClient.EventPlaybackTick, EventPlaybackClient.EventRecordClear {

private List<PlaybackFileCommandExtension> enabledExtensions = new ArrayList<>();

private Configuration config = null;

public PlaybackFileCommandsRegistry() {
super("FILECOMMAND_REGISTRY", new LinkedHashMap<>());
}
Expand All @@ -38,16 +38,16 @@ public void unregister(PlaybackFileCommandExtension extension) {
public boolean setEnabled(String extensionName, boolean enabled) {
return setEnabled(extensionName, enabled, true);
}

public boolean setEnabled(String extensionName, boolean enabled, boolean saveToConfig) {
PlaybackFileCommandExtension extension = REGISTRY.get(extensionName);
if(extension == null) {
if (extension == null) {
return false;
}
extension.setEnabled(enabled);
enabledExtensions = getEnabled();
if(saveToConfig) {

if (saveToConfig) {
saveConfig();
}
return true;
Expand All @@ -59,16 +59,20 @@ private void disableAll() {
});
}

public void setEnabled(String... extensionNames) {
setEnabled(Arrays.asList(extensionNames));
}

public void setEnabled(List<String> extensionNames) {
setEnabled(extensionNames, false);
}

public void setEnabled(List<String> extensionNames, boolean saveToConfig) {
disableAll();
for (String name : extensionNames) {
setEnabled(name, true, false);
}
if(saveToConfig)
if (saveToConfig)
saveConfig();
}

Expand All @@ -83,24 +87,24 @@ public List<PlaybackFileCommandExtension> getEnabled() {

return out;
}
public List<PlaybackFileCommandExtension> getAll(){

public List<PlaybackFileCommandExtension> getAll() {
return new ArrayList<>(REGISTRY.values());
}

@Override
public void onRecordTick(long index, TickContainer container) {
enabledExtensions.forEach(extension -> {
if(extension.isEnabled()) {
if (extension.isEnabled()) {
extension.onRecord(index, container);
}
});
}

@Override
public void onPlaybackTick(long index, TickContainer container) {
enabledExtensions.forEach(extension -> {
if(extension.isEnabled()) {
if (extension.isEnabled()) {
extension.onPlayback(index, container);
}
});
Expand All @@ -109,8 +113,8 @@ public void onPlaybackTick(long index, TickContainer container) {
public PlaybackFileCommandContainer handleOnSerialiseInline(long currentTick, TickContainer container) {
PlaybackFileCommandContainer out = new PlaybackFileCommandContainer();
for (PlaybackFileCommandExtension extension : enabledExtensions) {
PlaybackFileCommandContainer extensionContainer=extension.onSerialiseInlineComment(currentTick, container);
if(extensionContainer!=null) {
PlaybackFileCommandContainer extensionContainer = extension.onSerialiseInlineComment(currentTick, container);
if (extensionContainer != null) {
out.putAll(extensionContainer);
}
}
Expand All @@ -120,8 +124,8 @@ public PlaybackFileCommandContainer handleOnSerialiseInline(long currentTick, Ti
public PlaybackFileCommandContainer handleOnSerialiseEndline(long currentTick, TickContainer container) {
PlaybackFileCommandContainer out = new PlaybackFileCommandContainer();
for (PlaybackFileCommandExtension extension : enabledExtensions) {
PlaybackFileCommandContainer extensionContainer=extension.onSerialiseEndlineComment(currentTick, container);
if(extensionContainer!=null) {
PlaybackFileCommandContainer extensionContainer = extension.onSerialiseEndlineComment(currentTick, container);
if (extensionContainer != null) {
out.putAll(extensionContainer);
}
}
Expand Down Expand Up @@ -155,22 +159,22 @@ public void setConfig(Configuration config) {
this.config = config;
loadConfig();
}

private void loadConfig() {
if (config == null) {
return;
}
String enabled = config.get(TASmodConfig.EnabledFileCommands);
setEnabled(Arrays.asList(enabled.split(", ")));
}

private void saveConfig() {
if (config == null) {
return;
}
List<String> nameList = new ArrayList<>();
enabledExtensions.forEach(element ->{
List<String> nameList = new ArrayList<>();

enabledExtensions.forEach(element -> {
nameList.add(element.getExtensionName());
});
config.set(TASmodConfig.EnabledFileCommands, String.join(", ", nameList));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.minecrafttas.tasmod.playback.filecommands.builtin;

import java.io.File;
import java.io.IOException;
import java.io.Serializable;
import java.text.NumberFormat;
Expand Down Expand Up @@ -30,8 +29,6 @@
*/
public class DesyncMonitorFileCommandExtension extends PlaybackFileCommandExtension implements EventPlaybackClient.EventControllerStateChange {

private File tempDir = new File(Minecraft.getMinecraft().mcDataDir.getAbsolutePath() + File.separator + "saves" + File.separator + "tasfiles" + File.separator + "temp" + File.separator + "monitoring");

/**
* List containing {@link MonitorContainer MonitorContainers} in a TASfile
*/
Expand Down Expand Up @@ -352,7 +349,7 @@ public void onClear() {
} catch (IOException e) {
e.printStackTrace();
}
monitorContainer = new BigArrayList<MonitorContainer>(tempDir.toString());
monitorContainer = new BigArrayList<MonitorContainer>();
lastStatus = TextFormatting.GRAY + "Empty";
lastPos = "";
lastMotion = "";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,11 @@ public void onPlayback(long tick, TickContainer tickContainer) {
for (PlaybackFileCommand command : line) {
String[] args = command.getArgs();
if (args.length == 1) {
/*
* Ok this may seem dumb, but Boolean.parseBoolean returns false,
* even if something other then true or false was passed...
* If someone finds something less idiotic please tell me...
*/
switch (args[0]) {
case "true":
shouldRenderHud = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@ public PlaybackMetadata(PlaybackMetadataExtension extension) {
this(extension.getExtensionName());
}

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

private PlaybackMetadata(String extensionName, LinkedHashMap<String, String> data) {
public PlaybackMetadata(String extensionName, LinkedHashMap<String, String> data) {
this.extensionName = extensionName;
this.data = data;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,24 +25,24 @@ public class CreditsMetadataExtension extends PlaybackMetadataExtension implemen
/**
* The title/category of the TAS (e.g. KillSquid - Any% Glitched)
*/
private String title = "Insert TAS category here";
protected String title = "Insert TAS category here";
/**
* The author(s) of the TAS (e.g. Scribble, Pancake)
*/
private String authors = "Insert author here";
protected String authors = "Insert author here";
/**
* How long the TAS is going to take (e.g. 00:01.0 or 20ticks)
*/
private String playtime = "00:00.0";
protected String playtime = "00:00.0";
/**
* How often a savestate was loaded as a measurement of effort (e.g. 200)
*/
private int rerecords = 0;
protected int rerecords = 0;

/**
* If the credits where already printed in this instance
*/
private boolean creditsPrinted = false;
protected boolean creditsPrinted = false;

@Override
public String getExtensionName() {
Expand Down Expand Up @@ -84,17 +84,21 @@ public PlaybackMetadata onStore() {

@Override
public void onLoad(PlaybackMetadata metadata) {
title = metadata.getValue(CreditFields.Title);
authors = metadata.getValue(CreditFields.Author);
playtime = metadata.getValue(CreditFields.PlayTime);
title = getOrDefault(metadata.getValue(CreditFields.Title), title);
authors = getOrDefault(metadata.getValue(CreditFields.Author), authors);
playtime = getOrDefault(metadata.getValue(CreditFields.PlayTime), playtime);
try {
rerecords = Integer.parseInt(metadata.getValue(CreditFields.Rerecords));
rerecords = Integer.parseInt(getOrDefault(metadata.getValue(CreditFields.Rerecords), Integer.toString(rerecords)));
} catch (NumberFormatException e) {
rerecords = 0;
throw new PlaybackLoadException(e);
}
}

protected String getOrDefault(String value, String defaultVal) {
return value != null ? value : defaultVal;
}

@Override
public void onClear() {
title = "Insert TAS category here";
Expand All @@ -119,7 +123,7 @@ public void onPlaybackJoinedWorld(TASstate state) {
}
}

private void printMessage(String msg, TextFormatting format) {
protected void printMessage(String msg, TextFormatting format) {
String formatString = "";
if (format != null)
formatString = format.toString();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,15 @@ public class StartpositionMetadataExtension extends PlaybackMetadataExtension im
/**
* The startposition of the playback
*/
StartPosition startPosition = null;
protected StartPosition startPosition = null;

public static class StartPosition {

final double x;
final double y;
final double z;
final float pitch;
final float yaw;
public final double x;
public final double y;
public final double z;
public final float pitch;
public final float yaw;

public StartPosition(double x, double y, double z, float pitch, float yaw) {
this.x = x;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ public static BigArrayList<TickContainer> loadFromFile(Path file, SerialiserFlav
*/
public static SerialiserFlavorBase searchForFlavor(List<String> lines, List<SerialiserFlavorBase> flavorList) {
for (SerialiserFlavorBase flavor : flavorList) {
if (flavor.deserialiseFlavorName(lines)) {
if (flavor.checkFlavorName(lines)) {
return flavor.clone();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,13 @@ protected String joinNotEmpty(String delimiter, Iterable<String> args) {
return out;
}

/**
* Joins strings together but ignores empty strings
*
* @param delimiter The delimiter of the joined string
* @param args The strings to join
* @return Joined string
*/
protected String joinNotEmpty(String delimiter, String... args) {
return joinNotEmpty(delimiter, Arrays.asList(args));
}
Expand All @@ -377,7 +384,13 @@ protected String joinNotEmpty(String delimiter, String... args) {
*
*/

public boolean deserialiseFlavorName(List<String> headerLines) {
/**
* <p>Checks if the name of this flavor is present in the header of the TASfile.
* <p>Used to determine the flavor of the file if the flavor is not given
* @param headerLines The lines from the header to check
* @return True, if the flavor name is present in the header
*/
public boolean checkFlavorName(List<String> headerLines) {
for (String line : headerLines) {
Matcher matcher = extract("^Flavor: " + getExtensionName(), line);

Expand Down Expand Up @@ -425,7 +438,7 @@ protected void deserialiseFileCommandNames(List<String> headerLines) {
String extensionStrings = matcher.group(1);
String[] extensionNames = extensionStrings.split(", ?");

TASmodAPIRegistry.PLAYBACK_FILE_COMMAND.setEnabled(Arrays.asList(extensionNames));
TASmodAPIRegistry.PLAYBACK_FILE_COMMAND.setEnabled(extensionNames);
return;
}
}
Expand Down
Loading

0 comments on commit fac5bda

Please sign in to comment.