Clean up WorldMaker script. Make it more robust.

git-svn-id: svn://svn.code.sf.net/p/crossfire/code/maps/trunk@14796 282e977c-c81d-0410-88c4-b93c2d0d6712
master
akirschbaum 2011-07-08 17:27:01 +00:00
parent a772df9084
commit a8c71d01e8
1 changed files with 34 additions and 35 deletions

View File

@ -10,7 +10,6 @@ import java.io.File;
import java.nio.MappedByteBuffer; import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel; import java.nio.channels.FileChannel;
File getSimpleFilename(File mapFile) { File getSimpleFilename(File mapFile) {
String mapFilename = mapFile.getPath(); String mapFilename = mapFile.getPath();
int i = mapFilename.lastIndexOf(File.separator); int i = mapFilename.lastIndexOf(File.separator);
@ -21,7 +20,7 @@ File getSimpleFilename(File mapFile) {
} }
File getPngImageFilename(File mapFile) { File getPngImageFilename(File mapFile) {
return new File(Location + PictureDirectory + getSimpleFilename(mapFile) + ".png"); return new File(locationDir, PictureDirectory + getSimpleFilename(mapFile) + ".png");
} }
boolean updateMap(File mapFile, File pictureFile) { boolean updateMap(File mapFile, File pictureFile) {
@ -66,77 +65,77 @@ void checkDaList() {
DestWidth = TileWidth.intValue() * NumX.intValue(); DestWidth = TileWidth.intValue() * NumX.intValue();
DestHeight = TileHeight.intValue() * NumY.intValue(); DestHeight = TileHeight.intValue() * NumY.intValue();
if (Location == null || Location.length() < 1) { if (Location == null || Location.length() == 0) {
Location = globalSettings.getMapsDirectory().getPath(); locationDir = globalSettings.getMapsDirectory();
print("autodetected location " + Location); } else {
locationDir = new File(Location);
} }
print("World map will be " + DestWidth + "x" + DestHeight + " in size"); print("World map will be " + DestWidth + "x" + DestHeight + " in size");
if (!Location.endsWith(File.separator)) {
Location = Location + File.separator;
}
if (!PictureDirectory.endsWith(File.separator)) { if (!PictureDirectory.endsWith(File.separator)) {
PictureDirectory = PictureDirectory + File.separator; PictureDirectory = PictureDirectory + File.separator;
} }
new File(Location + PictureDirectory).mkdirs(); new File(locationDir, PictureDirectory).mkdirs();
final long bytesPerPixel = 3L;
HashSet mapList = new HashSet(); HashSet mapList = new HashSet();
boolean firstRun = false; boolean firstRun = false;
long headerSize = ("P6\n" + DestWidth + " "+DestHeight + "\n255\n").getBytes().length; byte[] header = ("P6\n" + DestWidth + " " + DestHeight + "\n255\n").getBytes("ISO-8859-1");
if (new File(Location + PictureDirectory + WorldPicture + ".ppm").exists()) { long headerSize = header.length;
runCommand("cp " + Location + PictureDirectory + WorldPicture + ".ppm /tmp/tmp.ppm"); File destinationFilePpm = new File(locationDir, PictureDirectory + WorldPicture + ".ppm");
File destinationFilePng = new File(locationDir, PictureDirectory + WorldPicture + ".png");
File tempFile = new File("/tmp/tmp.ppm");
if (destinationFilePpm.exists()) {
runCommand("cp '" + destinationFilePpm + "' '" + tempFile + "'");
} else { } else {
File f = new File("/tmp/tmp.ppm");
String header = "P6\n" + DestWidth + " " + DestHeight + "\n255\n";
print("generating empty picture"); print("generating empty picture");
FileOutputStream fos = new FileOutputStream(f, false); FileOutputStream fos = new FileOutputStream(tempFile, false);
fos.write(header.getBytes()); fos.write(header);
byte[] buf = new byte[(int) DestWidth.intValue() * 3]; byte[] buf = new byte[(int) DestWidth.intValue() * bytesPerPixel];
for (int i = 0; i < DestHeight.intValue(); i++) { for (int i = 0; i < DestHeight.intValue(); i++) {
fos.write(buf); fos.write(buf);
} }
fos.close(); fos.close();
firstRun = true; firstRun = true;
} }
long toSkip = ("P6\n" + TileWidth + " " + TileHeight+"\n255\n").getBytes().length; long toSkip = ("P6\n" + TileWidth + " " + TileHeight+"\n255\n").getBytes("ISO-8859-1").length;
RandomAccessFile raf = new RandomAccessFile(new File("/tmp/tmp.ppm"), "rw"); RandomAccessFile raf = new RandomAccessFile(tempFile, "rw");
FileChannel fc = raf.getChannel(); FileChannel fc = raf.getChannel();
byte[] buf = new byte[3 * TileWidth.intValue() * TileHeight.intValue()]; byte[] buf = new byte[bytesPerPixel * TileWidth.intValue() * TileHeight.intValue()];
File tempImageFile = new File("/tmp/ppm.tmp");
for (int x = 0; x < NumX.intValue(); x++) { for (int x = 0; x < NumX.intValue(); x++) {
for (int y = 0; y < NumY.intValue(); y++) { for (int y = 0; y < NumY.intValue(); y++) {
currentX = StartX.intValue() + x; currentX = StartX.intValue() + x;
currentY = StartY.intValue() + y; currentY = StartY.intValue() + y;
currentMap = new File(Location + MapFilename + "_" + currentX + "_" + currentY); currentMap = new File(locationDir, MapFilename + "_" + currentX + "_" + currentY);
currentPicture = getPngImageFilename(currentMap); currentPicture = getPngImageFilename(currentMap);
didUpdate = updateMap(currentMap, currentPicture); didUpdate = updateMap(currentMap, currentPicture);
if ((didUpdate || firstRun) && currentPicture.exists()) { if ((didUpdate || firstRun) && currentPicture.exists()) {
runCommand("pngtopnm " + currentPicture + " | pnmscale -xysize " + TileWidth + " " + TileHeight + " > /tmp/ppm.tmp"); runCommand("pngtopnm '" + currentPicture + "' | pnmscale -xysize " + TileWidth + " " + TileHeight + " > '" + tempImageFile + "'");
FileInputStream fis = new FileInputStream("/tmp/ppm.tmp"); FileInputStream fis = new FileInputStream(tempImageFile);
fis.skip(toSkip); fis.skip(toSkip);
fis.read(buf); fis.read(buf);
sx = x * TileWidth.intValue(); sx = x * TileWidth.intValue();
sy = y * TileHeight.intValue(); sy = y * TileHeight.intValue();
long index=((long) sy * (long) DestWidth.intValue() + (long) sx) * (long) 3 + headerSize; long index = ((long) sy * (long) DestWidth.intValue() + (long) sx) * bytesPerPixel + headerSize;
for (long row = 0; row < TileHeight.intValue(); row++){ for (long row = 0; row < TileHeight.intValue(); row++) {
MappedByteBuffer mbb = fc.map( MappedByteBuffer mbb = fc.map(FileChannel.MapMode.READ_WRITE, index + row * DestWidth.intValue() * bytesPerPixel, TileWidth.intValue() * bytesPerPixel);
java.nio.channels.FileChannel.MapMode.READ_WRITE, mbb.put(buf, (int) (row * TileWidth.intValue() * bytesPerPixel), (int) (TileWidth.intValue() * bytesPerPixel));
index + row * DestWidth.intValue() * (long) 3,
TileWidth.intValue() * (long) 3
);
mbb.put(buf, (int) (row * TileWidth.intValue() * 3), (int) (TileWidth.intValue() * 3));
} }
} }
} }
} }
raf.close(); raf.close();
runCommand("mv /tmp/tmp.ppm " + Location + PictureDirectory + WorldPicture + ".ppm"); runCommand("mv " + tempFile + " '" + destinationFilePpm + "'");
print("converting to png if possible.\n"); print("converting to png if possible.\n");
runCommand("pnmtopng " + Location + PictureDirectory + WorldPicture + ".ppm > /tmp/tmp.png"); runCommand("pnmtopng '" + destinationFilePpm + "' > /tmp/tmp.png");
runCommand("mv /tmp/tmp.png " + Location + PictureDirectory + WorldPicture + ".png"); runCommand("mv /tmp/tmp.png '" + destinationFilePng + "'");
} }
File locationDir;
checkDaList(); checkDaList();
print("Done!");]]></code> print("Done!");]]></code>
<mode> <mode>
@ -146,7 +145,7 @@ print("Done!");]]></code>
</mode> </mode>
<parameter> <parameter>
<name>Location</name> <name>Location</name>
<description>Specify the map directory to use by this script. Leave empty for auto</description> <description>Specify the map directory to use by this script. Leave empty for maps directory</description>
<type>java.lang.String</type> <type>java.lang.String</type>
<value /> <value />
</parameter> </parameter>