1
0

RegionModulesControllerPlugin.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489
  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_log.DebugFormat("[REGIONMODULES]: Initializing...");
  61. m_openSim = openSim;
  62. openSim.ApplicationRegistry.RegisterInterface<IRegionModulesController>(this);
  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. openSim.ConfigSource.Source.Configs["Modules"];
  74. if (modulesConfig == null)
  75. modulesConfig = 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(openSim.ConfigSource.Source);
  173. }
  174. // Immediately run PostInitialise on shared modules
  175. foreach (ISharedRegionModule module in m_sharedInstances)
  176. {
  177. module.PostInitialise();
  178. }
  179. }
  180. public void PostInitialise ()
  181. {
  182. }
  183. #endregion
  184. #region IPlugin implementation
  185. // We don't do that here
  186. //
  187. public void Initialise ()
  188. {
  189. throw new System.NotImplementedException();
  190. }
  191. #endregion
  192. #region IDisposable implementation
  193. // Cleanup
  194. //
  195. public void Dispose ()
  196. {
  197. // We expect that all regions have been removed already
  198. while (m_sharedInstances.Count > 0)
  199. {
  200. m_sharedInstances[0].Close();
  201. m_sharedInstances.RemoveAt(0);
  202. }
  203. m_sharedModules.Clear();
  204. m_nonSharedModules.Clear();
  205. }
  206. #endregion
  207. public string Version
  208. {
  209. get
  210. {
  211. return AddinManager.CurrentAddin.Version;
  212. }
  213. }
  214. public string Name
  215. {
  216. get
  217. {
  218. return m_name;
  219. }
  220. }
  221. #region IRegionModulesController implementation
  222. // The root of all evil.
  223. // This is where we handle adding the modules to scenes when they
  224. // load. This means that here we deal with replaceable interfaces,
  225. // nonshared modules, etc.
  226. //
  227. public void AddRegionToModules (Scene scene)
  228. {
  229. Dictionary<Type, ISharedRegionModule> deferredSharedModules =
  230. new Dictionary<Type, ISharedRegionModule>();
  231. Dictionary<Type, INonSharedRegionModule> deferredNonSharedModules =
  232. new Dictionary<Type, INonSharedRegionModule>();
  233. // We need this to see if a module has already been loaded and
  234. // has defined a replaceable interface. It's a generic call,
  235. // so this can't be used directly. It will be used later
  236. Type s = scene.GetType();
  237. MethodInfo mi = s.GetMethod("RequestModuleInterface");
  238. // This will hold the shared modules we actually load
  239. List<ISharedRegionModule> sharedlist =
  240. new List<ISharedRegionModule>();
  241. // Iterate over the shared modules that have been loaded
  242. // Add them to the new Scene
  243. foreach (ISharedRegionModule module in m_sharedInstances)
  244. {
  245. // Here is where we check if a replaceable interface
  246. // is defined. If it is, the module is checked against
  247. // the interfaces already defined. If the interface is
  248. // defined, we simply skip the module. Else, if the module
  249. // defines a replaceable interface, we add it to the deferred
  250. // list.
  251. Type replaceableInterface = module.ReplaceableInterface;
  252. if (replaceableInterface != null)
  253. {
  254. MethodInfo mii = mi.MakeGenericMethod(replaceableInterface);
  255. if (mii.Invoke(scene, new object[0]) != null)
  256. {
  257. m_log.DebugFormat("[REGIONMODULE]: Not loading {0} because another module has registered {1}", module.Name, replaceableInterface.ToString());
  258. continue;
  259. }
  260. deferredSharedModules[replaceableInterface] = module;
  261. m_log.DebugFormat("[REGIONMODULE]: Deferred load of {0}", module.Name);
  262. continue;
  263. }
  264. m_log.DebugFormat("[REGIONMODULE]: Adding scene {0} to shared module {1}",
  265. scene.RegionInfo.RegionName, module.Name);
  266. module.AddRegion(scene);
  267. scene.AddRegionModule(module.Name, module);
  268. sharedlist.Add(module);
  269. }
  270. IConfig modulesConfig =
  271. m_openSim.ConfigSource.Source.Configs["Modules"];
  272. // Scan for, and load, nonshared modules
  273. List<INonSharedRegionModule> list = new List<INonSharedRegionModule>();
  274. foreach (TypeExtensionNode node in m_nonSharedModules)
  275. {
  276. Object[] ctorArgs = new Object[] {0};
  277. // Read the config
  278. string moduleString =
  279. modulesConfig.GetString("Setup_" + node.Id, String.Empty);
  280. // Get the port number, if there is one
  281. if (moduleString != String.Empty)
  282. {
  283. // Get the port number from the string
  284. string[] moduleParts = moduleString.Split(new char[] {'/'},
  285. 2);
  286. if (moduleParts.Length > 1)
  287. ctorArgs[0] = Convert.ToUInt32(moduleParts[0]);
  288. }
  289. // Actually load it
  290. INonSharedRegionModule module = null;
  291. try
  292. {
  293. module = (INonSharedRegionModule)Activator.CreateInstance(node.Type, ctorArgs);
  294. }
  295. catch
  296. {
  297. module = (INonSharedRegionModule)Activator.CreateInstance(node.Type);
  298. }
  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. }