PhysicsPluginManager.cs 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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 Nini.Config;
  32. using log4net;
  33. using OpenSim.Framework;
  34. namespace OpenSim.Region.Physics.Manager
  35. {
  36. /// <summary>
  37. /// Description of MyClass.
  38. /// </summary>
  39. public class PhysicsPluginManager
  40. {
  41. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  42. private Dictionary<string, IPhysicsPlugin> _PhysPlugins = new Dictionary<string, IPhysicsPlugin>();
  43. private Dictionary<string, IMeshingPlugin> _MeshPlugins = new Dictionary<string, IMeshingPlugin>();
  44. /// <summary>
  45. /// Constructor.
  46. /// </summary>
  47. public PhysicsPluginManager()
  48. {
  49. // Load "plugins", that are hard coded and not existing in form of an external lib, and hence always
  50. // available
  51. IMeshingPlugin plugHard;
  52. plugHard = new ZeroMesherPlugin();
  53. _MeshPlugins.Add(plugHard.GetName(), plugHard);
  54. m_log.Info("[PHYSICS]: Added meshing engine: " + plugHard.GetName());
  55. }
  56. /// <summary>
  57. /// Get a physics scene for the given physics engine and mesher.
  58. /// </summary>
  59. /// <param name="physEngineName"></param>
  60. /// <param name="meshEngineName"></param>
  61. /// <param name="config"></param>
  62. /// <returns></returns>
  63. public PhysicsScene GetPhysicsScene(string physEngineName, string meshEngineName, IConfigSource config, string regionName)
  64. {
  65. if (String.IsNullOrEmpty(physEngineName))
  66. {
  67. return PhysicsScene.Null;
  68. }
  69. if (String.IsNullOrEmpty(meshEngineName))
  70. {
  71. return PhysicsScene.Null;
  72. }
  73. IMesher meshEngine = null;
  74. if (_MeshPlugins.ContainsKey(meshEngineName))
  75. {
  76. m_log.Info("[PHYSICS]: creating meshing engine " + meshEngineName);
  77. meshEngine = _MeshPlugins[meshEngineName].GetMesher(config);
  78. }
  79. else
  80. {
  81. m_log.WarnFormat("[PHYSICS]: couldn't find meshingEngine: {0}", meshEngineName);
  82. throw new ArgumentException(String.Format("couldn't find meshingEngine: {0}", meshEngineName));
  83. }
  84. if (_PhysPlugins.ContainsKey(physEngineName))
  85. {
  86. m_log.Info("[PHYSICS]: creating " + physEngineName);
  87. PhysicsScene result = _PhysPlugins[physEngineName].GetScene(regionName);
  88. result.Initialise(meshEngine, config);
  89. return result;
  90. }
  91. else
  92. {
  93. m_log.WarnFormat("[PHYSICS]: couldn't find physicsEngine: {0}", physEngineName);
  94. throw new ArgumentException(String.Format("couldn't find physicsEngine: {0}", physEngineName));
  95. }
  96. }
  97. /// <summary>
  98. /// Load all plugins in assemblies at the given path
  99. /// </summary>
  100. /// <param name="pluginsPath"></param>
  101. public void LoadPluginsFromAssemblies(string assembliesPath)
  102. {
  103. // Walk all assemblies (DLLs effectively) and see if they are home
  104. // of a plugin that is of interest for us
  105. string[] pluginFiles = Directory.GetFiles(assembliesPath, "*.dll");
  106. for (int i = 0; i < pluginFiles.Length; i++)
  107. {
  108. LoadPluginsFromAssembly(pluginFiles[i]);
  109. }
  110. }
  111. /// <summary>
  112. /// Load plugins from an assembly at the given path
  113. /// </summary>
  114. /// <param name="assemblyPath"></param>
  115. public void LoadPluginsFromAssembly(string assemblyPath)
  116. {
  117. // TODO / NOTE
  118. // The assembly named 'OpenSim.Region.Physics.BasicPhysicsPlugin' was loaded from
  119. // 'file:///C:/OpenSim/trunk2/bin/Physics/OpenSim.Region.Physics.BasicPhysicsPlugin.dll'
  120. // using the LoadFrom context. The use of this context can result in unexpected behavior
  121. // for serialization, casting and dependency resolution. In almost all cases, it is recommended
  122. // that the LoadFrom context be avoided. This can be done by installing assemblies in the
  123. // Global Assembly Cache or in the ApplicationBase directory and using Assembly.
  124. // Load when explicitly loading assemblies.
  125. Assembly pluginAssembly = null;
  126. Type[] types = null;
  127. try
  128. {
  129. pluginAssembly = Assembly.LoadFrom(assemblyPath);
  130. }
  131. catch (Exception ex)
  132. {
  133. m_log.Error("[PHYSICS]: Failed to load plugin from " + assemblyPath, ex);
  134. }
  135. if (pluginAssembly != null)
  136. {
  137. try
  138. {
  139. types = pluginAssembly.GetTypes();
  140. }
  141. catch (ReflectionTypeLoadException ex)
  142. {
  143. m_log.Error("[PHYSICS]: Failed to enumerate types in plugin from " + assemblyPath + ": " +
  144. ex.LoaderExceptions[0].Message, ex);
  145. }
  146. catch (Exception ex)
  147. {
  148. m_log.Error("[PHYSICS]: Failed to enumerate types in plugin from " + assemblyPath, ex);
  149. }
  150. if (types != null)
  151. {
  152. foreach (Type pluginType in types)
  153. {
  154. if (pluginType.IsPublic)
  155. {
  156. if (!pluginType.IsAbstract)
  157. {
  158. Type physTypeInterface = pluginType.GetInterface("IPhysicsPlugin", true);
  159. if (physTypeInterface != null)
  160. {
  161. IPhysicsPlugin plug =
  162. (IPhysicsPlugin)Activator.CreateInstance(pluginAssembly.GetType(pluginType.ToString()));
  163. plug.Init();
  164. if (!_PhysPlugins.ContainsKey(plug.GetName()))
  165. {
  166. _PhysPlugins.Add(plug.GetName(), plug);
  167. m_log.Info("[PHYSICS]: Added physics engine: " + plug.GetName());
  168. }
  169. }
  170. Type meshTypeInterface = pluginType.GetInterface("IMeshingPlugin", true);
  171. if (meshTypeInterface != null)
  172. {
  173. IMeshingPlugin plug =
  174. (IMeshingPlugin)Activator.CreateInstance(pluginAssembly.GetType(pluginType.ToString()));
  175. if (!_MeshPlugins.ContainsKey(plug.GetName()))
  176. {
  177. _MeshPlugins.Add(plug.GetName(), plug);
  178. m_log.Info("[PHYSICS]: Added meshing engine: " + plug.GetName());
  179. }
  180. }
  181. physTypeInterface = null;
  182. meshTypeInterface = null;
  183. }
  184. }
  185. }
  186. }
  187. }
  188. pluginAssembly = null;
  189. }
  190. //---
  191. public static void PhysicsPluginMessage(string message, bool isWarning)
  192. {
  193. if (isWarning)
  194. {
  195. m_log.Warn("[PHYSICS]: " + message);
  196. }
  197. else
  198. {
  199. m_log.Info("[PHYSICS]: " + message);
  200. }
  201. }
  202. //---
  203. }
  204. public interface IPhysicsPlugin
  205. {
  206. bool Init();
  207. PhysicsScene GetScene(String sceneIdentifier);
  208. string GetName();
  209. void Dispose();
  210. }
  211. public interface IMeshingPlugin
  212. {
  213. string GetName();
  214. IMesher GetMesher(IConfigSource config);
  215. }
  216. }