2018-01-23 04:02:39 -06:00
|
|
|
using System;
|
2017-03-04 07:59:37 -05:00
|
|
|
using System.IO;
|
2017-03-15 08:12:21 -04:00
|
|
|
using System.Linq;
|
2017-03-04 07:59:37 -05:00
|
|
|
|
|
|
|
|
namespace ACE.DatLoader
|
|
|
|
|
{
|
|
|
|
|
public class DatFile
|
|
|
|
|
{
|
2018-01-23 04:02:39 -06:00
|
|
|
private DatFileType? fileType;
|
2017-03-15 08:12:21 -04:00
|
|
|
|
2017-03-04 07:59:37 -05:00
|
|
|
/// <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; }
|
|
|
|
|
|
2017-04-04 12:44:57 -04:00
|
|
|
public DatDatabaseType DatType { get; private set; }
|
2017-03-15 08:12:21 -04:00
|
|
|
|
2017-03-04 07:59:37 -05:00
|
|
|
/// <summary>
|
|
|
|
|
/// populates a new CellFile from the specified buffer.
|
|
|
|
|
/// </summary>
|
2017-03-15 08:12:21 -04:00
|
|
|
public static DatFile FromBuffer(byte[] buffer, int offset, DatDatabaseType type)
|
2017-03-04 07:59:37 -05:00
|
|
|
{
|
|
|
|
|
DatFile cf = new DatFile()
|
|
|
|
|
{
|
|
|
|
|
BitFlags = BitConverter.ToUInt32(buffer, offset),
|
|
|
|
|
ObjectId = BitConverter.ToUInt32(buffer, offset + 4),
|
2017-03-15 08:12:21 -04:00
|
|
|
FileOffset = BitConverter.ToUInt32(buffer, offset + 8) + 4,
|
2017-03-04 07:59:37 -05:00
|
|
|
FileSize = BitConverter.ToUInt32(buffer, offset + 12),
|
|
|
|
|
Date = BitConverter.ToUInt32(buffer, offset + 16),
|
2017-03-15 08:12:21 -04:00
|
|
|
Iteration = BitConverter.ToUInt32(buffer, offset + 20),
|
|
|
|
|
DatType = type
|
2017-03-04 07:59:37 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return cf;
|
|
|
|
|
}
|
|
|
|
|
|
2017-03-15 08:12:21 -04:00
|
|
|
public DatFileType? GetFileType()
|
|
|
|
|
{
|
|
|
|
|
if (fileType != null)
|
|
|
|
|
return fileType.Value;
|
|
|
|
|
|
|
|
|
|
var type = typeof(DatFileType);
|
|
|
|
|
var enumTypes = Enum.GetValues(typeof(DatFileType)).Cast<DatFileType>().ToList();
|
|
|
|
|
|
2017-04-04 12:44:57 -04:00
|
|
|
foreach (var fileType in enumTypes)
|
2017-03-15 08:12:21 -04:00
|
|
|
{
|
|
|
|
|
var memInfo = type.GetMember(fileType.ToString());
|
|
|
|
|
var datType = memInfo[0].GetCustomAttributes(typeof(DatDatabaseTypeAttribute), false).Cast<DatDatabaseTypeAttribute>().ToList();
|
|
|
|
|
|
2018-01-23 04:02:39 -06:00
|
|
|
if (datType?.Count > 0 && datType[0].Type == DatType)
|
2017-03-15 08:12:21 -04:00
|
|
|
{
|
|
|
|
|
// file type matches, now check id range
|
|
|
|
|
var idRange = memInfo[0].GetCustomAttributes(typeof(DatFileTypeIdRangeAttribute), false).Cast<DatFileTypeIdRangeAttribute>().ToList();
|
2018-01-23 04:02:39 -06:00
|
|
|
if (idRange?.Count > 0 && idRange[0].BeginRange <= ObjectId && idRange[0].EndRange >= ObjectId)
|
2017-03-15 08:12:21 -04:00
|
|
|
{
|
|
|
|
|
// id range matches
|
|
|
|
|
this.fileType = fileType;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return fileType;
|
|
|
|
|
}
|
|
|
|
|
|
2017-03-04 07:59:37 -05:00
|
|
|
/// <summary>
|
|
|
|
|
/// uses the open stream to read content as a binary blob.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public byte[] GetContent(FileStream stream)
|
|
|
|
|
{
|
2018-01-23 04:02:39 -06:00
|
|
|
stream.Seek(FileOffset, SeekOrigin.Begin);
|
2017-03-04 07:59:37 -05:00
|
|
|
byte[] content = new byte[FileSize];
|
|
|
|
|
stream.Read(content, 0, Convert.ToInt32(FileSize));
|
|
|
|
|
return content;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|