Implement batch serialization/deserialization

This commit is contained in:
Christian Rendina 2024-12-18 18:59:25 +01:00
parent 329bec23ed
commit f6d26d7787
2 changed files with 56 additions and 31 deletions

View File

@ -25,6 +25,11 @@
<version>${project.parent.version}</version>
<scope>import</scope>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.18.0</version>
</dependency>
</dependencies>
<build>

View File

@ -1,5 +1,6 @@
package org.scaminc.minefork.guitools.baker;
import org.apache.commons.io.FilenameUtils;
import org.scaminc.minefork.guicore.NBTWriter;
import org.scaminc.minefork.guicore.XMLReader;
import org.scaminc.minefork.guicore.entity.Screen;
@ -8,52 +9,71 @@ import org.xml.sax.SAXException;
import javax.xml.parsers.ParserConfigurationException;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
public class GuiBaker {
private static void serialize(String in, String out)
{
try {
Screen scr = XMLReader.readFile(in);
if (scr == null)
throw new NullPointerException();
NBTCompound comp = NBTWriter.write(scr);
try (FileOutputStream s = new FileOutputStream(out)) {
try (DataOutputStream o = new DataOutputStream(s)) {
comp.write(o);
}
}
System.out.printf("NBT %s successfully written!%n", FilenameUtils.getName(out));
} catch (ParserConfigurationException | IOException | SAXException e) {
throw new RuntimeException(e);
}
}
private static void deserialize(String in, String out)
{
throw new RuntimeException("Deserialization is not supported!");
}
public static void main(String[] args)
{
if (args.length < 3)
{
System.out.println("MineFork GUI Baker");
System.out.println("Usage: p/u [input xml] [output nbt]");
System.out.println("Serialize to NBT: p [input xml] [output nbt]");
System.out.println("Deserialize from NBT: u [input nbt] [output xml]");
System.out.println("Batch serialization: bp [output directory] [input XML 1] [input NBT 2] [...]");
System.out.println("Batch deserialization: bp [output directory] [input NBT 1] [input NBT 2] [...]");
return;
}
if (args[0].equals("p"))
{
String in = args[1];
String out = args[2];
try {
Screen scr = XMLReader.readFile(in);
if (scr == null)
throw new NullPointerException();
NBTCompound comp = NBTWriter.write(scr);
try (FileOutputStream s = new FileOutputStream(out))
switch (args[0]) {
case "p" -> serialize(args[1], args[2]);
case "bp" -> {
String dir = FilenameUtils.getFullPathNoEndSeparator(args[1]) + File.separator;
for (int i = 2; i < args.length; i++)
{
try (DataOutputStream o = new DataOutputStream(s))
{
comp.write(o);
}
String cur = args[i];
String fn = FilenameUtils.getBaseName(cur);
serialize(cur, dir + fn + ".nbt");
}
System.out.println("NBT successfully written!");
} catch (ParserConfigurationException | IOException | SAXException e) {
throw new RuntimeException(e);
}
}
else if (args[0].equals("u"))
{
System.out.println("Conversion from NBT to XML is not supported yet!");
}
else
{
System.out.println("Unknown arguments");
case "u" -> deserialize(args[1], args[2]);
case "bu" -> {
String dir = FilenameUtils.getFullPathNoEndSeparator(args[1]) + File.separator;
for (int i = 2; i < args.length; i++)
{
String cur = args[i];
String fn = FilenameUtils.getBaseName(cur);
deserialize(cur, dir + fn + ".xml");
}
}
default -> System.out.println("Unknown arguments");
}
}
}