fix(weaver): fix #706 find system dlls (#729)

* added asmresolver back in

* disambiguate Assembly symbol

* add cache for pipeline asms

* add back precomp refs from pipeline
This commit is contained in:
c6burns 2019-04-06 14:02:14 -06:00 committed by vis2k
parent 247c4b7deb
commit 53be9b6d99
2 changed files with 27 additions and 11 deletions

View File

@ -6,7 +6,8 @@
using UnityEditor; using UnityEditor;
using UnityEditor.Compilation; using UnityEditor.Compilation;
using UnityEngine; using UnityEngine;
using Assembly = System.Reflection.Assembly; using DotNetAssembly = System.Reflection.Assembly;
using UnityAssembly = UnityEditor.Compilation.Assembly;
namespace Mirror.Weaver namespace Mirror.Weaver
{ {
@ -15,6 +16,8 @@ public static class CompilationFinishedHook
const string MirrorRuntimeAssemblyName = "Mirror"; const string MirrorRuntimeAssemblyName = "Mirror";
const string MirrorWeaverAssemblyName = "Mirror.Weaver"; const string MirrorWeaverAssemblyName = "Mirror.Weaver";
private static UnityAssembly[] _cachedAssemblies;
public static Action<string> OnWeaverMessage; // delegate for subscription to Weaver debug messages public static Action<string> OnWeaverMessage; // delegate for subscription to Weaver debug messages
public static Action<string> OnWeaverWarning; // delegate for subscription to Weaver warning messages public static Action<string> OnWeaverWarning; // delegate for subscription to Weaver warning messages
public static Action<string> OnWeaverError; // delete for subscription to Weaver error messages public static Action<string> OnWeaverError; // delete for subscription to Weaver error messages
@ -47,14 +50,15 @@ static void HandleError(string msg)
[InitializeOnLoadMethod] [InitializeOnLoadMethod]
static void OnInitializeOnLoad() static void OnInitializeOnLoad()
{ {
// pipeline assemblies are valid until the next call to OnInitializeOnLoad
_cachedAssemblies = CompilationPipeline.GetAssemblies();
CompilationPipeline.assemblyCompilationFinished += OnCompilationFinished; CompilationPipeline.assemblyCompilationFinished += OnCompilationFinished;
} }
static string FindMirrorRuntime() static string FindMirrorRuntime()
{ {
UnityEditor.Compilation.Assembly[] assemblies = CompilationPipeline.GetAssemblies(); foreach (UnityAssembly assembly in _cachedAssemblies)
foreach (UnityEditor.Compilation.Assembly assembly in assemblies)
{ {
if (assembly.name == MirrorRuntimeAssemblyName) if (assembly.name == MirrorRuntimeAssemblyName)
{ {
@ -70,16 +74,16 @@ static HashSet<string> GetDependencyDirectories(AssemblyName[] dependencies)
// Since this assembly is already loaded in the domain this is a // Since this assembly is already loaded in the domain this is a
// no-op and returns the already loaded assembly // no-op and returns the already loaded assembly
return new HashSet<string>( return new HashSet<string>(
dependencies.Select(dependency => Path.GetDirectoryName(Assembly.Load(dependency).Location)) dependencies.Select(dependency => Path.GetDirectoryName(DotNetAssembly.Load(dependency).Location))
); );
} }
// get all non-dynamic assembly directories // get all non-dynamic assembly directories
static HashSet<string> GetNonDynamicAssemblyDirectories(Assembly[] assemblies) static HashSet<string> GetNonDynamicAssemblyDirectories(DotNetAssembly[] assemblies)
{ {
HashSet<string> paths = new HashSet<string>(); HashSet<string> paths = new HashSet<string>();
foreach (Assembly assembly in assemblies) foreach (DotNetAssembly assembly in assemblies)
{ {
if (!assembly.IsDynamic) if (!assembly.IsDynamic)
{ {
@ -88,7 +92,7 @@ static HashSet<string> GetNonDynamicAssemblyDirectories(Assembly[] assemblies)
string assemblyName = assembly.GetName().Name; string assemblyName = assembly.GetName().Name;
if (File.Exists(assemblyName)) if (File.Exists(assemblyName))
{ {
paths.Add(Path.GetDirectoryName(Assembly.Load(assemblyName).Location)); paths.Add(Path.GetDirectoryName(DotNetAssembly.Load(assemblyName).Location));
} }
} }
} }
@ -148,8 +152,8 @@ static void OnCompilationFinished(string assemblyPath, CompilerMessage[] message
} }
// find all assemblies and the currently compiling assembly // find all assemblies and the currently compiling assembly
Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies(); DotNetAssembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies();
Assembly targetAssembly = assemblies.FirstOrDefault(asm => asm.GetName().Name == Path.GetFileNameWithoutExtension(assemblyPath)); DotNetAssembly targetAssembly = assemblies.FirstOrDefault(asm => asm.GetName().Name == Path.GetFileNameWithoutExtension(assemblyPath));
// prepare variables // prepare variables
HashSet<string> dependencyPaths = new HashSet<string>(); HashSet<string> dependencyPaths = new HashSet<string>();
@ -181,6 +185,17 @@ static void OnCompilationFinished(string assemblyPath, CompilerMessage[] message
dependencyPaths = GetNonDynamicAssemblyDirectories(assemblies); dependencyPaths = GetNonDynamicAssemblyDirectories(assemblies);
} }
// add compiled refs from CompilationPipeline
foreach (UnityAssembly unityAsm in _cachedAssemblies)
{
if (unityAsm.outputPath != assemblyPath) continue;
foreach (string unityAsmRef in unityAsm.compiledAssemblyReferences)
{
dependencyPaths.Add(Path.GetDirectoryName(unityAsmRef));
}
}
// construct full path to Project/Library/ScriptAssemblies // construct full path to Project/Library/ScriptAssemblies
string projectDirectory = Directory.GetParent(Application.dataPath).ToString(); string projectDirectory = Directory.GetParent(Application.dataPath).ToString();
string outputDirectory = Path.Combine(projectDirectory, Path.GetDirectoryName(assemblyPath)); string outputDirectory = Path.Combine(projectDirectory, Path.GetDirectoryName(assemblyPath));

View File

@ -31,7 +31,8 @@ public static bool Process(string unityEngine, string netDLL, string outputDirec
CheckAssemblies(assemblies); CheckAssemblies(assemblies);
Log.WarningMethod = printWarning; Log.WarningMethod = printWarning;
Log.ErrorMethod = printError; Log.ErrorMethod = printError;
return Weaver.WeaveAssemblies(assemblies, extraAssemblyPaths, null, outputDirectory, unityEngine, netDLL); IAssemblyResolver assemblyResolver = new DefaultAssemblyResolver();
return Weaver.WeaveAssemblies(assemblies, extraAssemblyPaths, assemblyResolver, outputDirectory, unityEngine, netDLL);
} }
private static void CheckDLLPath(string path) private static void CheckDLLPath(string path)