PhysicsManager.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /*
  2. * Copyright (c) OpenSim project, http://sim.opensecondlife.org/
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions are met:
  6. * * Redistributions of source code must retain the above copyright
  7. * notice, this list of conditions and the following disclaimer.
  8. * * Redistributions in binary form must reproduce the above copyright
  9. * notice, this list of conditions and the following disclaimer in the
  10. * documentation and/or other materials provided with the distribution.
  11. * * Neither the name of the <organization> nor the
  12. * names of its contributors may be used to endorse or promote products
  13. * derived from this software without specific prior written permission.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY <copyright holder> ``AS IS'' AND ANY
  16. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  17. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL <copyright holder> BE LIABLE FOR ANY
  19. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  20. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  21. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  22. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  23. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  24. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. *
  26. */
  27. using System;
  28. using System.Collections.Generic;
  29. using System.Collections;
  30. using System.IO;
  31. using System.Reflection;
  32. using Axiom.MathLib;
  33. namespace OpenSim.Physics.Manager
  34. {
  35. /// <summary>
  36. /// Description of MyClass.
  37. /// </summary>
  38. public class PhysicsManager
  39. {
  40. private Dictionary<string, IPhysicsPlugin> _plugins=new Dictionary<string, IPhysicsPlugin>();
  41. public PhysicsManager()
  42. {
  43. }
  44. public PhysicsScene GetPhysicsScene(string engineName)
  45. {
  46. if (String.IsNullOrEmpty(engineName))
  47. {
  48. return new NullPhysicsScene();
  49. }
  50. if(_plugins.ContainsKey(engineName))
  51. {
  52. OpenSim.Framework.Console.MainConsole.Instance.WriteLine("creating "+engineName);
  53. return _plugins[engineName].GetScene();
  54. }
  55. else
  56. {
  57. string error = String.Format("couldn't find physicsEngine: {0}", engineName);
  58. OpenSim.Framework.Console.MainConsole.Instance.WriteLine(error);
  59. throw new ArgumentException(error);
  60. }
  61. }
  62. public void LoadPlugins()
  63. {
  64. string path = Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory ,"Physics");
  65. string[] pluginFiles = Directory.GetFiles(path, "*.dll");
  66. for(int i= 0; i<pluginFiles.Length; i++)
  67. {
  68. this.AddPlugin(pluginFiles[i]);
  69. }
  70. }
  71. private void AddPlugin(string FileName)
  72. {
  73. Assembly pluginAssembly = Assembly.LoadFrom(FileName);
  74. foreach (Type pluginType in pluginAssembly.GetTypes())
  75. {
  76. if (pluginType.IsPublic)
  77. {
  78. if (!pluginType.IsAbstract)
  79. {
  80. Type typeInterface = pluginType.GetInterface("IPhysicsPlugin", true);
  81. if (typeInterface != null)
  82. {
  83. IPhysicsPlugin plug = (IPhysicsPlugin)Activator.CreateInstance(pluginAssembly.GetType(pluginType.ToString()));
  84. plug.Init();
  85. this._plugins.Add(plug.GetName(),plug);
  86. }
  87. typeInterface = null;
  88. }
  89. }
  90. }
  91. pluginAssembly = null;
  92. }
  93. }
  94. public interface IPhysicsPlugin
  95. {
  96. bool Init();
  97. PhysicsScene GetScene();
  98. string GetName();
  99. void Dispose();
  100. }
  101. }