RegionModulesControllerPlugin.cs 20 KB

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