Advertisement
Guest User

PakTool.cs

a guest
May 31st, 2013
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.94 KB | None | 0 0
  1. // @%WINDIR%\Microsoft.NET\Framework\v2.0.50727\csc PakTool.cs
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Text;
  5. using System.IO;
  6.  
  7. namespace PakTool
  8. {
  9.     class Program
  10.     {
  11.         public const ConsoleColor InfoColor = ConsoleColor.White;
  12.         public const ConsoleColor ToolColor = ConsoleColor.Gray;
  13.         public const ConsoleColor ErrorColor = ConsoleColor.Red;
  14.         public const ConsoleColor DefaultColor = ConsoleColor.Gray;
  15.         public const ConsoleColor WarningrColor = ConsoleColor.Yellow;
  16.  
  17.         public const int FileVersion = 4;
  18.         public const int FileHeaderSize = 9;
  19.         public const int ItemHeaderSize = 6;
  20.  
  21.         static int Main(string[] args)
  22.         {
  23.             try
  24.             {
  25.                 if (args.Length > 0)
  26.                 {
  27.                     if (args[0] == "decode" && Decode(args))
  28.                         return 0;
  29.                     else if (args[0] == "encode" && Encode(args))
  30.                         return 0;
  31.                 }
  32.  
  33.                 Error("Invalid parameters.");
  34.                 NewLine();
  35.                 Highlight("PakTool.exe decode -in filename [-out folder]");
  36.                 Highlight("PakTool.exe encode -in folder -out filename");
  37.                 return 1;
  38.             }
  39.             catch (Exception e)
  40.             {
  41.                 Console.WriteLine(e);
  42.                 return 1;
  43.             }
  44.         }
  45.  
  46.         public static bool Decode(string[] args)
  47.         {
  48.             string in1 = null;
  49.             string out1 = "output";
  50.  
  51.             for (int n = 1; n < args.Length; n++)
  52.             {
  53.                 if (args[n] == "-in" && n < args.Length - 1)
  54.                     in1 = args[++n];
  55.                 else if (args[n] == "-out" && n < args.Length - 1)
  56.                     out1 = args[++n];
  57.             }
  58.  
  59.             if (in1 == null)
  60.                 return false;
  61.  
  62.             try
  63.             {
  64.                 Directory.CreateDirectory(out1);
  65.  
  66.                 byte[] data = File.ReadAllBytes(in1);
  67.  
  68.                 if (data.Length < FileHeaderSize)
  69.                 {
  70.                     Error("Invalid file format.");
  71.                     return true;
  72.                 }
  73.  
  74.                 int version = BitConverter.ToInt32(data, 0);
  75.                 int count = BitConverter.ToInt32(data, 4);
  76.  
  77.                 if (version != FileVersion)
  78.                 {
  79.                     Error("Invalid file version [version is {0} instead of {1}].", version, FileVersion);
  80.                     return true;
  81.                 }
  82.  
  83.                 for (int n = 0, position = FileHeaderSize; n < count; n++, position += ItemHeaderSize)
  84.                 {
  85.                     int id = BitConverter.ToUInt16(data, position);
  86.                     int offset = BitConverter.ToInt32(data, position + 2);
  87.                     int size = BitConverter.ToInt32(data, position + 8) - offset;
  88.                     byte[] item = new byte[size];
  89.                     Array.Copy(data, offset, item, 0, size);
  90.                     string text = Encoding.ASCII.GetString(item);
  91.                     string filename = id.ToString("D5");
  92.                     if (item.Length >= 6 && Encoding.ASCII.GetString(item, 1, 5) == "PNG\r\n")
  93.                         filename += ".png";
  94.                     else if (item.Length >= 4 && Encoding.ASCII.GetString(item, 0, 4) == "RIFF")
  95.                         filename += ".wav";
  96.                     else if (item.Length >= 6 && Encoding.ASCII.GetString(item, 0, 6) == "GIF89a")
  97.                         filename += ".gif";
  98.                     else if (item.Length >= 2 && item[0] == 0xff && item[1] == 0xd8)
  99.                         filename += ".jpg";
  100.                     else if (item.Length > 0 && item[0] == '<')
  101.                         filename += ".html";
  102.                     else if (item.Length > 0 && item[0] == '/')
  103.                         filename += ".js";
  104.                     else if (item.Length > 0 && item[0] == '{')
  105.                         filename += ".json";
  106.                     else if (text.Contains("<body>") || text.Contains("<html>"))
  107.                         filename += ".html";
  108.                     else if (text.Contains("var ") || text.Contains("function ") || text.Contains("function"))
  109.                         filename += ".js";
  110.                     else if (text.Contains("px;"))
  111.                         filename += ".css";
  112.  
  113.                     File.WriteAllBytes(Path.Combine(out1, filename), item);
  114.                 }
  115.  
  116.             }
  117.             catch (Exception e)
  118.             {
  119.                 throw new Exception("Unable to decode input file.", e);
  120.             }
  121.  
  122.             return true;
  123.         }
  124.  
  125.         public static bool Encode(string[] args)
  126.         {
  127.             string in1 = null;
  128.             string out1 = null;
  129.  
  130.             for (int n = 1; n < args.Length; n++)
  131.             {
  132.                 if (args[n] == "-in" && n < args.Length - 1)
  133.                     in1 = args[++n];
  134.                 else if (args[n] == "-out" && n < args.Length - 1)
  135.                     out1 = args[++n];
  136.             }
  137.  
  138.             if (in1 == null || out1 == null)
  139.                 return false;
  140.  
  141.             try
  142.             {
  143.                 string[] files = Directory.GetFiles(in1);
  144.                 Array.Sort(files);
  145.  
  146.                 using (FileStream output = new FileStream(out1, FileMode.Create))
  147.                 using (BinaryWriter writer = new BinaryWriter(output))
  148.                 {
  149.                     writer.Write(FileVersion);
  150.                     writer.Write(files.Length);
  151.                     writer.Write((byte)1);
  152.  
  153.                     int offset = FileHeaderSize + (files.Length + 1) * ItemHeaderSize;
  154.                     for (int n = 0; n < files.Length; n++)
  155.                     {
  156.                         string file = files[n];
  157.                         int id = Convert.ToInt32(Path.GetFileNameWithoutExtension(file));
  158.                         writer.Write((ushort)id);
  159.                         writer.Write(offset);
  160.                         offset += (int)new FileInfo(file).Length;
  161.                     }
  162.  
  163.                     writer.Write((ushort)0);
  164.                     writer.Write(offset);
  165.  
  166.                     for (int n = 0; n < files.Length; n++)
  167.                     {
  168.                         string file = files[n];
  169.                         writer.Write(File.ReadAllBytes(file));
  170.                     }
  171.                 }
  172.  
  173.             }
  174.             catch (Exception e)
  175.             {
  176.                 throw new Exception("Unable to encode output file.", e);
  177.             }
  178.  
  179.             return true;
  180.         }
  181.  
  182.         public static void NewLine()
  183.         {
  184.             Console.WriteLine();
  185.         }
  186.  
  187.         public static void Text(string format, params object[] args)
  188.         {
  189.             Console.ForegroundColor = DefaultColor;
  190.             Console.WriteLine(format, args);
  191.         }
  192.  
  193.         public static void Highlight(string format, params object[] args)
  194.         {
  195.             Console.ForegroundColor = InfoColor;
  196.             Console.WriteLine(format, args);
  197.             Console.ForegroundColor = DefaultColor;
  198.         }
  199.  
  200.         public static void Warning(string format, params object[] args)
  201.         {
  202.             Console.ForegroundColor = WarningrColor;
  203.             Console.WriteLine(format, args);
  204.             Console.ForegroundColor = DefaultColor;
  205.         }
  206.  
  207.         public static void Error(string format, params object[] args)
  208.         {
  209.             Console.ForegroundColor = ErrorColor;
  210.             Console.WriteLine(format, args);
  211.             Console.ForegroundColor = DefaultColor;
  212.         }
  213.  
  214.         public static void Error(Exception e)
  215.         {
  216.             if (e.InnerException == null)
  217.                 Error("{0}", e.Message);
  218.             else
  219. #if DEBUG
  220.                 Error("{0} ----> {1}", e.Message, e.InnerException.ToString());
  221. #else
  222.                 Error("{0} ----> {1}", e.Message, e.InnerException.Message);
  223. #endif
  224.         }
  225.  
  226.     }
  227. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement