From 32e1b87e09f89af9bfba94ccbdb2841d246a0845 Mon Sep 17 00:00:00 2001 From: Gonk Date: Sun, 5 Apr 2020 20:46:03 +0200 Subject: [PATCH] Corrected linking of spritesheets in tmx - added or altered spritesheets are now linked with relative paths so that they can be committed to an ATCS project and don't have any unwanted path infomation included - when exporting the project all links to spritesheets are now changed to ../drawable/FILENAME.PNG --- hacked-libtiled/tiled/io/TMXMapWriter.java | 15 +++++- .../rpg/atcontentstudio/model/Project.java | 46 +++++++++++++++---- .../atcontentstudio/model/maps/TMXMap.java | 2 +- 3 files changed, 51 insertions(+), 12 deletions(-) diff --git a/hacked-libtiled/tiled/io/TMXMapWriter.java b/hacked-libtiled/tiled/io/TMXMapWriter.java index d7b5335..bda0076 100644 --- a/hacked-libtiled/tiled/io/TMXMapWriter.java +++ b/hacked-libtiled/tiled/io/TMXMapWriter.java @@ -47,6 +47,9 @@ import java.util.Vector; import java.util.zip.DeflaterOutputStream; import java.util.zip.GZIPOutputStream; +import com.gpl.rpg.atcontentstudio.model.GameSource.Type; +import com.gpl.rpg.atcontentstudio.model.maps.TMXMapSet; + import tiled.core.AnimatedTile; import tiled.core.Map; import tiled.core.MapLayer; @@ -238,7 +241,11 @@ public class TMXMapWriter } else { w.startElement("tileset"); w.writeAttribute("firstgid", getFirstGidForTileset(set)); - w.writeAttribute("source", getRelativePath(wp, source)); + if (set.sheet.parent.getDataType() == Type.source) { + w.writeAttribute("source", (TMXMapSet.DEFAULT_REL_PATH_TO_DRAWABLE + set.getName()).replace("\\", "/")); + } else { + w.writeAttribute("source", getRelativePath(wp, source)); + } if (set.getBaseDir() != null) { w.writeAttribute("basedir", set.getBaseDir()); } @@ -279,7 +286,11 @@ public class TMXMapWriter if (tileBitmapFile != null) { w.startElement("image"); - w.writeAttribute("source", getRelativePath(wp, tileBitmapFile)); + if (set.sheet.parent.getDataType() == Type.source) { + w.writeAttribute("source", (TMXMapSet.DEFAULT_REL_PATH_TO_DRAWABLE + set.getName()).replace("\\", "/")); + } else { + w.writeAttribute("source", getRelativePath(wp, tileBitmapFile)); + } if (set.sheetDimensions != null) { w.writeAttribute("width", set.sheetDimensions.width); w.writeAttribute("height", set.sheetDimensions.height); diff --git a/src/com/gpl/rpg/atcontentstudio/model/Project.java b/src/com/gpl/rpg/atcontentstudio/model/Project.java index e8182ca..a6dab19 100644 --- a/src/com/gpl/rpg/atcontentstudio/model/Project.java +++ b/src/com/gpl/rpg/atcontentstudio/model/Project.java @@ -11,6 +11,10 @@ import java.io.IOException; import java.io.Serializable; import java.io.StringReader; import java.io.StringWriter; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; import java.util.ArrayList; import java.util.Collections; import java.util.Enumeration; @@ -74,7 +78,9 @@ import com.gpl.rpg.atcontentstudio.utils.FileUtils; public class Project implements ProjectTreeNode, Serializable { private static final long serialVersionUID = 4807454973303366758L; + private static final String drawablePath = TMXMapSet.DEFAULT_REL_PATH_TO_DRAWABLE.replace("\\", "/"); + //Every instance field that is not transient will be saved in this file. public static final String SETTINGS_FILE = ".project"; @@ -1079,10 +1085,16 @@ public class Project implements ProjectTreeNode, Serializable { public void run() { Notification.addInfo("Exporting project \""+name+"\" as "+target.getAbsolutePath()); - File tmpDir = exportProjectToTmpDir(); + File tmpDir; + try { + tmpDir = exportProjectToTmpDir(); + FileUtils.writeToZip(tmpDir, target); + FileUtils.deleteDir(tmpDir); + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } - FileUtils.writeToZip(tmpDir, target); - FileUtils.deleteDir(tmpDir); Notification.addSuccess("Project \""+name+"\" exported as "+target.getAbsolutePath()); } @@ -1096,10 +1108,16 @@ public class Project implements ProjectTreeNode, Serializable { public void run() { Notification.addInfo("Exporting project \""+name+"\" into "+target.getAbsolutePath()); - File tmpDir = exportProjectToTmpDir(); + File tmpDir; + try { + tmpDir = exportProjectToTmpDir(); + FileUtils.copyOver(tmpDir, target); + FileUtils.deleteDir(tmpDir); + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } - FileUtils.copyOver(tmpDir, target); - FileUtils.deleteDir(tmpDir); Notification.addSuccess("Project \""+name+"\" exported into "+target.getAbsolutePath()); } @@ -1107,7 +1125,7 @@ public class Project implements ProjectTreeNode, Serializable { }); } - public File exportProjectToTmpDir() { + public File exportProjectToTmpDir() throws IOException { File tmpDir = new File(baseFolder, "tmp"); FileUtils.deleteDir(tmpDir); tmpDir.mkdir(); @@ -1139,12 +1157,12 @@ public class Project implements ProjectTreeNode, Serializable { writtenFiles = new LinkedList(); for (File createdMapFile : createdContent.gameMaps.mapFolder.listFiles()) { if (createdMapFile.getName().equalsIgnoreCase("worldmap.xml")) continue; - FileUtils.copyFile(createdMapFile, new File(tmpMapDir, createdMapFile.getName())); + copyTmxConverted(createdMapFile.toPath(), Paths.get(tmpMapDir.getAbsolutePath(), createdMapFile.getName())); writtenFiles.add(createdMapFile.getName()); } for (File alteredMapFile : alteredContent.gameMaps.mapFolder.listFiles()) { if (alteredMapFile.getName().equalsIgnoreCase("worldmap.xml")) continue; - FileUtils.copyFile(alteredMapFile, new File(tmpMapDir, alteredMapFile.getName())); + copyTmxConverted(alteredMapFile.toPath(), Paths.get(tmpMapDir.getAbsolutePath(), alteredMapFile.getName())); writtenFiles.add(alteredMapFile.getName()); } writtenFilesPerDataType.put(TMXMap.class, writtenFiles); @@ -1177,6 +1195,16 @@ public class Project implements ProjectTreeNode, Serializable { return tmpDir; } + private void copyTmxConverted(Path from, Path to) throws IOException { + String xml = new String(Files.readAllBytes(from), StandardCharsets.UTF_8); + xml = xml.replace("../../altered/spritesheets/", drawablePath); + xml = xml.replace("../../created/spritesheets/", drawablePath); + xml = xml.replace("../spritesheets/", drawablePath); + xml = xml.replace("../spritesheets/", drawablePath); + Files.write(to, xml.getBytes(StandardCharsets.UTF_8)); + } + + @SuppressWarnings("rawtypes") public List writeDataDeltaForDataType(GameDataCategory created, GameDataCategory altered, GameDataCategory source, Class gdeClass, File targetFolder) { List filenamesToWrite = new LinkedList(); diff --git a/src/com/gpl/rpg/atcontentstudio/model/maps/TMXMap.java b/src/com/gpl/rpg/atcontentstudio/model/maps/TMXMap.java index 4a2fe88..38da7fc 100644 --- a/src/com/gpl/rpg/atcontentstudio/model/maps/TMXMap.java +++ b/src/com/gpl/rpg/atcontentstudio/model/maps/TMXMap.java @@ -258,7 +258,7 @@ public class TMXMap extends GameDataElement { if (getDataType() == GameSource.Type.source) { writer.writeMap(tmxMap, baos, tmxFile.getAbsolutePath()); } else { - writer.writeMap(tmxMap, baos, getProject().baseContent.gameMaps.mapFolder.getAbsolutePath()+File.separator+"placeholder.tmx"); + writer.writeMap(tmxMap, baos, ((TMXMapSet)this.parent).mapFolder.getAbsolutePath()+File.separator+"placeholder.tmx"); } } catch (Exception e) { Notification.addError("Error while converting map "+getDesc()+" to XML: "+e.getMessage());