Gridarta Editor
TestMapArchObjectParser.java
Go to the documentation of this file.
1 /*
2  * Gridarta MMORPG map editor for Crossfire, Daimonin and similar games.
3  * Copyright (C) 2000-2023 The Gridarta Developers.
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program; if not, write to the Free Software Foundation, Inc.,
17  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18  */
19 
20 package net.sf.gridarta.model.io;
21 
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;
31 import net.sf.gridarta.utils.Size2D;
33 import org.jetbrains.annotations.NotNull;
34 import org.jetbrains.annotations.Nullable;
35 
40 public class TestMapArchObjectParser extends AbstractMapArchObjectParser<TestMapArchObject> {
41 
46  @NotNull
47  private static final Pattern TILE_PATH_PATTERN = Pattern.compile("(\\d+) (.+)");
48 
52  private int width;
53 
57  private int height;
58 
59  @Override
60  public void load(@NotNull final BufferedReader reader, @NotNull final TestMapArchObject mapArchObject) throws IOException {
61  width = 0;
62  height = 0;
63  super.load(reader, mapArchObject);
64  mapArchObject.setMapSize(new Size2D(Math.max(1, width), Math.max(1, height)));
65  }
66 
67  @Override
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")) {
70  while (true) {
71  final String msgLine = reader.readLine();
72  if (msgLine == null) {
73  throw new IOException("unexpected end of file in msg...endmsg field");
74  }
75 
76  final String trimmedMsgLine = StringUtils.removeTrailingWhitespace(msgLine);
77 
78  if (trimmedMsgLine.equals("endmsg")) {
79  break;
80  }
81 
82  if (!mapArchObject.getText().isEmpty()) {
83  mapArchObject.addText("\n");
84  }
85  mapArchObject.addText(trimmedMsgLine);
86  }
87  } else if (line.startsWith("tile_path_")) {
88  final Matcher m = TILE_PATH_PATTERN.matcher(line.substring(10));
89  if (!m.matches()) {
90  throw new InvalidMapFormatException("unexpected map attribute: '" + line + "'");
91  }
92 
93  // get tile path
94  try {
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));
98  } else {
99  throw new InvalidMapFormatException("unexpected map attribute: '" + line + "'");
100  }
101  } catch (final NumberFormatException ex) {
102  throw new InvalidMapFormatException("unexpected map attribute: '" + line + "'", ex);
103  }
104  } else if (line.equals("end")) {
105  return false;
106  } else {
107  switch (key) {
108  case "name":
109  mapArchObject.setMapName(value);
110  break;
111 
112  case "width":
113  width = NumberUtils.parseInt(value);
114  break;
115 
116  case "height":
117  height = NumberUtils.parseInt(value);
118  break;
119 
120  case "enter_x":
121  mapArchObject.setEnterX(NumberUtils.parseInt(value));
122  break;
123 
124  case "enter_y":
125  mapArchObject.setEnterY(NumberUtils.parseInt(value));
126  break;
127 
128  case "reset_timeout":
129  mapArchObject.setResetTimeout(NumberUtils.parseInt(value));
130  break;
131 
132  case "swap_time":
133  mapArchObject.setSwapTime(NumberUtils.parseInt(value));
134  break;
135 
136  case "difficulty":
137  mapArchObject.setDifficulty(NumberUtils.parseInt(value));
138  break;
139 
140  case "darkness":
141  mapArchObject.setDarkness(NumberUtils.parseInt(value));
142  break;
143 
144  case "fixed_resettime":
145  mapArchObject.setFixedReset(NumberUtils.parseInt(value) != 0);
146  break;
147 
148  case "outdoor":
149  mapArchObject.setOutdoor(NumberUtils.parseInt(value) != 0);
150  break;
151 
152  default:
153  throw new InvalidMapFormatException("unexpected map attribute: '" + line + "'");
154  }
155  }
156 
157  return true;
158  }
159 
160  @Override
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());
166  }
167  if (mapArchObject.getSwapTime() != 0) {
168  format.format("swap_time %d\n", mapArchObject.getSwapTime());
169  }
170  if (mapArchObject.getResetTimeout() != 0) {
171  format.format("reset_timeout %d\n", mapArchObject.getResetTimeout());
172  }
173  if (mapArchObject.isFixedReset()) {
174  appendable.append("fixed_resettime 1\n");
175  }
176  if (mapArchObject.getDifficulty() != 0) {
177  format.format("difficulty %d\n", mapArchObject.getDifficulty());
178  }
179  if (mapArchObject.getDarkness() != 0) {
180  format.format("darkness %d\n", mapArchObject.getDarkness());
181  }
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());
186  }
187  if (mapArchObject.getEnterY() != 0) {
188  format.format("enter_y %d\n", mapArchObject.getEnterY());
189  }
190  if (!mapArchObject.getText().trim().isEmpty()) {
191  format.format("msg\n" + "%s\n" + "endmsg\n", mapArchObject.getText().trim());
192  }
193  if (mapArchObject.isOutdoor()) {
194  appendable.append("outdoor 1\n");
195  }
196  for (final Direction direction : Direction.values()) {
197  if (!mapArchObject.getTilePath(direction).isEmpty()) {
198  format.format("tile_path_%d %s\n", direction.ordinal() + 1, mapArchObject.getTilePath(direction));
199  }
200  }
201  appendable.append("end\n");
202  }
203 
204 }
net.sf.gridarta.model.direction.Direction
A direction.
Definition: Direction.java:28
net.sf.gridarta.utils.NumberUtils.parseInt
static int parseInt(@NotNull final String s)
Parses an integer string.
Definition: NumberUtils.java:41
net.sf.gridarta
Base package of all Gridarta classes.
net.sf.gridarta.model.io.TestMapArchObjectParser.TILE_PATH_PATTERN
static final Pattern TILE_PATH_PATTERN
Matches the remainder of a "tile_path_xyz" line.
Definition: TestMapArchObjectParser.java:47
net.sf
net.sf.gridarta.model.io.TestMapArchObjectParser
A MapArchObjectParser for regression tests.
Definition: TestMapArchObjectParser.java:40
net.sf.gridarta.model.mapmodel
Definition: AboveFloorInsertionMode.java:20
net.sf.gridarta.utils.StringUtils.removeTrailingWhitespace
static String removeTrailingWhitespace(@NotNull final CharSequence str)
Removes trailing whitespace from a string.
Definition: StringUtils.java:123
net
net.sf.gridarta.model.io.AbstractMapArchObjectParser
Abstract base class for classes implementing MapArchObjectParsers.
Definition: AbstractMapArchObjectParser.java:39
net.sf.gridarta.model.io.TestMapArchObjectParser.width
int width
The width of the map.
Definition: TestMapArchObjectParser.java:52
net.sf.gridarta.model.io.InvalidMapFormatException
Exception that's thrown when a MapReader read a file and detected that it's in the wrong format.
Definition: InvalidMapFormatException.java:31
net.sf.gridarta.utils.StringUtils
Utility class for string manipulation.
Definition: StringUtils.java:31
net.sf.gridarta.model.mapmodel.MapFile
The location of a map file with a map directory.
Definition: MapFile.java:31
net.sf.gridarta.model.io.TestMapArchObjectParser.parseLine
boolean parseLine(@NotNull final String line, @NotNull final String key, @NotNull final String value, @NotNull final TestMapArchObject mapArchObject, @NotNull final BufferedReader reader)
Definition: TestMapArchObjectParser.java:68
net.sf.gridarta.model
net.sf.gridarta.model.io.TestMapArchObjectParser.load
void load(@NotNull final BufferedReader reader, @NotNull final TestMapArchObject mapArchObject)
Definition: TestMapArchObjectParser.java:60
net.sf.gridarta.model.maparchobject.TestMapArchObject
A MapArchObject implementation for testing purposes.
Definition: TestMapArchObject.java:28
net.sf.gridarta.model.maparchobject
Definition: AbstractMapArchObject.java:20
net.sf.gridarta.model.io.TestMapArchObjectParser.save
void save(@NotNull final Appendable appendable, @NotNull final TestMapArchObject mapArchObject, @Nullable final MapFile mapFile)
Definition: TestMapArchObjectParser.java:161
net.sf.gridarta.utils.Size2D
The class Size2D represents a 2d rectangular area.
Definition: Size2D.java:30
net.sf.gridarta.utils
Definition: ActionBuilderUtils.java:20
net.sf.gridarta.model.direction
Definition: Direction.java:20
net.sf.gridarta.model.io.TestMapArchObjectParser.height
int height
The height of the map.
Definition: TestMapArchObjectParser.java:57
net.sf.gridarta.utils.NumberUtils
Utility class for parsing strings into numbers.
Definition: NumberUtils.java:28