Crossfire JXClient, Trunk
Filenames.java
Go to the documentation of this file.
1 /*
2  * This file is part of JXClient, the Fullscreen Java Crossfire Client.
3  *
4  * JXClient is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * JXClient is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with JXClient; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17  *
18  * Copyright (C) 2005-2008 Yann Chachkoff
19  * Copyright (C) 2006-2017,2019-2023 Andreas Kirschbaum
20  * Copyright (C) 2010-2012,2014-2018,2020-2023 Nicolas Weeger
21  */
22 
23 package com.realtime.crossfire.jxclient.settings;
24 
26 import java.io.IOException;
27 import java.nio.file.Files;
28 import java.nio.file.Path;
29 import java.nio.file.Paths;
30 import org.jetbrains.annotations.NotNull;
31 import org.jetbrains.annotations.Nullable;
32 
37 public class Filenames {
38 
42  private Filenames() {
43  }
44 
49  @NotNull
50  public static Path getOriginalImageCacheDir() {
51  try {
52  return getSettingsFile("cache");
53  } catch (final IOException ex) {
54  System.err.println(ex.getMessage());
55  System.exit(1);
56  throw new AssertionError(ex);
57  }
58  }
59 
64  @NotNull
65  public static Path getScaledImageCacheDir() {
66  try {
67  return getSettingsFile("cache-x2");
68  } catch (final IOException ex) {
69  System.err.println(ex.getMessage());
70  System.exit(1);
71  throw new AssertionError(ex);
72  }
73  }
74 
79  @NotNull
80  public static Path getMagicMapImageCacheDir() {
81  try {
82  return getSettingsFile("cache-mm");
83  } catch (final IOException ex) {
84  System.err.println(ex.getMessage());
85  System.exit(1);
86  throw new AssertionError(ex);
87  }
88  }
89 
95  @NotNull
96  public static Path getSettingsFile() throws IOException {
97  return getSettingsFile("jxclient.conf");
98  }
99 
109  @NotNull
110  public static Path getShortcutsFile(@NotNull final CharSequence hostname, @NotNull final CharSequence character) throws IOException {
111  return getSettingsFile("shortcuts-"+encode(hostname)+"-"+encode(character)+".txt");
112  }
113 
123  @NotNull
124  public static Path getKeybindingsFileVersion2(@Nullable final CharSequence hostname, @Nullable final CharSequence character) throws IOException {
125  return getSettingsFile(hostname == null || character == null ? "keybindings2.txt" : "keybindings2-"+encode(hostname)+"-"+encode(character)+".txt");
126  }
127 
137  @NotNull
138  public static Path getKeybindingsFileVersion1(@Nullable final CharSequence hostname, @Nullable final CharSequence character) throws IOException {
139  return getSettingsFile(hostname == null || character == null ? "keybindings.txt" : "keybindings-"+encode(hostname)+"-"+encode(character)+".txt");
140  }
141 
147  @Nullable
148  public static Path getMetaserverCacheFile() {
149  try {
150  return getSettingsFile("metaserver.txt");
151  } catch (final IOException ex) {
152  System.err.println("Cannot access metaserver cache file: "+ex.getMessage());
153  return null;
154  }
155  }
156 
163  @NotNull
164  public static Path getDialogsFile(@NotNull final String skinName) throws IOException {
165  return getSettingsFile("skin_"+skinName).resolve("dialogs.txt");
166  }
167 
174  @NotNull
175  public static Path getSettingsFile(@NotNull final String filename) throws IOException {
176  final Path settingsDir = getCrossfireFile().resolve("jxclient");
177  Files.createDirectories(settingsDir);
178  return settingsDir.resolve(filename);
179  }
180 
186  @NotNull
187  private static Path getCrossfireFile() throws IOException {
188  final String home = System.getProperty("user.home");
189  if (home == null) {
190  throw new IOException("cannot find home directory");
191  }
192 
193  return Paths.get(home).resolve(".crossfire");
194  }
195 
201  @NotNull
202  private static String encode(@NotNull final CharSequence str) {
203  final StringBuilder sb = new StringBuilder();
204  for (int i = 0; i < str.length(); i++) {
205  final char ch = str.charAt(i);
206  if (('a' <= ch && ch <= 'z') || ('A' <= ch && ch <= 'Z') || ('0' <= ch && ch <= '9') || ch == '-' || ch == '_' || ch == '.') {
207  sb.append(ch);
208  } else {
209  sb.append('%');
210  HexCodec.hexEncode2(sb, ch);
211  }
212  }
213  return sb.toString();
214  }
215 
222  @NotNull
223  public static Path getMessageLogFile(@Nullable final String hostname) throws IOException {
224  return getSettingsFile(hostname == null ? "jxclient.txt" : "jxclient-"+hostname+".txt");
225  }
226 
227 }
com.realtime.crossfire.jxclient.settings.Filenames.getKeybindingsFileVersion2
static Path getKeybindingsFileVersion2(@Nullable final CharSequence hostname, @Nullable final CharSequence character)
Definition: Filenames.java:124
com.realtime.crossfire.jxclient
com.realtime.crossfire.jxclient.settings.Filenames.getMagicMapImageCacheDir
static Path getMagicMapImageCacheDir()
Definition: Filenames.java:80
com.realtime.crossfire.jxclient.settings.Filenames.getMessageLogFile
static Path getMessageLogFile(@Nullable final String hostname)
Definition: Filenames.java:223
com.realtime.crossfire.jxclient.settings.Filenames.getSettingsFile
static Path getSettingsFile()
Definition: Filenames.java:96
com.realtime.crossfire.jxclient.settings.Filenames.getDialogsFile
static Path getDialogsFile(@NotNull final String skinName)
Definition: Filenames.java:164
com.realtime.crossfire.jxclient.settings.Filenames.getMetaserverCacheFile
static Path getMetaserverCacheFile()
Definition: Filenames.java:148
com.realtime.crossfire.jxclient.util.HexCodec
Definition: HexCodec.java:31
com.realtime.crossfire.jxclient.settings.Filenames.encode
static String encode(@NotNull final CharSequence str)
Definition: Filenames.java:202
com.realtime.crossfire.jxclient.settings.Filenames.getCrossfireFile
static Path getCrossfireFile()
Definition: Filenames.java:187
com.realtime.crossfire.jxclient.settings.Filenames.Filenames
Filenames()
Definition: Filenames.java:42
com.realtime.crossfire.jxclient.settings.Filenames.getScaledImageCacheDir
static Path getScaledImageCacheDir()
Definition: Filenames.java:65
com.realtime.crossfire.jxclient.util
Definition: Codec.java:23
com.realtime.crossfire.jxclient.settings.Filenames.getSettingsFile
static Path getSettingsFile(@NotNull final String filename)
Definition: Filenames.java:175
com.realtime.crossfire.jxclient.settings.Filenames.getKeybindingsFileVersion1
static Path getKeybindingsFileVersion1(@Nullable final CharSequence hostname, @Nullable final CharSequence character)
Definition: Filenames.java:138
com.realtime.crossfire
com.realtime
com
com.realtime.crossfire.jxclient.settings.Filenames.getOriginalImageCacheDir
static Path getOriginalImageCacheDir()
Definition: Filenames.java:50
com.realtime.crossfire.jxclient.settings.Filenames.getShortcutsFile
static Path getShortcutsFile(@NotNull final CharSequence hostname, @NotNull final CharSequence character)
Definition: Filenames.java:110
com.realtime.crossfire.jxclient.settings.Filenames
Definition: Filenames.java:37
com.realtime.crossfire.jxclient.util.HexCodec.hexEncode2
static void hexEncode2(@NotNull final StringBuilder sb, final int value)
Definition: HexCodec.java:50