MexikanoS

C# - Martial Heroes Map Merger

Dec 28th, 2015
403
1
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.20 KB | None | 1 0
  1. using System;
  2. using System.IO;
  3. using System.Linq;
  4. using System.Drawing;
  5. using System.Windows.Forms;
  6. using System.Collections.Generic;
  7.  
  8. class FileInfo
  9. {
  10.     public string filePath
  11.     {
  12.         get;
  13.         set;
  14.     }
  15.     public int line
  16.     {
  17.         get;
  18.         set;
  19.     }
  20.     public int row
  21.     {
  22.         get;
  23.         set;
  24.     }
  25. }
  26.  
  27. static class MapMerger
  28. {
  29.     public static Dictionary<int, List<FileInfo>> files = new Dictionary<int, List<FileInfo>>();
  30.     public static Dictionary<int, Point> mapSizeStart = new Dictionary<int, Point>();
  31.     public static Dictionary<int, Point> mapSizeEnd = new Dictionary<int, Point>();
  32.  
  33.     [STAThread]
  34.     static void Main(string[] args)
  35.     {
  36.         Console.Title = "MH Map Merger";
  37.     SelectInputFolder:
  38.         Console.WriteLine("Select a folder with map images - \"data/effect/map\".");
  39.         FolderBrowserDialog _fBD = new FolderBrowserDialog();
  40.         DialogResult _openDataInf = DialogResult.None;
  41.         while(_openDataInf != DialogResult.OK)
  42.         {
  43.             _openDataInf = _fBD.ShowDialog();
  44.             if(_openDataInf == DialogResult.Cancel)
  45.             {
  46.                 Environment.Exit(0);
  47.             }
  48.         }
  49.         string[] _files = Directory.GetFiles(@_fBD.SelectedPath + "/", "d*x*");
  50.         if(_files.Count() == 0)
  51.         {
  52.             Console.WriteLine("Looks like you've selected a wrong folder.");
  53.             goto SelectInputFolder;
  54.         }
  55.         foreach(string i in Directory.GetFiles(@_fBD.SelectedPath + "/", "d*x*"))
  56.         {
  57.             addToMaps(Convert.ToInt32(Path.GetFileNameWithoutExtension(i).Substring(1, 3)), i);
  58.         }
  59.         Dictionary<int, List<FileInfo>> tempSort = new Dictionary<int, List<FileInfo>>();
  60.         foreach(KeyValuePair<int, List<FileInfo>> entry in files)
  61.         {
  62.             tempSort[entry.Key] = entry.Value.OrderBy(w => w.row).ThenBy(w => w.line).ToList();
  63.         }
  64.         files = tempSort;
  65.         Console.WriteLine("Selected \"{0}\" map folder with {1} items.", _fBD.SelectedPath, files.Count);
  66.         Console.WriteLine("Select a destination path.");
  67.  
  68.         FolderBrowserDialog folderDialog = new FolderBrowserDialog();
  69.  
  70.         string destinationPath = null;
  71.         _openDataInf = DialogResult.None;
  72.         while(_openDataInf != DialogResult.OK)
  73.         {
  74.             _openDataInf = folderDialog.ShowDialog();
  75.             if(_openDataInf == DialogResult.Cancel)
  76.             {
  77.                 Environment.Exit(0);
  78.                 return;
  79.             }
  80.             destinationPath = folderDialog.SelectedPath;
  81.         }
  82.         Console.WriteLine("Selected a destination path:\r\n{0}.", destinationPath);
  83.  
  84.         int index = 1;
  85.         Console.Title = "MH Map Merger " + index + "/" + files.Count;
  86.  
  87.         Bitmap b = new Bitmap(128, 128);
  88.         for(int h = 0;h < 128;h++)
  89.             for(int g = 0;g < 128;g++)
  90.                 b.SetPixel(h, g, Color.Black);
  91.         Image black = new Bitmap(b, new Size(128, 128));
  92.  
  93.         foreach(KeyValuePair<int, List<FileInfo>> entry in files)
  94.         {
  95.             int x = (mapSizeEnd[entry.Key].X - mapSizeStart[entry.Key].X) + 1;
  96.             int y = (mapSizeEnd[entry.Key].Y - mapSizeStart[entry.Key].Y) + 1;
  97.             Image _container = new Bitmap(x * 128, y * 128);
  98.             for(int i = 0;i < x;i++)
  99.             {
  100.                 for(int z = 0;z < y;z++)
  101.                 {
  102.                     Image tmp = null;
  103.                     FileInfo tmpzzz = findFileInfo(entry.Key, z + mapSizeStart[entry.Key].Y, i + mapSizeStart[entry.Key].X);
  104.                     if(tmpzzz != null)
  105.                     {
  106.                         tmp = new Bitmap(tmpzzz.filePath);
  107.                         tmp = new Bitmap(tmp, new Size(128, 128));
  108.                     }
  109.                     else
  110.                     {
  111.                         tmp = black;
  112.                     }
  113.  
  114.                     Bitmap _map = new Bitmap(tmp);
  115.                     using(Graphics g = Graphics.FromImage(_container))
  116.                     {
  117.                         g.DrawImage(_map, (i * 128), (z * 128));
  118.                     }
  119.                 }
  120.             }
  121.             Bitmap tempy = new Bitmap(_container);
  122.             tempy.Save(destinationPath + "/m" + entry.Key + ".jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
  123.             index++;
  124.             Console.Title = "MH Map Merger " + index + "/" + files.Count;
  125.         }
  126.  
  127.         Console.Title = "MH Map Merger - Done";
  128.         Console.WriteLine("Extracted {0} map files.", files.Count);
  129.         Console.ReadKey();
  130.     }
  131.  
  132.     private static FileInfo findFileInfo(int map, int line, int row)
  133.     {
  134.         return files[map].Find(w => w.line == line && w.row == row);
  135.     }
  136.  
  137.     private static void addToMaps(int map, string filePath)
  138.     {
  139.         if(Path.GetFileNameWithoutExtension(filePath).Contains("_"))
  140.             return;
  141.         if(!files.ContainsKey(map))
  142.             files.Add(map, new List<FileInfo>());
  143.         if(!mapSizeStart.ContainsKey(map))
  144.             mapSizeStart.Add(map, new Point(-1, -1));
  145.         if(!mapSizeEnd.ContainsKey(map))
  146.             mapSizeEnd.Add(map, new Point(-1, -1));
  147.         FileInfo fInfo = new FileInfo();
  148.         fInfo.filePath = filePath;
  149.         string fileName = Path.GetFileNameWithoutExtension(filePath);
  150.         fInfo.line = Convert.ToInt32(fileName.Substring(fileName.IndexOf("z") + 1));
  151.         fInfo.row = Convert.ToInt32(fileName.Substring(fileName.IndexOf("x") + 1, fileName.IndexOf("z") - fileName.IndexOf("x") - 1));
  152.         countNewOffsetsForMap(map, new Point(fInfo.row, fInfo.line));
  153.         files[map].Add(fInfo);
  154.     }
  155.  
  156.     private static void countNewOffsetsForMap(int map, Point point)
  157.     {
  158.         if(mapSizeStart[map].X == -1)
  159.             mapSizeStart[map] = point;
  160.         if(mapSizeEnd[map].X == -1)
  161.             mapSizeEnd[map] = point;
  162.         if(mapSizeStart[map].X > point.X)
  163.             mapSizeStart[map] = new Point(point.X, mapSizeStart[map].Y);
  164.         if(mapSizeStart[map].Y > point.Y)
  165.             mapSizeStart[map] = new Point(mapSizeStart[map].X, point.Y);
  166.         if(mapSizeEnd[map].X < point.X)
  167.             mapSizeEnd[map] = new Point(point.X, mapSizeEnd[map].Y);
  168.         if(mapSizeEnd[map].Y < point.Y)
  169.             mapSizeEnd[map] = new Point(mapSizeEnd[map].X, point.Y);
  170.     }
  171. }
Advertisement
Add Comment
Please, Sign In to add comment