SolidAlloy / ExtEvents

A better replacement for UnityEvents
Other
128 stars 13 forks source link

Missing 4 Parameter ExtEvent #9

Open fnnbrr opened 1 year ago

fnnbrr commented 1 year ago

Hey there, I'm looking to replace UnityEvents in my project with ExtEvents but there doesn't seem to be an ExtEvent<T1, T2, T3, T4> available to replace UnityEvent<T0, T1, T2, T3> which I'm currently using.

I imagine it would look like this (just a modification of ExtEvent<T1, T2, T3>), let me know if you'd like a PR:

namespace ExtEvents
{
    using System;
    using System.Runtime.CompilerServices;
    using JetBrains.Annotations;

    [Serializable]
    public class ExtEvent<T1, T2, T3, T4> : BaseExtEvent
    {
        private readonly unsafe void*[] _arguments = new void*[4];

        private Type[] _eventParamTypes;
        protected override Type[] EventParamTypes => _eventParamTypes ??= new Type[] { typeof(T1), typeof(T2), typeof(T3), typeof(T4) };

        /// <summary>
        /// The dynamic listeners list that you can add your listener to.
        /// </summary>
        [PublicAPI]
        public event Action<T1, T2, T3, T4> DynamicListeners;
        internal override Delegate _dynamicListeners => DynamicListeners;

        /// <summary>
        /// Invokes all listeners of the event.
        /// </summary>
        [PublicAPI]
        public void Invoke(T1 arg1, T2 arg2, T3 arg3, T4 arg4)
        {
            unsafe
            {
                _arguments[0] = Unsafe.AsPointer(ref arg1);
                _arguments[1] = Unsafe.AsPointer(ref arg2);
                _arguments[2] = Unsafe.AsPointer(ref arg3);
                _arguments[3] = Unsafe.AsPointer(ref arg4);

                // ReSharper disable once ForCanBeConvertedToForeach
                for (int index = 0; index < _persistentListeners.Length; index++)
                {
                    _persistentListeners[index].Invoke(_arguments);
                }
            }

            DynamicListeners?.Invoke(arg1, arg2, arg3, arg4);
        }

        public static ExtEvent<T1, T2, T3, T4> operator +(ExtEvent<T1, T2, T3, T4> extEvent, Action<T1, T2, T3, T4> listener)
        {
            if (extEvent == null)
                return null;

            extEvent.DynamicListeners += listener;
            return extEvent;
        }

        public static ExtEvent<T1, T2, T3, T4> operator -(ExtEvent<T1, T2, T3, T4> extEvent, Action<T1, T2, T3, T4> listener)
        {
            if (extEvent == null)
                return null;

            extEvent.DynamicListeners -= listener;
            return extEvent;
        }
    }
}
fnnbrr commented 1 year ago

For anyone else who also needs this functionality now, these steps worked for me to get ExtEvent<T1, T2, T3, T4> working:

  1. Make a new folder where you'll place your code
  2. In that folder, add an Assembly Definition Reference that references the ExtEvents Assembly Definition
  3. Create a file named ExtEvent`4.cs and paste in the code from my post above

Works as expected with version 1.7.0.