Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private static readonly Assembly? CpaAssembly = AppDomain.CurrentDomain.GetAssemblies().SingleOrDefaultF(assembly => assembly.GetName().Name == "ContentPatcherAnimations");
- private static bool IsFromContentPatcherAnimations() {
- if (CpaAssembly is null) {
- return false;
- }
- var stackTrace = new StackTrace(skipFrames: 2, fNeedFileInfo: false);
- foreach (var frame in stackTrace.GetFrames()) {
- if (frame.GetMethod() is { } method && method.DeclaringType?.Assembly == CpaAssembly) {
- return true;
- }
- }
- return false;
- }
- [Harmonize("PlatformSetData", Fixation.Prefix, PriorityLevel.Last, Generic.Struct, platform: Platform.MonoGame)]
- public static bool OnPlatformSetDataPre<T>(XTexture2D __instance, int level, T[] data, int startIndex, int elementCount) where T : unmanaged {
- XRectangle rect = __instance.Bounds();
- return OnPlatformSetDataPre(__instance, level, 0, ref rect, data, startIndex, elementCount);
- }
- [Harmonize("PlatformSetData", Fixation.Prefix, PriorityLevel.Last, Generic.Struct, platform: Platform.MonoGame)]
- public static bool OnPlatformSetDataPre<T>(XTexture2D __instance, int level, int arraySlice, ref XRectangle rect, T[] data, int startIndex, int elementCount) where T : unmanaged {
- var rectVal = rect;
- unsafe bool TryInternal() {
- if (arraySlice == 0) {
- fixed (T* dataPtr = data) {
- ReadOnlyPinnedSpan<T> span = new(data, dataPtr + startIndex, elementCount);
- if (GL.Texture2DExt.SetDataInternal(__instance, level, rectVal, span)) {
- return true;
- }
- }
- }
- return false;
- }
- if (__instance is (ManagedTexture2D or InternalTexture2D)) {
- return !TryInternal();
- }
- if (__instance.Format.IsBlock()) {
- __instance.Meta().IncrementRevision();
- return !TryInternal();
- }
- try {
- if (!CheckIsDataChanged(__instance, level, arraySlice, rect, data, startIndex, elementCount)) {
- Debug.Trace(
- $"SetData for '{__instance.NormalizedName()}' skipped; data unchanged ({rect})".Colorized(
- DrawingColor.LightGreen
- )
- );
- return false;
- }
- }
- catch (Exception ex) {
- Debug.Warning($"Exception while processing SetData for '{__instance.NormalizedName()}'", ex);
- }
- __instance.Meta().IncrementRevision();
- return !TryInternal();
- }
- [Harmonize("PlatformSetData", Fixation.Postfix, PriorityLevel.Last, Generic.Struct, platform: Platform.MonoGame)]
- public static void OnPlatformSetDataPost<T>(XTexture2D __instance, int level, T[] data, int startIndex, int elementCount) where T : unmanaged {
- OnPlatformSetDataPost(__instance, level, 0, __instance.Bounds(), data, startIndex, elementCount);
- }
- [Harmonize("PlatformSetData", Fixation.Postfix, PriorityLevel.Last, Generic.Struct, platform: Platform.MonoGame)]
- public static void OnPlatformSetDataPost<T>(XTexture2D __instance, int level, int arraySlice, XRectangle rect, T[] data, int startIndex, int elementCount) where T : unmanaged {
- if (__instance is (ManagedTexture2D or InternalTexture2D)) {
- return;
- }
- SetDataPurge(
- __instance,
- rect,
- data,
- startIndex,
- elementCount,
- animated: IsFromContentPatcherAnimations()
- );
- }
Advertisement
Add Comment
Please, Sign In to add comment