mirror of
https://github.com/Card-Forge/forge.git
synced 2025-11-17 03:08:02 +00:00
Updated GameFormat reader to support nested folders plus user defined custom formats from their local user directory. Moved Digital formats to a subfolder to demonstrate
This commit is contained in:
@@ -0,0 +1,160 @@
|
|||||||
|
/*
|
||||||
|
* Forge: Play Magic: the Gathering.
|
||||||
|
* Copyright (C) 2011 Nate
|
||||||
|
*
|
||||||
|
* This program is free software: you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU General Public License as published by
|
||||||
|
* the Free Software Foundation, either version 3 of the License, or
|
||||||
|
* (at your option) any later version.
|
||||||
|
*
|
||||||
|
* This program is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
* GNU General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License
|
||||||
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
package forge.util.storage;
|
||||||
|
|
||||||
|
import com.google.common.base.Function;
|
||||||
|
import forge.util.TextUtil;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.FileFilter;
|
||||||
|
import java.io.FilenameFilter;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.util.*;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This class treats every file in the given folder as a source for a named
|
||||||
|
* object. The descendant should implement read method to deserialize a single
|
||||||
|
* item. So that readAll will return a map of Name => Object as read from disk
|
||||||
|
*
|
||||||
|
* @param <T> the generic type
|
||||||
|
*/
|
||||||
|
public abstract class StorageReaderRecursiveFolderWithUserFolder<T> extends StorageReaderBase<T> {
|
||||||
|
/**
|
||||||
|
* @return the directory
|
||||||
|
*/
|
||||||
|
public File getDirectory() {
|
||||||
|
return directory;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getFullPath() {
|
||||||
|
return directory.getPath();
|
||||||
|
}
|
||||||
|
|
||||||
|
protected final File directory;
|
||||||
|
protected final File userDirectory;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Instantiates a new storage reader folder.
|
||||||
|
*
|
||||||
|
* @param itemDir0 the item dir0
|
||||||
|
*/
|
||||||
|
public StorageReaderRecursiveFolderWithUserFolder(final File itemDir0, final File userItemDir0, Function<? super T, String> keySelector0) {
|
||||||
|
super(keySelector0);
|
||||||
|
|
||||||
|
this.directory = itemDir0;
|
||||||
|
this.userDirectory = userItemDir0;
|
||||||
|
|
||||||
|
if (this.directory == null) {
|
||||||
|
throw new IllegalArgumentException("No directory specified");
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
if (this.directory.isFile()) {
|
||||||
|
throw new IOException("Not a directory");
|
||||||
|
} else {
|
||||||
|
this.directory.mkdirs();
|
||||||
|
if (!this.directory.isDirectory()) {
|
||||||
|
throw new IOException("Directory can't be created");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (final IOException ex) {
|
||||||
|
throw new RuntimeException("StorageReaderFolder.ctor() error, " + ex.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public final List<String> objectsThatFailedToLoad = new ArrayList<String>();
|
||||||
|
|
||||||
|
/* (non-Javadoc)
|
||||||
|
* @see forge.util.IItemReader#readAll()
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public Map<String, T> readAll() {
|
||||||
|
final Map<String, T> result = new TreeMap<String, T>();
|
||||||
|
|
||||||
|
Collection<File> forgeFormats = listFileTree(directory);
|
||||||
|
Collection<File> customFormats = listFileTree(userDirectory);
|
||||||
|
|
||||||
|
forgeFormats.addAll(customFormats);
|
||||||
|
|
||||||
|
final File[] files = forgeFormats.toArray(new File[forgeFormats.size()]);
|
||||||
|
|
||||||
|
for (final File file : files) {
|
||||||
|
try {
|
||||||
|
final T newDeck = this.read(file);
|
||||||
|
if (null == newDeck) {
|
||||||
|
final String msg = "An object stored in " + file.getPath() + " failed to load.\nPlease submit this as a bug with the mentioned file/directory attached.";
|
||||||
|
throw new RuntimeException(msg);
|
||||||
|
}
|
||||||
|
|
||||||
|
String newKey = keySelector.apply(newDeck);
|
||||||
|
if (result.containsKey(newKey)) {
|
||||||
|
System.err.println("StorageReaderFolder: Overwriting an object with key " + newKey);
|
||||||
|
}
|
||||||
|
result.put(newKey, newDeck);
|
||||||
|
} catch (final NoSuchElementException ex) {
|
||||||
|
final String message = TextUtil.concatWithSpace( file.getName(),"failed to load because ----", ex.getMessage());
|
||||||
|
objectsThatFailedToLoad.add(message);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
private Collection<File> listFileTree(File dir) {
|
||||||
|
Set<File> fileTree = new HashSet<File>();
|
||||||
|
if(dir==null||dir.listFiles(getFileFilter())==null){
|
||||||
|
return fileTree;
|
||||||
|
}
|
||||||
|
for (File entry : dir.listFiles(getFileFilter())) {
|
||||||
|
if (entry.isFile()) fileTree.add(entry);
|
||||||
|
else fileTree.addAll(listFileTree(entry));
|
||||||
|
}
|
||||||
|
return fileTree;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Read the object from file.
|
||||||
|
*
|
||||||
|
* @param file the file
|
||||||
|
* @return the object deserialized by inherited class
|
||||||
|
*/
|
||||||
|
protected abstract T read(File file);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* TODO: Write javadoc for this method.
|
||||||
|
*
|
||||||
|
* @return FilenameFilter to pick only relevant objects for deserialization
|
||||||
|
*/
|
||||||
|
protected abstract FilenameFilter getFileFilter();
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getItemKey(T item) {
|
||||||
|
return keySelector.apply(item);
|
||||||
|
}
|
||||||
|
|
||||||
|
// methods handling nested folders are provided. It's up to consumer whether to use these or not.
|
||||||
|
@Override
|
||||||
|
public Iterable<File> getSubFolders() {
|
||||||
|
File[] list = this.directory.listFiles(new FileFilter() {
|
||||||
|
@Override
|
||||||
|
public boolean accept(File file) {
|
||||||
|
return file.isDirectory() && !file.isHidden();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
return Arrays.asList(list);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -35,6 +35,7 @@ import forge.util.FileSection;
|
|||||||
import forge.util.FileUtil;
|
import forge.util.FileUtil;
|
||||||
import forge.util.storage.StorageBase;
|
import forge.util.storage.StorageBase;
|
||||||
import forge.util.storage.StorageReaderFolder;
|
import forge.util.storage.StorageReaderFolder;
|
||||||
|
import forge.util.storage.StorageReaderRecursiveFolderWithUserFolder;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.FilenameFilter;
|
import java.io.FilenameFilter;
|
||||||
@@ -252,11 +253,11 @@ public class GameFormat implements Comparable<GameFormat> {
|
|||||||
return index;
|
return index;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static class Reader extends StorageReaderFolder<GameFormat> {
|
public static class Reader extends StorageReaderRecursiveFolderWithUserFolder<GameFormat> {
|
||||||
List<GameFormat> naturallyOrdered = new ArrayList<GameFormat>();
|
List<GameFormat> naturallyOrdered = new ArrayList<GameFormat>();
|
||||||
|
|
||||||
public Reader(File file0) {
|
public Reader(File forgeFormats, File customFormats) {
|
||||||
super(file0, GameFormat.FN_GET_NAME);
|
super(forgeFormats, customFormats, GameFormat.FN_GET_NAME);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -326,7 +327,7 @@ public class GameFormat implements Comparable<GameFormat> {
|
|||||||
public static final FilenameFilter TXT_FILE_FILTER = new FilenameFilter() {
|
public static final FilenameFilter TXT_FILE_FILTER = new FilenameFilter() {
|
||||||
@Override
|
@Override
|
||||||
public boolean accept(final File dir, final String name) {
|
public boolean accept(final File dir, final String name) {
|
||||||
return name.endsWith(".txt");
|
return name.endsWith(".txt") || dir.isDirectory();
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -166,7 +166,8 @@ public final class FModel {
|
|||||||
ForgePreferences.DEV_MODE = preferences.getPrefBoolean(FPref.DEV_MODE_ENABLED);
|
ForgePreferences.DEV_MODE = preferences.getPrefBoolean(FPref.DEV_MODE_ENABLED);
|
||||||
ForgePreferences.UPLOAD_DRAFT = ForgePreferences.NET_CONN;
|
ForgePreferences.UPLOAD_DRAFT = ForgePreferences.NET_CONN;
|
||||||
|
|
||||||
formats = new GameFormat.Collection(new GameFormat.Reader( new File(ForgeConstants.FORMATS_DATA_DIR)));
|
formats = new GameFormat.Collection(new GameFormat.Reader( new File(ForgeConstants.FORMATS_DATA_DIR),
|
||||||
|
new File(ForgeConstants.USER_FORMATS_DIR)));
|
||||||
|
|
||||||
magicDb.setStandardPredicate(formats.getStandard().getFilterRules());
|
magicDb.setStandardPredicate(formats.getStandard().getFilterRules());
|
||||||
magicDb.setModernPredicate(formats.getModern().getFilterRules());
|
magicDb.setModernPredicate(formats.getModern().getFilterRules());
|
||||||
|
|||||||
@@ -208,6 +208,7 @@ public final class ForgeConstants {
|
|||||||
public static final String USER_CONQUEST_DIR = USER_DIR + "conquest" + PATH_SEPARATOR;
|
public static final String USER_CONQUEST_DIR = USER_DIR + "conquest" + PATH_SEPARATOR;
|
||||||
public static final String USER_PREFS_DIR = USER_DIR + "preferences" + PATH_SEPARATOR;
|
public static final String USER_PREFS_DIR = USER_DIR + "preferences" + PATH_SEPARATOR;
|
||||||
public static final String USER_GAMES_DIR = USER_DIR + "games" + PATH_SEPARATOR;
|
public static final String USER_GAMES_DIR = USER_DIR + "games" + PATH_SEPARATOR;
|
||||||
|
public static final String USER_FORMATS_DIR = USER_DIR + "customformats" + PATH_SEPARATOR;
|
||||||
public static final String LOG_FILE = USER_DIR + "forge.log";
|
public static final String LOG_FILE = USER_DIR + "forge.log";
|
||||||
public static final String ACHIEVEMENTS_DIR = USER_DIR + "achievements" + PATH_SEPARATOR;
|
public static final String ACHIEVEMENTS_DIR = USER_DIR + "achievements" + PATH_SEPARATOR;
|
||||||
public static final String DECK_DRAFT_DIR = DECK_BASE_DIR + "draft" + PATH_SEPARATOR;
|
public static final String DECK_DRAFT_DIR = DECK_BASE_DIR + "draft" + PATH_SEPARATOR;
|
||||||
|
|||||||
Reference in New Issue
Block a user