1
0

ModuleLoader.cs 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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.IO;
  30. using System.Reflection;
  31. using log4net;
  32. using Nini.Config;
  33. using OpenSim.Region.Framework.Interfaces;
  34. using OpenSim.Region.Framework.Scenes;
  35. namespace OpenSim.Region.Framework
  36. {
  37. public class ModuleLoader
  38. {
  39. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  40. public Dictionary<string, Assembly> LoadedAssemblys = new Dictionary<string, Assembly>();
  41. private readonly List<IRegionModule> m_loadedModules = new List<IRegionModule>();
  42. private readonly Dictionary<string, IRegionModule> m_loadedSharedModules = new Dictionary<string, IRegionModule>();
  43. private readonly IConfigSource m_config;
  44. public ModuleLoader(IConfigSource config)
  45. {
  46. m_config = config;
  47. }
  48. public IRegionModule[] GetLoadedSharedModules
  49. {
  50. get
  51. {
  52. IRegionModule[] regionModules = new IRegionModule[m_loadedSharedModules.Count];
  53. m_loadedSharedModules.Values.CopyTo(regionModules, 0);
  54. return regionModules;
  55. }
  56. }
  57. public List<IRegionModule> PickupModules(Scene scene, string moduleDir)
  58. {
  59. DirectoryInfo dir = new DirectoryInfo(moduleDir);
  60. List<IRegionModule> modules = new List<IRegionModule>();
  61. foreach (FileInfo fileInfo in dir.GetFiles("*.dll"))
  62. {
  63. modules.AddRange(LoadRegionModules(fileInfo.FullName, scene));
  64. }
  65. return modules;
  66. }
  67. public void LoadDefaultSharedModule(IRegionModule module)
  68. {
  69. if (m_loadedSharedModules.ContainsKey(module.Name))
  70. {
  71. m_log.ErrorFormat("[MODULES]: Module name \"{0}\" already exists in module list. Module not added!", module.Name);
  72. }
  73. else
  74. {
  75. m_loadedSharedModules.Add(module.Name, module);
  76. }
  77. }
  78. public void InitialiseSharedModules(Scene scene)
  79. {
  80. foreach (IRegionModule module in m_loadedSharedModules.Values)
  81. {
  82. module.Initialise(scene, m_config);
  83. scene.AddModule(module.Name, module); //should be doing this?
  84. }
  85. }
  86. public void InitializeModule(IRegionModule module, Scene scene)
  87. {
  88. module.Initialise(scene, m_config);
  89. scene.AddModule(module.Name, module);
  90. m_loadedModules.Add(module);
  91. }
  92. /// <summary>
  93. /// Loads/initialises a Module instance that can be used by multiple Regions
  94. /// </summary>
  95. /// <param name="dllName"></param>
  96. /// <param name="moduleName"></param>
  97. public void LoadSharedModule(string dllName, string moduleName)
  98. {
  99. IRegionModule module = LoadModule(dllName, moduleName);
  100. if (module != null)
  101. LoadSharedModule(module);
  102. }
  103. /// <summary>
  104. /// Loads/initialises a Module instance that can be used by multiple Regions
  105. /// </summary>
  106. /// <param name="module"></param>
  107. public void LoadSharedModule(IRegionModule module)
  108. {
  109. if (!m_loadedSharedModules.ContainsKey(module.Name))
  110. {
  111. m_loadedSharedModules.Add(module.Name, module);
  112. }
  113. }
  114. public List<IRegionModule> LoadRegionModules(string dllName, Scene scene)
  115. {
  116. IRegionModule[] modules = LoadModules(dllName);
  117. List<IRegionModule> initializedModules = new List<IRegionModule>();
  118. if (modules.Length > 0)
  119. {
  120. m_log.InfoFormat("[MODULES]: Found Module Library [{0}]", dllName);
  121. foreach (IRegionModule module in modules)
  122. {
  123. if (!module.IsSharedModule)
  124. {
  125. m_log.InfoFormat("[MODULES]: [{0}]: Initializing.", module.Name);
  126. InitializeModule(module, scene);
  127. initializedModules.Add(module);
  128. }
  129. else
  130. {
  131. m_log.InfoFormat("[MODULES]: [{0}]: Loading Shared Module.", module.Name);
  132. LoadSharedModule(module);
  133. }
  134. }
  135. }
  136. return initializedModules;
  137. }
  138. public void LoadRegionModule(string dllName, string moduleName, Scene scene)
  139. {
  140. IRegionModule module = LoadModule(dllName, moduleName);
  141. if (module != null)
  142. {
  143. InitializeModule(module, scene);
  144. }
  145. }
  146. /// <summary>
  147. /// Loads a external Module (if not already loaded) and creates a new instance of it.
  148. /// </summary>
  149. /// <param name="dllName"></param>
  150. /// <param name="moduleName"></param>
  151. public IRegionModule LoadModule(string dllName, string moduleName)
  152. {
  153. IRegionModule[] modules = LoadModules(dllName);
  154. foreach (IRegionModule module in modules)
  155. {
  156. if ((module != null) && (module.Name == moduleName))
  157. {
  158. return module;
  159. }
  160. }
  161. return null;
  162. }
  163. public IRegionModule[] LoadModules(string dllName)
  164. {
  165. //m_log.DebugFormat("[MODULES]: Looking for modules in {0}", dllName);
  166. List<IRegionModule> modules = new List<IRegionModule>();
  167. Assembly pluginAssembly;
  168. if (!LoadedAssemblys.TryGetValue(dllName, out pluginAssembly))
  169. {
  170. try
  171. {
  172. pluginAssembly = Assembly.LoadFrom(dllName);
  173. LoadedAssemblys.Add(dllName, pluginAssembly);
  174. }
  175. catch (BadImageFormatException)
  176. {
  177. //m_log.InfoFormat("[MODULES]: The file [{0}] is not a module assembly.", e.FileName);
  178. }
  179. }
  180. if (pluginAssembly != null)
  181. {
  182. try
  183. {
  184. foreach (Type pluginType in pluginAssembly.GetTypes())
  185. {
  186. if (pluginType.IsPublic)
  187. {
  188. if (!pluginType.IsAbstract)
  189. {
  190. if (pluginType.GetInterface("IRegionModule") != null)
  191. {
  192. modules.Add((IRegionModule)Activator.CreateInstance(pluginType));
  193. }
  194. }
  195. }
  196. }
  197. }
  198. catch (Exception e)
  199. {
  200. m_log.ErrorFormat(
  201. "[MODULES]: Could not load types for [{0}]. Exception {1}", pluginAssembly.FullName, e);
  202. // justincc: Right now this is fatal to really get the user's attention
  203. throw e;
  204. }
  205. }
  206. return modules.ToArray();
  207. }
  208. public void PostInitialise()
  209. {
  210. foreach (IRegionModule module in m_loadedSharedModules.Values)
  211. {
  212. module.PostInitialise();
  213. }
  214. foreach (IRegionModule module in m_loadedModules)
  215. {
  216. module.PostInitialise();
  217. }
  218. }
  219. public void ClearCache()
  220. {
  221. LoadedAssemblys.Clear();
  222. }
  223. public void UnloadModule(IRegionModule rm)
  224. {
  225. rm.Close();
  226. m_loadedModules.Remove(rm);
  227. }
  228. }
  229. }