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-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.util.ArrayList;
28 import java.util.List;
33 import net.sf.gridarta.utils.IOUtils;
34 import org.jetbrains.annotations.NotNull;
35 
40 // myInput.close() is invoked in this.close()
41 public class DefaultMapReader<G extends GameObject<G, A, R>, A extends MapArchObject<A>, R extends Archetype<G, A, R>> implements MapReader<G, A> {
42 
46  @NotNull
47  private final List<G> objects = new ArrayList<>();
48 
52  @NotNull
53  private final A mapArchObject;
54 
65  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 {
66  try (FileInputStream fis = new FileInputStream(file)) {
67  try (InputStreamReader isr = new InputStreamReader(fis, IOUtils.MAP_ENCODING)) {
68  try (BufferedReader br = new BufferedReader(isr)) {
69  mapArchObject = mapArchObjectFactory.newMapArchObject(false);
70  mapArchObjectParserFactory.newMapArchObjectParser().load(br, mapArchObject);
71  while (gameObjectParser.load(br, objects) != null) {
72  }
73  }
74  }
75  }
76 
77  gameObjectParser.collectTempList(objects);
78  }
79 
80  @NotNull
81  @Override
82  public A getMapArchObject() {
83  return mapArchObject;
84  }
85 
86  @NotNull
87  @Override
88  public List<G> getGameObjects() {
89  //noinspection AssignmentOrReturnOfFieldWithMutableType
90  return objects;
91  }
92 
93 }
net.sf.gridarta.model.io.MapReader
Interface for classes that read map files.
Definition: MapReader.java:31
net.sf.gridarta
Base package of all Gridarta classes.
net.sf
net.sf.gridarta.model.io.MapArchObjectParserFactory< A >
net.sf.gridarta.model.io.DefaultMapReader.getMapArchObject
A getMapArchObject()
Definition: DefaultMapReader.java:82
net.sf.gridarta.model.archetype
Definition: AbstractArchetype.java:20
net.sf.gridarta.model.gameobject.GameObject
Reflects a game object (object on a map).
Definition: GameObject.java:36
net.sf.gridarta.model.io.DefaultMapReader.objects
final List< G > objects
ArchObjects that are read from the map.
Definition: DefaultMapReader.java:47
net.sf.gridarta.model.gameobject
GameObjects are the objects based on Archetypes found on maps.
Definition: AbstractGameObject.java:20
net
net.sf.gridarta.model.io.DefaultMapReader.DefaultMapReader
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.
Definition: DefaultMapReader.java:65
net.sf.gridarta.model.maparchobject.MapArchObject
Interface for MapArchObjects.
Definition: MapArchObject.java:40
net.sf.gridarta.model.io.DefaultMapReader.mapArchObject
final A mapArchObject
Contains the map arch object.
Definition: DefaultMapReader.java:53
net.sf.gridarta.model.io.DefaultMapReader
Default implementation of MapReader.
Definition: DefaultMapReader.java:41
net.sf.gridarta.model
net.sf.gridarta.model.archetype.Archetype
Reflects an Archetype.
Definition: Archetype.java:41
net.sf.gridarta.utils.IOUtils
Utility-class for Gridarta's I/O.
Definition: IOUtils.java:40
net.sf.gridarta.model.maparchobject
Definition: AbstractMapArchObject.java:20
net.sf.gridarta.model.io.GameObjectParser< G, A, R >
net.sf.gridarta.model.io.DefaultMapReader.getGameObjects
List< G > getGameObjects()
Definition: DefaultMapReader.java:88
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
net.sf.gridarta.model.maparchobject.MapArchObjectFactory
Factory for creating MapArchObject instances.
Definition: MapArchObjectFactory.java:28