-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathSTFSFile.cs
255 lines (230 loc) · 7.01 KB
/
STFSFile.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
/*
* STFSFile.cs
*
* Copyright (c) 2015,2016, maxton. All rights reserved.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3.0 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; If not, see
* <http://www.gnu.org/licenses/>.
*/
using System;
using System.Collections.Generic;
namespace GameArchives.STFS
{
/// <summary>
/// Represents a file within an STFS package.
/// </summary>
class STFSFile : IFile
{
public string Name { get; }
public IDirectory Parent { get; }
public long Size { get; }
public bool Compressed => false;
public long CompressedSize => Size;
public IDictionary<string, object> ExtendedInfo { get; }
public System.IO.Stream Stream => GetStream();
STFSPackage container;
int[] dataBlocks;
int startBlock;
int numBlocks;
internal STFSFile(string name, uint size, int[] dataBlocks, STFSDirectory parent, STFSPackage container)
{
Name = name;
Parent = parent;
Size = size;
this.dataBlocks = dataBlocks;
numBlocks = dataBlocks.Length;
if(numBlocks > 0)
startBlock = dataBlocks[0];
this.container = container;
ExtendedInfo = new Dictionary<string, object>();
}
internal STFSFile(string name, uint size, int startBlock, int numBlocks, STFSDirectory parent, STFSPackage container)
{
Name = name;
Parent = parent;
Size = size;
this.dataBlocks = null;
this.startBlock = startBlock;
this.numBlocks = numBlocks;
this.container = container;
}
/// <summary>
/// The name of this file.
/// </summary>
/// <returns></returns>
public override string ToString()
{
return Name;
}
/// <summary>
/// Gets up to the first 2GiB of a file. For larger accesses use STFSFileStream.
/// </summary>
/// <returns></returns>
public byte[] GetBytes()
{
return this.GetStream().ReadBytes((int)(Size & 0x7FFFFFFF));
}
/// <summary>
/// Returns a Stream that allows access to the file's bytes.
/// </summary>
/// <returns></returns>
public System.IO.Stream GetStream()
{
if (dataBlocks == null)
{
dataBlocks = container.GetFileBlocks(startBlock, numBlocks, false);
}
return new STFSFileStream(container, dataBlocks, Size);
}
}
/// <summary>
/// A stream for accessing file data from within an STFS package.
/// </summary>
public class STFSFileStream : System.IO.Stream
{
STFSPackage container;
int[] blocks;
long position;
internal STFSFileStream(STFSPackage container, int[] blocks, long size)
{
Length = size;
this.blocks = blocks;
this.container = container;
position = 0;
}
/// <summary>
/// Denotes whether the stream can be read from.
/// </summary>
public override bool CanRead => true;
/// <summary>
/// Denotes whether the user can seek this stream.
/// </summary>
public override bool CanSeek => true;
/// <summary>
/// Denotes whether the user can write to this stream.
/// </summary>
public override bool CanWrite => false;
/// <summary>
/// The total length of this file.
/// </summary>
public override long Length { get; }
/// <summary>
/// The current position the stream points to within the file.
/// </summary>
public override long Position
{
get { return position; }
set
{
if (value < 0)
{
throw new ArgumentOutOfRangeException("Attempted to seek to before the beginning of the file.");
}
if (value > Length)
{
throw new System.IO.EndOfStreamException("Attempted to seek past the end of the file.");
}
position = value;
}
}
/// <summary>
/// Not implemented; read-only stream.
/// </summary>
public override void Flush()
{
throw new NotSupportedException();
}
/// <summary>
/// Reads `count` bytes into `buffer` at offset `offset`.
/// </summary>
/// <param name="buffer"></param>
/// <param name="offset"></param>
/// <param name="count"></param>
/// <returns></returns>
public override int Read(byte[] buffer, int offset, int count)
{
if (offset + count > buffer.Length)
{
throw new IndexOutOfRangeException("Attempt to fill buffer past its end");
}
if (this.Position == this.Length || this.Position + count > this.Length)
{
count = (int)(this.Length - this.Position);
//throw new System.IO.EndOfStreamException("Cannot read past end of file.");
}
uint firstBlock = 0;
int blockOffset = 0;
if (Position != 0)
{
firstBlock = (uint)(Position / 0x1000);
blockOffset = (int)(Position % 0x1000);
}
int totalBytesRead = 0;
for (uint i = firstBlock; i < blocks.Length; i++)
{
// Read up to 1 block of data.
container.stream.Position = container.BlockToOffset(blocks[i]) + blockOffset;
int bytesToRead = count > (0x1000 - blockOffset) ? (0x1000 - blockOffset) : count;
int readBytes = container.stream.Read(buffer, offset + totalBytesRead, bytesToRead);
count -= readBytes;
totalBytesRead += readBytes;
blockOffset = 0;
if (count <= 0)
break;
}
Position += totalBytesRead;
return totalBytesRead;
}
/// <summary>
/// Seek the stream to given position within the file relative to given origin.
/// </summary>
/// <param name="offset"></param>
/// <param name="origin"></param>
/// <returns></returns>
public override long Seek(long offset, System.IO.SeekOrigin origin)
{
switch (origin)
{
case System.IO.SeekOrigin.Begin:
Position = offset;
break;
case System.IO.SeekOrigin.Current:
Position = Position + offset;
break;
case System.IO.SeekOrigin.End:
Position = Length + offset;
break;
}
return Position;
}
/// <summary>
/// Not implemented; read-only stream.
/// </summary>
/// <param name="value"></param>
public override void SetLength(long value)
{
throw new NotSupportedException();
}
/// <summary>
/// Not implemented; read-only stream.
/// </summary>
/// <param name="buffer"></param>
/// <param name="offset"></param>
/// <param name="count"></param>
public override void Write(byte[] buffer, int offset, int count)
{
throw new NotSupportedException();
}
}
}