Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
WindowsManagedCallbackHandler.cs
Go to the documentation of this file.
1using System;
7using _003CCppImplementationDetails_003E;
8
10
12{
13 private unsafe void* _parentExitEvent = null;
14
15 private unsafe void* _callManagedPlease = null;
16
17 private unsafe void* _managedFunctionDataCanBeWritten = null;
18
19 private unsafe void* _proxyProcessNeedsToChange = null;
20
21 private unsafe void* _newCallbackRegistered;
22
24
26
28
30
31 private uint _managedCallArgs;
32
34
36
37 public unsafe static int Initialize(void* parentExitEvent)
38 {
39 if (parentExitEvent == null)
40 {
41 return -2147024809;
42 }
44 int num = instance.InitializeInstance(parentExitEvent);
45 if (num < 0)
46 {
47 instance = null;
48 }
49 return num;
50 }
51
53 {
54 int num = 0;
56 while (true)
57 {
58 bool flag = false;
59 uint num2 = 0u;
60 uint num3 = uint.MaxValue;
61 uint num4 = uint.MaxValue;
62 uint num5 = uint.MaxValue;
63 uint num6 = uint.MaxValue;
64 uint num7 = uint.MaxValue;
65 bool lockTaken = false;
66 try
67 {
70 if (num2 > 64)
71 {
72 return -2147220991;
73 }
74 for (int i = 0; i < nativeToManagedCallbacks.Count; i++)
75 {
77 }
78 num3 = num2 - 5;
80 num4 = num2 - 4;
82 num5 = num2 - 3;
84 num6 = num2 - 2;
86 }
87 finally
88 {
89 if (lockTaken)
90 {
92 }
93 }
94 bool lockTaken2 = false;
95 try
96 {
99 if (proxyProcessHandler != null)
100 {
101 IntPtr proxyProcessWantsToTalk = proxyProcessHandler.ProxyProcessWantsToTalk;
104 num7 = num2 - 1;
105 *(int*)((ref *(_003F*)(num7 * 4)) + (ref *(_003F*)(&_0024ArrayType_0024_0024_0024BY0EA_0040PAX))) = (int)intPtr2.ToPointer();
106 }
107 else
108 {
109 num2--;
110 }
111 uint num8 = global::_003CModule_003E.WaitForMultipleObjects(num2, (void**)(&_0024ArrayType_0024_0024_0024BY0EA_0040PAX), 0, uint.MaxValue);
112 if (num8 == uint.MaxValue)
113 {
114 uint lastError = global::_003CModule_003E.GetLastError();
115 int num9 = (((int)lastError > 0) ? ((int)(lastError & 0xFFFF) | -2147024896) : ((int)lastError));
116 num = num9;
117 }
118 num8 = num8;
119 if (num8 < num3)
120 {
121 bool flag2 = false;
124 uint num10 = managedCallArgs;
125 num = nativeToManagedCallback.eventSignalledFunction(nativeToManagedCallback.originalWaitHandle, nativeToManagedCallback.pContext, &managedCallType2, &num10, &flag2);
126 if (num >= 0)
127 {
130 if (flag2)
131 {
132 void* dupedWaitHandle = nativeToManagedCallback.dupedWaitHandle;
133 if (dupedWaitHandle != null && dupedWaitHandle != (void*)(-1))
134 {
135 global::_003CModule_003E.CloseHandle(dupedWaitHandle);
136 nativeToManagedCallback.dupedWaitHandle = (void*)(-1);
137 }
139 }
140 }
141 }
142 else if (num8 == num4)
143 {
146 global::_003CModule_003E.SetEvent(_managedFunctionDataCanBeWritten);
147 }
148 else if (num8 != num5 && num8 != num3)
149 {
150 if (num8 == num6)
151 {
152 Thread.CurrentThread.Abort();
153 }
154 else if (num7 != uint.MaxValue && num8 == num7)
155 {
159 global::_003CModule_003E.SetEvent(_proxyProcessHandler.SharedAsyncDataSafeToWrite.ToPointer());
160 }
161 }
162 else
163 {
164 flag = true;
165 }
166 }
167 finally
168 {
169 if (lockTaken2)
170 {
172 }
173 }
174 if (!flag)
175 {
176 break;
177 }
178 global::_003CModule_003E.Sleep(0u);
179 }
180 return num;
181 }
182
184 {
185 if (global::_003CModule_003E.SetEvent(_proxyProcessNeedsToChange) == 0)
186 {
187 uint lastError = global::_003CModule_003E.GetLastError();
188 return ((int)lastError > 0) ? ((int)(lastError & 0xFFFF) | -2147024896) : ((int)lastError);
189 }
190 bool lockTaken = false;
191 try
192 {
195 }
196 finally
197 {
198 if (lockTaken)
199 {
201 }
202 }
203 if (global::_003CModule_003E.ResetEvent(_proxyProcessNeedsToChange) == 0)
204 {
205 uint lastError2 = global::_003CModule_003E.GetLastError();
206 return ((int)lastError2 > 0) ? ((int)(lastError2 & 0xFFFF) | -2147024896) : ((int)lastError2);
207 }
208 return 0;
209 }
210
212 {
213 return instance.CallManagedFunctionForMeImpl(managedCallType, args);
214 }
215
216 public unsafe static int RegisterNativeToManagedCallback(void* waitForThisHandle, delegate*<void*, void*, ManagedCallType*, uint*, bool*, int> eventSignalledFunc, void* pContext)
217 {
218 return instance.RegisterNativeToManagedCallbackImpl(waitForThisHandle, eventSignalledFunc, pContext);
219 }
220
225
227 {
229 if (callManagedPlease != null)
230 {
231 global::_003CModule_003E.CloseHandle(callManagedPlease);
232 _callManagedPlease = null;
233 }
236 {
237 global::_003CModule_003E.CloseHandle(managedFunctionDataCanBeWritten);
239 }
241 if (proxyProcessNeedsToChange != null)
242 {
243 global::_003CModule_003E.CloseHandle(proxyProcessNeedsToChange);
245 }
247 if (newCallbackRegistered != null)
248 {
249 global::_003CModule_003E.CloseHandle(newCallbackRegistered);
251 }
253 {
254 do
255 {
256 global::_003CModule_003E.CloseHandle(nativeToManagedCallbacks[0].dupedWaitHandle);
258 }
260 }
261 }
262
268
269 private unsafe int InitializeInstance(void* parentExitEvent)
270 {
271 _parentExitEvent = parentExitEvent;
272 if ((_callManagedPlease = global::_003CModule_003E.CreateEventW(null, 0, 0, null)) == null)
273 {
274 uint lastError = global::_003CModule_003E.GetLastError();
275 return ((int)lastError > 0) ? ((int)(lastError & 0xFFFF) | -2147024896) : ((int)lastError);
276 }
277 if ((_managedFunctionDataCanBeWritten = global::_003CModule_003E.CreateEventW(null, 0, 1, null)) == null)
278 {
279 uint lastError2 = global::_003CModule_003E.GetLastError();
280 return ((int)lastError2 > 0) ? ((int)(lastError2 & 0xFFFF) | -2147024896) : ((int)lastError2);
281 }
282 if ((_proxyProcessNeedsToChange = global::_003CModule_003E.CreateEventW(null, 1, 0, null)) == null)
283 {
284 uint lastError3 = global::_003CModule_003E.GetLastError();
285 return ((int)lastError3 > 0) ? ((int)(lastError3 & 0xFFFF) | -2147024896) : ((int)lastError3);
286 }
287 if ((_newCallbackRegistered = global::_003CModule_003E.CreateEventW(null, 0, 0, null)) == null)
288 {
289 uint lastError4 = global::_003CModule_003E.GetLastError();
290 return ((int)lastError4 > 0) ? ((int)(lastError4 & 0xFFFF) | -2147024896) : ((int)lastError4);
291 }
292 return 0;
293 }
294
296 {
299 {
303 int result = 0;
304 switch (global::_003CModule_003E.WaitForMultipleObjects(2u, (void**)(&_0024ArrayType_0024_0024_0024BY01PAX), 0, uint.MaxValue))
305 {
306 case 0u:
307 result = 0;
308 break;
309 case 1u:
312 global::_003CModule_003E.SetEvent(_callManagedPlease);
313 break;
314 case uint.MaxValue:
315 {
316 uint lastError = global::_003CModule_003E.GetLastError();
317 result = (((int)lastError > 0) ? ((int)(lastError & 0xFFFF) | -2147024896) : ((int)lastError));
318 break;
319 }
320 }
321 return result;
322 }
323 return -2147467259;
324 }
325
326 private unsafe int RegisterNativeToManagedCallbackImpl(void* waitForThisHandle, delegate*<void*, void*, ManagedCallType*, uint*, bool*, int> eventSignalledFunc, void* pContext)
327 {
328 if (waitForThisHandle != null && eventSignalledFunc != (delegate*<void*, void*, ManagedCallType*, uint*, bool*, int>)null)
329 {
330 void* ptr = null;
331 int num = ((global::_003CModule_003E.DuplicateHandle(global::_003CModule_003E.GetCurrentProcess(), waitForThisHandle, global::_003CModule_003E.GetCurrentProcess(), &ptr, 0u, 0, 2u) == 0) ? (-2147467259) : 0);
332 int num2 = num;
333 if (num < 0)
334 {
335 if (ptr != null && ptr != (void*)(-1))
336 {
337 global::_003CModule_003E.CloseHandle(ptr);
338 }
339 return num;
340 }
341 bool lockTaken = false;
342 try
343 {
346 if (num2 >= 0)
347 {
350 if (global::_003CModule_003E.SetEvent(_newCallbackRegistered) == 0)
351 {
352 uint lastError = global::_003CModule_003E.GetLastError();
353 num2 = (((int)lastError > 0) ? ((int)(lastError & 0xFFFF) | -2147024896) : ((int)lastError));
354 }
355 if (num2 < 0)
356 {
358 }
359 }
360 }
361 finally
362 {
363 if (lockTaken)
364 {
366 }
367 }
368 return num2;
369 }
370 return -2147024809;
371 }
372
374 {
375 return (nativeToManagedCallbacks.Count + 6 > 64) ? (-2147220991) : 0;
376 }
377
379 protected virtual void Dispose([MarshalAs(UnmanagedType.U1)] bool P_0)
380 {
381 if (P_0)
382 {
384 return;
385 }
386 try
387 {
389 }
390 finally
391 {
392 base.Finalize();
393 }
394 }
395
396 public virtual sealed void Dispose()
397 {
398 Dispose(true);
399 GC.SuppressFinalize(this);
400 }
401
403 {
404 Dispose(false);
405 }
406}
static int CallManagedFunctionForMe(ManagedCallType managedCallType, uint args)
unsafe int CallManagedFunctionForMeImpl(ManagedCallType managedCallType, uint args)
virtual void Dispose([MarshalAs(UnmanagedType.U1)] bool P_0)
unsafe int WaitForAsyncOperationToFinish(out ManagedCallType managedCallType, out uint managedCallArgs)
static unsafe int RegisterNativeToManagedCallback(void *waitForThisHandle, delegate *< void *, void *, ManagedCallType *, uint *, bool *, int > eventSignalledFunc, void *pContext)
unsafe int RegisterNativeToManagedCallbackImpl(void *waitForThisHandle, delegate *< void *, void *, ManagedCallType *, uint *, bool *, int > eventSignalledFunc, void *pContext)
unsafe int SetProxyProcessHandler(IProxyProcessHandler proxyProcessHandler)
void RemoveAt(int index)
Definition List.cs:824
static void SuppressFinalize(object obj)
Definition GC.cs:202
Definition GC.cs:8
static void Exit(object obj)
static void Enter(object obj)
static Thread CurrentThread
Definition Thread.cs:312
unsafe void * ToPointer()
Definition IntPtr.cs:210