mirror of
https://github.com/Card-Forge/forge.git
synced 2025-11-18 11:48:02 +00:00
Add better validation for Planar Conquest readers
This commit is contained in:
@@ -12,13 +12,24 @@ public abstract class FCollectionReader<T> {
|
||||
}
|
||||
|
||||
void readAll(FCollection<T> collection) {
|
||||
for (final String line : FileUtil.readFile(file)) {
|
||||
final T item = read(line);
|
||||
for (String line : FileUtil.readFile(file)) {
|
||||
line = line.trim();
|
||||
if (line.isEmpty()) {
|
||||
continue; //ignore blank or whitespace lines
|
||||
}
|
||||
|
||||
T item = read(line);
|
||||
if (item != null) {
|
||||
collection.add(item);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected void alertInvalidLine(String line, String message) {
|
||||
System.err.println(message);
|
||||
System.err.println(line);
|
||||
System.err.println(file.getPath());
|
||||
}
|
||||
|
||||
protected abstract T read(String line);
|
||||
}
|
||||
|
||||
@@ -36,22 +36,10 @@ import java.util.TreeMap;
|
||||
public abstract class StorageReaderFile<T> extends StorageReaderBase<T> {
|
||||
protected final File file;
|
||||
|
||||
/**
|
||||
* Instantiates a new storage reader file.
|
||||
*
|
||||
* @param pathname the pathname
|
||||
* @param keySelector0 the key selector0
|
||||
*/
|
||||
public StorageReaderFile(final String pathname, final Function<? super T, String> keySelector0) {
|
||||
this(new File(pathname), keySelector0);
|
||||
}
|
||||
|
||||
/**
|
||||
* Instantiates a new storage reader file.
|
||||
*
|
||||
* @param file0 the file0
|
||||
* @param keySelector0 the key selector0
|
||||
*/
|
||||
public StorageReaderFile(final File file0, final Function<? super T, String> keySelector0) {
|
||||
super(keySelector0);
|
||||
file = file0;
|
||||
@@ -62,23 +50,24 @@ public abstract class StorageReaderFile<T> extends StorageReaderBase<T> {
|
||||
return file.getPath();
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see forge.util.IItemReader#readAll()
|
||||
*/
|
||||
@Override
|
||||
public Map<String, T> readAll() {
|
||||
final Map<String, T> result = new TreeMap<String, T>();
|
||||
|
||||
int idx = 0;
|
||||
for (final String s : FileUtil.readFile(file)) {
|
||||
if (!lineContainsObject(s)) {
|
||||
for (String line : FileUtil.readFile(file)) {
|
||||
line = line.trim();
|
||||
if (line.isEmpty()) {
|
||||
continue; //ignore blank or whitespace lines
|
||||
}
|
||||
|
||||
if (!lineContainsObject(line)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
final T item = read(s, idx);
|
||||
if (null == item) {
|
||||
final String msg = "An object stored in " + file.getPath() + " failed to load.\nPlease submit this as a bug with the mentioned file attached.";
|
||||
throw new RuntimeException(msg);
|
||||
T item = read(line, idx);
|
||||
if (item == null) {
|
||||
continue;
|
||||
}
|
||||
|
||||
idx++;
|
||||
@@ -92,31 +81,20 @@ public abstract class StorageReaderFile<T> extends StorageReaderBase<T> {
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* TODO: Write javadoc for this method.
|
||||
*
|
||||
* @param line
|
||||
* the line
|
||||
* @return the t
|
||||
*/
|
||||
protected abstract T read(String line, int idx);
|
||||
|
||||
/**
|
||||
* Line contains object.
|
||||
*
|
||||
* @param line
|
||||
* the line
|
||||
* @return true, if successful
|
||||
*/
|
||||
protected boolean lineContainsObject(final String line) {
|
||||
return !StringUtils.isBlank(line) && !line.trim().startsWith("#");
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see forge.util.IItemReader#getItemKey(java.lang.Object)
|
||||
*/
|
||||
@Override
|
||||
public String getItemKey(final T item) {
|
||||
return keySelector.apply(item);
|
||||
}
|
||||
|
||||
protected void alertInvalidLine(String line, String message) {
|
||||
System.err.println(message);
|
||||
System.err.println(line);
|
||||
System.err.println(file.getPath());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -238,7 +238,7 @@ public final class CardBlock implements Comparable<CardBlock> {
|
||||
*/
|
||||
@Override
|
||||
protected CardBlock read(String line, int i) {
|
||||
final String[] sParts = TextUtil.splitWithParenthesis(line.trim(), ',', 3);
|
||||
final String[] sParts = TextUtil.splitWithParenthesis(line, ',', 3);
|
||||
String name = sParts[0];
|
||||
|
||||
String[] numbers = sParts[1].trim().split("/");
|
||||
|
||||
@@ -103,11 +103,19 @@ public class ConquestEvent {
|
||||
String avatar = null;
|
||||
String description = null;
|
||||
|
||||
String[] pieces = line.trim().split("\\|");
|
||||
String key, value;
|
||||
String[] pieces = line.split("\\|");
|
||||
for (String piece : pieces) {
|
||||
String[] kv = piece.split(":", 2);
|
||||
String key = kv[0].trim().toLowerCase();
|
||||
String value = kv[1].trim();
|
||||
int idx = piece.indexOf(':');
|
||||
if (idx != -1) {
|
||||
key = piece.substring(0, idx).trim().toLowerCase();
|
||||
value = piece.substring(idx + 1).trim();
|
||||
}
|
||||
else {
|
||||
alertInvalidLine(line, "Invalid event definition.");
|
||||
key = piece.trim().toLowerCase();
|
||||
value = "";
|
||||
}
|
||||
switch(key) {
|
||||
case "name":
|
||||
name = value;
|
||||
@@ -133,6 +141,9 @@ public class ConquestEvent {
|
||||
case "desc":
|
||||
description = value;
|
||||
break;
|
||||
default:
|
||||
alertInvalidLine(line, "Invalid event definition.");
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (deck == null) {
|
||||
|
||||
@@ -243,11 +243,19 @@ public class ConquestPlane {
|
||||
String description = null;
|
||||
boolean unreachable = false;
|
||||
|
||||
String[] pieces = line.trim().split("\\|");
|
||||
String key, value;
|
||||
String[] pieces = line.split("\\|");
|
||||
for (String piece : pieces) {
|
||||
String[] kv = piece.split(":", 2);
|
||||
String key = kv[0].trim().toLowerCase();
|
||||
String value = kv[1].trim();
|
||||
int idx = piece.indexOf(':');
|
||||
if (idx != -1) {
|
||||
key = piece.substring(0, idx).trim().toLowerCase();
|
||||
value = piece.substring(idx + 1).trim();
|
||||
}
|
||||
else {
|
||||
alertInvalidLine(line, "Invalid plane definition.");
|
||||
key = piece.trim().toLowerCase();
|
||||
value = "";
|
||||
}
|
||||
switch(key) {
|
||||
case "name":
|
||||
name = value;
|
||||
@@ -266,6 +274,9 @@ public class ConquestPlane {
|
||||
case "desc":
|
||||
description = value;
|
||||
break;
|
||||
default:
|
||||
alertInvalidLine(line, "Invalid plane definition.");
|
||||
break;
|
||||
}
|
||||
}
|
||||
return new ConquestPlane(name, description, regionSize, unreachable);
|
||||
|
||||
@@ -90,11 +90,19 @@ public class ConquestRegion {
|
||||
ColorSet colorSet = ColorSet.ALL_COLORS;
|
||||
Predicate<PaperCard> pred = Predicates.alwaysTrue();
|
||||
|
||||
String[] pieces = line.trim().split("\\|");
|
||||
String key, value;
|
||||
String[] pieces = line.split("\\|");
|
||||
for (String piece : pieces) {
|
||||
String[] kv = piece.split(":", 2);
|
||||
String key = kv[0].trim().toLowerCase();
|
||||
String value = kv[1].trim();
|
||||
int idx = piece.indexOf(':');
|
||||
if (idx != -1) {
|
||||
key = piece.substring(0, idx).trim().toLowerCase();
|
||||
value = piece.substring(idx + 1).trim();
|
||||
}
|
||||
else {
|
||||
alertInvalidLine(line, "Invalid plane definition.");
|
||||
key = piece.trim().toLowerCase();
|
||||
value = "";
|
||||
}
|
||||
switch(key) {
|
||||
case "name":
|
||||
name = value;
|
||||
@@ -129,6 +137,9 @@ public class ConquestRegion {
|
||||
}
|
||||
};
|
||||
break;
|
||||
default:
|
||||
alertInvalidLine(line, "Invalid region definition.");
|
||||
break;
|
||||
}
|
||||
}
|
||||
return new ConquestRegion(plane, name, artCardName, colorSet, pred);
|
||||
|
||||
@@ -18,7 +18,6 @@
|
||||
package forge.quest;
|
||||
|
||||
import com.google.common.base.Function;
|
||||
|
||||
import forge.deck.Deck;
|
||||
import forge.game.GameFormat;
|
||||
import forge.item.PaperCard;
|
||||
@@ -131,28 +130,41 @@ public class QuestWorld implements Comparable<QuestWorld>{
|
||||
final List<String> sets = new ArrayList<String>();
|
||||
final List<String> bannedCards = new ArrayList<String>(); // if both empty, no format
|
||||
|
||||
// This is what you need to use here =>
|
||||
// FileSection.parse(line, ":", "|");
|
||||
|
||||
final String[] sParts = line.trim().split("\\|");
|
||||
|
||||
for (final String sPart : sParts) {
|
||||
|
||||
final String[] kv = sPart.split(":", 2);
|
||||
final String key = kv[0].toLowerCase();
|
||||
if ("name".equals(key)) {
|
||||
useName = kv[1];
|
||||
} else if ("dir".equals(key)) {
|
||||
useDir = kv[1];
|
||||
} else if ("sets".equals(key)) {
|
||||
sets.addAll(Arrays.asList(kv[1].split(", ")));
|
||||
} else if ("banned".equals(key)) {
|
||||
bannedCards.addAll(Arrays.asList(kv[1].split("; ")));
|
||||
String key, value;
|
||||
String[] pieces = line.split("\\|");
|
||||
for (String piece : pieces) {
|
||||
int idx = piece.indexOf(':');
|
||||
if (idx != -1) {
|
||||
key = piece.substring(0, idx).trim().toLowerCase();
|
||||
value = piece.substring(idx + 1).trim();
|
||||
}
|
||||
else {
|
||||
alertInvalidLine(line, "Invalid plane definition.");
|
||||
key = piece.trim().toLowerCase();
|
||||
value = "";
|
||||
}
|
||||
switch(key) {
|
||||
case "name":
|
||||
useName = value;
|
||||
break;
|
||||
case "dir":
|
||||
useDir = value;
|
||||
break;
|
||||
case "sets":
|
||||
sets.addAll(Arrays.asList(value.split(", ")));
|
||||
break;
|
||||
case "banned":
|
||||
bannedCards.addAll(Arrays.asList(value.split("; ")));
|
||||
break;
|
||||
default:
|
||||
alertInvalidLine(line, "Invalid quest world definition.");
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (useName == null) {
|
||||
throw new RuntimeException("A Quest World must have a name! Check worlds.txt file");
|
||||
alertInvalidLine(line, "Quest world must have a name.");
|
||||
return null;
|
||||
}
|
||||
|
||||
if (!sets.isEmpty() || !bannedCards.isEmpty()) {
|
||||
|
||||
Reference in New Issue
Block a user