DelegateHelpers.cs 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. using System;
  2. using UnityEngine.Profiling;
  3. namespace UnityEngine.InputSystem.Utilities
  4. {
  5. internal static class DelegateHelpers
  6. {
  7. // InvokeCallbacksSafe protects both against the callback getting removed while being called
  8. // and against exceptions being thrown by the callback.
  9. public static void InvokeCallbacksSafe(ref InlinedArray<Action> callbacks, string callbackName, object context = null)
  10. {
  11. if (callbacks.length == 0)
  12. return;
  13. Profiler.BeginSample(callbackName);
  14. for (var i = 0; i < callbacks.length; ++i)
  15. {
  16. var lengthBefore = callbacks.length;
  17. try
  18. {
  19. callbacks[i]();
  20. }
  21. catch (Exception exception)
  22. {
  23. if (context != null)
  24. Debug.LogError($"{exception.GetType().Name} while executing '{callbackName}' callbacks of '{context}'");
  25. else
  26. Debug.LogError($"{exception.GetType().Name} while executing '{callbackName}' callbacks");
  27. Debug.LogException(exception);
  28. }
  29. ////REVIEW: is this enough?
  30. if (callbacks.length == lengthBefore - 1)
  31. --i;
  32. }
  33. Profiler.EndSample();
  34. }
  35. public static void InvokeCallbacksSafe<TValue>(ref InlinedArray<Action<TValue>> callbacks, TValue argument, string callbackName, object context = null)
  36. {
  37. if (callbacks.length == 0)
  38. return;
  39. Profiler.BeginSample(callbackName);
  40. for (var i = 0; i < callbacks.length; ++i)
  41. {
  42. var lengthBefore = callbacks.length;
  43. try
  44. {
  45. callbacks[i](argument);
  46. }
  47. catch (Exception exception)
  48. {
  49. if (context != null)
  50. Debug.LogError($"{exception.GetType().Name} while executing '{callbackName}' callbacks of '{context}'");
  51. else
  52. Debug.LogError($"{exception.GetType().Name} while executing '{callbackName}' callbacks");
  53. Debug.LogException(exception);
  54. }
  55. ////REVIEW: is this enough?
  56. if (callbacks.length == lengthBefore - 1)
  57. --i;
  58. }
  59. Profiler.EndSample();
  60. }
  61. public static void InvokeCallbacksSafe<TValue1, TValue2>(ref InlinedArray<Action<TValue1, TValue2>> callbacks, TValue1 argument1, TValue2 argument2, string callbackName, object context = null)
  62. {
  63. if (callbacks.length == 0)
  64. return;
  65. Profiler.BeginSample(callbackName);
  66. for (var i = 0; i < callbacks.length; ++i)
  67. {
  68. var lengthBefore = callbacks.length;
  69. try
  70. {
  71. callbacks[i](argument1, argument2);
  72. }
  73. catch (Exception exception)
  74. {
  75. if (context != null)
  76. Debug.LogError($"{exception.GetType().Name} while executing '{callbackName}' callbacks of '{context}'");
  77. else
  78. Debug.LogError($"{exception.GetType().Name} while executing '{callbackName}' callbacks");
  79. Debug.LogException(exception);
  80. }
  81. ////REVIEW: is this enough?
  82. if (callbacks.length == lengthBefore - 1)
  83. --i;
  84. }
  85. Profiler.EndSample();
  86. }
  87. }
  88. }