Gridarta Editor
AnimationObjectsReader.java
Go to the documentation of this file.
1 /*
2  * Gridarta MMORPG map editor for Crossfire, Daimonin and similar games.
3  * Copyright (C) 2000-2015 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 class AnimationObjectsReader {
45 
50  }
51 
68  public static void loadAnimations(@NotNull final AnimationObjects animationObjects, @NotNull final ErrorViewCollector errorViewCollector, @NotNull final String path, @NotNull final File animFile, @NotNull final String startKey, final boolean ignoreOtherText) throws IOException, AnimationParseException {
69  try (FileInputStream fileInputStream = new FileInputStream(animFile)) {
70  try (Reader inputStreamReader = new InputStreamReader(fileInputStream, IOUtils.MAP_ENCODING)) {
71  loadAnimations(animationObjects, errorViewCollector, inputStreamReader, startKey, ignoreOtherText, path, null);
72  }
73  }
74  }
75 
97  public static void loadAnimations(@NotNull final AnimationObjects animationObjects, @NotNull final ErrorViewCollector errorViewCollector, final Reader reader, @NotNull final String startKey, final boolean ignoreOtherText, @Nullable final String path, @Nullable final Map<String, String> animations) throws AnimationParseException, IOException {
98  if (path == null && animations == null) {
99  throw new IllegalArgumentException();
100  }
101  if (path != null && animations != null) {
102  throw new IllegalArgumentException();
103  }
104  try (BufferedReader in = new BufferedReader(reader)) {
105  int lineNumber = 1;
106  while (true) {
107  final String line2 = in.readLine();
108  if (line2 == null) {
109  break;
110  }
111  final String line = line2.trim();
112  if (line.startsWith("#") || line.isEmpty()) {
113  /* ignore comment lines. */
114  } else if (line.startsWith(startKey)) {
115  lineNumber += processAnimation(line.substring(startKey.length()), lineNumber, in, startKey, animationObjects, errorViewCollector, path, animations);
116  } else if (line.equals("mina") || !ignoreOtherText) {
117  throw new AnimationParseException(startKey + "...", line2, lineNumber);
118  }
119 
120  lineNumber++;
121  }
122  }
123  }
124 
143  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 {
144  int lineNumber = startLineNumber;
145  final StringBuilder animText = new StringBuilder();
146  while (true) {
147  lineNumber++;
148  final String line3 = in.readLine();
149  if (line3 == null) {
150  throw new AnimationParseException("mina", "<end of file>", lineNumber);
151  }
152  final String line4 = line3.trim();
153  if (line4.startsWith("#") || line4.isEmpty()) {
154  /* ignore comment lines. */
155  } else if (line4.startsWith(startKey)) {
156  throw new AnimationParseException("mina", line4, lineNumber);
157  } else if (line4.equals("mina")) {
158  processAnimationLine(animationObjects, errorViewCollector, path, animations, animName, animText.toString());
159  break;
160  } else {
161  animText.append(line4).append('\n');
162  }
163  }
164  return lineNumber - startLineNumber;
165  }
166 
179  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) {
180  if (path == null && animations == null) {
181  throw new IllegalArgumentException();
182  }
183 
184  final String animPath;
185  if (path != null) {
186  animPath = path;
187  } else {
188  final String tmp = animations.get(animName);
189  if (tmp == null) {
190  errorViewCollector.addWarning(ErrorViewCategory.ANIMATIONS_ENTRY_INVALID, "no path found for animation: " + animName);
191  animPath = "/" + animName;
192  } else {
193  animPath = tmp;
194  }
195  }
196  try {
197  animationObjects.addAnimationObject(animName, animText, animPath);
198  } catch (final DuplicateAnimationException ex) {
199  errorViewCollector.addWarning(ErrorViewCategory.ANIMATIONS_ENTRY_INVALID, ex.getMessage());
200  } catch (final IllegalAnimationException ex) {
201  errorViewCollector.addWarning(ErrorViewCategory.ANIMATIONS_ENTRY_INVALID, "illegal animation: " + ex.getAnimationObject().getPath());
202  }
203  }
204 
205 }
This exception is thrown when parsing an animation definition file (.
Convenience class for adding messages to a ErrorView instance using a fixed category name...
Utility class for reading AnimationObjects from files.
static final String MAP_ENCODING
Encoding to use for maps and other data.
Definition: IOUtils.java:51
Gridarta can handle frame information of animations and allow the selection of an animation using a t...
static void loadAnimations(@NotNull final AnimationObjects animationObjects, @NotNull final ErrorViewCollector errorViewCollector, final Reader reader, @NotNull final String startKey, final boolean ignoreOtherText, @Nullable final String path, @Nullable final Map< String, String > animations)
Loads any number of animations from a reader.
Exception thrown to indicate that an animation object is not acceptable.
Defines possible error categories for ErrorView instances.
static void loadAnimations(@NotNull final AnimationObjects animationObjects, @NotNull final ErrorViewCollector errorViewCollector, @NotNull final String path, @NotNull final File animFile, @NotNull final String startKey, final boolean ignoreOtherText)
Loads animations from a file.
Base package of all Gridarta classes.
Utility-class for Gridarta&#39;s I/O.
Definition: IOUtils.java:40
AnimationObjects is a container for AnimationObjects.
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.
String getPath()
Get the path of this AbstractNamedObject.
NamedObject getAnimationObject()
Returns the illegal animation object.
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.
Exception that&#39;s thrown in case an animation name was not unique.
AnimationObjectsReader()
Private constructor to prevent instantiation.