Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
DeviceResourceManager.cs
Go to the documentation of this file.
1using System;
5
7
9{
11
13
14 private object pSyncObject;
15
16 private ulong _currentMaxHandle;
17
18 private void IncrementRefCount(ulong handle)
19 {
20 if (pSyncObject == null)
21 {
22 return;
23 }
24 bool lockTaken = false;
25 try
26 {
29 int currentRefCount = value.CurrentRefCount;
30 if (currentRefCount == int.MaxValue)
31 {
32 throw new InvalidOperationException();
33 }
34 value.CurrentRefCount = currentRefCount + 1;
36 }
37 finally
38 {
39 if (lockTaken)
40 {
42 }
43 }
44 }
45
46 [return: MarshalAs(UnmanagedType.U1)]
47 private bool Contains(ulong handle)
48 {
49 bool lockTaken = false;
50 try
51 {
54 {
55 return pResourceData[handle].ManagedObject.IsAlive;
56 }
57 return false;
58 }
59 finally
60 {
61 if (lockTaken)
62 {
64 }
65 }
66 }
67
69 {
70 pParentDevice = parent;
71 base._002Ector();
73 pSyncObject = new object();
75 }
76
77 public unsafe void AddTrackedObject(object managedObject, void* pComPtr, uint resourceManagementMode, ulong handle, ref ulong updatedHandle)
78 {
79 bool lockTaken = false;
80 try
81 {
84 {
86 value.pComPtr = pComPtr;
87 value.dwResourceManagementMode = resourceManagementMode;
88 value.CurrentRefCount = 1;
89 value.isDisposed = false;
92 }
93 else
94 {
96 value2.ManagedObject = new WeakReference(managedObject);
97 value2.pComPtr = pComPtr;
98 value2.ResourceName = string.Empty;
99 value2.ResourceTag = null;
100 value2.dwResourceManagementMode = resourceManagementMode;
101 value2.CurrentRefCount = 1;
102 value2.isDisposed = false;
103 updatedHandle = (value2.objectHandle = ++_currentMaxHandle);
106 }
107 }
108 finally
109 {
110 if (lockTaken)
111 {
113 }
114 }
115 }
116
118 {
119 bool lockTaken = false;
120 try
121 {
124 while (enumerator.MoveNext())
125 {
126 object target = enumerator.Current.ManagedObject.Target;
127 if (target != null)
128 {
129 IntPtr intPtr = selectionFilter(target);
132 if (intPtr3 != IntPtr.Zero)
133 {
134 return intPtr3;
135 }
136 }
137 }
138 return IntPtr.Zero;
139 }
140 finally
141 {
142 if (lockTaken)
143 {
145 }
146 }
147 }
148
149 public string GetCachedName(ulong handle)
150 {
151 bool lockTaken = false;
152 try
153 {
156 {
157 return pResourceData[handle].ResourceName;
158 }
159 return string.Empty;
160 }
161 finally
162 {
163 if (lockTaken)
164 {
166 }
167 }
168 }
169
170 public object GetCachedTag(ulong handle)
171 {
172 bool lockTaken = false;
173 try
174 {
177 {
178 return pResourceData[handle].ResourceTag;
179 }
180 return null;
181 }
182 finally
183 {
184 if (lockTaken)
185 {
187 }
188 }
189 }
190
191 public unsafe object GetCachedObject(void* pComPtr)
192 {
193 bool lockTaken = false;
194 try
195 {
198 while (enumerator.MoveNext())
199 {
200 ulong current = enumerator.Current;
201 if (pResourceData[current].pComPtr == pComPtr && pResourceData[current].ManagedObject.IsAlive)
202 {
203 return pResourceData[current].ManagedObject.Target;
204 }
205 }
206 return null;
207 }
208 finally
209 {
210 if (lockTaken)
211 {
213 }
214 }
215 }
216
217 public void SetCachedName(ulong handle, string name)
218 {
219 bool lockTaken = false;
220 try
221 {
224 value.ResourceName = name;
226 }
227 finally
228 {
229 if (lockTaken)
230 {
232 }
233 }
234 }
235
236 public void SetCachedTag(ulong handle, object tag)
237 {
238 bool lockTaken = false;
239 try
240 {
243 value.ResourceTag = tag;
245 }
246 finally
247 {
248 if (lockTaken)
249 {
251 }
252 }
253 }
254
255 public unsafe void ReleaseAllReferences(ulong handle, [MarshalAs(UnmanagedType.U1)] bool dispose)
256 {
257 if (pSyncObject == null)
258 {
259 return;
260 }
261 bool lockTaken = false;
262 try
263 {
266 {
267 return;
268 }
270 value.isDisposed = dispose;
271 if (dispose)
272 {
274 }
275 while (true)
276 {
277 int currentRefCount = value.CurrentRefCount;
278 if (currentRefCount <= 0)
279 {
280 break;
281 }
282 void* pComPtr = value.pComPtr;
283 uint num = ((delegate* unmanaged[Stdcall, Stdcall]<IntPtr, uint>)(int)(*(uint*)(*(int*)pComPtr + 8)))((nint)pComPtr);
284 value.CurrentRefCount = currentRefCount - 1;
285 }
286 value.pComPtr = null;
287 if (dispose)
288 {
290 }
291 else
292 {
294 }
295 }
296 finally
297 {
298 if (lockTaken)
299 {
301 }
302 }
303 }
304
306 {
307 if (pSyncObject == null)
308 {
309 return;
310 }
311 bool lockTaken = false;
312 try
313 {
317 while (enumerator.MoveNext())
318 {
319 ulong current = enumerator.Current;
321 if (resourceData.dwResourceManagementMode == 0)
322 {
324 if (managedObject.IsAlive && managedObject.Target is Effect { isDisposed: false } effect)
325 {
326 effect.OnLostDevice();
327 }
328 else
329 {
330 list.Add(resourceData.objectHandle);
331 }
332 }
333 }
335 while (enumerator2.MoveNext())
336 {
337 ulong current2 = enumerator2.Current;
338 if (pResourceData[current2].CurrentRefCount > 0 && pResourceData[current2].ManagedObject.Target is IGraphicsResource graphicsResource && !pResourceData[current2].isDisposed)
339 {
340 graphicsResource.ReleaseNativeObject(disposeManagedResource: false);
341 }
342 }
343 }
344 finally
345 {
346 if (lockTaken)
347 {
349 }
350 }
351 }
352
354 {
355 if (pSyncObject == null)
356 {
357 return;
358 }
359 bool lockTaken = false;
360 try
361 {
363 ulong[] array = new ulong[pResourceData.Keys.Count];
365 ulong[] array2 = array;
366 for (int i = 0; i < (nint)array2.LongLength; i++)
367 {
368 ulong key = array2[i];
369 if (pResourceData[key].CurrentRefCount > 0 && pResourceData[key].ManagedObject.Target is IGraphicsResource graphicsResource && !pResourceData[key].isDisposed)
370 {
371 graphicsResource.ReleaseNativeObject(disposeManagedResource: false);
372 }
373 }
374 }
375 finally
376 {
377 if (lockTaken)
378 {
380 }
381 }
382 }
383
385 {
386 if (pSyncObject == null)
387 {
388 return;
389 }
390 int num = 0;
391 bool lockTaken = false;
392 try
393 {
395 ulong[] array = new ulong[pResourceData.Keys.Count];
397 for (int i = 0; i < (nint)array.LongLength; i++)
398 {
399 ulong key = array[i];
401 switch (resourceData.dwResourceManagementMode)
402 {
403 case 1u:
404 {
407 {
408 if (!resourceData.isDisposed)
409 {
410 num = graphicsResource.SaveDataForRecreation();
411 }
412 if (num < 0)
413 {
415 }
416 }
417 break;
418 }
419 case 0u:
420 {
422 if (managedObject.IsAlive && managedObject.Target is Effect effect)
423 {
424 if (!resourceData.isDisposed)
425 {
426 num = effect.SaveDataForRecreation();
427 }
428 if (num < 0)
429 {
431 }
432 }
433 break;
434 }
435 }
436 }
437 }
438 finally
439 {
440 if (lockTaken)
441 {
443 }
444 }
445 }
446
448 {
449 if (pSyncObject == null)
450 {
451 return;
452 }
453 bool lockTaken = false;
454 try
455 {
457 ulong[] array = new ulong[pResourceData.Keys.Count];
460 for (int i = 0; i < (nint)array.LongLength; i++)
461 {
462 ulong key = array[i];
464 if (resourceData.dwResourceManagementMode != (uint)pool)
465 {
466 continue;
467 }
469 if (!managedObject.IsAlive)
470 {
471 continue;
472 }
473 Effect effect = managedObject.Target as Effect;
474 if (effect != null && !effect.isDisposed && !deviceRecreated)
475 {
476 effect.OnResetDevice();
477 continue;
478 }
480 {
481 if (effect != null)
482 {
483 list.Add(effect);
484 }
485 else
486 {
487 int num = graphicsResource.RecreateAndPopulateObject();
488 if (num < 0)
489 {
491 }
492 }
493 }
495 {
496 dynamicGraphicsResource.SetContentLost(isContentLost: true);
497 }
498 }
500 while (enumerator.MoveNext())
501 {
502 int num2 = enumerator.Current.RecreateAndPopulateObject();
503 if (num2 < 0)
504 {
506 }
507 }
508 }
509 finally
510 {
511 if (lockTaken)
512 {
514 }
515 }
516 }
517}
IntPtr FindResourceW(Converter< object, IntPtr > selectionFilter)
void RecreateResources(_D3DPOOL pool, [MarshalAs(UnmanagedType.U1)] bool deviceRecreated)
unsafe void AddTrackedObject(object managedObject, void *pComPtr, uint resourceManagementMode, ulong handle, ref ulong updatedHandle)
unsafe void ReleaseAllReferences(ulong handle, [MarshalAs(UnmanagedType.U1)] bool dispose)
void FireDestroyedEvent(string name, object tag)
static Exception GetExceptionFromResult(uint result)
bool ICollection< KeyValuePair< TKey, TValue > >. Remove(KeyValuePair< TKey, TValue > keyValuePair)
void Add(TKey key, TValue value)
static void Exit(object obj)
static void Enter(object obj)
static readonly IntPtr Zero
Definition IntPtr.cs:18