using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using Tap.Shared;
namespace OpenTap
{
///
/// Representation of an assembly including its dependencies. Part of the object model used in the PluginManager
///
[DebuggerDisplay("{Name} ({Location})")]
public class AssemblyData : ITypeDataSource
{
private static readonly TraceSource log = Log.CreateSource("AssemblyData");
///
/// The name of the assembly. This is the same as the filename without extension
///
public string Name { get; internal set; }
///
/// The file from which this assembly can be loaded. The information contained in this AssemblyData object comes from this file.
///
public string Location { get; }
/// Gets the attributes of this .net assembly.
public IEnumerable Attributes => Load()?.GetCustomAttributes() ?? Enumerable.Empty();
IEnumerable ITypeDataSource.Types => PluginTypes;
///
/// decorating assembly, if included
///
public PluginAssemblyAttribute PluginAssemblyAttribute { get; internal set; }
///
/// A list of Assemblies that this Assembly references.
///
public IEnumerable References { get; internal set; }
IEnumerable ITypeDataSource.References => References;
List pluginTypes;
///
/// Gets a list of plugin types that this Assembly defines
///
public IEnumerable PluginTypes => pluginTypes;
internal void AddPluginType(TypeData typename)
{
if (typename == null)
return;
if (pluginTypes == null)
pluginTypes = new List();
pluginTypes.Add(typename);
}
/// The loaded state of the assembly.
internal LoadStatus Status => assembly != null ? LoadStatus.Loaded : (failedLoad ? LoadStatus.FailedToLoad : LoadStatus.NotLoaded);
///
/// Gets the version of this Assembly. This will return null if the version cannot be parsed.
///
public Version Version { get; internal set; } = null;
// NoSemanticVersion - marker version instead of null to show that no version has been parsed. Null is a valid value for version.
static readonly SemanticVersion NoSemanticVersion = new SemanticVersion(-1, 0, 0, "", "invalidversion");
SemanticVersion semanticVersion = NoSemanticVersion;
///
/// Gets the version of this Assembly as a . Will return null if the version is not well formatted.
///
public SemanticVersion SemanticVersion
{
get
{
if (ReferenceEquals(semanticVersion, NoSemanticVersion))
{
if (SemanticVersion.TryParse(RawVersion, out var version))
semanticVersion = version;
else if (Version != null)
semanticVersion = new SemanticVersion(Version.Major, Version.Minor, Version.Revision, null, null);
else
semanticVersion = null;
}
return semanticVersion;
}
}
string ITypeDataSource.Version => RawVersion;
/// Raw version as set by the assembly.
internal string RawVersion { get; set; }
internal AssemblyData(string location, Assembly preloadedAssembly = null)
{
Location = location;
this.preloadedAssembly = preloadedAssembly;
}
/// Optionally set for preloaded assemblies.
readonly Assembly preloadedAssembly;
Assembly assembly;
bool failedLoad;
/// Gets the assembly without loading it.
internal Assembly GetCached() => assembly ?? preloadedAssembly;
/// Check if the assembly is loaded.
internal bool IsLoaded() => (assembly ?? preloadedAssembly) != null;
///
/// Returns the System.Reflection.Assembly corresponding to this.
/// If the assembly has not yet been loaded, this call will load it.
///
public Assembly Load()
{
if (failedLoad)
return null;
if (assembly == null)
{
try
{
var watch = Stopwatch.StartNew();
if (preloadedAssembly != null)
assembly = preloadedAssembly;
else
{
var _asm = AppDomain.CurrentDomain.GetAssemblies()
.FirstOrDefault(asm => !asm.IsDynamic && !string.IsNullOrWhiteSpace(asm.Location) && PathUtils.AreEqual(asm.Location, this.Location));
assembly = _asm;
}
if (assembly == null)
{
if (this.Name == "OpenTap")
{
assembly = typeof(PluginSearcher).Assembly;
}
else
{
assembly = Assembly.LoadFrom(Path.GetFullPath(this.Location));
}
}
try
{
// Find attribute
if (PluginAssemblyAttribute != null && PluginAssemblyAttribute.PluginInitMethod != null)
{
string fullName = PluginAssemblyAttribute.PluginInitMethod;
// Break into namespace, class, and method name
string[] names = fullName.Split('.');
if (names.Count() < 3)
throw new Exception($"Could not find method {fullName} in assembly: {Location}");
string methodName = names.Last();
string className = names.ElementAt(names.Count() - 2);
string namespacePath = string.Join(".", names.Take(names.Count() - 2));
Type initClass = assembly.GetType($"{namespacePath}.{className}");
// Check if loaded class exists and is static (abstract and sealed) and is public
if (initClass == null || !initClass.IsClass || !initClass.IsAbstract || !initClass.IsSealed || !initClass.IsPublic)
throw new Exception($"Could not find method {fullName} in assembly: {Location}");
MethodInfo initMethod = initClass.GetMethod(methodName);
// Check if loaded method exists and is static and returns void and is public
if (initMethod == null || !initMethod.IsStatic || initMethod.ReturnType != typeof(void) || !initMethod.IsPublic)
throw new Exception($"Could not find method {fullName} in assembly: {Location}");
// Invoke the method and unwrap the InnerException to get meaningful error message
try
{
initMethod.Invoke(null, null);
}
catch (TargetInvocationException exc)
{
throw exc.InnerException;
}
}
}
catch (Exception ex)
{
failedLoad = true;
assembly = null;
log.Error($"Failed to load plugins from {this.Location}");
log.Debug(ex);
return null;
}
log.Debug(watch, "Loaded {0}.", this.Name);
}
catch (SystemException ex)
{
failedLoad = true;
StringBuilder sb = new StringBuilder(String.Format("Failed to load plugins from {0}", this.Location));
bool addedZoneInfo = false;
try
{
var zonetype = Type.GetType("System.Security.Policy.Zone");
if (zonetype != null)
{
// Hack to support .net core without having to build separate assemblies.
dynamic zone = zonetype.GetMethod("CreateFromUrl").Invoke(null, new object[] { this.Location });
var sec = zone.SecurityZone.ToString();
if (sec.Contains("Internet") || sec.Contains("Untrusted"))
{
// The file is in an NTFS Windows operating system blocked state
sb.Append(" The file came from another computer and might be blocked to help protect this computer. Please unblock the file in Windows.");
addedZoneInfo = true;
}
}
}
catch (Exception e)
{
log.Error("Failed to check Security policy for file.");
log.Debug(e);
addedZoneInfo = true;
}
if (!addedZoneInfo)
sb.Append(" Error: " + ex.Message);
log.Error(sb.ToString());
log.Debug(ex);
}
}
return assembly;
}
/// Returns name and version as a string.
public override string ToString() => $"{Name}, {RawVersion}";
}
}