EventDelegate.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Reflection;
  4. using UnityEngine;
  5. [Serializable]
  6. public class EventDelegate
  7. {
  8. public EventDelegate()
  9. {
  10. }
  11. public EventDelegate(EventDelegate.Callback call)
  12. {
  13. this.Set(call);
  14. }
  15. public EventDelegate(MonoBehaviour target, string methodName)
  16. {
  17. this.Set(target, methodName);
  18. }
  19. public MonoBehaviour target
  20. {
  21. get
  22. {
  23. return this.mTarget;
  24. }
  25. set
  26. {
  27. this.mTarget = value;
  28. this.mCachedCallback = null;
  29. this.mRawDelegate = false;
  30. this.mCached = false;
  31. this.mMethod = null;
  32. this.mParameterInfos = null;
  33. this.mParameters = null;
  34. }
  35. }
  36. public string methodName
  37. {
  38. get
  39. {
  40. return this.mMethodName;
  41. }
  42. set
  43. {
  44. this.mMethodName = value;
  45. this.mCachedCallback = null;
  46. this.mRawDelegate = false;
  47. this.mCached = false;
  48. this.mMethod = null;
  49. this.mParameterInfos = null;
  50. this.mParameters = null;
  51. }
  52. }
  53. public EventDelegate.Parameter[] parameters
  54. {
  55. get
  56. {
  57. if (!this.mCached)
  58. {
  59. this.Cache();
  60. }
  61. return this.mParameters;
  62. }
  63. }
  64. public bool isValid
  65. {
  66. get
  67. {
  68. if (!this.mCached)
  69. {
  70. this.Cache();
  71. }
  72. return (this.mRawDelegate && this.mCachedCallback != null) || (this.mTarget != null && !string.IsNullOrEmpty(this.mMethodName));
  73. }
  74. }
  75. public bool isEnabled
  76. {
  77. get
  78. {
  79. if (!this.mCached)
  80. {
  81. this.Cache();
  82. }
  83. if (this.mRawDelegate && this.mCachedCallback != null)
  84. {
  85. return true;
  86. }
  87. if (this.mTarget == null)
  88. {
  89. return false;
  90. }
  91. MonoBehaviour monoBehaviour = this.mTarget;
  92. return monoBehaviour == null || monoBehaviour.enabled;
  93. }
  94. }
  95. private static string GetMethodName(EventDelegate.Callback callback)
  96. {
  97. return callback.Method.Name;
  98. }
  99. private static bool IsValid(EventDelegate.Callback callback)
  100. {
  101. return callback != null && callback.Method != null;
  102. }
  103. public override bool Equals(object obj)
  104. {
  105. if (obj == null)
  106. {
  107. return !this.isValid;
  108. }
  109. if (obj is EventDelegate.Callback)
  110. {
  111. EventDelegate.Callback callback = obj as EventDelegate.Callback;
  112. if (callback.Equals(this.mCachedCallback))
  113. {
  114. return true;
  115. }
  116. MonoBehaviour y = callback.Target as MonoBehaviour;
  117. return this.mTarget == y && string.Equals(this.mMethodName, EventDelegate.GetMethodName(callback));
  118. }
  119. else
  120. {
  121. if (obj is EventDelegate)
  122. {
  123. EventDelegate eventDelegate = obj as EventDelegate;
  124. return this.mTarget == eventDelegate.mTarget && string.Equals(this.mMethodName, eventDelegate.mMethodName);
  125. }
  126. return false;
  127. }
  128. }
  129. public override int GetHashCode()
  130. {
  131. return EventDelegate.s_Hash;
  132. }
  133. private void Set(EventDelegate.Callback call)
  134. {
  135. this.Clear();
  136. if (call != null && EventDelegate.IsValid(call))
  137. {
  138. this.mTarget = (call.Target as MonoBehaviour);
  139. if (this.mTarget == null)
  140. {
  141. this.mRawDelegate = true;
  142. this.mCachedCallback = call;
  143. this.mMethodName = null;
  144. }
  145. else
  146. {
  147. this.mMethodName = EventDelegate.GetMethodName(call);
  148. this.mRawDelegate = false;
  149. }
  150. }
  151. }
  152. public void Set(MonoBehaviour target, string methodName)
  153. {
  154. this.Clear();
  155. this.mTarget = target;
  156. this.mMethodName = methodName;
  157. }
  158. private void Cache()
  159. {
  160. this.mCached = true;
  161. if (this.mRawDelegate)
  162. {
  163. return;
  164. }
  165. if ((this.mCachedCallback == null || this.mCachedCallback.Target as MonoBehaviour != this.mTarget || EventDelegate.GetMethodName(this.mCachedCallback) != this.mMethodName) && this.mTarget != null && !string.IsNullOrEmpty(this.mMethodName))
  166. {
  167. Type type = this.mTarget.GetType();
  168. this.mMethod = null;
  169. while (type != null)
  170. {
  171. try
  172. {
  173. this.mMethod = type.GetMethod(this.mMethodName, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
  174. if (this.mMethod != null)
  175. {
  176. break;
  177. }
  178. }
  179. catch (Exception)
  180. {
  181. }
  182. type = type.BaseType;
  183. }
  184. if (this.mMethod == null)
  185. {
  186. Debug.LogError(string.Concat(new object[]
  187. {
  188. "Could not find method '",
  189. this.mMethodName,
  190. "' on ",
  191. this.mTarget.GetType()
  192. }), this.mTarget);
  193. return;
  194. }
  195. if (this.mMethod.ReturnType != typeof(void))
  196. {
  197. Debug.LogError(string.Concat(new object[]
  198. {
  199. this.mTarget.GetType(),
  200. ".",
  201. this.mMethodName,
  202. " must have a 'void' return type."
  203. }), this.mTarget);
  204. return;
  205. }
  206. this.mParameterInfos = this.mMethod.GetParameters();
  207. if (this.mParameterInfos.Length == 0)
  208. {
  209. this.mCachedCallback = (EventDelegate.Callback)Delegate.CreateDelegate(typeof(EventDelegate.Callback), this.mTarget, this.mMethodName);
  210. this.mArgs = null;
  211. this.mParameters = null;
  212. return;
  213. }
  214. this.mCachedCallback = null;
  215. if (this.mParameters == null || this.mParameters.Length != this.mParameterInfos.Length)
  216. {
  217. this.mParameters = new EventDelegate.Parameter[this.mParameterInfos.Length];
  218. int i = 0;
  219. int num = this.mParameters.Length;
  220. while (i < num)
  221. {
  222. this.mParameters[i] = new EventDelegate.Parameter();
  223. i++;
  224. }
  225. }
  226. int j = 0;
  227. int num2 = this.mParameters.Length;
  228. while (j < num2)
  229. {
  230. this.mParameters[j].expectedType = this.mParameterInfos[j].ParameterType;
  231. j++;
  232. }
  233. }
  234. }
  235. public bool Execute()
  236. {
  237. if (!this.mCached)
  238. {
  239. this.Cache();
  240. }
  241. if (this.mCachedCallback != null)
  242. {
  243. this.mCachedCallback();
  244. return true;
  245. }
  246. if (this.mMethod != null)
  247. {
  248. if (this.mParameters == null || this.mParameters.Length == 0)
  249. {
  250. this.mMethod.Invoke(this.mTarget, null);
  251. }
  252. else
  253. {
  254. if (this.mArgs == null || this.mArgs.Length != this.mParameters.Length)
  255. {
  256. this.mArgs = new object[this.mParameters.Length];
  257. }
  258. int i = 0;
  259. int num = this.mParameters.Length;
  260. while (i < num)
  261. {
  262. this.mArgs[i] = this.mParameters[i].value;
  263. i++;
  264. }
  265. try
  266. {
  267. this.mMethod.Invoke(this.mTarget, this.mArgs);
  268. }
  269. catch (ArgumentException ex)
  270. {
  271. string text = "Error calling ";
  272. if (this.mTarget == null)
  273. {
  274. text += this.mMethod.Name;
  275. }
  276. else
  277. {
  278. string text2 = text;
  279. text = string.Concat(new object[]
  280. {
  281. text2,
  282. this.mTarget.GetType(),
  283. ".",
  284. this.mMethod.Name
  285. });
  286. }
  287. text = text + ": " + ex.Message;
  288. text += "\n Expected: ";
  289. if (this.mParameterInfos.Length == 0)
  290. {
  291. text += "no arguments";
  292. }
  293. else
  294. {
  295. text += this.mParameterInfos[0];
  296. for (int j = 1; j < this.mParameterInfos.Length; j++)
  297. {
  298. text = text + ", " + this.mParameterInfos[j].ParameterType;
  299. }
  300. }
  301. text += "\n Received: ";
  302. if (this.mParameters.Length == 0)
  303. {
  304. text += "no arguments";
  305. }
  306. else
  307. {
  308. text += this.mParameters[0].type;
  309. for (int k = 1; k < this.mParameters.Length; k++)
  310. {
  311. text = text + ", " + this.mParameters[k].type;
  312. }
  313. }
  314. text += "\n";
  315. Debug.LogError(text);
  316. }
  317. int l = 0;
  318. int num2 = this.mArgs.Length;
  319. while (l < num2)
  320. {
  321. if (this.mParameterInfos[l].IsIn || this.mParameterInfos[l].IsOut)
  322. {
  323. this.mParameters[l].value = this.mArgs[l];
  324. }
  325. this.mArgs[l] = null;
  326. l++;
  327. }
  328. }
  329. return true;
  330. }
  331. return false;
  332. }
  333. public void Clear()
  334. {
  335. this.mTarget = null;
  336. this.mMethodName = null;
  337. this.mRawDelegate = false;
  338. this.mCachedCallback = null;
  339. this.mParameters = null;
  340. this.mCached = false;
  341. this.mMethod = null;
  342. this.mParameterInfos = null;
  343. this.mArgs = null;
  344. }
  345. public override string ToString()
  346. {
  347. if (!(this.mTarget != null))
  348. {
  349. return (!this.mRawDelegate) ? null : "[delegate]";
  350. }
  351. string text = this.mTarget.GetType().ToString();
  352. int num = text.LastIndexOf('.');
  353. if (num > 0)
  354. {
  355. text = text.Substring(num + 1);
  356. }
  357. if (!string.IsNullOrEmpty(this.methodName))
  358. {
  359. return text + "/" + this.methodName;
  360. }
  361. return text + "/[delegate]";
  362. }
  363. public static void Execute(List<EventDelegate> list)
  364. {
  365. if (list != null)
  366. {
  367. for (int i = 0; i < list.Count; i++)
  368. {
  369. EventDelegate eventDelegate = list[i];
  370. if (eventDelegate != null)
  371. {
  372. try
  373. {
  374. eventDelegate.Execute();
  375. }
  376. catch (Exception ex)
  377. {
  378. if (ex.InnerException != null)
  379. {
  380. Debug.LogError(ex.InnerException.Message);
  381. }
  382. else
  383. {
  384. Debug.LogError(ex.Message);
  385. }
  386. }
  387. if (i >= list.Count)
  388. {
  389. break;
  390. }
  391. if (list[i] != eventDelegate)
  392. {
  393. continue;
  394. }
  395. if (eventDelegate.oneShot)
  396. {
  397. list.RemoveAt(i);
  398. continue;
  399. }
  400. }
  401. }
  402. }
  403. }
  404. public static bool IsValid(List<EventDelegate> list)
  405. {
  406. if (list != null)
  407. {
  408. int i = 0;
  409. int count = list.Count;
  410. while (i < count)
  411. {
  412. EventDelegate eventDelegate = list[i];
  413. if (eventDelegate != null && eventDelegate.isValid)
  414. {
  415. return true;
  416. }
  417. i++;
  418. }
  419. }
  420. return false;
  421. }
  422. public static EventDelegate Set(List<EventDelegate> list, EventDelegate.Callback callback)
  423. {
  424. if (list != null)
  425. {
  426. EventDelegate eventDelegate = new EventDelegate(callback);
  427. list.Clear();
  428. list.Add(eventDelegate);
  429. return eventDelegate;
  430. }
  431. return null;
  432. }
  433. public static void Set(List<EventDelegate> list, EventDelegate del)
  434. {
  435. if (list != null)
  436. {
  437. list.Clear();
  438. list.Add(del);
  439. }
  440. }
  441. public static EventDelegate Add(List<EventDelegate> list, EventDelegate.Callback callback)
  442. {
  443. return EventDelegate.Add(list, callback, false);
  444. }
  445. public static EventDelegate Add(List<EventDelegate> list, EventDelegate.Callback callback, bool oneShot)
  446. {
  447. if (list != null)
  448. {
  449. int i = 0;
  450. int count = list.Count;
  451. while (i < count)
  452. {
  453. EventDelegate eventDelegate = list[i];
  454. if (eventDelegate != null && eventDelegate.Equals(callback))
  455. {
  456. return eventDelegate;
  457. }
  458. i++;
  459. }
  460. EventDelegate eventDelegate2 = new EventDelegate(callback);
  461. eventDelegate2.oneShot = oneShot;
  462. list.Add(eventDelegate2);
  463. return eventDelegate2;
  464. }
  465. Debug.LogWarning("Attempting to add a callback to a list that's null");
  466. return null;
  467. }
  468. public static void Add(List<EventDelegate> list, EventDelegate ev)
  469. {
  470. EventDelegate.Add(list, ev, ev.oneShot);
  471. }
  472. public static void Add(List<EventDelegate> list, EventDelegate ev, bool oneShot)
  473. {
  474. if (ev.mRawDelegate || ev.target == null || string.IsNullOrEmpty(ev.methodName))
  475. {
  476. EventDelegate.Add(list, ev.mCachedCallback, oneShot);
  477. }
  478. else if (list != null)
  479. {
  480. int i = 0;
  481. int count = list.Count;
  482. while (i < count)
  483. {
  484. EventDelegate eventDelegate = list[i];
  485. if (eventDelegate != null && eventDelegate.Equals(ev))
  486. {
  487. return;
  488. }
  489. i++;
  490. }
  491. EventDelegate eventDelegate2 = new EventDelegate(ev.target, ev.methodName);
  492. eventDelegate2.oneShot = oneShot;
  493. if (ev.mParameters != null && ev.mParameters.Length > 0)
  494. {
  495. eventDelegate2.mParameters = new EventDelegate.Parameter[ev.mParameters.Length];
  496. for (int j = 0; j < ev.mParameters.Length; j++)
  497. {
  498. eventDelegate2.mParameters[j] = ev.mParameters[j];
  499. }
  500. }
  501. list.Add(eventDelegate2);
  502. }
  503. else
  504. {
  505. Debug.LogWarning("Attempting to add a callback to a list that's null");
  506. }
  507. }
  508. public static bool Remove(List<EventDelegate> list, EventDelegate.Callback callback)
  509. {
  510. if (list != null)
  511. {
  512. int i = 0;
  513. int count = list.Count;
  514. while (i < count)
  515. {
  516. EventDelegate eventDelegate = list[i];
  517. if (eventDelegate != null && eventDelegate.Equals(callback))
  518. {
  519. list.RemoveAt(i);
  520. return true;
  521. }
  522. i++;
  523. }
  524. }
  525. return false;
  526. }
  527. public static bool Remove(List<EventDelegate> list, EventDelegate ev)
  528. {
  529. if (list != null)
  530. {
  531. int i = 0;
  532. int count = list.Count;
  533. while (i < count)
  534. {
  535. EventDelegate eventDelegate = list[i];
  536. if (eventDelegate != null && eventDelegate.Equals(ev))
  537. {
  538. list.RemoveAt(i);
  539. return true;
  540. }
  541. i++;
  542. }
  543. }
  544. return false;
  545. }
  546. [SerializeField]
  547. private MonoBehaviour mTarget;
  548. [SerializeField]
  549. private string mMethodName;
  550. [SerializeField]
  551. private EventDelegate.Parameter[] mParameters;
  552. public bool oneShot;
  553. [NonSerialized]
  554. private EventDelegate.Callback mCachedCallback;
  555. [NonSerialized]
  556. private bool mRawDelegate;
  557. [NonSerialized]
  558. private bool mCached;
  559. [NonSerialized]
  560. private MethodInfo mMethod;
  561. [NonSerialized]
  562. private ParameterInfo[] mParameterInfos;
  563. [NonSerialized]
  564. private object[] mArgs;
  565. private static int s_Hash = "EventDelegate".GetHashCode();
  566. [Serializable]
  567. public class Parameter
  568. {
  569. public Parameter()
  570. {
  571. }
  572. public Parameter(UnityEngine.Object obj, string field)
  573. {
  574. this.obj = obj;
  575. this.field = field;
  576. }
  577. public Parameter(object val)
  578. {
  579. this.mValue = val;
  580. }
  581. public object value
  582. {
  583. get
  584. {
  585. if (this.mValue != null)
  586. {
  587. return this.mValue;
  588. }
  589. if (!this.cached)
  590. {
  591. this.cached = true;
  592. this.fieldInfo = null;
  593. this.propInfo = null;
  594. if (this.obj != null && !string.IsNullOrEmpty(this.field))
  595. {
  596. Type type = this.obj.GetType();
  597. this.propInfo = type.GetProperty(this.field);
  598. if (this.propInfo == null)
  599. {
  600. this.fieldInfo = type.GetField(this.field);
  601. }
  602. }
  603. }
  604. if (this.propInfo != null)
  605. {
  606. return this.propInfo.GetValue(this.obj, null);
  607. }
  608. if (this.fieldInfo != null)
  609. {
  610. return this.fieldInfo.GetValue(this.obj);
  611. }
  612. if (this.obj != null)
  613. {
  614. return this.obj;
  615. }
  616. if (this.expectedType != null && this.expectedType.IsValueType)
  617. {
  618. return null;
  619. }
  620. return Convert.ChangeType(null, this.expectedType);
  621. }
  622. set
  623. {
  624. this.mValue = value;
  625. }
  626. }
  627. public Type type
  628. {
  629. get
  630. {
  631. if (this.mValue != null)
  632. {
  633. return this.mValue.GetType();
  634. }
  635. if (this.obj == null)
  636. {
  637. return typeof(void);
  638. }
  639. return this.obj.GetType();
  640. }
  641. }
  642. public UnityEngine.Object obj;
  643. public string field;
  644. [NonSerialized]
  645. private object mValue;
  646. [NonSerialized]
  647. public Type expectedType = typeof(void);
  648. [NonSerialized]
  649. public bool cached;
  650. [NonSerialized]
  651. public PropertyInfo propInfo;
  652. [NonSerialized]
  653. public FieldInfo fieldInfo;
  654. }
  655. public delegate void Callback();
  656. }