forked from ngoedde/SlimPK2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPK2Navigator.cs
227 lines (192 loc) · 6.73 KB
/
PK2Navigator.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
#region Usings
using SlimPK2.IO;
using SlimPK2.Types;
using System;
using System.Collections.Generic;
using System.Linq;
#endregion Usings
namespace SlimPK2
{
public class PK2Navigator : IDisposable
{
#region Properties
/// <summary>
/// Gets the current directory.
/// </summary>
/// <value>
/// The current directory.
/// </value>
public PK2Directory CurrentDirectory { get; private set; }
/// <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; private 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; private set; }
/// <summary>
/// Gets the files.
/// </summary>
/// <value>
/// The files.
/// </value>
public List<PK2File> Files { get; private set; }
/// <summary>
/// Gets the directories.
/// </summary>
/// <value>
/// The directories.
/// </value>
public List<PK2Directory> Directories { get; private set; }
/// <summary>
/// Gets the blocks.
/// </summary>
/// <value>
/// The blocks.
/// </value>
public PK2BlockCollection Blocks { get; private set; }
#endregion Properties
#region Constructor / Destructor
/// <summary>
/// Initializes a new instance of the <see cref="PK2Navigator"/> class.
/// </summary>
public PK2Navigator()
{
ReadBlockAt(256);
}
/// <summary>
/// Finalizes an instance of the <see cref="PK2Navigator"/> class.
/// </summary>
~PK2Navigator()
{
if (!IsDisposed && !IsDisposing)
Dispose();
}
#endregion Constructor / Destructor
#region Methods
/// <summary>
/// Changes the directory.
/// </summary>
/// <param name="directory">The directory.</param>
public void ChangeDirectory(PK2Directory directory)
{
if (directory.Parent != null && directory.Path != null)
ReadBlockAt(directory.Position);
else
ReadBlockAt(256);
CurrentDirectory = directory;
}
/// <summary>
/// Changes the directory.
/// </summary>
/// <param name="directory">The directory.</param>
public void ChangeDirectory(string directory)
{
var directoryObject = Directories.FirstOrDefault(dir => dir.Name == directory);
if (directoryObject == null)
throw new PK2DirectoryNotFoundException(directory);
if (directoryObject.Parent != null && directoryObject.Path != null)
ReadBlockAt(directoryObject.Position);
else
ReadBlockAt(256);
CurrentDirectory = directoryObject;
}
/// <summary>
/// Reloads this instance and re-indexes the current block.
/// </summary>
public void Reload()
{
ReadBlockAt(Blocks.Blocks[0].Offset);
}
/// <summary>
/// A helper method that will return the file by its name
/// </summary>
/// <param name="name">The name.</param>
/// <returns></returns>
public PK2File GetFile(string name)
{
return Files.FirstOrDefault(file => file.Name == name);
}
/// <summary>
/// Gets the directory.
/// </summary>
/// <param name="name">The name.</param>
/// <returns></returns>
public PK2Directory GetDirectory(string name)
{
return Directories.FirstOrDefault(directory => directory.Name == name);
}
/// <summary>
/// Gets the entry.
/// </summary>
/// <param name="name">The name.</param>
/// <returns></returns>
public PK2Entry GetEntry(string name)
{
return Blocks.GetEntries().FirstOrDefault(entry => entry.Name == name);
}
/// <summary>
/// Reads the block at.
/// This function does not recursively ready any "sub-chunks"
/// </summary>
/// <param name="position">The position.</param>
/// <returns></returns>
private void ReadBlockAt(ulong position)
{
Blocks = new PK2BlockCollection();
Directories = new List<PK2Directory>();
Files = new List<PK2File>();
while (true)
{
try
{
var currentBlockBuffer = FileAdapter.GetInstance().ReadData((long)position, 2560);
var currentBlock = new PK2Block(currentBlockBuffer, position);
Blocks.Blocks.Add(currentBlock);
foreach (var entry in currentBlock.Entries)
{
switch (entry.Type)
{
case PK2EntryType.Directory:
Directories.Add(new PK2Directory(entry, ""));
break;
case PK2EntryType.File:
Files.Add(new PK2File(entry, ""));
break;
}
}
//read the next block (only if the first block was not enough for the folder)
if (currentBlock.Entries[19].NextChain > 0)
{
position = currentBlock.Entries[19].NextChain;
continue;
}
break;
}
catch { throw new InvalidBlockException((long)position); } //should only be thrown, if the user fakes any position data in any entry!
}
}
/// <summary>
/// Releases unmanaged and - optionally - managed resources.
/// </summary>
/// <exception cref="System.ObjectDisposedException">PK2Navigator</exception>
public void Dispose()
{
if (IsDisposed || IsDisposing)
throw new ObjectDisposedException("PK2Navigator");
IsDisposing = true;
Directories = null;
Files = null;
IsDisposing = false;
IsDisposed = true;
}
#endregion Methods
}
}