Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 33 additions & 19 deletions tools/jnimarshalmethod-gen/App.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class App : MarshalByRefObject
internal const string Name = "jnimarshalmethod-gen";
static DirectoryAssemblyResolver resolver = new DirectoryAssemblyResolver (logger: (l, v) => { Console.WriteLine (v); }, loadDebugSymbols: true, loadReaderParameters: new ReaderParameters () { ReadSymbols = true });
static Dictionary<string, TypeBuilder> definedTypes = new Dictionary<string, TypeBuilder> ();
static Dictionary<string, TypeDefinition> typeMap = new Dictionary<string, TypeDefinition> ();
static public bool Debug;
static public bool Verbose;
static bool keepTemporary;
Expand Down Expand Up @@ -217,6 +218,8 @@ void CreateMarshalMethodAssembly (string path)

var ad = resolver.GetAssembly (path);

PrepareTypeMap (ad.MainModule);

foreach (var type in assembly.DefinedTypes) {
if (matchType) {
var matched = false;
Expand All @@ -231,7 +234,7 @@ void CreateMarshalMethodAssembly (string path)
if (type.IsGenericType || type.IsGenericTypeDefinition)
continue;

var td = ad.MainModule.FindType (type);
var td = FindType (type);

if (td == null) {
if (Verbose)
Expand Down Expand Up @@ -387,6 +390,35 @@ static void ColorMessage (string message, ConsoleColor color, TextWriter writer,
public static void Error (string message) => ColorMessage ($"Error: {Name}: {message}", ConsoleColor.Red, Console.Error);

public static void Warning (string message) => ColorMessage ($"Warning: {Name}: {message}", ConsoleColor.Yellow, Console.Error);

static void AddToTypeMap (TypeDefinition type)
{
typeMap [type.FullName] = type;

if (!type.HasNestedTypes)
return;

foreach (var nested in type.NestedTypes)
AddToTypeMap (nested);
}

static void PrepareTypeMap (ModuleDefinition md)
{
typeMap.Clear ();

foreach (var type in md.Types)
AddToTypeMap (type);
}

static TypeDefinition FindType (Type type)
{
TypeDefinition rv;
string cecilName = type.GetCecilName ();

typeMap.TryGetValue (cecilName, out rv);

return rv;
}
}

static class Extensions
Expand Down Expand Up @@ -511,23 +543,5 @@ public static bool NeedsMarshalMethod (this MethodDefinition md, DirectoryAssemb

return false;
}

static TypeDefinition FindType (Collection<TypeDefinition> types, string name)
{
foreach (var type in types) {
if (type.FullName == name)
return type;

if (name.StartsWith (type.FullName + '/', StringComparison.InvariantCulture))
return FindType (type.NestedTypes, name);
}

return null;
}

public static TypeDefinition FindType (this ModuleDefinition md, Type type)
{
return FindType (md.Types, type.GetCecilName ());
}
}
}