2014-10-22 13:47:38 +00:00
|
|
|
using System;
|
|
|
|
using System.IO;
|
|
|
|
|
2014-10-22 20:56:05 +00:00
|
|
|
namespace GarrysMod.AddonCreator.Addon
|
2014-10-22 13:47:38 +00:00
|
|
|
{
|
|
|
|
public class SegmentedAddonFileInfo : AddonFileInfo
|
|
|
|
{
|
2014-10-22 17:48:30 +00:00
|
|
|
private readonly long _len;
|
|
|
|
private readonly long _pos;
|
|
|
|
private readonly Stream _stream;
|
2014-10-22 13:47:38 +00:00
|
|
|
private int _hash;
|
|
|
|
|
|
|
|
public SegmentedAddonFileInfo(Stream stream, long pos, long len, int fileHash)
|
|
|
|
{
|
|
|
|
_stream = stream;
|
|
|
|
_pos = pos;
|
|
|
|
_len = len;
|
|
|
|
_hash = fileHash;
|
|
|
|
}
|
|
|
|
|
2014-10-23 00:29:37 +00:00
|
|
|
public override long Size
|
|
|
|
{
|
|
|
|
get
|
|
|
|
{
|
|
|
|
return _len;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public override int Crc32Hash
|
|
|
|
{
|
|
|
|
get { return _hash; }
|
|
|
|
}
|
|
|
|
|
2014-10-22 13:47:38 +00:00
|
|
|
public override byte[] GetContents()
|
|
|
|
{
|
|
|
|
lock (_stream)
|
|
|
|
{
|
|
|
|
var output = new byte[_len];
|
|
|
|
var oldpos = _stream.Position;
|
|
|
|
_stream.Position = _pos;
|
2014-10-22 17:07:03 +00:00
|
|
|
for (long i = 0; i < _len; i += int.MaxValue) // for-loop for supporting long file sizes
|
2014-10-22 13:47:38 +00:00
|
|
|
{
|
|
|
|
var toRead = (int) Math.Min(int.MaxValue, _len);
|
|
|
|
var buffer = new byte[toRead];
|
|
|
|
var readReal = _stream.Read(buffer, 0, toRead);
|
|
|
|
buffer.CopyTo(output, i);
|
2014-10-22 17:07:03 +00:00
|
|
|
|
|
|
|
i -= (toRead - readReal); // make absolutely sure everything gets read
|
2014-10-22 13:47:38 +00:00
|
|
|
}
|
2014-10-22 17:07:03 +00:00
|
|
|
_stream.Position = oldpos;
|
2014-10-22 13:47:38 +00:00
|
|
|
return output;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|