Gridarta Editor
AbstractCollectedResourcesReader.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.resource;
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.Collections;
30 import java.util.List;
31 import java.util.Map;
43 import net.sf.gridarta.utils.IOUtils;
44 import org.apache.log4j.Category;
45 import org.apache.log4j.Logger;
46 import org.jetbrains.annotations.NotNull;
47 import org.jetbrains.annotations.Nullable;
48 
54 public abstract class AbstractCollectedResourcesReader<G extends GameObject<G, A, R>, A extends MapArchObject<A>, R extends Archetype<G, A, R>> extends AbstractResourcesReader<G> {
55 
59  private static final Category LOG = Logger.getLogger(AbstractCollectedResourcesReader.class);
60 
64  @NotNull
65  private final File collectedDirectory;
66 
70  @NotNull
72 
76  @NotNull
78 
82  @NotNull
83  private final FaceObjects faceObjects;
84 
88  @NotNull
89  private final String animTreeFile;
90 
94  @NotNull
95  private final String archetypesFile;
96 
108  protected AbstractCollectedResourcesReader(@NotNull final File collectedDirectory, @Nullable final String imageSet, @NotNull final ArchetypeSet<G, A, R> archetypeSet, @NotNull final AbstractArchetypeParser<G, A, R, ?> archetypeParser, @NotNull final AnimationObjects animationObjects, @NotNull final FaceObjects faceObjects, @NotNull final String animTreeFile, @NotNull final String archetypesFile) {
109  super(collectedDirectory, imageSet, animationObjects, faceObjects);
110  this.collectedDirectory = collectedDirectory;
111  this.archetypeSet = archetypeSet;
112  this.archetypeParser = archetypeParser;
113  this.faceObjects = faceObjects;
114  this.animTreeFile = animTreeFile;
115  this.archetypesFile = archetypesFile;
116  }
117 
122  protected void loadAnimations(@NotNull final ErrorView errorView) {
123  Map<String, String> animations = null;
124  try {
125  final URL url = IOUtils.getResource(collectedDirectory, animTreeFile);
126  try {
127  animations = loadAnimTree(url);
128  } catch (final IOException ex) {
129  errorView.addWarning(ErrorViewCategory.ANIMTREE_FILE_INVALID, url + ": " + ex.getMessage());
130  }
131  } catch (final IOException ex) {
132  errorView.addWarning(ErrorViewCategory.ANIMTREE_FILE_INVALID, animTreeFile + ": " + ex.getMessage());
133  }
134  loadAnimationsFromCollect(errorView, animations == null ? Collections.emptyMap() : animations);
135  }
136 
142  protected void loadArchetypes(@NotNull final ErrorView errorView, @NotNull final List<G> invObjects) {
143  archetypeSet.setLoadedFromArchive(true);
144  try {
145  final int archetypeCount = archetypeSet.getArchetypeCount();
146 
147  final URL url = IOUtils.getResource(collectedDirectory, archetypesFile);
148  try {
149  try (InputStream inputStream = url.openStream()) {
150  try (Reader reader = new InputStreamReader(inputStream, IOUtils.MAP_ENCODING)) {
151  try (BufferedReader bufferedReader = new BufferedReader(reader)) {
152  archetypeParser.parseArchetypeFromStream(bufferedReader, null, null, null, "default", "default", "", invObjects, new ErrorViewCollector(errorView, url));
153  }
154  }
155  }
156  } catch (final IOException ex) {
157  errorView.addWarning(ErrorViewCategory.ARCHETYPES_FILE_INVALID, url + ": " + ex.getMessage());
158  }
159 
160  if (LOG.isInfoEnabled()) {
161  LOG.info("Loaded " + (archetypeSet.getArchetypeCount() - archetypeCount) + " archetypes from '" + url + "'.");
162  }
163  } catch (final IOException ex) {
164  errorView.addWarning(ErrorViewCategory.ARCHETYPES_FILE_INVALID, archetypesFile + ": " + ex.getMessage());
165  }
166  }
167 
173  @NotNull
174  protected FaceProvider loadFacesCollection(@NotNull final ErrorView errorView) {
175  return faceObjects.loadFacesCollection(errorView, collectedDirectory);
176  }
177 
178 }
Convenience class for adding messages to a ErrorView instance using a fixed category name...
Abstract base class for AbstractResourcesReaders that read from collected files.
FaceProvider loadFacesCollection(@NotNull final ErrorView errorView)
Loads all faces.
Reading and writing of maps, handling of paths.
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...
This interface represents a lazy loader that provides images on demand.
final AbstractArchetypeParser< G, A, R, ?> archetypeParser
The AbstractArchetypeParser to use.
void loadAnimationsFromCollect( @NotNull final ErrorView errorView, @NotNull final Map< String, String > animations)
Loads all animations from the big collected animations file.
final String animTreeFile
The name of the animation tree information file.
Defines possible error categories for ErrorView instances.
FaceProvider loadFacesCollection(@NotNull ErrorView errorView, @NotNull File collectedDirectory)
Loads all faces from a png collection file.
final AnimationObjects animationObjects
The animation objects instance.
Interface for classes displaying error messages.
Definition: ErrorView.java:28
Base package of all Gridarta classes.
void loadAnimations(@NotNull final ErrorView errorView)
Loads all animations.
Reflects a game object (object on a map).
Definition: GameObject.java:36
int getArchetypeCount()
Returns the number of Archetypes available.
Utility-class for Gridarta&#39;s I/O.
Definition: IOUtils.java:40
AbstractCollectedResourcesReader(@NotNull final File collectedDirectory, @Nullable final String imageSet, @NotNull final ArchetypeSet< G, A, R > archetypeSet, @NotNull final AbstractArchetypeParser< G, A, R, ?> archetypeParser, @NotNull final AnimationObjects animationObjects, @NotNull final FaceObjects faceObjects, @NotNull final String animTreeFile, @NotNull final String archetypesFile)
Creates a new instance.
AnimationObjects is a container for AnimationObjects.
final String archetypesFile
The name of the collected archetypes file.
GameObjects are the objects based on Archetypes found on maps.
void loadArchetypes(@NotNull final ErrorView errorView, @NotNull final List< G > invObjects)
Loads all archetypes.
void parseArchetypeFromStream(@NotNull final BufferedReader in, @Nullable final R prototype, @Nullable final String line, @Nullable final String archName, @NotNull final String panelName, @NotNull final String folderName, @NotNull final String archPath, @NotNull final List< G > invObjects, @NotNull final ErrorViewCollector errorViewCollector)
static URL getResource(@Nullable final File dir, @NotNull final String fileName)
Get the URL of a resource.
Definition: IOUtils.java:68
FaceObjects is a container for FaceObjects.
static final Category LOG
The logger for printing log messages.
static Map< String, String > loadAnimTree( @NotNull final URL url)
Loads animations from a file.
final ArchetypeSet< G, A, R > archetypeSet
The ArchetypeSet to update.
The face is the appearance of an object.
Interface that captures similarities between different ArchetypeSet implementations.
Abstract base class for archetype set loader classes.
Abstract base implementation of ArchetypeParser.
void setLoadedFromArchive(boolean loadedFromArchive)
Sets whether Archetypes were loaded from an archive.