API reference · 日本語API索引

MotionDebugger.cs

Runtime/Diagnostics/MotionDebugger.cs

1 using System;
2 using System.Collections;
3 using System.Collections.Generic;
4 using System.Diagnostics;
5 using MeatMotion.Adapters;
6 
7 namespace MeatMotion
8 {
9 #if UNITY_6000_7_OR_NEWER
10     [Unity.Scripting.LifecycleManagement.NoAutoStaticsCleanup] // The flags are settings. Tracked motions leave when their world is reset.
11 #endif
12     public static class MotionDebugger
13     {
14         public static bool Enabled;
15         public static bool EnableStackTrace;
16         static readonly TrackingList tracked = new TrackingList();
17         public static IReadOnlyList<TrackingState> Items => tracked;
18 
19         internal static MotionHandle Scheduled(MotionNode node, IMotionScheduler scheduler)
20         {
21             var handle = new MotionHandle(node);
22             if (Enabled) AddTracking(handle, scheduler);
23             return handle;
24         }
25 
26         public static void AddTracking(MotionHandle motionHandle, IMotionScheduler scheduler, int skipFrames = 3)
27         {
28             var node = motionHandle.RequireValid();
29             if (!node.Active) throw new InvalidOperationException("Cannot track an inactive motion.");
30             if (node.TrackingIndex >= 0) return;
31             tracked.Add(node, scheduler, EnableStackTrace ? new StackTrace(skipFrames, true) : null);
32         }
33 
34         public static void Clear() => tracked.Clear();
35 
36         internal static void Detach(MotionNode node) => tracked.Remove(node);
37 
38         // A record only needs managed callbacks and type metadata once a caller inspects it.
39         // Capture the stack and the Editor flag at creation, even when inspection happens later.
40         sealed class TrackingList : IReadOnlyList<TrackingState>
41         {
42             struct Entry
43             {
44                 internal MotionNode Node;
45                 internal IMotionScheduler Scheduler;
46                 internal StackTrace Trace;
47                 internal bool CreatedOnEditor;
48             }
49 
50             readonly List<Entry> slots = new List<Entry>();
51             public int Count { get; private set; }
52 
53             public TrackingState this[int index]
54             {
55                 get
56                 {
57                     Compact();
58                     var entry = slots[index];
59                     var node = entry.Node;
60                     if (node.Tracking != null) return node.Tracking;
61                     var state = TrackingState.Create();
62                     if (node.Data.State is ValueTrack track) track.Describe(state);
63                     else if (node.Children != null) state.SetTypes<double, NoOptions, DoubleMotionAdapter>();
64                     else state.SetTypes<float, NoOptions, FloatMotionAdapter>();
65                     state.Handle = new MotionHandle(node);
66                     state.Scheduler = entry.Scheduler;
67                     state.StackTrace = entry.Trace;
68                     state.CreatedOnEditor = entry.CreatedOnEditor;
69                     state.OriginalOnCompleteCallback = node.OnComplete;
70                     state.OriginalOnCancelCallback = node.OnCancel;
71                     state.Owner = node;
72                     return node.Tracking = state;
73                 }
74             }
75 
76             internal void Add(MotionNode node, IMotionScheduler scheduler, StackTrace trace)
77             {
78                 if (slots.Count > 32 && slots.Count - Count > Count) Compact();
79                 var entry = new Entry { Node = node, Scheduler = scheduler, Trace = trace };
80 #if UNITY_EDITOR
81                 entry.CreatedOnEditor = UnityEditor.EditorApplication.isPlaying;
82 #else
83                 entry.CreatedOnEditor = false;
84 #endif
85                 node.TrackingIndex = slots.Count;
86                 slots.Add(entry);
87                 Count++;
88             }
89 
90             internal void Remove(MotionNode node)
91             {
92                 var state = node.Tracking;
93                 if (state != null)
94                 {
95                     node.OnComplete = state.OriginalOnCompleteCallback;
96                     node.OnCancel = state.OriginalOnCancelCallback;
97                     node.Tracking = null;
98                     state.Owner = null;
99                 }
100                 slots[node.TrackingIndex] = default;
101                 node.TrackingIndex = -1;
102                 Count--;
103                 if (Count == 0) slots.Clear();
104             }
105 
106             internal void Clear()
107             {
108                 for (int i = slots.Count - 1; i >= 0 && Count > 0; i--)
109                 {
110                     var node = slots[i].Node;
111                     if (node != null) Remove(node);
112                 }
113             }
114 
115             void Compact()
116             {
117                 if (Count == slots.Count) return;
118                 int write = 0;
119                 for (int i = 0; i < slots.Count; i++)
120                 {
121                     var entry = slots[i];
122                     if (entry.Node == null) continue;
123                     entry.Node.TrackingIndex = write;
124                     slots[write++] = entry;
125                 }
126                 slots.RemoveRange(write, slots.Count - write);
127             }
128 
129             public IEnumerator<TrackingState> GetEnumerator()
130             {
131                 for (int i = 0; i < Count; i++) yield return this[i];
132             }
133 
134             IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
135         }
136 
137         public sealed class TrackingState
138         {
139             internal MotionNode Owner;
140             public Type ValueType, OptionsType, AdapterType;
141             public IMotionScheduler Scheduler;
142             public MotionHandle Handle;
143             public StackTrace StackTrace;
144             public bool CreatedOnEditor;
145             public Action OriginalOnCompleteCallback, OriginalOnCancelCallback;
146             public readonly Action OnCompleteDelegate, OnCancelDelegate;
147 
148             TrackingState()
149             {
150                 OnCompleteDelegate = () =>
151                 {
152                     var owner = Owner;
153                     ManagedMotionData.Invoke(OriginalOnCompleteCallback);
154                     if (owner != null && Owner == owner && !owner.Preserved) Detach(owner);
155                 };
156                 OnCancelDelegate = () =>
157                 {
158                     var callback = OriginalOnCancelCallback;
159                     if (Owner != null) Detach(Owner);
160                     ManagedMotionData.Invoke(callback);
161                 };
162             }
163 
164             public static TrackingState Create() => new TrackingState();
165 
166             internal void SetTypes<TValue, TOptions, TAdapter>()
167             {
168                 ValueType = typeof(TValue);
169                 OptionsType = typeof(TOptions);
170                 AdapterType = typeof(TAdapter);
171             }
172 
173         }
174     }
175 }