-
Notifications
You must be signed in to change notification settings - Fork 0
/
PAKArchiveExporter.cs
136 lines (113 loc) · 4.05 KB
/
PAKArchiveExporter.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
namespace Archiver
{
/// <summary>
/// Exporter for PAK file archives.
/// </summary>
class PAKArchiveExporter : IArchiveExporter
{
/// <summary>
/// First Header within a PAK file. Contains signature and directory location.
/// </summary>
class PAKMainHeader
{
public const int SIZE_OF = 12;
public const int SIGNATURE_LENGTH = 4;
public char[] Signature { get; private set; }
public int DirectoryOffset { get; set; }
public int DirectoryLength { get; set; }
public int NumFiles
{
get { return DirectoryLength / PAKFileHeader.SIZE_OF; }
}
public PAKMainHeader()
{
Signature = new char[SIGNATURE_LENGTH];
Signature[0] = 'P';
Signature[1] = 'A';
Signature[2] = 'C';
Signature[3] = 'K';
DirectoryOffset = SIZE_OF;
DirectoryLength = 0;
}
public void Read(BinaryReader reader)
{
Signature = reader.ReadChars(SIGNATURE_LENGTH);
DirectoryOffset = reader.ReadInt32();
DirectoryLength = reader.ReadInt32();
}
public void Write(BinaryWriter writer)
{
writer.Write(Signature, 0, SIGNATURE_LENGTH);
writer.Write(DirectoryOffset);
writer.Write(DirectoryLength);
}
}
/// <summary>
/// Header for every file entry within a PAK file.
/// </summary>
class PAKFileHeader
{
public const int SIZE_OF = 64;
public const int FILE_NAME_LENGTH = 56;
private char[] name = new char[FILE_NAME_LENGTH];
public char[] Name
{
get { return name; }
set
{
value.CopyTo(name, 0);
}
}
public int Offset { get; set; }
public int Size { get; set; }
public PAKFileHeader()
{
Offset = 0;
Size = 0;
}
public void Read(BinaryReader reader)
{
Name = reader.ReadChars(FILE_NAME_LENGTH);
Offset = reader.ReadInt32();
Size = reader.ReadInt32();
}
public void Write(BinaryWriter writer)
{
writer.Write(Name, 0, FILE_NAME_LENGTH);
writer.Write(Offset);
writer.Write(Size);
}
}
public void Export(ArchiveProject project, Stream stream)
{
BinaryWriter writer = new BinaryWriter(stream);
PAKMainHeader mainHeader = new PAKMainHeader();
long startPosition = stream.Position;
stream.Seek(PAKMainHeader.SIZE_OF, SeekOrigin.Current);
ICollection<ArchiveProjectEntry> entries = project.GetFiles();
List<PAKFileHeader> fileHeaders = new List<PAKFileHeader>();
foreach(ArchiveProjectEntry entry in entries)
{
byte[] contents = File.ReadAllBytes(entry.Path);
PAKFileHeader header = new PAKFileHeader
{
Name = entry.RelativePath.ToCharArray(),
Offset = mainHeader.DirectoryOffset,
Size = contents.Length
};
writer.Write(contents);
mainHeader.DirectoryOffset += contents.Length;
mainHeader.DirectoryLength += PAKFileHeader.SIZE_OF;
fileHeaders.Add(header);
}
fileHeaders.ForEach(header => header.Write(writer));
stream.Position = startPosition;
mainHeader.Write(writer);
stream.Position = startPosition + mainHeader.DirectoryOffset + mainHeader.DirectoryLength;
}
}
}