Advertisement
Guest User

BinaryADTTextureReader2

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