Gridarta Editor
ScriptUtils.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.scripts;
21 
22 import java.io.File;
23 import java.io.IOException;
25 import org.apache.log4j.Category;
26 import org.apache.log4j.Logger;
27 import org.jetbrains.annotations.NotNull;
28 
29 public class ScriptUtils {
30 
34  private static final Category LOG = Logger.getLogger(ScriptUtils.class);
35 
39  private ScriptUtils() {
40  }
41 
51  public static String localizeEventPath(@NotNull final File localMapDir, final File f, @NotNull final File mapDir) {
52  if (!mapDir.exists()) {
53  LOG.warn("Map directory '" + mapDir.getAbsolutePath() + "' does not exist!");
54  return f.getName();
55  }
56 
57  // find out if the script file is in a sub-directory of the map file
58  File tmp;
59  for (tmp = f.getParentFile(); tmp != null && !tmp.getAbsolutePath().equalsIgnoreCase(localMapDir.getAbsolutePath()); tmp = tmp.getParentFile()) {
60  }
61 
62  // FIXME: It would be a good idea to perform the canonization somewhere else.
63  // The paths returned by mapFile.getParentFile() and mControl.getMapDefaultFolder() should already be canonical
64  String path;
65  if (tmp == null) {
66  // script file is NOT in a sub-directory of map file -> absolute path
67  try {
68  path = f.getAbsolutePath().substring(mapDir.getCanonicalPath().length());
69  } catch (final IOException ignored) {
70  path = f.getAbsolutePath().substring(mapDir.getAbsolutePath().length());
71  }
72  path = path.replace('\\', '/');
73  if (!path.startsWith("/")) {
74  path = "/" + path; // leading slash
75  }
76  } else {
77  // script file is in a sub-directory of map file -> relative path
78  try {
79  path = f.getAbsolutePath().substring(localMapDir.getCanonicalPath().length());
80  } catch (final IOException ignored) {
81  path = f.getAbsolutePath().substring(localMapDir.getAbsolutePath().length());
82  }
83  path = path.replace('\\', '/');
84  while (!path.isEmpty() && path.startsWith("/")) {
85  path = path.substring(1); // no leading slash
86  }
87  }
88  return path;
89  }
90 
100  @NotNull
101  public static String chooseDefaultScriptName(@NotNull final File baseDir, final String archetypeName, final String scriptEnding, @NotNull final PathManager pathManager) {
102  String defScriptName = archetypeName.trim();
103  final int i = defScriptName.indexOf(' ');
104  if (i >= 0) {
105  if (defScriptName.length() > 12 || defScriptName.lastIndexOf(' ') != i) {
106  // if there are several whitespaces or the name is too long, just cut off the end
107  defScriptName = defScriptName.substring(0, i);
108  } else {
109  // if there is only one whitespace in a short name, remove whitespace
110  defScriptName = defScriptName.substring(0, i) + defScriptName.substring(i + 1, i + 2).toUpperCase() + defScriptName.substring(i + 2);
111  }
112  }
113  if (defScriptName.length() >= 3) {
114  defScriptName = defScriptName.substring(0, 1).toUpperCase() + defScriptName.substring(1);
115  }
116  defScriptName += "Script" + scriptEnding;
117 
118  return pathManager.getMapPath(new File(baseDir, defScriptName));
119  }
120 
121 }
static final Category LOG
The Logger for printing log messages.
This class contains methods for converting relative map paths to absolute map paths and vice versa...
Reading and writing of maps, handling of paths.
Base package of all Gridarta classes.
static String chooseDefaultScriptName(@NotNull final File baseDir, final String archetypeName, final String scriptEnding, @NotNull final PathManager pathManager)
Try to create a reasonable default script name for lazy users.
static String localizeEventPath(@NotNull final File localMapDir, final File f, @NotNull final File mapDir)
This method is called when the user selects a new event to be created.
ScriptUtils()
Private constructor to prevent instantiation.