mirror of
https://github.com/Card-Forge/forge.git
synced 2025-11-19 04:08:01 +00:00
Support saving changes to storage locations
This commit is contained in:
@@ -26,6 +26,8 @@ import java.util.Map;
|
|||||||
import java.util.TreeMap;
|
import java.util.TreeMap;
|
||||||
import java.util.regex.Pattern;
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
|
import org.apache.commons.lang3.StringUtils;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* TODO: Write javadoc for this type.
|
* TODO: Write javadoc for this type.
|
||||||
*
|
*
|
||||||
@@ -69,13 +71,15 @@ public class FileSection {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static Map<String, String> parseToMap(final String line, final String kvSeparator, final String pairSeparator) {
|
public static Map<String, String> parseToMap(final String line, final String kvSeparator, final String pairSeparator) {
|
||||||
final String[] pairs = line.split(Pattern.quote(pairSeparator));
|
|
||||||
final Pattern splitter = Pattern.compile(Pattern.quote(kvSeparator));
|
|
||||||
Map<String, String> result = new TreeMap<String, String>(String.CASE_INSENSITIVE_ORDER);
|
Map<String, String> result = new TreeMap<String, String>(String.CASE_INSENSITIVE_ORDER);
|
||||||
|
if (!StringUtils.isEmpty(line)) {
|
||||||
|
final String[] pairs = line.split(Pattern.quote(pairSeparator));
|
||||||
|
final Pattern splitter = Pattern.compile(Pattern.quote(kvSeparator));
|
||||||
|
|
||||||
for (final String dd : pairs) {
|
for (final String dd : pairs) {
|
||||||
final String[] v = splitter.split(dd, 2);
|
final String[] v = splitter.split(dd, 2);
|
||||||
result.put(v[0].trim(), v.length > 1 ? v[1].trim() : "");
|
result.put(v[0].trim(), v.length > 1 ? v[1].trim() : "");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -92,18 +92,8 @@ public final class FileUtil {
|
|||||||
return dir.delete();
|
return dir.delete();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
public static void writeFile(String filename, String text) {
|
||||||
* <p>
|
FileUtil.writeFile(new File(filename), text);
|
||||||
* writeFile.
|
|
||||||
* </p>
|
|
||||||
*
|
|
||||||
* @param filename
|
|
||||||
* a {@link java.lang.String} object.
|
|
||||||
* @param data
|
|
||||||
* a {@link java.util.List} object.
|
|
||||||
*/
|
|
||||||
public static void writeFile(final String filename, final List<String> data) {
|
|
||||||
FileUtil.writeFile(new File(filename), data);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void writeFile(File file, String text) {
|
public static void writeFile(File file, String text) {
|
||||||
@@ -116,6 +106,20 @@ public final class FileUtil {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* <p>
|
||||||
|
* writeFile.
|
||||||
|
* </p>
|
||||||
|
*
|
||||||
|
* @param filename
|
||||||
|
* a {@link java.lang.String} object.
|
||||||
|
* @param data
|
||||||
|
* a {@link java.util.List} object.
|
||||||
|
*/
|
||||||
|
public static void writeFile(String filename, List<String> data) {
|
||||||
|
FileUtil.writeFile(new File(filename), data);
|
||||||
|
}
|
||||||
|
|
||||||
// writes each element of ArrayList on a separate line
|
// writes each element of ArrayList on a separate line
|
||||||
// this is used to write a file of Strings
|
// this is used to write a file of Strings
|
||||||
// this will create a new file if needed
|
// this will create a new file if needed
|
||||||
|
|||||||
@@ -46,7 +46,7 @@ public class ForgeProfileProperties {
|
|||||||
private static final String CACHE_DIR_KEY = "cacheDir";
|
private static final String CACHE_DIR_KEY = "cacheDir";
|
||||||
private static final String CARD_PICS_DIR_KEY = "cardPicsDir";
|
private static final String CARD_PICS_DIR_KEY = "cardPicsDir";
|
||||||
private static final String CARD_PICS_SUB_DIRS_KEY = "cardPicsSubDirs";
|
private static final String CARD_PICS_SUB_DIRS_KEY = "cardPicsSubDirs";
|
||||||
private static final String SERVER_PORT = "serverPort";
|
private static final String SERVER_PORT_KEY = "serverPort";
|
||||||
|
|
||||||
private ForgeProfileProperties() {
|
private ForgeProfileProperties() {
|
||||||
//prevent initializing static class
|
//prevent initializing static class
|
||||||
@@ -64,12 +64,12 @@ public class ForgeProfileProperties {
|
|||||||
System.err.println("error while reading from profile properties file");
|
System.err.println("error while reading from profile properties file");
|
||||||
}
|
}
|
||||||
|
|
||||||
Pair<String, String> defaults = _getDefaultDirs();
|
Pair<String, String> defaults = getDefaultDirs();
|
||||||
userDir = getDir(props, USER_DIR_KEY, defaults.getLeft());
|
userDir = getDir(props, USER_DIR_KEY, defaults.getLeft());
|
||||||
cacheDir = getDir(props, CACHE_DIR_KEY, defaults.getRight());
|
cacheDir = getDir(props, CACHE_DIR_KEY, defaults.getRight());
|
||||||
cardPicsDir = getDir(props, CARD_PICS_DIR_KEY, cacheDir + "pics/cards/");
|
cardPicsDir = getDir(props, CARD_PICS_DIR_KEY, cacheDir + "pics" + File.separator + "cards" + File.separator);
|
||||||
cardPicsSubDirs = getMap(props, CARD_PICS_SUB_DIRS_KEY);
|
cardPicsSubDirs = getMap(props, CARD_PICS_SUB_DIRS_KEY);
|
||||||
serverPort = getInt(props, SERVER_PORT, 0);
|
serverPort = getInt(props, SERVER_PORT_KEY, 0);
|
||||||
|
|
||||||
//ensure directories exist
|
//ensure directories exist
|
||||||
FileUtil.ensureDirectoryExists(userDir);
|
FileUtil.ensureDirectoryExists(userDir);
|
||||||
@@ -82,6 +82,7 @@ public class ForgeProfileProperties {
|
|||||||
}
|
}
|
||||||
public static void setUserDir(String userDir0) {
|
public static void setUserDir(String userDir0) {
|
||||||
userDir = userDir0;
|
userDir = userDir0;
|
||||||
|
save();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static String getCacheDir() {
|
public static String getCacheDir() {
|
||||||
@@ -93,6 +94,7 @@ public class ForgeProfileProperties {
|
|||||||
cardPicsDir = cacheDir0 + cardPicsDir.substring(idx + cacheDir.length());
|
cardPicsDir = cacheDir0 + cardPicsDir.substring(idx + cacheDir.length());
|
||||||
}
|
}
|
||||||
cacheDir = cacheDir0;
|
cacheDir = cacheDir0;
|
||||||
|
save();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static String getCardPicsDir() {
|
public static String getCardPicsDir() {
|
||||||
@@ -100,6 +102,7 @@ public class ForgeProfileProperties {
|
|||||||
}
|
}
|
||||||
public static void setCardPicsDir(String cardPicsDir0) {
|
public static void setCardPicsDir(String cardPicsDir0) {
|
||||||
cardPicsDir = cardPicsDir0;
|
cardPicsDir = cardPicsDir0;
|
||||||
|
save();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Map<String, String> getCardPicsSubDirs() {
|
public static Map<String, String> getCardPicsSubDirs() {
|
||||||
@@ -140,10 +143,10 @@ public class ForgeProfileProperties {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// returns a pair <userDir, cacheDir>
|
// returns a pair <userDir, cacheDir>
|
||||||
private static Pair<String, String> _getDefaultDirs() {
|
private static Pair<String, String> getDefaultDirs() {
|
||||||
if (!GuiBase.getInterface().isRunningOnDesktop()) { //special case for mobile devices
|
if (!GuiBase.getInterface().isRunningOnDesktop()) { //special case for mobile devices
|
||||||
String assetsDir = ForgeConstants.ASSETS_DIR;
|
String assetsDir = ForgeConstants.ASSETS_DIR;
|
||||||
return Pair.of(assetsDir + "data/", assetsDir + "cache/");
|
return Pair.of(assetsDir + "data" + File.separator, assetsDir + "cache" + File.separator);
|
||||||
}
|
}
|
||||||
|
|
||||||
String osName = System.getProperty("os.name");
|
String osName = System.getProperty("os.name");
|
||||||
@@ -167,12 +170,11 @@ public class ForgeProfileProperties {
|
|||||||
if (StringUtils.isEmpty(cacheRoot)) {
|
if (StringUtils.isEmpty(cacheRoot)) {
|
||||||
cacheRoot = appRoot;
|
cacheRoot = appRoot;
|
||||||
}
|
}
|
||||||
// just use '/' everywhere instead of file.separator. it always works
|
|
||||||
// the cache dir is Forge/Cache instead of just Forge since appRoot and cacheRoot might be the
|
// the cache dir is Forge/Cache instead of just Forge since appRoot and cacheRoot might be the
|
||||||
// same directory on windows and we need to distinguish them.
|
// same directory on windows and we need to distinguish them.
|
||||||
return Pair.of(String.format("%s/Forge", appRoot),
|
return Pair.of(appRoot + File.separator + "Forge", cacheRoot + File.separator + "Forge" + File.separator + "Cache");
|
||||||
String.format("%s/Forge/Cache", cacheRoot));
|
}
|
||||||
} else if (StringUtils.containsIgnoreCase(osName, "mac os x")) {
|
else if (StringUtils.containsIgnoreCase(osName, "mac os x")) {
|
||||||
return Pair.of(String.format("%s/Library/Application Support/Forge", homeDir),
|
return Pair.of(String.format("%s/Library/Application Support/Forge", homeDir),
|
||||||
String.format("%s/Library/Caches/Forge", homeDir));
|
String.format("%s/Library/Caches/Forge", homeDir));
|
||||||
}
|
}
|
||||||
@@ -180,4 +182,51 @@ public class ForgeProfileProperties {
|
|||||||
// Linux and everything else
|
// Linux and everything else
|
||||||
return Pair.of(fallbackDataDir, String.format("%s/.cache/forge", homeDir));
|
return Pair.of(fallbackDataDir, String.format("%s/.cache/forge", homeDir));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static void save() {
|
||||||
|
Pair<String, String> defaults = getDefaultDirs();
|
||||||
|
String defaultUserDir = defaults.getLeft() + File.separator;
|
||||||
|
String defaultCacheDir = defaults.getRight() + File.separator;
|
||||||
|
String defaultCardPicsDir = defaultCacheDir + "pics" + File.separator + "cards" + File.separator;
|
||||||
|
|
||||||
|
//only append values that aren't equal to defaults
|
||||||
|
StringBuilder sb = new StringBuilder();
|
||||||
|
if (!userDir.equals(defaultUserDir)) { //ensure backslashes are escaped
|
||||||
|
sb.append(USER_DIR_KEY + "=" + userDir.replace("\\", "\\\\") + "\n");
|
||||||
|
}
|
||||||
|
if (!cacheDir.equals(defaultCacheDir)) {
|
||||||
|
sb.append(CACHE_DIR_KEY + "=" + cacheDir.replace("\\", "\\\\") + "\n");
|
||||||
|
}
|
||||||
|
if (!cardPicsDir.equals(defaultCardPicsDir)) {
|
||||||
|
sb.append(CARD_PICS_DIR_KEY + "=" + cardPicsDir.replace("\\", "\\\\") + "\n");
|
||||||
|
}
|
||||||
|
if (cardPicsSubDirs.size() > 0) {
|
||||||
|
sb.append(CARD_PICS_SUB_DIRS_KEY + "=");
|
||||||
|
boolean needDelim = false;
|
||||||
|
for (Map.Entry<String, String> entry : cardPicsSubDirs.entrySet()) {
|
||||||
|
if (needDelim) {
|
||||||
|
sb.append("|");
|
||||||
|
}
|
||||||
|
sb.append(entry.getKey() + "->" + entry.getValue());
|
||||||
|
}
|
||||||
|
sb.append("\n");
|
||||||
|
}
|
||||||
|
if (serverPort != 0) {
|
||||||
|
sb.append(SERVER_PORT_KEY + "=" + serverPort);
|
||||||
|
}
|
||||||
|
if (sb.length() > 0) {
|
||||||
|
FileUtil.writeFile(ForgeConstants.PROFILE_FILE, sb.toString());
|
||||||
|
}
|
||||||
|
else { //delete file if empty
|
||||||
|
try {
|
||||||
|
File file = new File(ForgeConstants.PROFILE_FILE);
|
||||||
|
if (file.exists()) {
|
||||||
|
file.delete();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user