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> <version>${project.parent.version}</version>
<scope>import</scope> <scope>import</scope>
</dependency> </dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.18.0</version>
</dependency>
</dependencies> </dependencies>
<build> <build>

View File

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