|
@@ -4,6 +4,7 @@ import com.fasterxml.jackson.databind.ObjectMapper;
|
|
import com.fasterxml.jackson.dataformat.xml.XmlFactory;
|
|
import com.fasterxml.jackson.dataformat.xml.XmlFactory;
|
|
import com.stanfy.gsonxml.GsonXml;
|
|
import com.stanfy.gsonxml.GsonXml;
|
|
import com.stanfy.gsonxml.GsonXmlBuilder;
|
|
import com.stanfy.gsonxml.GsonXmlBuilder;
|
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
import org.apache.commons.io.FileUtils;
|
|
import org.apache.commons.io.FileUtils;
|
|
import org.xml.sax.SAXException;
|
|
import org.xml.sax.SAXException;
|
|
import xyz.tbvns.XmlUtils;
|
|
import xyz.tbvns.XmlUtils;
|
|
@@ -14,67 +15,125 @@ import java.awt.*;
|
|
import java.awt.image.BufferedImage;
|
|
import java.awt.image.BufferedImage;
|
|
import java.io.File;
|
|
import java.io.File;
|
|
import java.io.IOException;
|
|
import java.io.IOException;
|
|
|
|
+import java.util.ArrayList;
|
|
|
|
+import java.util.List;
|
|
|
|
|
|
import static java.lang.Math.round;
|
|
import static java.lang.Math.round;
|
|
|
|
|
|
|
|
+@Slf4j
|
|
public class TextureExtractor {
|
|
public class TextureExtractor {
|
|
public static void extract(File plist, File textures, File output) throws IOException, ParserConfigurationException, SAXException {
|
|
public static void extract(File plist, File textures, File output) throws IOException, ParserConfigurationException, SAXException {
|
|
- String config = removeLines(FileUtils.readFileToString(plist), 2);
|
|
|
|
- config = XmlUtils.gamesheetToXML(config);
|
|
|
|
|
|
+ String config = XmlUtils.gamesheetToXML(removeLines(FileUtils.readFileToString(plist), 2));
|
|
|
|
+ System.out.println(config);
|
|
|
|
|
|
BufferedImage image = ImageIO.read(textures);
|
|
BufferedImage image = ImageIO.read(textures);
|
|
String[] readableConfig = config
|
|
String[] readableConfig = config
|
|
.replace(" ", "")
|
|
.replace(" ", "")
|
|
.split("\n");
|
|
.split("\n");
|
|
|
|
|
|
- if (true) {
|
|
|
|
- return;
|
|
|
|
|
|
+ List<TextureReference> textureReferences = new ArrayList<>();
|
|
|
|
+ TextureReference current = new TextureReference();
|
|
|
|
+ for (String s : readableConfig) {
|
|
|
|
+ if (s.equals("<dict>")) {
|
|
|
|
+ if (current.isValid()) textureReferences.add(current);
|
|
|
|
+ current = new TextureReference();
|
|
|
|
+ } else if (s.startsWith("<name>")) {
|
|
|
|
+ current.setName(
|
|
|
|
+ s.replace("<name>", "")
|
|
|
|
+ .replace("</name>", "")
|
|
|
|
+ );
|
|
|
|
+ } else if (s.startsWith("<textureRect>")) {
|
|
|
|
+ String[] usableString =
|
|
|
|
+ s.replace("<textureRect>", "")
|
|
|
|
+ .replace("</textureRect>", "")
|
|
|
|
+ .replace("{", "")
|
|
|
|
+ .replace("}", "")
|
|
|
|
+ .split(",");
|
|
|
|
+ current.setX(Float.parseFloat(usableString[0]));
|
|
|
|
+ current.setY(Float.parseFloat(usableString[1]));
|
|
|
|
+ current.setW(Float.parseFloat(usableString[2]));
|
|
|
|
+ current.setH(Float.parseFloat(usableString[3]));
|
|
|
|
+ } else if (s.startsWith("<textureRotated>")) {
|
|
|
|
+ current.setRotated(
|
|
|
|
+ Boolean.parseBoolean(
|
|
|
|
+ s.replace("<textureRotated>", "")
|
|
|
|
+ .replace("</textureRotated>", "")
|
|
|
|
+ )
|
|
|
|
+ );
|
|
|
|
+ }
|
|
}
|
|
}
|
|
|
|
|
|
- String name = "";
|
|
|
|
- float x = 0, y = 0, w = 0, h = 0;
|
|
|
|
- boolean rotated = false, ready = false;
|
|
|
|
- for (int i = 0; i < readableConfig.length; i++) {
|
|
|
|
- String line = readableConfig[i];
|
|
|
|
- if (line.contains("<key>") && line.contains(".png</key>")) {
|
|
|
|
- name = line.replace("<key>", "").replace("</key>", "");
|
|
|
|
- }
|
|
|
|
- else if (line.contains("<key>textureRect</key>")) {
|
|
|
|
- String data = readableConfig[i+1];
|
|
|
|
- String[] readableData = data
|
|
|
|
- .replace("<string>", "")
|
|
|
|
- .replace("</string>", "")
|
|
|
|
- .replace("}", "")
|
|
|
|
- .replace("{", "")
|
|
|
|
- .split(",");
|
|
|
|
- x = Float.parseFloat(readableData[0]);
|
|
|
|
- y = Float.parseFloat(readableData[1]);
|
|
|
|
- w = Float.parseFloat(readableData[2]);
|
|
|
|
- h = Float.parseFloat(readableData[3]);
|
|
|
|
- } else if (line.contains("<key>textureRotated</key>")) {
|
|
|
|
- if (readableConfig[i+1].contains("<true/>")) {
|
|
|
|
- rotated = true;
|
|
|
|
- } else {
|
|
|
|
- rotated = false;
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
- else if (line.contains("</dict>")) {
|
|
|
|
- ready = true;
|
|
|
|
- }
|
|
|
|
- if (ready && !name.isBlank()) {
|
|
|
|
|
|
+ for (TextureReference reference : textureReferences) {
|
|
|
|
+ new Thread(() -> {
|
|
BufferedImage crop;
|
|
BufferedImage crop;
|
|
try {
|
|
try {
|
|
- if (!rotated) {
|
|
|
|
- crop = image.getSubimage(round(x), round(y), round(w), round(h));
|
|
|
|
|
|
+ if (!reference.isRotated()) {
|
|
|
|
+ crop = image.getSubimage(
|
|
|
|
+ round(reference.getX()),
|
|
|
|
+ round(reference.getY()),
|
|
|
|
+ round(reference.getW()),
|
|
|
|
+ round(reference.getH())
|
|
|
|
+ );
|
|
} else {
|
|
} else {
|
|
- crop = rotate(image.getSubimage(round(x), round(y), round(h), round(w)));
|
|
|
|
|
|
+ crop = rotate(
|
|
|
|
+ image.getSubimage(
|
|
|
|
+ round(reference.getX()),
|
|
|
|
+ round(reference.getY()),
|
|
|
|
+ round(reference.getH()),
|
|
|
|
+ round(reference.getW())
|
|
|
|
+ )
|
|
|
|
+ );
|
|
}
|
|
}
|
|
- File file = new File(output.getPath() + "/" + name);
|
|
|
|
|
|
+ File file = new File(output.getPath() + "/" + reference.getName());
|
|
ImageIO.write(crop, "png", file);
|
|
ImageIO.write(crop, "png", file);
|
|
- } catch (Exception e) {}
|
|
|
|
- x=0; y=0; w=0; h=0; name = "";
|
|
|
|
- }
|
|
|
|
|
|
+ } catch (Exception e) {e.printStackTrace();}
|
|
|
|
+ }).start();
|
|
}
|
|
}
|
|
|
|
+//
|
|
|
|
+// String name = "";
|
|
|
|
+// float x = 0, y = 0, w = 0, h = 0;
|
|
|
|
+// boolean rotated = false, ready = false;
|
|
|
|
+// for (int i = 0; i < readableConfig.length; i++) {
|
|
|
|
+// String line = readableConfig[i];
|
|
|
|
+// if (line.contains("<key>") && line.contains(".png</key>")) {
|
|
|
|
+// name = line.replace("<key>", "").replace("</key>", "");
|
|
|
|
+// }
|
|
|
|
+// else if (line.contains("<key>textureRect</key>")) {
|
|
|
|
+// String data = readableConfig[i+1];
|
|
|
|
+// String[] readableData = data
|
|
|
|
+// .replace("<string>", "")
|
|
|
|
+// .replace("</string>", "")
|
|
|
|
+// .replace("}", "")
|
|
|
|
+// .replace("{", "")
|
|
|
|
+// .split(",");
|
|
|
|
+// x = Float.parseFloat(readableData[0]);
|
|
|
|
+// y = Float.parseFloat(readableData[1]);
|
|
|
|
+// w = Float.parseFloat(readableData[2]);
|
|
|
|
+// h = Float.parseFloat(readableData[3]);
|
|
|
|
+// } else if (line.contains("<key>textureRotated</key>")) {
|
|
|
|
+// if (readableConfig[i+1].contains("<true/>")) {
|
|
|
|
+// rotated = true;
|
|
|
|
+// } else {
|
|
|
|
+// rotated = false;
|
|
|
|
+// }
|
|
|
|
+// }
|
|
|
|
+// else if (line.contains("</dict>")) {
|
|
|
|
+// ready = true;
|
|
|
|
+// }
|
|
|
|
+// if (ready && !name.isBlank()) {
|
|
|
|
+// BufferedImage crop;
|
|
|
|
+// try {
|
|
|
|
+// if (!rotated) {
|
|
|
|
+// crop = image.getSubimage(round(x), round(y), round(w), round(h));
|
|
|
|
+// } else {
|
|
|
|
+// crop = rotate(image.getSubimage(round(x), round(y), round(h), round(w)));
|
|
|
|
+// }
|
|
|
|
+// File file = new File(output.getPath() + "/" + name);
|
|
|
|
+// ImageIO.write(crop, "png", file);
|
|
|
|
+// } catch (Exception e) {}
|
|
|
|
+// x=0; y=0; w=0; h=0; name = "";
|
|
|
|
+// }
|
|
|
|
+// }
|
|
}
|
|
}
|
|
|
|
|
|
public static BufferedImage rotate(BufferedImage src) {
|
|
public static BufferedImage rotate(BufferedImage src) {
|