RegionModulesControllerPlugin.cs 20 KB

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