Advertisement
Guest User

Untitled

a guest
May 23rd, 2017
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.88 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Reflection;
  6. using System.IO;
  7.  
  8. namespace Sandbox.Utils.Storage
  9. {
  10.     public class DBCFile<T> where T : new()
  11.     {
  12.         public SortedList<uint, T> Entries
  13.         {
  14.             get;
  15.             private
  16.             set;
  17.         }
  18.  
  19.         public DBCFile()
  20.         {
  21.             Entries = new SortedList<uint, T>();
  22.         }
  23.  
  24.         public bool InitFile(string fileName)
  25.         {
  26.             BinaryReader reader = null;
  27.             try
  28.             {
  29.                 reader = new BinaryReader(File.Open(fileName, FileMode.Open));
  30.             }
  31.             catch (Exception)
  32.             {
  33.                 Console.WriteLine("File {0} could not be opened!", fileName);
  34.                 return false;
  35.             }
  36.  
  37.             reader.BaseStream.Position = 4;
  38.             uint numRecords = reader.ReadUInt32();
  39.             uint numFields = reader.ReadUInt32();
  40.             uint recordSize = reader.ReadUInt32();
  41.             uint stringSize = reader.ReadUInt32();
  42.  
  43.             Type t = typeof(T);
  44.             PropertyInfo[] infos = t.GetProperties();
  45.             uint realNumFields = 0;
  46.             foreach (PropertyInfo inf in infos)
  47.             {
  48.                 if (inf.PropertyType.Equals(typeof(float)))
  49.                     ++realNumFields;
  50.                 else if (inf.PropertyType.Equals(typeof(uint)))
  51.                     ++realNumFields;
  52.                 else if (inf.PropertyType.Equals(typeof(int)))
  53.                     ++realNumFields;
  54.                 else if (inf.PropertyType.Equals(typeof(string)))
  55.                     realNumFields += 17;
  56.                 else if (inf.PropertyType.Equals(typeof(StringBuilder)))
  57.                     ++realNumFields;
  58.                 else if (inf.PropertyType.Equals(typeof(bool)))
  59.                     ++realNumFields;
  60.             }
  61.  
  62.             if (realNumFields != numFields)
  63.             {
  64.                 Console.WriteLine("{0} {1}", realNumFields, numFields);
  65.                 Console.WriteLine("Incorrect format in dbc '" + fileName + "'!");
  66.                 return false;
  67.             }
  68.  
  69.             byte[] data = reader.ReadBytes((int)numRecords * (int)recordSize);
  70.             byte[] strings = reader.ReadBytes((int)stringSize);
  71.  
  72.             reader.Close();
  73.  
  74.             int blockPtr = 0;
  75.  
  76.             for (uint i = 0; i < numRecords; ++i)
  77.             {
  78.                 T record = new T();
  79.  
  80.                 foreach (PropertyInfo inf in infos)
  81.                 {
  82.                     if (inf.PropertyType.Equals(typeof(float)))
  83.                     {
  84.                         inf.SetValue((object)record, (object)BitConverter.ToSingle(data, blockPtr), null);
  85.                         blockPtr += 4;
  86.                     }
  87.                     if (inf.PropertyType.Equals(typeof(uint)))
  88.                     {
  89.                         inf.SetValue((object)record, (object)BitConverter.ToUInt32(data, blockPtr), null);
  90.                         blockPtr += 4;
  91.                     }
  92.                     if (inf.PropertyType.Equals(typeof(int)))
  93.                     {
  94.                         inf.SetValue((object)record, (object)BitConverter.ToInt32(data, blockPtr), null);
  95.                         blockPtr += 4;
  96.                     }
  97.                     if (inf.PropertyType.Equals(typeof(bool)))
  98.                     {
  99.                         uint val = BitConverter.ToUInt32(data, blockPtr);
  100.                         blockPtr += 4;
  101.                         inf.SetValue((object)record, (object)(val > 0 ? true : false), null);
  102.                     }
  103.                     if (inf.PropertyType.Equals(typeof(string)))
  104.                     {
  105.                         uint strOfs = 0xFFFFFFFF;
  106.                         for (int j = 0; j < 16; ++j)
  107.                         {
  108.                             uint ofs = BitConverter.ToUInt32(data, blockPtr);
  109.                             blockPtr += 4;
  110.                             if (ofs != 0 && ofs < strings.Length)
  111.                                 strOfs = ofs;
  112.                         }
  113.                         blockPtr += 4;
  114.                         if (strOfs != 0xFFFFFFFF)
  115.                         {
  116.                             string txt = Encoding.ASCII.GetString(strings, (int)strOfs, strings.Length - (int)strOfs);
  117.                             if (txt.IndexOf('\0') != -1)
  118.                                 txt = txt.Substring(0, txt.IndexOf('\0'));
  119.  
  120.                             inf.SetValue((object)record, (object)txt, null);
  121.                         }
  122.                         else
  123.                             inf.SetValue((object)record, (object)"", null);
  124.                     }
  125.                     if (inf.PropertyType.Equals(typeof(StringBuilder)))
  126.                     {
  127.                         uint ofs = BitConverter.ToUInt32(data, blockPtr);
  128.                         blockPtr += 4;
  129.                         if (ofs < strings.Length)
  130.                         {
  131.                             string txt = Encoding.ASCII.GetString(strings, (int)ofs, strings.Length - (int)ofs);
  132.                             if (txt.IndexOf('\0') != -1)
  133.                                 txt = txt.Substring(0, txt.IndexOf('\0'));
  134.                             StringBuilder bldr = new StringBuilder(txt);
  135.                             inf.SetValue((object)record, (object)bldr, null);
  136.                         }
  137.                         else
  138.                         {
  139.                             StringBuilder bldr = new StringBuilder("");
  140.                             inf.SetValue((object)record, (object)bldr, null);
  141.                         }
  142.                     }
  143.  
  144.                 }
  145.  
  146.                 Entries.Add((uint)infos[0].GetValue((object)record, null), record);
  147.             }
  148.  
  149.             Console.WriteLine("Loaded {0} records from file '{1}'!", Entries.Values.Count, fileName);
  150.             return true;
  151.         }
  152.     }
  153. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement