Skip to content

Instantly share code, notes, and snippets.

@DevJohnC
Created October 25, 2013 03:30
Show Gist options
  • Select an option

  • Save DevJohnC/7149065 to your computer and use it in GitHub Desktop.

Select an option

Save DevJohnC/7149065 to your computer and use it in GitHub Desktop.
Loading plugins/modules from the filesystem, key differences between Mono and .NET
//
// Author: John Carruthers (johnc@frag-labs.com)
//
// Copyright (C) 2013 John Carruthers
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
using System;
using System.IO;
using System.Collections.Generic;
using System.Reflection;
using FragLabs.AdjutantOS.API.Apps;
using FragLabs.AdjutantOS.API.Log;
using FragLabs.AdjutantOS.API.Modules.Domains;
using FragLabs.AdjutantOS.API.Services;
namespace FragLabs.AdjutantOS.API.Modules
{
/// <summary>
/// Module loader to load modules from assemblies on disk.
/// </summary>
public class FilesystemModuleLoader : DomainShareObject, IModuleLoader
{
/// <summary>
/// Directory to search for modules.
/// </summary>
public string Directory { get; protected set; }
/// <summary>
/// Create a new instance of FilesystemModuleLoader.
/// </summary>
/// <param name="directory">Directory to search for modules.</param>
public FilesystemModuleLoader(string directory)
{
Directory = directory;
}
/// <summary>
/// Gets an array of available modules.
/// </summary>
public IEnumerable<Module> AvailableModules
{
get
{
var dir = new DirectoryInfo(Directory);
if (dir.Exists)
{
foreach (var moduleDir in dir.EnumerateDirectories())
{
if (File.Exists(Path.Combine(moduleDir.FullName, moduleDir.Name + ".dll")))
{
yield return new FilesystemModule(Path.Combine(moduleDir.FullName, moduleDir.Name + ".dll"), moduleDir.Name, this);
}
}
}
}
}
public void Load(Module module)
{
if (module == null) throw new ArgumentNullException("module");
if (module.Loader == null) throw new ArgumentException("Module load not set to an instance of an object");
if (!(module is FilesystemModule)) throw new ArgumentException("Module must be a FilesystemModule");
var apiVersion = ModuleManager.Current.InstalledApiVersion;
if (!apiVersion.Equals(module.ApiVersion))
throw new ApiVersionException(apiVersion, module.ApiVersion);
var fsModule = module as FilesystemModule;
Logger.Write(LogLevel.Debug, "Creating appdomain for new module");
var fi = new FileInfo(fsModule.Filename);
var path = fi.Directory.FullName;
var niceName = fi.Name;
if (niceName.EndsWith(fi.Extension))
niceName = niceName.Substring(0, niceName.Length - fi.Extension.Length);
var evidence = new System.Security.Policy.Evidence(AppDomain.CurrentDomain.Evidence);
var setup = AppDomain.CurrentDomain.SetupInformation;
if (Environment.IsRunningOnMono)
{
// mono needs to load the common references from the same filesystem location
// adding the modules's path to PrivateBinPath seems to resolve module specific references fine
setup.PrivateBinPath = String.Format ("{1};{0}", path, AppDomain.CurrentDomain.BaseDirectory);
setup.ApplicationBase = AppDomain.CurrentDomain.BaseDirectory;
}
else
{
// .NET needs to load everything from the module's directory, .NET doesn't like
// loading assemblies anywhere but the ApplicationBase
setup.PrivateBinPath = String.Format ("{0};{1}", path, AppDomain.CurrentDomain.BaseDirectory);
setup.ApplicationBase = path;
File.Copy(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "API.dll"), Path.Combine(path, "API.dll"), true);
if (File.Exists(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "UI.dll")))
File.Copy(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "UI.dll"), Path.Combine(path, "UI.dll"), true);
}
var appDomain = AppDomain.CreateDomain("Module::" + niceName, evidence, setup);
var remoteController = (DomainController)appDomain.CreateInstanceAndUnwrap(typeof(DomainController).Assembly.FullName, typeof(DomainController).FullName,
true, BindingFlags.Default, null, new object[] { DomainManager.Current }, null, null);
remoteController.Initialize(DomainManager.Current);
module.DomainController = remoteController;
remoteController.LoadAssembly(fsModule.Filename);
Logger.Write(LogLevel.Debug, "Installing apps in module \"{0}\"", module.Name);
var apps = remoteController.FindTypesThatImplement(typeof(IApp));
AppHost.Current.Add(apps);
Logger.Write(LogLevel.Debug, "Installing services in module \"{0}\"", module.Name);
var services = remoteController.FindTypesThatImplement(typeof(IService<>));
ServiceHost.Current.Add(services);
}
}
/// <summary>
/// Module on disk.
/// </summary>
public class FilesystemModule : Module
{
/// <summary>
/// Module filename.
/// </summary>
public string Filename { get; set; }
public FilesystemModule(string filename, string name, IModuleLoader loader) : base(name, loader)
{
Filename = filename;
ApiVersion = GetApiVersion(new FileInfo(filename).Directory.FullName);
}
static Version GetApiVersion(string modulePath)
{
var apiFile = Path.Combine(modulePath, "API.dll");
if (File.Exists(apiFile))
{
var an = AssemblyName.GetAssemblyName(apiFile);
return an.Version;
}
return null;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment