20 package net.sf.gridarta.model.io;
22 import java.io.BufferedReader;
23 import java.io.IOException;
24 import java.util.Formatter;
25 import java.util.regex.Matcher;
26 import java.util.regex.Pattern;
33 import org.jetbrains.annotations.NotNull;
34 import org.jetbrains.annotations.Nullable;
60 public void load(@NotNull
final BufferedReader reader, @NotNull
final TestMapArchObject mapArchObject)
throws IOException {
63 super.load(reader, mapArchObject);
68 protected boolean parseLine(@NotNull
final String line, @NotNull
final String key, @NotNull
final String value, @NotNull
final TestMapArchObject mapArchObject, @NotNull
final BufferedReader reader)
throws IOException {
69 if (line.equals(
"msg")) {
71 final String msgLine = reader.readLine();
72 if (msgLine ==
null) {
73 throw new IOException(
"unexpected end of file in msg...endmsg field");
78 if (trimmedMsgLine.equals(
"endmsg")) {
82 if (!mapArchObject.getText().isEmpty()) {
83 mapArchObject.addText(
"\n");
85 mapArchObject.addText(trimmedMsgLine);
87 }
else if (line.startsWith(
"tile_path_")) {
95 final int index = Integer.parseInt(m.group(1));
96 if (index > 0 && index <=
Direction.values().length) {
97 mapArchObject.setTilePath(
Direction.values()[index - 1], m.group(2));
101 }
catch (
final NumberFormatException ex) {
104 }
else if (line.equals(
"end")) {
109 mapArchObject.setMapName(value);
128 case "reset_timeout":
144 case "fixed_resettime":
161 public void save(@NotNull
final Appendable appendable, @NotNull
final TestMapArchObject mapArchObject, @Nullable
final MapFile mapFile)
throws IOException {
162 final Formatter format =
new Formatter(appendable);
163 appendable.append(
"arch map\n");
164 if (!mapArchObject.getMapName().isEmpty()) {
165 format.format(
"name %s\n", mapArchObject.getMapName());
167 if (mapArchObject.getSwapTime() != 0) {
168 format.format(
"swap_time %d\n", mapArchObject.getSwapTime());
170 if (mapArchObject.getResetTimeout() != 0) {
171 format.format(
"reset_timeout %d\n", mapArchObject.getResetTimeout());
173 if (mapArchObject.isFixedReset()) {
174 appendable.append(
"fixed_resettime 1\n");
176 if (mapArchObject.getDifficulty() != 0) {
177 format.format(
"difficulty %d\n", mapArchObject.getDifficulty());
179 if (mapArchObject.getDarkness() != 0) {
180 format.format(
"darkness %d\n", mapArchObject.getDarkness());
182 format.format(
"width %d\n", mapArchObject.getMapSize().getWidth());
183 format.format(
"height %d\n", mapArchObject.getMapSize().getHeight());
184 if (mapArchObject.getEnterX() != 0) {
185 format.format(
"enter_x %d\n", mapArchObject.getEnterX());
187 if (mapArchObject.getEnterY() != 0) {
188 format.format(
"enter_y %d\n", mapArchObject.getEnterY());
190 if (!mapArchObject.getText().trim().isEmpty()) {
191 format.format(
"msg\n" +
"%s\n" +
"endmsg\n", mapArchObject.getText().trim());
193 if (mapArchObject.isOutdoor()) {
194 appendable.append(
"outdoor 1\n");
197 if (!mapArchObject.getTilePath(direction).isEmpty()) {
198 format.format(
"tile_path_%d %s\n", direction.ordinal() + 1, mapArchObject.getTilePath(direction));
201 appendable.append(
"end\n");