Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
BlobHeap.cs
Go to the documentation of this file.
2using System.Text;
3
5
6internal struct BlobHeap
7{
8 private static byte[][] s_virtualValues;
9
10 internal readonly MemoryBlock Block;
11
13
14 internal BlobHeap(MemoryBlock block, MetadataKind metadataKind)
15 {
16 _lazyVirtualHeap = null;
17 Block = block;
18 if (s_virtualValues == null && metadataKind != 0)
19 {
20 s_virtualValues = new byte[5][]
21 {
22 null,
23 new byte[8] { 176, 63, 95, 127, 17, 213, 10, 58 },
24 new byte[160]
25 {
26 0, 36, 0, 0, 4, 128, 0, 0, 148, 0,
27 0, 0, 6, 2, 0, 0, 0, 36, 0, 0,
28 82, 83, 65, 49, 0, 4, 0, 0, 1, 0,
29 1, 0, 7, 209, 250, 87, 196, 174, 217, 240,
30 163, 46, 132, 170, 15, 174, 253, 13, 233, 232,
31 253, 106, 236, 143, 135, 251, 3, 118, 108, 131,
32 76, 153, 146, 30, 178, 59, 231, 154, 217, 213,
33 220, 193, 221, 154, 210, 54, 19, 33, 2, 144,
34 11, 114, 60, 249, 128, 149, 127, 196, 225, 119,
35 16, 143, 198, 7, 119, 79, 41, 232, 50, 14,
36 146, 234, 5, 236, 228, 232, 33, 192, 165, 239,
37 232, 241, 100, 92, 76, 12, 147, 193, 171, 153,
38 40, 93, 98, 44, 170, 101, 44, 29, 250, 214,
39 61, 116, 93, 111, 45, 229, 241, 126, 94, 175,
40 15, 196, 150, 61, 38, 28, 138, 18, 67, 101,
41 24, 32, 109, 192, 147, 52, 77, 90, 210, 147
42 },
43 new byte[25]
44 {
45 1, 0, 0, 0, 0, 0, 1, 0, 84, 2,
46 13, 65, 108, 108, 111, 119, 77, 117, 108, 116,
47 105, 112, 108, 101, 0
48 },
49 new byte[25]
50 {
51 1, 0, 0, 0, 0, 0, 1, 0, 84, 2,
52 13, 65, 108, 108, 111, 119, 77, 117, 108, 116,
53 105, 112, 108, 101, 1
54 }
55 };
56 }
57 }
58
59 internal byte[] GetBytes(BlobHandle handle)
60 {
61 if (handle.IsVirtual)
62 {
63 return GetVirtualBlobBytes(handle, unique: true);
64 }
65 int heapOffset = handle.GetHeapOffset();
66 int numberOfBytesRead;
67 int num = Block.PeekCompressedInteger(heapOffset, out numberOfBytesRead);
68 if (num == int.MaxValue)
69 {
71 }
72 return Block.PeekBytes(heapOffset + numberOfBytesRead, num);
73 }
74
76 {
77 if (handle.IsVirtual)
78 {
80 }
81 Block.PeekHeapValueOffsetAndSize(handle.GetHeapOffset(), out var offset, out var size);
82 return Block.GetMemoryBlockAt(offset, size);
83 }
84
86 {
88 lock (orCreateVirtualHeap)
89 {
90 if (!orCreateVirtualHeap.TryGetMemoryBlock(handle.RawValue, out var block))
91 {
92 return orCreateVirtualHeap.AddBlob(handle.RawValue, GetVirtualBlobBytes(handle, unique: false));
93 }
94 return block;
95 }
96 }
97
102
104 {
105 if (handle.IsVirtual)
106 {
107 return default(BlobHandle);
108 }
109 if (!Block.PeekHeapValueOffsetAndSize(handle.GetHeapOffset(), out var offset, out var size))
110 {
111 return default(BlobHandle);
112 }
113 int num = offset + size;
114 if (num >= Block.Length)
115 {
116 return default(BlobHandle);
117 }
118 return BlobHandle.FromOffset(num);
119 }
120
121 internal byte[] GetVirtualBlobBytes(BlobHandle handle, bool unique)
122 {
123 BlobHandle.VirtualIndex virtualIndex = handle.GetVirtualIndex();
124 byte[] array = s_virtualValues[(uint)virtualIndex];
125 if (virtualIndex - 3 <= BlobHandle.VirtualIndex.ContractPublicKeyToken)
126 {
127 array = (byte[])array.Clone();
128 handle.SubstituteTemplateParameters(array);
129 }
130 else if (unique)
131 {
132 array = (byte[])array.Clone();
133 }
134 return array;
135 }
136
138 {
139 BlobReader blobReader = GetBlobReader(handle);
140 int num = blobReader.ReadByte();
141 if (num > 127)
142 {
144 }
146 StringBuilder builder = instance.Builder;
147 bool flag = true;
148 while (blobReader.RemainingBytes > 0)
149 {
150 if (num != 0 && !flag)
151 {
152 builder.Append((char)num);
153 }
154 BlobReader blobReader2 = GetBlobReader(blobReader.ReadBlobHandle());
155 builder.Append(blobReader2.ReadUTF8(blobReader2.Length));
156 flag = false;
157 }
158 return instance.ToStringAndFree();
159 }
160
161 internal bool DocumentNameEquals(DocumentNameBlobHandle handle, string other, bool ignoreCase)
162 {
163 BlobReader blobReader = GetBlobReader(handle);
164 int num = blobReader.ReadByte();
165 if (num > 127)
166 {
167 return false;
168 }
169 int ignoreCaseMask = StringUtils.IgnoreCaseMask(ignoreCase);
170 int num2 = 0;
171 int firstDifferenceIndex;
172 for (bool flag = true; blobReader.RemainingBytes > 0; num2 = firstDifferenceIndex, flag = false)
173 {
174 if (num != 0 && !flag)
175 {
176 if (num2 == other.Length || !StringUtils.IsEqualAscii(other[num2], num, ignoreCaseMask))
177 {
178 return false;
179 }
180 num2++;
181 }
182 MemoryBlock memoryBlock = GetMemoryBlock(blobReader.ReadBlobHandle());
183 switch (memoryBlock.Utf8NullTerminatedFastCompare(0, other, num2, out firstDifferenceIndex, '\0', ignoreCase))
184 {
185 case MemoryBlock.FastComparisonResult.Inconclusive:
186 return GetDocumentName(handle).Equals(other, ignoreCase ? StringComparison.OrdinalIgnoreCase : StringComparison.Ordinal);
187 default:
188 if (firstDifferenceIndex - num2 == memoryBlock.Length)
189 {
190 continue;
191 }
192 break;
194 break;
195 }
196 return false;
197 }
198 return num2 == other.Length;
199 }
200}
MemoryBlock AddBlob(uint rawHandle, byte[] value)
bool TryGetMemoryBlock(uint rawHandle, out MemoryBlock block)
static VirtualHeap GetOrCreateVirtualHeap(ref VirtualHeap? lazyHeap)
static int IgnoreCaseMask(bool ignoreCase)
Definition StringUtils.cs:5
static bool IsEqualAscii(int a, int b, int ignoreCaseMask)
static string Format(string resourceFormat, object p1)
Definition SR.cs:118
static string InvalidDocumentName
Definition SR.cs:114
Definition SR.cs:7
StringBuilder Append(char value, int repeatCount)
unsafe MemoryBlock GetMemoryBlockAt(int offset, int length)
unsafe int PeekCompressedInteger(int offset, out int numberOfBytesRead)
unsafe byte[] PeekBytes(int offset, int byteCount)
bool PeekHeapValueOffsetAndSize(int index, out int offset, out int size)
unsafe FastComparisonResult Utf8NullTerminatedFastCompare(int offset, string text, int textStart, out int firstDifferenceIndex, char terminator, bool ignoreCase)
static BlobHandle FromOffset(int heapOffset)
Definition BlobHandle.cs:34
unsafe string ReadUTF8(int byteCount)
byte[] GetVirtualBlobBytes(BlobHandle handle, bool unique)
Definition BlobHeap.cs:121
BlobReader GetBlobReader(BlobHandle handle)
Definition BlobHeap.cs:98
byte[] GetBytes(BlobHandle handle)
Definition BlobHeap.cs:59
BlobHandle GetNextHandle(BlobHandle handle)
Definition BlobHeap.cs:103
MemoryBlock GetMemoryBlock(BlobHandle handle)
Definition BlobHeap.cs:75
bool DocumentNameEquals(DocumentNameBlobHandle handle, string other, bool ignoreCase)
Definition BlobHeap.cs:161
MemoryBlock GetVirtualHandleMemoryBlock(BlobHandle handle)
Definition BlobHeap.cs:85
BlobHeap(MemoryBlock block, MetadataKind metadataKind)
Definition BlobHeap.cs:14
string GetDocumentName(DocumentNameBlobHandle handle)
Definition BlobHeap.cs:137