File: MapValidator

package info (click to toggle)
crossfire-maps 1.75.0%2Bdfsg1-1
  • links: PTS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 275,656 kB
  • sloc: python: 7,711; sql: 92; sh: 73; makefile: 7
file content (159 lines) | stat: -rw-r--r-- 4,739 bytes parent folder | download | duplicates (3)
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
<?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.Iterator;
import net.sf.gridarta.model.gameobject.GameObject;
import net.sf.gridarta.model.io.RecursiveFileIterator;
import net.sf.gridarta.model.validation.ErrorCollector;
import net.sf.gridarta.model.validation.errors.ValidationError;

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

void checkMap(File mapFile, String mapPath) {
    try {
        map = mapManager.openMapFile(mapFile, false);
    } catch (IOException ex) {
        print("Cannot load map '"+mapFile+"': "+ex.getMessage());
        return;
    }
    if (map == null) {
        log(mapPath + ":");
        log("- cannot load map file");
        return;
    }

    ErrorCollector errorCollector;
    try {
        try {
            validators.validateAll(map.getMapModel());
        } finally {
            errorCollector = map.getMapModel().getErrors();
        }
    } finally {
        mapManager.release(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);

        Iterator it2 = validationError.getGameObjects().iterator();
        while (it2.hasNext()) {
            GameObject gameObject = it2.next();
            sb.append(" [").append(gameObject.getBestName()).append(']');
        }

        String parameter0 = validationError.getParameter(0);
        if (parameter0 != null) {
            sb.append(" [").append(parameter0);
            String parameter1 = validationError.getParameter(1);
            if (parameter1 != null) {
                sb.append(", ").append(parameter1);
            }
            sb.append(']');
        }

        log(sb.toString());
    }
}

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);
    }

    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()
        && path.startsWith(rootDirectory)
        && !name.equalsIgnoreCase("README")
        && !name.endsWith(".animation")
        && !name.endsWith(".msg")
        && !name.endsWith(".png")
        && !name.endsWith(".ppm")
        && !name.endsWith(".py")
        && !name.endsWith(".pyc")
        && !name.endsWith(".quests")
        && !name.endsWith(".txt")
        && !name.endsWith(".zip")
        && !name.equals("pshop_copier")
        && !name.equals("pshops_changelog")
        && !name.equals(".emergency")
        && !name.equals("ChangeLog")
        && !name.equals("COPYING")
        && !name.equals("TODO")
        && !path.contains("/Info/")
        && !path.contains("/editor/scripts/")) {
            checkMap(file, file.getPath().substring(mapDefaultFolder.length()));
        }
    }

    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>MapPathParameter</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>