forked from ngoedde/SlimPK2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPK2Archive.cs
343 lines (285 loc) · 11.9 KB
/
PK2Archive.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
#region Usings
using SlimPK2.IO;
using SlimPK2.Security;
using SlimPK2.Types;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
#endregion Usings
namespace SlimPK2
{
public class PK2Archive : IDisposable
{
#region Properties
/// <summary>
/// Gets or sets a value indicating whether this instance is disposing.
/// </summary>
/// <value>
/// <c>true</c> if this instance is disposing; otherwise, <c>false</c>.
/// </value>
public bool IsDisposing { get; set; }
/// <summary>
/// Gets or sets a value indicating whether this instance is disposed.
/// </summary>
/// <value>
/// <c>true</c> if this instance is disposed; otherwise, <c>false</c>.
/// </value>
public bool IsDisposed { get; set; }
/// <summary>
/// Gets or sets the header of the PK2 archive.
/// </summary>
/// <value>
/// The header.
/// </value>
public PK2Header Header { get; set; }
/// <summary>
/// Gets or sets the blocks.
/// </summary>
/// <value>
/// The blocks.
/// </value>
public List<PK2Block> Blocks { get; set; }
/// <summary>
/// Gets or sets the path of the PK2 archive.
/// </summary>
/// <value>
/// The path.
/// </value>
public string Path { get; set; }
/// <summary>
/// Gets or sets a value indicating whether this <see cref="PK2Archive"/> is loaded.
/// </summary>
/// <value>
/// <c>true</c> if loaded; otherwise, <c>false</c>.
/// </value>
public bool Loaded { get; private set; }
/// <summary>
/// Gets the configuration.
/// </summary>
/// <value>
/// The configuration.
/// </value>
public PK2Config Config { get; private set; }
#endregion Properties
#region Members
/// <summary>
/// The files.
/// </summary>
private List<PK2File> _files;
/// <summary>
/// The directories.
/// </summary>
private List<PK2Directory> _directories;
/// <summary>
/// The navigator.
/// </summary>
private PK2Navigator _navigator;
#endregion Members
#region Constructor / Destructor
/// <summary>
/// Initializes a new instance of the <see cref="PK2Archive"/> class.
/// The key is set to the isro standard encryption key.
/// Do not provide any baseKey, if you wish to use the default base key for the archive.
/// </summary>
/// <exception cref="System.IO.FileNotFoundException"></exception>
/// <exception cref="SlimPK2.Security.BlowfishSecurityException"></exception>
/// <exception cref="SlimPK2.Types.InvalidHeaderException"></exception>
public PK2Archive(string path, PK2Config config = null)
{
if (!File.Exists(path))
throw new FileNotFoundException(path);
//Initialize default config
if (config == null)
config = PK2Config.GetDefault();
//assign the config to this instance
Config = config;
//Create file reader...
FileAdapter.SetInstance(new FileAdapter(path));
Path = path;
//Read header...
Header = new PK2Header(FileAdapter.GetInstance().ReadData(0, 256));
if (Header.Encrypted)
{
var blowfishKey = BlowfishUtilities.GenerateFinalBlowfishKey(Config.Key, Config.BaseKey);
var blowfish = new Blowfish();
blowfish.Initialize(blowfishKey);
BlowfishUtilities.SetBlowfish(blowfish);
var tempChecksum =
BlowfishUtilities.GetBlowfish().Encode(Encoding.ASCII.GetBytes("Joymax Pak File"));
//Check if the security checksum equals the generated checksum
if (tempChecksum[0] != Header.SecurityChecksum[0]
|| tempChecksum[1] != Header.SecurityChecksum[1]
|| tempChecksum[2] != Header.SecurityChecksum[2])
throw new BlowfishSecurityException(Config.Key);
}
if (Config.Mode != PK2Mode.FreeBrowse)
{
switch (Config.Mode)
{
case PK2Mode.IndexBlocks:
Blocks = new List<PK2Block>();
ReadBlockAt(256);
break;
case PK2Mode.Index:
_files = new List<PK2File>();
_directories = new List<PK2Directory>();
ReadBlockAt(256, new PK2Directory());
break;
}
}
else
_navigator = new PK2Navigator();
Loaded = true;
}
/// <summary>
/// Finalizes an instance of the <see cref="PK2Archive"/> class.
/// </summary>
~PK2Archive()
{
if (!IsDisposing && !IsDisposed)
Dispose();
}
#endregion Constructor / Destructor
#region Methods
/// <summary>
/// Reads the block.
/// </summary>
/// <param name="position">The position.</param>
/// <param name="currentDirectory">The current directory.</param>
private void ReadBlockAt(ulong position, PK2Directory currentDirectory = null)
{
var currentBlockBuffer = FileAdapter.GetInstance().ReadData((long)position, 2560);
var currentBlock = new PK2Block(currentBlockBuffer, position);
//Check if the chain continues at another position
if (currentBlock.Entries[19].NextChain > 0)
ReadBlockAt(currentBlock.Entries[19].NextChain);
//Read the subfolder chain
//The folder "." and ".." are required if you which to use "free-browsing" mode, without reading the whole index at once
foreach (var pk2Entry in currentBlock.Entries.Where(entry => !entry.Name.StartsWith(".") && entry.Position > 0))
{
switch (pk2Entry.Type)
{
case PK2EntryType.Directory:
if (Config.Mode == PK2Mode.Index)
{
var subDirectory = new PK2Directory(pk2Entry, currentDirectory?.Path);
_directories.Add(subDirectory);
ReadBlockAt(pk2Entry.Position, subDirectory);
}
else
ReadBlockAt(pk2Entry.Position);
break;
case PK2EntryType.File:
if (Config.Mode == PK2Mode.Index)
{
_files.Add(new PK2File(pk2Entry, currentDirectory?.Path));
}
break;
}
}
if (Config.Mode == PK2Mode.IndexBlocks)
Blocks.Add(currentBlock);
}
/// <summary>
/// Gets the file.
/// </summary>
/// <param name="path">The path.</param>
/// <returns></returns>
/// <exception cref="SlimPK2.PK2NotLoadedException"></exception>
/// <exception cref="System.InvalidOperationException">The method GetFile() is only available in Index-Mode</exception>
public PK2File GetFile(string path)
{
if (!Loaded)
throw new PK2NotLoadedException();
if (Config.Mode != PK2Mode.Index)
throw new InvalidOperationException("The method GetFile() is only available in Index-Mode");
return _files.FirstOrDefault(file => file.Path == path);
}
/// <summary>
/// Gets the directory.
/// </summary>
/// <param name="path">The path.</param>
/// <returns></returns>
/// <exception cref="SlimPK2.PK2NotLoadedException"></exception>
/// <exception cref="System.InvalidOperationException">The method GetDirectory() is only available in Index-Mode</exception>
public PK2Directory GetDirectory(string path)
{
if (!Loaded)
throw new PK2NotLoadedException();
if (Config.Mode != PK2Mode.Index)
throw new InvalidOperationException("The method GetDirectory() is only available in Index-Mode");
return _directories.FirstOrDefault(directory => directory.Path == path);
}
/// <summary>
/// Gets the directories.
/// </summary>
/// <param name="path">The path.</param>
/// <param name="recursive">if set to <c>true</c> [recursive].</param>
/// <returns></returns>
/// <exception cref="SlimPK2.PK2NotLoadedException"></exception>
/// <exception cref="System.InvalidOperationException">The method GetFile() is only available in Index-Mode</exception>
public PK2Directory[] GetDirectories(string path, bool recursive)
{
if (!Loaded)
throw new PK2NotLoadedException();
if (Config.Mode != PK2Mode.Index)
throw new InvalidOperationException("The method GetDirectories() is only available in Index-Mode");
return recursive ? _directories.Where(directory => directory.Path.StartsWith(path)).ToArray() : _directories.Where(directory => directory.Parent == path).ToArray();
}
/// <summary>
/// Gets the files.
/// </summary>
/// <param name="path">The path.</param>
/// <param name="recursive">if set to <c>true</c> [recursive].</param>
/// <returns></returns>
/// <exception cref="SlimPK2.PK2NotLoadedException"></exception>
/// <exception cref="System.InvalidOperationException">The method GetFiles() is only available in Index-Mode</exception>
public PK2File[] GetFiles(string path, bool recursive)
{
if (!Loaded)
throw new PK2NotLoadedException();
if (Config.Mode != PK2Mode.Index)
throw new InvalidOperationException("The method GetFiles() is only available in Index-Mode");
return recursive ? _files.Where(file => file.Path.StartsWith(path)).ToArray() : _files.Where(file => file.Parent == path).ToArray();
}
/// <summary>
/// Gets the navigator.
/// This object can only be used in Free-Browse mode.
/// </summary>
/// <returns></returns>
/// <exception cref="SlimPK2.PK2NotLoadedException"></exception>
/// <exception cref="System.InvalidOperationException">The method GetNavigator() is only available in Freebrowse-Mode</exception>
public PK2Navigator GetNavigator()
{
if (!Loaded)
throw new PK2NotLoadedException();
if (Config.Mode != PK2Mode.FreeBrowse)
throw new InvalidOperationException("The method GetNavigator() is only available in Freebrowse-Mode");
return _navigator;
}
/// <summary>
/// Releases unmanaged and - optionally - managed resources.
/// </summary>
/// <exception cref="System.ObjectDisposedException">PK2Archive</exception>
public void Dispose()
{
if (IsDisposed || IsDisposing)
throw new ObjectDisposedException("PK2Archive");
IsDisposing = true;
BlowfishUtilities.SetBlowfish(null);
IO.FileAdapter.SetInstance(null);
Header = null;
Config = null;
Blocks = null;
Loaded = false;
_directories = null;
_files = null;
_navigator = null;
IsDisposing = false;
IsDisposed = true;
}
#endregion Methods
}
}