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

1
.gitattributes vendored
View File

@@ -32,6 +32,7 @@ forge-core/src/main/java/forge/card/CardFace.java -text
forge-core/src/main/java/forge/card/CardRarity.java -text forge-core/src/main/java/forge/card/CardRarity.java -text
forge-core/src/main/java/forge/card/CardRules.java -text forge-core/src/main/java/forge/card/CardRules.java -text
forge-core/src/main/java/forge/card/CardRulesPredicates.java -text forge-core/src/main/java/forge/card/CardRulesPredicates.java -text
forge-core/src/main/java/forge/card/CardScriptInfo.java -text
forge-core/src/main/java/forge/card/CardSplitType.java -text forge-core/src/main/java/forge/card/CardSplitType.java -text
forge-core/src/main/java/forge/card/CardType.java -text forge-core/src/main/java/forge/card/CardType.java -text
forge-core/src/main/java/forge/card/ColorSet.java -text forge-core/src/main/java/forge/card/ColorSet.java -text

View File

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

View File

@@ -1,6 +1,7 @@
package forge; package forge;
import java.io.File; import java.io.File;
import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.TreeMap; import java.util.TreeMap;
@@ -8,6 +9,7 @@ import java.util.TreeMap;
import forge.card.CardDb; import forge.card.CardDb;
import forge.card.CardEdition; import forge.card.CardEdition;
import forge.card.CardRules; import forge.card.CardRules;
import forge.card.CardScriptInfo;
import forge.card.PrintSheet; import forge.card.PrintSheet;
import forge.item.FatPack; import forge.item.FatPack;
import forge.item.SealedProduct; import forge.item.SealedProduct;
@@ -29,26 +31,31 @@ public class StaticData {
private final IStorage<SealedProduct.Template> tournaments; private final IStorage<SealedProduct.Template> tournaments;
private final IStorage<FatPack.Template> fatPacks; private final IStorage<FatPack.Template> fatPacks;
private final IStorage<PrintSheet> printSheets; private final IStorage<PrintSheet> printSheets;
private final HashMap<CardRules, CardScriptInfo> scriptLookup;
private static StaticData lastInstance = null; private static StaticData lastInstance = null;
public StaticData(ICardStorageReader reader, String editionFolder, String blockDataFolder) { 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))); this.editions = new CardEdition.Collection(new CardEdition.Reader(new File(editionFolder)));
lastInstance = this; lastInstance = this;
final Map<String, CardRules> regularCards = new TreeMap<String, CardRules>(String.CASE_INSENSITIVE_ORDER); 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); final Map<String, CardRules> variantsCards = new TreeMap<String, CardRules>(String.CASE_INSENSITIVE_ORDER);
List<CardRules> rules = reader.loadCards(); List<CardScriptInfo> cards = reader.loadCards();
for (CardRules card : rules) { for (CardScriptInfo card : cards) {
if (null == card) continue; if (null == card) continue;
final String cardName = card.getName(); final CardRules rules = card.getRules();
if ( card.isVariant() ) { scriptLookup.put(rules, card);
variantsCards.put(cardName, card);
final String cardName = rules.getName();
if (rules.isVariant()) {
variantsCards.put(cardName, rules);
} }
else { 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"))); 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; return lastInstance;
} }
@@ -100,4 +107,8 @@ public class StaticData {
public CardDb getVariantCards() { public CardDb getVariantCards() {
return variantCards; 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() } // writeAllDecks()
public static String readFileToString(String filename) { public static String readFileToString(String filename) {
StringBuilder s = new StringBuilder(); return TextUtil.join(readFile(filename), "\n");
for (String line : readFile(filename)) {
s.append(line).append('\n');
}
return s.toString();
} }
public static List<String> readFile(final String filename) { public static List<String> readFile(final String filename) {

View File

@@ -43,7 +43,7 @@ public class TextUtil {
return mapAsString.toString(); 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); 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 * 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) { public static String[] splitWithParenthesis(CharSequence input, char delimiter, int maxEntries, char openPar, char closePar, boolean skipEmpty) {
List<String> result = new ArrayList<String>(); List<String> result = new ArrayList<String>();
@@ -98,7 +98,18 @@ public class TextUtil {
String[] toReturn = result.toArray(ArrayUtils.EMPTY_STRING_ARRAY); String[] toReturn = result.toArray(ArrayUtils.EMPTY_STRING_ARRAY);
return trimParenthesis ? StringUtils.stripAll(toReturn, String.valueOf(openPar)) : toReturn; 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 * Converts an enum value to a printable label but upcasing the first letter
* and lcasing all subsequent letters * and lcasing all subsequent letters

View File

@@ -41,7 +41,9 @@ import forge.FThreads;
import forge.ICardStorageReader; import forge.ICardStorageReader;
import forge.IProgressObserver; import forge.IProgressObserver;
import forge.card.CardRules; import forge.card.CardRules;
import forge.card.CardScriptInfo;
import forge.util.FileUtil; import forge.util.FileUtil;
import forge.util.TextUtil;
/** /**
* <p> * <p>
@@ -112,10 +114,10 @@ public class CardStorageReader implements ICardStorageReader {
} // CardReader() } // CardReader()
private final List<CardRules> loadCardsInRange(final List<File> files, int from, int to) { private final List<CardScriptInfo> loadCardsInRange(final List<File> files, int from, int to) {
CardRules.Reader rulesReader = new CardRules.Reader(); CardRules.Reader rulesReader = new CardRules.Reader();
List<CardRules> result = new ArrayList<CardRules>(); List<CardScriptInfo> result = new ArrayList<CardScriptInfo>();
for(int i = from; i < to; i++) { for(int i = from; i < to; i++) {
File cardTxtFile = files.get(i); File cardTxtFile = files.get(i);
result.add(this.loadCard(rulesReader, cardTxtFile)); result.add(this.loadCard(rulesReader, cardTxtFile));
@@ -123,10 +125,10 @@ public class CardStorageReader implements ICardStorageReader {
return result; return result;
} }
private final List<CardRules> loadCardsInRangeFromZip(final List<ZipEntry> files, int from, int to) { private final List<CardScriptInfo> loadCardsInRangeFromZip(final List<ZipEntry> files, int from, int to) {
CardRules.Reader rulesReader = new CardRules.Reader(); CardRules.Reader rulesReader = new CardRules.Reader();
List<CardRules> result = new ArrayList<CardRules>(); List<CardScriptInfo> result = new ArrayList<CardScriptInfo>();
for(int i = from; i < to; i++) { for(int i = from; i < to; i++) {
ZipEntry ze = files.get(i); ZipEntry ze = files.get(i);
// if (ze.getName().endsWith(CardStorageReader.CARD_FILE_DOT_EXTENSION)) // already filtered! // if (ze.getName().endsWith(CardStorageReader.CARD_FILE_DOT_EXTENSION)) // already filtered!
@@ -143,11 +145,12 @@ public class CardStorageReader implements ICardStorageReader {
* *
* @return the Card or null if it was not found. * @return the Card or null if it was not found.
*/ */
public final List<CardRules> loadCards() { @Override
public final List<CardScriptInfo> loadCards() {
progressObserver.setOperationName("Loading card data", true); progressObserver.setOperationName("Loading card data", true);
progressObserver.report(0, NUMBER_OF_PARTS); progressObserver.report(0, NUMBER_OF_PARTS);
final List<Callable<List<CardRules>>> tasks; final List<Callable<List<CardScriptInfo>>> tasks;
long estimatedFilesRemaining; long estimatedFilesRemaining;
// Iterate through txt files or zip archive. // Iterate through txt files or zip archive.
@@ -175,7 +178,7 @@ public class CardStorageReader implements ICardStorageReader {
StopWatch sw = new StopWatch(); StopWatch sw = new StopWatch();
sw.start(); sw.start();
List<CardRules> res = executeLoadTask(tasks); List<CardScriptInfo> res = executeLoadTask(tasks);
sw.stop(); sw.stop();
final long timeOnParse = sw.getTime(); final long timeOnParse = sw.getTime();
@@ -185,20 +188,20 @@ public class CardStorageReader implements ICardStorageReader {
return res; return res;
} // loadCardsUntilYouFind(String) } // loadCardsUntilYouFind(String)
private List<CardRules> executeLoadTask(final List<Callable<List<CardRules>>> tasks) { private List<CardScriptInfo> executeLoadTask(final List<Callable<List<CardScriptInfo>>> tasks) {
List<CardRules> result = new ArrayList<CardRules>(); List<CardScriptInfo> result = new ArrayList<CardScriptInfo>();
try { try {
if ( useThreadPool ) { if ( useThreadPool ) {
final ExecutorService executor = FThreads.getComputingPool(0.5f); final ExecutorService executor = FThreads.getComputingPool(0.5f);
final List<Future<List<CardRules>>> parts = executor.invokeAll(tasks); final List<Future<List<CardScriptInfo>>> parts = executor.invokeAll(tasks);
executor.shutdown(); executor.shutdown();
cdl.await(); cdl.await();
for(Future<List<CardRules>> pp : parts) { for(Future<List<CardScriptInfo>> pp : parts) {
result.addAll(pp.get()); result.addAll(pp.get());
} }
} else { } else {
for(Callable<List<CardRules>> c : tasks) { for(Callable<List<CardScriptInfo>> c : tasks) {
result.addAll(c.call()); result.addAll(c.call());
} }
} }
@@ -213,17 +216,17 @@ public class CardStorageReader implements ICardStorageReader {
return result; return result;
} }
private List<Callable<List<CardRules>>> makeTaskListForZip(final List<ZipEntry> entries) { private List<Callable<List<CardScriptInfo>>> makeTaskListForZip(final List<ZipEntry> entries) {
int totalFiles = entries.size(); int totalFiles = entries.size();
int filesPerPart = totalFiles / NUMBER_OF_PARTS; int filesPerPart = totalFiles / NUMBER_OF_PARTS;
final List<Callable<List<CardRules>>> tasks = new ArrayList<Callable<List<CardRules>>>(); final List<Callable<List<CardScriptInfo>>> tasks = new ArrayList<Callable<List<CardScriptInfo>>>();
for (int iPart = 0; iPart < NUMBER_OF_PARTS; iPart++) { for (int iPart = 0; iPart < NUMBER_OF_PARTS; iPart++) {
final int from = iPart * filesPerPart; final int from = iPart * filesPerPart;
final int till = iPart == NUMBER_OF_PARTS - 1 ? totalFiles : from + filesPerPart; final int till = iPart == NUMBER_OF_PARTS - 1 ? totalFiles : from + filesPerPart;
tasks.add(new Callable<List<CardRules>>() { tasks.add(new Callable<List<CardScriptInfo>>() {
@Override @Override
public List<CardRules> call() throws Exception{ public List<CardScriptInfo> call() throws Exception{
List<CardRules> res = loadCardsInRangeFromZip(entries, from, till); List<CardScriptInfo> res = loadCardsInRangeFromZip(entries, from, till);
cdl.countDown(); cdl.countDown();
progressObserver.report(NUMBER_OF_PARTS - (int)cdl.getCount(), NUMBER_OF_PARTS); progressObserver.report(NUMBER_OF_PARTS - (int)cdl.getCount(), NUMBER_OF_PARTS);
return res; return res;
@@ -233,17 +236,17 @@ public class CardStorageReader implements ICardStorageReader {
return tasks; return tasks;
} }
private List<Callable<List<CardRules>>> makeTaskListForFiles(final List<File> allFiles) { private List<Callable<List<CardScriptInfo>>> makeTaskListForFiles(final List<File> allFiles) {
int totalFiles = allFiles.size(); int totalFiles = allFiles.size();
int filesPerPart = totalFiles / NUMBER_OF_PARTS; int filesPerPart = totalFiles / NUMBER_OF_PARTS;
final List<Callable<List<CardRules>>> tasks = new ArrayList<Callable<List<CardRules>>>(); final List<Callable<List<CardScriptInfo>>> tasks = new ArrayList<Callable<List<CardScriptInfo>>>();
for (int iPart = 0; iPart < NUMBER_OF_PARTS; iPart++) { for (int iPart = 0; iPart < NUMBER_OF_PARTS; iPart++) {
final int from = iPart * filesPerPart; final int from = iPart * filesPerPart;
final int till = iPart == NUMBER_OF_PARTS - 1 ? totalFiles : from + filesPerPart; final int till = iPart == NUMBER_OF_PARTS - 1 ? totalFiles : from + filesPerPart;
tasks.add(new Callable<List<CardRules>>() { tasks.add(new Callable<List<CardScriptInfo>>() {
@Override @Override
public List<CardRules> call() throws Exception{ public List<CardScriptInfo> call() throws Exception{
List<CardRules> res = loadCardsInRange(allFiles, from, till); List<CardScriptInfo> res = loadCardsInRange(allFiles, from, till);
cdl.countDown(); cdl.countDown();
progressObserver.report(NUMBER_OF_PARTS - (int)cdl.getCount(), NUMBER_OF_PARTS); progressObserver.report(NUMBER_OF_PARTS - (int)cdl.getCount(), NUMBER_OF_PARTS);
return res; return res;
@@ -282,13 +285,13 @@ public class CardStorageReader implements ICardStorageReader {
* *
* @return the card loaded from the stream * @return the card loaded from the stream
*/ */
protected final CardRules loadCard(CardRules.Reader reader, final InputStream inputStream) { protected final CardScriptInfo loadCard(CardRules.Reader reader, final InputStream inputStream, final File file) {
reader.reset(); reader.reset();
InputStreamReader isr = new InputStreamReader(inputStream, this.charset); InputStreamReader isr = new InputStreamReader(inputStream, this.charset);
List<String> allLines = FileUtil.readAllLines(isr, true); List<String> allLines = FileUtil.readAllLines(isr, true);
return reader.readCard(allLines); return new CardScriptInfo(TextUtil.join(allLines, "\n"), file, reader.readCard(allLines));
} }
/** /**
@@ -299,13 +302,11 @@ public class CardStorageReader implements ICardStorageReader {
* *
* @return a new Card instance * @return a new Card instance
*/ */
protected final CardRules loadCard(final CardRules.Reader reader, final File file) { protected final CardScriptInfo loadCard(final CardRules.Reader reader, final File file) {
FileInputStream fileInputStream = null; FileInputStream fileInputStream = null;
try { try {
fileInputStream = new FileInputStream(file); fileInputStream = new FileInputStream(file);
CardRules rules = this.loadCard(reader, fileInputStream); return this.loadCard(reader, fileInputStream, file);
//rules.setSourceFile(file);
return rules;
} catch (final FileNotFoundException ex) { } catch (final FileNotFoundException ex) {
throw new RuntimeException("CardReader : run error -- file not found: " + file.getPath(), ex); throw new RuntimeException("CardReader : run error -- file not found: " + file.getPath(), ex);
} finally { } finally {
@@ -326,11 +327,11 @@ public class CardStorageReader implements ICardStorageReader {
* *
* @return a new Card instance * @return a new Card instance
*/ */
protected final CardRules loadCard(final CardRules.Reader rulesReader, final ZipEntry entry) { protected final CardScriptInfo loadCard(final CardRules.Reader rulesReader, final ZipEntry entry) {
InputStream zipInputStream = null; InputStream zipInputStream = null;
try { try {
zipInputStream = this.zip.getInputStream(entry); zipInputStream = this.zip.getInputStream(entry);
return this.loadCard(rulesReader, zipInputStream); return this.loadCard(rulesReader, zipInputStream, null);
} catch (final IOException exn) { } catch (final IOException exn) {
throw new RuntimeException(exn); throw new RuntimeException(exn);
// PM // PM
@@ -345,5 +346,4 @@ public class CardStorageReader implements ICardStorageReader {
} }
} }
} }
} }

View File

@@ -6,6 +6,8 @@ import javax.swing.event.DocumentListener;
import forge.Command; import forge.Command;
import forge.Singletons; import forge.Singletons;
import forge.StaticData;
import forge.card.CardScriptInfo;
import forge.gui.framework.FScreen; import forge.gui.framework.FScreen;
import forge.gui.framework.ICDoc; import forge.gui.framework.ICDoc;
import forge.gui.toolbox.FTextEditor; import forge.gui.toolbox.FTextEditor;
@@ -25,11 +27,11 @@ import forge.item.PaperCard;
public enum CCardScript implements ICDoc { public enum CCardScript implements ICDoc {
/** */ /** */
SINGLETON_INSTANCE; SINGLETON_INSTANCE;
private PaperCard currentCard; private PaperCard currentCard;
private String baseText; private String baseText;
private boolean isTextDirty; private boolean isTextDirty;
private CCardScript() { private CCardScript() {
VCardScript.SINGLETON_INSTANCE.getTxtScript().addDocumentListener(new DocumentListener() { VCardScript.SINGLETON_INSTANCE.getTxtScript().addDocumentListener(new DocumentListener() {
@Override @Override
@@ -48,7 +50,7 @@ public enum CCardScript implements ICDoc {
} }
}); });
} }
private void updateDirtyFlag() { private void updateDirtyFlag() {
boolean isTextNowDirty = !VCardScript.SINGLETON_INSTANCE.getTxtScript().getText().equals(baseText); boolean isTextNowDirty = !VCardScript.SINGLETON_INSTANCE.getTxtScript().getText().equals(baseText);
if (this.isTextDirty == isTextNowDirty) { return; } if (this.isTextDirty == isTextNowDirty) { return; }
@@ -72,21 +74,16 @@ public enum CCardScript implements ICDoc {
this.currentCard = card; this.currentCard = card;
refresh(); refresh();
} }
public void refresh() { public void refresh() {
String text = ""; String text = "";
boolean editable = false; boolean editable = false;
if (this.currentCard != null) { if (this.currentCard != null) {
// File sourceFile = this.currentCard.getRules().getSourceFile(); CardScriptInfo scriptInfo = StaticData.instance().getScriptInfo(this.currentCard.getRules());
// if (sourceFile != null) { if (scriptInfo != null) {
// try { text = scriptInfo.getText();
// text = FileUtil.readFileToString(sourceFile); editable = scriptInfo.canEdit();
// editable = true; }
// }
// catch (final Exception ex) {
// text = "Couldn't read file - " + sourceFile + "\n\nException:\n" + ex.toString();
// }
// }
} }
this.baseText = text; this.baseText = text;
@@ -95,11 +92,11 @@ public enum CCardScript implements ICDoc {
txtScript.setEditable(editable); txtScript.setEditable(editable);
txtScript.setCaretPosition(0); //keep scrolled to top txtScript.setCaretPosition(0); //keep scrolled to top
} }
public boolean hasChanges() { public boolean hasChanges() {
return (this.currentCard != null && this.isTextDirty); return (this.currentCard != null && this.isTextDirty);
} }
public boolean canSwitchAway(boolean isCardChanging) { public boolean canSwitchAway(boolean isCardChanging) {
if (!hasChanges()) { return true; } if (!hasChanges()) { return true; }
@@ -119,7 +116,7 @@ public enum CCardScript implements ICDoc {
} }
return true; return true;
} }
public boolean saveChanges() { public boolean saveChanges() {
if (!hasChanges()) { return true; } //not need if text hasn't been changed if (!hasChanges()) { return true; } //not need if text hasn't been changed
@@ -132,7 +129,7 @@ public enum CCardScript implements ICDoc {
// PrintWriter p = new PrintWriter(sourceFile); // PrintWriter p = new PrintWriter(sourceFile);
// p.print(text); // p.print(text);
// p.close(); // p.close();
// //
// this.baseText = text; // this.baseText = text;
// updateDirtyFlag(); // updateDirtyFlag();
// //