2015-02-11 19:30:27 -08:00
|
|
|
package magic.utility;
|
2014-02-19 18:12:00 -08:00
|
|
|
|
2015-02-17 17:51:51 -08:00
|
|
|
import magic.data.DeckGenerators;
|
|
|
|
import magic.data.DeckUtils;
|
|
|
|
import magic.data.KeywordDefinitions;
|
|
|
|
import magic.data.CubeDefinitions;
|
|
|
|
import magic.data.CardDefinitions;
|
|
|
|
import magic.data.UnimplementedParser;
|
|
|
|
import magic.data.GeneralConfig;
|
|
|
|
import magic.model.MagicGameLog;
|
|
|
|
import magic.utility.MagicFileSystem.DataPath;
|
|
|
|
|
2015-03-28 18:12:40 -07:00
|
|
|
import java.util.concurrent.ExecutorService;
|
2015-03-25 07:56:31 -07:00
|
|
|
import java.util.concurrent.Executors;
|
|
|
|
import java.util.concurrent.FutureTask;
|
|
|
|
import java.util.concurrent.ExecutionException;
|
2015-02-16 03:06:10 -08:00
|
|
|
import java.lang.management.ManagementFactory;
|
|
|
|
import java.lang.management.RuntimeMXBean;
|
|
|
|
import java.util.List;
|
2015-02-17 17:51:51 -08:00
|
|
|
import java.io.File;
|
2015-02-16 03:06:10 -08:00
|
|
|
|
2015-02-11 19:37:00 -08:00
|
|
|
final public class MagicSystem {
|
|
|
|
private MagicSystem() {}
|
2014-02-19 18:12:00 -08:00
|
|
|
|
|
|
|
public static final boolean IS_WINDOWS_OS = System.getProperty("os.name").toLowerCase().startsWith("windows");
|
2015-03-25 07:56:31 -07:00
|
|
|
private static final ProgressReporter reporter = new ProgressReporter();
|
2015-05-04 07:49:20 -07:00
|
|
|
|
|
|
|
private static final FutureTask<Void> loadCards = new LoadCardsFutureTask(new Runnable() {
|
|
|
|
@Override
|
2015-03-28 19:23:34 -07:00
|
|
|
public void run() {
|
|
|
|
initializeEngine(reporter);
|
|
|
|
}
|
2015-05-04 07:49:20 -07:00
|
|
|
});
|
2014-02-19 18:12:00 -08:00
|
|
|
|
2014-03-06 01:45:19 -08:00
|
|
|
public static boolean isTestGame() {
|
|
|
|
return (System.getProperty("testGame") != null);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static boolean isDevMode() {
|
2014-06-01 00:51:01 -07:00
|
|
|
return Boolean.getBoolean("devMode") || isDebugMode();
|
2014-03-06 01:45:19 -08:00
|
|
|
}
|
|
|
|
|
2014-05-11 12:11:06 -07:00
|
|
|
public static boolean isDebugMode() {
|
|
|
|
return Boolean.getBoolean("debug");
|
|
|
|
}
|
2014-09-18 10:35:09 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* add "-DparseMissing=true" VM argument for parsing scripts_missing folder.
|
|
|
|
*/
|
|
|
|
public static boolean isParseMissing() {
|
|
|
|
return Boolean.getBoolean("parseMissing");
|
|
|
|
}
|
2014-05-11 12:11:06 -07:00
|
|
|
|
2014-03-06 01:45:19 -08:00
|
|
|
/**
|
|
|
|
* add "-DselfMode=true" VM argument for AI vs AI mode.
|
|
|
|
*/
|
|
|
|
public static boolean isAiVersusAi() {
|
|
|
|
return Boolean.getBoolean("selfMode");
|
|
|
|
}
|
|
|
|
|
2014-08-29 03:45:40 -07:00
|
|
|
/**
|
|
|
|
* add "-DshowStats=true" to output startup statistics to console.
|
|
|
|
*/
|
|
|
|
public static boolean showStartupStats() {
|
|
|
|
return Boolean.getBoolean("showStats");
|
|
|
|
}
|
|
|
|
|
2015-02-16 03:06:10 -08:00
|
|
|
public static String getHeapUtilizationStats() {
|
|
|
|
final int mb = 1024*1024;
|
|
|
|
final Runtime runtime = Runtime.getRuntime();
|
|
|
|
return "Used Memory: " + (runtime.totalMemory() - runtime.freeMemory()) / mb + "M" +
|
|
|
|
"\nFree Memory: " + runtime.freeMemory() / mb + "M" +
|
|
|
|
"\nTotal Memory: " + runtime.totalMemory() / mb + "M" +
|
|
|
|
"\nMax Memory: " + runtime.maxMemory() / mb + "M";
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets VM arguments.
|
|
|
|
*/
|
|
|
|
public static String getRuntimeParameters() {
|
|
|
|
RuntimeMXBean bean = ManagementFactory.getRuntimeMXBean();
|
|
|
|
List<String> aList = bean.getInputArguments();
|
|
|
|
String params = "";
|
|
|
|
for (int i = 0; i < aList.size(); i++) {
|
|
|
|
params += aList.get(i) + "\n";
|
|
|
|
}
|
|
|
|
return params;
|
|
|
|
}
|
2015-03-24 18:57:49 -07:00
|
|
|
|
2015-03-25 07:56:31 -07:00
|
|
|
public static String getLoadingProgress() {
|
|
|
|
return reporter.getMessage();
|
2015-03-24 18:57:49 -07:00
|
|
|
}
|
2015-02-16 03:06:10 -08:00
|
|
|
|
2015-03-25 07:56:31 -07:00
|
|
|
public static void waitForAllCards() {
|
|
|
|
if (loadCards.isDone()) {
|
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
try {
|
|
|
|
loadCards.get();
|
|
|
|
} catch (final InterruptedException|ExecutionException ex) {
|
|
|
|
throw new RuntimeException(ex);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-24 18:57:49 -07:00
|
|
|
private static void initializeEngine(final ProgressReporter reporter) {
|
2015-03-24 18:44:09 -07:00
|
|
|
if (isParseMissing()) {
|
2015-02-17 17:51:51 -08:00
|
|
|
UnimplementedParser.parseScriptsMissing(reporter);
|
|
|
|
reporter.setMessage("Parsing card abilities...");
|
|
|
|
UnimplementedParser.parseCardAbilities();
|
2015-03-25 07:56:31 -07:00
|
|
|
|
2015-02-17 17:51:51 -08:00
|
|
|
}
|
|
|
|
CardDefinitions.loadCardDefinitions(reporter);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void initialize(final ProgressReporter reporter) {
|
2015-03-24 11:14:35 -07:00
|
|
|
|
2015-02-17 17:51:51 -08:00
|
|
|
// must load config here otherwise annotated card image theme-specifc
|
|
|
|
// icons are not loaded before the AbilityIcon class is initialized
|
|
|
|
// and you end up with the default icons instead.
|
|
|
|
GeneralConfig.getInstance().load();
|
|
|
|
|
|
|
|
final File gamePathFile = MagicFileSystem.getDataPath().toFile();
|
|
|
|
if (!gamePathFile.exists() && !gamePathFile.mkdir()) {
|
|
|
|
System.err.println("Unable to create directory " + gamePathFile.toString());
|
|
|
|
}
|
|
|
|
|
|
|
|
final File modsPathFile = MagicFileSystem.getDataPath(DataPath.MODS).toFile();
|
|
|
|
if (!modsPathFile.exists() && !modsPathFile.mkdir()) {
|
|
|
|
System.err.println("Unable to create directory " + modsPathFile.toString());
|
|
|
|
}
|
|
|
|
|
|
|
|
DeckUtils.createDeckFolder();
|
2015-03-24 11:14:35 -07:00
|
|
|
|
|
|
|
// setup the game log
|
|
|
|
reporter.setMessage("Initializing log...");
|
|
|
|
MagicGameLog.initialize();
|
2015-03-25 07:56:31 -07:00
|
|
|
|
|
|
|
// start a separate thread to load cards
|
2015-03-28 18:12:40 -07:00
|
|
|
final ExecutorService background = Executors.newSingleThreadExecutor();
|
|
|
|
background.execute(loadCards);
|
|
|
|
background.execute(new Runnable() {
|
|
|
|
public void run() {
|
|
|
|
CardDefinitions.postCardDefinitions();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
background.shutdown();
|
2015-03-25 07:56:31 -07:00
|
|
|
|
|
|
|
// if parse scripts missing or pre-load abilities then load cards synchronously
|
2015-03-24 18:44:09 -07:00
|
|
|
if (isParseMissing() || isDebugMode()) {
|
2015-03-25 07:56:31 -07:00
|
|
|
waitForAllCards();
|
2015-03-24 11:14:35 -07:00
|
|
|
}
|
2015-03-26 04:28:35 -07:00
|
|
|
|
|
|
|
if (isDebugMode()) {
|
|
|
|
reporter.setMessage("Loading card abilities...");
|
|
|
|
CardDefinitions.loadCardAbilities();
|
|
|
|
}
|
2015-03-24 11:14:35 -07:00
|
|
|
|
2015-03-24 18:44:09 -07:00
|
|
|
reporter.setMessage("Loading cube definitions...");
|
|
|
|
CubeDefinitions.loadCubeDefinitions();
|
|
|
|
reporter.setMessage("Loading deck generators...");
|
|
|
|
DeckGenerators.getInstance().loadDeckGenerators();
|
2015-03-24 11:14:35 -07:00
|
|
|
reporter.setMessage("Loading keyword definitions...");
|
|
|
|
KeywordDefinitions.getInstance().loadKeywordDefinitions();
|
2015-02-17 17:51:51 -08:00
|
|
|
}
|
2014-02-19 18:12:00 -08:00
|
|
|
}
|