| 1 |
using System; |
| 2 |
using System.IO; |
| 3 |
|
| 4 |
namespace Oni |
| 5 |
{ |
| 6 |
internal sealed class InstanceFileHeader |
| 7 |
{ |
| 8 |
public const long OniPCTemplateChecksum = 0x0003bcdf33dc271f; |
| 9 |
public const long OniMacTemplateChecksum = 0x0003bcdf23c13061; |
| 10 |
public const int Version31 = 0x56523331; |
| 11 |
public const int Version32 = 0x56523332; |
| 12 |
public const long Signature = 0x0008001000140040; |
| 13 |
|
| 14 |
#region Private data |
| 15 |
private long templateChecksum; |
| 16 |
private int version; |
| 17 |
private long signature; |
| 18 |
private int instanceCount; |
| 19 |
private int nameCount; |
| 20 |
private int templateCount; |
| 21 |
private int dataTableOffset; |
| 22 |
private int dataTableSize; |
| 23 |
private int nameTableOffset; |
| 24 |
private int nameTableSize; |
| 25 |
private int rawTableOffset; |
| 26 |
private int rawTableSize; |
| 27 |
#endregion |
| 28 |
|
| 29 |
internal static InstanceFileHeader Read(BinaryReader reader) |
| 30 |
{ |
| 31 |
var header = new InstanceFileHeader |
| 32 |
{ |
| 33 |
templateChecksum = reader.ReadInt64(), |
| 34 |
version = reader.ReadInt32(), |
| 35 |
signature = reader.ReadInt64() |
| 36 |
}; |
| 37 |
|
| 38 |
ValidateHeader(header); |
| 39 |
|
| 40 |
header.instanceCount = reader.ReadInt32(); |
| 41 |
header.nameCount = reader.ReadInt32(); |
| 42 |
header.templateCount = reader.ReadInt32(); |
| 43 |
header.dataTableOffset = reader.ReadInt32(); |
| 44 |
header.dataTableSize = reader.ReadInt32(); |
| 45 |
header.nameTableOffset = reader.ReadInt32(); |
| 46 |
header.nameTableSize = reader.ReadInt32(); |
| 47 |
|
| 48 |
if (header.version == Version32) |
| 49 |
{ |
| 50 |
header.rawTableOffset = reader.ReadInt32(); |
| 51 |
header.rawTableSize = reader.ReadInt32(); |
| 52 |
|
| 53 |
reader.Skip(8); |
| 54 |
} |
| 55 |
else |
| 56 |
{ |
| 57 |
reader.Skip(16); |
| 58 |
} |
| 59 |
|
| 60 |
return header; |
| 61 |
} |
| 62 |
|
| 63 |
private static void ValidateHeader(InstanceFileHeader header) |
| 64 |
{ |
| 65 |
if (header.templateChecksum != OniPCTemplateChecksum && header.templateChecksum != OniMacTemplateChecksum) |
| 66 |
{ |
| 67 |
header.templateChecksum = OniMacTemplateChecksum; |
| 68 |
//throw new InvalidDataException("Invalid template checksum"); |
| 69 |
} |
| 70 |
|
| 71 |
if (header.version != Version31 && header.version != Version32) |
| 72 |
throw new InvalidDataException("Unknown file version"); |
| 73 |
|
| 74 |
if (header.version == Version31 && header.signature != Signature) |
| 75 |
throw new InvalidDataException("Invalid file signature"); |
| 76 |
} |
| 77 |
|
| 78 |
public long TemplateChecksum => templateChecksum; |
| 79 |
public int Version => version; |
| 80 |
public int InstanceCount => instanceCount; |
| 81 |
public int NameCount => nameCount; |
| 82 |
public int TemplateCoun => templateCount; |
| 83 |
public int DataTableOffset => dataTableOffset; |
| 84 |
public int DataTableSize => dataTableSize; |
| 85 |
public int NameTableOffset => nameTableOffset; |
| 86 |
public int NameTableSize => nameTableSize; |
| 87 |
public int RawTableOffset => rawTableOffset; |
| 88 |
public int RawTableSize => rawTableSize; |
| 89 |
} |
| 90 |
} |