forked from BAndysc/WoWDatabaseEditor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMpqArchive.cs
122 lines (94 loc) · 4.03 KB
/
MpqArchive.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
// Original code by https://github.com/Thenarden/nmpq
// Apache-2.0 License
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using WDE.Common.MPQ;
using WDE.MPQ.Parsing;
namespace WDE.MPQ
{
public partial class MpqArchive : IMpqArchive, IMpqArchiveDetails
{
private readonly string path;
public IMpqArchiveDetails Details => this;
public int SectorSize { get; private set; }
public IMpqUserDataHeader UserDataHeader { get; private set; }
public ArchiveHeader ArchiveHeader { get; private set; }
public IMpqHashTable HashTable { get; private set; }
public IList<BlockTableEntry> BlockTable { get; private set; }
public IList<string> KnownFiles => _knownFiles ?? (_knownFiles = ParseListfile().AsReadOnly());
private BinaryReader _reader;
private bool _cleanupStreamOnDispose;
private IList<string>? _knownFiles;
protected MpqArchive(string path, Stream stream, bool cleanupStreamOnDispose)
{
this.path = path;
_reader = new BinaryReader(stream, Encoding.UTF8);
_cleanupStreamOnDispose = cleanupStreamOnDispose;
var header = ParseUserDataHeader(_reader);
UserDataHeader = header ?? throw new MpqParsingException("Invalid MPQ header. This is probably not an MPQ archive.");
ParseArchiveHeader();
SectorSize = 512 << ArchiveHeader.SectorSizeShift;
var hashTableEntries = ReadTableEntires<HashTableEntry>("(hash table)", ArchiveHeader.HashTableOffset,
ArchiveHeader.HashTableEntryCount);
var blockTableEntries = ReadTableEntires<BlockTableEntry>("(block table)", ArchiveHeader.BlockTableOffset,
ArchiveHeader.BlockTableEntryCount);
BlockTable = blockTableEntries.ToList().AsReadOnly();
HashTable = new MpqHashTable(hashTableEntries.ToArray());
}
public static IMpqUserDataHeader? ParseUserDataHeader(string path)
{
if (path == null) throw new ArgumentNullException("path");
using (var stream = File.OpenRead(path))
return ParseUserDataHeader(stream);
}
public static IMpqUserDataHeader? ParseUserDataHeader(byte[] data)
{
if (data == null) throw new ArgumentNullException("data");
using (var stream = new MemoryStream(data))
return ParseUserDataHeader(stream);
}
public static IMpqUserDataHeader? ParseUserDataHeader(Stream stream)
{
if (stream == null) throw new ArgumentNullException("stream");
using (var reader = new BinaryReader(stream))
return ParseUserDataHeader(reader);
}
public static IMpqArchive Open(string path)
{
if (path == null) throw new ArgumentNullException("path");
var archive = new MpqArchive(path, File.OpenRead(path), true);
return archive;
}
public static IMpqArchive Open(byte[] data)
{
if (data == null) throw new ArgumentNullException("data");
var archive = new MpqArchive(null!, new MemoryStream(data), true);
return archive;
}
public static IMpqArchive Open(Stream stream)
{
if (stream == null) throw new ArgumentNullException("stream");
var archive = new MpqArchive(null!, stream, false);
return archive;
}
// seeks to a stream posistion relative to the start of the archive
private void SeekToArchiveOffset(long offset)
{
_reader!.BaseStream.Seek(UserDataHeader.ArchiveOffset + offset, SeekOrigin.Begin);
}
public void Dispose()
{
if (_cleanupStreamOnDispose)
{
if (_reader != null)
{
_reader.Dispose();
_reader = null!;
}
}
}
}
}