mirror of
https://github.com/Card-Forge/forge.git
synced 2025-11-15 18:28:00 +00:00
moved CardStorageReader to core project
This commit is contained in:
5
.gitattributes
vendored
5
.gitattributes
vendored
@@ -19,8 +19,7 @@ forge-core/.settings/org.eclipse.core.resources.prefs -text
|
||||
forge-core/.settings/org.eclipse.jdt.core.prefs -text
|
||||
forge-core/.settings/org.eclipse.m2e.core.prefs -text
|
||||
forge-core/pom.xml -text
|
||||
forge-core/src/main/java/forge/ICardStorageReader.java -text
|
||||
forge-core/src/main/java/forge/IProgressObserver.java -text
|
||||
forge-core/src/main/java/forge/CardStorageReader.java -text
|
||||
forge-core/src/main/java/forge/StaticData.java -text
|
||||
forge-core/src/main/java/forge/card/BoosterGenerator.java svneol=native#text/plain
|
||||
forge-core/src/main/java/forge/card/BoosterSlots.java -text
|
||||
@@ -98,6 +97,7 @@ forge-core/src/main/java/forge/util/Lang.java -text
|
||||
forge-core/src/main/java/forge/util/MyRandom.java svneol=native#text/plain
|
||||
forge-core/src/main/java/forge/util/PredicateString.java -text
|
||||
forge-core/src/main/java/forge/util/TextUtil.java -text
|
||||
forge-core/src/main/java/forge/util/ThreadUtil.java -text
|
||||
forge-core/src/main/java/forge/util/package-info.java -text
|
||||
forge-core/src/main/java/forge/util/storage/IStorage.java -text
|
||||
forge-core/src/main/java/forge/util/storage/StorageBase.java -text
|
||||
@@ -14944,7 +14944,6 @@ forge-gui/src/main/java/forge/card/ability/package-info.java svneol=native#text/
|
||||
forge-gui/src/main/java/forge/card/cardfactory/CardFactory.java svneol=native#text/plain
|
||||
forge-gui/src/main/java/forge/card/cardfactory/CardFactoryCreatures.java svneol=native#text/plain
|
||||
forge-gui/src/main/java/forge/card/cardfactory/CardFactoryUtil.java svneol=native#text/plain
|
||||
forge-gui/src/main/java/forge/card/cardfactory/CardStorageReader.java svneol=native#text/plain
|
||||
forge-gui/src/main/java/forge/card/cardfactory/package-info.java svneol=native#text/plain
|
||||
forge-gui/src/main/java/forge/card/cost/Cost.java svneol=native#text/plain
|
||||
forge-gui/src/main/java/forge/card/cost/CostAddMana.java -text
|
||||
|
||||
@@ -15,7 +15,7 @@
|
||||
* 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.cardfactory;
|
||||
package forge;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
@@ -37,11 +37,9 @@ import java.util.zip.ZipFile;
|
||||
|
||||
import org.apache.commons.lang3.time.StopWatch;
|
||||
|
||||
import forge.FThreads;
|
||||
import forge.ICardStorageReader;
|
||||
import forge.IProgressObserver;
|
||||
import forge.card.CardRules;
|
||||
import forge.util.FileUtil;
|
||||
import forge.util.ThreadUtil;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
@@ -49,21 +47,35 @@ import forge.util.FileUtil;
|
||||
* </p>
|
||||
*
|
||||
* @author Forge
|
||||
* @version $Id$
|
||||
* @version $Id: CardStorageReader.java 23742 2013-11-22 16:32:56Z Max mtg $
|
||||
*/
|
||||
|
||||
public class CardStorageReader implements ICardStorageReader {
|
||||
public class CardStorageReader {
|
||||
public interface Observer {
|
||||
public void cardLoaded(CardRules rules, List<String> lines, File fileOnDisk);
|
||||
}
|
||||
|
||||
public interface ProgressObserver{
|
||||
void setOperationName(String name, boolean usePercents);
|
||||
void report(int current, int total);
|
||||
|
||||
// does nothing, used when they pass null instead of an instance
|
||||
public final static ProgressObserver emptyObserver = new ProgressObserver() {
|
||||
@Override public void setOperationName(String name, boolean usePercents) {}
|
||||
@Override public void report(int current, int total) {}
|
||||
};
|
||||
}
|
||||
|
||||
private static final String CARD_FILE_DOT_EXTENSION = ".txt";
|
||||
|
||||
/** Default charset when loading from files. */
|
||||
public static final String DEFAULT_CHARSET_NAME = "US-ASCII";
|
||||
|
||||
private final boolean useThreadPool = FThreads.isMultiCoreSystem();
|
||||
private final boolean useThreadPool = ThreadUtil.isMultiCoreSystem();
|
||||
public final static int NUMBER_OF_PARTS = 25;
|
||||
|
||||
private final CountDownLatch cdl = new CountDownLatch(NUMBER_OF_PARTS);
|
||||
private final IProgressObserver progressObserver;
|
||||
private final ProgressObserver progressObserver;
|
||||
|
||||
private transient File cardsfolder;
|
||||
|
||||
@@ -87,8 +99,8 @@ public class CardStorageReader implements ICardStorageReader {
|
||||
* if true, attempts to load cards from a zip file, if one
|
||||
* exists.
|
||||
*/
|
||||
public CardStorageReader(String cardDataDir, final boolean useZip, IProgressObserver progressObserver, ICardStorageReader.Observer observer) {
|
||||
this.progressObserver = progressObserver != null ? progressObserver : IProgressObserver.emptyObserver;
|
||||
public CardStorageReader(String cardDataDir, final boolean useZip, CardStorageReader.ProgressObserver progressObserver, Observer observer) {
|
||||
this.progressObserver = progressObserver != null ? progressObserver : CardStorageReader.ProgressObserver.emptyObserver;
|
||||
this.cardsfolder = new File(cardDataDir);
|
||||
this.observer = observer;
|
||||
|
||||
@@ -193,7 +205,7 @@ public class CardStorageReader implements ICardStorageReader {
|
||||
|
||||
try {
|
||||
if ( useThreadPool ) {
|
||||
final ExecutorService executor = FThreads.getComputingPool(0.5f);
|
||||
final ExecutorService executor = ThreadUtil.getComputingPool(0.5f);
|
||||
final List<Future<List<CardRules>>> parts = executor.invokeAll(tasks);
|
||||
executor.shutdown();
|
||||
cdl.await();
|
||||
@@ -1,14 +0,0 @@
|
||||
package forge;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.List;
|
||||
|
||||
import forge.card.CardRules;
|
||||
|
||||
public interface ICardStorageReader {
|
||||
List<CardRules> loadCards();
|
||||
|
||||
public interface Observer {
|
||||
public void cardLoaded(CardRules rules, List<String> lines, File fileOnDisk);
|
||||
}
|
||||
}
|
||||
@@ -1,12 +0,0 @@
|
||||
package forge;
|
||||
|
||||
public interface IProgressObserver{
|
||||
void setOperationName(String name, boolean usePercents);
|
||||
void report(int current, int total);
|
||||
|
||||
// does nothing, used when they pass null instead of an instance
|
||||
public final static IProgressObserver emptyObserver = new IProgressObserver() {
|
||||
@Override public void setOperationName(String name, boolean usePercents) {}
|
||||
@Override public void report(int current, int total) {}
|
||||
};
|
||||
}
|
||||
@@ -32,7 +32,7 @@ public class StaticData {
|
||||
|
||||
private static StaticData lastInstance = null;
|
||||
|
||||
public StaticData(ICardStorageReader reader, String editionFolder, String blockDataFolder) {
|
||||
public StaticData(CardStorageReader reader, String editionFolder, String blockDataFolder) {
|
||||
this.editions = new CardEdition.Collection(new CardEdition.Reader(new File(editionFolder)));
|
||||
lastInstance = this;
|
||||
|
||||
|
||||
54
forge-core/src/main/java/forge/util/ThreadUtil.java
Normal file
54
forge-core/src/main/java/forge/util/ThreadUtil.java
Normal file
@@ -0,0 +1,54 @@
|
||||
package forge.util;
|
||||
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.ScheduledExecutorService;
|
||||
import java.util.concurrent.ThreadFactory;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
public class ThreadUtil {
|
||||
|
||||
static {
|
||||
System.out.printf("(ThreadUtil first call): Running on a machine with %d cpu core(s)%n", Runtime.getRuntime().availableProcessors() );
|
||||
}
|
||||
|
||||
private static class WorkerThreadFactory implements ThreadFactory {
|
||||
private int countr = 0;
|
||||
private String prefix = "";
|
||||
|
||||
public WorkerThreadFactory(String prefix) {
|
||||
this.prefix = prefix;
|
||||
}
|
||||
|
||||
public Thread newThread(Runnable r) {
|
||||
return new Thread(r, prefix + "-" + countr++);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
private final static ExecutorService gameThreadPool = Executors.newCachedThreadPool(new WorkerThreadFactory("Game"));
|
||||
private static ExecutorService getGameThreadPool() { return gameThreadPool; }
|
||||
private final static ScheduledExecutorService scheduledPool = Executors.newScheduledThreadPool(2, new WorkerThreadFactory("Delayed"));
|
||||
private static ScheduledExecutorService getScheduledPool() { return scheduledPool; }
|
||||
|
||||
// This pool is designed to parallel CPU or IO intensive tasks like parse cards or download images, assuming a load factor of 0.5
|
||||
public final static ExecutorService getComputingPool(float loadFactor) {
|
||||
return Executors.newFixedThreadPool((int)(Runtime.getRuntime().availableProcessors() / (1-loadFactor)));
|
||||
}
|
||||
|
||||
public static boolean isMultiCoreSystem() {
|
||||
return Runtime.getRuntime().availableProcessors() > 1;
|
||||
}
|
||||
|
||||
public static void invokeInGameThread(Runnable toRun) {
|
||||
getGameThreadPool().execute(toRun);
|
||||
}
|
||||
public static void delay(int milliseconds, Runnable inputUpdater) {
|
||||
getScheduledPool().schedule(inputUpdater, milliseconds, TimeUnit.MILLISECONDS);
|
||||
}
|
||||
|
||||
public static boolean isGameThread() {
|
||||
return Thread.currentThread().getName().startsWith("Game");
|
||||
}
|
||||
}
|
||||
@@ -2,53 +2,18 @@ package forge;
|
||||
|
||||
import java.io.PrintStream;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.ScheduledExecutorService;
|
||||
import java.util.concurrent.ThreadFactory;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import javax.swing.SwingUtilities;
|
||||
|
||||
import forge.util.ThreadUtil;
|
||||
|
||||
/**
|
||||
* TODO: Write javadoc for this type.
|
||||
*
|
||||
*/
|
||||
public class FThreads {
|
||||
|
||||
static {
|
||||
System.out.printf("(FThreads static ctor): Running on a machine with %d cpu core(s)%n", Runtime.getRuntime().availableProcessors() );
|
||||
}
|
||||
|
||||
private static class WorkerThreadFactory implements ThreadFactory {
|
||||
private int countr = 0;
|
||||
private String prefix = "";
|
||||
|
||||
public WorkerThreadFactory(String prefix) {
|
||||
this.prefix = prefix;
|
||||
}
|
||||
|
||||
public Thread newThread(Runnable r) {
|
||||
return new Thread(r, prefix + "-" + countr++);
|
||||
}
|
||||
}
|
||||
|
||||
private FThreads() { } // no instances supposed
|
||||
|
||||
private final static ExecutorService gameThreadPool = Executors.newCachedThreadPool(new WorkerThreadFactory("Game"));
|
||||
private static ExecutorService getGameThreadPool() { return gameThreadPool; }
|
||||
private final static ScheduledExecutorService scheduledPool = Executors.newScheduledThreadPool(2, new WorkerThreadFactory("Delayed"));
|
||||
private static ScheduledExecutorService getScheduledPool() { return scheduledPool; }
|
||||
|
||||
// This pool is designed to parallel CPU or IO intensive tasks like parse cards or download images, assuming a load factor of 0.5
|
||||
public final static ExecutorService getComputingPool(float loadFactor) {
|
||||
return Executors.newFixedThreadPool((int)(Runtime.getRuntime().availableProcessors() / (1-loadFactor)));
|
||||
}
|
||||
|
||||
public static boolean isMultiCoreSystem() {
|
||||
return Runtime.getRuntime().availableProcessors() > 1;
|
||||
}
|
||||
|
||||
/** Checks if calling method uses event dispatch thread.
|
||||
* Exception thrown if method is on "wrong" thread.
|
||||
* A boolean is passed to indicate if the method must be EDT or not.
|
||||
@@ -111,9 +76,7 @@ public class FThreads {
|
||||
}
|
||||
|
||||
|
||||
public static void invokeInGameThread(Runnable toRun) {
|
||||
getGameThreadPool().execute(toRun);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* TODO: Write javadoc for this method.
|
||||
@@ -124,17 +87,13 @@ public class FThreads {
|
||||
}
|
||||
|
||||
|
||||
public static void delay(int milliseconds, Runnable inputUpdater) {
|
||||
getScheduledPool().schedule(inputUpdater, milliseconds, TimeUnit.MILLISECONDS);
|
||||
}
|
||||
|
||||
public static void delayInEDT(int milliseconds, final Runnable inputUpdater) {
|
||||
Runnable runInEdt = new Runnable() {
|
||||
@Override public void run() {
|
||||
FThreads.invokeInEdtNowOrLater(inputUpdater);
|
||||
}
|
||||
};
|
||||
delay(milliseconds, runInEdt);
|
||||
ThreadUtil.delay(milliseconds, runInEdt);
|
||||
}
|
||||
|
||||
public static String debugGetCurrThreadId() {
|
||||
@@ -171,9 +130,4 @@ public class FThreads {
|
||||
public static String debugGetStackTraceItem(int depth) {
|
||||
return debugGetStackTraceItem(depth, false);
|
||||
}
|
||||
|
||||
public static boolean isGameThread() {
|
||||
return Thread.currentThread().getName().startsWith("Game");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -17,11 +17,9 @@
|
||||
*/
|
||||
package forge;
|
||||
|
||||
import forge.card.cardfactory.CardStorageReader;
|
||||
import forge.control.FControl;
|
||||
import forge.gui.toolbox.FProgressBar;
|
||||
import forge.gui.workshop.CardScriptInfo;
|
||||
import forge.gui.workshop.controllers.CCardScript;
|
||||
import forge.model.FModel;
|
||||
import forge.properties.NewConstants;
|
||||
import forge.view.FView;
|
||||
@@ -57,7 +55,8 @@ public final class Singletons {
|
||||
if(withUi)
|
||||
view = FView.SINGLETON_INSTANCE;
|
||||
|
||||
IProgressObserver progressBarBridge = view == null ? IProgressObserver.emptyObserver : new IProgressObserver() {
|
||||
CardStorageReader.ProgressObserver progressBarBridge = view == null
|
||||
? CardStorageReader.ProgressObserver.emptyObserver : new CardStorageReader.ProgressObserver() {
|
||||
FProgressBar bar = view.getSplash().getProgressBar();
|
||||
@Override
|
||||
public void setOperationName(final String name, final boolean usePercents) {
|
||||
|
||||
@@ -75,6 +75,7 @@ import forge.game.zone.Zone;
|
||||
import forge.game.zone.ZoneType;
|
||||
import forge.util.Aggregates;
|
||||
import forge.util.CollectionSuppliers;
|
||||
import forge.util.ThreadUtil;
|
||||
import forge.util.maps.HashMapOfLists;
|
||||
import forge.util.maps.MapOfLists;
|
||||
|
||||
@@ -1620,10 +1621,10 @@ public class GameAction {
|
||||
|
||||
// Invokes given runnable in Game thread pool - used to start game and perform actions from UI (when game-0 waits for input)
|
||||
public void invoke(final Runnable proc) {
|
||||
if( FThreads.isGameThread() ) {
|
||||
if( ThreadUtil.isGameThread() ) {
|
||||
proc.run();
|
||||
} else
|
||||
FThreads.invokeInGameThread(proc);
|
||||
ThreadUtil.invokeInGameThread(proc);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -26,12 +26,12 @@ import javax.xml.stream.events.Attribute;
|
||||
import javax.xml.stream.events.StartElement;
|
||||
import javax.xml.stream.events.XMLEvent;
|
||||
|
||||
import forge.FThreads;
|
||||
import forge.Singletons;
|
||||
import forge.gui.toolbox.FAbsolutePositioner;
|
||||
import forge.properties.FileLocation;
|
||||
import forge.properties.NewConstants;
|
||||
import forge.util.CollectionSuppliers;
|
||||
import forge.util.ThreadUtil;
|
||||
import forge.util.maps.HashMapOfLists;
|
||||
import forge.util.maps.MapOfLists;
|
||||
import forge.view.FFrame;
|
||||
@@ -63,7 +63,7 @@ public final class SLayoutIO {
|
||||
|
||||
public static void saveWindowLayout() {
|
||||
if (saveWindowRequested.getAndSet(true)) { return; }
|
||||
FThreads.delay(500, new Runnable() {
|
||||
ThreadUtil.delay(500, new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
finishSaveWindowLayout();
|
||||
@@ -230,7 +230,7 @@ public final class SLayoutIO {
|
||||
*/
|
||||
public static void saveLayout(final File f0) {
|
||||
if( saveRequested.getAndSet(true) ) return;
|
||||
FThreads.delay(100, new Runnable() {
|
||||
ThreadUtil.delay(100, new Runnable() {
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
|
||||
@@ -22,13 +22,13 @@ import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import forge.Card;
|
||||
import forge.FThreads;
|
||||
import forge.game.Game;
|
||||
import forge.game.player.Player;
|
||||
import forge.game.zone.ZoneType;
|
||||
import forge.gui.GuiDialog;
|
||||
import forge.gui.match.CMatchUI;
|
||||
import forge.util.Lang;
|
||||
import forge.util.ThreadUtil;
|
||||
import forge.view.ButtonUtil;
|
||||
/**
|
||||
* <p>
|
||||
@@ -122,7 +122,7 @@ public class InputConfirmMulligan extends InputSyncronizedBase {
|
||||
|
||||
if ( isSerumPowder && GuiDialog.confirm(c0, "Use " + c0.getName() + "'s ability?")) {
|
||||
cardSelectLocked = true;
|
||||
FThreads.invokeInGameThread(new Runnable() {
|
||||
ThreadUtil.invokeInGameThread(new Runnable() {
|
||||
public void run() {
|
||||
List<Card> hand = new ArrayList<Card>(c0.getController().getCardsIn(ZoneType.Hand));
|
||||
for (Card c : hand) {
|
||||
|
||||
@@ -10,6 +10,7 @@ import forge.card.spellability.SpellAbility;
|
||||
import forge.control.InputQueue;
|
||||
import forge.game.player.Player;
|
||||
import forge.gui.match.CMatchUI;
|
||||
import forge.util.ThreadUtil;
|
||||
import forge.view.ButtonUtil;
|
||||
|
||||
/**
|
||||
@@ -24,7 +25,7 @@ public class InputLockUI implements Input {
|
||||
|
||||
public void showMessageInitial() {
|
||||
int ixCall = 1 + iCall.getAndIncrement();
|
||||
FThreads.delay(500, new InputUpdater(ixCall));
|
||||
ThreadUtil.delay(500, new InputUpdater(ixCall));
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -20,12 +20,11 @@ package forge.gui.workshop;
|
||||
import java.io.File;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.TreeMap;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
||||
import forge.ICardStorageReader;
|
||||
import forge.CardStorageReader;
|
||||
import forge.card.CardRules;
|
||||
|
||||
public final class CardScriptInfo {
|
||||
@@ -58,7 +57,7 @@ public final class CardScriptInfo {
|
||||
return allScrips.get(name);
|
||||
}
|
||||
|
||||
public static ICardStorageReader.Observer readerObserver = new ICardStorageReader.Observer() {
|
||||
public static CardStorageReader.Observer readerObserver = new CardStorageReader.Observer() {
|
||||
@Override
|
||||
public void cardLoaded(CardRules rules, List<String> lines, File fileOnDisk) {
|
||||
allScrips.put(rules.getName(), new CardScriptInfo(StringUtils.join(lines, '\n'), fileOnDisk));
|
||||
|
||||
@@ -11,8 +11,8 @@ import java.util.List;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import forge.CardStorageReader;
|
||||
import forge.card.CardRules;
|
||||
import forge.card.cardfactory.CardStorageReader;
|
||||
import forge.properties.NewConstants;
|
||||
import forge.util.FileUtil;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user