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
This commit is contained in:
Gonk
2020-04-05 20:46:03 +02:00
parent 4b6be99d98
commit 32e1b87e09
3 changed files with 51 additions and 12 deletions

View File

@@ -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);

View File

@@ -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<String>();
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<String> writeDataDeltaForDataType(GameDataCategory<? extends JSONElement> created, GameDataCategory<? extends JSONElement> altered, GameDataCategory<? extends JSONElement> source, Class<? extends JSONElement> gdeClass, File targetFolder) {
List<String> filenamesToWrite = new LinkedList<String>();

View File

@@ -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());