123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708 |
- using System;
- using System.Collections.Generic;
- using System.Reflection;
- using UnityEngine;
- [Serializable]
- public class EventDelegate
- {
- public EventDelegate()
- {
- }
- public EventDelegate(EventDelegate.Callback call)
- {
- this.Set(call);
- }
- public EventDelegate(MonoBehaviour target, string methodName)
- {
- this.Set(target, methodName);
- }
- public MonoBehaviour target
- {
- get
- {
- return this.mTarget;
- }
- set
- {
- this.mTarget = value;
- this.mCachedCallback = null;
- this.mRawDelegate = false;
- this.mCached = false;
- this.mMethod = null;
- this.mParameterInfos = null;
- this.mParameters = null;
- }
- }
- public string methodName
- {
- get
- {
- return this.mMethodName;
- }
- set
- {
- this.mMethodName = value;
- this.mCachedCallback = null;
- this.mRawDelegate = false;
- this.mCached = false;
- this.mMethod = null;
- this.mParameterInfos = null;
- this.mParameters = null;
- }
- }
- public EventDelegate.Parameter[] parameters
- {
- get
- {
- if (!this.mCached)
- {
- this.Cache();
- }
- return this.mParameters;
- }
- }
- public bool isValid
- {
- get
- {
- if (!this.mCached)
- {
- this.Cache();
- }
- return (this.mRawDelegate && this.mCachedCallback != null) || (this.mTarget != null && !string.IsNullOrEmpty(this.mMethodName));
- }
- }
- public bool isEnabled
- {
- get
- {
- if (!this.mCached)
- {
- this.Cache();
- }
- if (this.mRawDelegate && this.mCachedCallback != null)
- {
- return true;
- }
- if (this.mTarget == null)
- {
- return false;
- }
- MonoBehaviour monoBehaviour = this.mTarget;
- return monoBehaviour == null || monoBehaviour.enabled;
- }
- }
- private static string GetMethodName(EventDelegate.Callback callback)
- {
- return callback.Method.Name;
- }
- private static bool IsValid(EventDelegate.Callback callback)
- {
- return callback != null && callback.Method != null;
- }
- public override bool Equals(object obj)
- {
- if (obj == null)
- {
- return !this.isValid;
- }
- if (obj is EventDelegate.Callback)
- {
- EventDelegate.Callback callback = obj as EventDelegate.Callback;
- if (callback.Equals(this.mCachedCallback))
- {
- return true;
- }
- MonoBehaviour y = callback.Target as MonoBehaviour;
- return this.mTarget == y && string.Equals(this.mMethodName, EventDelegate.GetMethodName(callback));
- }
- else
- {
- if (obj is EventDelegate)
- {
- EventDelegate eventDelegate = obj as EventDelegate;
- return this.mTarget == eventDelegate.mTarget && string.Equals(this.mMethodName, eventDelegate.mMethodName);
- }
- return false;
- }
- }
- public override int GetHashCode()
- {
- return EventDelegate.s_Hash;
- }
- private void Set(EventDelegate.Callback call)
- {
- this.Clear();
- if (call != null && EventDelegate.IsValid(call))
- {
- this.mTarget = (call.Target as MonoBehaviour);
- if (this.mTarget == null)
- {
- this.mRawDelegate = true;
- this.mCachedCallback = call;
- this.mMethodName = null;
- }
- else
- {
- this.mMethodName = EventDelegate.GetMethodName(call);
- this.mRawDelegate = false;
- }
- }
- }
- public void Set(MonoBehaviour target, string methodName)
- {
- this.Clear();
- this.mTarget = target;
- this.mMethodName = methodName;
- }
- private void Cache()
- {
- this.mCached = true;
- if (this.mRawDelegate)
- {
- return;
- }
- if ((this.mCachedCallback == null || this.mCachedCallback.Target as MonoBehaviour != this.mTarget || EventDelegate.GetMethodName(this.mCachedCallback) != this.mMethodName) && this.mTarget != null && !string.IsNullOrEmpty(this.mMethodName))
- {
- Type type = this.mTarget.GetType();
- this.mMethod = null;
- while (type != null)
- {
- try
- {
- this.mMethod = type.GetMethod(this.mMethodName, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
- if (this.mMethod != null)
- {
- break;
- }
- }
- catch (Exception)
- {
- }
- type = type.BaseType;
- }
- if (this.mMethod == null)
- {
- Debug.LogError(string.Concat(new object[]
- {
- "Could not find method '",
- this.mMethodName,
- "' on ",
- this.mTarget.GetType()
- }), this.mTarget);
- return;
- }
- if (this.mMethod.ReturnType != typeof(void))
- {
- Debug.LogError(string.Concat(new object[]
- {
- this.mTarget.GetType(),
- ".",
- this.mMethodName,
- " must have a 'void' return type."
- }), this.mTarget);
- return;
- }
- this.mParameterInfos = this.mMethod.GetParameters();
- if (this.mParameterInfos.Length == 0)
- {
- this.mCachedCallback = (EventDelegate.Callback)Delegate.CreateDelegate(typeof(EventDelegate.Callback), this.mTarget, this.mMethodName);
- this.mArgs = null;
- this.mParameters = null;
- return;
- }
- this.mCachedCallback = null;
- if (this.mParameters == null || this.mParameters.Length != this.mParameterInfos.Length)
- {
- this.mParameters = new EventDelegate.Parameter[this.mParameterInfos.Length];
- int i = 0;
- int num = this.mParameters.Length;
- while (i < num)
- {
- this.mParameters[i] = new EventDelegate.Parameter();
- i++;
- }
- }
- int j = 0;
- int num2 = this.mParameters.Length;
- while (j < num2)
- {
- this.mParameters[j].expectedType = this.mParameterInfos[j].ParameterType;
- j++;
- }
- }
- }
- public bool Execute()
- {
- if (!this.mCached)
- {
- this.Cache();
- }
- if (this.mCachedCallback != null)
- {
- this.mCachedCallback();
- return true;
- }
- if (this.mMethod != null)
- {
- if (this.mParameters == null || this.mParameters.Length == 0)
- {
- this.mMethod.Invoke(this.mTarget, null);
- }
- else
- {
- if (this.mArgs == null || this.mArgs.Length != this.mParameters.Length)
- {
- this.mArgs = new object[this.mParameters.Length];
- }
- int i = 0;
- int num = this.mParameters.Length;
- while (i < num)
- {
- this.mArgs[i] = this.mParameters[i].value;
- i++;
- }
- try
- {
- this.mMethod.Invoke(this.mTarget, this.mArgs);
- }
- catch (ArgumentException ex)
- {
- string text = "Error calling ";
- if (this.mTarget == null)
- {
- text += this.mMethod.Name;
- }
- else
- {
- string text2 = text;
- text = string.Concat(new object[]
- {
- text2,
- this.mTarget.GetType(),
- ".",
- this.mMethod.Name
- });
- }
- text = text + ": " + ex.Message;
- text += "\n Expected: ";
- if (this.mParameterInfos.Length == 0)
- {
- text += "no arguments";
- }
- else
- {
- text += this.mParameterInfos[0];
- for (int j = 1; j < this.mParameterInfos.Length; j++)
- {
- text = text + ", " + this.mParameterInfos[j].ParameterType;
- }
- }
- text += "\n Received: ";
- if (this.mParameters.Length == 0)
- {
- text += "no arguments";
- }
- else
- {
- text += this.mParameters[0].type;
- for (int k = 1; k < this.mParameters.Length; k++)
- {
- text = text + ", " + this.mParameters[k].type;
- }
- }
- text += "\n";
- Debug.LogError(text);
- }
- int l = 0;
- int num2 = this.mArgs.Length;
- while (l < num2)
- {
- if (this.mParameterInfos[l].IsIn || this.mParameterInfos[l].IsOut)
- {
- this.mParameters[l].value = this.mArgs[l];
- }
- this.mArgs[l] = null;
- l++;
- }
- }
- return true;
- }
- return false;
- }
- public void Clear()
- {
- this.mTarget = null;
- this.mMethodName = null;
- this.mRawDelegate = false;
- this.mCachedCallback = null;
- this.mParameters = null;
- this.mCached = false;
- this.mMethod = null;
- this.mParameterInfos = null;
- this.mArgs = null;
- }
- public override string ToString()
- {
- if (!(this.mTarget != null))
- {
- return (!this.mRawDelegate) ? null : "[delegate]";
- }
- string text = this.mTarget.GetType().ToString();
- int num = text.LastIndexOf('.');
- if (num > 0)
- {
- text = text.Substring(num + 1);
- }
- if (!string.IsNullOrEmpty(this.methodName))
- {
- return text + "/" + this.methodName;
- }
- return text + "/[delegate]";
- }
- public static void Execute(List<EventDelegate> list)
- {
- if (list != null)
- {
- for (int i = 0; i < list.Count; i++)
- {
- EventDelegate eventDelegate = list[i];
- if (eventDelegate != null)
- {
- try
- {
- eventDelegate.Execute();
- }
- catch (Exception ex)
- {
- if (ex.InnerException != null)
- {
- Debug.LogError(ex.InnerException.Message);
- }
- else
- {
- Debug.LogError(ex.Message);
- }
- }
- if (i >= list.Count)
- {
- break;
- }
- if (list[i] != eventDelegate)
- {
- continue;
- }
- if (eventDelegate.oneShot)
- {
- list.RemoveAt(i);
- continue;
- }
- }
- }
- }
- }
- public static bool IsValid(List<EventDelegate> list)
- {
- if (list != null)
- {
- int i = 0;
- int count = list.Count;
- while (i < count)
- {
- EventDelegate eventDelegate = list[i];
- if (eventDelegate != null && eventDelegate.isValid)
- {
- return true;
- }
- i++;
- }
- }
- return false;
- }
- public static EventDelegate Set(List<EventDelegate> list, EventDelegate.Callback callback)
- {
- if (list != null)
- {
- EventDelegate eventDelegate = new EventDelegate(callback);
- list.Clear();
- list.Add(eventDelegate);
- return eventDelegate;
- }
- return null;
- }
- public static void Set(List<EventDelegate> list, EventDelegate del)
- {
- if (list != null)
- {
- list.Clear();
- list.Add(del);
- }
- }
- public static EventDelegate Add(List<EventDelegate> list, EventDelegate.Callback callback)
- {
- return EventDelegate.Add(list, callback, false);
- }
- public static EventDelegate Add(List<EventDelegate> list, EventDelegate.Callback callback, bool oneShot)
- {
- if (list != null)
- {
- int i = 0;
- int count = list.Count;
- while (i < count)
- {
- EventDelegate eventDelegate = list[i];
- if (eventDelegate != null && eventDelegate.Equals(callback))
- {
- return eventDelegate;
- }
- i++;
- }
- EventDelegate eventDelegate2 = new EventDelegate(callback);
- eventDelegate2.oneShot = oneShot;
- list.Add(eventDelegate2);
- return eventDelegate2;
- }
- Debug.LogWarning("Attempting to add a callback to a list that's null");
- return null;
- }
- public static void Add(List<EventDelegate> list, EventDelegate ev)
- {
- EventDelegate.Add(list, ev, ev.oneShot);
- }
- public static void Add(List<EventDelegate> list, EventDelegate ev, bool oneShot)
- {
- if (ev.mRawDelegate || ev.target == null || string.IsNullOrEmpty(ev.methodName))
- {
- EventDelegate.Add(list, ev.mCachedCallback, oneShot);
- }
- else if (list != null)
- {
- int i = 0;
- int count = list.Count;
- while (i < count)
- {
- EventDelegate eventDelegate = list[i];
- if (eventDelegate != null && eventDelegate.Equals(ev))
- {
- return;
- }
- i++;
- }
- EventDelegate eventDelegate2 = new EventDelegate(ev.target, ev.methodName);
- eventDelegate2.oneShot = oneShot;
- if (ev.mParameters != null && ev.mParameters.Length > 0)
- {
- eventDelegate2.mParameters = new EventDelegate.Parameter[ev.mParameters.Length];
- for (int j = 0; j < ev.mParameters.Length; j++)
- {
- eventDelegate2.mParameters[j] = ev.mParameters[j];
- }
- }
- list.Add(eventDelegate2);
- }
- else
- {
- Debug.LogWarning("Attempting to add a callback to a list that's null");
- }
- }
- public static bool Remove(List<EventDelegate> list, EventDelegate.Callback callback)
- {
- if (list != null)
- {
- int i = 0;
- int count = list.Count;
- while (i < count)
- {
- EventDelegate eventDelegate = list[i];
- if (eventDelegate != null && eventDelegate.Equals(callback))
- {
- list.RemoveAt(i);
- return true;
- }
- i++;
- }
- }
- return false;
- }
- public static bool Remove(List<EventDelegate> list, EventDelegate ev)
- {
- if (list != null)
- {
- int i = 0;
- int count = list.Count;
- while (i < count)
- {
- EventDelegate eventDelegate = list[i];
- if (eventDelegate != null && eventDelegate.Equals(ev))
- {
- list.RemoveAt(i);
- return true;
- }
- i++;
- }
- }
- return false;
- }
- [SerializeField]
- private MonoBehaviour mTarget;
- [SerializeField]
- private string mMethodName;
- [SerializeField]
- private EventDelegate.Parameter[] mParameters;
- public bool oneShot;
- [NonSerialized]
- private EventDelegate.Callback mCachedCallback;
- [NonSerialized]
- private bool mRawDelegate;
- [NonSerialized]
- private bool mCached;
- [NonSerialized]
- private MethodInfo mMethod;
- [NonSerialized]
- private ParameterInfo[] mParameterInfos;
- [NonSerialized]
- private object[] mArgs;
- private static int s_Hash = "EventDelegate".GetHashCode();
- [Serializable]
- public class Parameter
- {
- public Parameter()
- {
- }
- public Parameter(UnityEngine.Object obj, string field)
- {
- this.obj = obj;
- this.field = field;
- }
- public Parameter(object val)
- {
- this.mValue = val;
- }
- public object value
- {
- get
- {
- if (this.mValue != null)
- {
- return this.mValue;
- }
- if (!this.cached)
- {
- this.cached = true;
- this.fieldInfo = null;
- this.propInfo = null;
- if (this.obj != null && !string.IsNullOrEmpty(this.field))
- {
- Type type = this.obj.GetType();
- this.propInfo = type.GetProperty(this.field);
- if (this.propInfo == null)
- {
- this.fieldInfo = type.GetField(this.field);
- }
- }
- }
- if (this.propInfo != null)
- {
- return this.propInfo.GetValue(this.obj, null);
- }
- if (this.fieldInfo != null)
- {
- return this.fieldInfo.GetValue(this.obj);
- }
- if (this.obj != null)
- {
- return this.obj;
- }
- if (this.expectedType != null && this.expectedType.IsValueType)
- {
- return null;
- }
- return Convert.ChangeType(null, this.expectedType);
- }
- set
- {
- this.mValue = value;
- }
- }
- public Type type
- {
- get
- {
- if (this.mValue != null)
- {
- return this.mValue.GetType();
- }
- if (this.obj == null)
- {
- return typeof(void);
- }
- return this.obj.GetType();
- }
- }
- public UnityEngine.Object obj;
- public string field;
- [NonSerialized]
- private object mValue;
- [NonSerialized]
- public Type expectedType = typeof(void);
- [NonSerialized]
- public bool cached;
- [NonSerialized]
- public PropertyInfo propInfo;
- [NonSerialized]
- public FieldInfo fieldInfo;
- }
- public delegate void Callback();
- }
|