mirror of
https://github.com/Card-Forge/forge.git
synced 2025-11-18 19:58:00 +00:00
Support displaying scripts in Workshop again
This commit is contained in:
1
.gitattributes
vendored
1
.gitattributes
vendored
@@ -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/CardRules.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/CardType.java -text
|
||||
forge-core/src/main/java/forge/card/ColorSet.java -text
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -100,4 +107,8 @@ public class StaticData {
|
||||
public CardDb getVariantCards() {
|
||||
return variantCards;
|
||||
}
|
||||
|
||||
public CardScriptInfo getScriptInfo(CardRules rules) {
|
||||
return scriptLookup.get(rules);
|
||||
}
|
||||
}
|
||||
|
||||
52
forge-core/src/main/java/forge/card/CardScriptInfo.java
Normal file
52
forge-core/src/main/java/forge/card/CardScriptInfo.java
Normal 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;
|
||||
}
|
||||
}
|
||||
@@ -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) {
|
||||
|
||||
@@ -99,6 +99,17 @@ public class TextUtil {
|
||||
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
|
||||
|
||||
@@ -41,7 +41,9 @@ import forge.FThreads;
|
||||
import forge.ICardStorageReader;
|
||||
import forge.IProgressObserver;
|
||||
import forge.card.CardRules;
|
||||
import forge.card.CardScriptInfo;
|
||||
import forge.util.FileUtil;
|
||||
import forge.util.TextUtil;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
@@ -112,10 +114,10 @@ public class CardStorageReader implements ICardStorageReader {
|
||||
|
||||
} // 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();
|
||||
|
||||
List<CardRules> result = new ArrayList<CardRules>();
|
||||
List<CardScriptInfo> result = new ArrayList<CardScriptInfo>();
|
||||
for(int i = from; i < to; i++) {
|
||||
File cardTxtFile = files.get(i);
|
||||
result.add(this.loadCard(rulesReader, cardTxtFile));
|
||||
@@ -123,10 +125,10 @@ public class CardStorageReader implements ICardStorageReader {
|
||||
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();
|
||||
|
||||
List<CardRules> result = new ArrayList<CardRules>();
|
||||
List<CardScriptInfo> result = new ArrayList<CardScriptInfo>();
|
||||
for(int i = from; i < to; i++) {
|
||||
ZipEntry ze = files.get(i);
|
||||
// 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.
|
||||
*/
|
||||
public final List<CardRules> loadCards() {
|
||||
@Override
|
||||
public final List<CardScriptInfo> loadCards() {
|
||||
progressObserver.setOperationName("Loading card data", true);
|
||||
progressObserver.report(0, NUMBER_OF_PARTS);
|
||||
|
||||
final List<Callable<List<CardRules>>> tasks;
|
||||
final List<Callable<List<CardScriptInfo>>> tasks;
|
||||
long estimatedFilesRemaining;
|
||||
|
||||
// Iterate through txt files or zip archive.
|
||||
@@ -175,7 +178,7 @@ public class CardStorageReader implements ICardStorageReader {
|
||||
StopWatch sw = new StopWatch();
|
||||
sw.start();
|
||||
|
||||
List<CardRules> res = executeLoadTask(tasks);
|
||||
List<CardScriptInfo> res = executeLoadTask(tasks);
|
||||
|
||||
sw.stop();
|
||||
final long timeOnParse = sw.getTime();
|
||||
@@ -185,20 +188,20 @@ public class CardStorageReader implements ICardStorageReader {
|
||||
return res;
|
||||
} // loadCardsUntilYouFind(String)
|
||||
|
||||
private List<CardRules> executeLoadTask(final List<Callable<List<CardRules>>> tasks) {
|
||||
List<CardRules> result = new ArrayList<CardRules>();
|
||||
private List<CardScriptInfo> executeLoadTask(final List<Callable<List<CardScriptInfo>>> tasks) {
|
||||
List<CardScriptInfo> result = new ArrayList<CardScriptInfo>();
|
||||
|
||||
try {
|
||||
if ( useThreadPool ) {
|
||||
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();
|
||||
cdl.await();
|
||||
for(Future<List<CardRules>> pp : parts) {
|
||||
for(Future<List<CardScriptInfo>> pp : parts) {
|
||||
result.addAll(pp.get());
|
||||
}
|
||||
} else {
|
||||
for(Callable<List<CardRules>> c : tasks) {
|
||||
for(Callable<List<CardScriptInfo>> c : tasks) {
|
||||
result.addAll(c.call());
|
||||
}
|
||||
}
|
||||
@@ -213,17 +216,17 @@ public class CardStorageReader implements ICardStorageReader {
|
||||
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 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++) {
|
||||
final int from = iPart * 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
|
||||
public List<CardRules> call() throws Exception{
|
||||
List<CardRules> res = loadCardsInRangeFromZip(entries, from, till);
|
||||
public List<CardScriptInfo> call() throws Exception{
|
||||
List<CardScriptInfo> res = loadCardsInRangeFromZip(entries, from, till);
|
||||
cdl.countDown();
|
||||
progressObserver.report(NUMBER_OF_PARTS - (int)cdl.getCount(), NUMBER_OF_PARTS);
|
||||
return res;
|
||||
@@ -233,17 +236,17 @@ public class CardStorageReader implements ICardStorageReader {
|
||||
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 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++) {
|
||||
final int from = iPart * 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
|
||||
public List<CardRules> call() throws Exception{
|
||||
List<CardRules> res = loadCardsInRange(allFiles, from, till);
|
||||
public List<CardScriptInfo> call() throws Exception{
|
||||
List<CardScriptInfo> res = loadCardsInRange(allFiles, from, till);
|
||||
cdl.countDown();
|
||||
progressObserver.report(NUMBER_OF_PARTS - (int)cdl.getCount(), NUMBER_OF_PARTS);
|
||||
return res;
|
||||
@@ -282,13 +285,13 @@ public class CardStorageReader implements ICardStorageReader {
|
||||
*
|
||||
* @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();
|
||||
|
||||
InputStreamReader isr = new InputStreamReader(inputStream, this.charset);
|
||||
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
|
||||
*/
|
||||
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;
|
||||
try {
|
||||
fileInputStream = new FileInputStream(file);
|
||||
CardRules rules = this.loadCard(reader, fileInputStream);
|
||||
//rules.setSourceFile(file);
|
||||
return rules;
|
||||
return this.loadCard(reader, fileInputStream, file);
|
||||
} catch (final FileNotFoundException ex) {
|
||||
throw new RuntimeException("CardReader : run error -- file not found: " + file.getPath(), ex);
|
||||
} finally {
|
||||
@@ -326,11 +327,11 @@ public class CardStorageReader implements ICardStorageReader {
|
||||
*
|
||||
* @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;
|
||||
try {
|
||||
zipInputStream = this.zip.getInputStream(entry);
|
||||
return this.loadCard(rulesReader, zipInputStream);
|
||||
return this.loadCard(rulesReader, zipInputStream, null);
|
||||
} catch (final IOException exn) {
|
||||
throw new RuntimeException(exn);
|
||||
// PM
|
||||
@@ -345,5 +346,4 @@ public class CardStorageReader implements ICardStorageReader {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -6,6 +6,8 @@ import javax.swing.event.DocumentListener;
|
||||
|
||||
import forge.Command;
|
||||
import forge.Singletons;
|
||||
import forge.StaticData;
|
||||
import forge.card.CardScriptInfo;
|
||||
import forge.gui.framework.FScreen;
|
||||
import forge.gui.framework.ICDoc;
|
||||
import forge.gui.toolbox.FTextEditor;
|
||||
@@ -77,16 +79,11 @@ public enum CCardScript implements ICDoc {
|
||||
String text = "";
|
||||
boolean editable = false;
|
||||
if (this.currentCard != null) {
|
||||
// File sourceFile = this.currentCard.getRules().getSourceFile();
|
||||
// if (sourceFile != null) {
|
||||
// try {
|
||||
// text = FileUtil.readFileToString(sourceFile);
|
||||
// editable = true;
|
||||
// }
|
||||
// catch (final Exception ex) {
|
||||
// text = "Couldn't read file - " + sourceFile + "\n\nException:\n" + ex.toString();
|
||||
// }
|
||||
// }
|
||||
CardScriptInfo scriptInfo = StaticData.instance().getScriptInfo(this.currentCard.getRules());
|
||||
if (scriptInfo != null) {
|
||||
text = scriptInfo.getText();
|
||||
editable = scriptInfo.canEdit();
|
||||
}
|
||||
}
|
||||
this.baseText = text;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user