Make editor scripts work with latest Gridarta build.
git-svn-id: svn://svn.code.sf.net/p/crossfire/code/maps/trunk@9158 282e977c-c81d-0410-88c4-b93c2d0d6712master
parent
1511921f17
commit
68e0d4b266
|
@ -309,7 +309,7 @@ if (baseDirectory.endsWith("/")) {
|
|||
baseDirectory = baseDirectory.substring(0, baseDirectory.length() - 1);
|
||||
}
|
||||
|
||||
String mapDefaultFolder = mainControl.getMapDefaultFolder();
|
||||
String mapDefaultFolder = mainControl.getGlobalSettings().getMapDefaultFolder();
|
||||
String rootDirectory = mapDefaultFolder + baseDirectory;
|
||||
Iterator it = new RecursiveFileIterator(new File(rootDirectory));
|
||||
while (it.hasNext()) {
|
||||
|
|
|
@ -25,7 +25,7 @@ if (baseDirectory.endsWith("/")) {
|
|||
baseDirectory = baseDirectory.substring(0, baseDirectory.length() - 1);
|
||||
}
|
||||
|
||||
String mapDefaultFolder = mainControl.getMapDefaultFolder();
|
||||
String mapDefaultFolder = mainControl.getGlobalSettings().getMapDefaultFolder();
|
||||
String rootDirectory = mapDefaultFolder + baseDirectory;
|
||||
Iterator it = new RecursiveFileIterator(new File(rootDirectory));
|
||||
while (it.hasNext()) {
|
||||
|
|
|
@ -80,7 +80,7 @@ try {
|
|||
baseDirectory = baseDirectory.substring(0, baseDirectory.length() - 1);
|
||||
}
|
||||
|
||||
String mapDefaultFolder = mainControl.getMapDefaultFolder();
|
||||
String mapDefaultFolder = mainControl.getGlobalSettings().getMapDefaultFolder();
|
||||
String rootDirectory = mapDefaultFolder + baseDirectory;
|
||||
Iterator it = new RecursiveFileIterator(new File(rootDirectory));
|
||||
while (it.hasNext()) {
|
||||
|
|
|
@ -1,152 +1,152 @@
|
|||
<?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;
|
||||
|
||||
|
||||
/*
|
||||
* Functions declaration
|
||||
*/
|
||||
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(Location + 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 + ".");
|
||||
map = mainControl.getMapManager().openMapFile(mapFile, false);
|
||||
if (map == null) {
|
||||
return false;
|
||||
}
|
||||
try {
|
||||
try {
|
||||
ImageIO.write(map.getFullImage(), "png", pictureFile);
|
||||
} catch (IOException ex) {
|
||||
print("cannot write " + pictureFile + ": " + ex.getMessage());
|
||||
return false;
|
||||
}
|
||||
} finally {
|
||||
if (map.nViews() <= 0) {
|
||||
mainControl.getMapManager().closeLevel(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;
|
||||
}
|
||||
|
||||
/*
|
||||
* Running code
|
||||
*/
|
||||
void checkDaList() {
|
||||
DestWidth = TileWidth.intValue() * NumX.intValue();
|
||||
DestHeight = TileHeight.intValue() * NumY.intValue();
|
||||
|
||||
if (Location == null || Location.length() < 1) {
|
||||
Location = mainControl.getMapDefaultFolder();
|
||||
print("autodetected location " + Location);
|
||||
}
|
||||
|
||||
print("World map will be " + DestWidth + "x" + DestHeight + " in size");
|
||||
if (!Location.endsWith(File.separator)) {
|
||||
Location = Location + File.separator;
|
||||
}
|
||||
|
||||
if (!PictureDirectory.endsWith(File.separator)) {
|
||||
PictureDirectory = PictureDirectory + File.separator;
|
||||
}
|
||||
new File(Location + PictureDirectory).mkdirs();
|
||||
|
||||
HashSet mapList = new HashSet();
|
||||
boolean firstRun = false;
|
||||
print("...");
|
||||
long headerSize = ("P6\n"+DestWidth+" "+DestHeight+"\n255\n").getBytes().length;
|
||||
print("...");
|
||||
if (new File(Location + PictureDirectory + WorldPicture + ".ppm").exists()) {
|
||||
runCommand("cp " + Location + PictureDirectory + WorldPicture + ".ppm /tmp/tmp.ppm");
|
||||
} else {
|
||||
File f = new File("/tmp/tmp.ppm");
|
||||
String header = "P6\n"+DestWidth+" "+DestHeight+"\n255\n" ;
|
||||
print("generating empty picture");
|
||||
FileOutputStream fos = new FileOutputStream(f,false);
|
||||
fos.write(header.getBytes());
|
||||
byte[] buf = new byte[(int)DestWidth.intValue()*3];
|
||||
for (int i=0;i<DestHeight.intValue();i++) fos.write(buf);
|
||||
fos.close();
|
||||
firstRun = true;
|
||||
}
|
||||
print("gogogo");
|
||||
long toSkip = ("P6\n"+TileWidth+" "+TileHeight+"\n255\n").getBytes().length;
|
||||
RandomAccessFile raf = new RandomAccessFile(new File("/tmp/tmp.ppm"),"rw");
|
||||
FileChannel fc = raf.getChannel();
|
||||
byte[] buf = new byte[3*TileWidth.intValue()*TileHeight.intValue()];
|
||||
|
||||
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(Location + MapFilename + "_" + currentX + "_" + currentY);
|
||||
currentPicture = getPngImageFilename(currentMap);
|
||||
didUpdate = updateMap(currentMap, currentPicture);
|
||||
if ((didUpdate || firstRun) && currentPicture.exists()) {
|
||||
runCommand("pngtopnm " + currentPicture + " | pnmscale -xysize " + TileWidth + " " + TileHeight + " > /tmp/ppm.tmp");
|
||||
FileInputStream fis = new FileInputStream("/tmp/ppm.tmp");
|
||||
fis.skip(toSkip);
|
||||
fis.read(buf);
|
||||
sx = x * TileWidth.intValue();
|
||||
sy = y * TileHeight.intValue();
|
||||
long index=((long)sy*(long)DestWidth.intValue()+(long)sx)*(long)3+headerSize;
|
||||
for (long row=0; row<TileHeight.intValue();row++){
|
||||
MappedByteBuffer mbb = fc.map(
|
||||
java.nio.channels.FileChannel.MapMode.READ_WRITE,
|
||||
index+row*DestWidth.intValue()*(long)3,
|
||||
TileWidth.intValue()*(long)3
|
||||
);
|
||||
mbb.put(buf,(int)(row*TileWidth.intValue()*3),(int)(TileWidth.intValue()*3));
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
raf.close();
|
||||
runCommand("mv /tmp/tmp.ppm " + Location + PictureDirectory + WorldPicture + ".ppm");
|
||||
print("converting to png if possible.\n");
|
||||
runCommand("pnmtopng " + Location + PictureDirectory + WorldPicture + ".ppm > /tmp/tmp.png");
|
||||
runCommand("mv /tmp/tmp.png " + Location + PictureDirectory + WorldPicture + ".png");
|
||||
}
|
||||
|
||||
checkDaList();
|
||||
<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;
|
||||
|
||||
|
||||
/*
|
||||
* Functions declaration
|
||||
*/
|
||||
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(Location + 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 + ".");
|
||||
map = mainControl.getMapManager().openMapFile(mapFile, false);
|
||||
if (map == null) {
|
||||
return false;
|
||||
}
|
||||
try {
|
||||
try {
|
||||
ImageIO.write(map.getRenderer().getFullImage(), "png", pictureFile);
|
||||
} catch (IOException ex) {
|
||||
print("cannot write " + pictureFile + ": " + ex.getMessage());
|
||||
return false;
|
||||
}
|
||||
} finally {
|
||||
if (map.nViews() <= 0) {
|
||||
mainControl.getMapManager().closeLevel(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;
|
||||
}
|
||||
|
||||
/*
|
||||
* Running code
|
||||
*/
|
||||
void checkDaList() {
|
||||
DestWidth = TileWidth.intValue() * NumX.intValue();
|
||||
DestHeight = TileHeight.intValue() * NumY.intValue();
|
||||
|
||||
if (Location == null || Location.length() < 1) {
|
||||
Location = mainControl.getGlobalSettings().getMapDefaultFolder();
|
||||
print("autodetected location " + Location);
|
||||
}
|
||||
|
||||
print("World map will be " + DestWidth + "x" + DestHeight + " in size");
|
||||
if (!Location.endsWith(File.separator)) {
|
||||
Location = Location + File.separator;
|
||||
}
|
||||
|
||||
if (!PictureDirectory.endsWith(File.separator)) {
|
||||
PictureDirectory = PictureDirectory + File.separator;
|
||||
}
|
||||
new File(Location + PictureDirectory).mkdirs();
|
||||
|
||||
HashSet mapList = new HashSet();
|
||||
boolean firstRun = false;
|
||||
print("...");
|
||||
long headerSize = ("P6\n"+DestWidth+" "+DestHeight+"\n255\n").getBytes().length;
|
||||
print("...");
|
||||
if (new File(Location + PictureDirectory + WorldPicture + ".ppm").exists()) {
|
||||
runCommand("cp " + Location + PictureDirectory + WorldPicture + ".ppm /tmp/tmp.ppm");
|
||||
} else {
|
||||
File f = new File("/tmp/tmp.ppm");
|
||||
String header = "P6\n"+DestWidth+" "+DestHeight+"\n255\n" ;
|
||||
print("generating empty picture");
|
||||
FileOutputStream fos = new FileOutputStream(f,false);
|
||||
fos.write(header.getBytes());
|
||||
byte[] buf = new byte[(int)DestWidth.intValue()*3];
|
||||
for (int i=0;i<DestHeight.intValue();i++) fos.write(buf);
|
||||
fos.close();
|
||||
firstRun = true;
|
||||
}
|
||||
print("gogogo");
|
||||
long toSkip = ("P6\n"+TileWidth+" "+TileHeight+"\n255\n").getBytes().length;
|
||||
RandomAccessFile raf = new RandomAccessFile(new File("/tmp/tmp.ppm"),"rw");
|
||||
FileChannel fc = raf.getChannel();
|
||||
byte[] buf = new byte[3*TileWidth.intValue()*TileHeight.intValue()];
|
||||
|
||||
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(Location + MapFilename + "_" + currentX + "_" + currentY);
|
||||
currentPicture = getPngImageFilename(currentMap);
|
||||
didUpdate = updateMap(currentMap, currentPicture);
|
||||
if ((didUpdate || firstRun) && currentPicture.exists()) {
|
||||
runCommand("pngtopnm " + currentPicture + " | pnmscale -xysize " + TileWidth + " " + TileHeight + " > /tmp/ppm.tmp");
|
||||
FileInputStream fis = new FileInputStream("/tmp/ppm.tmp");
|
||||
fis.skip(toSkip);
|
||||
fis.read(buf);
|
||||
sx = x * TileWidth.intValue();
|
||||
sy = y * TileHeight.intValue();
|
||||
long index=((long)sy*(long)DestWidth.intValue()+(long)sx)*(long)3+headerSize;
|
||||
for (long row=0; row<TileHeight.intValue();row++){
|
||||
MappedByteBuffer mbb = fc.map(
|
||||
java.nio.channels.FileChannel.MapMode.READ_WRITE,
|
||||
index+row*DestWidth.intValue()*(long)3,
|
||||
TileWidth.intValue()*(long)3
|
||||
);
|
||||
mbb.put(buf,(int)(row*TileWidth.intValue()*3),(int)(TileWidth.intValue()*3));
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
raf.close();
|
||||
runCommand("mv /tmp/tmp.ppm " + Location + PictureDirectory + WorldPicture + ".ppm");
|
||||
print("converting to png if possible.\n");
|
||||
runCommand("pnmtopng " + Location + PictureDirectory + WorldPicture + ".ppm > /tmp/tmp.png");
|
||||
runCommand("mv /tmp/tmp.png " + Location + PictureDirectory + WorldPicture + ".png");
|
||||
}
|
||||
|
||||
checkDaList();
|
||||
print("Done!");]]></code>
|
||||
<mode>
|
||||
<autoboot>false</autoboot>
|
||||
|
|
Loading…
Reference in New Issue