Removed passing of options and replaced checks with option object access.

This commit is contained in:
Jarl Gullberg 2017-06-26 21:11:59 +02:00
parent fec6c18702
commit 77ebecb9f7
No known key found for this signature in database
GPG key ID: 750FF6F6BDA72D23

View file

@ -52,10 +52,7 @@ namespace OpenTK.Rewrite
} }
var program = new Program(); var program = new Program();
var file = args[0]; program.Rewrite();
var key = args[1];
var options = args.Where(a => a.StartsWith("-") || a.StartsWith("/"));
program.Rewrite(file, key, options);
} }
// mscorlib types // mscorlib types
@ -69,13 +66,8 @@ namespace OpenTK.Rewrite
// OpenTK.BindingsBase // OpenTK.BindingsBase
private static TypeDefinition TypeBindingsBase; private static TypeDefinition TypeBindingsBase;
private static bool dllimport; private void Rewrite()
private void Rewrite(string file, string keyfile, IEnumerable<string> options)
{ {
IEnumerable<string> optionsEnumerated = options as IList<string> ?? options.ToList();
dllimport = optionsEnumerated.Contains("-dllimport");
// Specify assembly read and write parameters // Specify assembly read and write parameters
// We want to keep a valid symbols file (pdb or mdb) // We want to keep a valid symbols file (pdb or mdb)
var read_params = new ReaderParameters(); var read_params = new ReaderParameters();
@ -85,11 +77,11 @@ namespace OpenTK.Rewrite
read_params.ReadWrite = true; read_params.ReadWrite = true;
write_params.WriteSymbols = true; write_params.WriteSymbols = true;
if (!String.IsNullOrEmpty(keyfile) && File.Exists(keyfile)) if (!String.IsNullOrEmpty(Options.StrongNameKey) && File.Exists(Options.StrongNameKey))
{ {
keyfile = Path.GetFullPath(keyfile); string absoluteKeyFilePath = Path.GetFullPath(Options.StrongNameKey);
using (var fs = new FileStream(keyfile, FileMode.Open, FileAccess.Read)) using (var fs = new FileStream(absoluteKeyFilePath, FileMode.Open, FileAccess.Read))
{ {
var keypair = new System.Reflection.StrongNameKeyPair(fs); var keypair = new System.Reflection.StrongNameKeyPair(fs);
write_params.StrongNameKeyPair = keypair; write_params.StrongNameKeyPair = keypair;
@ -103,7 +95,7 @@ namespace OpenTK.Rewrite
// Load assembly and process all modules // Load assembly and process all modules
try try
{ {
using (AssemblyDefinition assembly = AssemblyDefinition.ReadAssembly(file, read_params)) using (AssemblyDefinition assembly = AssemblyDefinition.ReadAssembly(Options.TargetAssembly, read_params))
{ {
var rewritten = assembly.CustomAttributes.FirstOrDefault(a => a.AttributeType.Name == "RewrittenAttribute"); var rewritten = assembly.CustomAttributes.FirstOrDefault(a => a.AttributeType.Name == "RewrittenAttribute");
if (rewritten == null) if (rewritten == null)
@ -143,7 +135,7 @@ namespace OpenTK.Rewrite
{ {
foreach (var type in module.Types) foreach (var type in module.Types)
{ {
Rewrite(type, optionsEnumerated); Rewrite(type);
} }
} }
} }
@ -163,7 +155,7 @@ namespace OpenTK.Rewrite
} }
} }
private void Rewrite(TypeDefinition type, IEnumerable<string> options) private void Rewrite(TypeDefinition type)
{ {
var entry_points = type.Fields.FirstOrDefault(f => f.Name == "EntryPoints"); var entry_points = type.Fields.FirstOrDefault(f => f.Name == "EntryPoints");
if (entry_points != null) if (entry_points != null)
@ -173,7 +165,7 @@ namespace OpenTK.Rewrite
entry_signatures.AddRange(type.Methods entry_signatures.AddRange(type.Methods
.Where(t => t.CustomAttributes.Any(a => a.AttributeType.Name == "SlotAttribute"))); .Where(t => t.CustomAttributes.Any(a => a.AttributeType.Name == "SlotAttribute")));
Rewrite(type, entry_points, entry_signatures, options); Rewrite(type, entry_points, entry_signatures);
RemoveNativeSignatures(type, entry_signatures); RemoveNativeSignatures(type, entry_signatures);
} }
@ -191,7 +183,7 @@ namespace OpenTK.Rewrite
private static int GetSlot(MethodDefinition signature) private static int GetSlot(MethodDefinition signature)
{ {
// Pretend there is no slots if we want to force everything to work through DllImport (Android & iOS) // Pretend there is no slots if we want to force everything to work through DllImport (Android & iOS)
if (dllimport) if (Options.UseDLLImport)
{ {
return -1; return -1;
} }
@ -207,14 +199,13 @@ namespace OpenTK.Rewrite
} }
private void Rewrite(TypeDefinition type, FieldDefinition entry_points, private void Rewrite(TypeDefinition type, FieldDefinition entry_points,
List<MethodDefinition> entry_signatures, IEnumerable<string> options) List<MethodDefinition> entry_signatures)
{ {
// Rewrite all wrapper methods // Rewrite all wrapper methods
var wrapper_signatures = new List<MethodDefinition>(); var wrapper_signatures = new List<MethodDefinition>();
wrapper_signatures.AddRange(type.Methods wrapper_signatures.AddRange(type.Methods
.Where(m => m.IsPublic && m.CustomAttributes.Any(a => a.AttributeType.Name == "AutoGeneratedAttribute"))); .Where(m => m.IsPublic && m.CustomAttributes.Any(a => a.AttributeType.Name == "AutoGeneratedAttribute")));
IEnumerable<string> optionsEnumerated = options as IList<string> ?? options.ToList();
foreach (var wrapper in wrapper_signatures) foreach (var wrapper in wrapper_signatures)
{ {
var autogenerated = wrapper.CustomAttributes var autogenerated = wrapper.CustomAttributes
@ -226,7 +217,7 @@ namespace OpenTK.Rewrite
var signature = entry_signatures.FirstOrDefault(s => s.Name == signature_name); var signature = entry_signatures.FirstOrDefault(s => s.Name == signature_name);
int slot = GetSlot(signature); int slot = GetSlot(signature);
ProcessMethod(wrapper, signature, slot, entry_points, optionsEnumerated); ProcessMethod(wrapper, signature, slot, entry_points);
} }
} }
@ -236,7 +227,7 @@ namespace OpenTK.Rewrite
{ {
foreach (var nested_type in type.NestedTypes) foreach (var nested_type in type.NestedTypes)
{ {
Rewrite(nested_type, entry_points, entry_signatures, optionsEnumerated); Rewrite(nested_type, entry_points, entry_signatures);
} }
} }
} }
@ -283,7 +274,7 @@ namespace OpenTK.Rewrite
// Create body for method // Create body for method
private static void ProcessMethod(MethodDefinition wrapper, MethodDefinition native, int slot, private static void ProcessMethod(MethodDefinition wrapper, MethodDefinition native, int slot,
FieldDefinition entry_points, IEnumerable<string> options) FieldDefinition entry_points)
{ {
var body = wrapper.Body; var body = wrapper.Body;
var il = body.GetILProcessor(); var il = body.GetILProcessor();
@ -294,8 +285,7 @@ namespace OpenTK.Rewrite
// and push each parameter on the stack // and push each parameter on the stack
DebugVariables vars = null; DebugVariables vars = null;
IEnumerable<string> optionsEnumerated = options as IList<string> ?? options.ToList(); if (Options.EnableDebugCalls)
if (optionsEnumerated.Contains("-debug"))
{ {
vars = EmitDebugPrologue(wrapper, il); vars = EmitDebugPrologue(wrapper, il);
} }
@ -333,7 +323,7 @@ namespace OpenTK.Rewrite
EmitParameterEpilogues(wrapper, native, body, il, generatedVariables); EmitParameterEpilogues(wrapper, native, body, il, generatedVariables);
if (optionsEnumerated.Contains("-debug")) if (Options.EnableDebugCalls)
{ {
EmitDebugEpilogue(wrapper, il, vars); EmitDebugEpilogue(wrapper, il, vars);
} }