PageRenderTime 17ms CodeModel.GetById 13ms app.highlight 2ms RepoModel.GetById 1ms app.codeStats 0ms

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