Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.IO.Compression;
- using System.IO.Packaging;
- using System.Linq;
- using System.Text;
- namespace Mp.Danmaku.Build
- {
- class Program
- {
- static void Main(string[] args)
- {
- string sourceDirectory = "";
- string destinationDirectory = "";
- string buildFilePath = "";
- ParseArguments(args, ref sourceDirectory, ref buildFilePath, ref destinationDirectory);
- var setFile = Path.Combine(sourceDirectory, "set");
- var cards = ParseSetFile(setFile);
- var decks = ParseBuildFile(buildFilePath);
- PopulateDecks(decks, cards);
- var deckSourcePrefix = GetDeckPrefix(setFile);
- var deckSourceSuffix = GetDeckSuffix(setFile);
- WriteSetFiles(decks, deckSourcePrefix, deckSourceSuffix, destinationDirectory);
- CopyImageFiles(decks, sourceDirectory, destinationDirectory);
- CompressToMseFiles(decks, destinationDirectory);
- }
- private static void CompressToMseFiles(List<Deck> decks, string destinationDirectory)
- {
- foreach (var item in decks)
- {
- var path = Path.Combine(destinationDirectory, BuildDeckPath(item));
- var setPath = Path.Combine(destinationDirectory, path + ".mse-set");
- using (Package package = Package.Open(setPath, FileMode.Create))
- {
- var packUri = PackUriHelper.CreatePartUri(new Uri("set", UriKind.Relative));
- PackagePart packagePartDocument = package.CreatePart(packUri, System.Net.Mime.MediaTypeNames.Text.Plain);
- using (FileStream fileStream = new FileStream(Path.Combine(path, "set"), FileMode.Open, FileAccess.Read))
- {
- CopyStream(fileStream, packagePartDocument.GetStream());
- }
- var images = new DirectoryInfo(Path.Combine(destinationDirectory, path)).GetFiles().Where(l => l.Name.StartsWith("image", StringComparison.OrdinalIgnoreCase));
- foreach (var currentImage in images)
- {
- var uri = PackUriHelper.CreatePartUri(new Uri(currentImage.Name, UriKind.Relative));
- PackagePart part = package.CreatePart(uri, System.Net.Mime.MediaTypeNames.Image.Jpeg);
- using (FileStream fileStream = new FileStream(currentImage.FullName, FileMode.Open, FileAccess.Read))
- {
- CopyStream(fileStream, part.GetStream());
- }
- }
- }
- }
- }
- private static void CopyStream(Stream source, Stream target)
- {
- const int bufSize = 0x1000;
- byte[] buf = new byte[bufSize];
- int bytesRead = 0;
- while ((bytesRead = source.Read(buf, 0, bufSize)) > 0)
- target.Write(buf, 0, bytesRead);
- }
- private static string BuildDeckPath(IEnumerable<string> path)
- {
- return path.Aggregate((workingPath, word) => workingPath = Path.Combine(workingPath, word)).Replace("#","");
- }
- private static void CopyImageFiles(List<Deck> decks, string sourceDirectory, string destinationDirectory)
- {
- foreach (var deck in decks)
- {
- var uniqueImages = (from item in deck.Cards
- select item.GetTagValue("image")).Distinct();
- var deckPath = deck.Aggregate((workingPath, next) => workingPath = Path.Combine(workingPath, next));
- deckPath = Path.Combine(destinationDirectory, deckPath);
- deckPath = deckPath.Replace("#", "");
- foreach (var image in uniqueImages)
- {
- var sourceFile = Path.Combine(sourceDirectory, image);
- var destinationFile = Path.Combine(deckPath, image);
- File.Copy(sourceFile, destinationFile, true);
- }
- }
- }
- private static void ParseArguments(string[] args, ref string sourceDirectory, ref string buildFilePath, ref string destinationDirectory)
- {
- for (int i = 0; i < args.Count(); i++)
- {
- if (args[i] == "-s")
- {
- i++;
- sourceDirectory = args[i];
- }
- else if (args[i] == "-b")
- {
- i++;
- buildFilePath = args[i];
- }
- else if (args[i] == "-d")
- {
- i++;
- destinationDirectory = args[i];
- }
- }
- }
- private static void WriteSetFiles(List<Deck> decks, List<string> deckSourcePrefix, List<string> deckSourceSuffix, string destinationDirectory)
- {
- foreach (var deck in decks)
- {
- List<string> outFile = new List<string>();
- outFile.AddRange(deckSourcePrefix);
- foreach (var card in deck.Cards)
- {
- outFile.AddRange(card);
- }
- outFile.AddRange(deckSourceSuffix);
- var deckPath = deck.Aggregate((workingPath, next) => workingPath = Path.Combine(workingPath, next));
- deckPath = Path.Combine(destinationDirectory, deckPath);
- deckPath = deckPath.Replace("#", "");
- Directory.CreateDirectory(deckPath);
- var filePath = Path.Combine(deckPath, "set");
- File.WriteAllLines(filePath, outFile.ToArray(), Encoding.UTF8);
- }
- }
- private static List<string> GetDeckSuffix(string setFile)
- {
- //hardcoded for now
- var suffix = new List<string>()
- {
- "version control:",
- "\ttype: none",
- "apprentice code: ",
- };
- return suffix;
- }
- private static List<string> GetDeckPrefix(string setFile)
- {
- var lines= File.ReadAllLines(setFile);
- var result = new List<string>();
- for (int i = 0; !lines[i].Contains("card:"); i++)
- {
- result.Add(lines[i]);
- }
- return result;
- }
- public static void PopulateDecks(IEnumerable<Deck> decks, IEnumerable<CardSource> cards)
- {
- Season currentSeason = Season.Spring;
- foreach (Deck currentDeck in decks)
- {
- foreach (CardCount cardListing in currentDeck.CardListing)
- {
- var cardDefinition = GetCard(cards, cardListing.Name);
- if(cardDefinition == null)
- {
- throw new Exception("I can't find the definition for the card " + cardListing.Name);
- }
- for (int i = 0; i < cardListing.Count; i++)
- {
- string[] contents = new string[cardDefinition.Count];
- cardDefinition.CopyTo(contents);
- var copy = new CardSource(contents);
- var season = copy.GetTagValue("season");
- if (String.IsNullOrEmpty(season)
- || season.Equals("No Season", StringComparison.OrdinalIgnoreCase)
- || season.Equals("None", StringComparison.OrdinalIgnoreCase))
- {
- copy.SetTagValue("season", currentSeason.ToString());
- }
- copy.SetTagValue("expansion", currentDeck[currentDeck.Count()-2].Replace("#",""));
- currentDeck.Cards.Add(copy);
- currentSeason = NextSeason(currentSeason);
- }
- }
- }
- }
- private static CardSource GetCard(IEnumerable<CardSource> cards, string name)
- {
- return (from item in cards
- where item.GetTagValue("name").Equals(name, StringComparison.OrdinalIgnoreCase)
- select item).FirstOrDefault();
- }
- private static Season NextSeason(Season currentSeason)
- {
- return (Season)((((int)currentSeason) + 1) % 4);
- }
- public static List<CardSource> ParseSetFile(string setFilePath)
- {
- var cards = new List<CardSource>();
- var setLines = File.ReadAllLines(setFilePath);
- CardSource currentCard = new CardSource();
- foreach (var item in setLines)
- {
- if (item.Equals("card:", StringComparison.OrdinalIgnoreCase))
- {
- currentCard = new CardSource();
- cards.Add(currentCard);
- }
- currentCard.Add(item);
- }
- return cards;
- }
- public static List<Deck> ParseBuildFile(string buildFilePath)
- {
- var decks = new List<Deck>();
- var buildLines = File.ReadAllLines(buildFilePath);
- Deck currentDeck = new Deck();
- foreach (var line in buildLines)
- {
- if (!String.IsNullOrEmpty(line) && line.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries).Count() > 0)
- {
- if (line.StartsWith("#"))
- {
- currentDeck = new Deck(line.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries));
- decks.Add(currentDeck);
- }
- else
- {
- currentDeck.CardListing.Add(new CardCount(line.Split(',')));
- }
- }
- }
- return decks;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment