Repository navigation
Expand file tree
/
Copy pathNamespaceMapping.cs
More file actions
92 lines (76 loc) · 2.76 KB
/
Copy pathNamespaceMapping.cs
File metadata and controls
92 lines (76 loc) · 2.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
using System;
using System.Collections.Generic;
using CodeGenerationTarget = Xamarin.Android.Binder.CodeGenerationTarget;
namespace MonoDroid.Generation
{
public class NamespaceMapping
{
Dictionary<string,string> mappings = new Dictionary<string,string> ();
public NamespaceMapping (IEnumerable<GenBase> gens)
{
foreach (GenBase gen in gens)
if (!mappings.ContainsKey (gen.PackageName))
mappings [gen.PackageName] = gen.Namespace;
}
bool IsGeneratable { get { return true; } }
public void Generate (CodeGenerationOptions opt, GenerationInfo gen_info)
{
using (var sw = gen_info.OpenStream (opt.GetFileName ("__NamespaceMapping__"))) {
sw.WriteLine ("using System;");
sw.WriteLine ();
if (opt.CodeGenerationTarget != CodeGenerationTarget.JavaInterop1) {
// Only the experimental format is marked: the absence of the attribute means
// JavaPeerCallbackFormatAttribute.ConnectorDelegates, so existing bindings and
// existing consumers are unaffected.
if (opt.UseUnmanagedCallersOnlyCallbacks) {
sw.WriteLine ("[assembly:global::Java.Interop.JavaPeerCallbackFormat (global::Java.Interop.JavaPeerCallbackFormatAttribute.UnmanagedCallersOnlyCallbacks)]");
sw.WriteLine ();
}
foreach (var p in mappings) {
sw.WriteLine ($"[assembly:global::Android.Runtime.NamespaceMapping (Java = \"{p.Key}\", Managed=\"{p.Value}\")]");
}
sw.WriteLine ();
}
// [UnmanagedFunctionPointer (CallingConvention.Winapi)]
// delegate bool _JniMarshal_PPL_Z (IntPtr jnienv, IntPtr klass, IntPtr a);
foreach (var jni in opt.GetJniMarshalDelegates ()) {
sw.WriteLine ("[global::System.Runtime.InteropServices.UnmanagedFunctionPointer (global::System.Runtime.InteropServices.CallingConvention.Winapi)]");
sw.WriteLine ($"delegate {FromJniType (jni[jni.Length - 1])} {jni} (IntPtr jnienv, IntPtr klass{GetDelegateParameters (jni)});");
}
}
}
string GetDelegateParameters (string jni)
{
var parameters = new List<string> ();
jni = jni.Substring ("_JniMarshal_PP".Length);
var index = 0;
while (jni[index] != '_') {
parameters.Add ($"{FromJniType (jni [index])} p{index}");
index++;
}
if (parameters.Count == 0)
return string.Empty;
return ", " + string.Join (", ", parameters);
}
string FromJniType (char c)
{
switch (c) {
case 'B': return "sbyte";
case 'b': return "byte";
case 'C': return "char";
case 'D': return "double";
case 'F': return "float";
case 'I': return "int";
case 'i': return "uint";
case 'J': return "long";
case 'j': return "ulong";
case 'S': return "short";
case 's': return "ushort";
case 'Z': return "bool";
case 'V': return "void";
default:
return "IntPtr"; ;
}
}
}
}