Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
Effect.cs
Go to the documentation of this file.
1using System;
8
10
12{
14
16
17 internal byte[] pCachedEffectData;
18
20
22
24
25 internal static object pSyncObject = new object();
26
27 internal unsafe ID3DXEffect* pComPtr;
28
30
32
34 {
35 get
36 {
37 return _currentTechnique;
38 }
39 set
40 {
43 if (value == null)
44 {
46 }
48 {
49 if (value._parent != this)
50 {
51 throw new InvalidOperationException();
52 }
53 EffectPass activePass = _parent.activePass;
54 if (activePass != null)
55 {
56 activePass.EndPass();
57 _parent.activePass = null;
58 }
60 int num = ((delegate* unmanaged[Stdcall, Stdcall]<IntPtr, sbyte*, int>)(int)(*(uint*)(*(int*)ptr + 232)))((nint)ptr, value._handle);
61 if (num < 0)
62 {
64 }
66 }
67 }
68 }
69
93
94 private unsafe void CreateEffectFromCode(GraphicsDevice graphicsDevice, byte[] effectCode)
95 {
96 //The blocks IL_0203 are reachable both inside and outside the pinned region starting at IL_0057. ILSpy has duplicated these blocks in order to place them both within and outside the `fixed` statement.
97 //The blocks IL_0203 are reachable both inside and outside the pinned region starting at IL_00ac. ILSpy has duplicated these blocks in order to place them both within and outside the `fixed` statement.
98 if (effectCode != null)
99 {
100 int num = effectCode.Length;
101 if (num != 0)
102 {
103 if (num % 4 != 0)
104 {
105 string text = "effectCode";
107 }
108 if (graphicsDevice == null)
109 {
111 }
112 if ((uint)num >= 8u)
113 {
114 ID3DXBuffer* ptr = null;
115 fixed (void* ptr2 = &System.Runtime.CompilerServices.Unsafe.As<byte, void>(ref effectCode[0]))
116 {
117 uint* ptr3 = (uint*)ptr2;
118 if (*ptr3 == 3169848271u)
119 {
120 ptr3++;
121 uint num2 = *ptr3;
122 if ((nint)effectCode.LongLength >= (int)(num2 + 4))
123 {
125 if (*(int*)ptr4 == -16840447)
126 {
127 StateTrackerDevice* pStateTracker = graphicsDevice.pStateTracker;
128 *(int*)((byte*)pStateTracker + 92) = 0;
129 *(int*)((byte*)pStateTracker + 96) = 0;
130 *(int*)((byte*)pStateTracker + 100) = 0;
132 {
133 int num3 = _003CModule_003E.D3DXCreateEffectEx((IDirect3DDevice9*)pStateTracker, ptr4, (uint)((nint)effectCode.LongLength - (int)num2), null, null, null, 131072u, null, ptr5, &ptr);
134 if (ptr != null)
135 {
137 ((delegate* unmanaged[Stdcall, Stdcall]<IntPtr, uint>)(int)(*(uint*)(*(int*)intPtr + 8)))((nint)intPtr);
138 ptr = null;
139 }
140 uint num4 = *(uint*)((byte*)pStateTracker + 92);
141 if (num4 != 0)
142 {
144 }
145 uint num5 = *(uint*)((byte*)pStateTracker + 96);
146 if (num5 != 0)
147 {
149 }
150 if (num3 < 0)
151 {
153 }
154 int num6 = *(int*)((byte*)pStateTracker + 100);
155 if (num6 < 0)
156 {
158 }
159 _parent = graphicsDevice;
165 if (!enumerator.MoveNext())
166 {
167 return;
168 }
169 do
170 {
172 if (!enumerator2.MoveNext())
173 {
174 continue;
175 }
176 uint* ptr6 = ptr3 + 1;
177 do
178 {
179 EffectPass current = enumerator2.Current;
180 if (ptr6 < ptr4)
181 {
182 ptr3++;
183 ptr6++;
184 current._stateFlags = (EffectStateFlags)(*ptr3);
185 ptr3++;
186 ptr6++;
187 current._textureFlags = *ptr3;
188 continue;
189 }
191 }
192 while (enumerator2.MoveNext());
193 }
194 while (enumerator.MoveNext());
195 return;
196 }
197 }
198 }
199 }
201 }
202 }
204 }
205 }
207 }
208
209 private static string FormatShaderModel(uint shaderModel)
210 {
211 if (shaderModel == 513)
212 {
213 return "2.x";
214 }
215 return string.Format(args: new object[2]
216 {
217 shaderModel >> 8,
218 shaderModel & 0xFFu
219 }, provider: CultureInfo.CurrentCulture, format: "{0}.{1}");
220 }
221
222 private unsafe Effect(ID3DXEffect* pInterface, GraphicsDevice pDevice)
223 {
225 ((object)this)._002Ector();
226 try
227 {
228 _parent = pDevice;
230 return;
231 }
232 catch
233 {
234 //try-fault
235 base.Dispose(true);
236 throw;
237 }
238 }
239
240 public Effect(GraphicsDevice graphicsDevice, byte[] effectCode)
241 {
242 try
243 {
244 CreateEffectFromCode(graphicsDevice, effectCode);
245 return;
246 }
247 catch
248 {
249 //try-fault
250 base.Dispose(true);
251 throw;
252 }
253 }
254
255 protected unsafe Effect(Effect cloneSource)
256 {
257 try
258 {
259 if (cloneSource == null)
260 {
262 }
263 IntPtr intPtr = (IntPtr)cloneSource.pComPtr;
265 GraphicsDevice parent = cloneSource._parent;
266 if (parent == null)
267 {
269 }
270 ID3DXEffect* ptr = null;
271 ID3DXEffect* ptr2 = cloneSource.pComPtr;
272 int num = ((delegate* unmanaged[Stdcall, Stdcall]<IntPtr, IDirect3DDevice9*, ID3DXEffect**, int>)(int)(*(uint*)(*(int*)ptr2 + 308)))((nint)ptr2, (IDirect3DDevice9*)parent.pStateTracker, &ptr);
273 if (num < 0)
274 {
276 }
277 _parent = parent;
278 pComPtr = ptr;
280 cloneSource.AddClonedEffect(this);
284 int num2 = 0;
285 if (0 >= pParamCollection.Count)
286 {
287 return;
288 }
289 do
290 {
292 if (effectParameter.ElementCount > 1)
293 {
297 {
300 {
303 {
304 goto IL_015f;
305 }
306 }
307 }
308 int rows = effectParameter._rows;
309 int columns = effectParameter._columns;
310 int num3 = effectParameter.ElementCount * columns * rows;
311 if (num3 > 0)
312 {
313 float[] valueSingleArray = cloneSource.pParamCollection[num2].GetValueSingleArray(num3);
315 }
316 }
317 goto IL_015f;
318 IL_015f:
319 num2++;
320 }
321 while (num2 < pParamCollection.Count);
322 return;
323 }
324 catch
325 {
326 //try-fault
327 base.Dispose(true);
328 throw;
329 }
330 }
331
332 public virtual Effect Clone()
333 {
334 return new Effect(this);
335 }
336
337 protected internal virtual void OnApply()
338 {
339 }
340
341 internal unsafe void OnLostDevice()
342 {
346 int num = ((delegate* unmanaged[Stdcall, Stdcall]<IntPtr, int>)(int)(*(uint*)(*(int*)intPtr2 + 276)))((nint)intPtr2);
347 if (num < 0)
348 {
350 }
351 }
352
353 internal unsafe void OnResetDevice()
354 {
358 int num = ((delegate* unmanaged[Stdcall, Stdcall]<IntPtr, int>)(int)(*(uint*)(*(int*)intPtr2 + 280)))((nint)intPtr2);
359 if (num < 0)
360 {
362 }
363 }
364
365 internal void AddClonedEffect(Effect effect)
366 {
367 if (pClonedEffects == null)
368 {
370 }
371 pClonedEffects.Add(new WeakReference(effect));
372 }
373
374 internal virtual int SaveDataForRecreation()
375 {
378 return 0;
379 }
380
382 {
383 //ILSpy generated this explicit interface implementation from .override directive in SaveDataForRecreation
384 return this.SaveDataForRecreation();
385 }
386
387 internal unsafe virtual int RecreateAndPopulateObject()
388 {
389 if (pComPtr != null)
390 {
391 return 0;
392 }
393 GraphicsDevice parent = _parent;
394 if (parent == null)
395 {
396 return -2147467259;
397 }
398 int num = 0;
400 if (weakReference == null)
401 {
403 }
404 else if (weakReference.Target is Effect { pComPtr: var ptr } && ptr != null)
405 {
406 ID3DXEffect* ptr2 = null;
407 num = ((delegate* unmanaged[Stdcall, Stdcall]<IntPtr, IDirect3DDevice9*, ID3DXEffect**, int>)(int)(*(uint*)(*(int*)ptr + 308)))((nint)ptr, (IDirect3DDevice9*)_parent.pStateTracker, &ptr2);
408 pComPtr = ptr2;
411 }
413 if (list != null)
414 {
415 int num2 = 0;
416 if (0 < list.Count)
417 {
418 do
419 {
420 if (num >= 0 && pClonedEffects[num2].Target is Effect effect2 && effect2.pComPtr == null)
421 {
422 num = effect2.RecreateAndPopulateObject();
423 }
424 num2++;
425 }
426 while (num2 < pClonedEffects.Count);
427 }
428 }
430 if (currentTechnique != null && pComPtr != null)
431 {
433 _currentTechnique = null;
435 }
436 return num;
437 }
438
440 {
441 //ILSpy generated this explicit interface implementation from .override directive in RecreateAndPopulateObject
442 return this.RecreateAndPopulateObject();
443 }
444
445 internal unsafe virtual void ReleaseNativeObject([MarshalAs(UnmanagedType.U1)] bool disposeManagedResource)
446 {
447 bool lockTaken = false;
448 try
449 {
451 GraphicsDevice parent = _parent;
452 if (parent != null && pComPtr != null)
453 {
455 }
456 pComPtr = null;
457 }
458 finally
459 {
460 if (lockTaken)
461 {
463 }
464 }
465 }
466
468 {
469 //ILSpy generated this explicit interface implementation from .override directive in ReleaseNativeObject
470 this.ReleaseNativeObject(disposeManagedResource);
471 }
472
473 [return: MarshalAs(UnmanagedType.U1)]
474 internal virtual bool WantParameter(EffectParameter P_0)
475 {
476 return true;
477 }
478
479 internal unsafe static Effect GetManagedObject(ID3DXEffect* pInterface, GraphicsDevice pDevice, uint pool)
480 {
482 if (effect != null)
483 {
484 ((delegate* unmanaged[Stdcall, Stdcall]<IntPtr, uint>)(int)(*(uint*)(*(int*)pInterface + 8)))((nint)pInterface);
485 effect.isDisposed = false;
487 }
488 else
489 {
490 effect = new Effect(pInterface, pDevice);
491 pDevice.Resources.AddTrackedObject(effect, pInterface, pool, 0uL, ref effect._internalHandle);
492 }
493 return effect;
494 }
495
496 private void OnObjectCreation()
497 {
499 }
500
501 private void _0021Effect()
502 {
503 if (!isDisposed)
504 {
505 isDisposed = true;
507 }
508 }
509
510 private void _007EEffect()
511 {
512 _0021Effect();
513 }
514
516 protected override void Dispose([MarshalAs(UnmanagedType.U1)] bool P_0)
517 {
518 if (P_0)
519 {
520 try
521 {
522 _007EEffect();
523 return;
524 }
525 finally
526 {
527 base.Dispose(true);
528 }
529 }
530 try
531 {
532 _0021Effect();
533 }
534 finally
535 {
536 base.Dispose(false);
537 }
538 }
539}
unsafe void AddTrackedObject(object managedObject, void *pComPtr, uint resourceManagementMode, ulong handle, ref ulong updatedHandle)
unsafe void ReleaseAllReferences(ulong handle, [MarshalAs(UnmanagedType.U1)] bool dispose)
unsafe void UpdateParent(ID3DXBaseEffect *parent, sbyte *parameter, [MarshalAs(UnmanagedType.U1)] bool arrayElements)
override void Dispose([MarshalAs(UnmanagedType.U1)] bool P_0)
Definition Effect.cs:516
Effect(GraphicsDevice graphicsDevice, byte[] effectCode)
Definition Effect.cs:240
EffectTechniqueCollection Techniques
Definition Effect.cs:31
EffectParameterCollection Parameters
Definition Effect.cs:29
static unsafe Effect GetManagedObject(ID3DXEffect *pInterface, GraphicsDevice pDevice, uint pool)
Definition Effect.cs:479
virtual bool WantParameter(EffectParameter P_0)
Definition Effect.cs:474
unsafe Effect(ID3DXEffect *pInterface, GraphicsDevice pDevice)
Definition Effect.cs:222
static string FormatShaderModel(uint shaderModel)
Definition Effect.cs:209
unsafe void CreateEffectFromCode(GraphicsDevice graphicsDevice, byte[] effectCode)
Definition Effect.cs:94
List< WeakReference > pClonedEffects
Definition Effect.cs:21
unsafe EffectTechnique CurrentTechnique
Definition Effect.cs:34
unsafe Effect(Effect cloneSource)
Definition Effect.cs:255
virtual unsafe void ReleaseNativeObject([MarshalAs(UnmanagedType.U1)] bool disposeManagedResource)
Definition Effect.cs:445
EffectParameterCollection pParamCollection
Definition Effect.cs:15
virtual unsafe int RecreateAndPopulateObject()
Definition Effect.cs:387
void AddClonedEffect(Effect effect)
Definition Effect.cs:365
EffectTechniqueCollection pTechniqueCollection
Definition Effect.cs:13
static Exception GetExceptionFromResult(uint result)
static void CheckDisposed(object obj, IntPtr pComPtr)
Definition Helpers.cs:188
static void ReRegisterForFinalize(object obj)
Definition GC.cs:214
Definition GC.cs:8
static CultureInfo CurrentCulture
static void Exit(object obj)
static void Enter(object obj)
void ReleaseNativeObject([MarshalAs(UnmanagedType.U1)] bool disposeManagedResource)