Advertisement
Guest User

BinaryADTTextureReader

a guest
Jan 14th, 2012
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.19 KB | None | 0 0
  1. using System;
  2. using System.IO;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Runtime.InteropServices;
  7.  
  8. namespace BinaryReading
  9. {
  10.     class Program
  11.     {
  12.         static void Main(string[] args)
  13.         {
  14.             BinaryReader foo = new BinaryReader(File.Open("C:\\Users\\Exitium\\Desktop\\WoW WoTLK\\World\\Maps\\Maruum\\Maruum_52_16.adt", FileMode.Open));
  15.             MVER mver = ByteToType<MVER>(foo);
  16.             MHDR mhdr = ByteToType<MHDR>(foo);
  17.             foo.BaseStream.Seek(mhdr.mtex - 4, SeekOrigin.Begin);
  18.             string[] mtex = ReadMTEX(foo, mhdr.mmdx, mhdr.mtex);
  19.  
  20.             /*if (mver.magic == 'MVER' && mver.size == 4 && mver.version == 18)
  21.             {
  22.                 Console.WriteLine(mver.version);
  23.                 Console.WriteLine(mver.size);
  24.                 Console.WriteLine(mver.magic);
  25.                 Console.ReadLine();
  26.             }
  27.             else
  28.             {
  29.                 Console.WriteLine("This is no WoTLK ADT");
  30.                 Console.ReadLine();
  31.             }*/
  32.             foo.BaseStream.Seek(mhdr.mtex, SeekOrigin.Begin);
  33.             foreach (string str in mtex)
  34.             {
  35.                 Console.WriteLine(str);
  36.             }
  37.             Console.ReadLine();
  38.  
  39.         }
  40.  
  41.         public static string[] ReadMTEX(BinaryReader reader, int mhdr, int mtex)
  42.         {
  43.             char c;
  44.             int lines = 0;
  45.             do
  46.             {
  47.                 for (int i = Convert.ToInt32(reader.BaseStream.Position); i < reader.BaseStream.Length; i++)
  48.                 {
  49.                     if ((c = (char)reader.ReadByte()) == 0)
  50.                     {
  51.                         ++lines;
  52.                         break;
  53.                     }
  54.                 }
  55.             }
  56.             while (reader.BaseStream.Position < mhdr);
  57.  
  58.             reader.BaseStream.Seek(mtex - 4, SeekOrigin.Begin);
  59.             string[] result = new string[lines - 1];
  60.  
  61.             int tæller = 0;
  62.  
  63.             do
  64.             {
  65.                 for (int i = Convert.ToInt32(reader.BaseStream.Position); i < reader.BaseStream.Length; i++)
  66.                 {
  67.                     if ((c = (char)reader.ReadByte()) == 0)
  68.                     {
  69.                         if ((tæller + 2) < lines)
  70.                         {
  71.                             ++tæller;
  72.                         }
  73.                         break;
  74.                     }
  75.                     result[tæller] += c.ToString();
  76.                 }
  77.             }
  78.             while (reader.BaseStream.Position < mhdr);
  79.  
  80.             return result;
  81.         }
  82.  
  83.         public static T ByteToType<T>(BinaryReader reader)
  84.         {
  85.             byte[] bytes = reader.ReadBytes(Marshal.SizeOf(typeof(T)));
  86.  
  87.             GCHandle handle = GCHandle.Alloc(bytes, GCHandleType.Pinned);
  88.             T theStructure = (T)Marshal.PtrToStructure(handle.AddrOfPinnedObject(), typeof(T));
  89.             handle.Free();
  90.  
  91.             return theStructure;
  92.         }
  93.  
  94.         [StructLayout(LayoutKind.Explicit)]
  95.         public struct MVER
  96.         {
  97.             [FieldOffset(0)]
  98.             public int magic;
  99.             [FieldOffset(4)]
  100.             public int size;
  101.             [FieldOffset(8)]
  102.             public int version;
  103.         }
  104.  
  105.         [StructLayout(LayoutKind.Explicit)]
  106.         public struct MHDR
  107.         {
  108.             [FieldOffset(0)]
  109.             public int magic;
  110.             [FieldOffset(4)]
  111.             public int size;
  112.             [FieldOffset(8)]
  113.             public int flags;
  114.             [FieldOffset(12)]
  115.             public int mcin;
  116.             [FieldOffset(16)]
  117.             public int mtex;
  118.             [FieldOffset(20)]
  119.             public int mmdx;
  120.             [FieldOffset(24)]
  121.             public int mmid;
  122.             [FieldOffset(28)]
  123.             public int mwmo;
  124.             [FieldOffset(32)]
  125.             public int mwid;
  126.             [FieldOffset(36)]
  127.             public int mddf;
  128.             [FieldOffset(40)]
  129.             public int modf;
  130.             [FieldOffset(44)]
  131.             public int mfbo;
  132.             [FieldOffset(48)]
  133.             public int mh2o;
  134.             [FieldOffset(52)]
  135.             public int mtfx;
  136.         }
  137.  
  138.     }
  139. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement