RegionScriptEngineBase.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. /*
  2. * Copyright (c) Contributors, http://opensimulator.org/
  3. * See CONTRIBUTORS.TXT for a full list of copyright holders.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. * * Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * * Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. * * Neither the name of the OpenSim Project nor the
  13. * names of its contributors may be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
  17. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  18. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
  20. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  21. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  22. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  23. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  25. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. using System;
  28. using System.Collections.Generic;
  29. using System.Reflection;
  30. using System.Text;
  31. using log4net;
  32. using Nini.Config;
  33. using OpenSim.ApplicationPlugins.ScriptEngine.Components;
  34. using OpenSim.Region.Environment.Scenes;
  35. namespace OpenSim.ApplicationPlugins.ScriptEngine
  36. {
  37. public abstract class RegionScriptEngineBase
  38. {
  39. /// <summary>
  40. /// Called on region initialize
  41. /// </summary>
  42. public abstract void Initialize();
  43. public abstract string Name { get; }
  44. /// <summary>
  45. /// Called before components receive Close()
  46. /// </summary>
  47. public abstract void PreClose();
  48. // Hold list of all the different components we have working for us
  49. public List<ComponentBase> Components = new List<ComponentBase>();
  50. public Scene m_Scene;
  51. public IConfigSource m_ConfigSource;
  52. public readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  53. public void Initialize(Scene scene, IConfigSource source)
  54. {
  55. // Region started
  56. m_Scene = scene;
  57. m_ConfigSource = source;
  58. Initialize();
  59. }
  60. /// <summary>
  61. /// Creates instances of script engine components inside "components" List
  62. /// </summary>
  63. /// <param name="ComponentList">Array of comonent names to initialize</param>
  64. public void InitializeComponents(string[] ComponentList)
  65. {
  66. // Take list of providers to initialize and make instances of them
  67. foreach (string c in ComponentList)
  68. {
  69. m_log.Info("[" + Name + "]: Loading: " + c);
  70. lock (Components)
  71. {
  72. lock (ComponentRegistry.providers)
  73. {
  74. try
  75. {
  76. if (ComponentRegistry.providers.ContainsKey(c))
  77. Components.Add(Activator.CreateInstance(ComponentRegistry.providers[c]) as ComponentBase);
  78. else
  79. m_log.Error("[" + Name + "]: Component \"" + c + "\" not found, can not load");
  80. }
  81. catch (Exception ex)
  82. {
  83. m_log.Error("[" + Name + "]: Exception loading \"" + c + "\": " + ex.ToString());
  84. }
  85. }
  86. }
  87. }
  88. // Run Initialize on all our providers, hand over a reference of ourself.
  89. foreach (ComponentBase p in Components.ToArray())
  90. {
  91. try
  92. {
  93. p.Initialize(this);
  94. }
  95. catch (Exception ex)
  96. {
  97. lock (Components)
  98. {
  99. m_log.Error("[" + Name + "]: Error initializing \"" + p.GetType().FullName + "\": " +
  100. ex.ToString());
  101. if (Components.Contains(p))
  102. Components.Remove(p);
  103. }
  104. }
  105. }
  106. // All modules has been initialized, call Start() on them.
  107. foreach (ComponentBase p in Components.ToArray())
  108. {
  109. try
  110. {
  111. p.Start();
  112. }
  113. catch (Exception ex)
  114. {
  115. lock (Components)
  116. {
  117. m_log.Error("[" + Name + "]: Error starting \"" + p.GetType().FullName + "\": " + ex.ToString());
  118. if (Components.Contains(p))
  119. Components.Remove(p);
  120. }
  121. }
  122. }
  123. }
  124. #region Functions to return lists based on type
  125. // Predicate for searching List for a certain type
  126. private static bool FindType<T>(ComponentBase pb)
  127. {
  128. if (pb.GetType() is T)
  129. return true;
  130. return false;
  131. }
  132. public List<ComponentBase> GetCommandComponentList()
  133. {
  134. return Components.FindAll(FindType<CommandBase>);
  135. }
  136. public List<ComponentBase> GetCompilerComponentList()
  137. {
  138. return Components.FindAll(FindType<CompilerBase>);
  139. }
  140. public List<ComponentBase> GetEventComponentList()
  141. {
  142. return Components.FindAll(FindType<EventBase>);
  143. }
  144. public List<ComponentBase> GetScheduleComponentList()
  145. {
  146. return Components.FindAll(FindType<SchedulerBase>);
  147. }
  148. #endregion
  149. public void Close()
  150. {
  151. // We need to shut down
  152. // First call abstracted PreClose()
  153. PreClose();
  154. // Then Call Close() on all components
  155. foreach (ComponentBase p in Components.ToArray())
  156. {
  157. try
  158. {
  159. p.Close();
  160. }
  161. catch (Exception)
  162. {
  163. // TODO: Print error to console
  164. }
  165. }
  166. }
  167. }
  168. }