mirror of
https://github.com/Card-Forge/forge.git
synced 2025-11-18 19:58:00 +00:00
Support directory being included in Deck object
This commit is contained in:
@@ -19,17 +19,16 @@ package forge.deck;
|
||||
|
||||
import forge.item.InventoryItem;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.Serializable;
|
||||
|
||||
/**
|
||||
* TODO: Write javadoc for this type.
|
||||
*
|
||||
*/
|
||||
|
||||
public abstract class DeckBase implements Serializable, Comparable<DeckBase>, InventoryItem {
|
||||
private static final long serialVersionUID = -7538150536939660052L;
|
||||
// gameType is from Constant.GameType, like GameType.Regular
|
||||
|
||||
private final String name;
|
||||
private transient String directory;
|
||||
private String comment = null;
|
||||
|
||||
/**
|
||||
@@ -38,7 +37,7 @@ public abstract class DeckBase implements Serializable, Comparable<DeckBase>, In
|
||||
* @param name0 the name0
|
||||
*/
|
||||
public DeckBase(final String name0) {
|
||||
this.name = name0.replace('/', '_');
|
||||
name = name0.replace('/', '_');
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
@@ -46,7 +45,7 @@ public abstract class DeckBase implements Serializable, Comparable<DeckBase>, In
|
||||
*/
|
||||
@Override
|
||||
public int compareTo(final DeckBase d) {
|
||||
return this.getName().compareTo(d.getName());
|
||||
return name.compareTo(d.name);
|
||||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
@@ -54,7 +53,7 @@ public abstract class DeckBase implements Serializable, Comparable<DeckBase>, In
|
||||
public boolean equals(final Object o) {
|
||||
if (o instanceof DeckBase) {
|
||||
final DeckBase d = (DeckBase) o;
|
||||
return this.getName().equals(d.getName());
|
||||
return name.equals(d.name);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
@@ -66,19 +65,28 @@ public abstract class DeckBase implements Serializable, Comparable<DeckBase>, In
|
||||
*/
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return (this.name.hashCode() * 17) + this.name.hashCode();
|
||||
return (name.hashCode() * 17) + name.hashCode();
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see forge.util.IHasName#getName()
|
||||
*/
|
||||
public String getName() {
|
||||
return this.name;
|
||||
return name;
|
||||
}
|
||||
|
||||
public String getDirectory() {
|
||||
return directory;
|
||||
}
|
||||
public void setDirectory(File file, String rootDir) {
|
||||
directory = file.getParent().substring(rootDir.length());
|
||||
}
|
||||
|
||||
public String getUniqueKey() {
|
||||
if (directory == null) { return name; }
|
||||
return directory + "/" + name;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return this.name;
|
||||
return name;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -86,8 +94,8 @@ public abstract class DeckBase implements Serializable, Comparable<DeckBase>, In
|
||||
*
|
||||
* @param comment the new comment
|
||||
*/
|
||||
public void setComment(final String comment) {
|
||||
this.comment = comment;
|
||||
public void setComment(final String comment0) {
|
||||
comment = comment0;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -98,7 +106,7 @@ public abstract class DeckBase implements Serializable, Comparable<DeckBase>, In
|
||||
* @return a {@link java.lang.String} object.
|
||||
*/
|
||||
public String getComment() {
|
||||
return this.comment;
|
||||
return comment;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -115,7 +123,8 @@ public abstract class DeckBase implements Serializable, Comparable<DeckBase>, In
|
||||
* @param clone the clone
|
||||
*/
|
||||
protected void cloneFieldsTo(final DeckBase clone) {
|
||||
clone.comment = this.comment;
|
||||
clone.directory = directory;
|
||||
clone.comment = comment;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -125,8 +134,8 @@ public abstract class DeckBase implements Serializable, Comparable<DeckBase>, In
|
||||
* @return the deck base
|
||||
*/
|
||||
public DeckBase copyTo(final String name0) {
|
||||
final DeckBase obj = this.newInstance(name0);
|
||||
this.cloneFieldsTo(obj);
|
||||
final DeckBase obj = newInstance(name0);
|
||||
cloneFieldsTo(obj);
|
||||
return obj;
|
||||
}
|
||||
|
||||
@@ -136,7 +145,7 @@ public abstract class DeckBase implements Serializable, Comparable<DeckBase>, In
|
||||
* @return the best file name
|
||||
*/
|
||||
public final String getBestFileName() {
|
||||
return this.getName().replaceAll("[^-_$#@.,{[()]} a-zA-Z0-9]", "");
|
||||
return name.replaceAll("[^-_$#@.,{[()]} a-zA-Z0-9]", "");
|
||||
}
|
||||
|
||||
public abstract boolean isEmpty();
|
||||
|
||||
@@ -18,10 +18,12 @@
|
||||
package forge.deck.io;
|
||||
|
||||
import com.google.common.collect.ImmutableList;
|
||||
|
||||
import forge.deck.Deck;
|
||||
import forge.deck.DeckGroup;
|
||||
import forge.util.IItemSerializer;
|
||||
import forge.util.storage.StorageReaderFolder;
|
||||
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
||||
import java.io.File;
|
||||
@@ -33,15 +35,18 @@ import java.util.List;
|
||||
*
|
||||
*/
|
||||
public class DeckGroupSerializer extends StorageReaderFolder<DeckGroup> implements IItemSerializer<DeckGroup> {
|
||||
private final String humanDeckFile = "human.dck";
|
||||
private static final String humanDeckFile = "human.dck";
|
||||
|
||||
private final String rootDir;
|
||||
|
||||
/**
|
||||
* Instantiates a new deck group serializer.
|
||||
*
|
||||
* @param deckDir0 the deck dir0
|
||||
*/
|
||||
public DeckGroupSerializer(final File deckDir0) {
|
||||
public DeckGroupSerializer(final File deckDir0, String rootDir0) {
|
||||
super(deckDir0, DeckGroup.FN_NAME_SELECTOR);
|
||||
rootDir = rootDir0;
|
||||
}
|
||||
|
||||
/** The Constant MAX_DRAFT_PLAYERS. */
|
||||
@@ -54,9 +59,9 @@ public class DeckGroupSerializer extends StorageReaderFolder<DeckGroup> implemen
|
||||
*/
|
||||
@Override
|
||||
public void save(final DeckGroup unit) {
|
||||
final File f = this.makeFileFor(unit);
|
||||
final File f = makeFileFor(unit);
|
||||
f.mkdir();
|
||||
DeckSerializer.writeDeck(unit.getHumanDeck(), new File(f, this.humanDeckFile));
|
||||
DeckSerializer.writeDeck(unit.getHumanDeck(), new File(f, humanDeckFile));
|
||||
final List<Deck> aiDecks = unit.getAiDecks();
|
||||
for (int i = 1; i <= aiDecks.size(); i++) {
|
||||
DeckSerializer.writeDeck(aiDecks.get(i - 1), new File(f, "ai-" + i + ".dck"));
|
||||
@@ -68,21 +73,24 @@ public class DeckGroupSerializer extends StorageReaderFolder<DeckGroup> implemen
|
||||
*/
|
||||
@Override
|
||||
protected final DeckGroup read(final File file) {
|
||||
final Deck humanDeck = DeckSerializer.fromFile(new File(file, humanDeckFile));
|
||||
if (humanDeck == null) { return null; }
|
||||
|
||||
final Deck human = DeckSerializer.fromFile(new File(file, this.humanDeckFile));
|
||||
if (null == human) {
|
||||
return null;
|
||||
}
|
||||
humanDeck.setDirectory(file, rootDir);
|
||||
|
||||
final DeckGroup d = new DeckGroup(human.getName());
|
||||
d.setHumanDeck(human);
|
||||
final DeckGroup d = new DeckGroup(humanDeck.getName());
|
||||
d.setHumanDeck(humanDeck);
|
||||
for (int i = 1; i < DeckGroupSerializer.MAX_DRAFT_PLAYERS; i++) {
|
||||
final File theFile = new File(file, "ai-" + i + ".dck");
|
||||
if (!theFile.exists()) {
|
||||
break;
|
||||
}
|
||||
|
||||
d.addAiDeck(DeckSerializer.fromFile(theFile));
|
||||
Deck aiDeck = DeckSerializer.fromFile(theFile);
|
||||
if (aiDeck != null) {
|
||||
aiDeck.setDirectory(theFile, rootDir);
|
||||
d.addAiDeck(aiDeck);
|
||||
}
|
||||
}
|
||||
return d;
|
||||
}
|
||||
@@ -95,7 +103,7 @@ public class DeckGroupSerializer extends StorageReaderFolder<DeckGroup> implemen
|
||||
*/
|
||||
@Override
|
||||
public void erase(final DeckGroup unit) {
|
||||
final File dir = this.makeFileFor(unit);
|
||||
final File dir = makeFileFor(unit);
|
||||
final File[] files = dir.listFiles();
|
||||
for (final File f : files) {
|
||||
f.delete();
|
||||
@@ -110,7 +118,7 @@ public class DeckGroupSerializer extends StorageReaderFolder<DeckGroup> implemen
|
||||
* @return the file
|
||||
*/
|
||||
public File makeFileFor(final DeckGroup decks) {
|
||||
return new File(this.directory, decks.getBestFileName());
|
||||
return new File(directory, decks.getBestFileName());
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -127,7 +135,7 @@ public class DeckGroupSerializer extends StorageReaderFolder<DeckGroup> implemen
|
||||
final File testSubject = new File(dir, name);
|
||||
final boolean isVisibleFolder = testSubject.isDirectory() && !testSubject.isHidden();
|
||||
final boolean hasGoodName = StringUtils.isNotEmpty(name) && !name.startsWith(".");
|
||||
final File fileHumanDeck = new File(testSubject, DeckGroupSerializer.this.humanDeckFile);
|
||||
final File fileHumanDeck = new File(testSubject, DeckGroupSerializer.humanDeckFile);
|
||||
return isVisibleFolder && hasGoodName && fileHumanDeck.exists();
|
||||
}
|
||||
};
|
||||
|
||||
@@ -33,9 +33,11 @@ import java.util.Map;
|
||||
* This class knows how to make a file out of a deck object and vice versa.
|
||||
*/
|
||||
public class DeckStorage extends StorageReaderFolder<Deck> implements IItemSerializer<Deck> {
|
||||
private final boolean moveWronglyNamedDecks;
|
||||
public static final String FILE_EXTENSION = ".dck";
|
||||
|
||||
private final String rootDir;
|
||||
private final boolean moveWronglyNamedDecks;
|
||||
|
||||
/** Constant <code>DCKFileFilter</code>. */
|
||||
public static final FilenameFilter DCK_FILE_FILTER = new FilenameFilter() {
|
||||
@Override
|
||||
@@ -44,12 +46,13 @@ public class DeckStorage extends StorageReaderFolder<Deck> implements IItemSeria
|
||||
}
|
||||
};
|
||||
|
||||
public DeckStorage(final File deckDir0) {
|
||||
this(deckDir0, false);
|
||||
public DeckStorage(final File deckDir0, final String rootDir0) {
|
||||
this(deckDir0, rootDir0, false);
|
||||
}
|
||||
|
||||
public DeckStorage(final File deckDir0, boolean moveWrongDecks) {
|
||||
public DeckStorage(final File deckDir0, final String rootDir0, boolean moveWrongDecks) {
|
||||
super(deckDir0, Deck.FN_NAME_SELECTOR);
|
||||
rootDir = rootDir0;
|
||||
moveWronglyNamedDecks = moveWrongDecks;
|
||||
}
|
||||
|
||||
@@ -60,7 +63,7 @@ public class DeckStorage extends StorageReaderFolder<Deck> implements IItemSeria
|
||||
public IItemReader<Deck> getReaderForFolder(File subfolder) {
|
||||
if ( !subfolder.getParentFile().equals(directory) )
|
||||
throw new UnsupportedOperationException("Only child folders of " + directory + " may be processed");
|
||||
return new DeckStorage(subfolder, false);
|
||||
return new DeckStorage(subfolder, rootDir, false);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -85,6 +88,10 @@ public class DeckStorage extends StorageReaderFolder<Deck> implements IItemSeria
|
||||
if (moveWronglyNamedDecks) {
|
||||
adjustFileLocation(file, result);
|
||||
}
|
||||
|
||||
if (result != null) {
|
||||
result.setDirectory(file, rootDir);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
@@ -47,15 +47,15 @@ public class CardCollections {
|
||||
public CardCollections() {
|
||||
final StopWatch sw = new StopWatch();
|
||||
sw.start();
|
||||
constructed = new StorageImmediatelySerialized<Deck> ("Constructed decks", new DeckStorage(new File(ForgeConstants.DECK_CONSTRUCTED_DIR), true), true);
|
||||
draft = new StorageImmediatelySerialized<DeckGroup>("Draft deck sets", new DeckGroupSerializer(new File(ForgeConstants.DECK_DRAFT_DIR)));
|
||||
sealed = new StorageImmediatelySerialized<DeckGroup>("Sealed deck sets", new DeckGroupSerializer(new File(ForgeConstants.DECK_SEALED_DIR)));
|
||||
winston = new StorageImmediatelySerialized<DeckGroup>("Winston draft deck sets", new DeckGroupSerializer(new File(ForgeConstants.DECK_WINSTON_DIR)));
|
||||
cube = new StorageImmediatelySerialized<Deck> ("Cubes", new DeckStorage(new File(ForgeConstants.DECK_CUBE_DIR)));
|
||||
scheme = new StorageImmediatelySerialized<Deck> ("Archenemy decks", new DeckStorage(new File(ForgeConstants.DECK_SCHEME_DIR)));
|
||||
plane = new StorageImmediatelySerialized<Deck> ("Planechase decks", new DeckStorage(new File(ForgeConstants.DECK_PLANE_DIR)));
|
||||
commander = new StorageImmediatelySerialized<Deck> ("Commander decks", new DeckStorage(new File(ForgeConstants.DECK_COMMANDER_DIR)));
|
||||
tinyLeaders = new StorageImmediatelySerialized<Deck> ("Commander decks", new DeckStorage(new File(ForgeConstants.DECK_TINY_LEADERS_DIR)));
|
||||
constructed = new StorageImmediatelySerialized<Deck> ("Constructed decks", new DeckStorage(new File(ForgeConstants.DECK_CONSTRUCTED_DIR), ForgeConstants.DECK_BASE_DIR, true), true);
|
||||
draft = new StorageImmediatelySerialized<DeckGroup>("Draft deck sets", new DeckGroupSerializer(new File(ForgeConstants.DECK_DRAFT_DIR), ForgeConstants.DECK_BASE_DIR));
|
||||
sealed = new StorageImmediatelySerialized<DeckGroup>("Sealed deck sets", new DeckGroupSerializer(new File(ForgeConstants.DECK_SEALED_DIR), ForgeConstants.DECK_BASE_DIR));
|
||||
winston = new StorageImmediatelySerialized<DeckGroup>("Winston draft deck sets", new DeckGroupSerializer(new File(ForgeConstants.DECK_WINSTON_DIR), ForgeConstants.DECK_BASE_DIR));
|
||||
cube = new StorageImmediatelySerialized<Deck> ("Cubes", new DeckStorage(new File(ForgeConstants.DECK_CUBE_DIR), ForgeConstants.RES_DIR));
|
||||
scheme = new StorageImmediatelySerialized<Deck> ("Archenemy decks", new DeckStorage(new File(ForgeConstants.DECK_SCHEME_DIR), ForgeConstants.DECK_BASE_DIR));
|
||||
plane = new StorageImmediatelySerialized<Deck> ("Planechase decks", new DeckStorage(new File(ForgeConstants.DECK_PLANE_DIR), ForgeConstants.DECK_BASE_DIR));
|
||||
commander = new StorageImmediatelySerialized<Deck> ("Commander decks", new DeckStorage(new File(ForgeConstants.DECK_COMMANDER_DIR), ForgeConstants.DECK_BASE_DIR));
|
||||
tinyLeaders = new StorageImmediatelySerialized<Deck> ("Commander decks", new DeckStorage(new File(ForgeConstants.DECK_TINY_LEADERS_DIR), ForgeConstants.DECK_BASE_DIR));
|
||||
sw.stop();
|
||||
System.out.printf("Read decks (%d ms): %d constructed, %d sealed, %d draft, %d cubes, %d scheme, %d planar, %d commander, %d tiny leaders.%n", sw.getTime(), constructed.size(), sealed.size(), draft.size(), cube.size(), scheme.size(), plane.size(), commander.size(), tinyLeaders.size());
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user