cosmetix
[nyatools.git] / mono / monox.d
blobcb75e11c5695f21e730c4f23c08eadb2a8f82af6
1 module monox is aliced;
2 private:
4 import std.string : fromStringz;
5 import iv.vfs.io;
7 import iv.ncserial;
10 // ////////////////////////////////////////////////////////////////////////// //
11 public struct AssemblyInfo {
12 uint id;
13 string name;
17 // ////////////////////////////////////////////////////////////////////////// //
18 public struct NCClassInfo {
19 uint id;
20 uint asmid;
21 string name;
22 string nspace;
23 // constructed
24 @NCIgnore string asmname;
25 @NCIgnore string fullname;
26 @NCIgnore string fullnamenoasm;
28 bool isNameEqu (const(char)[] name) const {
29 import std.algorithm : endsWith;
30 if (!fullname.endsWith(name)) return false;
31 if (fullname.length == name.length) return true;
32 if (fullname[$-name.length-1] != '.') return false;
33 return true;
38 // ////////////////////////////////////////////////////////////////////////// //
39 public struct FieldInfo {
40 uint type;
41 uint ofs;
42 uint flags;
43 string name;
44 string typename;
48 // ////////////////////////////////////////////////////////////////////////// //
49 extern(C) nothrow @nogc {
50 enum RTLD_LAZY = 0x00001; // POSIX
51 extern void* __libc_dlopen_mode (const(char)* name, int mode);
52 extern void* __libc_dlsym (void* soh, const(char)* name);
53 extern int __libc_dlclose (void* soh);
57 // ////////////////////////////////////////////////////////////////////////// //
58 alias BOOL = int;
59 alias UINT32 = uint;
60 alias uintptr_t = uint;
63 // ////////////////////////////////////////////////////////////////////////// //
64 // original source: blob.h in the mono sourcecode
65 // Encoding for type signatures used in the Metadata
66 alias MonoTypeEnum = uint;
67 enum /*MonoTypeEnum*/ : uint {
68 MONO_TYPE_END = 0x00, // End of List
69 MONO_TYPE_VOID = 0x01,
70 MONO_TYPE_BOOLEAN = 0x02,
71 MONO_TYPE_CHAR = 0x03,
72 MONO_TYPE_I1 = 0x04,
73 MONO_TYPE_U1 = 0x05,
74 MONO_TYPE_I2 = 0x06,
75 MONO_TYPE_U2 = 0x07,
76 MONO_TYPE_I4 = 0x08,
77 MONO_TYPE_U4 = 0x09,
78 MONO_TYPE_I8 = 0x0a,
79 MONO_TYPE_U8 = 0x0b,
80 MONO_TYPE_R4 = 0x0c,
81 MONO_TYPE_R8 = 0x0d,
82 MONO_TYPE_STRING = 0x0e,
83 MONO_TYPE_PTR = 0x0f, // arg: <type> token
84 MONO_TYPE_BYREF = 0x10, // arg: <type> token
85 MONO_TYPE_VALUETYPE = 0x11, // arg: <type> token
86 MONO_TYPE_CLASS = 0x12, // arg: <type> token
87 MONO_TYPE_VAR = 0x13, // number
88 MONO_TYPE_ARRAY = 0x14, // type, rank, boundsCount, bound1, loCount, lo1
89 MONO_TYPE_GENERICINST= 0x15, // <type> <type-arg-count> <type-1> \x{2026} <type-n>
90 MONO_TYPE_TYPEDBYREF = 0x16,
91 MONO_TYPE_I = 0x18,
92 MONO_TYPE_U = 0x19,
93 MONO_TYPE_FNPTR = 0x1b, // arg: full method signature
94 MONO_TYPE_OBJECT = 0x1c,
95 MONO_TYPE_SZARRAY = 0x1d, // 0-based one-dim-array
96 MONO_TYPE_MVAR = 0x1e, // number
97 MONO_TYPE_CMOD_REQD = 0x1f, // arg: typedef or typeref token
98 MONO_TYPE_CMOD_OPT = 0x20, // optional arg: typedef or typref token
99 MONO_TYPE_INTERNAL = 0x21, // CLR internal type
101 MONO_TYPE_MODIFIER = 0x40, // Or with the following types
102 MONO_TYPE_SENTINEL = 0x41, // Sentinel for varargs method signature
103 MONO_TYPE_PINNED = 0x45, // Local var that points to pinned object
105 MONO_TYPE_ENUM = 0x55, // an enumeration
108 alias MonoMetaTableEnum = uint;
109 enum /*MonoMetaTableEnum*/ : uint {
110 MONO_TABLE_MODULE,
111 MONO_TABLE_TYPEREF,
112 MONO_TABLE_TYPEDEF,
113 MONO_TABLE_FIELD_POINTER,
114 MONO_TABLE_FIELD,
115 MONO_TABLE_METHOD_POINTER,
116 MONO_TABLE_METHOD,
117 MONO_TABLE_PARAM_POINTER,
118 MONO_TABLE_PARAM,
119 MONO_TABLE_INTERFACEIMPL,
120 MONO_TABLE_MEMBERREF, // 0xa
121 MONO_TABLE_CONSTANT,
122 MONO_TABLE_CUSTOMATTRIBUTE,
123 MONO_TABLE_FIELDMARSHAL,
124 MONO_TABLE_DECLSECURITY,
125 MONO_TABLE_CLASSLAYOUT,
126 MONO_TABLE_FIELDLAYOUT, // 0x10
127 MONO_TABLE_STANDALONESIG,
128 MONO_TABLE_EVENTMAP,
129 MONO_TABLE_EVENT_POINTER,
130 MONO_TABLE_EVENT,
131 MONO_TABLE_PROPERTYMAP,
132 MONO_TABLE_PROPERTY_POINTER,
133 MONO_TABLE_PROPERTY,
134 MONO_TABLE_METHODSEMANTICS,
135 MONO_TABLE_METHODIMPL,
136 MONO_TABLE_MODULEREF, // 0x1a
137 MONO_TABLE_TYPESPEC,
138 MONO_TABLE_IMPLMAP,
139 MONO_TABLE_FIELDRVA,
140 MONO_TABLE_UNUSED6,
141 MONO_TABLE_UNUSED7,
142 MONO_TABLE_ASSEMBLY, // 0x20
143 MONO_TABLE_ASSEMBLYPROCESSOR,
144 MONO_TABLE_ASSEMBLYOS,
145 MONO_TABLE_ASSEMBLYREF,
146 MONO_TABLE_ASSEMBLYREFPROCESSOR,
147 MONO_TABLE_ASSEMBLYREFOS,
148 MONO_TABLE_FILE,
149 MONO_TABLE_EXPORTEDTYPE,
150 MONO_TABLE_MANIFESTRESOURCE,
151 MONO_TABLE_NESTEDCLASS,
152 MONO_TABLE_GENERICPARAM, // 0x2a
153 MONO_TABLE_METHODSPEC,
154 MONO_TABLE_GENERICPARAMCONSTRAINT,
157 enum {
158 MONO_TYPEDEF_FLAGS,
159 MONO_TYPEDEF_NAME,
160 MONO_TYPEDEF_NAMESPACE,
161 MONO_TYPEDEF_EXTENDS,
162 MONO_TYPEDEF_FIELD_LIST,
163 MONO_TYPEDEF_METHOD_LIST,
164 MONO_TYPEDEF_SIZE
167 enum {
168 MONO_METHOD_RVA,
169 MONO_METHOD_IMPLFLAGS,
170 MONO_METHOD_FLAGS,
171 MONO_METHOD_NAME,
172 MONO_METHOD_SIGNATURE,
173 MONO_METHOD_PARAMLIST,
174 MONO_METHOD_SIZE
177 alias MonoTokenType = uint;
178 enum /*MonoTokenType*/ : uint {
179 MONO_TOKEN_MODULE = 0x00000000,
180 MONO_TOKEN_TYPE_REF = 0x01000000,
181 MONO_TOKEN_TYPE_DEF = 0x02000000,
182 MONO_TOKEN_FIELD_DEF = 0x04000000,
183 MONO_TOKEN_METHOD_DEF = 0x06000000,
184 MONO_TOKEN_PARAM_DEF = 0x08000000,
185 MONO_TOKEN_INTERFACE_IMPL = 0x09000000,
186 MONO_TOKEN_MEMBER_REF = 0x0a000000,
187 MONO_TOKEN_CUSTOM_ATTRIBUTE = 0x0c000000,
188 MONO_TOKEN_PERMISSION = 0x0e000000,
189 MONO_TOKEN_SIGNATURE = 0x11000000,
190 MONO_TOKEN_EVENT = 0x14000000,
191 MONO_TOKEN_PROPERTY = 0x17000000,
192 MONO_TOKEN_MODULE_REF = 0x1a000000,
193 MONO_TOKEN_TYPE_SPEC = 0x1b000000,
194 MONO_TOKEN_ASSEMBLY = 0x20000000,
195 MONO_TOKEN_ASSEMBLY_REF = 0x23000000,
196 MONO_TOKEN_FILE = 0x26000000,
197 MONO_TOKEN_EXPORTED_TYPE = 0x27000000,
198 MONO_TOKEN_MANIFEST_RESOURCE = 0x28000000,
199 MONO_TOKEN_GENERIC_PARAM = 0x2a000000,
200 MONO_TOKEN_METHOD_SPEC = 0x2b000000,
202 // These do not match metadata tables directly
203 MONO_TOKEN_STRING = 0x70000000,
204 MONO_TOKEN_NAME = 0x71000000,
205 MONO_TOKEN_BASE_TYPE = 0x72000000,
209 // ////////////////////////////////////////////////////////////////////////// //
210 alias MonoDomainFunc = extern(C) void function (void* domain, void* user_data) nothrow @nogc;
211 alias GFunc = extern(C) void function (void* data, void* user_data) nothrow @nogc;
213 alias G_FREE = extern(C) void function (void* ptr) nothrow @nogc;
215 alias MONO_GET_ROOT_DOMAIN = extern(C) void* function () nothrow @nogc;
216 alias MONO_THREAD_ATTACH = extern(C) void* function (void* domain) nothrow @nogc;
217 alias MONO_THREAD_DETACH = extern(C) void function (void* monothread) nothrow @nogc;
218 alias MONO_OBJECT_GET_CLASS = extern(C) void* function (void* object) nothrow @nogc;
220 alias MONO_DOMAIN_FOREACH = extern(C) void function (MonoDomainFunc func, void* user_data) nothrow /*@nogc*/;
222 alias MONO_DOMAIN_SET = extern(C) int function (void* domain, BOOL force) nothrow @nogc;
223 alias MONO_ASSEMBLY_FOREACH = extern(C) int function (GFunc func, void* user_data) nothrow /*@nogc*/;
224 alias MONO_ASSEMBLY_GET_IMAGE = extern(C) void* function (void* assembly) nothrow @nogc;
225 alias MONO_ASSEMBLY_OPEN = extern(C) void* function (void* fname, int* status) nothrow @nogc;
226 alias MONO_IMAGE_GET_ASSEMBLY = extern(C) void* function (void* image) nothrow @nogc;
227 alias MONO_IMAGE_GET_NAME = extern(C) char* function (void* image) nothrow @nogc;
228 alias MONO_IMAGE_OPEN = extern(C) void* function (const(char)* fname, int* status) nothrow @nogc;
230 alias MONO_IMAGE_GET_TABLE_INFO = extern(C) void* function (void* image, int table_id) nothrow @nogc;
231 alias MONO_TABLE_INFO_GET_ROWS = extern(C) int function (void* tableinfo) nothrow @nogc;
232 alias MONO_METADATA_DECODE_ROW_COL = extern(C) int function (void* tableinfo, int idx, uint col) nothrow @nogc;
233 alias MONO_METADATA_STRING_HEAP = extern(C) ubyte* function (void* image, UINT32 index) nothrow @nogc; //???
235 alias MONO_CLASS_FROM_NAME_CASE = extern(C) void* function (void* image, const(char)* name_space, const(char)* name) nothrow @nogc;
236 alias MONO_CLASS_GET_NAME = extern(C) char* function (void* klass) nothrow @nogc;
237 alias MONO_CLASS_GET_NAMESPACE = extern(C) char* function (void* klass) nothrow @nogc; // returns namespace name
238 alias MONO_CLASS_GET = extern(C) void* function (void* image, UINT32 tokenindex) nothrow @nogc;
239 alias MONO_CLASS_GET_METHODS = extern(C) void* function (void* klass, void* iter) nothrow @nogc;
240 alias MONO_CLASS_GET_METHOD_FROM_NAME = extern(C) void* function (void* klass, const(char)* methodname, int paramcount) nothrow @nogc;
241 alias MONO_CLASS_GET_FIELDS = extern(C) void* function (void* klass, void* iter) nothrow @nogc;
242 alias MONO_CLASS_GET_PARENT = extern(C) void* function (void* klass) nothrow @nogc;
243 alias MONO_CLASS_VTABLE = extern(C) void* function (void* domain, void* klass) nothrow @nogc;
244 alias MONO_CLASS_FROM_MONO_TYPE = extern(C) void* function (void* type) nothrow @nogc;
245 alias MONO_CLASS_GET_ELEMENT_CLASS = extern(C) void* function (void* klass) nothrow @nogc;
247 alias MONO_CLASS_NUM_FIELDS = extern(C) int function (void* klass) nothrow @nogc;
248 alias MONO_CLASS_NUM_METHODS = extern(C) int function (void* klass) nothrow @nogc;
250 alias MONO_FIELD_GET_NAME = extern(C) char* function (void* field) nothrow @nogc; // should be g_free'd
251 alias MONO_FIELD_GET_TYPE = extern(C) void* function (void* field) nothrow @nogc;
252 alias MONO_FIELD_GET_PARENT = extern(C) void* function (void* field) nothrow @nogc;
253 alias MONO_FIELD_GET_OFFSET = extern(C) int function (void* field) nothrow @nogc;
255 alias MONO_TYPE_GET_NAME = extern(C) char* function (void* type) nothrow @nogc;
256 alias MONO_TYPE_GET_TYPE = extern(C) int function (void* type) nothrow @nogc;
257 alias MONO_TYPE_GET_NAME_FULL = extern(C) char* function (void* type, int format) nothrow @nogc;
258 alias MONO_FIELD_GET_FLAGS = extern(C) int function (void* type) nothrow @nogc;
260 alias MONO_METHOD_GET_NAME = extern(C) char* function (void* method) nothrow @nogc;
261 alias MONO_COMPILE_METHOD = extern(C) void* function (void* method) nothrow @nogc;
262 alias MONO_FREE_METHOD = extern(C) void function (void* method) nothrow @nogc;
264 alias MONO_JIT_INFO_TABLE_FIND = extern(C) void* function (void* domain, void* addr) nothrow @nogc;
266 alias MONO_JIT_INFO_GET_METHOD = extern(C) void* function (void* jitinfo) nothrow @nogc;
267 alias MONO_JIT_INFO_GET_CODE_START = extern(C) void* function (void* jitinfo) nothrow @nogc;
268 alias MONO_JIT_INFO_GET_CODE_SIZE = extern(C) int function (void* jitinfo) nothrow @nogc;
270 alias MONO_METHOD_GET_HEADER = extern(C) void* function (void* method) nothrow @nogc;
271 alias MONO_METHOD_GET_CLASS = extern(C) void* function (void* method) nothrow @nogc;
272 alias MONO_METHOD_SIG = extern(C) void* function (void* method) nothrow @nogc;
273 alias MONO_METHOD_GET_PARAM_NAMES = extern(C) void* function (void* method, const(char)** names) nothrow @nogc;
275 alias MONO_METHOD_HEADER_GET_CODE = extern(C) void* function (void* methodheader, UINT32 *code_size, UINT32 *max_stack) nothrow @nogc;
276 alias MONO_DISASM_CODE = extern(C) char* function (void* dishelper, void* method, void* ip, void* end) nothrow @nogc;
278 alias MONO_SIGNATURE_GET_DESC = extern(C) char* function (void* signature, int include_namespace) nothrow @nogc;
279 alias MONO_SIGNATURE_GET_PARAM_COUNT = extern(C) int function (void* signature) nothrow @nogc;
280 alias MONO_SIGNATURE_GET_RETURN_TYPE = extern(C) void* function (void* signature) nothrow @nogc;
282 alias MONO_IMAGE_RVA_MAP = extern(C) void* function (void* image, UINT32 addr) nothrow @nogc;
283 alias MONO_VTABLE_GET_STATIC_FIELD_DATA = extern(C) void* function (void* vtable) nothrow @nogc;
285 alias MONO_METHOD_DESC_NEW = extern(C) void* function (const(char)* name, int include_namespace) nothrow @nogc;
286 alias MONO_METHOD_DESC_FROM_METHOD = extern(C) void* function (void* method) nothrow @nogc;
287 alias MONO_METHOD_DESC_FREE = extern(C) void function (void* desc) nothrow @nogc;
289 alias MONO_ASSEMBLY_NAME_NEW = extern(C) void* function (const(char)* name) nothrow @nogc;
290 alias MONO_ASSEMBLY_LOADED = extern(C) void* function (void* aname) nothrow @nogc;
291 alias MONO_IMAGE_LOADED = extern(C) void* function (void* aname) nothrow @nogc;
293 alias MONO_STRING_NEW = extern(C) void* function (void* domain, const(char)* text) nothrow @nogc;
294 alias MONO_STRING_TO_UTF8 = extern(C) char* function (void*) nothrow @nogc;
295 alias MONO_ARRAY_NEW = extern(C) void* function (void* domain, void* eclass, uintptr_t n) nothrow @nogc;
296 alias MONO_OBJECT_TO_STRING = extern(C) void* function (void* object, void* *exc) nothrow @nogc;
297 alias MONO_FREE = extern(C) void function (void*) nothrow @nogc;
299 alias MONO_METHOD_DESC_SEARCH_IN_IMAGE = extern(C) void* function (void* desc, void* image) nothrow @nogc;
300 alias MONO_RUNTIME_INVOKE = extern(C) void* function (void* method, void* obj, void* *params, void* *exc) nothrow @nogc;
301 alias MONO_RUNTIME_INVOKE_ARRAY = extern(C) void* function (void* method, void* obj, void* params, void* *exc) nothrow @nogc;
302 alias MONO_VALUE_BOX = extern(C) void* function (void* domain, void* klass, void* val) nothrow @nogc;
303 alias MONO_OBJECT_UNBOX = extern(C) void* function (void* obj) nothrow @nogc;
304 alias MONO_CLASS_GET_TYPE = extern(C) void* function (void* klass) nothrow @nogc;
307 // ////////////////////////////////////////////////////////////////////////// //
308 enum MonoAPI;
309 enum Optional;
311 __gshared void* hmono;
312 __gshared void* mono_selfthread;
314 @MonoAPI G_FREE g_free;
315 @MonoAPI __gshared MONO_GET_ROOT_DOMAIN mono_get_root_domain;
316 @MonoAPI __gshared MONO_THREAD_ATTACH mono_thread_attach;
317 @MonoAPI __gshared MONO_THREAD_DETACH mono_thread_detach;
318 @MonoAPI __gshared MONO_OBJECT_GET_CLASS mono_object_get_class;
319 @MonoAPI __gshared MONO_CLASS_GET_NAME mono_class_get_name;
320 @MonoAPI __gshared MONO_CLASS_GET_NAMESPACE mono_class_get_namespace;
321 @MonoAPI __gshared MONO_CLASS_GET_PARENT mono_class_get_parent;
322 @MonoAPI __gshared MONO_CLASS_VTABLE mono_class_vtable;
323 @MonoAPI __gshared MONO_CLASS_FROM_MONO_TYPE mono_class_from_mono_type;
325 @MonoAPI __gshared MONO_DOMAIN_FOREACH mono_domain_foreach;
326 @MonoAPI __gshared MONO_DOMAIN_SET mono_domain_set;
327 @MonoAPI __gshared MONO_ASSEMBLY_FOREACH mono_assembly_foreach;
328 @MonoAPI __gshared MONO_ASSEMBLY_GET_IMAGE mono_assembly_get_image;
329 @MonoAPI __gshared MONO_IMAGE_GET_ASSEMBLY mono_image_get_assembly;
330 @MonoAPI __gshared MONO_ASSEMBLY_OPEN mono_assembly_open;
332 @MonoAPI __gshared MONO_IMAGE_GET_NAME mono_image_get_name;
333 @MonoAPI __gshared MONO_IMAGE_GET_TABLE_INFO mono_image_get_table_info;
334 @MonoAPI __gshared MONO_IMAGE_RVA_MAP mono_image_rva_map;
335 @MonoAPI __gshared MONO_IMAGE_OPEN mono_image_open;
336 @MonoAPI __gshared MONO_IMAGE_LOADED mono_image_loaded;
338 @MonoAPI __gshared MONO_TABLE_INFO_GET_ROWS mono_table_info_get_rows;
339 @MonoAPI __gshared MONO_METADATA_DECODE_ROW_COL mono_metadata_decode_row_col;
340 @MonoAPI __gshared MONO_METADATA_STRING_HEAP mono_metadata_string_heap;
341 @MonoAPI __gshared MONO_CLASS_GET mono_class_get;
342 @MonoAPI __gshared MONO_CLASS_FROM_NAME_CASE mono_class_from_name_case;
344 @MonoAPI __gshared MONO_CLASS_NUM_FIELDS mono_class_num_fields;
345 @MonoAPI __gshared MONO_CLASS_GET_FIELDS mono_class_get_fields;
347 @MonoAPI __gshared MONO_CLASS_NUM_METHODS mono_class_num_methods;
348 @MonoAPI __gshared MONO_CLASS_GET_METHODS mono_class_get_methods;
350 @MonoAPI __gshared MONO_CLASS_GET_METHOD_FROM_NAME mono_class_get_method_from_name;
351 @MonoAPI __gshared MONO_CLASS_GET_ELEMENT_CLASS mono_class_get_element_class;
354 @MonoAPI __gshared MONO_FIELD_GET_NAME mono_field_get_name;
355 @MonoAPI __gshared MONO_FIELD_GET_TYPE mono_field_get_type;
356 @MonoAPI __gshared MONO_FIELD_GET_PARENT mono_field_get_parent;
357 @MonoAPI __gshared MONO_FIELD_GET_OFFSET mono_field_get_offset;
359 @MonoAPI __gshared MONO_TYPE_GET_NAME mono_type_get_name;
360 @MonoAPI __gshared MONO_TYPE_GET_TYPE mono_type_get_type;
361 @MonoAPI __gshared MONO_TYPE_GET_NAME_FULL mono_type_get_name_full;
362 @MonoAPI __gshared MONO_FIELD_GET_FLAGS mono_field_get_flags;
364 @MonoAPI __gshared MONO_METHOD_GET_NAME mono_method_get_name;
365 @MonoAPI __gshared MONO_METHOD_GET_HEADER mono_method_get_header;
366 @MonoAPI __gshared MONO_METHOD_GET_CLASS mono_method_get_class;
367 @MonoAPI __gshared MONO_METHOD_SIG mono_method_signature;
368 @MonoAPI __gshared MONO_METHOD_GET_PARAM_NAMES mono_method_get_param_names;
370 @MonoAPI __gshared MONO_SIGNATURE_GET_DESC mono_signature_get_desc;
371 @MonoAPI __gshared MONO_SIGNATURE_GET_PARAM_COUNT mono_signature_get_param_count;
372 @MonoAPI __gshared MONO_SIGNATURE_GET_RETURN_TYPE mono_signature_get_return_type;
375 @MonoAPI __gshared MONO_COMPILE_METHOD mono_compile_method;
376 @MonoAPI __gshared MONO_FREE_METHOD mono_free_method;
378 @MonoAPI __gshared MONO_JIT_INFO_TABLE_FIND mono_jit_info_table_find;
379 @MonoAPI __gshared MONO_JIT_INFO_GET_METHOD mono_jit_info_get_method;
380 @MonoAPI __gshared MONO_JIT_INFO_GET_CODE_START mono_jit_info_get_code_start;
381 @MonoAPI __gshared MONO_JIT_INFO_GET_CODE_SIZE mono_jit_info_get_code_size;
383 @MonoAPI __gshared MONO_METHOD_HEADER_GET_CODE mono_method_header_get_code;
384 @MonoAPI __gshared MONO_DISASM_CODE mono_disasm_code;
386 @MonoAPI __gshared MONO_VTABLE_GET_STATIC_FIELD_DATA mono_vtable_get_static_field_data;
388 @MonoAPI __gshared MONO_METHOD_DESC_NEW mono_method_desc_new;
389 @MonoAPI __gshared MONO_METHOD_DESC_FROM_METHOD mono_method_desc_from_method;
390 @MonoAPI __gshared MONO_METHOD_DESC_FREE mono_method_desc_free;
391 @MonoAPI @Optional __gshared MONO_ASSEMBLY_NAME_NEW mono_assembly_name_new;
392 @MonoAPI __gshared MONO_ASSEMBLY_LOADED mono_assembly_loaded;
394 @MonoAPI __gshared MONO_STRING_NEW mono_string_new;
395 @MonoAPI __gshared MONO_STRING_TO_UTF8 mono_string_to_utf8;
396 @MonoAPI __gshared MONO_ARRAY_NEW mono_array_new;
397 @MonoAPI @Optional __gshared MONO_OBJECT_TO_STRING mono_object_to_string;
398 @MonoAPI @Optional __gshared MONO_FREE mono_free; //???
399 @MonoAPI __gshared MONO_VALUE_BOX mono_value_box;
400 @MonoAPI __gshared MONO_OBJECT_UNBOX mono_object_unbox;
401 @MonoAPI __gshared MONO_CLASS_GET_TYPE mono_class_get_type;
403 @MonoAPI __gshared MONO_METHOD_DESC_SEARCH_IN_IMAGE mono_method_desc_search_in_image;
404 @MonoAPI __gshared MONO_RUNTIME_INVOKE mono_runtime_invoke;
406 __gshared int attached = 0;
409 // ////////////////////////////////////////////////////////////////////////// //
410 __gshared void* k32h;
411 extern(Windows) __gshared void* function (const(char)* name) nothrow @nogc LoadLibA;
412 extern(Windows) __gshared void* function (void* hmod, const(char)* name) nothrow @nogc GetProcAddr;
415 string getKernelDllName () {
416 import core.sys.posix.unistd : getpid;
417 import core.stdc.stdio : FILE, fopen, fclose, snprintf, fgets;
418 import core.stdc.string : strstr;
419 bool found = false;
420 FILE *fp;
421 char[30] filename;
422 char[852] line;
423 int pid = cast(int)getpid();
424 snprintf(filename.ptr, filename.length, "/proc/%d/maps", pid);
425 fp = fopen(filename.ptr, "r");
426 if (fp is null) return null;
427 while (fgets(line.ptr, 850, fp) !is null) {
428 import std.string : fromStringz;
429 auto ln = line.ptr.fromStringz;
430 while (ln.length && ln[$-1] <= ' ') ln = ln[0..$-1];
431 if (ln.length == 0 || ln.ptr[0] <= ' ') continue;
432 // remove 5 fields
433 foreach (immutable _; 0..5) {
434 while (ln.length > 0 && ln.ptr[0] > ' ') ln = ln[1..$];
435 while (ln.length > 0 && ln.ptr[0] <= ' ') ln = ln[1..$];
437 if (ln.length == 0) continue;
438 auto savedln = ln;
439 int pos = cast(int)ln.length;
440 while (pos > 0 && ln.ptr[pos-1] != '/') --pos;
441 ln = ln[pos..$];
442 if (ln == "kernel32.dll.so") {
443 // add zero (it is safe, we always has room for it)
444 savedln.ptr[savedln.length] = 0;
445 auto dp = new char[](savedln.length+1);
446 dp[] = savedln.ptr[0..savedln.length+1];
447 return cast(string)(dp[0..$-1]); // zero is there, but not included in length
450 fclose(fp);
451 return null;
455 // ////////////////////////////////////////////////////////////////////////// //
456 public void loadWineAPI () {
457 import core.stdc.stdio : stderr, fprintf;
459 auto k32name = getKernelDllName();
460 if (k32name.length == 0) { fprintf(stderr, "MSG: not a WINE process!\n"); return; }
461 fprintf(stderr, "MSG: kernel: [%s]\n", k32name.ptr);
463 k32h = __libc_dlopen_mode(k32name.ptr, 1); // lazy
464 if (k32h is null) { fprintf(stderr, "MSG: FUCK0 (no kernel32.dll)\n"); return; }
466 *cast(void**)&LoadLibA = __libc_dlsym(k32h, "LoadLibraryA");
467 if (LoadLibA is null) { fprintf(stderr, "MSG: FUCK1 (no LoadLibraryA)\n"); k32h = null; return; }
469 *cast(void**)&GetProcAddr = __libc_dlsym(k32h, "GetProcAddress");
470 if (GetProcAddr is null) { fprintf(stderr, "MSG: FUCK2 (no GetProcAddress)\n"); k32h = null; return; }
472 fprintf(stderr, "MSG: WINE API loaded\n");
475 public @property bool isWineAPILoaded () nothrow @trusted @nogc { return (k32h !is null); }
478 // ////////////////////////////////////////////////////////////////////////// //
479 public void loadMonoAPI () {
480 import core.stdc.stdio : stderr, fprintf;
481 import std.traits;
483 if (!isWineAPILoaded) {
484 fprintf(stderr, "MSG: no WINE API was found\n");
485 attached = -1;
486 return;
489 if (attached != 0) return;
491 hmono = LoadLibA("mono.dll");
492 if (hmono is null) {
493 fprintf(stderr, "MSG: no mono API was found\n");
494 attached = -1;
495 return;
498 attached = 1;
499 foreach (string memname; __traits(allMembers, mixin(__MODULE__))) {
500 static if (is(typeof(__traits(getMember, mixin(__MODULE__), memname)))) {
501 alias mem = Identity!(__traits(getMember, mixin(__MODULE__), memname));
502 static if (is(typeof(&mem)) && hasUDA!(mem, MonoAPI)) {
503 //pragma(msg, memname);
504 mixin("*cast(void**)&"~memname~" = GetProcAddr(hmono, "~memname.stringof~");");
505 static if (hasUDA!(mem, Optional)) {
506 mixin("if ("~memname~` is null) { import core.stdc.stdio : stderr, fprintf; fprintf(stderr, "MSG: mono API '%s' not found!\n", `~memname.stringof~".ptr); }");
507 } else {
508 mixin("if ("~memname~` is null) { import core.stdc.stdio : stderr, fprintf; fprintf(stderr, "MSG: mono API '%s' not found (FATAL)!\n", `~memname.stringof~".ptr); }");
509 mixin("if ("~memname~" is null) attached = -1;");
514 if (attached == 1) mono_selfthread = mono_thread_attach(mono_get_root_domain());
517 public @property bool isMonoAPILoaded () nothrow @trusted @nogc { return (attached == 1); }
520 // ////////////////////////////////////////////////////////////////////////// //
521 export string getAssemblyName (uint asmid) {
522 auto image = mono_assembly_get_image(cast(void*)asmid);
523 if (image is null) return "<none>";
524 auto imgname = mono_image_get_name(image);
525 if (imgname is null) return "<none>";
526 scope(exit) g_free(imgname);
527 return imgname.fromStringz.idup;
531 // ////////////////////////////////////////////////////////////////////////// //
532 extern(C) void assemblyEnumerator (void* assembly, void* userptr) nothrow {
533 auto image = mono_assembly_get_image(assembly);
534 if (image is null) return;
535 auto imgname = mono_image_get_name(image);
536 if (imgname is null) return;
537 scope(exit) g_free(imgname);
538 AssemblyInfo[]* arr = cast(AssemblyInfo[]*)userptr;
539 try {
540 (*arr) ~= AssemblyInfo(cast(uint)assembly, imgname.fromStringz.idup);
541 } catch (Exception) {}
544 export AssemblyInfo[] enumAssemblies () {
545 AssemblyInfo[] arr;
546 mono_assembly_foreach(cast(GFunc)&assemblyEnumerator, &arr);
547 return arr;
551 // ////////////////////////////////////////////////////////////////////////// //
552 void collectAssemblyClasses (uint asmid, ref NCClassInfo[] clist) {
553 auto image = mono_assembly_get_image(cast(void*)asmid);
554 if (image is null) return;
555 //auto imgname = mono_image_get_name(image);
556 //if (imgname is null) return;
557 //scope(exit) g_free(imgname);
558 //auto imgnamestr = imgname.fromStringz.idup;
559 auto tdef = mono_image_get_table_info(image, MONO_TABLE_TYPEDEF);
560 if (tdef is null) return;
561 auto tdefcount = mono_table_info_get_rows(tdef);
562 foreach (immutable int i; 0..tdefcount) {
563 auto c = mono_class_get(image, MONO_TOKEN_TYPE_DEF|i+1);
564 auto name = mono_class_get_name(c);
565 if (name is null) continue;
566 scope(exit) g_free(name);
567 auto nspace = mono_class_get_namespace(c);
568 scope(exit) if (nspace !is null) g_free(nspace);
569 clist ~= NCClassInfo(cast(uint)c, asmid, name.fromStringz.idup, nspace.fromStringz.idup);
573 extern(C) void assemblyEnumeratorForClassList (void* assembly, void* userptr) nothrow {
574 auto image = mono_assembly_get_image(assembly);
575 if (image is null) return;
576 NCClassInfo[]* arr = cast(NCClassInfo[]*)userptr;
577 try {
578 collectAssemblyClasses(cast(uint)assembly, *arr);
579 } catch (Exception) {}
582 export NCClassInfo[] getClassList (uint asmid=0) {
583 NCClassInfo[] arr;
584 if (asmid == 0) {
585 mono_assembly_foreach(cast(GFunc)&assemblyEnumeratorForClassList, &arr);
586 } else {
587 collectAssemblyClasses(asmid, arr);
589 return arr;
593 // ////////////////////////////////////////////////////////////////////////// //
594 export FieldInfo[] getClassFields (uint clsid) {
595 void* c = cast(void*)clsid;
596 auto cname = mono_class_get_name(c);
597 if (cname is null) return null;
598 scope(exit) g_free(cname);
599 void* iter = null;
600 FieldInfo[] res;
601 for (;;) {
602 auto field = mono_class_get_fields(c, &iter);
603 if (field is null) break;
604 auto fieldtype = mono_field_get_type(field);
605 auto name = mono_field_get_name(field);
606 if (name is null) continue;
607 scope(exit) g_free(name);
608 auto tname = mono_type_get_name(fieldtype);
609 if (tname is null) continue;
610 scope(exit) g_free(tname);
611 FieldInfo fi;
612 fi.type = mono_type_get_type(fieldtype);
613 fi.ofs = mono_field_get_offset(field);
614 fi.flags = mono_field_get_flags(field);
615 fi.name = name.fromStringz.idup;
616 fi.typename = tname.fromStringz.idup;
618 WriteQword((UINT_PTR)mono_field_get_parent(field));
619 WriteDword(mono_field_get_flags(field));
621 res ~= fi;
623 return res;