maps/editor/scripts/MapNormalizer

130 lines
4.5 KiB
XML

<?xml version="1.0" encoding="UTF-8"?>
<script>
<name>MapNormalizer</name>
<code xml:space="preserve">import java.io.File;
import java.util.Iterator;
import net.sf.gridarta.gui.dialog.shrinkmapsize.ShrinkMapSizeUtils;
import net.sf.gridarta.map.mapmodel.MapModel;
import net.sf.gridarta.map.mapmodel.MapSquare;
import net.sf.gridarta.model.direction.Direction;
import net.sf.gridarta.model.gameobject.GameObject;
import net.sf.gridarta.model.io.AttributeListUtils;
import net.sf.gridarta.model.io.RecursiveFileIterator;
void normalizeGameObject(GameObject gameObject) {
Iterator it = gameObject.iterator();
while (it.hasNext()) {
normalizeGameObject(it.next());
}
gameObject.setObjectText(AttributeListUtils.diffArchTextValues(gameObject.getArchetype(), gameObject.getObjectText()));
}
void normalizeMap(File mapFile, String mapPath) {
print(mapPath);
try {
map = mapManager.openMapFile(mapFile, false);
} catch (IOException ex) {
String message = ex.getMessage();
if (!message.startsWith("unexpected first line of map ")) {
print("Cannot load map '"+mapFile+"': "+ex.getMessage());
}
return;
}
try {
mapModel = map.getMapModel();
mapModel.beginTransaction("Normalize");
try {
if (!mapPath.startsWith("/styles")
&amp;&amp; !mapPath.startsWith("/editor/pickmaps")
&amp;&amp; !mapPath.startsWith("/editor/walls")
&amp;&amp; !mapPath.endsWith("/.gitignore")
&amp;&amp; !mapPath.endsWith("/__pycache__")) {
mapArchObject = mapModel.getMapArchObject();
int shrinkFlags = 0;
if (mapArchObject.getTilePath(Direction.NORTH).length() &lt;= 0 &amp;&amp; mapArchObject.getTilePath(Direction.SOUTH).length() &lt;= 0) {
shrinkFlags |= ShrinkMapSizeUtils.SHRINK_EAST;
}
if (mapArchObject.getTilePath(Direction.EAST).length() &lt;= 0 &amp;&amp; mapArchObject.getTilePath(Direction.WEST).length() &lt;= 0) {
shrinkFlags |= ShrinkMapSizeUtils.SHRINK_SOUTH;
}
ShrinkMapSizeUtils.shrinkMap(mapModel, shrinkFlags);
}
Iterator it = mapModel.iterator();
while (it.hasNext()) {
Iterator it2 = it.next().iterator();
while (it2.hasNext()) {
normalizeGameObject(it2.next());
}
}
} finally {
mapModel.endTransaction();
}
if (mapModel.isModified()) {
map.save();
}
} finally {
mapManager.release(map);
}
}
if (baseDirectory == null || baseDirectory.length() &lt;= 0) {
baseDirectory = "/";
}
print("Normalizing maps below " + baseDirectory + "...");
if (baseDirectory.endsWith("/")) {
baseDirectory = baseDirectory.substring(0, baseDirectory.length() - 1);
}
String mapDefaultFolder = globalSettings.getMapsDirectory().getPath();
String rootDirectory = mapDefaultFolder + baseDirectory;
Iterator it = new RecursiveFileIterator(new File(rootDirectory));
while (it.hasNext()) {
File file = it.next();
String name = file.getName();
String path = file.getPath();
if (file.isFile()
&amp;&amp; path.startsWith(rootDirectory)
&amp;&amp; !name.endsWith(".animation")
&amp;&amp; !name.endsWith(".msg")
&amp;&amp; !name.endsWith(".png")
&amp;&amp; !name.endsWith(".ppm")
&amp;&amp; !name.endsWith(".py")
&amp;&amp; !name.endsWith(".pyc")
&amp;&amp; !name.endsWith(".quests")
&amp;&amp; !name.endsWith(".txt")
&amp;&amp; !name.endsWith(".zip")
&amp;&amp; !name.equals(".emergency")
&amp;&amp; !name.equals(".gitignore")
&amp;&amp; !name.equals("COPYING")
&amp;&amp; !name.equals("ChangeLog")
&amp;&amp; !name.equals("TODO")
&amp;&amp; !name.equals("__pycache__")
&amp;&amp; !name.equals("pshop_copier")
&amp;&amp; !name.equals("pshops_changelog")
&amp;&amp; !name.equalsIgnoreCase("README")
&amp;&amp; !path.contains("/.git/")
&amp;&amp; !path.contains("/Info/")
&amp;&amp; !path.contains("/editor/scripts/")) {
normalizeMap(file, file.getPath().substring(mapDefaultFolder.length()));
}
}
print("Done.");</code>
<mode>
<autoboot>false</autoboot>
<bash>true</bash>
<filter>false</filter>
</mode>
<parameter>
<name>baseDirectory</name>
<description>Base Directory</description>
<type>MapPathParameter</type>
<value>/</value>
</parameter>
</script>