20 package net.sf.gridarta.model.io;
22 import java.io.BufferedReader;
23 import java.io.IOException;
24 import java.util.regex.Matcher;
25 import java.util.regex.Pattern;
31 import org.jetbrains.annotations.NotNull;
50 public void load(@NotNull
final BufferedReader reader, @NotNull
final A mapArchObject)
throws IOException {
51 final String firstLine = reader.readLine();
52 if (firstLine == null) {
53 throw new IOException(
"unexpected end of file in map header");
58 if (!trimmedFirstLine.equals(
"arch map")) {
59 throw new InvalidMapFormatException(
"unexpected first line of map file: '" + trimmedFirstLine +
"', expected 'arch map'");
65 final String line = reader.readLine();
67 throw new IOException(
"unexpected end of file in map header");
72 if (
parseLine(trimmedLine, mapArchObject, reader)) {
74 }
else if (trimmedLine.equals(
"msg")) {
76 final String msgLine = reader.readLine();
77 if (msgLine == null) {
78 throw new IOException(
"unexpected end of file in msg...endmsg field");
83 if (trimmedMsgLine.equals(
"endmsg")) {
87 if (!mapArchObject.getText().isEmpty()) {
88 mapArchObject.addText(
"\n");
90 mapArchObject.addText(trimmedMsgLine);
92 }
else if (trimmedLine.equals(
"end")) {
94 }
else if (trimmedLine.startsWith(
"name ")) {
95 mapArchObject.setMapName(trimmedLine.substring(5));
96 }
else if (trimmedLine.startsWith(
"width ")) {
98 }
else if (trimmedLine.startsWith(
"height ")) {
100 }
else if (trimmedLine.startsWith(
"enter_x ")) {
102 }
else if (trimmedLine.startsWith(
"enter_y ")) {
104 }
else if (trimmedLine.startsWith(
"reset_timeout ")) {
106 }
else if (trimmedLine.startsWith(
"swap_time ")) {
108 }
else if (trimmedLine.startsWith(
"difficulty ")) {
110 }
else if (trimmedLine.startsWith(
"darkness ")) {
112 }
else if (trimmedLine.startsWith(
"fixed_resettime ")) {
114 mapArchObject.setFixedReset(
true);
116 }
else if (trimmedLine.startsWith(
"outdoor ")) {
118 mapArchObject.setOutdoor(
true);
120 }
else if (trimmedLine.startsWith(
"tile_path_")) {
121 final Matcher m = TILE_PATH_PATTERN.matcher(trimmedLine.substring(10));
128 final int index = Integer.valueOf(m.group(1));
129 if (index > 0 && index <=
Direction.values().length) {
130 mapArchObject.setTilePath(
Direction.values()[index - 1], m.group(2));
134 }
catch (
final NumberFormatException ignored) {
143 mapArchObject.setMapSize(
new Size2D(Math.max(1, width), Math.max(1, height)));
154 protected abstract boolean parseLine(@NotNull String line, @NotNull A mapArchObject, @NotNull BufferedReader reader)
throws IOException;
Utility class for string manipulation.
abstract boolean parseLine(@NotNull String line, @NotNull A mapArchObject, @NotNull BufferedReader reader)
Parse a line for this editor type.
Abstract base class for classes implementing MapArchObjectParsers.
static final Pattern TILE_PATH_PATTERN
Matches the remainder of a "tile_path_xyz" line.
static String removeTrailingWhitespace(@NotNull final CharSequence str)
Removes trailing whitespace from a string.
Utility class for parsing strings into numbers.
Base package of all Gridarta classes.
static int parseInt(@NotNull final String s)
Parses an integer string.
Interface for classes that read or write MapArchObject instances.
void load(@NotNull final BufferedReader reader, @NotNull final A mapArchObject)
Interface for MapArchObjects.
The class Size2D represents a 2d rectangular area.