PageRenderTime 47ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/Source/ChipzIRC/ScriptedEventMgr.cs

https://bitbucket.org/tierai/chipzirc
C# | 222 lines | 124 code | 32 blank | 66 comment | 9 complexity | 3444ba2d165a1d2931e502a2ebf870a8 MD5 | raw file
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Windows.Forms;
  4. using Chipz.Core;
  5. namespace ChipzIRC
  6. {
  7. public interface IAutoEventInstance
  8. {
  9. }
  10. /// <summary>
  11. /// ScriptedEventMgr
  12. /// TODO: Replace with EVENT in DynClass.
  13. /// </summary>
  14. public class ScriptedEventMgr : IDisposable
  15. {
  16. public static ScriptedEventMgr Instance
  17. {
  18. get { return s_instance; }
  19. }
  20. static ScriptedEventMgr s_instance = new ScriptedEventMgr();
  21. private ScriptedEventMgr()
  22. {
  23. InstanceMgr<IAutoEventInstance>.InstanceAdded += InstanceMgr_InstanceAdded;
  24. InstanceMgr<IAutoEventInstance>.InstanceRemoved += InstanceMgr_InstanceRemoved;
  25. }
  26. void InstanceMgr_InstanceAdded(object sender, EventArgs<IAutoEventInstance> e)
  27. {
  28. AddInstanceEvents(e.Value);
  29. }
  30. void InstanceMgr_InstanceRemoved(object sender, EventArgs<IAutoEventInstance> e)
  31. {
  32. RemoveInstanceEvents(e.Value);
  33. }
  34. //List<object> m_instanceList = new List<object>();
  35. List<ScriptedEvent> m_scriptedEvents = new List<ScriptedEvent>();
  36. Dictionary<object, List<ScriptedEvent>> m_instanceEvents = new Dictionary<object, List<ScriptedEvent>>();
  37. void Trace(object msg)
  38. {
  39. System.Diagnostics.Trace.WriteLine(msg, "ScriptedEventMgr");
  40. }
  41. public ScriptedEvent[] LoadEvents(string code)
  42. {
  43. return LoadEvents(code, null);
  44. }
  45. public ScriptedEvent[] LoadEvents(string code, string language)
  46. {
  47. if (string.IsNullOrEmpty(code))
  48. throw new InvalidOperationException("Code cannot be null or empty");
  49. if (code.StartsWith("#"))
  50. {
  51. string tag = "#language=";
  52. int idx = code.IndexOf('\n');
  53. if (idx < 0)
  54. throw new InvalidOperationException();
  55. string str = code.Substring(0, idx).Trim();
  56. if (!str.StartsWith(tag))
  57. throw new InvalidOperationException("#language= expected");
  58. str = str.Substring(tag.Length);
  59. language = str.Trim();
  60. code = code.Substring(idx);
  61. }
  62. if (string.IsNullOrEmpty(language))
  63. language = "boo";
  64. // throw new InvalidOperationException("Invalid language string");
  65. language = language.ToLower();
  66. if (language == "boo")
  67. {
  68. return ScriptedEvent.ParseBoo(code);
  69. }
  70. /* else if (language == "c#")
  71. {
  72. return ScriptedEvent.ParseCSharp(code);
  73. }
  74. else if (language == "vb")
  75. {
  76. return ScriptedEvent.ParseVB(code);
  77. }*/
  78. else
  79. throw new NotSupportedException("Language " + language + " is not supported");
  80. }
  81. /*public void AddForm(Form form)
  82. {
  83. try
  84. {
  85. AddInstance(form);
  86. form.FormClosed += new FormClosedEventHandler(form_FormClosed);
  87. }
  88. catch (Exception ex)
  89. {
  90. Trace(ex);
  91. }
  92. }
  93. void form_FormClosed(object sender, FormClosedEventArgs e)
  94. {
  95. RemoveInstance(sender);
  96. }
  97. public void AddInstance(object instance)
  98. {
  99. try
  100. {
  101. m_instanceList.Add(instance);
  102. AddInstanceEvents(instance);
  103. if(instance is Control)
  104. {
  105. (instance as Form).FormClosed += delegate(object _sender, FormClosedEventArgs _e) { RemoveInstance(_sender); };
  106. }
  107. Trace("Instance of type " + instance.GetType().FullName + " added");
  108. }
  109. catch (Exception ex)
  110. {
  111. Trace(ex);
  112. }
  113. }
  114. public void RemoveInstance(object instance)
  115. {
  116. try
  117. {
  118. if (!m_instanceList.Contains(instance))
  119. return;
  120. m_instanceList.Remove(instance);
  121. RemoveInstanceEvents(instance);
  122. Trace("Instance of type " + instance.GetType().FullName + " removed");
  123. }
  124. catch (Exception ex)
  125. {
  126. Trace(ex);
  127. }
  128. }*/
  129. public void Clear()
  130. {
  131. try
  132. {
  133. foreach(object instance in InstanceMgr<IAutoEventInstance>.Instances)
  134. RemoveInstanceEvents(instance);
  135. m_scriptedEvents.Clear();
  136. }
  137. catch (Exception ex)
  138. {
  139. Trace(ex);
  140. }
  141. }
  142. void AddInstanceEvents(object instance)
  143. {
  144. Type type = instance.GetType();
  145. m_instanceEvents[instance] = new List<ScriptedEvent>();
  146. foreach(ScriptedEvent e in m_scriptedEvents)
  147. {
  148. if (e.EventInfo.DeclaringType.IsAssignableFrom(type))
  149. {
  150. e.Add(instance);
  151. m_instanceEvents[instance].Add(e);
  152. }
  153. }
  154. }
  155. void RemoveInstanceEvents(object instance)
  156. {
  157. if(!m_instanceEvents.ContainsKey(instance))
  158. return;
  159. List<ScriptedEvent> list = m_instanceEvents[instance];
  160. m_instanceEvents.Remove(instance);
  161. foreach(ScriptedEvent e in list)
  162. e.Remove(instance);
  163. }
  164. public void SetEvents(ScriptedEvent[] events)
  165. {
  166. try
  167. {
  168. Clear();
  169. m_scriptedEvents.AddRange(events);
  170. foreach (object instance in InstanceMgr<IAutoEventInstance>.Instances)
  171. AddInstanceEvents(instance);
  172. }
  173. catch (Exception ex)
  174. {
  175. Trace(ex);
  176. throw ex;
  177. }
  178. }
  179. #region IDisposable Members
  180. public void Dispose()
  181. {
  182. Clear();
  183. }
  184. #endregion
  185. }
  186. }