forked from Unity-Technologies/UnityDataTools
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUnityFileReader.cs
More file actions
139 lines (113 loc) · 3.8 KB
/
UnityFileReader.cs
File metadata and controls
139 lines (113 loc) · 3.8 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
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
using System;
using System.IO;
using System.Text;
using Force.Crc32;
namespace UnityDataTools.FileSystem;
// This class can be used to read typed data from a UnityFile. Is uses a buffer for better performance.
public class UnityFileReader : IDisposable
{
UnityFile m_File;
byte[] m_Buffer;
long m_BufferStartInFile;
long m_BufferEndInFile;
public long Length { get; }
public UnityFileReader(string path, int bufferSize)
{
m_Buffer = new byte[bufferSize];
m_BufferStartInFile = 0;
m_BufferEndInFile = 0;
m_File = UnityFileSystem.OpenFile(path);
Length = m_File.GetSize();
}
int GetBufferOffset(long fileOffset, int count)
{
// Should we update the buffer?
if (fileOffset < m_BufferStartInFile || fileOffset + count > m_BufferEndInFile)
{
if (count > m_Buffer.Length)
throw new IOException("Requested size is larger than cache size");
m_BufferStartInFile = m_File.Seek(fileOffset);
if (m_BufferStartInFile != fileOffset)
throw new IOException("Invalid file offset");
m_BufferEndInFile = m_File.Read(m_Buffer.Length, m_Buffer);
m_BufferEndInFile += m_BufferStartInFile;
}
return (int)(fileOffset - m_BufferStartInFile);
}
public void ReadArray(long fileOffset, int size, Array dest)
{
var offset = GetBufferOffset(fileOffset, size);
Buffer.BlockCopy(m_Buffer, offset, dest, 0, size);
}
public string ReadString(long fileOffset, int size)
{
var offset = GetBufferOffset(fileOffset, size);
return Encoding.Default.GetString(m_Buffer, offset, size);
}
public float ReadFloat(long fileOffset)
{
var offset = GetBufferOffset(fileOffset, 4);
return BitConverter.ToSingle(m_Buffer, offset);
}
public double ReadDouble(long fileOffset)
{
var offset = GetBufferOffset(fileOffset, 8);
return BitConverter.ToDouble(m_Buffer, offset);
}
public long ReadInt64(long fileOffset)
{
var offset = GetBufferOffset(fileOffset, 8);
return BitConverter.ToInt64(m_Buffer, offset);
}
public ulong ReadUInt64(long fileOffset)
{
var offset = GetBufferOffset(fileOffset, 8);
return BitConverter.ToUInt64(m_Buffer, offset);
}
public int ReadInt32(long fileOffset)
{
var offset = GetBufferOffset(fileOffset, 4);
return BitConverter.ToInt32(m_Buffer, offset);
}
public uint ReadUInt32(long fileOffset)
{
var offset = GetBufferOffset(fileOffset, 4);
return BitConverter.ToUInt32(m_Buffer, offset);
}
public short ReadInt16(long fileOffset)
{
var offset = GetBufferOffset(fileOffset, 2);
return BitConverter.ToInt16(m_Buffer, offset);
}
public ushort ReadUInt16(long fileOffset)
{
var offset = GetBufferOffset(fileOffset, 2);
return BitConverter.ToUInt16(m_Buffer, offset);
}
public sbyte ReadInt8(long fileOffset)
{
var offset = GetBufferOffset(fileOffset, 1);
return (sbyte)m_Buffer[offset];
}
public byte ReadUInt8(long fileOffset)
{
var offset = GetBufferOffset(fileOffset, 1);
return m_Buffer[offset];
}
public uint ComputeCRC(long fileOffset, int size, uint crc32 = 0)
{
var readSize = size > m_Buffer.Length ? m_Buffer.Length : size;
var readBytes = 0;
while (readBytes < size)
{
var offset = GetBufferOffset(fileOffset, readSize);
crc32 = Crc32Algorithm.Append(crc32, m_Buffer, offset, readSize);
readBytes += readSize;
}
return crc32;
}
public void Dispose()
{
m_File.Dispose();
}
}