|
| 1 | +using System.Collections; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.IO; |
| 4 | +using System.Linq; |
| 5 | +using System.Reflection; |
| 6 | +using TransformChangesDebugger.API; |
| 7 | +using TransformChangesDebugger.API.Patches; |
| 8 | +using UnityEngine; |
| 9 | + |
| 10 | +public class HardToPinpointJitterDebugger : MonoBehaviour |
| 11 | +{ |
| 12 | + [SerializeField] private TrackTransformChanges TrackingObject; |
| 13 | + |
| 14 | + [Header("Before and after approach")] |
| 15 | + [SerializeField] private float WaitForNSecondsBeforePrintingInformation = 1f; |
| 16 | + [SerializeField] private float DistanceThresholdBetweenNewAndOldValue = 3f; |
| 17 | + [SerializeField] private int FrameCountToReviewOnCheck = 50; |
| 18 | + |
| 19 | + [Header("Review Modifiers approach")] |
| 20 | + [SerializeField] private float PrintModifiersEveryNSeconds = 5f; |
| 21 | + |
| 22 | + [Header("Disable changes done by modifiers")] [SerializeField] |
| 23 | + private bool DisableChangesDoneByModifiers = false; |
| 24 | + [SerializeField] private string DisableChangesDoneByModifiersContaining = "FrustratingDisruptor"; |
| 25 | + |
| 26 | + |
| 27 | + void Awake() |
| 28 | + { |
| 29 | + //Tracking needs to be initialized, best done before other scripts had a chance to run |
| 30 | + InitializeTracking(); |
| 31 | + |
| 32 | + //1ST SCENARIO: |
| 33 | + //we know that transform jitters in some frame, every n seconds we're going to review last FrameCountToReviewOnCheck to see if any change causes vectors to get further than DistanceThresholdBetweenNewAndOldValue |
| 34 | + //reason behind being valid changes should create circular motion, jitter is likely causing big jump |
| 35 | + StartCoroutine(TryToCaptureSuspiciousChangeBasedOnBeforeAndAfterChangeDistanceDifference()); |
| 36 | + |
| 37 | + //2ND SCENARIO: |
| 38 | + //in this scenario we'd like to understand which object is changing ours, this will be then used to find code / objects and review what it is doing |
| 39 | + //reason being you're quite familiar with what should happen to the object and quite likely will be able to quickly spot something odd making changes - just need to make sure that's visible to you |
| 40 | + StartCoroutine(TryToCaptureSuspiciousChangeBasedOnModifiersMakingChanges()); |
| 41 | + |
| 42 | + //3RD SCENARIO |
| 43 | + //by that stage you already know 'FrustratingDisruptor' is making some odd changes that are likely causing the issue, just to confirm |
| 44 | + //you'll turn that change off at runtime to see if issue is gone (just tick DisableChangesDoneByModifiers in editor) |
| 45 | + SkipSpecificChangesIfEnabled(); |
| 46 | + } |
| 47 | + |
| 48 | + static void InitializeTracking() |
| 49 | + { |
| 50 | + var userCodeAssembly = new FileInfo(Assembly.GetExecutingAssembly().Location); |
| 51 | + var thirdPartyToolAssembly = new FileInfo(userCodeAssembly.Directory.FullName + "/ThirdPartyTool.dll"); |
| 52 | + |
| 53 | + var userChosenAssembliesToPatch = new List<FileInfo>() { userCodeAssembly }; //you can specify which assemblies are patched (to save time) |
| 54 | + var allAvailableAssembliesToPatch = new List<FileInfo> { userCodeAssembly, thirdPartyToolAssembly }; //and you also specify all available assemblies, this is mainly for performance-statistics that GUI can use |
| 55 | + |
| 56 | + TransformChangesDebuggerManager.IsTrackingEnabled = true; |
| 57 | + TransformChangesDebuggerManager.Initialize(allAvailableAssembliesToPatch, userChosenAssembliesToPatch); |
| 58 | + } |
| 59 | + |
| 60 | + IEnumerator TryToCaptureSuspiciousChangeBasedOnBeforeAndAfterChangeDistanceDifference() |
| 61 | + { |
| 62 | + while (true) |
| 63 | + { |
| 64 | + yield return new WaitForSeconds(WaitForNSecondsBeforePrintingInformation); |
| 65 | + |
| 66 | + for (var frameIndexAdjustment = 0; frameIndexAdjustment < FrameCountToReviewOnCheck; frameIndexAdjustment++) // we're going to check changes for last FrameCountToReviewOnCheck frames that had changes captured |
| 67 | + { |
| 68 | + //let's get changes that were captured for object |
| 69 | + var newestChanges = TransformChangesTracker.GetFrameChangesForTrackedObject( |
| 70 | + TransformChangesTracker.GetNewestFrameNumberWithTrackedChanges(frameIndexAdjustment), //resolves latest frame index with changes |
| 71 | + TrackingObject |
| 72 | + ); |
| 73 | + |
| 74 | + //check change before and after vales for distance over threshold |
| 75 | + var changeWhichCreatesDistanceOverThreshold = newestChanges.FirstOrDefault(c => Vector3.Distance((Vector3) c.NewValue, (Vector3) c.ValueBeforeChange) > DistanceThresholdBetweenNewAndOldValue); |
| 76 | + if (changeWhichCreatesDistanceOverThreshold != null) |
| 77 | + { |
| 78 | + //you can put a debugger breakpoint here to inspect further, you should quickly find FrustratingDisruptor is causing issues |
| 79 | + Debug.Log($"Following change has moved transform suspiciously (CallingObjectName: {changeWhichCreatesDistanceOverThreshold.CallingObject.name}, " + |
| 80 | + $"NewValue: {changeWhichCreatesDistanceOverThreshold.NewValue}, " + |
| 81 | + $"ValueBeforeChange: {changeWhichCreatesDistanceOverThreshold.ValueBeforeChange})", |
| 82 | + changeWhichCreatesDistanceOverThreshold.CallingObject |
| 83 | + ); |
| 84 | + } |
| 85 | + } |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + IEnumerator TryToCaptureSuspiciousChangeBasedOnModifiersMakingChanges() |
| 90 | + { |
| 91 | + while (true) |
| 92 | + { |
| 93 | + yield return new WaitForSeconds(PrintModifiersEveryNSeconds); |
| 94 | + |
| 95 | + // get modifiers that changed tracked object (in any captured frame) |
| 96 | + var modifiers = TransformChangesTracker.GetModifiers(TrackingObject); |
| 97 | + Debug.Log($"Following modifiers changed: {TrackingObject.name}"); |
| 98 | + for (var i = 0; i < modifiers.Count; i++) |
| 99 | + { |
| 100 | + var modifier = modifiers[i]; |
| 101 | + Debug.Log($"Modifier ({i}) {modifier.CallingObjectFullPath}: via method {modifier.CallingFromMethodName}", modifier.CallingObject); |
| 102 | + } |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + void SkipSpecificChangesIfEnabled() |
| 107 | + { |
| 108 | + //set up predicate that'll look for changes originating from object containing 'DisableChangesDoneByModifiersContaining' |
| 109 | + TransformChangesDebuggerManager.SkipTransformChangesFor((IlWeavedValuesArray ilWeavedValuesArray, Component changingComponent) => |
| 110 | + { |
| 111 | + if (!DisableChangesDoneByModifiers) return false; |
| 112 | + |
| 113 | + if (changingComponent.transform == TrackingObject.transform) |
| 114 | + { |
| 115 | + if (ilWeavedValuesArray.CallingObject.name.Contains(DisableChangesDoneByModifiersContaining)) |
| 116 | + { |
| 117 | + return true; //skip change execution |
| 118 | + //actual change entry will still be created with WasChangeSkipped = true |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + return false; |
| 123 | + }); |
| 124 | + |
| 125 | + //you can also set up SkipTransformChangesFor and pass specific TransformModifier |
| 126 | + } |
| 127 | +} |
0 commit comments