annotate InteropCodeGen/gendotnet.cs @ 2:f1deea9c97a0

Cleaned up the interop code generator, added wrapper application to initialize kernel driver and run mono
author Ivo Smits
date Wed, 04 May 2011 16:10:08 +0200
parents e1ec7bf71313
children 64226294e26b
rev   line source
0
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
1 //+reference System.dll
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
2 //+reference System.Xml.dll
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
3 using System;
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
4 using System.Xml.XPath;
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
5 using System.IO;
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
6 using System.Collections.Generic;
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
7 class Program {
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
8 enum ParameterType {
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
9 In,
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
10 Out,
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
11 Reference,
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
12 Return,
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
13 Param,
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
14 }
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
15 class TypeInfo {
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
16 public String Name { get; private set; }
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
17 public String[] Attributes { get; private set; }
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
18
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
19 public TypeInfo(String name, params String[] attributes) {
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
20 this.Name = name;
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
21 this.Attributes = attributes;
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
22 }
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
23 public void WriteDeclaration(TextWriter o, ParameterType vartype) {
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
24 WriteAttributes(o, vartype);
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
25 o.Write(Name);
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
26 }
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
27 public void WriteAttributes(TextWriter o, ParameterType vartype) {
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
28 foreach (String attribute in Attributes) {
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
29 o.Write("[");
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
30 if (vartype == ParameterType.Return) o.Write("return: ");
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
31 else if (vartype == ParameterType.Param) o.Write("param: ");
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
32 o.Write(attribute);
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
33 o.Write("] ");
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
34 }
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
35 if (vartype == ParameterType.Out) o.Write("out ");
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
36 else if (vartype == ParameterType.Reference) o.Write("ref ");
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
37 }
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
38 }
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
39 static Dictionary<String, TypeInfo> Types = new Dictionary<string, TypeInfo>();
2
f1deea9c97a0 Cleaned up the interop code generator, added wrapper application to initialize kernel driver and run mono
Ivo Smits
parents: 0
diff changeset
40 static void AddInterfaceTypes(params String[] names) {
f1deea9c97a0 Cleaned up the interop code generator, added wrapper application to initialize kernel driver and run mono
Ivo Smits
parents: 0
diff changeset
41 foreach (String name in names) Types.Add(name, new TypeInfo(name, "MarshalAs(UnmanagedType.Interface)"));
f1deea9c97a0 Cleaned up the interop code generator, added wrapper application to initialize kernel driver and run mono
Ivo Smits
parents: 0
diff changeset
42 }
f1deea9c97a0 Cleaned up the interop code generator, added wrapper application to initialize kernel driver and run mono
Ivo Smits
parents: 0
diff changeset
43 static void AddEnumTypes(params String[] names) {
f1deea9c97a0 Cleaned up the interop code generator, added wrapper application to initialize kernel driver and run mono
Ivo Smits
parents: 0
diff changeset
44 foreach (String name in names) Types.Add(name, new TypeInfo(name));
f1deea9c97a0 Cleaned up the interop code generator, added wrapper application to initialize kernel driver and run mono
Ivo Smits
parents: 0
diff changeset
45 }
0
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
46 static void InitTypes() {
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
47 Types.Add("short", new TypeInfo("Int16"));
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
48 Types.Add("unsigned short", new TypeInfo("UInt16"));
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
49 Types.Add("unsigned long", new TypeInfo("UInt32"));
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
50 Types.Add("long long", new TypeInfo("Int64"));
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
51 Types.Add("long", new TypeInfo("Int32"));
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
52 Types.Add("octet", new TypeInfo("Byte"));
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
53 Types.Add("unsigned long long", new TypeInfo("UInt32"));
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
54 //Types.Add("boolean", new TypeInfo("Boolean", "MarshalAs(UnmanagedType.Bool)"));
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
55 Types.Add("boolean", new TypeInfo("Int32"));
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
56 Types.Add("wstring", new TypeInfo("String", "MarshalAs(UnmanagedType.LPWStr)"));
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
57 //Types.Add("uuid", new TypeInfo("Guid", true, "MarshalAs(UnmanagedType.LPStruct)"));
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
58 Types.Add("uuid", new TypeInfo("Guid", "MarshalAs(UnmanagedType.LPStruct)"));
2
f1deea9c97a0 Cleaned up the interop code generator, added wrapper application to initialize kernel driver and run mono
Ivo Smits
parents: 0
diff changeset
59 Types.Add("$unknown", new TypeInfo("IntPtr"));
0
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
60
2
f1deea9c97a0 Cleaned up the interop code generator, added wrapper application to initialize kernel driver and run mono
Ivo Smits
parents: 0
diff changeset
61 AddInterfaceTypes("IMachine", "ISession", "IVirtualBox", "IHost", "IConsole", "ISystemProperties");
f1deea9c97a0 Cleaned up the interop code generator, added wrapper application to initialize kernel driver and run mono
Ivo Smits
parents: 0
diff changeset
62 AddInterfaceTypes("IProgress", "IDisplay", "IMouse", "IKeyboard", "IFramebuffer", "IFramebufferOverlay");
f1deea9c97a0 Cleaned up the interop code generator, added wrapper application to initialize kernel driver and run mono
Ivo Smits
parents: 0
diff changeset
63 AddInterfaceTypes("IMachineDebugger", "IVirtualBoxErrorInfo", "IEventSource", "IEventListener", "IEventContext");
f1deea9c97a0 Cleaned up the interop code generator, added wrapper application to initialize kernel driver and run mono
Ivo Smits
parents: 0
diff changeset
64 AddInterfaceTypes("IConsoleCallback", "IEvent", "IBandwidthControl", "IVRDEServerInfo", "IAdditionsFacility");
f1deea9c97a0 Cleaned up the interop code generator, added wrapper application to initialize kernel driver and run mono
Ivo Smits
parents: 0
diff changeset
65 AddInterfaceTypes("IExtPackPlugIn", "IExtPackFile", "IExtPack", "IBIOSSettings", "IVFSExplorer", "IUSBController");
f1deea9c97a0 Cleaned up the interop code generator, added wrapper application to initialize kernel driver and run mono
Ivo Smits
parents: 0
diff changeset
66 AddInterfaceTypes("IStorageController", "IVRDPServer", "ISnapshot", "INATEngine", "IUSBDeviceFilter");
f1deea9c97a0 Cleaned up the interop code generator, added wrapper application to initialize kernel driver and run mono
Ivo Smits
parents: 0
diff changeset
67 AddInterfaceTypes("IMediumAttachment", "IUSBDevice", "IParallelPort", "ISerialPort", "INetworkAdapter");
f1deea9c97a0 Cleaned up the interop code generator, added wrapper application to initialize kernel driver and run mono
Ivo Smits
parents: 0
diff changeset
68 AddInterfaceTypes("IMedium", "IGuestOSType", "ISharedFolder", "IPerformanceCollector", "IDHCPServer");
f1deea9c97a0 Cleaned up the interop code generator, added wrapper application to initialize kernel driver and run mono
Ivo Smits
parents: 0
diff changeset
69 AddInterfaceTypes("IAppliance", "IVirtualBoxCallback", "IPerformanceMetric", "IMediumFormat", "IHostUSBDevice");
f1deea9c97a0 Cleaned up the interop code generator, added wrapper application to initialize kernel driver and run mono
Ivo Smits
parents: 0
diff changeset
70 AddInterfaceTypes("IHostNetworkInterface", "IHostUSBDeviceFilter", "IRemoteDisplayInfo", "IGuest");
f1deea9c97a0 Cleaned up the interop code generator, added wrapper application to initialize kernel driver and run mono
Ivo Smits
parents: 0
diff changeset
71 AddInterfaceTypes("IVirtualSystemDescription", "IAudioAdapter", "IExtPackManager", "IVRDEServer");
f1deea9c97a0 Cleaned up the interop code generator, added wrapper application to initialize kernel driver and run mono
Ivo Smits
parents: 0
diff changeset
72 AddInterfaceTypes("IPciDeviceAttachment", "IBandwidthGroup");
f1deea9c97a0 Cleaned up the interop code generator, added wrapper application to initialize kernel driver and run mono
Ivo Smits
parents: 0
diff changeset
73
f1deea9c97a0 Cleaned up the interop code generator, added wrapper application to initialize kernel driver and run mono
Ivo Smits
parents: 0
diff changeset
74 AddEnumTypes("LockType", "MediumVariant", "AccessMode", "NATProtocol", "FirmwareType", "StorageControllerType");
f1deea9c97a0 Cleaned up the interop code generator, added wrapper application to initialize kernel driver and run mono
Ivo Smits
parents: 0
diff changeset
75 AddEnumTypes("StorageBus", "SessionType", "SessionState", "USBDeviceState", "USBDeviceFilterAction");
f1deea9c97a0 Cleaned up the interop code generator, added wrapper application to initialize kernel driver and run mono
Ivo Smits
parents: 0
diff changeset
76 AddEnumTypes("AudioControllerType", "AudioDriverType", "VRDPAuthType", "MachineState", "NetworkAdapterType");
f1deea9c97a0 Cleaned up the interop code generator, added wrapper application to initialize kernel driver and run mono
Ivo Smits
parents: 0
diff changeset
77 AddEnumTypes("NetworkAttachmentType", "PortMode", "DeviceType", "MediumState", "VFSType", "MediumType");
f1deea9c97a0 Cleaned up the interop code generator, added wrapper application to initialize kernel driver and run mono
Ivo Smits
parents: 0
diff changeset
78 AddEnumTypes("HostNetworkInterfaceType", "DeviceActivity", "HostNetworkInterfaceMediumType", "HostNetworkInterfaceStatus");
f1deea9c97a0 Cleaned up the interop code generator, added wrapper application to initialize kernel driver and run mono
Ivo Smits
parents: 0
diff changeset
79 AddEnumTypes("ProcessorFeature", "BIOSBootMenuMode", "VirtualSystemDescriptionType", "VirtualSystemDescriptionValueType");
f1deea9c97a0 Cleaned up the interop code generator, added wrapper application to initialize kernel driver and run mono
Ivo Smits
parents: 0
diff changeset
80 AddEnumTypes("KeyboardHidType", "ClipboardMode", "PointingHidType", "CPUPropertyType", "HWVirtExPropertyType");
f1deea9c97a0 Cleaned up the interop code generator, added wrapper application to initialize kernel driver and run mono
Ivo Smits
parents: 0
diff changeset
81 AddEnumTypes("ChipsetType", "FaultToleranceState", "BandwidthGroupType", "ExecuteProcessStatus", "VBoxEventType");
f1deea9c97a0 Cleaned up the interop code generator, added wrapper application to initialize kernel driver and run mono
Ivo Smits
parents: 0
diff changeset
82 AddEnumTypes("GuestMonitorChangedEventType", "AuthType", "NetworkAdapterPromiscModePolicy", "AdditionsRunLevelType");
f1deea9c97a0 Cleaned up the interop code generator, added wrapper application to initialize kernel driver and run mono
Ivo Smits
parents: 0
diff changeset
83 AddEnumTypes("AdditionsFacilityStatus", "AdditionsFacilityType", "AdditionsFacilityClass", "CleanupMode", "DataType");
0
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
84 }
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
85 static void Main(string[] args) {
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
86 InitTypes();
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
87
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
88 XPathDocument doc = new XPathDocument("VirtualBox.xidl");
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
89 XPathNavigator nav = doc.CreateNavigator();
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
90 nav = nav.SelectSingleNode("/idl/library[@name='VirtualBox']");
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
91 TextWriter o = Console.Out;
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
92 o.WriteLine("using System;");
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
93 o.WriteLine("using System.Runtime.CompilerServices;");
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
94 o.WriteLine("using System.Runtime.InteropServices;");
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
95 o.WriteLine("namespace VirtualBox {");
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
96 foreach (XPathNavigator intf in nav.Select("interface")) {
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
97 ProcessInterface(intf, o, nav);
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
98 }
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
99 foreach (XPathNavigator intf in nav.Select("enum")) {
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
100 ProcessEnum(intf, o);
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
101 }
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
102 o.WriteLine("}");
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
103 }
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
104 static void ProcessEnum(XPathNavigator nav, TextWriter o) {
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
105 String typeName = nav.SelectSingleNode("@name").Value;
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
106 if (!Types.ContainsKey(typeName)) Types.Add(typeName, new TypeInfo(typeName));
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
107 o.WriteLine("[Guid(\"{0}\")]", nav.SelectSingleNode("@uuid").Value);
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
108 o.WriteLine("enum " + typeName + " {");
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
109 foreach (XPathNavigator member in nav.SelectChildren(XPathNodeType.Element)) {
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
110 switch (member.Name) {
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
111 case "desc":
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
112 break;
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
113 case "const":
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
114 o.WriteLine("\t{0} = {1},", member.SelectSingleNode("@name").Value, member.SelectSingleNode("@value").Value);
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
115 break;
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
116 default:
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
117 Console.Error.WriteLine("Unknown member type {0}", member.Name);
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
118 break;
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
119 }
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
120 }
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
121 o.WriteLine("}");
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
122 }
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
123 static void ProcessInterface(XPathNavigator nav, TextWriter o, XPathNavigator parentNav) {
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
124 String typeName = nav.SelectSingleNode("@name").Value;
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
125 if (!Types.ContainsKey(typeName)) Types.Add(typeName, new TypeInfo(typeName, "MarshalAs(UnmanagedType.Interface)"));
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
126 o.WriteLine("[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]");
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
127 o.WriteLine("[Guid(\"{0}\")]", nav.SelectSingleNode("@uuid").Value);
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
128 o.WriteLine("[ComImport()]");
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
129 o.Write("interface " + typeName);
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
130 o.WriteLine(" {");
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
131 ProcessInterfaceMembersAndInherited(nav, o, parentNav);
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
132 o.WriteLine("}");
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
133 }
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
134 static void ProcessInterfaceMembersAndInherited(XPathNavigator nav, TextWriter o, XPathNavigator parentNav) {
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
135 XPathItem item = nav.SelectSingleNode("@extends");
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
136 if (item != null && item.Value[0] != '$') {
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
137 XPathNavigator inav = parentNav.SelectSingleNode("interface[@name='" + item.Value + "']");
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
138 ProcessInterfaceMembersAndInherited(inav, o, parentNav);
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
139 }
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
140 ProcessInterfaceMembers(nav.SelectChildren(XPathNodeType.Element), o);
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
141 }
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
142 static void ProcessInterfaceMembers(XPathNodeIterator members, TextWriter oattribute) {
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
143 XPathItem item;
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
144 StringWriter omethod = new StringWriter();
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
145 foreach (XPathNavigator member in members) {
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
146 switch (member.Name) {
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
147 case "desc":
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
148 break;
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
149 case "attribute": {
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
150 String mName = member.SelectSingleNode("@name").Value;
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
151 item = member.SelectSingleNode("@readonly");
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
152 Boolean mReadOnly = item == null ? false : item.Value == "yes";
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
153 mName = mName.Substring(0, 1).ToUpper() + mName.Substring(1);
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
154 oattribute.Write("\t");
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
155 TypeInfo t = GetTypeDeclaration(member);
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
156 oattribute.Write(t.Name);
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
157 oattribute.Write(" ");
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
158 oattribute.Write(mName);
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
159 oattribute.WriteLine(" {");
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
160 oattribute.Write("\t\t[MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)] ");
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
161 t.WriteAttributes(oattribute, ParameterType.Return);
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
162 oattribute.WriteLine("get;");
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
163 if (!mReadOnly) {
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
164 oattribute.Write("\t\t[MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)] ");
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
165 t.WriteAttributes(oattribute, ParameterType.Param);
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
166 oattribute.WriteLine("set;");
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
167 }
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
168 oattribute.WriteLine("\t}");
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
169 } break;
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
170 case "method": {
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
171 String mName = member.SelectSingleNode("@name").Value;
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
172 XPathNodeIterator mParamReturn = member.Select("param[@dir='return']");
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
173 omethod.WriteLine("\t[MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]");
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
174 omethod.Write("\t");
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
175 bool mReturn = false;
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
176 if (mParamReturn == null || mParamReturn.Count != 1) {
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
177 omethod.Write("void");
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
178 } else {
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
179 mParamReturn.MoveNext();
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
180 mReturn = true;
2
f1deea9c97a0 Cleaned up the interop code generator, added wrapper application to initialize kernel driver and run mono
Ivo Smits
parents: 0
diff changeset
181 WriteTypeDeclaration(omethod, mParamReturn.Current, ParameterType.Return);
0
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
182 }
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
183 mName = mName.Substring(0, 1).ToUpper() + mName.Substring(1);
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
184 omethod.Write(" ");
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
185 omethod.Write(mName);
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
186 omethod.Write("(");
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
187 XPathNodeIterator mParams = member.Select("param");
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
188 bool first = true;
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
189 if (mParams.MoveNext()) {
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
190 while (true) {
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
191 XPathNavigator mParam = mParams.Current;
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
192 String pDir = mParam.SelectSingleNode("@dir").Value;
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
193 if (pDir == "return" && mReturn) {
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
194 if (mParams.MoveNext()) {
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
195 continue;
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
196 } else {
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
197 break;
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
198 }
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
199 } else if (pDir == "in") {
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
200 } else if (pDir == "out") {
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
201 } else {
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
202 Console.Error.WriteLine("Unsupported parameter direction {0}", pDir);
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
203 }
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
204 if (first) {
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
205 first = false;
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
206 } else {
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
207 omethod.Write(", ");
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
208 }
2
f1deea9c97a0 Cleaned up the interop code generator, added wrapper application to initialize kernel driver and run mono
Ivo Smits
parents: 0
diff changeset
209 WriteTypeDeclaration(omethod, mParam, pDir == "out" ? ParameterType.Out : ParameterType.In);
0
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
210 omethod.Write(" p");
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
211 omethod.Write(mParam.SelectSingleNode("@name").Value);
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
212 if (!mParams.MoveNext()) break;
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
213 }
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
214 }
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
215 omethod.WriteLine(");");
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
216 } break;
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
217 default:
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
218 Console.Error.WriteLine("Unknown member type {0}", member.Name);
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
219 break;
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
220 }
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
221 }
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
222 oattribute.Write((omethod as StringWriter).ToString());
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
223 }
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
224 private static TypeInfo GetTypeDeclaration(XPathNavigator nav) {
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
225 ParameterType pt = ParameterType.Param;
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
226 return GetTypeDeclaration(nav, ref pt);
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
227 }
2
f1deea9c97a0 Cleaned up the interop code generator, added wrapper application to initialize kernel driver and run mono
Ivo Smits
parents: 0
diff changeset
228 private static void WriteTypeDeclaration(TextWriter o, XPathNavigator nav, ParameterType paramType) {
f1deea9c97a0 Cleaned up the interop code generator, added wrapper application to initialize kernel driver and run mono
Ivo Smits
parents: 0
diff changeset
229 TypeInfo t = GetTypeDeclaration(nav, ref paramType);
f1deea9c97a0 Cleaned up the interop code generator, added wrapper application to initialize kernel driver and run mono
Ivo Smits
parents: 0
diff changeset
230 t.WriteDeclaration(o, paramType);
f1deea9c97a0 Cleaned up the interop code generator, added wrapper application to initialize kernel driver and run mono
Ivo Smits
parents: 0
diff changeset
231 }
0
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
232 private static TypeInfo GetTypeDeclaration(XPathNavigator nav, ref ParameterType paramType) {
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
233 String mType = nav.SelectSingleNode("@type").Value;
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
234 XPathItem item;
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
235 item = nav.SelectSingleNode("@mod");
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
236 String mMod = item == null ? null : item.Value;
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
237 item = nav.SelectSingleNode("@safearray");
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
238 Boolean pArray = item == null ? false : item.Value == "yes";
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
239
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
240 TypeInfo t;
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
241 if (!Types.TryGetValue(mType, out t)) {
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
242 Console.Error.WriteLine("Type {0} is not supported", mType);
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
243 t = new TypeInfo("void");
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
244 }
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
245 if (mMod == "ptr") {
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
246 if (paramType == ParameterType.In) {
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
247 paramType = ParameterType.Reference;
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
248 } else {
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
249 t = new TypeInfo("IntPtr");
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
250 }
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
251 } else if (mMod == "string") {
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
252 t = new TypeInfo("String", "MarshalAs(UnmanagedType.LPWStr)");
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
253 } else if (mMod != null) {
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
254 Console.Error.WriteLine("Type modifier {0} is not supported", mMod);
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
255 t = new TypeInfo("void");
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
256 }
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
257 if (pArray) {
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
258 t = new TypeInfo("Array", "MarshalAs(UnmanagedType.SafeArray)");
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
259 if (paramType == ParameterType.In || paramType == ParameterType.Out) paramType = ParameterType.Reference;
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
260 }
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
261 return t;
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
262 }
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
263 }