/// <summary> /// Patches the file. /// </summary> /// <param name="file">The file.</param> public bool PatchFile(string file) { file = Path.Combine(Directory.GetCurrentDirectory(), file); var fileTime = new FileTime(file); var checkFile = Path.GetFullPath(file) + ".check"; // If checkFile and checkInteropBuilderFile up-to-date, then nothing to do if (fileTime.CheckFileUpToDate(checkFile)) { Log("Nothing to do. SharpPatch patch was already applied for assembly [{0}]", file); return(false); } // Copy PDB from input assembly to output assembly if any var readerParameters = new ReaderParameters { InMemory = true }; try { readerParameters.AssemblyResolver = AssemblyResolver ?? CreateDepsFileAssemblyResolver(file); } catch (Exception ex) { LogError($"Unable to load assembly resolver: {ex}"); return(false); } var writerParameters = new WriterParameters(); var pdbName = Path.ChangeExtension(file, "pdb"); if (File.Exists(pdbName)) { readerParameters.SymbolReaderProvider = GetSymbolReaderProvider(pdbName); readerParameters.ReadSymbols = true; writerParameters.WriteSymbols = true; } // Read Assembly assembly = AssemblyDefinition.ReadAssembly(file, readerParameters); // Import void* and int32 voidType = assembly.MainModule.TypeSystem.Void.Resolve(); voidPointerType = new PointerType(assembly.MainModule.ImportReference(voidType)); intType = assembly.MainModule.ImportReference(assembly.MainModule.TypeSystem.Int32.Resolve()); // Remove CompilationRelaxationsAttribute for (int i = 0; i < assembly.CustomAttributes.Count; i++) { var customAttribute = assembly.CustomAttributes[i]; if (customAttribute.AttributeType.FullName == typeof(CompilationRelaxationsAttribute).FullName) { assembly.CustomAttributes.RemoveAt(i); i--; } } Log("SharpPatch interop patch for assembly [{0}]", file); foreach (var type in assembly.MainModule.Types) { PatchType(type); } // Remove All Interop classes foreach (var type in classToRemoveList) { assembly.MainModule.Types.Remove(type); } var outputFilePath = file; assembly.Write(outputFilePath, writerParameters); fileTime = new FileTime(file); // Update Check file fileTime.UpdateCheckFile(checkFile); Log("SharpPatch patch done for assembly [{0}]", file); return(true); }
/// <summary> /// Patches the file. /// </summary> /// <param name="file">The file.</param> public bool PatchFile(string file) { file = Path.Combine(Directory.GetCurrentDirectory(), file); var fileTime = new FileTime(file); var checkFile = Path.GetFullPath(file) + ".check"; // If checkFile and checkInteropBuilderFile up-to-date, then nothing to do if (fileTime.CheckFileUpToDate(checkFile)) { Log("Nothing to do. SharpPatch patch was already applied for assembly [{0}]", file); return(false); } // Copy PDB from input assembly to output assembly if any var readerParameters = new ReaderParameters { InMemory = true }; try { readerParameters.AssemblyResolver = AssemblyResolver ?? CreateDepsFileAssemblyResolver(file); } catch (Exception ex) { LogError($"Unable to load assembly resolver: {ex}"); return(false); } var writerParameters = new WriterParameters(); var pdbName = Path.ChangeExtension(file, "pdb"); if (File.Exists(pdbName)) { readerParameters.SymbolReaderProvider = GetSymbolReaderProvider(pdbName); readerParameters.ReadSymbols = true; writerParameters.WriteSymbols = true; } // Read Assembly assembly = AssemblyDefinition.ReadAssembly(file, readerParameters); Log("SharpPatch interop patch for assembly [{0}]", file); foreach (var type in assembly.MainModule.Types) { PatchType(type); } // Remove All Interop classes foreach (var type in classToRemoveList) { assembly.MainModule.Types.Remove(type); } var outputFilePath = file; assembly.Write(outputFilePath, writerParameters); fileTime = new FileTime(file); // Update Check file fileTime.UpdateCheckFile(checkFile); Log("SharpPatch patch done for assembly [{0}]", file); return(true); }