From f6d26d7787ed68b8031e53d015f0e03290ce1246 Mon Sep 17 00:00:00 2001 From: Arves100 Date: Wed, 18 Dec 2024 18:59:25 +0100 Subject: [PATCH] Implement batch serialization/deserialization --- guibaker/pom.xml | 5 ++ .../minefork/guitools/baker/GuiBaker.java | 82 ++++++++++++------- 2 files changed, 56 insertions(+), 31 deletions(-) diff --git a/guibaker/pom.xml b/guibaker/pom.xml index 3897e49..f316ed8 100644 --- a/guibaker/pom.xml +++ b/guibaker/pom.xml @@ -25,6 +25,11 @@ ${project.parent.version} import + + commons-io + commons-io + 2.18.0 + diff --git a/guibaker/src/main/java/org/scaminc/minefork/guitools/baker/GuiBaker.java b/guibaker/src/main/java/org/scaminc/minefork/guitools/baker/GuiBaker.java index 726e1bc..4bbb5bd 100644 --- a/guibaker/src/main/java/org/scaminc/minefork/guitools/baker/GuiBaker.java +++ b/guibaker/src/main/java/org/scaminc/minefork/guitools/baker/GuiBaker.java @@ -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"); } } }