-
Notifications
You must be signed in to change notification settings - Fork 69
Expand file tree
/
Copy pathUnityFileStream.cs
More file actions
82 lines (66 loc) · 2.54 KB
/
Copy pathUnityFileStream.cs
File metadata and controls
82 lines (66 loc) · 2.54 KB
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
using System;
using System.IO;
namespace UnityDataTools.FileSystem;
/// <summary>
/// A read-only, seekable <see cref="Stream"/> over a <see cref="UnityFile"/>. This lets code that
/// expects a standard Stream read from any path the native filesystem can open, including files
/// inside a mounted archive (e.g. "archive:/CAB-...").
/// </summary>
public class UnityFileStream : Stream
{
private readonly UnityFile m_File;
private readonly long m_Length;
private long m_Position;
public UnityFileStream(string path)
{
m_File = UnityFileSystem.OpenFile(path);
m_Length = m_File.GetSize();
}
public override bool CanRead => true;
public override bool CanSeek => true;
public override bool CanWrite => false;
public override long Length => m_Length;
public override long Position
{
get => m_Position;
set => Seek(value, System.IO.SeekOrigin.Begin);
}
public override int Read(byte[] buffer, int offset, int count)
{
if (count == 0 || m_Position >= m_Length)
return 0;
long toRead = Math.Min(count, m_Length - m_Position);
// UnityFile.Read always fills the destination buffer from index 0, so when the caller
// wants the data at a non-zero offset we read into a temporary buffer and copy it across.
byte[] dest = offset == 0 ? buffer : new byte[toRead];
m_File.Seek(m_Position);
long actual = m_File.Read(toRead, dest);
if (offset != 0)
Buffer.BlockCopy(dest, 0, buffer, offset, (int)actual);
m_Position += actual;
return (int)actual;
}
public override long Seek(long offset, System.IO.SeekOrigin origin)
{
long newPosition = origin switch
{
System.IO.SeekOrigin.Begin => offset,
System.IO.SeekOrigin.Current => m_Position + offset,
System.IO.SeekOrigin.End => m_Length + offset,
_ => throw new ArgumentOutOfRangeException(nameof(origin)),
};
if (newPosition < 0)
throw new IOException("Attempted to seek before the start of the stream.");
m_Position = newPosition;
return m_Position;
}
public override void Flush() { }
public override void SetLength(long value) => throw new NotSupportedException();
public override void Write(byte[] buffer, int offset, int count) => throw new NotSupportedException();
protected override void Dispose(bool disposing)
{
if (disposing)
m_File.Dispose();
base.Dispose(disposing);
}
}