Support displaying scripts in Workshop again

This commit is contained in:
drdev
2013-11-22 03:21:37 +00:00
parent 72d0cfdbbc
commit 31b5f4181a
8 changed files with 134 additions and 66 deletions

View File

@@ -2,8 +2,8 @@ package forge;
import java.util.List;
import forge.card.CardRules;
import forge.card.CardScriptInfo;
public interface ICardStorageReader{
List<CardRules> loadCards();
List<CardScriptInfo> loadCards();
}

View File

@@ -1,6 +1,7 @@
package forge;
import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
@@ -8,6 +9,7 @@ import java.util.TreeMap;
import forge.card.CardDb;
import forge.card.CardEdition;
import forge.card.CardRules;
import forge.card.CardScriptInfo;
import forge.card.PrintSheet;
import forge.item.FatPack;
import forge.item.SealedProduct;
@@ -29,26 +31,31 @@ public class StaticData {
private final IStorage<SealedProduct.Template> tournaments;
private final IStorage<FatPack.Template> fatPacks;
private final IStorage<PrintSheet> printSheets;
private final HashMap<CardRules, CardScriptInfo> scriptLookup;
private static StaticData lastInstance = null;
public StaticData(ICardStorageReader reader, String editionFolder, String blockDataFolder) {
this.scriptLookup = new HashMap<CardRules, CardScriptInfo>();
this.editions = new CardEdition.Collection(new CardEdition.Reader(new File(editionFolder)));
lastInstance = this;
final Map<String, CardRules> regularCards = new TreeMap<String, CardRules>(String.CASE_INSENSITIVE_ORDER);
final Map<String, CardRules> variantsCards = new TreeMap<String, CardRules>(String.CASE_INSENSITIVE_ORDER);
List<CardRules> rules = reader.loadCards();
for (CardRules card : rules) {
List<CardScriptInfo> cards = reader.loadCards();
for (CardScriptInfo card : cards) {
if (null == card) continue;
final String cardName = card.getName();
if ( card.isVariant() ) {
variantsCards.put(cardName, card);
final CardRules rules = card.getRules();
scriptLookup.put(rules, card);
final String cardName = rules.getName();
if (rules.isVariant()) {
variantsCards.put(cardName, rules);
}
else {
regularCards.put(cardName, card);
regularCards.put(cardName, rules);
}
}
@@ -62,7 +69,7 @@ public class StaticData {
this.printSheets = new StorageBase<PrintSheet>("Special print runs", new PrintSheet.Reader(new File(blockDataFolder, "printsheets.txt")));
}
public final static StaticData instance() {
public final static StaticData instance() {
return lastInstance;
}
@@ -100,4 +107,8 @@ public class StaticData {
public CardDb getVariantCards() {
return variantCards;
}
public CardScriptInfo getScriptInfo(CardRules rules) {
return scriptLookup.get(rules);
}
}

View File

@@ -0,0 +1,52 @@
/*
* Forge: Play Magic: the Gathering.
* Copyright (C) 2011 Forge Team
*
* 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.card;
import java.io.File;
/**
* Info pertaining to a card script file
*
*/
public final class CardScriptInfo {
private String text;
private File file;
private CardRules rules;
public CardScriptInfo(String text0, File file0, CardRules rules0) {
this.text = text0;
this.file = file0;
this.rules = rules0;
}
public String getText() {
return this.text;
}
public File getFile() {
return this.file;
}
public boolean canEdit() {
return this.file != null;
}
public CardRules getRules() {
return this.rules;
}
}

View File

@@ -112,11 +112,7 @@ public final class FileUtil {
} // writeAllDecks()
public static String readFileToString(String filename) {
StringBuilder s = new StringBuilder();
for (String line : readFile(filename)) {
s.append(line).append('\n');
}
return s.toString();
return TextUtil.join(readFile(filename), "\n");
}
public static List<String> readFile(final String filename) {

View File

@@ -43,7 +43,7 @@ public class TextUtil {
return mapAsString.toString();
}
public static String[] split(CharSequence input, char delimiter) {
public static String[] split(CharSequence input, char delimiter) {
return splitWithParenthesis(input, delimiter, Integer.MAX_VALUE, '\0', '\0', true);
}
@@ -68,7 +68,7 @@ public class TextUtil {
/**
* Split string separated by a single char delimiter, can take parenthesis in account
* It's faster than String.split, and allows parenthesis
* It's faster than String.split, and allows parenthesis
*/
public static String[] splitWithParenthesis(CharSequence input, char delimiter, int maxEntries, char openPar, char closePar, boolean skipEmpty) {
List<String> result = new ArrayList<String>();
@@ -98,7 +98,18 @@ public class TextUtil {
String[] toReturn = result.toArray(ArrayUtils.EMPTY_STRING_ARRAY);
return trimParenthesis ? StringUtils.stripAll(toReturn, String.valueOf(openPar)) : toReturn;
}
public static String join(Iterable<String> strs, String delim) {
StringBuilder sb = new StringBuilder();
for (String str : strs) {
if (sb.length() > 0) {
sb.append(delim);
}
sb.append(str);
}
return sb.toString();
}
/**
* Converts an enum value to a printable label but upcasing the first letter
* and lcasing all subsequent letters