< Summary

Information
Line coverage
100%
Covered lines: 51
Uncovered lines: 0
Coverable lines: 51
Total lines: 211
Line coverage: 100%
Branch coverage
N/A
Covered branches: 0
Total branches: 0
Branch coverage: N/A
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
File 1: GetViewHashCode()0%110100%
File 2: ScriptableEvent()0%110100%
File 2: Invoke(...)0%110100%
File 2: Invoke(...)0%110100%
File 2: InvokeInternal(...)0%330100%
File 2: OnBeforeInvoked(...)0%110100%
File 2: OnAfterInvoked(...)0%110100%
File 2: OnPreStart()0%110100%
File 2: WarnIfLeftOverSubscribers()0%110100%
File 2: ClearSubscribers(...)0%220100%
File 2: OnDisabled()0%110100%

File(s)

./Packages/se.hertzole.scriptable-values/Runtime/Events/ScriptableEvent.Generic.Bindings.cs

#LineLine coverage
 1#if SCRIPTABLE_VALUES_RUNTIME_BINDING && SCRIPTABLE_VALUES_UITOOLKIT
 2#nullable enable
 3
 4using System.Collections.Generic;
 5
 6namespace Hertzole.ScriptableValues
 7{
 8    partial class ScriptableEvent<T>
 9    {
 10        /// <inheritdoc />
 11        protected override long GetViewHashCode()
 20812        {
 13            unchecked
 20814            {
 20815                long hash = 17;
 20816                hash = hash * 23 + base.GetViewHashCode();
 20817                hash = hash * 23 + EqualityComparer<T?>.Default.GetHashCode(PreviousArgs);
 20818                return hash;
 19            }
 20820        }
 21    }
 22}
 23#endif

./Packages/se.hertzole.scriptable-values/Runtime/Events/ScriptableEvent.Generic.cs

