Skip to content

Commit

Permalink
fix(weaver): fix #706 find system dlls (#729)
Browse files Browse the repository at this point in the history
* added asmresolver back in

* disambiguate Assembly symbol

* add cache for pipeline asms

* add back precomp refs from pipeline
  • Loading branch information
c6burns authored and vis2k committed Apr 6, 2019
1 parent 247c4b7 commit 53be9b6
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 11 deletions.
35 changes: 25 additions & 10 deletions Assets/Mirror/Editor/Weaver/CompilationFinishedHook.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
using UnityEditor;
using UnityEditor.Compilation;
using UnityEngine;
using Assembly = System.Reflection.Assembly;
using DotNetAssembly = System.Reflection.Assembly;
using UnityAssembly = UnityEditor.Compilation.Assembly;

namespace Mirror.Weaver
{
Expand All @@ -15,6 +16,8 @@ public static class CompilationFinishedHook
const string MirrorRuntimeAssemblyName = "Mirror";
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> OnWeaverWarning; // delegate for subscription to Weaver warning messages
public static Action<string> OnWeaverError; // delete for subscription to Weaver error messages
Expand Down Expand Up @@ -47,14 +50,15 @@ static void HandleError(string msg)
[InitializeOnLoadMethod]
static void OnInitializeOnLoad()
{
// pipeline assemblies are valid until the next call to OnInitializeOnLoad
_cachedAssemblies = CompilationPipeline.GetAssemblies();

CompilationPipeline.assemblyCompilationFinished += OnCompilationFinished;
}

static string FindMirrorRuntime()
{
UnityEditor.Compilation.Assembly[] assemblies = CompilationPipeline.GetAssemblies();

foreach (UnityEditor.Compilation.Assembly assembly in assemblies)
foreach (UnityAssembly assembly in _cachedAssemblies)
{
if (assembly.name == MirrorRuntimeAssemblyName)
{
Expand All @@ -70,16 +74,16 @@ static HashSet<string> GetDependencyDirectories(AssemblyName[] dependencies)
// Since this assembly is already loaded in the domain this is a
// no-op and returns the already loaded assembly
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
static HashSet<string> GetNonDynamicAssemblyDirectories(Assembly[] assemblies)
static HashSet<string> GetNonDynamicAssemblyDirectories(DotNetAssembly[] assemblies)
{
HashSet<string> paths = new HashSet<string>();

foreach (Assembly assembly in assemblies)
foreach (DotNetAssembly assembly in assemblies)
{
if (!assembly.IsDynamic)
{
Expand All @@ -88,7 +92,7 @@ static HashSet<string> GetNonDynamicAssemblyDirectories(Assembly[] assemblies)
string assemblyName = assembly.GetName().Name;
if (File.Exists(assemblyName))
{
paths.Add(Path.GetDirectoryName(Assembly.Load(assemblyName).Location));
paths.Add(Path.GetDirectoryName(DotNetAssembly.Load(assemblyName).Location));
}
}
}
Expand Down Expand Up @@ -148,8 +152,8 @@ static void OnCompilationFinished(string assemblyPath, CompilerMessage[] message
}

// find all assemblies and the currently compiling assembly
Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies();
Assembly targetAssembly = assemblies.FirstOrDefault(asm => asm.GetName().Name == Path.GetFileNameWithoutExtension(assemblyPath));
DotNetAssembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies();
DotNetAssembly targetAssembly = assemblies.FirstOrDefault(asm => asm.GetName().Name == Path.GetFileNameWithoutExtension(assemblyPath));

// prepare variables
HashSet<string> dependencyPaths = new HashSet<string>();
Expand Down Expand Up @@ -181,6 +185,17 @@ static void OnCompilationFinished(string assemblyPath, CompilerMessage[] message
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
string projectDirectory = Directory.GetParent(Application.dataPath).ToString();
string outputDirectory = Path.Combine(projectDirectory, Path.GetDirectoryName(assemblyPath));
Expand Down
3 changes: 2 additions & 1 deletion Assets/Mirror/Editor/Weaver/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ public static bool Process(string unityEngine, string netDLL, string outputDirec
CheckAssemblies(assemblies);
Log.WarningMethod = printWarning;
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)
Expand Down

0 comments on commit 53be9b6

Please sign in to comment.