Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
DataCollector.cs
Go to the documentation of this file.
2
4
5internal struct DataCollector
6{
7 [ThreadStatic]
9
10 private unsafe byte* scratchEnd;
11
13
14 private unsafe GCHandle* pinsEnd;
15
17
18 private unsafe byte* scratch;
19
20 private unsafe EventSource.EventData* datas;
21
22 private unsafe GCHandle* pins;
23
24 private byte[] buffer;
25
26 private int bufferPos;
27
28 private int bufferNesting;
29
30 private bool writingScalars;
31
32 internal unsafe void Enable(byte* scratch, int scratchSize, EventSource.EventData* datas, int dataCount, GCHandle* pins, int pinCount)
33 {
35 scratchEnd = scratch + scratchSize;
36 datasEnd = datas + dataCount;
37 pinsEnd = pins + pinCount;
38 this.scratch = scratch;
39 this.datas = datas;
40 this.pins = pins;
41 writingScalars = false;
42 }
43
44 internal void Disable()
45 {
46 this = default(DataCollector);
47 }
48
49 internal unsafe EventSource.EventData* Finish()
50 {
51 ScalarsEnd();
52 return datas;
53 }
54
55 internal unsafe void AddScalar(void* value, int size)
56 {
57 if (bufferNesting == 0)
58 {
59 byte* ptr = scratch;
60 byte* ptr2 = ptr + size;
61 if (scratchEnd < ptr2)
62 {
64 }
66 scratch = ptr2;
67 for (int i = 0; i != size; i++)
68 {
69 ptr[i] = ((byte*)value)[i];
70 }
71 }
72 else
73 {
74 int num = bufferPos;
75 int num2;
76 checked
77 {
78 bufferPos += size;
80 num2 = 0;
81 }
82 while (num2 != size)
83 {
84 buffer[num] = ((byte*)value)[num2];
85 num2++;
86 num++;
87 }
88 }
89 }
90
91 internal unsafe void AddNullTerminatedString(string value)
92 {
93 if (value == null)
94 {
95 value = string.Empty;
96 }
97 int num = value.IndexOf('\0');
98 if (num < 0)
99 {
100 num = value.Length;
101 }
102 int num2 = (num + 1) * 2;
103 if (bufferNesting != 0)
104 {
105 EnsureBuffer(num2);
106 }
107 if (bufferNesting == 0)
108 {
109 ScalarsEnd();
110 PinArray(value, num2);
111 return;
112 }
113 int startIndex = bufferPos;
114 checked
115 {
116 bufferPos += num2;
117 EnsureBuffer();
118 fixed (char* ptr = value)
119 {
120 void* ptr2 = ptr;
121 Marshal.Copy((IntPtr)ptr2, buffer, startIndex, num2);
122 }
123 }
124 }
125
126 internal unsafe void AddArray(Array value, int length, int itemSize)
127 {
128 if (length > 65535)
129 {
130 length = 65535;
131 }
132 int num = length * itemSize;
133 if (bufferNesting != 0)
134 {
135 EnsureBuffer(num + 2);
136 }
137 AddScalar(&length, 2);
138 checked
139 {
140 if (length != 0)
141 {
142 if (bufferNesting == 0)
143 {
144 ScalarsEnd();
145 PinArray(value, num);
146 return;
147 }
148 int dstOffset = bufferPos;
149 bufferPos += num;
150 EnsureBuffer();
151 Buffer.BlockCopy(value, 0, buffer, dstOffset, num);
152 }
153 }
154 }
155
156 internal int BeginBufferedArray()
157 {
159 bufferPos += 2;
160 return bufferPos;
161 }
162
163 internal void EndBufferedArray(int bookmark, int count)
164 {
165 EnsureBuffer();
166 buffer[bookmark - 2] = (byte)count;
167 buffer[bookmark - 1] = (byte)(count >> 8);
168 EndBuffered();
169 }
170
171 internal void BeginBuffered()
172 {
173 ScalarsEnd();
175 }
176
177 internal void EndBuffered()
178 {
180 if (bufferNesting == 0)
181 {
182 EnsureBuffer();
184 buffer = null;
185 bufferPos = 0;
186 }
187 }
188
189 private void EnsureBuffer()
190 {
191 int num = bufferPos;
192 if (buffer == null || buffer.Length < num)
193 {
194 GrowBuffer(num);
195 }
196 }
197
198 private void EnsureBuffer(int additionalSize)
199 {
200 int num = bufferPos + additionalSize;
201 if (buffer == null || buffer.Length < num)
202 {
203 GrowBuffer(num);
204 }
205 }
206
207 private void GrowBuffer(int required)
208 {
209 int num = ((buffer == null) ? 64 : buffer.Length);
210 do
211 {
212 num *= 2;
213 }
214 while (num < required);
215 Array.Resize(ref buffer, num);
216 }
217
218 private unsafe void PinArray(object value, int size)
219 {
220 GCHandle* ptr = pins;
221 if (pinsEnd <= ptr)
222 {
224 }
226 if (datasEnd <= ptr2)
227 {
229 }
230 pins = ptr + 1;
231 datas = ptr2 + 1;
232 *ptr = GCHandle.Alloc(value, GCHandleType.Pinned);
233 ptr2->DataPointer = ptr->AddrOfPinnedObject();
234 ptr2->m_Size = size;
235 }
236
237 private unsafe void ScalarsBegin()
238 {
239 if (!writingScalars)
240 {
242 if (datasEnd <= ptr)
243 {
245 }
246 ptr->DataPointer = (IntPtr)scratch;
247 writingScalars = true;
248 }
249 }
250
251 private unsafe void ScalarsEnd()
252 {
253 checked
254 {
255 if (writingScalars)
256 {
258 ptr->m_Size = (int)(scratch - unchecked((byte*)checked((nuint)ptr->m_Ptr)));
259 datas = ptr + 1;
260 writingScalars = false;
261 }
262 }
263 }
264}
static void BlockCopy(Array src, int srcOffset, Array dst, int dstOffset, int count)
Definition Buffer.cs:102
static void Copy(int[] source, int startIndex, IntPtr destination, int length)
Definition Marshal.cs:800
static string EventSource_PinArrayOutOfRange
Definition SR.cs:1270
static string EventSource_AddScalarOutOfRange
Definition SR.cs:1186
static string EventSource_DataDescriptorsOutOfRange
Definition SR.cs:1192
Definition SR.cs:7
unsafe EventSource.EventData * datas
unsafe void AddArray(Array value, int length, int itemSize)
unsafe void AddScalar(void *value, int size)
unsafe EventSource.EventData * datasEnd
void EndBufferedArray(int bookmark, int count)
unsafe void PinArray(object value, int size)
unsafe EventSource.EventData * Finish()
unsafe void Enable(byte *scratch, int scratchSize, EventSource.EventData *datas, int dataCount, GCHandle *pins, int pinCount)
unsafe void AddNullTerminatedString(string value)
unsafe EventSource.EventData * datasStart
static GCHandle Alloc(object? value)
Definition GCHandle.cs:81