RegionModulesControllerPlugin.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  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 OpenSimulator 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. Dictionary<Type, ISharedRegionModule> deferredSharedModules =
  128. new Dictionary<Type, ISharedRegionModule>();
  129. Dictionary<Type, INonSharedRegionModule> deferredNonSharedModules =
  130. new Dictionary<Type, INonSharedRegionModule>();
  131. Type s = scene.GetType();
  132. MethodInfo mi = s.GetMethod("RequestModuleInterface");
  133. List<ISharedRegionModule> sharedlist = new List<ISharedRegionModule>();
  134. foreach (ISharedRegionModule module in m_sharedInstances)
  135. {
  136. Type replaceableInterface = module.ReplaceableInterface;
  137. if (replaceableInterface != null)
  138. {
  139. MethodInfo mii = mi.MakeGenericMethod(replaceableInterface);
  140. if (mii.Invoke(scene, new object[0]) != null)
  141. {
  142. m_log.DebugFormat("[REGIONMODULE]: Not loading {0} because another module has registered {1}", module.Name, replaceableInterface.ToString());
  143. continue;
  144. }
  145. deferredSharedModules[replaceableInterface] = module;
  146. m_log.DebugFormat("[REGIONMODULE]: Deferred load of {0}", module.Name);
  147. continue;
  148. }
  149. m_log.DebugFormat("[REGIONMODULE]: Adding scene {0} to shared module {1}",
  150. scene.RegionInfo.RegionName, module.Name);
  151. module.AddRegion(scene);
  152. scene.AddRegionModule(module.Name, module);
  153. sharedlist.Add(module);
  154. }
  155. List<INonSharedRegionModule> list = new List<INonSharedRegionModule>();
  156. foreach (Type type in m_nonSharedModules)
  157. {
  158. INonSharedRegionModule module = (INonSharedRegionModule)Activator.CreateInstance(type);
  159. Type replaceableInterface = module.ReplaceableInterface;
  160. if (replaceableInterface != null)
  161. {
  162. MethodInfo mii = mi.MakeGenericMethod(replaceableInterface);
  163. if (mii.Invoke(scene, new object[0]) != null)
  164. {
  165. m_log.DebugFormat("[REGIONMODULE]: Not loading {0} because another module has registered {1}", module.Name, replaceableInterface.ToString());
  166. continue;
  167. }
  168. deferredNonSharedModules[replaceableInterface] = module;
  169. m_log.DebugFormat("[REGIONMODULE]: Deferred load of {0}", module.Name);
  170. continue;
  171. }
  172. m_log.DebugFormat("[REGIONMODULE]: Adding scene {0} to non-shared module {1}",
  173. scene.RegionInfo.RegionName, module.Name);
  174. module.Initialise(m_openSim.ConfigSource.Source);
  175. list.Add(module);
  176. }
  177. foreach (INonSharedRegionModule module in list)
  178. {
  179. module.AddRegion(scene);
  180. scene.AddRegionModule(module.Name, module);
  181. }
  182. // Now all modules without a replaceable base interface are loaded
  183. // Replaceable modules have either been skipped, or omitted.
  184. // Now scan the deferred modules here
  185. foreach (ISharedRegionModule module in deferredSharedModules.Values)
  186. {
  187. Type replaceableInterface = module.ReplaceableInterface;
  188. MethodInfo mii = mi.MakeGenericMethod(replaceableInterface);
  189. if (mii.Invoke(scene, new object[0]) != null)
  190. {
  191. m_log.DebugFormat("[REGIONMODULE]: Not loading {0} because another module has registered {1}", module.Name, replaceableInterface.ToString());
  192. continue;
  193. }
  194. m_log.DebugFormat("[REGIONMODULE]: Adding scene {0} to shared module {1} (deferred)",
  195. scene.RegionInfo.RegionName, module.Name);
  196. module.AddRegion(scene);
  197. scene.AddRegionModule(module.Name, module);
  198. sharedlist.Add(module);
  199. }
  200. List<INonSharedRegionModule> deferredlist = new List<INonSharedRegionModule>();
  201. foreach (INonSharedRegionModule module in deferredNonSharedModules.Values)
  202. {
  203. Type replaceableInterface = module.ReplaceableInterface;
  204. if (replaceableInterface != null)
  205. {
  206. MethodInfo mii = mi.MakeGenericMethod(replaceableInterface);
  207. if (mii.Invoke(scene, new object[0]) != null)
  208. {
  209. m_log.DebugFormat("[REGIONMODULE]: Not loading {0} because another module has registered {1}", module.Name, replaceableInterface.ToString());
  210. continue;
  211. }
  212. }
  213. m_log.DebugFormat("[REGIONMODULE]: Adding scene {0} to non-shared module {1} (deferred)",
  214. scene.RegionInfo.RegionName, module.Name);
  215. module.Initialise(m_openSim.ConfigSource.Source);
  216. list.Add(module);
  217. deferredlist.Add(module);
  218. }
  219. foreach (INonSharedRegionModule module in deferredlist)
  220. {
  221. module.AddRegion(scene);
  222. scene.AddRegionModule(module.Name, module);
  223. }
  224. // This is needed for all module types. Modules will register
  225. // Interfaces with scene in AddScene, and will also need a means
  226. // to access interfaces registered by other modules. Without
  227. // this extra method, a module attempting to use another modules's
  228. // interface would be successful only depending on load order,
  229. // which can't be depended upon, or modules would need to resort
  230. // to ugly kludges to attempt to request interfaces when needed
  231. // and unneccessary caching logic repeated in all modules.
  232. // The extra function stub is just that much cleaner
  233. //
  234. foreach (ISharedRegionModule module in sharedlist)
  235. {
  236. module.RegionLoaded(scene);
  237. }
  238. foreach (INonSharedRegionModule module in list)
  239. {
  240. module.RegionLoaded(scene);
  241. }
  242. }
  243. public void RemoveRegionFromModules (Scene scene)
  244. {
  245. foreach (IRegionModuleBase module in scene.RegionModules.Values)
  246. {
  247. m_log.DebugFormat("[REGIONMODULE]: Removing scene {0} from module {1}",
  248. scene.RegionInfo.RegionName, module.Name);
  249. module.RemoveRegion(scene);
  250. if (module is INonSharedRegionModule)
  251. {
  252. // as we were the only user, this instance has to die
  253. module.Close();
  254. }
  255. }
  256. scene.RegionModules.Clear();
  257. }
  258. #endregion
  259. }
  260. }