PhysicsPluginManager.cs 9.7 KB

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