ModuleLoader.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  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 OpenSim 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.Environment.Interfaces;
  34. using OpenSim.Region.Environment.Modules.Agent.AssetTransaction;
  35. using OpenSim.Region.Environment.Modules.Avatar.Chat;
  36. using OpenSim.Region.Environment.Modules.Avatar.InstantMessage;
  37. using OpenSim.Region.Environment.Modules.Scripting.DynamicTexture;
  38. using OpenSim.Region.Environment.Modules.Scripting.LoadImageURL;
  39. using OpenSim.Region.Environment.Modules.Scripting.XMLRPC;
  40. using OpenSim.Region.Environment.Scenes;
  41. //TODO: Why do we reference InstantMessageModule here?
  42. namespace OpenSim.Region.Environment
  43. {
  44. public class ModuleLoader
  45. {
  46. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  47. public Dictionary<string, Assembly> LoadedAssemblys = new Dictionary<string, Assembly>();
  48. private readonly List<IRegionModule> m_loadedModules = new List<IRegionModule>();
  49. private readonly Dictionary<string, IRegionModule> m_loadedSharedModules = new Dictionary<string, IRegionModule>();
  50. private readonly IConfigSource m_config;
  51. public ModuleLoader(IConfigSource config)
  52. {
  53. m_config = config;
  54. }
  55. public IRegionModule[] GetLoadedSharedModules
  56. {
  57. get
  58. {
  59. IRegionModule[] regionModules = new IRegionModule[m_loadedSharedModules.Count];
  60. m_loadedSharedModules.Values.CopyTo(regionModules, 0);
  61. return regionModules;
  62. }
  63. }
  64. public List<IRegionModule> PickupModules(Scene scene, string moduleDir)
  65. {
  66. DirectoryInfo dir = new DirectoryInfo(moduleDir);
  67. List<IRegionModule> modules = new List<IRegionModule>();
  68. foreach (FileInfo fileInfo in dir.GetFiles("*.dll"))
  69. {
  70. modules.AddRange(LoadRegionModules(fileInfo.FullName, scene));
  71. }
  72. return modules;
  73. }
  74. public void LoadDefaultSharedModules()
  75. {
  76. DynamicTextureModule dynamicModule = new DynamicTextureModule();
  77. if (m_loadedSharedModules.ContainsKey(dynamicModule.Name))
  78. {
  79. m_log.ErrorFormat("[MODULES]: Module name \"{0}\" already exists in module list. Module type {1} not added!", dynamicModule.Name, "DynamicTextureModule");
  80. }
  81. else
  82. {
  83. m_loadedSharedModules.Add(dynamicModule.Name, dynamicModule);
  84. }
  85. // ChatModule chat = new ChatModule();
  86. // if (m_loadedSharedModules.ContainsKey(chat.Name))
  87. // {
  88. // m_log.ErrorFormat("[MODULES]: Module name \"{0}\" already exists in module list. Module type {1} not added!", chat.Name, "ChatModule");
  89. // }
  90. // else
  91. // {
  92. // m_loadedSharedModules.Add(chat.Name, chat);
  93. // }
  94. InstantMessageModule imMod = new InstantMessageModule();
  95. if (m_loadedSharedModules.ContainsKey(imMod.Name))
  96. {
  97. m_log.ErrorFormat("[MODULES]: Module name \"{0}\" already exists in module list. Module type {1} not added!", imMod.Name, "InstantMessageModule");
  98. }
  99. else
  100. {
  101. m_loadedSharedModules.Add(imMod.Name, imMod);
  102. }
  103. LoadImageURLModule loadMod = new LoadImageURLModule();
  104. if (m_loadedSharedModules.ContainsKey(loadMod.Name))
  105. {
  106. m_log.ErrorFormat("[MODULES]: Module name \"{0}\" already exists in module list. Module type {1} not added!", loadMod.Name, "LoadImageURLModule");
  107. }
  108. else
  109. {
  110. m_loadedSharedModules.Add(loadMod.Name, loadMod);
  111. }
  112. // AvatarFactoryModule avatarFactory = new AvatarFactoryModule();
  113. // if (m_loadedSharedModules.ContainsKey(avatarFactory.Name))
  114. // {
  115. // m_log.ErrorFormat("[MODULES]: Module name \"{0}\" already exists in module list. Module type {1} not added!", avatarFactory.Name, "AvarFactoryModule");
  116. // }
  117. // else
  118. // {
  119. // m_loadedSharedModules.Add(avatarFactory.Name, avatarFactory);
  120. // }
  121. XMLRPCModule xmlRpcMod = new XMLRPCModule();
  122. if (m_loadedSharedModules.ContainsKey(xmlRpcMod.Name))
  123. {
  124. m_log.ErrorFormat("[MODULES]: Module name \"{0}\" already exists in module list. Module type {1} not added!", xmlRpcMod.Name, "XMLRPCModule");
  125. }
  126. else
  127. {
  128. m_loadedSharedModules.Add(xmlRpcMod.Name, xmlRpcMod);
  129. }
  130. //TextureDownloadModule textureModule = new TextureDownloadModule();
  131. //LoadedSharedModules.Add(textureModule.Name, textureModule);
  132. AssetTransactionModule transactionsModule = new AssetTransactionModule();
  133. if (m_loadedSharedModules.ContainsKey(transactionsModule.Name))
  134. {
  135. m_log.ErrorFormat("[MODULES]: Module name \"{0}\" already exists in module list. Module type {1} not added!", transactionsModule.Name, "TransactionModule");
  136. }
  137. else
  138. {
  139. m_loadedSharedModules.Add(transactionsModule.Name, transactionsModule);
  140. }
  141. }
  142. public void InitialiseSharedModules(Scene scene)
  143. {
  144. foreach (IRegionModule module in m_loadedSharedModules.Values)
  145. {
  146. module.Initialise(scene, m_config);
  147. scene.AddModule(module.Name, module); //should be doing this?
  148. }
  149. }
  150. public void InitializeModule(IRegionModule module, Scene scene)
  151. {
  152. module.Initialise(scene, m_config);
  153. scene.AddModule(module.Name, module);
  154. m_loadedModules.Add(module);
  155. }
  156. /// <summary>
  157. /// Loads/initialises a Module instance that can be used by multiple Regions
  158. /// </summary>
  159. /// <param name="dllName"></param>
  160. /// <param name="moduleName"></param>
  161. public void LoadSharedModule(string dllName, string moduleName)
  162. {
  163. IRegionModule module = LoadModule(dllName, moduleName);
  164. if (module != null)
  165. LoadSharedModule(module);
  166. }
  167. /// <summary>
  168. /// Loads/initialises a Module instance that can be used by multiple Regions
  169. /// </summary>
  170. /// <param name="module"></param>
  171. public void LoadSharedModule(IRegionModule module)
  172. {
  173. if (!m_loadedSharedModules.ContainsKey(module.Name))
  174. {
  175. m_loadedSharedModules.Add(module.Name, module);
  176. }
  177. }
  178. public List<IRegionModule> LoadRegionModules(string dllName, Scene scene)
  179. {
  180. IRegionModule[] modules = LoadModules(dllName);
  181. List<IRegionModule> initializedModules = new List<IRegionModule>();
  182. if (modules.Length > 0)
  183. {
  184. m_log.InfoFormat("[MODULES]: Found Module Library [{0}]", dllName);
  185. foreach (IRegionModule module in modules)
  186. {
  187. if (!module.IsSharedModule)
  188. {
  189. m_log.InfoFormat("[MODULES]: [{0}]: Initializing.", module.Name);
  190. InitializeModule(module, scene);
  191. initializedModules.Add(module);
  192. }
  193. else
  194. {
  195. m_log.InfoFormat("[MODULES]: [{0}]: Loading Shared Module.", module.Name);
  196. LoadSharedModule(module);
  197. }
  198. }
  199. }
  200. return initializedModules;
  201. }
  202. public void LoadRegionModule(string dllName, string moduleName, Scene scene)
  203. {
  204. IRegionModule module = LoadModule(dllName, moduleName);
  205. if (module != null)
  206. {
  207. InitializeModule(module, scene);
  208. }
  209. }
  210. /// <summary>
  211. /// Loads a external Module (if not already loaded) and creates a new instance of it.
  212. /// </summary>
  213. /// <param name="dllName"></param>
  214. /// <param name="moduleName"></param>
  215. public IRegionModule LoadModule(string dllName, string moduleName)
  216. {
  217. IRegionModule[] modules = LoadModules(dllName);
  218. foreach (IRegionModule module in modules)
  219. {
  220. if ((module != null) && (module.Name == moduleName))
  221. {
  222. return module;
  223. }
  224. }
  225. return null;
  226. }
  227. public IRegionModule[] LoadModules(string dllName)
  228. {
  229. List<IRegionModule> modules = new List<IRegionModule>();
  230. Assembly pluginAssembly;
  231. if (!LoadedAssemblys.TryGetValue(dllName, out pluginAssembly))
  232. {
  233. try
  234. {
  235. pluginAssembly = Assembly.LoadFrom(dllName);
  236. LoadedAssemblys.Add(dllName, pluginAssembly);
  237. }
  238. catch (BadImageFormatException)
  239. {
  240. //m_log.InfoFormat("[MODULES]: The file [{0}] is not a module assembly.", e.FileName);
  241. }
  242. }
  243. if (pluginAssembly != null)
  244. {
  245. try
  246. {
  247. foreach (Type pluginType in pluginAssembly.GetTypes())
  248. {
  249. if (pluginType.IsPublic)
  250. {
  251. if (!pluginType.IsAbstract)
  252. {
  253. if (pluginType.GetInterface("IRegionModule") != null)
  254. {
  255. modules.Add((IRegionModule)Activator.CreateInstance(pluginType));
  256. }
  257. }
  258. }
  259. }
  260. }
  261. catch (ReflectionTypeLoadException)
  262. {
  263. m_log.InfoFormat("[MODULES]: Could not load types for [{0}].", pluginAssembly.FullName);
  264. }
  265. }
  266. return modules.ToArray();
  267. }
  268. public void PostInitialise()
  269. {
  270. foreach (IRegionModule module in m_loadedSharedModules.Values)
  271. {
  272. module.PostInitialise();
  273. }
  274. foreach (IRegionModule module in m_loadedModules)
  275. {
  276. module.PostInitialise();
  277. }
  278. }
  279. public void ClearCache()
  280. {
  281. LoadedAssemblys.Clear();
  282. }
  283. public void UnloadModule(IRegionModule rm)
  284. {
  285. rm.Close();
  286. m_loadedModules.Remove(rm);
  287. }
  288. }
  289. }