Guest User

h

a guest
Nov 21st, 2015
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 10.25 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.IO.Compression;
  5. using System.IO.Packaging;
  6. using System.Linq;
  7. using System.Text;
  8.  
  9. namespace Mp.Danmaku.Build
  10. {
  11.     class Program
  12.     {
  13.         static void Main(string[] args)
  14.         {
  15.             string sourceDirectory = "";
  16.             string destinationDirectory = "";
  17.             string buildFilePath = "";
  18.  
  19.             ParseArguments(args, ref sourceDirectory, ref buildFilePath, ref destinationDirectory);
  20.  
  21.             var setFile = Path.Combine(sourceDirectory, "set");
  22.             var cards = ParseSetFile(setFile);
  23.  
  24.             var decks = ParseBuildFile(buildFilePath);
  25.  
  26.             PopulateDecks(decks, cards);
  27.  
  28.             var deckSourcePrefix = GetDeckPrefix(setFile);
  29.             var deckSourceSuffix = GetDeckSuffix(setFile);
  30.  
  31.             WriteSetFiles(decks, deckSourcePrefix, deckSourceSuffix, destinationDirectory);
  32.             CopyImageFiles(decks, sourceDirectory, destinationDirectory);
  33.             CompressToMseFiles(decks, destinationDirectory);
  34.            
  35.         }
  36.  
  37.         private static void CompressToMseFiles(List<Deck> decks, string destinationDirectory)
  38.         {
  39.             foreach (var item in decks)
  40.             {
  41.                 var path = Path.Combine(destinationDirectory, BuildDeckPath(item));
  42.                 var setPath = Path.Combine(destinationDirectory, path + ".mse-set");
  43.  
  44.                 using (Package package = Package.Open(setPath, FileMode.Create))
  45.                 {
  46.                     var packUri = PackUriHelper.CreatePartUri(new Uri("set", UriKind.Relative));
  47.  
  48.                     PackagePart packagePartDocument = package.CreatePart(packUri, System.Net.Mime.MediaTypeNames.Text.Plain);
  49.  
  50.                     using (FileStream fileStream = new FileStream(Path.Combine(path, "set"), FileMode.Open, FileAccess.Read))
  51.                     {
  52.                         CopyStream(fileStream, packagePartDocument.GetStream());
  53.                     }
  54.  
  55.                     var images = new DirectoryInfo(Path.Combine(destinationDirectory, path)).GetFiles().Where(l => l.Name.StartsWith("image", StringComparison.OrdinalIgnoreCase));
  56.  
  57.                     foreach (var currentImage in images)
  58.                     {
  59.                         var uri = PackUriHelper.CreatePartUri(new Uri(currentImage.Name, UriKind.Relative));
  60.  
  61.                         PackagePart part = package.CreatePart(uri, System.Net.Mime.MediaTypeNames.Image.Jpeg);
  62.  
  63.                         using (FileStream fileStream = new FileStream(currentImage.FullName, FileMode.Open, FileAccess.Read))
  64.                         {
  65.                             CopyStream(fileStream, part.GetStream());
  66.                         }
  67.                     }
  68.  
  69.                 }
  70.  
  71.  
  72.                
  73.             }
  74.         }
  75.  
  76.         private static void CopyStream(Stream source, Stream target)
  77.         {
  78.             const int bufSize = 0x1000;
  79.             byte[] buf = new byte[bufSize];
  80.             int bytesRead = 0;
  81.             while ((bytesRead = source.Read(buf, 0, bufSize)) > 0)
  82.                 target.Write(buf, 0, bytesRead);
  83.         }
  84.  
  85.         private static string BuildDeckPath(IEnumerable<string> path)
  86.         {
  87.             return path.Aggregate((workingPath, word) => workingPath = Path.Combine(workingPath, word)).Replace("#","");
  88.         }
  89.  
  90.         private static void CopyImageFiles(List<Deck> decks, string sourceDirectory, string destinationDirectory)
  91.         {
  92.             foreach (var deck in decks)
  93.             {
  94.                 var uniqueImages = (from item in deck.Cards
  95.                                     select item.GetTagValue("image")).Distinct();
  96.  
  97.                 var deckPath = deck.Aggregate((workingPath, next) => workingPath = Path.Combine(workingPath, next));
  98.                 deckPath = Path.Combine(destinationDirectory, deckPath);
  99.                 deckPath = deckPath.Replace("#", "");
  100.  
  101.                 foreach (var image in uniqueImages)
  102.                 {
  103.                     var sourceFile = Path.Combine(sourceDirectory, image);
  104.                     var destinationFile = Path.Combine(deckPath, image);
  105.  
  106.                     File.Copy(sourceFile, destinationFile, true);
  107.                 }
  108.             }
  109.         }
  110.  
  111.         private static void ParseArguments(string[] args, ref string sourceDirectory, ref string buildFilePath, ref string destinationDirectory)
  112.         {
  113.             for (int i = 0; i < args.Count(); i++)
  114.             {
  115.                 if (args[i] == "-s")
  116.                 {
  117.                     i++;
  118.                     sourceDirectory = args[i];
  119.                 }
  120.                 else if (args[i] == "-b")
  121.                 {
  122.                     i++;
  123.                     buildFilePath = args[i];
  124.                 }
  125.                 else if (args[i] == "-d")
  126.                 {
  127.                     i++;
  128.                     destinationDirectory = args[i];
  129.                 }
  130.             }
  131.         }
  132.  
  133.         private static void WriteSetFiles(List<Deck> decks, List<string> deckSourcePrefix, List<string> deckSourceSuffix, string destinationDirectory)
  134.         {
  135.             foreach (var deck in decks)
  136.             {
  137.                 List<string> outFile = new List<string>();
  138.  
  139.                 outFile.AddRange(deckSourcePrefix);
  140.  
  141.                 foreach (var card in deck.Cards)
  142.                 {
  143.                     outFile.AddRange(card);
  144.                 }
  145.  
  146.                 outFile.AddRange(deckSourceSuffix);
  147.                
  148.                 var deckPath = deck.Aggregate((workingPath, next) => workingPath = Path.Combine(workingPath, next));
  149.                 deckPath = Path.Combine(destinationDirectory, deckPath);
  150.                 deckPath = deckPath.Replace("#", "");
  151.  
  152.                 Directory.CreateDirectory(deckPath);
  153.  
  154.                 var filePath = Path.Combine(deckPath, "set");
  155.                 File.WriteAllLines(filePath, outFile.ToArray(), Encoding.UTF8);
  156.             }
  157.         }
  158.  
  159.         private static List<string> GetDeckSuffix(string setFile)
  160.         {
  161.             //hardcoded for now
  162.             var suffix = new List<string>()
  163.             {
  164.                 "version control:",
  165.                 "\ttype: none",
  166.                 "apprentice code: ",
  167.             };
  168.  
  169.             return suffix;
  170.         }
  171.  
  172.         private static List<string> GetDeckPrefix(string setFile)
  173.         {
  174.             var lines= File.ReadAllLines(setFile);
  175.  
  176.             var result = new List<string>();
  177.  
  178.             for (int i = 0; !lines[i].Contains("card:"); i++)
  179.             {
  180.                 result.Add(lines[i]);
  181.             }
  182.  
  183.             return result;
  184.         }
  185.  
  186.         public static void PopulateDecks(IEnumerable<Deck> decks, IEnumerable<CardSource> cards)
  187.         {
  188.             Season currentSeason = Season.Spring;
  189.  
  190.  
  191.  
  192.             foreach (Deck currentDeck in decks)
  193.             {
  194.                 foreach (CardCount cardListing in currentDeck.CardListing)
  195.                 {
  196.                     var cardDefinition = GetCard(cards, cardListing.Name);
  197.  
  198.                     if(cardDefinition == null)
  199.                     {
  200.                         throw new Exception("I can't find the definition for the card " + cardListing.Name);
  201.                     }
  202.  
  203.                     for (int i = 0; i < cardListing.Count; i++)
  204.                     {
  205.                         string[] contents = new string[cardDefinition.Count];
  206.                         cardDefinition.CopyTo(contents);
  207.                         var copy = new CardSource(contents);
  208.  
  209.                         var season = copy.GetTagValue("season");
  210.                         if (String.IsNullOrEmpty(season)
  211.                             || season.Equals("No Season", StringComparison.OrdinalIgnoreCase)
  212.                              || season.Equals("None", StringComparison.OrdinalIgnoreCase))
  213.                         {
  214.                             copy.SetTagValue("season", currentSeason.ToString());
  215.                         }
  216.                        
  217.                         copy.SetTagValue("expansion", currentDeck[currentDeck.Count()-2].Replace("#",""));
  218.                        
  219.                         currentDeck.Cards.Add(copy);
  220.  
  221.                         currentSeason = NextSeason(currentSeason);
  222.                     }
  223.                 }
  224.             }
  225.         }
  226.  
  227.  
  228.         private static CardSource GetCard(IEnumerable<CardSource> cards, string name)
  229.         {
  230.             return (from item in cards
  231.                    where item.GetTagValue("name").Equals(name, StringComparison.OrdinalIgnoreCase)
  232.                    select item).FirstOrDefault();
  233.         }
  234.  
  235.         private static Season NextSeason(Season currentSeason)
  236.         {
  237.             return (Season)((((int)currentSeason) + 1) % 4);
  238.         }
  239.  
  240.         public static List<CardSource> ParseSetFile(string setFilePath)
  241.         {
  242.             var cards = new List<CardSource>();
  243.  
  244.             var setLines = File.ReadAllLines(setFilePath);
  245.  
  246.             CardSource currentCard = new CardSource();
  247.             foreach (var item in setLines)
  248.             {
  249.                 if (item.Equals("card:", StringComparison.OrdinalIgnoreCase))
  250.                 {
  251.                     currentCard = new CardSource();
  252.                     cards.Add(currentCard);
  253.                 }
  254.  
  255.                 currentCard.Add(item);
  256.             }
  257.  
  258.             return cards;
  259.         }
  260.  
  261.         public static List<Deck> ParseBuildFile(string buildFilePath)
  262.         {
  263.             var decks = new List<Deck>();
  264.  
  265.             var buildLines = File.ReadAllLines(buildFilePath);
  266.  
  267.             Deck currentDeck = new Deck();
  268.  
  269.             foreach (var line in buildLines)
  270.             {
  271.                 if (!String.IsNullOrEmpty(line) && line.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries).Count() > 0)
  272.                 {
  273.                     if (line.StartsWith("#"))
  274.                     {
  275.                         currentDeck = new Deck(line.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries));
  276.                         decks.Add(currentDeck);
  277.                     }
  278.                     else
  279.                     {
  280.                         currentDeck.CardListing.Add(new CardCount(line.Split(',')));
  281.                     }
  282.                 }
  283.             }
  284.  
  285.             return decks;
  286.         }
  287.     }
  288. }
Advertisement
Add Comment
Please, Sign In to add comment