RegionModulesControllerPlugin.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484
  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 Nini.Config;
  33. using OpenSim;
  34. using OpenSim.Region.Framework.Interfaces;
  35. using OpenSim.Region.Framework.Scenes;
  36. namespace OpenSim.ApplicationPlugins.RegionModulesController
  37. {
  38. public class RegionModulesControllerPlugin : IRegionModulesController,
  39. IApplicationPlugin
  40. {
  41. // Logger
  42. private static readonly ILog m_log =
  43. LogManager.GetLogger(
  44. MethodBase.GetCurrentMethod().DeclaringType);
  45. // Config access
  46. private OpenSimBase m_openSim;
  47. // Our name
  48. private string m_name;
  49. // Internal lists to collect information about modules present
  50. private List<TypeExtensionNode> m_nonSharedModules =
  51. new List<TypeExtensionNode>();
  52. private List<TypeExtensionNode> m_sharedModules =
  53. new List<TypeExtensionNode>();
  54. // List of shared module instances, for adding to Scenes
  55. private List<ISharedRegionModule> m_sharedInstances =
  56. new List<ISharedRegionModule>();
  57. #region IApplicationPlugin implementation
  58. public void Initialise (OpenSimBase openSim)
  59. {
  60. m_openSim = openSim;
  61. m_openSim.ApplicationRegistry.RegisterInterface<IRegionModulesController>(this);
  62. m_log.DebugFormat("[REGIONMODULES]: Initializing...");
  63. // Who we are
  64. string id = AddinManager.CurrentAddin.Id;
  65. // Make friendly name
  66. int pos = id.LastIndexOf(".");
  67. if (pos == -1)
  68. m_name = id;
  69. else
  70. m_name = id.Substring(pos + 1);
  71. // The [Modules] section in the ini file
  72. IConfig modulesConfig =
  73. m_openSim.ConfigSource.Source.Configs["Modules"];
  74. if (modulesConfig == null)
  75. modulesConfig = m_openSim.ConfigSource.Source.AddConfig("Modules");
  76. // Scan modules and load all that aren't disabled
  77. foreach (TypeExtensionNode node in
  78. AddinManager.GetExtensionNodes("/OpenSim/RegionModules"))
  79. {
  80. if (node.Type.GetInterface(typeof(ISharedRegionModule).ToString()) != null)
  81. {
  82. if (CheckModuleEnabled(node, modulesConfig))
  83. {
  84. m_log.DebugFormat("[REGIONMODULES]: Found shared region module {0}, class {1}", node.Id, node.Type);
  85. m_sharedModules.Add(node);
  86. }
  87. }
  88. else if (node.Type.GetInterface(typeof(INonSharedRegionModule).ToString()) != null)
  89. {
  90. if (CheckModuleEnabled(node, modulesConfig))
  91. {
  92. m_log.DebugFormat("[REGIONMODULES]: Found non-shared region module {0}, class {1}", node.Id, node.Type);
  93. m_nonSharedModules.Add(node);
  94. }
  95. }
  96. else
  97. {
  98. m_log.DebugFormat("[REGIONMODULES]: Found unknown type of module {0}, class {1}", node.Id, node.Type);
  99. }
  100. }
  101. // Load and init the module. We try a constructor with a port
  102. // if a port was given, fall back to one without if there is
  103. // no port or the more specific constructor fails.
  104. // This will be removed, so that any module capable of using a port
  105. // must provide a constructor with a port in the future.
  106. // For now, we do this so migration is easy.
  107. //
  108. foreach (TypeExtensionNode node in m_sharedModules)
  109. {
  110. Object[] ctorArgs = new Object[] { (uint)0 };
  111. // Read the config again
  112. string moduleString =
  113. modulesConfig.GetString("Setup_" + node.Id, String.Empty);
  114. // Get the port number, if there is one
  115. if (moduleString != String.Empty)
  116. {
  117. // Get the port number from the string
  118. string[] moduleParts = moduleString.Split(new char[] { '/' },
  119. 2);
  120. if (moduleParts.Length > 1)
  121. ctorArgs[0] = Convert.ToUInt32(moduleParts[0]);
  122. }
  123. // Try loading and initilaizing the module, using the
  124. // port if appropriate
  125. ISharedRegionModule module = null;
  126. try
  127. {
  128. module = (ISharedRegionModule)Activator.CreateInstance(
  129. node.Type, ctorArgs);
  130. }
  131. catch
  132. {
  133. module = (ISharedRegionModule)Activator.CreateInstance(
  134. node.Type);
  135. }
  136. // OK, we're up and running
  137. m_sharedInstances.Add(module);
  138. module.Initialise(m_openSim.ConfigSource.Source);
  139. }
  140. }
  141. public void PostInitialise ()
  142. {
  143. m_log.DebugFormat("[REGIONMODULES]: PostInitializing...");
  144. // Immediately run PostInitialise on shared modules
  145. foreach (ISharedRegionModule module in m_sharedInstances)
  146. {
  147. module.PostInitialise();
  148. }
  149. }
  150. #endregion
  151. #region IPlugin implementation
  152. // We don't do that here
  153. //
  154. public void Initialise ()
  155. {
  156. throw new System.NotImplementedException();
  157. }
  158. #endregion
  159. #region IDisposable implementation
  160. // Cleanup
  161. //
  162. public void Dispose ()
  163. {
  164. // We expect that all regions have been removed already
  165. while (m_sharedInstances.Count > 0)
  166. {
  167. m_sharedInstances[0].Close();
  168. m_sharedInstances.RemoveAt(0);
  169. }
  170. m_sharedModules.Clear();
  171. m_nonSharedModules.Clear();
  172. }
  173. #endregion
  174. public string Version
  175. {
  176. get
  177. {
  178. return AddinManager.CurrentAddin.Version;
  179. }
  180. }
  181. public string Name
  182. {
  183. get
  184. {
  185. return m_name;
  186. }
  187. }
  188. #region Region Module interfacesController implementation
  189. /// <summary>
  190. /// Check that the given module is no disabled in the [Modules] section of the config files.
  191. /// </summary>
  192. /// <param name="node"></param>
  193. /// <param name="modulesConfig">The config section</param>
  194. /// <returns>true if the module is enabled, false if it is disabled</returns>
  195. protected bool CheckModuleEnabled(TypeExtensionNode node, IConfig modulesConfig)
  196. {
  197. // Get the config string
  198. string moduleString =
  199. modulesConfig.GetString("Setup_" + node.Id, String.Empty);
  200. // We have a selector
  201. if (moduleString != String.Empty)
  202. {
  203. // Allow disabling modules even if they don't have
  204. // support for it
  205. if (moduleString == "disabled")
  206. return false;
  207. // Split off port, if present
  208. string[] moduleParts = moduleString.Split(new char[] { '/' }, 2);
  209. // Format is [port/][class]
  210. string className = moduleParts[0];
  211. if (moduleParts.Length > 1)
  212. className = moduleParts[1];
  213. // Match the class name if given
  214. if (className != String.Empty &&
  215. node.Type.ToString() != className)
  216. return false;
  217. }
  218. return true;
  219. }
  220. // The root of all evil.
  221. // This is where we handle adding the modules to scenes when they
  222. // load. This means that here we deal with replaceable interfaces,
  223. // nonshared modules, etc.
  224. //
  225. public void AddRegionToModules (Scene scene)
  226. {
  227. Dictionary<Type, ISharedRegionModule> deferredSharedModules =
  228. new Dictionary<Type, ISharedRegionModule>();
  229. Dictionary<Type, INonSharedRegionModule> deferredNonSharedModules =
  230. new Dictionary<Type, INonSharedRegionModule>();
  231. // We need this to see if a module has already been loaded and
  232. // has defined a replaceable interface. It's a generic call,
  233. // so this can't be used directly. It will be used later
  234. Type s = scene.GetType();
  235. MethodInfo mi = s.GetMethod("RequestModuleInterface");
  236. // This will hold the shared modules we actually load
  237. List<ISharedRegionModule> sharedlist =
  238. new List<ISharedRegionModule>();
  239. // Iterate over the shared modules that have been loaded
  240. // Add them to the new Scene
  241. foreach (ISharedRegionModule module in m_sharedInstances)
  242. {
  243. // Here is where we check if a replaceable interface
  244. // is defined. If it is, the module is checked against
  245. // the interfaces already defined. If the interface is
  246. // defined, we simply skip the module. Else, if the module
  247. // defines a replaceable interface, we add it to the deferred
  248. // list.
  249. Type replaceableInterface = module.ReplaceableInterface;
  250. if (replaceableInterface != null)
  251. {
  252. MethodInfo mii = mi.MakeGenericMethod(replaceableInterface);
  253. if (mii.Invoke(scene, new object[0]) != null)
  254. {
  255. m_log.DebugFormat("[REGIONMODULE]: Not loading {0} because another module has registered {1}", module.Name, replaceableInterface.ToString());
  256. continue;
  257. }
  258. deferredSharedModules[replaceableInterface] = module;
  259. m_log.DebugFormat("[REGIONMODULE]: Deferred load of {0}", module.Name);
  260. continue;
  261. }
  262. m_log.DebugFormat("[REGIONMODULE]: Adding scene {0} to shared module {1}",
  263. scene.RegionInfo.RegionName, module.Name);
  264. module.AddRegion(scene);
  265. scene.AddRegionModule(module.Name, module);
  266. sharedlist.Add(module);
  267. }
  268. IConfig modulesConfig =
  269. m_openSim.ConfigSource.Source.Configs["Modules"];
  270. // Scan for, and load, nonshared modules
  271. List<INonSharedRegionModule> list = new List<INonSharedRegionModule>();
  272. foreach (TypeExtensionNode node in m_nonSharedModules)
  273. {
  274. Object[] ctorArgs = new Object[] {0};
  275. // Read the config
  276. string moduleString =
  277. modulesConfig.GetString("Setup_" + node.Id, String.Empty);
  278. // Get the port number, if there is one
  279. if (moduleString != String.Empty)
  280. {
  281. // Get the port number from the string
  282. string[] moduleParts = moduleString.Split(new char[] {'/'},
  283. 2);
  284. if (moduleParts.Length > 1)
  285. ctorArgs[0] = Convert.ToUInt32(moduleParts[0]);
  286. }
  287. // Actually load it
  288. INonSharedRegionModule module = null;
  289. Type[] ctorParamTypes = new Type[ctorArgs.Length];
  290. for (int i = 0; i < ctorParamTypes.Length; i++)
  291. ctorParamTypes[i] = ctorArgs[i].GetType();
  292. if (node.Type.GetConstructor(ctorParamTypes) != null)
  293. module = (INonSharedRegionModule)Activator.CreateInstance(node.Type, ctorArgs);
  294. else
  295. module = (INonSharedRegionModule)Activator.CreateInstance(node.Type);
  296. // Check for replaceable interfaces
  297. Type replaceableInterface = module.ReplaceableInterface;
  298. if (replaceableInterface != null)
  299. {
  300. MethodInfo mii = mi.MakeGenericMethod(replaceableInterface);
  301. if (mii.Invoke(scene, new object[0]) != null)
  302. {
  303. m_log.DebugFormat("[REGIONMODULE]: Not loading {0} because another module has registered {1}", module.Name, replaceableInterface.ToString());
  304. continue;
  305. }
  306. deferredNonSharedModules[replaceableInterface] = module;
  307. m_log.DebugFormat("[REGIONMODULE]: Deferred load of {0}", module.Name);
  308. continue;
  309. }
  310. m_log.DebugFormat("[REGIONMODULE]: Adding scene {0} to non-shared module {1}",
  311. scene.RegionInfo.RegionName, module.Name);
  312. // Initialise the module
  313. module.Initialise(m_openSim.ConfigSource.Source);
  314. list.Add(module);
  315. }
  316. // Now add the modules that we found to the scene. If a module
  317. // wishes to override a replaceable interface, it needs to
  318. // register it in Initialise, so that the deferred module
  319. // won't load.
  320. foreach (INonSharedRegionModule module in list)
  321. {
  322. module.AddRegion(scene);
  323. scene.AddRegionModule(module.Name, module);
  324. }
  325. // Now all modules without a replaceable base interface are loaded
  326. // Replaceable modules have either been skipped, or omitted.
  327. // Now scan the deferred modules here
  328. foreach (ISharedRegionModule module in deferredSharedModules.Values)
  329. {
  330. // Determine if the interface has been replaced
  331. Type replaceableInterface = module.ReplaceableInterface;
  332. MethodInfo mii = mi.MakeGenericMethod(replaceableInterface);
  333. if (mii.Invoke(scene, new object[0]) != null)
  334. {
  335. m_log.DebugFormat("[REGIONMODULE]: Not loading {0} because another module has registered {1}", module.Name, replaceableInterface.ToString());
  336. continue;
  337. }
  338. m_log.DebugFormat("[REGIONMODULE]: Adding scene {0} to shared module {1} (deferred)",
  339. scene.RegionInfo.RegionName, module.Name);
  340. // Not replaced, load the module
  341. module.AddRegion(scene);
  342. scene.AddRegionModule(module.Name, module);
  343. sharedlist.Add(module);
  344. }
  345. // Same thing for nonshared modules, load them unless overridden
  346. List<INonSharedRegionModule> deferredlist =
  347. new List<INonSharedRegionModule>();
  348. foreach (INonSharedRegionModule module in deferredNonSharedModules.Values)
  349. {
  350. // Check interface override
  351. Type replaceableInterface = module.ReplaceableInterface;
  352. if (replaceableInterface != null)
  353. {
  354. MethodInfo mii = mi.MakeGenericMethod(replaceableInterface);
  355. if (mii.Invoke(scene, new object[0]) != null)
  356. {
  357. m_log.DebugFormat("[REGIONMODULE]: Not loading {0} because another module has registered {1}", module.Name, replaceableInterface.ToString());
  358. continue;
  359. }
  360. }
  361. m_log.DebugFormat("[REGIONMODULE]: Adding scene {0} to non-shared module {1} (deferred)",
  362. scene.RegionInfo.RegionName, module.Name);
  363. module.Initialise(m_openSim.ConfigSource.Source);
  364. list.Add(module);
  365. deferredlist.Add(module);
  366. }
  367. // Finally, load valid deferred modules
  368. foreach (INonSharedRegionModule module in deferredlist)
  369. {
  370. module.AddRegion(scene);
  371. scene.AddRegionModule(module.Name, module);
  372. }
  373. // This is needed for all module types. Modules will register
  374. // Interfaces with scene in AddScene, and will also need a means
  375. // to access interfaces registered by other modules. Without
  376. // this extra method, a module attempting to use another modules's
  377. // interface would be successful only depending on load order,
  378. // which can't be depended upon, or modules would need to resort
  379. // to ugly kludges to attempt to request interfaces when needed
  380. // and unneccessary caching logic repeated in all modules.
  381. // The extra function stub is just that much cleaner
  382. //
  383. foreach (ISharedRegionModule module in sharedlist)
  384. {
  385. module.RegionLoaded(scene);
  386. }
  387. foreach (INonSharedRegionModule module in list)
  388. {
  389. module.RegionLoaded(scene);
  390. }
  391. }
  392. public void RemoveRegionFromModules (Scene scene)
  393. {
  394. foreach (IRegionModuleBase module in scene.RegionModules.Values)
  395. {
  396. m_log.DebugFormat("[REGIONMODULE]: Removing scene {0} from module {1}",
  397. scene.RegionInfo.RegionName, module.Name);
  398. module.RemoveRegion(scene);
  399. if (module is INonSharedRegionModule)
  400. {
  401. // as we were the only user, this instance has to die
  402. module.Close();
  403. }
  404. }
  405. scene.RegionModules.Clear();
  406. }
  407. #endregion
  408. }
  409. }