private static bool Weave(string unityEngineDLL, string assemblyPath, string[] depAssemblyPaths, string baseModuleRuntimeDLL) { using (DefaultAssemblyResolver asmResolver = new DefaultAssemblyResolver()) using (CurrentAssembly = AssemblyDefinition.ReadAssembly(assemblyPath, new ReaderParameters { ReadWrite = true, ReadSymbols = true, AssemblyResolver = asmResolver })) { asmResolver.AddSearchDirectory(Path.GetDirectoryName(assemblyPath)); asmResolver.AddSearchDirectory(Helpers.FindUnityEngineDLLDirectoryName()); asmResolver.AddSearchDirectory(Path.GetDirectoryName(unityEngineDLL)); foreach (string path in depAssemblyPaths) { asmResolver.AddSearchDirectory(path); } if (WeaveModule(CurrentAssembly.MainModule, baseModuleRuntimeDLL)) { try { CurrentAssembly.Write(new WriterParameters { WriteSymbols = true }); } finally { asmResolver.Dispose(); } return(true); } } return(false); }
public void Dispose() { _resolver.Dispose(); _resolver = null; _assembly.Dispose(); _assembly = null; }
public void Dispose() { if (resolver != null) { resolver.Dispose(); } resolver = null; }
static void Main(string[] args) { var libsdir = args[0]; var outlibs = args[1]; if (!Directory.Exists(libsdir)) { throw new Exception("create a libs folder and copy battletech Managed assemblies to it, then rebuild this project"); } else { Console.WriteLine("Making all names public"); var assemblyFiles = Directory.GetFiles(libsdir, "*.dll", SearchOption.AllDirectories); var assemblyResolver = new DefaultAssemblyResolver(); assemblyResolver.AddSearchDirectory(libsdir); var readerParameters = new ReaderParameters { AssemblyResolver = assemblyResolver, InMemory = true, ReadWrite = true, ReadingMode = ReadingMode.Deferred }; var assemblies = assemblyFiles.Select(file => { Console.WriteLine("For: " + file); var def = AssemblyDefinition.ReadAssembly(file, readerParameters); var types = def.Modules.SelectMany(m => m.GetAllTypes()).ToList(); var methods = types.SelectMany(t => t.Methods).ToList(); var fields = types.SelectMany(t => t.Fields).ToList(); foreach (var type in types.Where(t => t.IsNested)) { type.IsNestedPublic = true; } foreach (var type in types.Where(t => !t.IsNested)) { type.IsPublic = true; } methods.ForEach(m => m.IsPublic = true); fields.ForEach(m => m.IsPublic = true); return(file, def); }).ToList(); assemblyResolver.Dispose(); Directory.CreateDirectory(outlibs); Console.WriteLine("Saving"); assemblies.ForEach(def => def.Item2.Write(Path.Combine(outlibs, Path.GetFileName(def.file)))); Console.WriteLine("Complete"); } }
public void Dispose() { if (test_module != null) { test_module.Dispose(); } if (test_resolver != null) { test_resolver.Dispose(); } }
private static bool Weave(IList <MapDefinition> metadata) { var weavers = typeof(ConfigurationWeaver).Assembly() .GetLoadableTypes() .Where(t => typeof(IWeaver).IsAssignableFrom(t) && t.IsClass() && !t.IsAbstract()) .OrderBy(t => t.Name) .Select(t => (IWeaver)Activator.CreateInstance(t)) .ToArray(); foreach (var assemblyGroup in metadata.GroupBy(m => m.AssemblyPath)) { var hasSymbols = Path.HasExtension(Path.ChangeExtension(assemblyGroup.Key, "pdb")); var assemblyResolver = new DefaultAssemblyResolver(); assemblyResolver.AddSearchDirectory(Path.GetDirectoryName(assemblyGroup.Key)); using (var assembly = AssemblyDefinition.ReadAssembly( assemblyGroup.Key, new ReaderParameters { ReadWrite = true, ReadSymbols = hasSymbols, AssemblyResolver = assemblyResolver })) { foreach (var mapDefinition in assemblyGroup) { var typeDefinition = assembly.MainModule.GetTypeDefFromFullName(mapDefinition.TypeFullName); foreach (var weaver in weavers) { weaver.Weave(assembly, typeDefinition, mapDefinition.ColumnDefinitions); } } assemblyResolver.Dispose(); assembly.Write(new WriterParameters { WriteSymbols = hasSymbols }); } } return(true); }
public static void Main(params string[] args) { Options = args.Where(a => a.StartsWith("/")) .Select(a => a.TrimStart('/')) .Select(a => new { Parameter = a.Trim(), Separator = a.Trim().IndexOf(':') }) .ToDictionary(a => a.Separator == -1 ? a.Parameter : a.Parameter.Substring(0, a.Separator).ToLower(), a => a.Separator == -1 ? null : a.Parameter.Substring(a.Separator + 1), StringComparer.InvariantCultureIgnoreCase); Parameters = args.Where(a => !a.StartsWith("/")) .ToList(); Options.TryGetValue("patch", out string patchAssemblyPath); Options.TryGetValue("source", out string sourceAssemblyPath); Options.TryGetValue("target", out string targetAssemblyPath); if (!File.Exists(patchAssemblyPath)) { return; } if (!File.Exists(sourceAssemblyPath)) { return; } if (targetAssemblyPath == null) { targetAssemblyPath = sourceAssemblyPath; } //string patchAssemblyPath = @"C:\Projects\jbatonnet\Azzembly\SourceTree\bin\Debug\SourceTree.dll"; //string sourceAssemblyPath = @"C:\Projects\Tests\SourceTree\app-2.5.5\SourceTree.exe"; //string targetAssemblyPath = @"C:\Projects\Tests\FinalSourceTree\SourceTree.exe"; DefaultAssemblyResolver assemblyResolver = new DefaultAssemblyResolver(); assemblyResolver.AddSearchDirectory(Path.GetDirectoryName(sourceAssemblyPath)); AssemblyDefinition patchAssembly = AssemblyDefinition.ReadAssembly(patchAssemblyPath, new ReaderParameters() { AssemblyResolver = assemblyResolver, ReadSymbols = true }); AssemblyDefinition sourceAssembly = AssemblyDefinition.ReadAssembly(sourceAssemblyPath, new ReaderParameters() { AssemblyResolver = assemblyResolver }); string patchAttributeFullName = typeof(PatchAttribute).FullName; Dictionary <TypeDefinition, TypeDefinition> newTypesMapping = new Dictionary <TypeDefinition, TypeDefinition>(); Dictionary <FieldDefinition, FieldDefinition> newFieldsMapping = new Dictionary <FieldDefinition, FieldDefinition>(); Dictionary <MethodDefinition, MethodDefinition> newMethodsMapping = new Dictionary <MethodDefinition, MethodDefinition>(); foreach (TypeDefinition patchType in patchAssembly.MainModule.Types) { CustomAttribute patchAttribute = patchType.CustomAttributes.FirstOrDefault(a => a.AttributeType.FullName == patchAttributeFullName); if (patchAttribute == null) { //sourceAssembly.MainModule.Types.Add(type); } else { CustomAttributeArgument patchAttributeArgument = patchAttribute.ConstructorArguments[0]; TypeReference sourceTypeReference = patchAttributeArgument.Value as TypeReference; TypeDefinition sourceType = sourceTypeReference.Resolve(); TypeDefinition targetType = sourceAssembly.MainModule.Types.FirstOrDefault(t => t.FullName == sourceType.FullName); PatchType(sourceAssembly.MainModule, patchType, targetType); } } sourceAssembly.Write(targetAssemblyPath + ".temp", new WriterParameters() { SymbolWriterProvider = new Mono.Cecil.Pdb.PdbWriterProvider(), WriteSymbols = true }); sourceAssembly.Dispose(); patchAssembly.Dispose(); assemblyResolver.Dispose(); if (File.Exists(targetAssemblyPath)) { File.Delete(targetAssemblyPath); } File.Move(targetAssemblyPath + ".temp", targetAssemblyPath); }
public void Dispose() { resolver.Dispose(); }
public void Dispose() => resolver.Dispose();
public void Dispose() { testModule?.Dispose(); testResolver?.Dispose(); }
internal static int Clean(PurifyOptions opts) { var files = GetAssemblyFiles(opts.WorkDir).Where(f => !f.Contains(".OS.")).ToArray(); log.Info($"Found {files.Length} files!"); var resolv = new DefaultAssemblyResolver(); resolv.AddSearchDirectory(opts.WorkDir); var rparam = new ReaderParameters { AssemblyResolver = resolv }; var wparam = new WriterParameters(); var halDir = Path.Combine(opts.CodeDir); halDir = Directory.CreateDirectory(halDir).FullName; foreach (var file in files) { using (var stream = new MemoryStream(File.ReadAllBytes(file))) { var ass = AssemblyDefinition.ReadAssembly(stream, rparam); log.Info($" - '{ass.FullName}'"); var methods = CollectPInvokes(ass).ToList(); if (!methods.Any()) { continue; } var apiName = ass.Name.Name + ".OS.Api"; var apiCS = Path.Combine(halDir, apiName + ".cs"); var implName = ass.Name.Name + ".OS.Impl"; var implCS = Path.Combine(halDir, implName + ".cs"); using (var pinvoke = File.CreateText(apiCS)) { var nsp = Noast.Create <INamespace>(apiName); nsp.AddUsing("System"); nsp.AddUsing("System.Runtime.InteropServices"); nsp.AddUsing("System.Runtime.InteropServices.ComTypes"); nsp.AddUsing("System.Text"); var cla = Noast.Create <IInterface>("IPlatform", nsp); foreach (var meth in methods.Select(m => m.Item1)) { cla.Methods.Add(meth); } pinvoke.Write(nsp); } string f; var apiCSAss = Compiler.CreateAssembly(string.Empty, apiName, new[] { apiCS }); File.Copy(apiCSAss.Location, f = Path.Combine(halDir, apiName + ".dll"), true); log.Info($" --> '{Path.GetFileName(apiCS)}' ({Path.GetFileName(f)})"); var apiAssDef = AssemblyDefinition.ReadAssembly(f, rparam); using (var pinvoke = File.CreateText(implCS)) { var nsp = Noast.Create <INamespace>(implName); nsp.AddUsing("System"); nsp.AddUsing("System.Runtime.InteropServices"); nsp.AddUsing("System.Runtime.InteropServices.ComTypes"); nsp.AddUsing("System.Text"); var cla = Noast.Create <IClass>("Win32Platform", nsp).With(Visibility.Internal); cla.AddImplements(apiName + ".IPlatform"); foreach (var meth in methods.Select(m => m.Item1).SelectMany(DelegateInterface)) { cla.Methods.Add(meth); } cla = Noast.Create <IClass>("MonoPlatform", nsp).With(Visibility.Internal); cla.AddImplements(apiName + ".IPlatform"); foreach (var meth in methods.Select(m => m.Item1).SelectMany(ImplementInterface)) { cla.Methods.Add(meth); } cla = Noast.Create <IClass>("Platforms", nsp).With(Visibility.Public).With(Modifier.Static); cla.Methods.Add(CreateSwitchMethod()); pinvoke.Write(nsp); } var implCSAss = Compiler.CreateAssembly(string.Empty, implName, new[] { implCS }, new[] { f }); File.Copy(implCSAss.Location, f = Path.Combine(halDir, implName + ".dll"), true); log.Info($" --> '{Path.GetFileName(implCS)}' ({Path.GetFileName(f)})"); var implAssDef = AssemblyDefinition.ReadAssembly(f, rparam); var iplat = apiAssDef.GetAllTypes().First(t => t.Name == "IPlatform"); var platfs = implAssDef.GetAllTypes().First(t => t.Name == "Platforms"); var platGet = platfs.Methods.First(); PurifyCalls(methods.Select(m => m.Item2), platGet, iplat); log.InfoFormat(" added '{0}'!", CopyTypeRef(apiCSAss, opts.WorkDir)); log.InfoFormat(" added '{0}'!", CopyTypeRef(implCSAss, opts.WorkDir)); ass.Write(file, wparam); } } resolv.Dispose(); return(0); }