<?xml version="1.0" encoding="UTF-8"?>
<script>
  <name>MapValidator</name>
  <code><![CDATA[import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.util.Arrays;
import java.util.Iterator;
import net.sf.gridarta.gameobject.GameObject;
import net.sf.gridarta.map.validation.ErrorCollector;
import net.sf.gridarta.map.validation.ValidationError;

void log(String message) {
    print(message);
    if (logFile != null) {
        logFile.write(message);
        logFile.write('\n');
    }
}

void checkMap(File mapFile, String mapPath) {
    map = mainControl.getMapManager().openMapFile(mapFile, false);
    if (map == null) {
        log(mapPath + ":");
        log("- cannot load map file");
        return;
    }
    ErrorCollector errorCollector;
    try {
        errorCollector = mainControl.runMapValidation(map.getMapModel());
    } finally {
        if (map.nViews() <= 0) {
            mainControl.getMapManager().closeLevel(map);
        }
    }

    StringBuffer sb = new StringBuffer();
    int numberOfErrors = 0;
    Iterator it = errorCollector.iterator();
    while (it.hasNext()) {
        ValidationError validationError = it.next();

        if (errorLimit > 0 && numberOfErrors >= errorLimit) {
            log("- <skipping more errors>");
            break;
        }

        if (numberOfErrors == 0) {
            log(mapPath + ":");
        }
        numberOfErrors++;

        sb.setLength(0);
        sb.append("- ");

        sb.append(validationError);

        GameObject gameObject = validationError.getGameObject();
        if (gameObject != null) {
            sb.append(" [").append(gameObject.getBestName()).append(']');
        }

        String parameter = validationError.getParameter();
        if (parameter != null) {
            sb.append(" [").append(parameter).append(']');
        }

        log(sb.toString());
    }
}

void checkDirectory(File mapFile, String mapPath) {
    File[] files = mapFile.listFiles();
    if (files == null) {
        log("Cannot read directory " + mapFile);
        return;
    }
    Arrays.sort(files);
    for (File file : files) {
        String name = file.getName();
        if (!name.equals(".svn") && !name.equals("README")) {
            checkMaps(file, mapPath + "/" + file.getName());
        }
    }
}

void checkMaps(File mapFile, String mapPath) {
    if (mapFile.isDirectory()) {
        checkDirectory(mapFile, mapPath);
    } else if (mapFile.isFile()) {
        checkMap(mapFile, mapPath);
    }
}

Writer logFile = logFilename.length() <= 0 ? null : new BufferedWriter(new FileWriter(logFilename));
try {
    if (baseDirectory == null || baseDirectory.length() <= 0) {
        baseDirectory = "/";
    }
    log("Checking maps below " + baseDirectory + "...");
    if (baseDirectory.endsWith("/")) {
        baseDirectory = baseDirectory.substring(0, baseDirectory.length() - 1);
    }

    checkMaps(new File(mainControl.getMapDefaultFolder() + baseDirectory), baseDirectory);

    log("Done.");
} finally {
    if (logFile != null) {
        logFile.close();
    }
}]]></code>
  <mode>
    <autoboot>false</autoboot>
    <bash>true</bash>
    <filter>false</filter>
  </mode>
  <parameter>
    <name>baseDirectory</name>
    <description>Base Directory</description>
    <type>java.lang.String</type>
    <value>/</value>
  </parameter>
  <parameter>
    <name>errorLimit</name>
    <description>Maximum number of errors to show for each map; 0=show all errors</description>
    <type>java.lang.Integer</type>
    <value>20</value>
    <minimum>0</minimum>
    <maximum>2147483647</maximum>
  </parameter>
  <parameter>
    <name>logFilename</name>
    <description>Copy errors to this file; empty=no copy to file</description>
    <type>java.lang.String</type>
    <value />
  </parameter>
</script>

