PhysicsPluginManager.cs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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. namespace OpenSim.Region.Physics.Manager
  33. {
  34. /// <summary>
  35. /// Description of MyClass.
  36. /// </summary>
  37. public class PhysicsPluginManager
  38. {
  39. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  40. private Dictionary<string, IPhysicsPlugin> _PhysPlugins = new Dictionary<string, IPhysicsPlugin>();
  41. private Dictionary<string, IMeshingPlugin> _MeshPlugins = new Dictionary<string, IMeshingPlugin>();
  42. public PhysicsPluginManager()
  43. {
  44. }
  45. public PhysicsScene GetPhysicsScene(string physEngineName, string meshEngineName)
  46. {
  47. if (String.IsNullOrEmpty(physEngineName))
  48. {
  49. return PhysicsScene.Null;
  50. }
  51. if (String.IsNullOrEmpty(meshEngineName))
  52. {
  53. return PhysicsScene.Null;
  54. }
  55. IMesher meshEngine = null;
  56. if (_MeshPlugins.ContainsKey(meshEngineName))
  57. {
  58. m_log.Info("[PHYSICS]: creating meshing engine " + meshEngineName);
  59. meshEngine = _MeshPlugins[meshEngineName].GetMesher();
  60. }
  61. else
  62. {
  63. m_log.WarnFormat("[PHYSICS]: couldn't find meshingEngine: {0}", meshEngineName);
  64. throw new ArgumentException(String.Format("couldn't find meshingEngine: {0}", meshEngineName));
  65. }
  66. if (_PhysPlugins.ContainsKey(physEngineName))
  67. {
  68. m_log.Info("[PHYSICS]: creating " + physEngineName);
  69. PhysicsScene result = _PhysPlugins[physEngineName].GetScene();
  70. result.Initialise(meshEngine);
  71. return result;
  72. }
  73. else
  74. {
  75. m_log.WarnFormat("[PHYSICS]: couldn't find physicsEngine: {0}", physEngineName);
  76. throw new ArgumentException(String.Format("couldn't find physicsEngine: {0}", physEngineName));
  77. }
  78. }
  79. public void LoadPlugins()
  80. {
  81. // Load "plugins", that are hard coded and not existing in form of an external lib
  82. IMeshingPlugin plugHard;
  83. plugHard = new ZeroMesherPlugin();
  84. _MeshPlugins.Add(plugHard.GetName(), plugHard);
  85. m_log.Info("[PHYSICS]: Added meshing engine: " + plugHard.GetName());
  86. // And now walk all assemblies (DLLs effectively) and see if they are home
  87. // of a plugin that is of interest for us
  88. string path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Physics");
  89. string[] pluginFiles = Directory.GetFiles(path, "*.dll");
  90. for (int i = 0; i < pluginFiles.Length; i++)
  91. {
  92. AddPlugin(pluginFiles[i]);
  93. }
  94. }
  95. private void AddPlugin(string FileName)
  96. {
  97. // TODO / NOTE
  98. // The assembly named 'OpenSim.Region.Physics.BasicPhysicsPlugin' was loaded from
  99. // 'file:///C:/OpenSim/trunk2/bin/Physics/OpenSim.Region.Physics.BasicPhysicsPlugin.dll'
  100. // using the LoadFrom context. The use of this context can result in unexpected behavior
  101. // for serialization, casting and dependency resolution. In almost all cases, it is recommended
  102. // that the LoadFrom context be avoided. This can be done by installing assemblies in the
  103. // Global Assembly Cache or in the ApplicationBase directory and using Assembly.
  104. // Load when explicitly loading assemblies.
  105. Assembly pluginAssembly = Assembly.LoadFrom(FileName);
  106. foreach (Type pluginType in pluginAssembly.GetTypes())
  107. {
  108. if (pluginType.IsPublic)
  109. {
  110. if (!pluginType.IsAbstract)
  111. {
  112. Type physTypeInterface = pluginType.GetInterface("IPhysicsPlugin", true);
  113. if (physTypeInterface != null)
  114. {
  115. IPhysicsPlugin plug =
  116. (IPhysicsPlugin) Activator.CreateInstance(pluginAssembly.GetType(pluginType.ToString()));
  117. plug.Init();
  118. _PhysPlugins.Add(plug.GetName(), plug);
  119. m_log.Info("[PHYSICS]: Added physics engine: " + plug.GetName());
  120. }
  121. Type meshTypeInterface = pluginType.GetInterface("IMeshingPlugin", true);
  122. if (meshTypeInterface != null)
  123. {
  124. IMeshingPlugin plug =
  125. (IMeshingPlugin) Activator.CreateInstance(pluginAssembly.GetType(pluginType.ToString()));
  126. _MeshPlugins.Add(plug.GetName(), plug);
  127. m_log.Info("[PHYSICS]: Added meshing engine: " + plug.GetName());
  128. }
  129. physTypeInterface = null;
  130. meshTypeInterface = null;
  131. }
  132. }
  133. }
  134. pluginAssembly = null;
  135. }
  136. //---
  137. public static void PhysicsPluginMessage(string message, bool isWarning)
  138. {
  139. if (isWarning)
  140. {
  141. m_log.Warn("[PHYSICS]: " + message);
  142. }
  143. else
  144. {
  145. m_log.Info("[PHYSICS]: " + message);
  146. }
  147. }
  148. //---
  149. }
  150. public interface IPhysicsPlugin
  151. {
  152. bool Init();
  153. PhysicsScene GetScene();
  154. string GetName();
  155. void Dispose();
  156. }
  157. public interface IMeshingPlugin
  158. {
  159. string GetName();
  160. IMesher GetMesher();
  161. }
  162. }