Gridarta Editor
TestMapControlCreator.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.mapcontrol;
21 
22 import java.io.File;
23 import java.io.IOException;
58 import net.sf.gridarta.utils.Size2D;
60 import org.jetbrains.annotations.NotNull;
61 import org.jetbrains.annotations.Nullable;
62 import org.junit.Assert;
63 
68 public class TestMapControlCreator {
69 
73  @NotNull
75 
79  @NotNull
81 
85  @NotNull
86  private final PathManager pathManager;
87 
91  @NotNull
93 
97  @NotNull
99 
103  @NotNull
105 
109  @NotNull
111 
115  @NotNull
117 
121  @NotNull
123 
127  @NotNull
129 
133  @NotNull
135 
140  final GameObjectMatcher exitGameObjectMatcher = new TypeNrsGameObjectMatcher(TestMapModelHelper.EXIT_TYPE);
141  exitMatcher = new ExitMatcher<>(exitGameObjectMatcher);
153  tmpMapManager.setFileControl(fileControl);
154  mapManager = tmpMapManager;
156  tmpPickmapManager.setFileControl(fileControl);
157  pickmapManager = tmpPickmapManager;
158  }
159 
167  }
168 
176  public MapControl<TestGameObject, TestMapArchObject, TestArchetype> newMapControl(@Nullable final MapFile mapFile, @NotNull final String mapName, @NotNull final Size2D mapSize) {
177  final TestMapArchObject mapArchObject = mapArchObjectFactory.newMapArchObject(false);
178  mapArchObject.setMapSize(mapSize);
179  mapArchObject.setMapName(mapName);
180  return mapManager.newMap(null, mapArchObject, mapFile, true);
181  }
182 
187  @NotNull
189  return pathManager;
190  }
191 
196  @NotNull
198  return projectSettings;
199  }
200 
205  @NotNull
207  return exitMatcher;
208  }
209 
214  @NotNull
217  }
218 
223  @NotNull
225  return mapManager;
226  }
227 
232  @NotNull
235  }
236 
241  @NotNull
244  }
245 
250  @NotNull
252  return mapModelCreator;
253  }
254 
259  @NotNull
261  return mapArchObjectFactory;
262  }
263 
268  @NotNull
270  return mapModelFactory;
271  }
272 
277  @NotNull
279  return pickmapManager;
280  }
281 
286  @NotNull
288  return mapWriter;
289  }
290 
297  @NotNull
298  public DefaultMapReader<TestGameObject, TestMapArchObject, TestArchetype> newMapReader(@NotNull final File mapFile) throws IOException {
300  }
301 
311  @NotNull
312  public File createMapsDirectory(@NotNull final String... specs) throws IOException {
313  final File mapsDirectory = createTempDir("gridarta");
314  projectSettings.setMapsDirectory(mapsDirectory);
315  @Nullable File directoryToDelete = mapsDirectory;
316  try {
317  createMaps(specs);
318  directoryToDelete = null;
319  } finally {
320  if (directoryToDelete != null) {
321  deleteTempDir(directoryToDelete);
322  }
323  }
324  return mapsDirectory;
325  }
326 
332  private void createMaps(@NotNull final String @NotNull [] specs) throws IOException {
333  for (final String spec : specs) {
334  final String[] tmp = StringUtils.PATTERN_COLON.split(spec, 2);
335  if (tmp.length != 2) {
336  throw new IllegalArgumentException("syntax error: " + spec);
337  }
338 
339  final MapFile mapFile = new PathManager(projectSettings).getMapFile(new File(projectSettings.getMapsDirectory(), tmp[0]));
340  final String mapName = tmp[1];
341 
342  final File file = mapFile.getFile();
343  if (!file.getParentFile().exists() && !file.getParentFile().mkdirs()) {
344  throw new IOException("cannot create directory: " + file.getParentFile());
345  }
346 
349  mapControl.getMapModel().getMapArchObject().setMapName(mapName);
350  mapControl.saveAs(mapFile);
351  }
352  }
353 
362  @NotNull
363  private static File createTempDir(@NotNull final String prefix) throws IOException {
364  final String tmpDir = System.getProperty("java.io.tmpdir");
365  if (tmpDir == null) {
366  throw new IOException("the system property 'java.io.tmpdir' does not exist");
367  }
368 
369  final File tmpFile = new File(tmpDir);
370  if (!tmpFile.exists() && !tmpFile.mkdirs()) {
371  throw new IOException("cannot create temporary directory " + tmpFile);
372  }
373 
374  final File result = File.createTempFile(prefix, null);
375  if (!result.delete()) {
376  throw new IOException("cannot delete temporary file " + result);
377  }
378  if (!result.mkdir()) {
379  throw new IOException("cannot create temporary directory " + result);
380  }
381 
382  return result;
383  }
384 
389  public static void deleteTempDir(@NotNull final File dir) {
390  final File[] files = dir.listFiles();
391  if (files != null) {
392  for (final File file : files) {
393  if (file.isDirectory()) {
394  deleteTempDir(file);
395  } else if (!file.delete()) {
396  Assert.fail("cannot delete file " + file);
397  }
398  }
399  }
400  if (!dir.delete()) {
401  Assert.fail("cannot delete directory " + dir);
402  }
403  }
404 
405 }
net.sf.gridarta.model.archetype.TestArchetype
Definition: TestArchetype.java:30
net.sf.gridarta.model.mapmodel.TestMapModelCreator.getInsertionModeSet
InsertionModeSet< TestGameObject, TestMapArchObject, TestArchetype > getInsertionModeSet()
Definition: TestMapModelCreator.java:230
net.sf.gridarta.model.mapcontrol.TestMapControlCreator.createMapsDirectory
File createMapsDirectory(@NotNull final String... specs)
Definition: TestMapControlCreator.java:312
net.sf.gridarta.model.mapcontrol.TestMapControlCreator.getMapManager
MapManager< TestGameObject, TestMapArchObject, TestArchetype > getMapManager()
Definition: TestMapControlCreator.java:224
net.sf.gridarta.model.mapmanager
Definition: AbstractMapManager.java:20
net.sf.gridarta.model.mapcontrol.TestMapControlCreator.pathManager
final PathManager pathManager
Definition: TestMapControlCreator.java:86
net.sf.gridarta.model.mapmodel.MapModel
Definition: MapModel.java:75
net.sf.gridarta.model.mapcontrol.MapControl.saveAs
void saveAs(@NotNull MapFile mapFile)
net.sf.gridarta.model.mapcontrol.MapControlFactory< TestGameObject, TestMapArchObject, TestArchetype >
net.sf.gridarta.model.mapmodel.MapModel.getMapArchObject
A getMapArchObject()
net.sf.gridarta.model.mapcontrol.TestMapControlCreator.mapArchObjectParserFactory
final MapArchObjectParserFactory< TestMapArchObject > mapArchObjectParserFactory
Definition: TestMapControlCreator.java:98
net.sf.gridarta.model.mapmodel.InsertionModeSet
Definition: InsertionModeSet.java:33
net.sf.gridarta.model.mapmanager.MapManager
Definition: MapManager.java:37
net.sf.gridarta.model.mapmodel.TestMapModelCreator.newTestMapModelHelper
TestMapModelHelper newTestMapModelHelper()
Definition: TestMapModelCreator.java:313
net.sf.gridarta.model.archetypechooser.ArchetypeChooserModel
Definition: ArchetypeChooserModel.java:38
net.sf.gridarta
net.sf.gridarta.model.mapcontrol.TestMapControlFactory
Definition: TestMapControlFactory.java:38
net.sf.gridarta.model.mapmodel.TestMapModelHelper
Definition: TestMapModelHelper.java:45
net.sf.gridarta.utils.StringUtils.PATTERN_COLON
static final Pattern PATTERN_COLON
Definition: StringUtils.java:91
net.sf.gridarta.model.mapmanager.AbstractMapManager
Definition: AbstractMapManager.java:48
net.sf.gridarta.model.mapmanager.AbstractMapManager.setFileControl
void setFileControl(@NotNull final FileControl< G, A, R > fileControl)
Definition: AbstractMapManager.java:121
net.sf.gridarta.model.mapcontrol.TestMapControlCreator.getInsertionModeSet
InsertionModeSet< TestGameObject, TestMapArchObject, TestArchetype > getInsertionModeSet()
Definition: TestMapControlCreator.java:233
net.sf.gridarta.model.maparchobject.TestMapArchObjectFactory
Definition: TestMapArchObjectFactory.java:28
net.sf.gridarta.model.archetypechooser
Definition: ArchetypeChooserFolder.java:20
net.sf.gridarta.model.io.MapReaderFactory
Definition: MapReaderFactory.java:32
net.sf.gridarta.model.gameobject.TestGameObject
Definition: TestGameObject.java:34
net.sf.gridarta.model.mapcontrol.TestMapControlCreator.mapArchObjectFactory
final MapArchObjectFactory< TestMapArchObject > mapArchObjectFactory
Definition: TestMapControlCreator.java:116
net.sf
net.sf.gridarta.model.mapmanager.FileControl
Definition: FileControl.java:30
net.sf.gridarta.model.io.PathManager
Definition: PathManager.java:39
net.sf.gridarta.model.io.MapArchObjectParserFactory
Definition: MapArchObjectParserFactory.java:29
net.sf.gridarta.model.mapcontrol.TestMapControlCreator.mapManager
final MapManager< TestGameObject, TestMapArchObject, TestArchetype > mapManager
Definition: TestMapControlCreator.java:74
net.sf.gridarta.model.mapmodel
Definition: AboveFloorInsertionMode.java:20
net.sf.gridarta.model.mapmodel.TestMapModelCreator
Definition: TestMapModelCreator.java:63
net.sf.gridarta.model.autojoin.AutojoinListsHelper
Definition: AutojoinListsHelper.java:36
net.sf.gridarta.model.maparchobject.AbstractMapArchObject.setMapName
void setMapName(@NotNull final String name)
Definition: AbstractMapArchObject.java:228
net.sf.gridarta.model.mapmodel.MapModelFactory
Definition: MapModelFactory.java:35
net.sf.gridarta.model.mapcontrol.TestMapControlCreator.deleteTempDir
static void deleteTempDir(@NotNull final File dir)
Definition: TestMapControlCreator.java:389
net.sf.gridarta.model.match.GameObjectMatcher
Definition: GameObjectMatcher.java:30
net.sf.gridarta.model.archetype
Definition: AbstractArchetype.java:20
net.sf.gridarta.model.mapcontrol.TestMapControlCreator.mapModelFactory
final MapModelFactory< TestGameObject, TestMapArchObject, TestArchetype > mapModelFactory
Definition: TestMapControlCreator.java:122
net.sf.gridarta.model.exitconnector.ExitMatcher
Definition: ExitMatcher.java:36
net.sf.gridarta.model.mapmanager.TestFileControl
Definition: TestFileControl.java:34
net.sf.gridarta.model.mapcontrol.TestMapControlCreator.getMapModelFactory
MapModelFactory< TestGameObject, TestMapArchObject, TestArchetype > getMapModelFactory()
Definition: TestMapControlCreator.java:269
net.sf.gridarta.model.mapcontrol.TestMapControlCreator.getPathManager
PathManager getPathManager()
Definition: TestMapControlCreator.java:188
net.sf.gridarta.model.mapcontrol.TestMapControlCreator.createMaps
void createMaps(@NotNull final String @NotNull[] specs)
Definition: TestMapControlCreator.java:332
net.sf.gridarta.model.mapmodel.TestMapModelCreator.getAutojoinLists
AutojoinLists< TestGameObject, TestMapArchObject, TestArchetype > getAutojoinLists()
Definition: TestMapModelCreator.java:322
net.sf.gridarta.model.maparchobject.AbstractMapArchObject.setMapSize
void setMapSize(@NotNull final Size2D mapSize)
Definition: AbstractMapArchObject.java:209
net.sf.gridarta.model.mapcontrol.TestMapControlCreator.getMapArchObjectFactory
MapArchObjectFactory< TestMapArchObject > getMapArchObjectFactory()
Definition: TestMapControlCreator.java:260
net.sf.gridarta.model.mapcontrol.TestMapControlCreator.createTempDir
static File createTempDir(@NotNull final String prefix)
Definition: TestMapControlCreator.java:363
net.sf.gridarta.model.mapmanager.DefaultMapManager
Definition: DefaultMapManager.java:40
net.sf.gridarta.model.mapcontrol.TestMapControlCreator.getArchetypeSet
ArchetypeSet< TestGameObject, TestMapArchObject, TestArchetype > getArchetypeSet()
Definition: TestMapControlCreator.java:215
net.sf.gridarta.model.mapcontrol.TestMapControlCreator
Definition: TestMapControlCreator.java:68
net.sf.gridarta.model.mapcontrol.TestMapControlCreator.pickmapManager
final MapManager< TestGameObject, TestMapArchObject, TestArchetype > pickmapManager
Definition: TestMapControlCreator.java:80
net.sf.gridarta.model.gameobject
Definition: AbstractGameObject.java:20
net
net.sf.gridarta.model.mapmodel.TestMapModelCreator.newMapModel
MapModel< TestGameObject, TestMapArchObject, TestArchetype > newMapModel(final int w, final int h)
Definition: TestMapModelCreator.java:168
net.sf.gridarta.model.match.TypeNrsGameObjectMatcher
Definition: TypeNrsGameObjectMatcher.java:30
net.sf.gridarta.model.mapmodel.TestMapModelCreator.getFaceObjectProviders
FaceObjectProviders getFaceObjectProviders()
Definition: TestMapModelCreator.java:366
net.sf.gridarta.model.mapmodel.TestMapModelCreator.getGameObjectFactory
GameObjectFactory< TestGameObject, TestMapArchObject, TestArchetype > getGameObjectFactory()
Definition: TestMapModelCreator.java:294
net.sf.gridarta.model.io.MapWriter
Definition: MapWriter.java:34
net.sf.gridarta.model.match
Definition: AndGameObjectMatcher.java:20
net.sf.gridarta.model.autojoin
Definition: AutojoinList.java:20
net.sf.gridarta.model.mapcontrol.TestMapControlCreator.newMapModelCreator
TestMapModelHelper newMapModelCreator()
Definition: TestMapControlCreator.java:165
net.sf.gridarta.model.io.TestMapArchObjectParserFactory
Definition: TestMapArchObjectParserFactory.java:29
net.sf.gridarta.model.mapmodel.TestMapModelCreator.getArchetypeSet
ArchetypeSet< TestGameObject, TestMapArchObject, TestArchetype > getArchetypeSet()
Definition: TestMapModelCreator.java:349
net.sf.gridarta.model.io.DefaultMapWriter
Definition: DefaultMapWriter.java:36
net.sf.gridarta.model.io.PathManager.getMapFile
MapFile getMapFile(@NotNull final AbsoluteMapPath mapPath)
Definition: PathManager.java:72
net.sf.gridarta.model.settings.TestProjectSettings
Definition: TestProjectSettings.java:29
net.sf.gridarta.model.mapmodel.MapFile.getFile
File getFile()
Definition: MapFile.java:102
net.sf.gridarta.model.mapcontrol.TestMapControlCreator.getMapWriter
MapWriter< TestGameObject, TestMapArchObject, TestArchetype > getMapWriter()
Definition: TestMapControlCreator.java:287
net.sf.gridarta.model.archetypeset.ArchetypeSet
Definition: ArchetypeSet.java:37
net.sf.gridarta.model.mapcontrol.TestMapControlCreator.getPickmapManager
MapManager< TestGameObject, TestMapArchObject, TestArchetype > getPickmapManager()
Definition: TestMapControlCreator.java:278
net.sf.gridarta.model.settings.ProjectSettings.setMapsDirectory
void setMapsDirectory(@NotNull File mapsDirectory)
net.sf.gridarta.model.mapcontrol.TestMapControlCreator.newMapReader
DefaultMapReader< TestGameObject, TestMapArchObject, TestArchetype > newMapReader(@NotNull final File mapFile)
Definition: TestMapControlCreator.java:298
net.sf.gridarta.model.mapmodel.TestMapModelCreator.getGameObjectMatchers
GameObjectMatchers getGameObjectMatchers()
Definition: TestMapModelCreator.java:340
net.sf.gridarta.utils.StringUtils
Definition: StringUtils.java:31
net.sf.gridarta.model.mapmodel.MapFile
Definition: MapFile.java:31
net.sf.gridarta.model.exitconnector
Definition: AbstractExitConnectorModel.java:20
net.sf.gridarta.model.io
Definition: AbstractArchetypeParser.java:20
net.sf.gridarta.model.settings.ProjectSettings
Definition: ProjectSettings.java:29
net.sf.gridarta.model.io.DefaultMapReader
Definition: DefaultMapReader.java:41
net.sf.gridarta.model.mapcontrol.TestMapControlCreator.TestMapControlCreator
TestMapControlCreator()
Definition: TestMapControlCreator.java:139
net.sf.gridarta.model.mapmodel.TestMapModelCreator.getTopmostInsertionMode
InsertionMode getTopmostInsertionMode()
Definition: TestMapModelCreator.java:239
net.sf.gridarta.model
net.sf.gridarta.model.mapcontrol.TestMapControlCreator.getProjectSettings
ProjectSettings getProjectSettings()
Definition: TestMapControlCreator.java:197
net.sf.gridarta.model.mapmodel.TestMapModelHelper.EXIT_TYPE
static final int EXIT_TYPE
Definition: TestMapModelHelper.java:50
net.sf.gridarta.model.mapcontrol.TestMapControlCreator.exitMatcher
final ExitMatcher< TestGameObject, TestMapArchObject, TestArchetype > exitMatcher
Definition: TestMapControlCreator.java:92
net.sf.gridarta.model.mapcontrol.TestMapControlCreator.newMapControl
MapControl< TestGameObject, TestMapArchObject, TestArchetype > newMapControl(@Nullable final MapFile mapFile, @NotNull final String mapName, @NotNull final Size2D mapSize)
Definition: TestMapControlCreator.java:176
net.sf.gridarta.model.archetype.DuplicateArchetypeException
Definition: DuplicateArchetypeException.java:29
net.sf.gridarta.model.io.TestMapReaderFactory
Definition: TestMapReaderFactory.java:34
net.sf.gridarta.model.mapcontrol.MapControl
Definition: MapControl.java:35
net.sf.gridarta.model.mapcontrol.TestMapControlCreator.getMapModelCreator
TestMapModelCreator getMapModelCreator()
Definition: TestMapControlCreator.java:251
net.sf.gridarta.model.mapcontrol.MapControl.getMapModel
MapModel< G, A, R > getMapModel()
net.sf.gridarta.model.maparchobject.TestMapArchObject
Definition: TestMapArchObject.java:28
net.sf.gridarta.model.maparchobject
Definition: AbstractMapArchObject.java:20
net.sf.gridarta.model.mapcontrol.TestMapControlCreator.getExitMatcher
ExitMatcher< TestGameObject, TestMapArchObject, TestArchetype > getExitMatcher()
Definition: TestMapControlCreator.java:206
net.sf.gridarta.model.settings.ProjectSettings.getMapsDirectory
File getMapsDirectory()
net.sf.gridarta.model.mapcontrol.DefaultMapControl
Definition: DefaultMapControl.java:44
net.sf.gridarta.model.io.GameObjectParser
Definition: GameObjectParser.java:37
net.sf.gridarta.model.mapcontrol.TestMapControlCreator.mapWriter
final MapWriter< TestGameObject, TestMapArchObject, TestArchetype > mapWriter
Definition: TestMapControlCreator.java:134
net.sf.gridarta.model.maparchobject.MapArchObjectFactory.newMapArchObject
A newMapArchObject(boolean addDefaultAttributes)
net.sf.gridarta.model.archetypeset
Definition: ArchetypeSet.java:20
net.sf.gridarta.utils.Size2D
Definition: Size2D.java:30
net.sf.gridarta.utils
Definition: ActionBuilderUtils.java:20
net.sf.gridarta.model.mapcontrol.TestMapControlCreator.gameObjectParser
final GameObjectParser< TestGameObject, TestMapArchObject, TestArchetype > gameObjectParser
Definition: TestMapControlCreator.java:104
net.sf.gridarta.model.settings
Definition: AbstractDefaultProjectSettings.java:20
net.sf.gridarta.model.mapcontrol.TestMapControlCreator.projectSettings
final ProjectSettings projectSettings
Definition: TestMapControlCreator.java:110
net.sf.gridarta.model.maparchobject.MapArchObjectFactory
Definition: MapArchObjectFactory.java:28
net.sf.gridarta.model.mapmodel.TestMapModelCreator.newGameObjectParser
GameObjectParser< TestGameObject, TestMapArchObject, TestArchetype > newGameObjectParser()
Definition: TestMapModelCreator.java:357
net.sf.gridarta.model.mapcontrol.TestMapControlCreator.newAutojoinListsHelper
AutojoinListsHelper newAutojoinListsHelper()
Definition: TestMapControlCreator.java:242
net.sf.gridarta.model.mapcontrol.TestMapControlCreator.mapModelCreator
final TestMapModelCreator mapModelCreator
Definition: TestMapControlCreator.java:128