CardReader.readCard is a public static function now. This will allow to store pets' characteristics as card description txt files.

This commit is contained in:
Maxmtg
2012-03-13 06:17:34 +00:00
parent c5a353095d
commit d45a92e748
9 changed files with 329 additions and 190 deletions

1
.gitattributes vendored
View File

@@ -11637,6 +11637,7 @@ src/main/java/forge/util/IItemSerializer.java -text
src/main/java/forge/util/IStorage.java -text
src/main/java/forge/util/IStorageView.java -text
src/main/java/forge/util/IgnoringXStream.java -text
src/main/java/forge/util/LineReader.java -text
src/main/java/forge/util/MyRandom.java svneol=native#text/plain
src/main/java/forge/util/Predicate.java -text
src/main/java/forge/util/PredicateString.java -text

View File

@@ -51,6 +51,8 @@ import forge.card.replacement.ReplacementHandler;
import forge.card.trigger.TriggerHandler;
import forge.error.ErrorViewer;
import forge.gui.toolbox.FProgressBar;
import forge.util.FileUtil;
import forge.util.LineReader;
import forge.view.SplashFrame;
/**
@@ -341,35 +343,6 @@ public class CardReader implements Runnable {
}
}
/**
* <p>
* Reads a line from the given reader and handles exceptions.
* </p>
*
* @param reader
* a {@link java.io.BufferedReader} object.
* @return a {@link java.lang.String} object.
*/
public static String readLine(final BufferedReader reader) {
// makes the checked exception, into an unchecked runtime exception
try {
String line = reader.readLine();
if (line != null) {
line = line.trim();
}
return line;
} catch (final Exception ex) {
ErrorViewer.showError(ex);
throw new RuntimeException("CardReader : readLine(Card) error", ex);
// by
// Braids
// on
// 8/18/11
// 10:53
// PM
}
} // readLine(BufferedReader)
/**
* <p>
* load a card.
@@ -381,27 +354,52 @@ public class CardReader implements Runnable {
* @return the card loaded from the stream
*/
protected final Card loadCard(final InputStream inputStream) {
final Card card = new Card();
this.rulesReader.reset();
InputStreamReader inputStreamReader = null;
BufferedReader reader = null;
try {
inputStreamReader = new InputStreamReader(inputStream, this.charset);
reader = new BufferedReader(inputStreamReader);
Card card = readCard( new LineReader(inputStream, this.charset), this.rulesReader, mapToFill );
String line = CardReader.readLine(reader);
while (!"End".equals(line)) {
this.rulesReader.parseLine(line);
if (line.isEmpty()) {
// Ignore empty lines.
} else if (line.charAt(0) == '#') {
// 8/18/11 10:59 PM
// no need to do anything, this indicates a comment line
} else if (line.startsWith("Name:")) {
if (card.isInAlternateState()) {
card.setState("Original");
}
this.listRulesToFill.add(this.rulesReader.getCard());
this.mapToFill.put(card.getName(), card);
return card;
}
public static Card readCard(Iterable<String> lines)
{
return readCard(lines, null, null);
}
/**
* Returns the card read from input stream
* @param lines are input lines
* @param rulesReader is used to fill CardPrinted characteristics
* @param mapToFill is used to eliminate duplicates
* @return the card
*/
public static Card readCard(Iterable<String> lines, CardRulesReader rulesReader, Map<String, Card> mapToFill) {
final Card card = new Card();
boolean ignoreTheRest = false;
for(String line : lines ) {
line = line.trim();
if("End".equals(line)) { ignoreTheRest = true; continue; }
if(ignoreTheRest) { continue; } // have to deplete the iterator
// otherwise the underlying class would close its stream on finalize only
if (line.isEmpty() || line.charAt(0) == '#')
continue;
if ( null != rulesReader )
rulesReader.parseLine(line);
if (line.startsWith("Name:")) {
final String value = line.substring(5);
// System.out.println(s);
if (this.mapToFill.containsKey(value)) {
if (mapToFill != null && mapToFill.containsKey(value)) {
break; // this card has already been loaded.
} else {
card.setName(value);
@@ -500,30 +498,7 @@ public class CardReader implements Runnable {
card.setColor(newCols);
card.setCardColorsOverridden(true);
}
line = CardReader.readLine(reader);
} // while !End
} finally {
try {
reader.close();
} catch (final IOException ignored) {
// 11:08
// PM
}
try {
inputStreamReader.close();
} catch (final IOException ignored) {
// 11:08
// PM
}
}
if (card.isInAlternateState()) {
card.setState("Original");
}
this.listRulesToFill.add(this.rulesReader.getCard());
this.mapToFill.put(card.getName(), card);
return card;
}

View File

@@ -19,6 +19,7 @@ package forge.game.limited;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.TreeMap;
@@ -36,7 +37,7 @@ public class CardRatings {
private static Map<String, Integer> blockRatings = new TreeMap<String, Integer>();
private static Map<String, Integer> customRatings = new TreeMap<String, Integer>();
private static ArrayList<String> tempRatings = new ArrayList<String>();
private static List<String> tempRatings = new ArrayList<String>();
/**
* Instantiates a new card ratings.
@@ -60,7 +61,7 @@ public class CardRatings {
}
private void loadFullRatings() {
final ArrayList<String> sRatings = FileUtil.readFile("res/draft/fullRatings.dat");
final List<String> sRatings = FileUtil.readFile("res/draft/fullRatings.dat");
if (sRatings.size() > 1) {
for (final String s : sRatings) {
if (s.length() > 3) {
@@ -74,7 +75,7 @@ public class CardRatings {
}
private void loadBlockRatings() {
final ArrayList<String> sRatings = FileUtil.readFile("res/draft/blockRatings.dat");
final List<String> sRatings = FileUtil.readFile("res/draft/blockRatings.dat");
if (sRatings.size() > 1) {
for (final String s : sRatings) {
if (s.length() > 3) {
@@ -88,7 +89,7 @@ public class CardRatings {
}
private void loadCustomRatings() {
final ArrayList<String> sRatings = FileUtil.readFile("res/draft/customRatings.dat");
final List<String> sRatings = FileUtil.readFile("res/draft/customRatings.dat");
if (sRatings.size() > 1) {
for (final String s : sRatings) {
if (s.length() > 3) {

View File

@@ -136,7 +136,7 @@ public class SealedDeck {
for (final String element : dList) {
if (element.endsWith(".sealed")) {
final ArrayList<String> dfData = FileUtil.readFile("res/sealed/" + element);
final List<String> dfData = FileUtil.readFile("res/sealed/" + element);
final CustomLimited cs = CustomLimited.parse(dfData, Singletons.getModel().getDecks().getCubes());
customs.add(cs);
}

View File

@@ -181,7 +181,7 @@ public enum FModel {
*/
public static void loadDynamicGamedata() {
if (!Constant.CardTypes.LOADED[0]) {
final ArrayList<String> typeListFile = FileUtil.readFile("res/gamedata/TypeLists.txt");
final List<String> typeListFile = FileUtil.readFile("res/gamedata/TypeLists.txt");
ArrayList<String> tList = null;
@@ -262,7 +262,7 @@ public enum FModel {
}
if (!Constant.Keywords.LOADED[0]) {
final ArrayList<String> nskwListFile = FileUtil.readFile("res/gamedata/NonStackingKWList.txt");
final List<String> nskwListFile = FileUtil.readFile("res/gamedata/NonStackingKWList.txt");
Constant.Keywords.NON_STACKING_LIST[0] = new ConstantStringArrayList();

View File

@@ -92,6 +92,7 @@ public class QuestPetPlant extends QuestPetAbstract {
petCard.setBaseDefense(4);
petCard.addIntrinsicKeyword("Deathtouch");
// A:AB$ GainLife | Cost$ T | LifeAmount$ 1 | SpellDescription$ You gain 1 life.
final Cost abCost = new Cost("T", petCard.getName(), true);
final SpellAbility ability = new AbilityActivated(petCard, abCost, null) {
private static final long serialVersionUID = 7546242087593613719L;

View File

@@ -26,6 +26,7 @@ import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Reader;
import java.net.Proxy;
import java.net.URL;
import java.util.ArrayList;
@@ -116,7 +117,7 @@ public final class FileUtil {
* a {@link java.lang.String} object.
* @return a {@link java.util.ArrayList} object.
*/
public static ArrayList<String> readFile(final String filename) {
public static List<String> readFile(final String filename) {
return FileUtil.readFile(new File(filename));
}
@@ -132,30 +133,47 @@ public final class FileUtil {
* a {@link java.io.File} object.
* @return a {@link java.util.ArrayList} object.
*/
public static ArrayList<String> readFile(final File file) {
final ArrayList<String> list = new ArrayList<String>();
BufferedReader in;
public static List<String> readFile(final File file) {
try {
if ((file == null) || !file.exists()) {
return list;
return new ArrayList<String>();
}
in = new BufferedReader(new FileReader(file));
String line;
while ((line = in.readLine()) != null) {
list.add(line);
}
in.close();
return readAllLines(new FileReader(file), false);
} catch (final Exception ex) {
ErrorViewer.showError(ex);
throw new RuntimeException("FileUtil : readFile() error, " + ex);
}
return list;
} // readFile()
public static List<String> readAllLines(Reader reader){
return readAllLines(reader, false);
}
/**
* Reads all lines from given reader to a list of strings
* @param reader is a reader (e.g. FileReader, InputStreamReader)
* @param mayTrim defines whether to trim lines.
* @return list of strings
*/
public static List<String> readAllLines(Reader reader, boolean mayTrim){
final ArrayList<String> list = new ArrayList<String>();
try {
BufferedReader in = new BufferedReader(reader);
String line;
while ((line = in.readLine()) != null) {
if ( mayTrim ) {
line = line.trim();
}
list.add(line);
}
in.close();
} catch (IOException ex ) {
ErrorViewer.showError(ex);
throw new RuntimeException("FileUtil : readAllLines() error, " + ex);
}
return list;
}
/**
* Download url into file.
*

View File

@@ -0,0 +1,145 @@
package forge.util;
/**
* TODO: Write javadoc for this type.
*
*/
import java.io.BufferedReader;
import java.io.Closeable;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.NoSuchElementException;
/**
* Represents the lines found in an {@link InputStream}. The lines are read
* one at a time using {@link BufferedReader#readLine()} and may be streamed
* through an iterator or returned all at once.
*
* <p>This class does not handle any concurrency issues.
*
* <p>The stream is closed automatically when the for loop is done :)
*
* <pre>{@code
* for(String line : new LineReader(stream))
* // ...
* }</pre>
*
* <p>An {@link IllegalStateException} will be thrown if any {@link IOException}s
* occur when reading or closing the stream.
*
* @author Torleif Berger
* @license http://creativecommons.org/licenses/by/3.0/
* @see http://www.geekality.net/?p=1614
*/
public class LineReader implements Iterable<String>, Closeable
{
private BufferedReader reader;
public LineReader(InputStream stream) { this(stream, null); }
public LineReader(InputStream stream, Charset charset)
{
reader = new BufferedReader(new InputStreamReader(stream, charset));
}
/**
* Closes the underlying stream.
*/
@Override
public void close() throws IOException
{
reader.close();
}
/**
* Makes sure the underlying stream is closed.
*/
@Override
protected void finalize() throws Throwable
{
close();
}
/**
* Returns an iterator over the lines remaining to be read.
*
* <p>The underlying stream is closed automatically once {@link Iterator#hasNext()}
* returns false. This means that the stream should be closed after using a for loop.
*
* @return This iterator.
*/
@Override
public Iterator<String> iterator()
{
return new LineIterator();
}
/**
* Returns all lines remaining to be read and closes the stream.
*
* @return The lines read from the stream.
*/
public Collection<String> readLines()
{
Collection<String> lines = new ArrayList<String>();
for(String line : this)
{
lines.add(line);
}
return lines;
}
private class LineIterator implements Iterator<String>
{
private String nextLine;
public String bufferNext()
{
try
{
return nextLine = reader.readLine();
}
catch (IOException e)
{
throw new IllegalStateException("I/O error while reading stream.", e);
}
}
public boolean hasNext()
{
boolean hasNext = nextLine != null || bufferNext() != null;
if ( ! hasNext)
try
{
reader.close();
}
catch (IOException e)
{
throw new IllegalStateException("I/O error when closing stream.", e);
}
return hasNext;
}
public String next()
{
if ( ! hasNext())
throw new NoSuchElementException();
String result = nextLine;
nextLine = null;
return result;
}
public void remove() {
throw new UnsupportedOperationException();
}
}
}

View File

@@ -18,7 +18,6 @@
package forge.util;
import java.io.File;
import java.util.ArrayList;
import java.util.Map;
import java.util.TreeMap;
@@ -55,9 +54,8 @@ public abstract class StorageReaderFile<T> implements IItemReader<T> {
@Override
public Map<String, T> readAll() {
final Map<String, T> result = new TreeMap<String, T>();
final ArrayList<String> fData = FileUtil.readFile(this.file);
for (final String s : fData) {
for (final String s : FileUtil.readFile(this.file)) {
if (!this.lineContainsObject(s)) {
continue;
}