| | | 1 | | #nullable enable |
| | | 2 | | |
| | | 3 | | using System; |
| | | 4 | | using System.Diagnostics; |
| | | 5 | | using Object = UnityEngine.Object; |
| | | 6 | | #if DEBUG |
| | | 7 | | using System.Reflection; |
| | | 8 | | using System.Text; |
| | | 9 | | using Debug = UnityEngine.Debug; |
| | | 10 | | #endif |
| | | 11 | | |
| | | 12 | | namespace Hertzole.ScriptableValues.Helpers |
| | | 13 | | { |
| | | 14 | | /// <summary> |
| | | 15 | | /// Debug only helper methods for events. |
| | | 16 | | /// </summary> |
| | | 17 | | public static class EventHelper |
| | | 18 | | { |
| | | 19 | | /// <summary> |
| | | 20 | | /// Helper method that warns if the given delegate has any subscribers left. |
| | | 21 | | /// <para>This method is removed in non-debug builds, but you don't need to explicitly remove it yourself.</ |
| | | 22 | | /// </summary> |
| | | 23 | | /// <remarks>This method only does something in DEBUG builds.</remarks> |
| | | 24 | | /// <param name="action">The delegate to check.</param> |
| | | 25 | | /// <param name="parameterName">The name of the delegate that will be used in the message.</param> |
| | | 26 | | /// <param name="targetObject">Optional target object to ping when the log is selected.</param> |
| | | 27 | | /// <typeparam name="T">The type of the delegate.</typeparam> |
| | | 28 | | [Conditional("DEBUG")] |
| | | 29 | | public static void WarnIfLeftOverSubscribers<T>(T action, string parameterName, Object? targetObject = null) |
| | 118465 | 30 | | { |
| | | 31 | | #if DEBUG |
| | 118465 | 32 | | if (typeof(T).IsSubclassOf(typeof(Delegate)) && action != null) |
| | 324 | 33 | | { |
| | 324 | 34 | | Delegate del = (Delegate) (object) action; |
| | | 35 | | |
| | 324 | 36 | | CreateWarning(del.GetInvocationList().AsSpan(), parameterName, targetObject); |
| | 324 | 37 | | } |
| | | 38 | | #endif |
| | 118465 | 39 | | } |
| | | 40 | | |
| | | 41 | | #if DEBUG |
| | | 42 | | private static void CreateWarning(ReadOnlySpan<Delegate> delegates, string parameterName, Object? targetObject) |
| | 324 | 43 | | { |
| | 324 | 44 | | StringBuilder sb = new StringBuilder(); |
| | 324 | 45 | | sb.Append($"{parameterName}"); |
| | 324 | 46 | | if (targetObject != null) |
| | 324 | 47 | | { |
| | 324 | 48 | | sb.Append($" in object {targetObject.name} ({targetObject.GetType().FullName})"); |
| | 324 | 49 | | } |
| | | 50 | | |
| | 324 | 51 | | sb.AppendLine(" has some leftover subscribers:"); |
| | 324 | 52 | | WriteDelegates(sb, delegates); |
| | | 53 | | |
| | 324 | 54 | | Debug.LogWarning(sb.ToString(), targetObject); |
| | 324 | 55 | | } |
| | | 56 | | |
| | | 57 | | private static void WriteDelegates(StringBuilder sb, in ReadOnlySpan<Delegate> delegates) |
| | 324 | 58 | | { |
| | 1296 | 59 | | for (int i = 0; i < delegates.Length; i++) |
| | 324 | 60 | | { |
| | 324 | 61 | | sb.Append(delegates[i].Method.DeclaringType); |
| | 324 | 62 | | sb.Append('.'); |
| | 324 | 63 | | sb.Append(delegates[i].Method.Name); |
| | | 64 | | |
| | 324 | 65 | | WriteParameters(sb, delegates[i]); |
| | | 66 | | |
| | 324 | 67 | | if (i < delegates.Length - 1) |
| | 0 | 68 | | { |
| | 0 | 69 | | sb.AppendLine(); |
| | 0 | 70 | | } |
| | 324 | 71 | | } |
| | 324 | 72 | | } |
| | | 73 | | |
| | | 74 | | private static void WriteParameters(StringBuilder sb, Delegate del) |
| | 324 | 75 | | { |
| | 324 | 76 | | ParameterInfo[] parameters = del.Method.GetParameters(); |
| | 324 | 77 | | if (parameters.Length > 0) |
| | 324 | 78 | | { |
| | 324 | 79 | | sb.Append('('); |
| | | 80 | | |
| | 1896 | 81 | | for (int i = 0; i < parameters.Length; i++) |
| | 624 | 82 | | { |
| | 624 | 83 | | sb.Append(parameters[i].Name); |
| | 624 | 84 | | if (i < parameters.Length - 1) |
| | 300 | 85 | | { |
| | 300 | 86 | | sb.Append(", "); |
| | 300 | 87 | | } |
| | 624 | 88 | | } |
| | | 89 | | |
| | 324 | 90 | | sb.Append(')'); |
| | 324 | 91 | | } |
| | 324 | 92 | | } |
| | | 93 | | #endif |
| | | 94 | | } |
| | | 95 | | } |