Weaver: Weave(AssemblyDefinition). WeaveFromFile code moved into CompilationFinishedHook to prepare for ILPP.

This commit is contained in:
vis2k 2021-08-17 11:49:51 +08:00
parent ba067d30bf
commit 5f93a7ce7e
2 changed files with 69 additions and 51 deletions

View File

@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Mono.CecilX;
using UnityEditor;
using UnityEditor.Compilation;
using UnityEngine;
@ -139,7 +140,7 @@ static void OnCompilationFinished(string assemblyPath, CompilerMessage[] message
Log.Warning = HandleWarning;
Log.Error = HandleError;
if (!Weaver.Weave(assemblyPath, dependencyPaths.ToArray()))
if (!WeaveFromFile(assemblyPath, dependencyPaths.ToArray()))
{
// Set false...will be checked in \Editor\EnterPlayModeSettingsCheck.CheckSuccessfulWeave()
SessionState.SetBool("MIRROR_WEAVE_SUCCESS", false);
@ -167,5 +168,29 @@ static HashSet<string> GetDependecyPaths(string assemblyPath)
return dependencyPaths;
}
// helper function to invoke Weaver with an AssemblyDefinition from a
// file path, with dependencies added.
static bool WeaveFromFile(string assemblyPath, string[] dependencies)
{
// open the file as stream
using (FileStream stream = new FileStream(assemblyPath, FileMode.Open, FileAccess.ReadWrite))
{
using (DefaultAssemblyResolver asmResolver = new DefaultAssemblyResolver())
using (AssemblyDefinition asmDef = AssemblyDefinition.ReadAssembly(stream, new ReaderParameters { ReadWrite = true, ReadSymbols = true, AssemblyResolver = asmResolver }))
{
asmResolver.AddSearchDirectory(Path.GetDirectoryName(assemblyPath));
asmResolver.AddSearchDirectory(Helpers.UnityEngineDllDirectoryName());
if (dependencies != null)
{
foreach (string path in dependencies)
{
asmResolver.AddSearchDirectory(path);
}
}
return Weaver.Weave(asmDef);
}
}
}
}
}

View File

@ -125,23 +125,17 @@ static bool ContainsGeneratedCodeClass(ModuleDefinition module)
td.Name == GeneratedCodeClassName);
}
public static bool Weave(string assName, IEnumerable<string> dependencies)
// Weave takes an AssemblyDefinition to be compatible with both old and
// new weavers:
// * old takes a filepath, new takes a in-memory byte[]
// * old uses DefaultAssemblyResolver with added dependencies paths,
// new uses ...?
public static bool Weave(AssemblyDefinition asmDef)
{
WeavingFailed = false;
try
{
using (DefaultAssemblyResolver asmResolver = new DefaultAssemblyResolver())
using (CurrentAssembly = AssemblyDefinition.ReadAssembly(assName, new ReaderParameters { ReadWrite = true, ReadSymbols = true, AssemblyResolver = asmResolver }))
{
asmResolver.AddSearchDirectory(Path.GetDirectoryName(assName));
asmResolver.AddSearchDirectory(Helpers.UnityEngineDllDirectoryName());
if (dependencies != null)
{
foreach (string path in dependencies)
{
asmResolver.AddSearchDirectory(path);
}
}
CurrentAssembly = asmDef;
// fix "No writer found for ..." error
// https://github.com/vis2k/Mirror/issues/2579
@ -191,7 +185,6 @@ public static bool Weave(string assName, IEnumerable<string> dependencies)
WriterParameters writeParams = new WriterParameters { WriteSymbols = true };
CurrentAssembly.Write(writeParams);
}
}
return true;
}