Files
UnrealEngineUWP/Engine/Source/Programs/DotNETCommon/DotNETUtilities/AssemblyUtils.cs
Wes Hunt cefa87e4b7 Ionic.zip code import
* Moving Ionic.Zip source code into UE4 from UE4 source.
* Only one left should be in Binaries/DotNET.
* Moving TPS info to source location.
* Deleting several copies that were floating around.

Assembly Resolve Refactor
* Added AssemblyUtils.InstallAssemblyResolver to handle resolving of known assemblies that may not exist in the same folder as the referencing assembly.
* This is now installed by UAT and UBT, which should handle all needs to load Ionic.Zip and RPCUtility.exe from scripts that install into subfolders of Binaries/DotNET.
* Other assemblies can be added easily as necesary, centralizing the location where this is handled.
* Removed AssemblyResolver from RPCUtilHelper as UBT handles it automatically now.
* Removed Ionic.Zip references from projects that weren't really using it.
#codereview:ben.marsh

[CL 2646891 by Wes Hunt in Main branch]
2015-08-06 15:55:44 -04:00

59 lines
2.6 KiB
C#

// Copyright 1998-2015 Epic Games, Inc. All Rights Reserved.
using System;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
namespace Tools.DotNETCommon
{
public static class AssemblyUtils
{
/// <summary>
/// Gets the original location (path and filename) of an assembly.
/// This method is using Assembly.CodeBase property to properly resolve original
/// assembly path in case shadow copying is enabled.
/// </summary>
/// <returns>Absolute path and filename to the assembly.</returns>
public static string GetOriginalLocation(this Assembly ThisAssembly)
{
return new Uri(ThisAssembly.CodeBase).LocalPath;
}
/// <summary>
/// Version info of the executable which runs this code.
/// </summary>
public static FileVersionInfo ExecutableVersion
{
get
{
return FileVersionInfo.GetVersionInfo(Assembly.GetEntryAssembly().GetOriginalLocation());
}
}
/// <summary>
/// Installs an assembly resolver. Mostly used to get shared assemblies that we don't want copied around to various output locations as happens when "Copy Local" is set to true
/// for an assembly reference (which is the default).
/// </summary>
public static void InstallAssemblyResolver(string PathToBinariesDotNET)
{
AppDomain.CurrentDomain.AssemblyResolve += (sender, args) =>
{
// Name is fully qualified assembly definition - e.g. "p4dn, Version=1.0.0.0, Culture=neutral, PublicKeyToken=ff968dc1933aba6f"
string AssemblyName = args.Name.Split(',')[0];
// Look for known assembly names we check into Binaries/DotNET/. Return null if we can't find it.
return (
from KnownAssemblyName in new[] { "RPCUtility.exe", "Ionic.Zip.Reduced.dll" }
where AssemblyName.Equals(Path.GetFileNameWithoutExtension(KnownAssemblyName), StringComparison.InvariantCultureIgnoreCase)
let ResolvedAssemblyFilename = Path.Combine(PathToBinariesDotNET, KnownAssemblyName)
// check if the file exists first. If we just try to load it, we correctly throw an exception, but it's a generic
// FileNotFoundException, which is not informative. Better to return null.
select File.Exists(ResolvedAssemblyFilename) ? Assembly.LoadFile(ResolvedAssemblyFilename) : null
).FirstOrDefault();
};
}
}
}