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