Gridarta Editor
DefaultMapReader.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.util.ArrayList;
28 import java.util.Collections;
29 import java.util.List;
34 import net.sf.gridarta.utils.IOUtils;
35 import org.jetbrains.annotations.NotNull;
36 
41 // myInput.close() is invoked in this.close()
42 public class DefaultMapReader<G extends GameObject<G, A, R>, A extends MapArchObject<A>, R extends Archetype<G, A, R>> implements MapReader<G, A> {
43 
47  @NotNull
48  private final List<G> objects = new ArrayList<>();
49 
53  @NotNull
54  private final A mapArchObject;
55 
66  public DefaultMapReader(@NotNull final MapArchObjectParserFactory<A> mapArchObjectParserFactory, @NotNull final MapArchObjectFactory<A> mapArchObjectFactory, @NotNull final GameObjectParser<G, A, R> gameObjectParser, @NotNull final File file) throws IOException {
67  try (FileInputStream fis = new FileInputStream(file)) {
68  try (InputStreamReader isr = new InputStreamReader(fis, IOUtils.MAP_ENCODING)) {
69  try (BufferedReader br = new BufferedReader(isr)) {
70  mapArchObject = mapArchObjectFactory.newMapArchObject(false);
71  mapArchObjectParserFactory.newMapArchObjectParser().load(br, mapArchObject);
72  while (gameObjectParser.load(br, objects) != null) {
73  }
74  }
75  }
76  }
77 
78  gameObjectParser.collectTempList(objects);
79  }
80 
81  @NotNull
82  @Override
83  public A getMapArchObject() {
84  return mapArchObject;
85  }
86 
87  @NotNull
88  @Override
89  public List<G> getGameObjects() {
90  return Collections.unmodifiableList(objects);
91  }
92 
93 }
static final String MAP_ENCODING
Encoding to use for maps and other data.
Definition: IOUtils.java:51
Default implementation of MapReader.
Factory for creating MapArchObject instances.
final List< G > objects
ArchObjects that are read from the map.
Base package of all Gridarta classes.
Interface for classes that read map files.
Definition: MapReader.java:31
Reflects a game object (object on a map).
Definition: GameObject.java:36
Utility-class for Gridarta&#39;s I/O.
Definition: IOUtils.java:40
GameObjects are the objects based on Archetypes found on maps.
final A mapArchObject
Contains the map arch object.
DefaultMapReader(@NotNull final MapArchObjectParserFactory< A > mapArchObjectParserFactory, @NotNull final MapArchObjectFactory< A > mapArchObjectFactory, @NotNull final GameObjectParser< G, A, R > gameObjectParser, @NotNull final File file)
Open a file for reading it as a map.