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

@@ -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 {
}
}
}
}

View File

@@ -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;
@@ -25,11 +27,11 @@ import forge.item.PaperCard;
public enum CCardScript implements ICDoc {
/** */
SINGLETON_INSTANCE;
private PaperCard currentCard;
private String baseText;
private boolean isTextDirty;
private CCardScript() {
VCardScript.SINGLETON_INSTANCE.getTxtScript().addDocumentListener(new DocumentListener() {
@Override
@@ -48,7 +50,7 @@ public enum CCardScript implements ICDoc {
}
});
}
private void updateDirtyFlag() {
boolean isTextNowDirty = !VCardScript.SINGLETON_INSTANCE.getTxtScript().getText().equals(baseText);
if (this.isTextDirty == isTextNowDirty) { return; }
@@ -72,21 +74,16 @@ public enum CCardScript implements ICDoc {
this.currentCard = card;
refresh();
}
public void refresh() {
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;
@@ -95,11 +92,11 @@ public enum CCardScript implements ICDoc {
txtScript.setEditable(editable);
txtScript.setCaretPosition(0); //keep scrolled to top
}
public boolean hasChanges() {
return (this.currentCard != null && this.isTextDirty);
}
public boolean canSwitchAway(boolean isCardChanging) {
if (!hasChanges()) { return true; }
@@ -119,7 +116,7 @@ public enum CCardScript implements ICDoc {
}
return true;
}
public boolean saveChanges() {
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);
// p.print(text);
// p.close();
//
//
// this.baseText = text;
// updateDirtyFlag();
//