Advertisement
Guest User

Untitled

a guest
Jan 22nd, 2019
278
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.71 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Runtime.InteropServices;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. using PhilLibX.IO;
  9.  
  10. namespace TyrantLib
  11. {
  12.     public partial class ResidentEvil2
  13.     {
  14.         public class Material
  15.         {
  16.             #region Constants
  17.             /// <summary>
  18.             /// PFB Magic Number/FourCC
  19.             /// </summary>
  20.             public const int Magic = 0x46444D;
  21.             #endregion
  22.  
  23.             /// <summary>
  24.             /// Material File Header
  25.             /// </summary>
  26.             [StructLayout(LayoutKind.Sequential, Pack = 1)]
  27.             public struct Header
  28.             {
  29.                 /// <summary>
  30.                 /// Magic
  31.                 /// </summary>
  32.                 public uint Magic { get; set; }
  33.  
  34.                 /// <summary>
  35.                 /// Unknown, probably version
  36.                 /// </summary>
  37.                 public ushort Unknown { get; set; }
  38.  
  39.                 /// <summary>
  40.                 /// Number of Materials
  41.                 /// </summary>
  42.                 public int MaterialCount { get; set; }
  43.  
  44.                 /// <summary>
  45.                 /// Null Padding
  46.                 /// </summary>
  47.                 [MarshalAs(UnmanagedType.ByValArray, SizeConst = 6)]
  48.                 public byte[] Padding;
  49.             }
  50.  
  51.             /// <summary>
  52.             /// Material Entry
  53.             /// </summary>
  54.             [StructLayout(LayoutKind.Sequential, Pack = 1)]
  55.             public struct MaterialEntry
  56.             {
  57.                 /// <summary>
  58.                 /// Offsets to the name of the Material
  59.                 /// </summary>
  60.                 public long NameOffset { get; set; }
  61.  
  62.                 /// <summary>
  63.                 /// Unknown bytes (What looks like a hash and some constants)
  64.                 /// </summary>
  65.                 [MarshalAs(UnmanagedType.ByValArray, SizeConst = 20)]
  66.                 public byte[] Unknown;
  67.  
  68.                 /// <summary>
  69.                 /// Number of Textures
  70.                 /// </summary>
  71.                 public int TextureCount { get; set; }
  72.  
  73.                 /// <summary>
  74.                 /// Null Padding
  75.                 /// </summary>
  76.                 private long Padding { get; set; }
  77.  
  78.                 /// <summary>
  79.                 /// Unknown Offset
  80.                 /// </summary>
  81.                 public long UnknownOffset { get; set; }
  82.  
  83.                 /// <summary>
  84.                 /// Pointer to the Texture Entries
  85.                 /// </summary>
  86.                 public long TexturesOffset { get; set; }
  87.  
  88.                 /// <summary>
  89.                 /// Unknown Offsets
  90.                 /// </summary>
  91.                 [MarshalAs(UnmanagedType.ByValArray, SizeConst = 16)]
  92.                 public byte[] UnknownOffsets;
  93.             }
  94.  
  95.             /// <summary>
  96.             /// Texture File Entry
  97.             /// </summary>
  98.             public struct TextureEntry
  99.             {
  100.                 /// <summary>
  101.                 /// Offset to Texture Type String (BaseColor, etc.)
  102.                 /// </summary>
  103.                 public long TextureTypeOffset { get; set; }
  104.  
  105.                 /// <summary>
  106.                 /// Hash of some kind, doesn't match MurMur results
  107.                 /// </summary>
  108.                 public long Hash { get; set; }
  109.  
  110.                 /// <summary>
  111.                 /// Texture Name Offset
  112.                 /// </summary>
  113.                 public long TextureNameOffset { get; set; }
  114.             }
  115.  
  116.             /// <summary>
  117.             /// Loads Materials from the Material File
  118.             /// </summary>
  119.             public static Dictionary<string, Dictionary<string, string>> LoadMaterialsFromFile(Stream stream)
  120.             {
  121.                 // Create reader
  122.                 using (var reader = new BinaryReader(stream))
  123.                 {
  124.                     var results = new Dictionary<string, Dictionary<string, string>>();
  125.                     // Read Header
  126.                     var header = reader.ReadStruct<Header>();
  127.                     // Read Materials
  128.                     var materials = reader.ReadArray<MaterialEntry>(header.MaterialCount);
  129.                     // Loop through them
  130.                     foreach(var material in materials)
  131.                     {
  132.                         // Read Material Name
  133.                         reader.Seek(material.NameOffset, SeekOrigin.Begin);
  134.                         string materialName = reader.ReadUTF16NullTerminatedString();
  135.                         // Set texture list
  136.                         results[materialName] = new Dictionary<string, string>();
  137.                         // Read Textures
  138.                         reader.Seek(material.TexturesOffset, SeekOrigin.Begin);
  139.                         var textures = reader.ReadArray<TextureEntry>(material.TextureCount);
  140.                         // Loop through them
  141.                         foreach(var texture in textures)
  142.                         {
  143.                             // Read Texture Type
  144.                             reader.Seek(texture.TextureTypeOffset, SeekOrigin.Begin);
  145.                             string textureType = reader.ReadUTF16NullTerminatedString();
  146.                             // Read Name
  147.                             reader.Seek(texture.TextureNameOffset, SeekOrigin.Begin);
  148.                             string textureName = reader.ReadUTF16NullTerminatedString();
  149.                             // Set it
  150.                             results[materialName][textureType] = textureName;
  151.                         }
  152.                     }
  153.                     // Done
  154.                     return results;
  155.                 }
  156.             }
  157.         }
  158.     }
  159. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement