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-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 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("neither path nor animations is set");
100  }
101  if (path != null && animations != null) {
102  throw new IllegalArgumentException("both path and animations are set");
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("neither path not animations is set");
182  }
183 
184  final String animPath;
185  if (path == null) {
186  final String tmp = animations.get(animName);
187  if (tmp == null) {
188  errorViewCollector.addWarning(ErrorViewCategory.ANIMATIONS_ENTRY_INVALID, "no path found for animation: " + animName);
189  animPath = "/" + animName;
190  } else {
191  animPath = tmp;
192  }
193  } else {
194  animPath = path;
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 }
net.sf.gridarta.model.anim.IllegalAnimationException.getAnimationObject
NamedObject getAnimationObject()
Definition: IllegalAnimationException.java:58
net.sf.gridarta.model.anim.DuplicateAnimationException
Definition: DuplicateAnimationException.java:27
net.sf.gridarta.model.anim.IllegalAnimationException
Definition: IllegalAnimationException.java:29
net.sf.gridarta.model.io.AnimationObjectsReader.loadAnimations
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)
Definition: AnimationObjectsReader.java:68
net.sf.gridarta
net.sf.gridarta.model.errorview.ErrorViewCollector
Definition: ErrorViewCollector.java:31
net.sf.gridarta.model.io.AnimationObjectsReader.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)
Definition: AnimationObjectsReader.java:179
net.sf
net.sf.gridarta.model.io.AnimationObjectsReader.loadAnimations
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)
Definition: AnimationObjectsReader.java:97
net.sf.gridarta.model.anim.AnimationObjects
Definition: AnimationObjects.java:30
net.sf.gridarta.utils.IOUtils.MAP_ENCODING
static final String MAP_ENCODING
Definition: IOUtils.java:52
net.sf.gridarta.model.io.AnimationObjectsReader
Definition: AnimationObjectsReader.java:44
net.sf.gridarta.model.data.NamedObject.getPath
String getPath()
net
net.sf.gridarta.model.errorview
Definition: ErrorView.java:20
net.sf.gridarta.model.errorview.ErrorViewCategory.ANIMATIONS_ENTRY_INVALID
ANIMATIONS_ENTRY_INVALID
Definition: ErrorViewCategory.java:36
net.sf.gridarta.model.io.AnimationObjectsReader.AnimationObjectsReader
AnimationObjectsReader()
Definition: AnimationObjectsReader.java:49
net.sf.gridarta.model
net.sf.gridarta.model.anim
Definition: AbstractAnimationObjects.java:20
net.sf.gridarta.model.errorview.ErrorViewCategory
Definition: ErrorViewCategory.java:28
net.sf.gridarta.model.io.AnimationObjectsReader.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)
Definition: AnimationObjectsReader.java:143
net.sf.gridarta.utils.IOUtils
Definition: IOUtils.java:40
net.sf.gridarta.utils
Definition: ActionBuilderUtils.java:20
net.sf.gridarta.model.anim.AnimationParseException
Definition: AnimationParseException.java:31