OVRMessenger.cs 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. using System;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. internal static class OVRMessenger
  5. {
  6. public static void MarkAsPermanent(string eventType)
  7. {
  8. OVRMessenger.permanentMessages.Add(eventType);
  9. }
  10. public static void Cleanup()
  11. {
  12. List<string> list = new List<string>();
  13. foreach (KeyValuePair<string, Delegate> keyValuePair in OVRMessenger.eventTable)
  14. {
  15. bool flag = false;
  16. foreach (string b in OVRMessenger.permanentMessages)
  17. {
  18. if (keyValuePair.Key == b)
  19. {
  20. flag = true;
  21. break;
  22. }
  23. }
  24. if (!flag)
  25. {
  26. list.Add(keyValuePair.Key);
  27. }
  28. }
  29. foreach (string key in list)
  30. {
  31. OVRMessenger.eventTable.Remove(key);
  32. }
  33. }
  34. public static void PrintEventTable()
  35. {
  36. Debug.Log("\t\t\t=== MESSENGER PrintEventTable ===");
  37. foreach (KeyValuePair<string, Delegate> keyValuePair in OVRMessenger.eventTable)
  38. {
  39. Debug.Log(string.Concat(new object[]
  40. {
  41. "\t\t\t",
  42. keyValuePair.Key,
  43. "\t\t",
  44. keyValuePair.Value
  45. }));
  46. }
  47. Debug.Log("\n");
  48. }
  49. public static void OnListenerAdding(string eventType, Delegate listenerBeingAdded)
  50. {
  51. if (!OVRMessenger.eventTable.ContainsKey(eventType))
  52. {
  53. OVRMessenger.eventTable.Add(eventType, null);
  54. }
  55. Delegate @delegate = OVRMessenger.eventTable[eventType];
  56. if (@delegate != null && @delegate.GetType() != listenerBeingAdded.GetType())
  57. {
  58. throw new OVRMessenger.ListenerException(string.Format("Attempting to add listener with inconsistent signature for event type {0}. Current listeners have type {1} and listener being added has type {2}", eventType, @delegate.GetType().Name, listenerBeingAdded.GetType().Name));
  59. }
  60. }
  61. public static void OnListenerRemoving(string eventType, Delegate listenerBeingRemoved)
  62. {
  63. if (!OVRMessenger.eventTable.ContainsKey(eventType))
  64. {
  65. throw new OVRMessenger.ListenerException(string.Format("Attempting to remove listener for type \"{0}\" but Messenger doesn't know about this event type.", eventType));
  66. }
  67. Delegate @delegate = OVRMessenger.eventTable[eventType];
  68. if (@delegate == null)
  69. {
  70. throw new OVRMessenger.ListenerException(string.Format("Attempting to remove listener with for event type \"{0}\" but current listener is null.", eventType));
  71. }
  72. if (@delegate.GetType() != listenerBeingRemoved.GetType())
  73. {
  74. throw new OVRMessenger.ListenerException(string.Format("Attempting to remove listener with inconsistent signature for event type {0}. Current listeners have type {1} and listener being removed has type {2}", eventType, @delegate.GetType().Name, listenerBeingRemoved.GetType().Name));
  75. }
  76. }
  77. public static void OnListenerRemoved(string eventType)
  78. {
  79. if (OVRMessenger.eventTable[eventType] == null)
  80. {
  81. OVRMessenger.eventTable.Remove(eventType);
  82. }
  83. }
  84. public static void OnBroadcasting(string eventType)
  85. {
  86. }
  87. public static OVRMessenger.BroadcastException CreateBroadcastSignatureException(string eventType)
  88. {
  89. return new OVRMessenger.BroadcastException(string.Format("Broadcasting message \"{0}\" but listeners have a different signature than the broadcaster.", eventType));
  90. }
  91. public static void AddListener(string eventType, OVRCallback handler)
  92. {
  93. OVRMessenger.OnListenerAdding(eventType, handler);
  94. OVRMessenger.eventTable[eventType] = (OVRCallback)Delegate.Combine((OVRCallback)OVRMessenger.eventTable[eventType], handler);
  95. }
  96. public static void AddListener<T>(string eventType, OVRCallback<T> handler)
  97. {
  98. OVRMessenger.OnListenerAdding(eventType, handler);
  99. OVRMessenger.eventTable[eventType] = (OVRCallback<T>)Delegate.Combine((OVRCallback<T>)OVRMessenger.eventTable[eventType], handler);
  100. }
  101. public static void AddListener<T, U>(string eventType, OVRCallback<T, U> handler)
  102. {
  103. OVRMessenger.OnListenerAdding(eventType, handler);
  104. OVRMessenger.eventTable[eventType] = (OVRCallback<T, U>)Delegate.Combine((OVRCallback<T, U>)OVRMessenger.eventTable[eventType], handler);
  105. }
  106. public static void AddListener<T, U, V>(string eventType, OVRCallback<T, U, V> handler)
  107. {
  108. OVRMessenger.OnListenerAdding(eventType, handler);
  109. OVRMessenger.eventTable[eventType] = (OVRCallback<T, U, V>)Delegate.Combine((OVRCallback<T, U, V>)OVRMessenger.eventTable[eventType], handler);
  110. }
  111. public static void RemoveListener(string eventType, OVRCallback handler)
  112. {
  113. OVRMessenger.OnListenerRemoving(eventType, handler);
  114. OVRMessenger.eventTable[eventType] = (OVRCallback)Delegate.Remove((OVRCallback)OVRMessenger.eventTable[eventType], handler);
  115. OVRMessenger.OnListenerRemoved(eventType);
  116. }
  117. public static void RemoveListener<T>(string eventType, OVRCallback<T> handler)
  118. {
  119. OVRMessenger.OnListenerRemoving(eventType, handler);
  120. OVRMessenger.eventTable[eventType] = (OVRCallback<T>)Delegate.Remove((OVRCallback<T>)OVRMessenger.eventTable[eventType], handler);
  121. OVRMessenger.OnListenerRemoved(eventType);
  122. }
  123. public static void RemoveListener<T, U>(string eventType, OVRCallback<T, U> handler)
  124. {
  125. OVRMessenger.OnListenerRemoving(eventType, handler);
  126. OVRMessenger.eventTable[eventType] = (OVRCallback<T, U>)Delegate.Remove((OVRCallback<T, U>)OVRMessenger.eventTable[eventType], handler);
  127. OVRMessenger.OnListenerRemoved(eventType);
  128. }
  129. public static void RemoveListener<T, U, V>(string eventType, OVRCallback<T, U, V> handler)
  130. {
  131. OVRMessenger.OnListenerRemoving(eventType, handler);
  132. OVRMessenger.eventTable[eventType] = (OVRCallback<T, U, V>)Delegate.Remove((OVRCallback<T, U, V>)OVRMessenger.eventTable[eventType], handler);
  133. OVRMessenger.OnListenerRemoved(eventType);
  134. }
  135. public static void Broadcast(string eventType)
  136. {
  137. OVRMessenger.OnBroadcasting(eventType);
  138. Delegate @delegate;
  139. if (OVRMessenger.eventTable.TryGetValue(eventType, out @delegate))
  140. {
  141. OVRCallback ovrcallback = @delegate as OVRCallback;
  142. if (ovrcallback == null)
  143. {
  144. throw OVRMessenger.CreateBroadcastSignatureException(eventType);
  145. }
  146. ovrcallback();
  147. }
  148. }
  149. public static void Broadcast<T>(string eventType, T arg1)
  150. {
  151. OVRMessenger.OnBroadcasting(eventType);
  152. Delegate @delegate;
  153. if (OVRMessenger.eventTable.TryGetValue(eventType, out @delegate))
  154. {
  155. OVRCallback<T> ovrcallback = @delegate as OVRCallback<T>;
  156. if (ovrcallback == null)
  157. {
  158. throw OVRMessenger.CreateBroadcastSignatureException(eventType);
  159. }
  160. ovrcallback(arg1);
  161. }
  162. }
  163. public static void Broadcast<T, U>(string eventType, T arg1, U arg2)
  164. {
  165. OVRMessenger.OnBroadcasting(eventType);
  166. Delegate @delegate;
  167. if (OVRMessenger.eventTable.TryGetValue(eventType, out @delegate))
  168. {
  169. OVRCallback<T, U> ovrcallback = @delegate as OVRCallback<T, U>;
  170. if (ovrcallback == null)
  171. {
  172. throw OVRMessenger.CreateBroadcastSignatureException(eventType);
  173. }
  174. ovrcallback(arg1, arg2);
  175. }
  176. }
  177. public static void Broadcast<T, U, V>(string eventType, T arg1, U arg2, V arg3)
  178. {
  179. OVRMessenger.OnBroadcasting(eventType);
  180. Delegate @delegate;
  181. if (OVRMessenger.eventTable.TryGetValue(eventType, out @delegate))
  182. {
  183. OVRCallback<T, U, V> ovrcallback = @delegate as OVRCallback<T, U, V>;
  184. if (ovrcallback == null)
  185. {
  186. throw OVRMessenger.CreateBroadcastSignatureException(eventType);
  187. }
  188. ovrcallback(arg1, arg2, arg3);
  189. }
  190. }
  191. private static MessengerHelper messengerHelper = new GameObject("MessengerHelper").AddComponent<MessengerHelper>();
  192. public static Dictionary<string, Delegate> eventTable = new Dictionary<string, Delegate>();
  193. public static List<string> permanentMessages = new List<string>();
  194. public class BroadcastException : Exception
  195. {
  196. public BroadcastException(string msg) : base(msg)
  197. {
  198. }
  199. }
  200. public class ListenerException : Exception
  201. {
  202. public ListenerException(string msg) : base(msg)
  203. {
  204. }
  205. }
  206. }