public static T ReadChunk <T>(IntPtr h) where T : new() { byte[] b; int size; GCHandle pinnedArray; IntPtr addr; T type = new T(); string id; b = MPQ.ReadFile(h, 4); Array.Reverse(b); id = Encoding.Default.GetString(b); b = MPQ.ReadFile(h, 4); size = BitConverter.ToInt32(b, 0); b = MPQ.ReadFile(h, size); pinnedArray = GCHandle.Alloc(b, GCHandleType.Pinned); addr = pinnedArray.AddrOfPinnedObject(); type = (T)Marshal.PtrToStructure(addr, typeof(T)); pinnedArray.Free(); return(type); }
public static T Read <T>(IntPtr h) where T : new() { object ret; switch (Type.GetTypeCode(typeof(T))) { case TypeCode.Int32: ret = BitConverter.ToInt32(MPQ.ReadFile(h, 4), 0); break; case TypeCode.Int64: ret = BitConverter.ToInt64(MPQ.ReadFile(h, 8), 0); break; case TypeCode.Single: ret = BitConverter.ToSingle(MPQ.ReadFile(h, 4), 0); break; case TypeCode.Byte: ret = MPQ.ReadFile(h, 1)[0]; break; default: throw new NotSupportedException(typeof(T).FullName + " is not currently supported by Read<T>"); } return((T)ret); }
public static T ReadStruct <T>(IntPtr h) where T : new() { T ret = new T(); int size = Marshal.SizeOf(ret); Byte[] b = MPQ.ReadFile(h, size); GCHandle pinnedArray = GCHandle.Alloc(b, GCHandleType.Pinned); IntPtr addr = pinnedArray.AddrOfPinnedObject(); ret = (T)Marshal.PtrToStructure(addr, typeof(T)); pinnedArray.Free(); return(ret); }
public static void SkipChunk(IntPtr h) { byte[] b; int size; GCHandle pinnedArray; IntPtr addr; string id; b = MPQ.ReadFile(h, 4); Array.Reverse(b); id = Encoding.Default.GetString(b); b = MPQ.ReadFile(h, 4); size = BitConverter.ToInt32(b, 0); b = MPQ.ReadFile(h, size); }