1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219
|
<?xml version="1.0" encoding="UTF-8"?>
<script>
<name>WorldMaker</name>
<code><![CDATA[import cfeditor.IGUIConstants;
import java.io.IOException;
import javax.imageio.ImageIO;
import java.io.RandomAccessFile;
import java.io.FileOutputStream;
import java.io.File;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
File getSimpleFilename(File mapFile) {
String mapFilename = mapFile.getPath();
int i = mapFilename.lastIndexOf(File.separator);
if (i > 0) {
mapFilename = mapFilename.substring(i + 1);
}
return new File(mapFilename);
}
File getPngImageFilename(File mapFile) {
return new File(locationDir, PictureDirectory + getSimpleFilename(mapFile) + ".png");
}
boolean updateMap(File mapFile, File pictureFile) {
if (!mapFile.exists()) {
return false;
}
if (pictureFile.exists() && pictureFile.lastModified() >= mapFile.lastModified()) {
return false;
}
print("converting " + mapFile + " to " + pictureFile + ".");
try {
map = mapManager.openMapFile(mapFile, false);
} catch (IOException ex) {
return false;
}
try {
try {
ImageIO.write(rendererFactory.newSimpleMapRenderer(map.getMapModel()).getFullImage(), "png", pictureFile);
} catch (IOException ex) {
print("cannot write " + pictureFile + ": " + ex.getMessage());
return false;
}
} finally {
mapManager.release(map);
}
return true;
}
boolean runCommand(String cmd) {
f = File.createTempFile("WMaker", ".sh");
FileWriter out = new FileWriter(f);
out.write(cmd);
out.close();
print("running " + cmd);
Process p = Runtime.getRuntime().exec("sh " + f.getAbsolutePath());
p.waitFor();
f.delete();
return p.exitValue() == 0;
}
void checkDaList() {
DestWidth = TileWidth.intValue() * NumX.intValue();
DestHeight = TileHeight.intValue() * NumY.intValue();
if (Location == null || Location.length() == 0) {
locationDir = globalSettings.getMapsDirectory();
} else {
locationDir = new File(Location);
}
print("World map will be " + DestWidth + "x" + DestHeight + " in size");
if (!PictureDirectory.endsWith(File.separator)) {
PictureDirectory = PictureDirectory + File.separator;
}
new File(locationDir, PictureDirectory).mkdirs();
final long bytesPerPixel = 3L;
HashSet mapList = new HashSet();
boolean firstRun = false;
byte[] header = ("P6\n" + DestWidth + " " + DestHeight + "\n255\n").getBytes("ISO-8859-1");
long headerSize = header.length;
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 {
print("generating empty picture");
FileOutputStream fos = new FileOutputStream(tempFile, false);
fos.write(header);
byte[] buf = new byte[(int) DestWidth.intValue() * bytesPerPixel];
for (int i = 0; i < DestHeight.intValue(); i++) {
fos.write(buf);
}
fos.close();
firstRun = true;
}
long toSkip = ("P6\n" + TileWidth + " " + TileHeight+"\n255\n").getBytes("ISO-8859-1").length;
RandomAccessFile raf = new RandomAccessFile(tempFile, "rw");
FileChannel fc = raf.getChannel();
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 y = 0; y < NumY.intValue(); y++) {
currentX = StartX.intValue() + x;
currentY = StartY.intValue() + y;
currentMap = new File(locationDir, MapFilename + "_" + currentX + "_" + currentY);
currentPicture = getPngImageFilename(currentMap);
didUpdate = updateMap(currentMap, currentPicture);
if ((didUpdate || firstRun) && currentPicture.exists()) {
runCommand("pngtopnm '" + currentPicture + "' | pnmscale -xysize " + TileWidth + " " + TileHeight + " > '" + tempImageFile + "'");
FileInputStream fis = new FileInputStream(tempImageFile);
fis.skip(toSkip);
fis.read(buf);
sx = x * TileWidth.intValue();
sy = y * TileHeight.intValue();
long index = ((long) sy * (long) DestWidth.intValue() + (long) sx) * bytesPerPixel + headerSize;
for (long row = 0; row < TileHeight.intValue(); row++) {
MappedByteBuffer mbb = fc.map(FileChannel.MapMode.READ_WRITE, index + row * DestWidth.intValue() * bytesPerPixel, TileWidth.intValue() * bytesPerPixel);
mbb.put(buf, (int) (row * TileWidth.intValue() * bytesPerPixel), (int) (TileWidth.intValue() * bytesPerPixel));
}
}
}
}
raf.close();
runCommand("mv " + tempFile + " '" + destinationFilePpm + "'");
print("converting to png if possible.\n");
runCommand("pnmtopng '" + destinationFilePpm + "' > /tmp/tmp.png");
runCommand("mv /tmp/tmp.png '" + destinationFilePng + "'");
}
File locationDir;
checkDaList();
print("Done!");]]></code>
<mode>
<autoboot>false</autoboot>
<bash>true</bash>
<filter>false</filter>
</mode>
<parameter>
<name>Location</name>
<description>Specify the map directory to use by this script. Leave empty for maps directory</description>
<type>java.lang.String</type>
<value />
</parameter>
<parameter>
<name>MapFilename</name>
<description>This map file name will be appended to the 'Location' parameter and '_mapx_mapy' will be added at the end</description>
<type>java.lang.String</type>
<value>world/world</value>
</parameter>
<parameter>
<name>TileWidth</name>
<description>The width in pixel of each generate map image</description>
<type>java.lang.Integer</type>
<value>50</value>
<minimum>0</minimum>
<maximum>2000</maximum>
</parameter>
<parameter>
<name>TileHeight</name>
<description>The height in pixel of each generated map image</description>
<type>java.lang.Integer</type>
<value>50</value>
<minimum>0</minimum>
<maximum>2000</maximum>
</parameter>
<parameter>
<name>NumX</name>
<description>The number of maps along X axis to analyze</description>
<type>java.lang.Integer</type>
<value>30</value>
<minimum>0</minimum>
<maximum>50000</maximum>
</parameter>
<parameter>
<name>NumY</name>
<description>The number of maps along Y axis to analyze</description>
<type>java.lang.Integer</type>
<value>30</value>
<minimum>0</minimum>
<maximum>50000</maximum>
</parameter>
<parameter>
<name>StartX</name>
<description>The first coordinate along X axis to analyze</description>
<type>java.lang.Integer</type>
<value>100</value>
<minimum>0</minimum>
<maximum>50000</maximum>
</parameter>
<parameter>
<name>StartY</name>
<description>The first coordinate along Y axis to analyze</description>
<type>java.lang.Integer</type>
<value>100</value>
<minimum>0</minimum>
<maximum>50000</maximum>
</parameter>
<parameter>
<name>PictureDirectory</name>
<description>The subdirectory where to put pictures</description>
<type>java.lang.String</type>
<value>images</value>
</parameter>
<parameter>
<name>WorldPicture</name>
<description>The picture which will store the world map</description>
<type>java.lang.String</type>
<value>worldmap</value>
</parameter>
</script>
|