#LineLine coverage
 1#nullable enable
 2
 3using System;
 4using Hertzole.ScriptableValues.Helpers;
 5using UnityEngine;
 6using UnityEngine.Events;
 7using UnityEngine.Serialization;
 8#if SCRIPTABLE_VALUES_PROPERTIES
 9using Unity.Properties;
 10#endif
 11
 12namespace Hertzole.ScriptableValues
 13{
 14    /// <summary>
 15    ///     A <see cref="ScriptableObject" /> that can be invoked to trigger an event with an argument.
 16    /// </summary>
 17    /// <typeparam name="T">The type of the argument.</typeparam>
 18#if UNITY_EDITOR
 19    [HelpURL(Documentation.SCRIPTABLE_EVENT_GENERIC_URL)]
 20#endif
 21    public abstract partial class ScriptableEvent<T> : RuntimeScriptableObject
 22    {
 23        [FormerlySerializedAs("onInvokedWithArgs")]
 24        [SerializeField]
 25        [EditorTooltip("Called when the event is invoked.")]
 26#if SCRIPTABLE_VALUES_PROPERTIES
 27        [DontCreateProperty]
 28#endif
 879829        internal UnityEvent<T> onInvoked = new UnityEvent<T>();
 30
 31#if UNITY_EDITOR // This does not need to be included in the build as it's only used in the editor.
 32#pragma warning disable CS0414 // The field is assigned but its value is never used
 33        /// <summary>
 34        ///     Used in the editor to invoke the event with an argument from the editor.
 35        /// </summary>
 36        [SerializeField]
 37#if SCRIPTABLE_VALUES_PROPERTIES
 38        [DontCreateProperty]
 39#endif
 879840        internal T? editorInvokeValue = default;
 41#pragma warning restore CS0414 // The field is assigned but its value is never used
 42#endif
 43
 44        // The arguments that was passed to the event when it was invoked.
 45        // This is used to set the previous value.
 46        private T? currentArgs;
 47        private T? previousArgs;
 48
 49        /// <summary>
 50        ///     The arguments that was passed to the event when it was invoked.
 51        /// </summary>
 52#if SCRIPTABLE_VALUES_PROPERTIES
 53#if SCRIPTABLE_VALUES_PROPERTIES_SUPPORTS_READONLY
 54        [CreateProperty(ReadOnly = true)]
 55#else
 56        [CreateProperty]
 57#endif // SCRIPTABLE_VALUES_PROPERTIES_SUPPORTS_READONLY
 58#endif // SCRIPTABLE_VALUES_PROPERTIES
 59        public T? PreviousArgs
 60        {
 1940461            get { return previousArgs; }
 5335262            private set { SetField(ref previousArgs, value, ScriptableEvent.previousArgsChanging, ScriptableEvent.previo
 63        }
 64
 65#if UNITY_INCLUDE_TESTS
 66        internal bool InvokedHasSubscribers
 67        {
 468068            get { return OnInvoked != null; }
 69        }
 70#endif
 71
 72#if UNITY_EDITOR
 73        // Used for the CreateAssetMenu attribute order.
 74        internal const int ORDER = ScriptableEvent.ORDER + 1;
 75#endif
 76
 77        /// <summary>
 78        ///     Called when the event is invoked.
 79        /// </summary>
 80        public event EventHandler<T>? OnInvoked;
 81
 82        /// <summary>
 83        ///     Invokes the event with the specified argument and this <see cref="ScriptableEvent{T}" /> as the sender.
 84        /// </summary>
 85        /// <param name="args">The argument to send with the event.</param>
 86        public void Invoke(T args)
 236487        {
 236488            InvokeInternal(this, args, 1);
 236489        }
 90
 91        /// <summary>
 92        ///     Invokes the event with the specified sender and argument.
 93        /// </summary>
 94        /// <param name="sender">The object that invoked the event.</param>
 95        /// <param name="args">The argument to send with the event.</param>
 96        public void Invoke(object sender, T args)
 714097        {
 714098            InvokeInternal(sender, args, 1);
 714099        }
 100
 101        /// <summary>
 102        ///     Invokes the event with the specified sender and argument.
 103        /// </summary>
 104        /// <param name="sender">The object that invoked the event.</param>
 105        /// <param name="args">The argument to send with the event.</param>
 106        /// <param name="skipFrames">How many frames of stack trace to skip.</param>
 107        private void InvokeInternal(object sender, T args, int skipFrames)
 9504108        {
 9504109            if (!OnBeforeInvoked(sender, args))
 156110            {
 156111                return;
 112            }
 113
 114            // Skip at least one frame to avoid the Invoke method itself being included in the stack trace.
 9348115            AddStackTrace(1 + skipFrames);
 116
 9348117            PreviousArgs = currentArgs;
 9348118            currentArgs = args;
 119
 9348120            OnInvoked?.Invoke(sender, args);
 9348121            onInvoked.Invoke(args);
 122
 9348123            OnAfterInvoked(sender, args);
 9504124        }
 125
 126        /// <summary>
 127        ///     Called before the event is invoked. This can be used to prevent the event from being invoked.
 128        /// </summary>
 129        /// <param name="sender">The object that invoked the event.</param>
 130        /// <param name="args">The argument that was passed to the event.</param>
 131        /// <returns><c>true</c> if the event should be invoked; otherwise, <c>false</c>.</returns>
 132        protected virtual bool OnBeforeInvoked(object sender, T args)
 9036133        {
 9036134            return true;
 9036135        }
 136
 137        /// <summary>
 138        ///     Called after the event has been invoked.
 139        /// </summary>
 140        /// <param name="sender">The object that invoked the event.</param>
 141        /// <param name="args">The argument that was passed to the event.</param>
 18072142        protected virtual void OnAfterInvoked(object sender, T args) { }
 143
 144        /// <inheritdoc />
 145        protected override void OnPreStart()
 8228146        {
 147            // Remove any subscribers that are left over from play mode.
 148            // Don't warn if there are any subscribers left over because we already do that in OnExitPlayMode.
 8228149            ClearSubscribers();
 8228150            PreviousArgs = default;
 8228151        }
 152
 153        /// <summary>
 154        ///     Warns if there are any left-over subscribers to the event.
 155        /// </summary>
 156        /// <remarks>This will only be called in the Unity editor and builds with the DEBUG flag.</remarks>
 157        protected override void WarnIfLeftOverSubscribers()
 8306158        {
 8306159            base.WarnIfLeftOverSubscribers();
 8306160            EventHelper.WarnIfLeftOverSubscribers(OnInvoked, nameof(OnInvoked), this);
 8306161        }
 162
 163        /// <summary>
 164        ///     Removes any subscribers from the event.
 165        /// </summary>
 166        /// <param name="warnIfLeftOver">
 167        ///     If <c>true</c>, a warning will be printed in the console if there are any subscribers.
 168        ///     The warning will only be printed in the editor and debug builds.
 169        /// </param>
 170        public void ClearSubscribers(bool warnIfLeftOver = false)
 8234171        {
 172#if DEBUG
 8234173            if (warnIfLeftOver)
 6174            {
 6175                WarnIfLeftOverSubscribers();
 6176            }
 177#endif
 178
 8234179            OnInvoked = null;
 8234180        }
 181
 182        /// <inheritdoc />
 183        protected override void OnDisabled()
 8300184        {
 8300185            WarnIfLeftOverSubscribers();
 8300186        }
 187    }
 188}