Ameisen

Untitled

May 13th, 2022
1,028
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.14 KB | None | 0 0
  1.     private static readonly Assembly? CpaAssembly = AppDomain.CurrentDomain.GetAssemblies().SingleOrDefaultF(assembly => assembly.GetName().Name == "ContentPatcherAnimations");
  2.     private static bool IsFromContentPatcherAnimations() {
  3.         if (CpaAssembly is null) {
  4.             return false;
  5.         }
  6.  
  7.         var stackTrace = new StackTrace(skipFrames: 2, fNeedFileInfo: false);
  8.         foreach (var frame in stackTrace.GetFrames()) {
  9.             if (frame.GetMethod() is { } method && method.DeclaringType?.Assembly == CpaAssembly) {
  10.                 return true;
  11.             }
  12.         }
  13.  
  14.         return false;
  15.     }
  16.  
  17.     [Harmonize("PlatformSetData", Fixation.Prefix, PriorityLevel.Last, Generic.Struct, platform: Platform.MonoGame)]
  18.     public static bool OnPlatformSetDataPre<T>(XTexture2D __instance, int level, T[] data, int startIndex, int elementCount) where T : unmanaged {
  19.         XRectangle rect = __instance.Bounds();
  20.         return OnPlatformSetDataPre(__instance, level, 0, ref rect, data, startIndex, elementCount);
  21.     }
  22.  
  23.     [Harmonize("PlatformSetData", Fixation.Prefix, PriorityLevel.Last, Generic.Struct, platform: Platform.MonoGame)]
  24.     public static bool OnPlatformSetDataPre<T>(XTexture2D __instance, int level, int arraySlice, ref XRectangle rect, T[] data, int startIndex, int elementCount) where T : unmanaged {
  25.         var rectVal = rect;
  26.  
  27.         unsafe bool TryInternal() {
  28.             if (arraySlice == 0) {
  29.                 fixed (T* dataPtr = data) {
  30.                     ReadOnlyPinnedSpan<T> span = new(data, dataPtr + startIndex, elementCount);
  31.                     if (GL.Texture2DExt.SetDataInternal(__instance, level, rectVal, span)) {
  32.                         return true;
  33.                     }
  34.                 }
  35.             }
  36.  
  37.             return false;
  38.         }
  39.  
  40.         if (__instance is (ManagedTexture2D or InternalTexture2D)) {
  41.             return !TryInternal();
  42.         }
  43.  
  44.         if (__instance.Format.IsBlock()) {
  45.             __instance.Meta().IncrementRevision();
  46.             return !TryInternal();
  47.         }
  48.  
  49.         try {
  50.             if (!CheckIsDataChanged(__instance, level, arraySlice, rect, data, startIndex, elementCount)) {
  51.                 Debug.Trace(
  52.                     $"SetData for '{__instance.NormalizedName()}' skipped; data unchanged ({rect})".Colorized(
  53.                         DrawingColor.LightGreen
  54.                     )
  55.                 );
  56.                 return false;
  57.             }
  58.         }
  59.         catch (Exception ex) {
  60.             Debug.Warning($"Exception while processing SetData for '{__instance.NormalizedName()}'", ex);
  61.         }
  62.  
  63.         __instance.Meta().IncrementRevision();
  64.         return !TryInternal();
  65.     }
  66.  
  67.     [Harmonize("PlatformSetData", Fixation.Postfix, PriorityLevel.Last, Generic.Struct, platform: Platform.MonoGame)]
  68.     public static void OnPlatformSetDataPost<T>(XTexture2D __instance, int level, T[] data, int startIndex, int elementCount) where T : unmanaged {
  69.         OnPlatformSetDataPost(__instance, level, 0, __instance.Bounds(), data, startIndex, elementCount);
  70.     }
  71.  
  72.     [Harmonize("PlatformSetData", Fixation.Postfix, PriorityLevel.Last, Generic.Struct, platform: Platform.MonoGame)]
  73.     public static void OnPlatformSetDataPost<T>(XTexture2D __instance, int level, int arraySlice, XRectangle rect, T[] data, int startIndex, int elementCount) where T : unmanaged {
  74.         if (__instance is (ManagedTexture2D or InternalTexture2D)) {
  75.             return;
  76.         }
  77.  
  78.         SetDataPurge(
  79.             __instance,
  80.             rect,
  81.             data,
  82.             startIndex,
  83.             elementCount,
  84.             animated: IsFromContentPatcherAnimations()
  85.         );
  86.     }
Advertisement
Add Comment
Please, Sign In to add comment