RegionModulesControllerPlugin.cs 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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 log4net;
  31. using Mono.Addins;
  32. using OpenSim;
  33. using OpenSim.Region.Framework.Interfaces;
  34. using OpenSim.Region.Framework.Scenes;
  35. namespace OpenSim.ApplicationPlugins.RegionModulesController
  36. {
  37. public class RegionModulesControllerPlugin : IRegionModulesController, IApplicationPlugin
  38. {
  39. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  40. private OpenSimBase m_openSim; // for getting the config
  41. private string m_name;
  42. private List<Type> m_nonSharedModules = new List<Type>();
  43. private List<Type> m_sharedModules = new List<Type>();
  44. private List<ISharedRegionModule> m_sharedInstances = new List<ISharedRegionModule>();
  45. #region IApplicationPlugin implementation
  46. public void Initialise (OpenSimBase openSim)
  47. {
  48. m_log.DebugFormat("[REGIONMODULES]: Initializing...");
  49. m_openSim = openSim;
  50. openSim.ApplicationRegistry.RegisterInterface<IRegionModulesController>(this);
  51. string id = AddinManager.CurrentAddin.Id;
  52. int pos = id.LastIndexOf(".");
  53. if (pos == -1) m_name = id;
  54. else m_name = id.Substring(pos + 1);
  55. //ExtensionNodeList list = AddinManager.GetExtensionNodes("/OpenSim/RegionModules");
  56. // load all the (new) region-module classes
  57. foreach (TypeExtensionNode node in AddinManager.GetExtensionNodes("/OpenSim/RegionModules"))
  58. {
  59. // TODO why does node.Type.isSubclassOf(typeof(ISharedRegionModule)) not work?
  60. if (node.Type.GetInterface(typeof(ISharedRegionModule).ToString()) != null)
  61. {
  62. m_log.DebugFormat("[REGIONMODULES]: Found shared region module {0}, class {1}", node.Id, node.Type);
  63. m_sharedModules.Add(node.Type);
  64. }
  65. else if (node.Type.GetInterface(typeof(INonSharedRegionModule).ToString()) != null)
  66. {
  67. m_log.DebugFormat("[REGIONMODULES]: Found non-shared region module {0}, class {1}", node.Id, node.Type);
  68. m_nonSharedModules.Add(node.Type);
  69. }
  70. else
  71. m_log.DebugFormat("[REGIONMODULES]: Found unknown type of module {0}, class {1}", node.Id, node.Type);
  72. }
  73. // now we've got all the region-module classes loaded, create one instance of every ISharedRegionModule,
  74. // initialize and postinitialize it. This Initialise we are in is called before LoadRegion.PostInitialise
  75. // is called (which loads the regions), so we don't have any regions in the server yet.
  76. foreach (Type type in m_sharedModules)
  77. {
  78. ISharedRegionModule module = (ISharedRegionModule)Activator.CreateInstance(type);
  79. m_sharedInstances.Add(module);
  80. module.Initialise(openSim.ConfigSource.Source);
  81. }
  82. foreach (ISharedRegionModule module in m_sharedInstances)
  83. {
  84. module.PostInitialise();
  85. }
  86. }
  87. public void PostInitialise ()
  88. {
  89. }
  90. #endregion
  91. #region IPlugin implementation
  92. public void Initialise ()
  93. {
  94. throw new System.NotImplementedException();
  95. }
  96. #endregion
  97. #region IDisposable implementation
  98. public void Dispose ()
  99. {
  100. // we expect that all regions have been removed already
  101. while (m_sharedInstances.Count > 0)
  102. {
  103. m_sharedInstances[0].Close();
  104. m_sharedInstances.RemoveAt(0);
  105. }
  106. m_sharedModules.Clear();
  107. m_nonSharedModules.Clear();
  108. }
  109. #endregion
  110. public string Version
  111. {
  112. get
  113. {
  114. return AddinManager.CurrentAddin.Version;
  115. }
  116. }
  117. public string Name
  118. {
  119. get
  120. {
  121. return m_name;
  122. }
  123. }
  124. #region IRegionModulesController implementation
  125. public void AddRegionToModules (Scene scene)
  126. {
  127. foreach (ISharedRegionModule module in m_sharedInstances)
  128. {
  129. m_log.DebugFormat("[REGIONMODULE]: Adding scene {0} to shared module {1}",
  130. scene.RegionInfo.RegionName, module.Name);
  131. module.AddRegion(scene);
  132. scene.AddRegionModule(module.Name, module);
  133. }
  134. List<INonSharedRegionModule> list = new List<INonSharedRegionModule>();
  135. foreach (Type type in m_nonSharedModules)
  136. {
  137. INonSharedRegionModule module = (INonSharedRegionModule)Activator.CreateInstance(type);
  138. m_log.DebugFormat("[REGIONMODULE]: Adding scene {0} to non-shared module {1}",
  139. scene.RegionInfo.RegionName, module.Name);
  140. module.Initialise(m_openSim.ConfigSource.Source);
  141. list.Add(module);
  142. }
  143. foreach (INonSharedRegionModule module in list)
  144. {
  145. module.AddRegion(scene);
  146. scene.AddRegionModule(module.Name, module);
  147. }
  148. // This is needed for all module types. Modules will register
  149. // Interfaces with scene in AddScene, and will also need a means
  150. // to access interfaces registered by other modules. Without
  151. // this extra method, a module attempting to use another modules's
  152. // interface would be successful only depending on load order,
  153. // which can't be depended upon, or modules would need to resort
  154. // to ugly kludges to attempt to request interfaces when needed
  155. // and unneccessary caching logic repeated in all modules.
  156. // The extra function stub is just that much cleaner
  157. //
  158. foreach (ISharedRegionModule module in m_sharedInstances)
  159. {
  160. module.RegionLoaded(scene);
  161. }
  162. foreach (INonSharedRegionModule module in list)
  163. {
  164. module.RegionLoaded(scene);
  165. }
  166. }
  167. public void RemoveRegionFromModules (Scene scene)
  168. {
  169. foreach (IRegionModuleBase module in scene.RegionModules.Values)
  170. {
  171. m_log.DebugFormat("[REGIONMODULE]: Removing scene {0} from module {1}",
  172. scene.RegionInfo.RegionName, module.Name);
  173. module.RemoveRegion(scene);
  174. if (module is INonSharedRegionModule)
  175. {
  176. // as we were the only user, this instance has to die
  177. module.Close();
  178. }
  179. }
  180. scene.RegionModules.Clear();
  181. }
  182. #endregion
  183. }
  184. }