public void OnPostBuildPlayerScriptDLLs(BuildReport report) { // This is a bit hacky - we need actual references, not directories, containing references, so we pass folder/dummy.dll // knowing that dummy.dll will be stripped. var systemAssemblies = CompilationPipeline.GetSystemAssemblyDirectories(ApiCompatibilityLevel.NET_Standard_2_0).Select(d => Path.Combine(d, "dummy.dll")); var referencePaths = systemAssemblies .Concat(report.files.Select(f => f.path)) .ToArray(); var assembliesToWeave = report.files.Where(f => f.role == "ManagedLibrary"); foreach (var file in assembliesToWeave) { WeaveAssemblyCore(file.path, referencePaths); } if (report.summary.platform == BuildTarget.iOS) { var realmAssemblyPath = report.files .SingleOrDefault(r => "Realm.dll".Equals(Path.GetFileName(r.path), StringComparison.OrdinalIgnoreCase)) .path; var realmResolutionResult = WeaverAssemblyResolver.Resolve(realmAssemblyPath, referencePaths); using (realmResolutionResult) { var wrappersReference = realmResolutionResult.Module.ModuleReferences.SingleOrDefault(r => r.Name == "realm-wrappers"); if (wrappersReference != null) { wrappersReference.Name = "__Internal"; realmResolutionResult.SaveModuleUpdates(); } } } }
public static ResolutionResult Resolve(string assemblyPath, IEnumerable <string> references) { if (assemblyPath == null) { return(null); } var absolutePath = GetAbsolutePath(assemblyPath); if (!File.Exists(absolutePath)) { return(null); } var resolver = new WeaverAssemblyResolver(references); var readerParameters = new ReaderParameters { AssemblyResolver = resolver, InMemory = true, }; var hasDebugInfo = false; var module = ModuleDefinition.ReadModule(absolutePath, readerParameters); try { module.ReadSymbols(); hasDebugInfo = true; } catch { } return(new ResolutionResult(module, absolutePath, resolver, hasDebugInfo)); }
public ResolutionResult(ModuleDefinition module, FileStream fileStream, WeaverAssemblyResolver resolver, bool writeSymbols) { Module = module; _resolver = resolver; _fileStream = fileStream; _writerParameters = new WriterParameters(); if (writeSymbols) { _writerParameters.WriteSymbols = true; _writerParameters.SymbolWriterProvider = new PdbWriterProvider(); } }
private static bool WeaveAssemblyCore(string assemblyPath, IEnumerable <string> references) { var name = Path.GetFileNameWithoutExtension(assemblyPath); try { var timer = new Stopwatch(); timer.Start(); var resolutionResult = WeaverAssemblyResolver.Resolve(assemblyPath, references); if (resolutionResult == null) { return(false); } using (resolutionResult) { // Unity doesn't add the [TargetFramework] attribute when compiling the assembly. However, it's // using NETStandard2, so we just hardcode this. var weaver = new Weaver(resolutionResult.Module, UnityLogger.Instance, new FrameworkName(".NETStandard,Version=v2.0")); var results = weaver.Execute(); // Unity creates an entry in the build console for each item, so let's not pollute it. if (results.SkipReason == null) { resolutionResult.SaveModuleUpdates(); UnityLogger.Instance.Info($"[{name}] Weaving completed in {timer.ElapsedMilliseconds} ms.{Environment.NewLine}{results}"); return(true); } } } catch (Exception ex) { UnityLogger.Instance.Warning($"[{name}] Weaving failed: {ex}"); } return(false); }
public static ResolutionResult Resolve(string assemblyPath, IEnumerable <string> references) { if (assemblyPath == null) { return(null); } var absolutePath = GetAbsolutePath(assemblyPath); if (!File.Exists(absolutePath)) { return(null); } var assemblyStream = new FileStream(absolutePath, FileMode.Open, FileAccess.ReadWrite); var resolver = new WeaverAssemblyResolver(references); var readerParameters = new ReaderParameters { ReadingMode = ReadingMode.Immediate, ReadWrite = true, AssemblyResolver = resolver, ReadSymbols = false, }; var hasDebugInfo = File.Exists(absolutePath.Replace(".dll", ".pdb")); if (hasDebugInfo) { readerParameters.ReadSymbols = true; readerParameters.SymbolReaderProvider = new PdbReaderProvider(); } var module = ModuleDefinition.ReadModule(assemblyStream, readerParameters); return(new ResolutionResult(module, assemblyStream, resolver, hasDebugInfo)); }