Skip to content

Commit aa05473

Browse files
committed
[PlaybackSerialiser] More documentation
- Added more iterable BigArrayLists
1 parent eec2fd2 commit aa05473

File tree

3 files changed

+244
-22
lines changed

3 files changed

+244
-22
lines changed

src/main/java/com/minecrafttas/tasmod/playback/filecommands/PlaybackFileCommand.java

Lines changed: 152 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,37 @@
1111
import com.minecrafttas.mctcommon.file.AbstractDataFile;
1212
import com.minecrafttas.mctcommon.registry.Registerable;
1313
import com.minecrafttas.tasmod.TASmodClient;
14+
import com.minecrafttas.tasmod.commands.CommandFileCommand;
15+
import com.minecrafttas.tasmod.playback.PlaybackControllerClient;
1416
import com.minecrafttas.tasmod.playback.PlaybackControllerClient.InputContainer;
17+
import com.minecrafttas.tasmod.playback.tasfile.PlaybackSerialiser;
1518
import com.minecrafttas.tasmod.playback.tasfile.flavor.SerialiserFlavorBase;
1619

1720
public class PlaybackFileCommand {
1821

22+
/**
23+
* The name of the fileCommand
24+
*/
1925
private String name;
2026

27+
/**
28+
* The arguments of the fileCommand
29+
*/
2130
private String[] args;
2231

32+
/**
33+
* Creates a new FileCommand with no arguments
34+
* @param name The {@link #name}
35+
*/
2336
public PlaybackFileCommand(String name) {
2437
this(name, (String[]) null);
2538
}
2639

40+
/**
41+
* Creates a new FileCommand
42+
* @param name The {@link #name}
43+
* @param args The {@link #args}r
44+
*/
2745
public PlaybackFileCommand(String name, String... args) {
2846
if (args == null) {
2947
args = new String[] {};
@@ -32,10 +50,16 @@ public PlaybackFileCommand(String name, String... args) {
3250
this.args = args;
3351
}
3452

53+
/**
54+
* @return {@link #name}
55+
*/
3556
public String getName() {
3657
return name;
3758
}
3859

60+
/**
61+
* @return {@link #args}
62+
*/
3963
public String[] getArgs() {
4064
return args;
4165
}
@@ -94,6 +118,12 @@ public PlaybackFileCommandExtension(String tempFolderName) {
94118
this(TASmodClient.tasfiledirectory.resolve("temp").resolve(tempFolderName));
95119
}
96120

121+
/**
122+
* <p>Creates a FileCommandExtension and creates a temp folder with<br>
123+
* at the specified path for the {@link BigArrayList} files
124+
*
125+
* @param tempFolderName The name of the temp folder
126+
*/
97127
public PlaybackFileCommandExtension(Path tempDirectory) {
98128
if (tempDirectory == null) {
99129
tempDir = null;
@@ -112,16 +142,56 @@ public PlaybackFileCommandExtension(Path tempDirectory) {
112142
endlineFileCommandStorage = new BigArrayList<>(tempDir.toString());
113143
}
114144

145+
/**
146+
* Whether this extension is enabled.<br>
147+
* Can be changed e.g. via the {@link CommandFileCommand} command
148+
*/
115149
protected boolean enabled = false;
116150

151+
/**
152+
* <p>The names of all file commands that should be handled by this extension
153+
* <p>Imagine having the following file commands in the playback file:
154+
* <pre>
155+
* // $desyncMonitor(13, 0, 1, 1, 1, 1); $hud(true);
156+
* </pre>
157+
* And you want to support the hud file command with an extension,<br>
158+
* then you must return a string array with the name:
159+
* <pre>
160+
* public String[] getFileCommandNames() {
161+
* return new String[]{"hud"};
162+
* }
163+
* </pre>
164+
* Now, methods like {@link #onDeserialiseEndlineComment(long, InputContainer, SortedFileCommandContainer) onDeserialiseEndlineComment}
165+
* will only have a {@link SortedFileCommandContainer}<br>
166+
* with the "hud" {@link FileCommandsInTickList} as a parameter. "desyncMonitor" will be ignored.
167+
* <p>
168+
* To also include "desyncMonitor" in the {@link SortedFileCommandContainer}, simply add that name to the array:
169+
* <pre>
170+
* public String[] getFileCommandNames() {
171+
* return new String[]{"hud", "desyncMonitor"};
172+
* }
173+
* </pre>
174+
*
175+
* @return A string array of file command names
176+
*/
117177
public abstract String[] getFileCommandNames();
118178

179+
/**
180+
* Fired when using the {@link CommandFileCommand} and setting this extension to enabled
181+
*/
119182
public void onEnable() {
120183
};
121184

185+
/**
186+
* Fired when using the {@link CommandFileCommand} and setting this extension to disabled
187+
*/
122188
public void onDisable() {
123189
};
124190

191+
/**
192+
* Fired when {@link PlaybackControllerClient#clear()} is called<br>
193+
* Make sure to call <code>super.onClear()</code> as it clears the {@link BigArrayLists} in this extension!
194+
*/
125195
public void onClear() {
126196
try {
127197
inlineFileCommandStorage.clearMemory();
@@ -133,12 +203,32 @@ public void onClear() {
133203
endlineFileCommandStorage = new BigArrayList<>();
134204
};
135205

206+
/**
207+
* Fired when the {@link PlaybackControllerClient} is recording inputs<br>
208+
* Usually used to generate new FileCommands during recording.
209+
* @param tick The current tick in the recording
210+
* @param inputContainer The current inputs in the recording
211+
*/
136212
public void onRecord(long tick, InputContainer inputContainer) {
137213
};
138214

215+
/**
216+
* Fired when the {@link PlaybackControllerClient} is playing back inputs.<br>
217+
* Usually used to generate new FileCommands during playback.
218+
* @param tick The current tick in the playback
219+
* @param inputContainer The current inputs in the playback
220+
*/
139221
public void onPlayback(long tick, InputContainer inputContainer) {
140222
};
141223

224+
/**
225+
* Fired when the {@link PlaybackSerialiser} writes the inputs to a file.<br>
226+
* This is used to store your inlineFileCommands into an comment.
227+
*
228+
* @param tick The current tick that is serialised
229+
* @param inputContainer The current inputs that are being serialised
230+
* @return A {@link SortedFileCommandContainer} with your filecommands for one container that you want serialised
231+
*/
142232
public SortedFileCommandContainer onSerialiseInlineComment(long tick, InputContainer inputContainer) {
143233
SortedFileCommandContainer out = new SortedFileCommandContainer();
144234
if (tick >= inlineFileCommandStorage.size())
@@ -155,6 +245,14 @@ public SortedFileCommandContainer onSerialiseInlineComment(long tick, InputConta
155245
return out;
156246
}
157247

248+
/**
249+
* Fired when the {@link PlaybackSerialiser} writes the inputs to a file.<br>
250+
* This is used to store your endlineFileCommands into an comment.
251+
*
252+
* @param tick The current tick that is serialised
253+
* @param inputContainer The current inputs that are being serialised
254+
* @return A {@link SortedFileCommandContainer} with your filecommands for one container that you want serialised
255+
*/
158256
public SortedFileCommandContainer onSerialiseEndlineComment(long tick, InputContainer inputContainer) {
159257
SortedFileCommandContainer out = new SortedFileCommandContainer();
160258
if (tick >= endlineFileCommandStorage.size())
@@ -171,24 +269,47 @@ public SortedFileCommandContainer onSerialiseEndlineComment(long tick, InputCont
171269
return out;
172270
}
173271

174-
public void onDeserialiseInlineComment(long tick, InputContainer container, SortedFileCommandContainer fileCommandContainer) {
272+
/**
273+
* Fired when the {@link PlaybackSerialiser} reads the inputs from a file.<br>
274+
* This is used to load your inlineFileCommands from a comment into {@link #inlineFileCommandStorage} to be used in {@link #onPlayback(long, InputContainer)}.
275+
*
276+
* @param tick The current tick that is deserialised
277+
* @param inputContainer The current inputs that are being deserialised
278+
* @param fileCommandContainer The {@link SortedFileCommandContainer} that was deserialised
279+
*/
280+
public void onDeserialiseInlineComment(long tick, InputContainer inputContainer, SortedFileCommandContainer fileCommandContainer) {
175281
if (fileCommandContainer == null)
176282
return;
177283

178284
inlineFileCommandStorage.add(fileCommandContainer);
179285
}
180286

181-
public void onDeserialiseEndlineComment(long tick, InputContainer container, SortedFileCommandContainer fileCommandContainer) {
287+
/**
288+
* Fired when the {@link PlaybackSerialiser} reads the inputs from a file.<br>
289+
* This is used to load your endlineFileCommands from a comment into {@link #endlineFileCommandStorage} to be used in {@link #onPlayback(long, InputContainer)}.
290+
*
291+
* @param tick The current tick that is deserialised
292+
* @param inputContainer The current inputs that are being deserialised
293+
* @param fileCommandContainer The {@link SortedFileCommandContainer} that was deserialised
294+
*/
295+
public void onDeserialiseEndlineComment(long tick, InputContainer inputContainer, SortedFileCommandContainer fileCommandContainer) {
182296
if (fileCommandContainer == null)
183297
return;
184298

185299
endlineFileCommandStorage.add(fileCommandContainer);
186300
}
187301

302+
/**
303+
* @return {@link #enabled}
304+
*/
188305
public boolean isEnabled() {
189306
return enabled;
190307
}
191308

309+
/**
310+
* Set {@link #enabled} and run {@link #onEnable()} and {@link #onDisable()}
311+
* @param enabled Sets {@link #enabled}
312+
*/
192313
public void setEnabled(boolean enabled) {
193314
if (enabled)
194315
onEnable();
@@ -239,8 +360,8 @@ public static class FileCommandsInCommentList extends ArrayList<PlaybackFileComm
239360
* <p>This would translate into an ArrayList like
240361
* <pre>
241362
* [
242-
* [$desyncMonitor(13, 0, 1, 1, 1, 1);, $hud(true);] &lt;- One {@link FileCommandsInCommentList}
243-
* [$desyncMonitor(16, 3, 1, 1, 1, 1);, $hud(false);]
363+
* [$desyncMonitor(13, 0, 1, 1, 1, 1);, $hud(true);], &lt;- One {@link FileCommandsInCommentList}
364+
* [$desyncMonitor(16, 3, 1, 1, 1, 1);, $hud(false);],
244365
* [$label(Test);, $hud(false);]
245366
* ]
246367
* </pre>
@@ -259,6 +380,28 @@ public SortedFileCommandContainer sort() {
259380
/*
260381
* Fill the HashMap in SortedFileCommandContainer with empty FileCommandsInCommentList
261382
* for each different FileCommand name found in this UnsortedFileCommandContainer.
383+
*
384+
* We have to do this, since absent FileCommands are set to null.
385+
*
386+
* Example:
387+
* // $desyncMonitor(13, 0, 1, 1, 1, 1); $hud(true);
388+
* // $desyncMonitor(16, 3, 1, 1, 1, 1); $hud(false);
389+
* // $label(Test); $hud(false);
390+
*
391+
* In line 1, the "label" FC is missing
392+
* In line 2, once again, "label is missing
393+
* In line 3, desyncMonitor is missing.
394+
*
395+
* If it's missing, we need to set that spot to null.
396+
*
397+
* So first we create empty Hashmaps for each:
398+
* {
399+
* "desyncMonitor": [],
400+
* "hud": [],
401+
* "label": []
402+
* }
403+
*
404+
* Then iterate through all filecommands and set null where a FileCommand is absent
262405
*/
263406
for (FileCommandsInCommentList unsortedFileCommandsList : this) {
264407
if (unsortedFileCommandsList != null) {
@@ -268,7 +411,7 @@ public SortedFileCommandContainer sort() {
268411
}
269412
}
270413

271-
/**
414+
/*
272415
* Add the FileCommands to the previously created FileCommandsInCommentLists
273416
*/
274417
for (FileCommandsInCommentList unsortedFileCommandsList : this) {
@@ -285,7 +428,7 @@ public SortedFileCommandContainer sort() {
285428

286429
boolean valuePresent = false;
287430
if (unsortedFileCommandsList != null) {
288-
/**
431+
/*
289432
* Iterates through all filecommands in a comment
290433
* and adds it to the sorted list if found
291434
*/
@@ -296,7 +439,7 @@ public SortedFileCommandContainer sort() {
296439
}
297440
}
298441
}
299-
/**
442+
/*
300443
* If the value is not found,
301444
* add null to indicate that the
302445
* file command is missing from this comment
@@ -336,9 +479,10 @@ public static class FileCommandsInTickList extends ArrayList<PlaybackFileCommand
336479
}
337480

338481
/**
339-
* <p>A LinkedHashMap for storing {@link FileCommandsInCommentList} sorted by the name of the FileCommand name.
482+
* <p>A LinkedHashMap for storing {@link FileCommandsInTickList} sorted by the name of the FileCommand name.
340483
* <p>The key represents the FileCommand name, while the elements are the {@link FileCommandsInTickList}
341484
* <p>This stands in contrast to the {@link UnsortedFileCommandContainer}, which can be obtained by calling {@link SortedFileCommandContainer#unsort() unsort()}
485+
* <p>Used in {@link PlaybackFileCommandExtension PlaybackFileCommandExtensions} as this format makes it easier to distribute the file commands to their respective class extensions
342486
* <h5>Example</h5>
343487
* <pre>
344488
* // $desyncMonitor(13, 0, 1, 1, 1, 1); $hud(true);

src/main/java/com/minecrafttas/tasmod/playback/tasfile/PlaybackSerialiser.java

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import com.minecrafttas.tasmod.playback.tasfile.exception.PlaybackSaveException;
1717
import com.minecrafttas.tasmod.playback.tasfile.flavor.SerialiserFlavorBase;
1818
import com.minecrafttas.tasmod.registries.TASmodAPIRegistry;
19+
import com.minecrafttas.tasmod.savestates.SavestateHandlerClient;
1920
import com.minecrafttas.tasmod.util.FileThread;
2021

2122
/**
@@ -25,6 +26,10 @@
2526
*/
2627
public class PlaybackSerialiser {
2728

29+
/**
30+
* The default flavor name used for saving TASfiles when no flavor is specified,<br>
31+
* used in {@link #saveToFile(Path, BigArrayList, String, long)}
32+
*/
2833
private static String defaultFlavor = "beta1";
2934

3035
/**
@@ -112,8 +117,8 @@ public static void saveToFile(Path path, BigArrayList<InputContainer> container,
112117
}
113118

114119
BigArrayList<String> tickLines = flavor.serialise(container, stopIndex);
115-
for (long i = 0; i < tickLines.size(); i++) {
116-
writerThread.addLine(tickLines.get(i));
120+
for (String tickLine : tickLines) {
121+
writerThread.addLine(tickLine);
117122
}
118123

119124
writerThread.close();
@@ -132,6 +137,19 @@ public static BigArrayList<InputContainer> loadFromFile(Path file) throws Playba
132137
return loadFromFile(file, true);
133138
}
134139

140+
/**
141+
* Loads a BigArrayList of {@link InputContainer InputContainers} from a file.<br>
142+
* Tries to determine the {@link SerialiserFlavorBase flavor} by reading the header of the TASfile
143+
*
144+
* <p>Has a parameter to stop processing extension data, usually used when
145+
* {@link SavestateHandlerClient#loadstate(String) loading savestates} to prevent loadstates from overwriting data
146+
*
147+
* @param file The file to load from
148+
* @param processExtensions Sets {@link SerialiserFlavorBase#processExtensions}
149+
* @return The loaded BigArrayList of {@link InputContainer InputContainers}
150+
* @throws PlaybackLoadException If the file contains errors
151+
* @throws IOException If the file could not be read
152+
*/
135153
public static BigArrayList<InputContainer> loadFromFile(Path file, boolean processExtensions) throws PlaybackLoadException, IOException {
136154
if (file == null) {
137155
throw new PlaybackLoadException("Load from file failed. No file specified");
@@ -160,6 +178,19 @@ public static BigArrayList<InputContainer> loadFromFile(Path file, String flavor
160178
return loadFromFile(file, flavorName, true);
161179
}
162180

181+
/**
182+
* Loads a BigArrayList of {@link InputContainer InputContainers} from a file, with a specific flavor
183+
*
184+
* <p>Has a parameter to stop processing extension data, usually used when
185+
* {@link SavestateHandlerClient#loadstate(String) loading savestates} to prevent loadstates from overwriting data
186+
*
187+
* @param file The file to load from
188+
* @param flavorName The name of the {@link SerialiserFlavorBase flavor} to use. If the detected flavor in the TASfile mismatches, a {@link PlaybackLoadException} is thrown
189+
* @param processExtensions Sets {@link SerialiserFlavorBase#processExtensions}
190+
* @return The loaded BigArrayList of {@link InputContainer InputContainers}
191+
* @throws PlaybackLoadException If the file contains errors
192+
* @throws IOException If the file could not be read
193+
*/
163194
public static BigArrayList<InputContainer> loadFromFile(Path file, String flavorName, boolean processExtensions) throws PlaybackLoadException, IOException {
164195

165196
// If the flavor is null or empty, try to determine the flavor by reading the header

0 commit comments

Comments
 (0)