MexikanoS

C# - Martial Heroes VFS Extractor

Dec 13th, 2015
580
1
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.36 KB | None | 1 0
  1. using System;
  2. using System.IO;
  3. using System.Windows.Forms;
  4. using System.Collections.Generic;
  5.  
  6. class vfsExtract
  7. {
  8.     enum MHTypes
  9.     {
  10.         None,
  11.         MartialHeroes,
  12.         DOOnline
  13.     }
  14.  
  15.     [STAThread]
  16.     static void Main(string[] args)
  17.     {
  18.         Console.Title = "MH Extractor";
  19.  
  20.         OpenFileDialog openFileDialog = new OpenFileDialog();
  21.         openFileDialog.Filter = "Data file (.inf)|*.inf";
  22.         openFileDialog.FilterIndex = 1;
  23.  
  24. OpenInfFile:
  25.         Console.WriteLine("Provide an .inf file path.");
  26.  
  27.         string dataInfPath = null;
  28.         DialogResult _openDataInf = DialogResult.None;
  29.         while(_openDataInf != DialogResult.OK)
  30.         {
  31.             _openDataInf = openFileDialog.ShowDialog();
  32.             if(_openDataInf == DialogResult.Cancel)
  33.             {
  34.                 Environment.Exit(0);
  35.                 return;
  36.             }
  37.             dataInfPath = openFileDialog.FileName;
  38.         }
  39.         Console.WriteLine("Selected .inf file: {0}.", dataInfPath);
  40.  
  41.         byte[] bytes = File.ReadAllBytes(dataInfPath);
  42.         if(bytes.Length < 24)
  43.         {
  44.             Console.WriteLine("The file you provided is either wrong or corrupt.");
  45.             goto OpenInfFile;
  46.         }
  47.  
  48.         MHTypes _mhType = MHTypes.None;
  49.         _mhType = bytes[20] == 0x0 ? MHTypes.DOOnline : MHTypes.MartialHeroes;
  50.         if(_mhType == MHTypes.None)
  51.         {
  52.             Environment.Exit(0);
  53.             return;
  54.         }
  55.  
  56.         int files = BitConverter.ToInt32(bytes, 12);
  57.         Console.WriteLine("Files found: {0}.", files);
  58.  
  59.         List<string> fileNames = new List<string>(files);
  60.         List<Tuple<Int64, int>> offLength = new List<Tuple<Int64, int>>(files);
  61.  
  62.         int position = 0;
  63.         switch(_mhType)
  64.         {
  65.             case MHTypes.DOOnline:
  66.                 position = 24;
  67.                 break;
  68.             case MHTypes.MartialHeroes:
  69.                 position = 20;
  70.                 break;
  71.         }
  72.         while(position < bytes.Length)
  73.         {
  74.             string name = "";
  75.             for(int i = 0;i < 104;i++)
  76.             {
  77.                 if(bytes[position + i] == 0x00)
  78.                 {
  79.                     break;
  80.                 }
  81.                 name += (char)bytes[position + i];
  82.             }
  83.  
  84.             try
  85.             {
  86.                 fileNames.Add(name);
  87.                 switch(_mhType)
  88.                 {
  89.                     case MHTypes.DOOnline:
  90.                         offLength.Add(new Tuple<Int64, int>(BitConverter.ToInt64(bytes, position + 104), BitConverter.ToInt32(bytes, position + 112)));
  91.                         break;
  92.                     case MHTypes.MartialHeroes:
  93.                         offLength.Add(new Tuple<Int64, int>(BitConverter.ToInt32(bytes, position + 100), BitConverter.ToInt32(bytes, position + 104)));
  94.                         break;
  95.                 }
  96.             }
  97.             catch(ArgumentException)
  98.             {
  99.                 Console.WriteLine("It seems, that you've choosed other version of Martial Heroes inf file, than the required one.");
  100.                 Environment.Exit(0);
  101.                 return;
  102.             }
  103.  
  104.             position += _mhType == MHTypes.DOOnline ? 144 : _mhType == MHTypes.MartialHeroes ? 132 : 0;
  105.         }
  106.  
  107.         if(fileNames.Count == 0)
  108.         {
  109.             Console.WriteLine("Looks like, you've provided wrong .inf file.");
  110.             goto OpenInfFile;
  111.         }
  112.  
  113.         Console.WriteLine("Select a destination path.");
  114.  
  115.         FolderBrowserDialog folderDialog = new FolderBrowserDialog();
  116.  
  117.         string destinationPath = null;
  118.         _openDataInf = DialogResult.None;
  119.         while(_openDataInf != DialogResult.OK)
  120.         {
  121.             _openDataInf = folderDialog.ShowDialog();
  122.             if(_openDataInf == DialogResult.Cancel)
  123.             {
  124.                 Environment.Exit(0);
  125.                 return;
  126.             }
  127.             destinationPath = folderDialog.SelectedPath;
  128.         }
  129.         Console.WriteLine("Selected a destination path:\r\n{0}.", destinationPath);
  130.  
  131.         BinaryReader fstream = new BinaryReader(File.OpenRead(/*vfsPath*/Path.ChangeExtension(Path.GetDirectoryName(dataInfPath) + "/data/" + Path.GetFileName(dataInfPath), ".vfs")));
  132.         for(int i = 0;i < fileNames.Count;i++)
  133.         {
  134.             if(!Directory.Exists(destinationPath + "\\" + Path.GetDirectoryName(fileNames[i])))
  135.             {
  136.                 Directory.CreateDirectory(destinationPath + "\\" + Path.GetDirectoryName(fileNames[i]));
  137.             }
  138.  
  139.             fstream.BaseStream.Seek(offLength[i].Item1, SeekOrigin.Begin);
  140.  
  141.             try
  142.             {
  143.                 using(FileStream vfsStream = new FileStream(destinationPath + "\\" + fileNames[i], FileMode.Create))
  144.                 using(BinaryWriter bw = new BinaryWriter(vfsStream))
  145.                 {
  146.                     vfsStream.Write(fstream.ReadBytes((int)offLength[i].Item2), 0, (int)offLength[i].Item2);
  147.                     Console.Title = "MH Extractor " + (i + 1) + "/" + fileNames.Count;
  148.                 }
  149.             }
  150.             catch(Exception e)
  151.             {
  152.                 Console.WriteLine("Could not write the file: {0}.", destinationPath + "\\" + fileNames[i]);
  153.                 Console.WriteLine(e);
  154.                 Environment.Exit(0);
  155.                 return;
  156.             }
  157.         }
  158.         fstream.Close();
  159.  
  160.         Console.Title = "MH Extractor - Done";
  161.         Console.WriteLine("Extraction has been finished.");
  162.         Console.ReadKey();
  163.     }
  164. }
Advertisement
Add Comment
Please, Sign In to add comment