/Aurora/Modules/World/Startup/PhysicsPluginManager.cs

https://bitbucket.org/VirtualReality/software-testing · C# · 128 lines · 79 code · 8 blank · 41 comment · 4 complexity · 76fa5731338db6443a5fc3a2bad9f45a MD5 · raw file

  1. /*
  2. * Copyright (c) Contributors, http://aurora-sim.org/, 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 Aurora-Sim 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 Aurora.Framework;
  28. using Aurora.Framework.ConsoleFramework;
  29. using Aurora.Framework.ModuleLoader;
  30. using Aurora.Framework.Physics;
  31. using Aurora.Framework.SceneInfo;
  32. using Nini.Config;
  33. using System;
  34. using System.Collections.Generic;
  35. namespace Aurora.Modules.Startup
  36. {
  37. /// <summary>
  38. /// Description of MyClass.
  39. /// </summary>
  40. public class PhysicsPluginManager
  41. {
  42. private readonly Dictionary<string, IMeshingPlugin> _MeshPlugins = new Dictionary<string, IMeshingPlugin>();
  43. private readonly Dictionary<string, IPhysicsPlugin> _PhysPlugins = new Dictionary<string, IPhysicsPlugin>();
  44. /// <summary>
  45. /// Get a physics scene for the given physics engine and mesher.
  46. /// </summary>
  47. /// <param name="physEngineName"></param>
  48. /// <param name="meshEngineName"></param>
  49. /// <param name="config"></param>
  50. /// <param name="scene"></param>
  51. /// <returns></returns>
  52. public PhysicsScene GetPhysicsScene(string physEngineName, string meshEngineName, IConfigSource config,
  53. IScene scene)
  54. {
  55. if (String.IsNullOrEmpty(physEngineName))
  56. {
  57. return new NullPhysicsScene();
  58. }
  59. if (String.IsNullOrEmpty(meshEngineName))
  60. {
  61. return new NullPhysicsScene();
  62. }
  63. IMesher meshEngine = null;
  64. if (_MeshPlugins.ContainsKey(meshEngineName))
  65. {
  66. meshEngine = _MeshPlugins[meshEngineName].GetMesher(config);
  67. }
  68. else
  69. {
  70. MainConsole.Instance.WarnFormat("[Physics]: Couldn't find meshing engine: {0}", meshEngineName);
  71. throw new ArgumentException(String.Format("couldn't find meshing engine: {0}", meshEngineName));
  72. }
  73. if (_PhysPlugins.ContainsKey(physEngineName))
  74. {
  75. MainConsole.Instance.Debug("[Physics]: Loading physics engine: " + physEngineName);
  76. PhysicsScene result = _PhysPlugins[physEngineName].GetScene();
  77. result.Initialise(meshEngine, scene);
  78. result.PostInitialise(config);
  79. return result;
  80. }
  81. else
  82. {
  83. MainConsole.Instance.WarnFormat("[Physics]: Couldn't find physics engine: {0}", physEngineName);
  84. throw new ArgumentException(String.Format("couldn't find physics engine: {0}", physEngineName));
  85. }
  86. }
  87. /// <summary>
  88. /// Load all plugins in assemblies at the given path
  89. /// </summary>
  90. /// <param name="assembliesPath"></param>
  91. public void LoadPluginsFromAssemblies(string assembliesPath)
  92. {
  93. List<IPhysicsPlugin> physicsPlugins =
  94. AuroraModuleLoader.LoadModules<IPhysicsPlugin>(assembliesPath);
  95. List<IMeshingPlugin> meshingPlugins =
  96. AuroraModuleLoader.LoadModules<IMeshingPlugin>(assembliesPath);
  97. meshingPlugins.AddRange(AuroraModuleLoader.LoadModules<IMeshingPlugin>(""));
  98. foreach (IPhysicsPlugin plug in physicsPlugins)
  99. {
  100. try
  101. {
  102. _PhysPlugins.Add(plug.GetName(), plug);
  103. }
  104. catch
  105. {
  106. }
  107. }
  108. foreach (IMeshingPlugin plug in meshingPlugins)
  109. {
  110. try
  111. {
  112. _MeshPlugins.Add(plug.GetName(), plug);
  113. }
  114. catch
  115. {
  116. }
  117. }
  118. }
  119. }
  120. }