Gridarta Editor
AutojoinListsParser.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.autojoin;
21 
22 import java.io.BufferedReader;
23 import java.io.File;
24 import java.io.IOException;
25 import java.io.InputStream;
26 import java.io.InputStreamReader;
27 import java.io.Reader;
28 import java.net.URL;
29 import java.util.ArrayList;
30 import java.util.List;
38 import net.sf.gridarta.utils.IOUtils;
40 import org.jetbrains.annotations.NotNull;
41 
46 public class AutojoinListsParser {
47 
51  private static final String FILENAME = "autojoin.txt";
52 
56  private AutojoinListsParser() {
57  }
58 
66  @NotNull
67  public static <G extends GameObject<G, A, R>, A extends MapArchObject<A>, R extends Archetype<G, A, R>> AutojoinLists<G, A, R> loadList(@NotNull final ErrorView errorView, final ArchetypeSet<G, A, R> archetypeSet, @NotNull final File baseDir) {
68  try {
69  final URL url = IOUtils.getResource(baseDir, FILENAME);
70  try {
71  try (InputStream inputStream = url.openStream()) {
72  try (Reader reader = new InputStreamReader(inputStream, IOUtils.MAP_ENCODING)) {
73  try (BufferedReader stream = new BufferedReader(reader)) {
74  return loadList(errorView, archetypeSet, url, stream);
75  }
76  }
77  }
78  } catch (final IOException ex) {
79  errorView.addWarning(ErrorViewCategory.AUTOJOIN_FILE_INVALID, url + ": " + ex.getMessage());
80  }
81  } catch (final IOException ex) {
82  errorView.addWarning(ErrorViewCategory.AUTOJOIN_FILE_INVALID, FILENAME + ": " + ex.getMessage());
83  }
84 
85  return new AutojoinLists<>();
86  }
87 
97  @NotNull
98  private static <G extends GameObject<G, A, R>, A extends MapArchObject<A>, R extends Archetype<G, A, R>> AutojoinLists<G, A, R> loadList(@NotNull final ErrorView errorView, final ArchetypeSet<G, A, R> archetypeSet, @NotNull final URL url, @NotNull final BufferedReader bufferedReader) throws IOException {
99  final AutojoinLists<G, A, R> autojoinLists = new AutojoinLists<>();
100  boolean sectionFlag = false; // true while section (start ... end) read; XXX: replace flag with control flow
101  final List<List<R>> archetypes = new ArrayList<>(AutojoinList.SIZE);
102  final StringBuilder undefinedArchetypes = new StringBuilder();
103  boolean skipList = true;
104  while (true) {
105  final String line2 = bufferedReader.readLine();
106  if (line2 == null) {
107  break;
108  }
109  if (!line2.startsWith("#") && !line2.isEmpty()) {
110  final String line = line2.trim(); // remove whitespace at both ends
111 
112  if (sectionFlag) {
113  // we are inside a section
114  if (line.equals("end")) {
115  if (undefinedArchetypes.length() > 0) {
116  errorView.addWarning(ErrorViewCategory.AUTOJOIN_ENTRY_INVALID, url + ": Autojoin list references undefined archetypes:" + undefinedArchetypes);
117  }
118  if (!skipList) {
119  try {
120  final AutojoinList<G, A, R> autojoinList = new AutojoinList<>(archetypes);
121  autojoinLists.addAutojoinList(autojoinList);
122  } catch (final IllegalAutojoinListException ex) {
123  errorView.addWarning(ErrorViewCategory.AUTOJOIN_ENTRY_INVALID, url + ": " + ex.getMessage());
124  }
125  }
126  sectionFlag = false;
127  } else {
128  final List<R> tmp = new ArrayList<>();
129  for (final String archetypeName : StringUtils.PATTERN_WHITESPACE.split(line)) {
130  try {
131  final R archetype = archetypeSet.getArchetype(archetypeName);
132  if (archetype.isMulti()) {
133  errorView.addWarning(ErrorViewCategory.AUTOJOIN_ENTRY_INVALID, url + ": list contains multi-part game object: archetype '" + line + "'");
134  skipList = true;
135  } else {
136  tmp.add(archetype);
137  }
138  } catch (final UndefinedArchetypeException ex) {
139  undefinedArchetypes.append(' ').append(ex.getMessage());
140  skipList = true;
141  }
142  }
143  archetypes.add(tmp);
144  }
145  } else {
146  // we are outside a section
147  if (line.equals("start")) {
148  sectionFlag = true;
149  archetypes.clear();
150  skipList = false;
151  undefinedArchetypes.setLength(0);
152  }
153  }
154  }
155  }
156  return autojoinLists;
157  }
158 
159 }
net.sf.gridarta.utils.IOUtils.getResource
static URL getResource(@NotNull final File dir, @NotNull final String fileName)
Get the URL of a resource.
Definition: IOUtils.java:69
net.sf.gridarta.model.autojoin.AutojoinListsParser.AutojoinListsParser
AutojoinListsParser()
Creates a new instance.
Definition: AutojoinListsParser.java:56
net.sf.gridarta.model.autojoin.AutojoinListsParser
Loader for AutojoinLists instances from files.
Definition: AutojoinListsParser.java:46
net.sf.gridarta.model.autojoin.AutojoinListsParser.loadList
static< G extends GameObject< G, A, R > A extends R extends Archetype< G, A, R > AutojoinLists< G, A, R > loadList(@NotNull final ErrorView errorView, final ArchetypeSet< G, A, R > archetypeSet, @NotNull final URL url, @NotNull final BufferedReader bufferedReader)
Definition: AutojoinListsParser.java:98
net.sf.gridarta.model.errorview.ErrorViewCategory.AUTOJOIN_FILE_INVALID
AUTOJOIN_FILE_INVALID
Definition: ErrorViewCategory.java:60
net.sf.gridarta
Base package of all Gridarta classes.
net.sf.gridarta.model.archetype.UndefinedArchetypeException
Exception thrown if an Archetype does not exist.
Definition: UndefinedArchetypeException.java:28
net.sf
net.sf.gridarta.model.autojoin.AutojoinList.SIZE
static final int SIZE
The number of archetypes in an autojoin list.
Definition: AutojoinList.java:55
net.sf.gridarta.model.errorview.ErrorView
Interface for classes displaying error messages.
Definition: ErrorView.java:28
net.sf.gridarta.model.archetype
Definition: AbstractArchetype.java:20
net.sf.gridarta.model.gameobject.GameObject
Reflects a game object (object on a map).
Definition: GameObject.java:36
net.sf.gridarta.model.autojoin.AutojoinListsParser.FILENAME
static final String FILENAME
The file defining autojoin lists.
Definition: AutojoinListsParser.java:51
net.sf.gridarta.utils.StringUtils.PATTERN_WHITESPACE
static final Pattern PATTERN_WHITESPACE
Pattern to match whitespace excluding NL and CR.
Definition: StringUtils.java:37
net.sf.gridarta.model.gameobject
GameObjects are the objects based on Archetypes found on maps.
Definition: AbstractGameObject.java:20
net
net.sf.gridarta.model.errorview
Definition: ErrorView.java:20
net.sf.gridarta.model.errorview.ErrorViewCategory
Defines possible error categories for ErrorView instances.
Definition: ErrorViewCategory.java:28
net.sf.gridarta.model.maparchobject.MapArchObject
Interface for MapArchObjects.
Definition: MapArchObject.java:40
net.sf.gridarta.model.archetypeset.ArchetypeSet
Interface that captures similarities between different ArchetypeSet implementations.
Definition: ArchetypeSet.java:37
net.sf.gridarta.model.autojoin.AutojoinList
Contains a list of (typically wall-)arches which do autojoining.
Definition: AutojoinList.java:39
net.sf.gridarta.model.autojoin.IllegalAutojoinListException
Exception thrown if an AutojoinList is invalid.
Definition: IllegalAutojoinListException.java:28
net.sf.gridarta.utils.StringUtils
Utility class for string manipulation.
Definition: StringUtils.java:31
net.sf.gridarta.model.autojoin.AutojoinLists< G, A, R >
net.sf.gridarta.model
net.sf.gridarta.model.archetype.Archetype
Reflects an Archetype.
Definition: Archetype.java:41
net.sf.gridarta.model.autojoin.AutojoinLists.addAutojoinList
void addAutojoinList(@NotNull final AutojoinList< G, A, R > autojoinList)
Adds a new autojoin list.
Definition: AutojoinLists.java:53
net.sf.gridarta.model.autojoin.AutojoinListsParser.loadList
static< G extends GameObject< G, A, R > A extends R extends Archetype< G, A, R > AutojoinLists< G, A, R > loadList(@NotNull final ErrorView errorView, final ArchetypeSet< G, A, R > archetypeSet, @NotNull final File baseDir)
Definition: AutojoinListsParser.java:67
net.sf.gridarta.utils.IOUtils
Utility-class for Gridarta's I/O.
Definition: IOUtils.java:40
net.sf.gridarta.model.maparchobject
Definition: AbstractMapArchObject.java:20
net.sf.gridarta.model.archetypeset
Definition: ArchetypeSet.java:20
net.sf.gridarta.model.errorview.ErrorViewCategory.AUTOJOIN_ENTRY_INVALID
AUTOJOIN_ENTRY_INVALID
Definition: ErrorViewCategory.java:58
net.sf.gridarta.utils
Definition: ActionBuilderUtils.java:20
net.sf.gridarta.utils.IOUtils.MAP_ENCODING
static final String MAP_ENCODING
Encoding to use for maps and other data.
Definition: IOUtils.java:52