Gridarta Editor
AbstractAnimationObjectsReader.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.File;
24 import java.io.FileInputStream;
25 import java.io.IOException;
26 import java.io.InputStreamReader;
27 import java.io.Reader;
28 import java.util.Map;
35 import net.sf.gridarta.utils.IOUtils;
36 import org.jetbrains.annotations.NotNull;
37 import org.jetbrains.annotations.Nullable;
38 
44 public abstract class AbstractAnimationObjectsReader {
45 
50  }
51 
66  public void loadAnimations(@NotNull final AnimationObjects animationObjects, @NotNull final ErrorViewCollector errorViewCollector, @NotNull final String path, @NotNull final File animFile, @NotNull final String startKey) throws IOException, AnimationParseException {
67  try (FileInputStream fileInputStream = new FileInputStream(animFile)) {
68  try (Reader inputStreamReader = new InputStreamReader(fileInputStream, IOUtils.MAP_ENCODING)) {
69  loadAnimations(animationObjects, errorViewCollector, inputStreamReader, startKey, path, null);
70  }
71  }
72  }
73 
93  public void loadAnimations(@NotNull final AnimationObjects animationObjects, @NotNull final ErrorViewCollector errorViewCollector, final Reader reader, @NotNull final String startKey, @Nullable final String path, @Nullable final Map<String, String> animations) throws AnimationParseException, IOException {
94  if (path == null && animations == null) {
95  throw new IllegalArgumentException("neither path nor animations is set");
96  }
97  if (path != null && animations != null) {
98  throw new IllegalArgumentException("both path and animations are set");
99  }
100  try (BufferedReader in = new BufferedReader(reader)) {
101  int lineNumber = 1;
102  while (true) {
103  final String line2 = in.readLine();
104  if (line2 == null) {
105  break;
106  }
107  final String line = line2.trim();
108  if (line.startsWith("#") || line.isEmpty()) {
109  /* ignore comment lines. */
110  } else if (line.startsWith(startKey)) {
111  lineNumber += processAnimation(line.substring(startKey.length()), lineNumber, in, startKey, animationObjects, errorViewCollector, path, animations);
112  } else {
113  final int lineNumbers = parseLine(line, lineNumber, in, errorViewCollector);
114  if (lineNumbers >= 0) {
115  lineNumber += lineNumbers;
116  } else {
117  throw new AnimationParseException(startKey + "...", line2, lineNumber);
118  }
119  }
120 
121  lineNumber++;
122  }
123  }
124  }
125 
138  protected abstract int parseLine(@NotNull String line, int startLineNumber, @NotNull BufferedReader in, @NotNull ErrorViewCollector errorViewCollector) throws AnimationParseException, IOException;
139 
158  private static int processAnimation(@NotNull final String animName, final int startLineNumber, @NotNull final BufferedReader in, @NotNull final String startKey, @NotNull final AnimationObjects animationObjects, @NotNull final ErrorViewCollector errorViewCollector, @Nullable final String path, @Nullable final Map<String, String> animations) throws AnimationParseException, IOException {
159  int lineNumber = startLineNumber;
160  final StringBuilder animText = new StringBuilder();
161  while (true) {
162  lineNumber++;
163  final String line3 = in.readLine();
164  if (line3 == null) {
165  throw new AnimationParseException("mina", "<end of file>", lineNumber);
166  }
167  final String line4 = line3.trim();
168  if (line4.startsWith("#") || line4.isEmpty()) {
169  /* ignore comment lines. */
170  } else if (line4.startsWith(startKey)) {
171  throw new AnimationParseException("mina", line4, lineNumber);
172  } else if (line4.equals("mina")) {
173  processAnimationLine(animationObjects, errorViewCollector, path, animations, animName, animText.toString());
174  break;
175  } else {
176  animText.append(line4).append('\n');
177  }
178  }
179  return lineNumber - startLineNumber;
180  }
181 
194  private static void processAnimationLine(@NotNull final AnimationObjects animationObjects, @NotNull final ErrorViewCollector errorViewCollector, @Nullable final String path, @Nullable final Map<String, String> animations, @NotNull final String animName, @NotNull final String animText) {
195  if (path == null && animations == null) {
196  throw new IllegalArgumentException("neither path not animations is set");
197  }
198 
199  final String animPath;
200  if (path == null) {
201  final String tmp = animations.get(animName);
202  if (tmp == null) {
203  errorViewCollector.addWarning(ErrorViewCategory.ANIMATIONS_ENTRY_INVALID, "no path found for animation: " + animName);
204  animPath = "/" + animName;
205  } else {
206  animPath = tmp;
207  }
208  } else {
209  animPath = path;
210  }
211  try {
212  animationObjects.addAnimationObject(animName, animText, animPath);
213  } catch (final DuplicateAnimationException ex) {
214  errorViewCollector.addWarning(ErrorViewCategory.ANIMATIONS_ENTRY_INVALID, ex.getMessage());
215  } catch (final IllegalAnimationException ex) {
216  errorViewCollector.addWarning(ErrorViewCategory.ANIMATIONS_ENTRY_INVALID, "illegal animation: " + ex.getAnimationObject().getPath());
217  }
218  }
219 
220 }
net.sf.gridarta
Base package of all Gridarta classes.
net.sf.gridarta.model.io.AbstractAnimationObjectsReader.loadAnimations
void loadAnimations(@NotNull final AnimationObjects animationObjects, @NotNull final ErrorViewCollector errorViewCollector, final Reader reader, @NotNull final String startKey, @Nullable final String path, @Nullable final Map< String, String > animations)
Loads any number of animations from a reader.
Definition: AbstractAnimationObjectsReader.java:93
net.sf.gridarta.model.anim.AnimationObjects
AnimationObjects is a container for AnimationObjects.
Definition: AnimationObjects.java:30
net.sf.gridarta.model.anim.DuplicateAnimationException
Exception that's thrown in case an animation name was not unique.
Definition: DuplicateAnimationException.java:27
net.sf
net.sf.gridarta.model.io.AbstractAnimationObjectsReader
Utility class for reading AnimationObjects from files.
Definition: AbstractAnimationObjectsReader.java:44
net.sf.gridarta.model.io.AbstractAnimationObjectsReader.processAnimationLine
static void processAnimationLine(@NotNull final AnimationObjects animationObjects, @NotNull final ErrorViewCollector errorViewCollector, @Nullable final String path, @Nullable final Map< String, String > animations, @NotNull final String animName, @NotNull final String animText)
Processes a complete anim..mina block.
Definition: AbstractAnimationObjectsReader.java:194
net.sf.gridarta.model.anim.IllegalAnimationException
Exception thrown to indicate that an animation object is not acceptable.
Definition: IllegalAnimationException.java:29
net
net.sf.gridarta.model.errorview
Definition: ErrorView.java:20
net.sf.gridarta.model.errorview.ErrorViewCollector
Convenience class for adding messages to a ErrorView instance using a fixed category name.
Definition: ErrorViewCollector.java:31
net.sf.gridarta.model.anim.AnimationParseException
This exception is thrown when parsing an animation definition file (.
Definition: AnimationParseException.java:31
net.sf.gridarta.model.errorview.ErrorViewCategory
Defines possible error categories for ErrorView instances.
Definition: ErrorViewCategory.java:28
net.sf.gridarta.model.io.AbstractAnimationObjectsReader.AbstractAnimationObjectsReader
AbstractAnimationObjectsReader()
Creates a new instance.
Definition: AbstractAnimationObjectsReader.java:49
net.sf.gridarta.model.errorview.ErrorViewCategory.ANIMATIONS_ENTRY_INVALID
ANIMATIONS_ENTRY_INVALID
Definition: ErrorViewCategory.java:36
net.sf.gridarta.model.io.AbstractAnimationObjectsReader.parseLine
abstract int parseLine(@NotNull String line, int startLineNumber, @NotNull BufferedReader in, @NotNull ErrorViewCollector errorViewCollector)
Parses a line.
net.sf.gridarta.model
net.sf.gridarta.model.io.AbstractAnimationObjectsReader.processAnimation
static int processAnimation(@NotNull final String animName, final int startLineNumber, @NotNull final BufferedReader in, @NotNull final String startKey, @NotNull final AnimationObjects animationObjects, @NotNull final ErrorViewCollector errorViewCollector, @Nullable final String path, @Nullable final Map< String, String > animations)
Processes an anim..mina block.
Definition: AbstractAnimationObjectsReader.java:158
net.sf.gridarta.model.io.AbstractAnimationObjectsReader.loadAnimations
void loadAnimations(@NotNull final AnimationObjects animationObjects, @NotNull final ErrorViewCollector errorViewCollector, @NotNull final String path, @NotNull final File animFile, @NotNull final String startKey)
Loads animations from a file.
Definition: AbstractAnimationObjectsReader.java:66
net.sf.gridarta.utils.IOUtils
Utility-class for Gridarta's I/O.
Definition: IOUtils.java:40
net.sf.gridarta.model.data.NamedObject.getPath
String getPath()
Get the path of this AbstractNamedObject.
net.sf.gridarta.model.anim
Gridarta can handle frame information of animations and allow the selection of an animation using a t...
Definition: AbstractAnimationObjects.java:20
net.sf.gridarta.model.anim.IllegalAnimationException.getAnimationObject
NamedObject getAnimationObject()
Returns the illegal animation object.
Definition: IllegalAnimationException.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