ace/Source/ACE.DatLoader/DatFile.cs
Mag-nus 6fe177a600
ACE.DatLoader refactoring (#594)
* Change >> 1) & 1 to & 2

Cosmetic change pointed out by Rawaho.

* Better fix of the Portal Flags in the dat code

* ACE.DatLoader refactoring

This new code format uses extention methods to re-use code. It allows decouples object unpacking from DatReader and reduces the dependancy to a simple BinaryReader.

* Forgot to remove the temp StarterAreas code from the transition

* Some files were ignored, not sure why. This should fix
2018-01-23 04:02:39 -06:00

91 lines
3 KiB
C#

using System;
using System.IO;
using System.Linq;
namespace ACE.DatLoader
{
public class DatFile
{
private DatFileType? fileType;
/// <summary>
/// private to force loading from the static method
/// </summary>
private DatFile()
{
}
public uint BitFlags { get; private set; }
public uint ObjectId { get; private set; }
public uint FileOffset { get; private set; }
public uint FileSize { get; private set; }
public uint Date { get; private set; }
public uint Iteration { get; private set; }
public DatDatabaseType DatType { get; private set; }
/// <summary>
/// populates a new CellFile from the specified buffer.
/// </summary>
public static DatFile FromBuffer(byte[] buffer, int offset, DatDatabaseType type)
{
DatFile cf = new DatFile()
{
BitFlags = BitConverter.ToUInt32(buffer, offset),
ObjectId = BitConverter.ToUInt32(buffer, offset + 4),
FileOffset = BitConverter.ToUInt32(buffer, offset + 8) + 4,
FileSize = BitConverter.ToUInt32(buffer, offset + 12),
Date = BitConverter.ToUInt32(buffer, offset + 16),
Iteration = BitConverter.ToUInt32(buffer, offset + 20),
DatType = type
};
return cf;
}
public DatFileType? GetFileType()
{
if (fileType != null)
return fileType.Value;
var type = typeof(DatFileType);
var enumTypes = Enum.GetValues(typeof(DatFileType)).Cast<DatFileType>().ToList();
foreach (var fileType in enumTypes)
{
var memInfo = type.GetMember(fileType.ToString());
var datType = memInfo[0].GetCustomAttributes(typeof(DatDatabaseTypeAttribute), false).Cast<DatDatabaseTypeAttribute>().ToList();
if (datType?.Count > 0 && datType[0].Type == DatType)
{
// file type matches, now check id range
var idRange = memInfo[0].GetCustomAttributes(typeof(DatFileTypeIdRangeAttribute), false).Cast<DatFileTypeIdRangeAttribute>().ToList();
if (idRange?.Count > 0 && idRange[0].BeginRange <= ObjectId && idRange[0].EndRange >= ObjectId)
{
// id range matches
this.fileType = fileType;
break;
}
}
}
return fileType;
}
/// <summary>
/// uses the open stream to read content as a binary blob.
/// </summary>
public byte[] GetContent(FileStream stream)
{
stream.Seek(FileOffset, SeekOrigin.Begin);
byte[] content = new byte[FileSize];
stream.Read(content, 0, Convert.ToInt32(FileSize));
return content;
}
}
}