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,24 +9,13 @@ 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 {
public static void main(String[] args) private static void serialize(String in, String out)
{ {
if (args.length < 3)
{
System.out.println("MineFork GUI Baker");
System.out.println("Usage: p/u [input xml] [output nbt]");
return;
}
if (args[0].equals("p"))
{
String in = args[1];
String out = args[2];
try { try {
Screen scr = XMLReader.readFile(in); Screen scr = XMLReader.readFile(in);
if (scr == null) if (scr == null)
@ -33,27 +23,57 @@ public class GuiBaker {
NBTCompound comp = NBTWriter.write(scr); NBTCompound comp = NBTWriter.write(scr);
try (FileOutputStream s = new FileOutputStream(out)) try (FileOutputStream s = new FileOutputStream(out)) {
{ try (DataOutputStream o = new DataOutputStream(s)) {
try (DataOutputStream o = new DataOutputStream(s))
{
comp.write(o); comp.write(o);
} }
} }
System.out.println("NBT successfully written!"); System.out.printf("NBT %s successfully written!%n", FilenameUtils.getName(out));
} catch (ParserConfigurationException | IOException | SAXException e) { } catch (ParserConfigurationException | IOException | SAXException e) {
throw new RuntimeException(e); throw new RuntimeException(e);
} }
}
} private static void deserialize(String in, String out)
else if (args[0].equals("u"))
{ {
System.out.println("Conversion from NBT to XML is not supported yet!"); throw new RuntimeException("Deserialization is not supported!");
} }
else
public static void main(String[] args)
{ {
System.out.println("Unknown arguments"); if (args.length < 3)
{
System.out.println("MineFork GUI Baker");
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;
}
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++)
{
String cur = args[i];
String fn = FilenameUtils.getBaseName(cur);
serialize(cur, dir + fn + ".nbt");
}
}
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");
} }
} }
} }