Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Reflection;
- using System.IO;
- namespace Sandbox.Utils.Storage
- {
- public class DBCFile<T> where T : new()
- {
- public SortedList<uint, T> Entries
- {
- get;
- private
- set;
- }
- public DBCFile()
- {
- Entries = new SortedList<uint, T>();
- }
- public bool InitFile(string fileName)
- {
- BinaryReader reader = null;
- try
- {
- reader = new BinaryReader(File.Open(fileName, FileMode.Open));
- }
- catch (Exception)
- {
- Console.WriteLine("File {0} could not be opened!", fileName);
- return false;
- }
- reader.BaseStream.Position = 4;
- uint numRecords = reader.ReadUInt32();
- uint numFields = reader.ReadUInt32();
- uint recordSize = reader.ReadUInt32();
- uint stringSize = reader.ReadUInt32();
- Type t = typeof(T);
- PropertyInfo[] infos = t.GetProperties();
- uint realNumFields = 0;
- foreach (PropertyInfo inf in infos)
- {
- if (inf.PropertyType.Equals(typeof(float)))
- ++realNumFields;
- else if (inf.PropertyType.Equals(typeof(uint)))
- ++realNumFields;
- else if (inf.PropertyType.Equals(typeof(int)))
- ++realNumFields;
- else if (inf.PropertyType.Equals(typeof(string)))
- realNumFields += 17;
- else if (inf.PropertyType.Equals(typeof(StringBuilder)))
- ++realNumFields;
- else if (inf.PropertyType.Equals(typeof(bool)))
- ++realNumFields;
- }
- if (realNumFields != numFields)
- {
- Console.WriteLine("{0} {1}", realNumFields, numFields);
- Console.WriteLine("Incorrect format in dbc '" + fileName + "'!");
- return false;
- }
- byte[] data = reader.ReadBytes((int)numRecords * (int)recordSize);
- byte[] strings = reader.ReadBytes((int)stringSize);
- reader.Close();
- int blockPtr = 0;
- for (uint i = 0; i < numRecords; ++i)
- {
- T record = new T();
- foreach (PropertyInfo inf in infos)
- {
- if (inf.PropertyType.Equals(typeof(float)))
- {
- inf.SetValue((object)record, (object)BitConverter.ToSingle(data, blockPtr), null);
- blockPtr += 4;
- }
- if (inf.PropertyType.Equals(typeof(uint)))
- {
- inf.SetValue((object)record, (object)BitConverter.ToUInt32(data, blockPtr), null);
- blockPtr += 4;
- }
- if (inf.PropertyType.Equals(typeof(int)))
- {
- inf.SetValue((object)record, (object)BitConverter.ToInt32(data, blockPtr), null);
- blockPtr += 4;
- }
- if (inf.PropertyType.Equals(typeof(bool)))
- {
- uint val = BitConverter.ToUInt32(data, blockPtr);
- blockPtr += 4;
- inf.SetValue((object)record, (object)(val > 0 ? true : false), null);
- }
- if (inf.PropertyType.Equals(typeof(string)))
- {
- uint strOfs = 0xFFFFFFFF;
- for (int j = 0; j < 16; ++j)
- {
- uint ofs = BitConverter.ToUInt32(data, blockPtr);
- blockPtr += 4;
- if (ofs != 0 && ofs < strings.Length)
- strOfs = ofs;
- }
- blockPtr += 4;
- if (strOfs != 0xFFFFFFFF)
- {
- string txt = Encoding.ASCII.GetString(strings, (int)strOfs, strings.Length - (int)strOfs);
- if (txt.IndexOf('\0') != -1)
- txt = txt.Substring(0, txt.IndexOf('\0'));
- inf.SetValue((object)record, (object)txt, null);
- }
- else
- inf.SetValue((object)record, (object)"", null);
- }
- if (inf.PropertyType.Equals(typeof(StringBuilder)))
- {
- uint ofs = BitConverter.ToUInt32(data, blockPtr);
- blockPtr += 4;
- if (ofs < strings.Length)
- {
- string txt = Encoding.ASCII.GetString(strings, (int)ofs, strings.Length - (int)ofs);
- if (txt.IndexOf('\0') != -1)
- txt = txt.Substring(0, txt.IndexOf('\0'));
- StringBuilder bldr = new StringBuilder(txt);
- inf.SetValue((object)record, (object)bldr, null);
- }
- else
- {
- StringBuilder bldr = new StringBuilder("");
- inf.SetValue((object)record, (object)bldr, null);
- }
- }
- }
- Entries.Add((uint)infos[0].GetValue((object)record, null), record);
- }
- Console.WriteLine("Loaded {0} records from file '{1}'!", Entries.Values.Count, fileName);
- return true;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement