mtg-genetic-deckbuilding

Generating and improving Magic: The Gathering decks using a genetic algorithm
git clone https://kevincorvisier.fr/git/mtg-genetic-deckbuilding.git
Log | Files | Refs | LICENSE

Main.java (3517B)


      1 package fr.kevincorvisier.mtg.gdb;
      2 
      3 import java.io.BufferedInputStream;
      4 import java.io.File;
      5 import java.io.FileOutputStream;
      6 import java.io.IOException;
      7 import java.net.URISyntaxException;
      8 import java.net.URL;
      9 import java.nio.file.Files;
     10 
     11 import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
     12 import org.apache.commons.compress.archivers.tar.TarArchiveInputStream;
     13 import org.apache.commons.compress.compressors.bzip2.BZip2CompressorInputStream;
     14 import org.apache.commons.lang3.StringUtils;
     15 import org.springframework.context.annotation.AnnotationConfigApplicationContext;
     16 
     17 import com.google.common.io.ByteStreams;
     18 
     19 import dev.dirs.ProjectDirectories;
     20 import forge.gui.GuiBase;
     21 import forge.localinstance.properties.ForgeConstants;
     22 import forge.localinstance.properties.ForgePreferences;
     23 import forge.model.FModel;
     24 import forge.util.FileUtil;
     25 import fr.kevincorvisier.mtg.gdb.ai.GeneticAlgorithm;
     26 import lombok.extern.slf4j.Slf4j;
     27 
     28 @Slf4j
     29 public class Main
     30 {
     31 	private static final GuiFake GUI_INTERFACE = new GuiFake()
     32 	{
     33 		@Override
     34 		public String getAssetsDir()
     35 		{
     36 			try
     37 			{
     38 				final File dataDir = new File(ProjectDirectories.from("", "", "mtg-genetic-deckbuilding").dataDir);
     39 				final File resDir = new File(dataDir, "res");
     40 				final File versionFile = new File(dataDir, "version");
     41 
     42 				if (!resDir.exists() || !(versionFile.exists() && "1.6.65".equals(Files.readString(versionFile.toPath()))))
     43 				{
     44 					FileUtil.deleteDirectory(resDir);
     45 					FileUtil.deleteDirectory(versionFile);
     46 					downloadForgeRes(dataDir);
     47 					Files.writeString(versionFile.toPath(), "1.6.65");
     48 				}
     49 
     50 				return resDir.getParentFile().getCanonicalPath() + File.separator;
     51 			}
     52 			catch (final Exception e)
     53 			{
     54 				log.error("Error while searching for assetsDir", e);
     55 			}
     56 
     57 			return "";
     58 		}
     59 	};
     60 
     61 	private static final String FORGE_RELEASE = "https://github.com/Card-Forge/forge/releases/download/forge-1.6.65/forge-gui-desktop-1.6.65.tar.bz2";
     62 	private static final String[] FORGE_RESOURCES = { "res/ai/", "res/cardsfolder/", "res/deckgendecks/", "res/editions/", "res/formats/",
     63 			"res/languages/en-US.properties", "res/tokenscripts/" };
     64 
     65 	public static void downloadForgeRes(final File dest) throws IOException
     66 	{
     67 		log.info("Downloading forge release: {}", FORGE_RELEASE);
     68 
     69 		try (final TarArchiveInputStream zis = new TarArchiveInputStream(
     70 				new BZip2CompressorInputStream(new BufferedInputStream(new URL(FORGE_RELEASE).openStream()))))
     71 		{
     72 			TarArchiveEntry entry;
     73 			while ((entry = zis.getNextEntry()) != null)
     74 			{
     75 				if (!entry.isDirectory() && StringUtils.startsWithAny(entry.getName(), FORGE_RESOURCES))
     76 				{
     77 					log.info("Extracting {}", entry.getName());
     78 					final File file = new File(dest, entry.getName());
     79 					file.getParentFile().mkdirs();
     80 					ByteStreams.copy(zis, new FileOutputStream(file));
     81 				}
     82 			}
     83 		}
     84 	}
     85 
     86 	public static void main(final String[] args) throws URISyntaxException
     87 	{
     88 		try
     89 		{
     90 			GuiBase.setInterface(GUI_INTERFACE);
     91 
     92 			log.info("AssetsDir: {}", ForgeConstants.ASSETS_DIR);
     93 
     94 			FModel.initialize(null, preferences -> {
     95 				preferences.setPref(ForgePreferences.FPref.LOAD_CARD_SCRIPTS_LAZILY, false);
     96 				return null;
     97 			});
     98 
     99 			try (final AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(ApplicationConfig.class))
    100 			{
    101 				final GeneticAlgorithm ga = context.getBean(GeneticAlgorithm.class); // 2
    102 				ga.run();
    103 			}
    104 		}
    105 		catch (final Exception e)
    106 		{
    107 			log.error("Uncatched exception in main thread, stopping", e);
    108 		}
    109 	}
    110 }