private void AddPlugin(string FileName) { Assembly pluginAssembly = Assembly.LoadFrom(FileName); foreach (Type pluginType in pluginAssembly.GetTypes()) { if (pluginType.IsPublic) { if (!pluginType.IsAbstract) { Type typeInterface = pluginType.GetInterface("IPhysicsPlugin", true); if (typeInterface != null) { IPhysicsPlugin plug = (IPhysicsPlugin)Activator.CreateInstance(pluginAssembly.GetType(pluginType.ToString())); plug.Init(); this._plugins.Add(plug.GetName(), plug); } typeInterface = null; } } } pluginAssembly = null; }
/// <summary> /// Load plugins from an assembly at the given path /// </summary> /// <param name="assemblyPath"></param> public void LoadPluginsFromAssembly(string assemblyPath) { // TODO / NOTE // The assembly named 'OpenSim.Region.Physics.BasicPhysicsPlugin' was loaded from // 'file:///C:/OpenSim/trunk2/bin/Physics/OpenSim.Region.Physics.BasicPhysicsPlugin.dll' // using the LoadFrom context. The use of this context can result in unexpected behavior // for serialization, casting and dependency resolution. In almost all cases, it is recommended // that the LoadFrom context be avoided. This can be done by installing assemblies in the // Global Assembly Cache or in the ApplicationBase directory and using Assembly. // Load when explicitly loading assemblies. Assembly pluginAssembly = null; Type[] types = null; try { pluginAssembly = Assembly.LoadFrom(assemblyPath); } catch (Exception ex) { m_log.Error("[PHYSICS]: Failed to load plugin from " + assemblyPath, ex); } if (pluginAssembly != null) { try { types = pluginAssembly.GetTypes(); } catch (ReflectionTypeLoadException ex) { m_log.Error("[PHYSICS]: Failed to enumerate types in plugin from " + assemblyPath + ": " + ex.LoaderExceptions[0].Message, ex); } catch (Exception ex) { m_log.Error("[PHYSICS]: Failed to enumerate types in plugin from " + assemblyPath, ex); } if (types != null) { foreach (Type pluginType in types) { if (pluginType.IsPublic) { if (!pluginType.IsAbstract) { Type physTypeInterface = pluginType.GetInterface("IPhysicsPlugin", true); if (physTypeInterface != null) { IPhysicsPlugin plug = (IPhysicsPlugin)Activator.CreateInstance(pluginAssembly.GetType(pluginType.ToString())); plug.Init(); if (!_PhysPlugins.ContainsKey(plug.GetName())) { _PhysPlugins.Add(plug.GetName(), plug); m_log.Info("[PHYSICS]: Added physics engine: " + plug.GetName()); } } Type meshTypeInterface = pluginType.GetInterface("IMeshingPlugin", true); if (meshTypeInterface != null) { IMeshingPlugin plug = (IMeshingPlugin)Activator.CreateInstance(pluginAssembly.GetType(pluginType.ToString())); if (!_MeshPlugins.ContainsKey(plug.GetName())) { _MeshPlugins.Add(plug.GetName(), plug); m_log.Info("[PHYSICS]: Added meshing engine: " + plug.GetName()); } } physTypeInterface = null; meshTypeInterface = null; } } } } } pluginAssembly = null; }