Sat, 05 Nov 2005 02:09:30 +0000
[gaim-migrate @ 14273]
Mono cleanup patch from Eoin Coffey
First, I changed mono_loader_ to ml_, since I was
getting sick of typing mono_loader_ :-D
Moved the mono runtime init and deinit code out of
mono.c into ml_init and ml_uninit in mono-helper.c
Added api/Status.cs and loader/status-glue.c so the
.net api now knows very little (as in the 'id') of
statuses.
committer: Gary Kramlich <grim@reaperworld.com>
| 11660 | 1 | /* |
| 2 | * Mono Plugin Loader | |
| 3 | * | |
| 4 | * -- Thanks to the perl plugin loader for all the great tips ;-) | |
| 5 | * | |
| 6 | * Eoin Coffey | |
| 7 | */ | |
| 8 | ||
| 9 | #ifdef HAVE_CONFIG_H | |
| 10 | #include <config.h> | |
| 11 | #endif | |
| 12 | ||
| 13 | #include <glib.h> | |
| 14 | #include <string.h> | |
| 15 | #include "mono-helper.h" | |
| 16 | #include "mono-glue.h" | |
| 17 | #include "value.h" | |
|
11952
9cefc8360bff
[gaim-migrate @ 14243]
Eoin Coffey <ecoffey@soc.pidgin.im>
parents:
11786
diff
changeset
|
18 | #include "debug.h" |
|
9cefc8360bff
[gaim-migrate @ 14243]
Eoin Coffey <ecoffey@soc.pidgin.im>
parents:
11786
diff
changeset
|
19 | |
|
11980
fe5c2c58508c
[gaim-migrate @ 14273]
Eoin Coffey <ecoffey@soc.pidgin.im>
parents:
11952
diff
changeset
|
20 | static gboolean _runtime_active = FALSE; |
|
fe5c2c58508c
[gaim-migrate @ 14273]
Eoin Coffey <ecoffey@soc.pidgin.im>
parents:
11952
diff
changeset
|
21 | |
|
fe5c2c58508c
[gaim-migrate @ 14273]
Eoin Coffey <ecoffey@soc.pidgin.im>
parents:
11952
diff
changeset
|
22 | gboolean ml_init() |
|
fe5c2c58508c
[gaim-migrate @ 14273]
Eoin Coffey <ecoffey@soc.pidgin.im>
parents:
11952
diff
changeset
|
23 | { |
|
fe5c2c58508c
[gaim-migrate @ 14273]
Eoin Coffey <ecoffey@soc.pidgin.im>
parents:
11952
diff
changeset
|
24 | MonoDomain *d; |
|
fe5c2c58508c
[gaim-migrate @ 14273]
Eoin Coffey <ecoffey@soc.pidgin.im>
parents:
11952
diff
changeset
|
25 | |
|
fe5c2c58508c
[gaim-migrate @ 14273]
Eoin Coffey <ecoffey@soc.pidgin.im>
parents:
11952
diff
changeset
|
26 | g_return_val_if_fail(_runtime_active == FALSE, TRUE); |
|
fe5c2c58508c
[gaim-migrate @ 14273]
Eoin Coffey <ecoffey@soc.pidgin.im>
parents:
11952
diff
changeset
|
27 | |
|
fe5c2c58508c
[gaim-migrate @ 14273]
Eoin Coffey <ecoffey@soc.pidgin.im>
parents:
11952
diff
changeset
|
28 | d = mono_jit_init("gaim"); |
|
fe5c2c58508c
[gaim-migrate @ 14273]
Eoin Coffey <ecoffey@soc.pidgin.im>
parents:
11952
diff
changeset
|
29 | |
|
fe5c2c58508c
[gaim-migrate @ 14273]
Eoin Coffey <ecoffey@soc.pidgin.im>
parents:
11952
diff
changeset
|
30 | if (!d) { |
|
fe5c2c58508c
[gaim-migrate @ 14273]
Eoin Coffey <ecoffey@soc.pidgin.im>
parents:
11952
diff
changeset
|
31 | ml_set_domain(NULL); |
|
fe5c2c58508c
[gaim-migrate @ 14273]
Eoin Coffey <ecoffey@soc.pidgin.im>
parents:
11952
diff
changeset
|
32 | return FALSE; |
|
fe5c2c58508c
[gaim-migrate @ 14273]
Eoin Coffey <ecoffey@soc.pidgin.im>
parents:
11952
diff
changeset
|
33 | } |
|
fe5c2c58508c
[gaim-migrate @ 14273]
Eoin Coffey <ecoffey@soc.pidgin.im>
parents:
11952
diff
changeset
|
34 | |
|
fe5c2c58508c
[gaim-migrate @ 14273]
Eoin Coffey <ecoffey@soc.pidgin.im>
parents:
11952
diff
changeset
|
35 | ml_set_domain(d); |
|
fe5c2c58508c
[gaim-migrate @ 14273]
Eoin Coffey <ecoffey@soc.pidgin.im>
parents:
11952
diff
changeset
|
36 | |
|
fe5c2c58508c
[gaim-migrate @ 14273]
Eoin Coffey <ecoffey@soc.pidgin.im>
parents:
11952
diff
changeset
|
37 | ml_init_internal_calls(); |
|
fe5c2c58508c
[gaim-migrate @ 14273]
Eoin Coffey <ecoffey@soc.pidgin.im>
parents:
11952
diff
changeset
|
38 | |
|
fe5c2c58508c
[gaim-migrate @ 14273]
Eoin Coffey <ecoffey@soc.pidgin.im>
parents:
11952
diff
changeset
|
39 | _runtime_active = TRUE; |
|
fe5c2c58508c
[gaim-migrate @ 14273]
Eoin Coffey <ecoffey@soc.pidgin.im>
parents:
11952
diff
changeset
|
40 | |
|
fe5c2c58508c
[gaim-migrate @ 14273]
Eoin Coffey <ecoffey@soc.pidgin.im>
parents:
11952
diff
changeset
|
41 | return TRUE; |
|
fe5c2c58508c
[gaim-migrate @ 14273]
Eoin Coffey <ecoffey@soc.pidgin.im>
parents:
11952
diff
changeset
|
42 | } |
|
fe5c2c58508c
[gaim-migrate @ 14273]
Eoin Coffey <ecoffey@soc.pidgin.im>
parents:
11952
diff
changeset
|
43 | |
|
fe5c2c58508c
[gaim-migrate @ 14273]
Eoin Coffey <ecoffey@soc.pidgin.im>
parents:
11952
diff
changeset
|
44 | void ml_uninit() |
|
fe5c2c58508c
[gaim-migrate @ 14273]
Eoin Coffey <ecoffey@soc.pidgin.im>
parents:
11952
diff
changeset
|
45 | { |
|
fe5c2c58508c
[gaim-migrate @ 14273]
Eoin Coffey <ecoffey@soc.pidgin.im>
parents:
11952
diff
changeset
|
46 | g_return_if_fail(_runtime_active == TRUE); |
|
fe5c2c58508c
[gaim-migrate @ 14273]
Eoin Coffey <ecoffey@soc.pidgin.im>
parents:
11952
diff
changeset
|
47 | |
|
fe5c2c58508c
[gaim-migrate @ 14273]
Eoin Coffey <ecoffey@soc.pidgin.im>
parents:
11952
diff
changeset
|
48 | mono_jit_cleanup(ml_get_domain()); |
|
fe5c2c58508c
[gaim-migrate @ 14273]
Eoin Coffey <ecoffey@soc.pidgin.im>
parents:
11952
diff
changeset
|
49 | |
|
fe5c2c58508c
[gaim-migrate @ 14273]
Eoin Coffey <ecoffey@soc.pidgin.im>
parents:
11952
diff
changeset
|
50 | ml_set_domain(NULL); |
|
fe5c2c58508c
[gaim-migrate @ 14273]
Eoin Coffey <ecoffey@soc.pidgin.im>
parents:
11952
diff
changeset
|
51 | |
|
fe5c2c58508c
[gaim-migrate @ 14273]
Eoin Coffey <ecoffey@soc.pidgin.im>
parents:
11952
diff
changeset
|
52 | _runtime_active = FALSE; |
|
fe5c2c58508c
[gaim-migrate @ 14273]
Eoin Coffey <ecoffey@soc.pidgin.im>
parents:
11952
diff
changeset
|
53 | } |
|
fe5c2c58508c
[gaim-migrate @ 14273]
Eoin Coffey <ecoffey@soc.pidgin.im>
parents:
11952
diff
changeset
|
54 | |
|
fe5c2c58508c
[gaim-migrate @ 14273]
Eoin Coffey <ecoffey@soc.pidgin.im>
parents:
11952
diff
changeset
|
55 | MonoObject* ml_delegate_invoke(MonoObject *method, void **params) |
|
11952
9cefc8360bff
[gaim-migrate @ 14243]
Eoin Coffey <ecoffey@soc.pidgin.im>
parents:
11786
diff
changeset
|
56 | { |
|
9cefc8360bff
[gaim-migrate @ 14243]
Eoin Coffey <ecoffey@soc.pidgin.im>
parents:
11786
diff
changeset
|
57 | MonoObject *ret, *exception; |
|
9cefc8360bff
[gaim-migrate @ 14243]
Eoin Coffey <ecoffey@soc.pidgin.im>
parents:
11786
diff
changeset
|
58 | |
|
9cefc8360bff
[gaim-migrate @ 14243]
Eoin Coffey <ecoffey@soc.pidgin.im>
parents:
11786
diff
changeset
|
59 | ret = mono_runtime_delegate_invoke(method, params, &exception); |
|
9cefc8360bff
[gaim-migrate @ 14243]
Eoin Coffey <ecoffey@soc.pidgin.im>
parents:
11786
diff
changeset
|
60 | if (exception) { |
|
9cefc8360bff
[gaim-migrate @ 14243]
Eoin Coffey <ecoffey@soc.pidgin.im>
parents:
11786
diff
changeset
|
61 | gaim_debug(GAIM_DEBUG_ERROR, "mono", "caught exception: %s\n", mono_class_get_name(mono_object_get_class(exception))); |
|
9cefc8360bff
[gaim-migrate @ 14243]
Eoin Coffey <ecoffey@soc.pidgin.im>
parents:
11786
diff
changeset
|
62 | } |
|
9cefc8360bff
[gaim-migrate @ 14243]
Eoin Coffey <ecoffey@soc.pidgin.im>
parents:
11786
diff
changeset
|
63 | |
|
9cefc8360bff
[gaim-migrate @ 14243]
Eoin Coffey <ecoffey@soc.pidgin.im>
parents:
11786
diff
changeset
|
64 | return ret; |
|
9cefc8360bff
[gaim-migrate @ 14243]
Eoin Coffey <ecoffey@soc.pidgin.im>
parents:
11786
diff
changeset
|
65 | } |
|
9cefc8360bff
[gaim-migrate @ 14243]
Eoin Coffey <ecoffey@soc.pidgin.im>
parents:
11786
diff
changeset
|
66 | |
|
11980
fe5c2c58508c
[gaim-migrate @ 14273]
Eoin Coffey <ecoffey@soc.pidgin.im>
parents:
11952
diff
changeset
|
67 | MonoObject* ml_invoke(MonoMethod *method, void *obj, void **params) |
|
11952
9cefc8360bff
[gaim-migrate @ 14243]
Eoin Coffey <ecoffey@soc.pidgin.im>
parents:
11786
diff
changeset
|
68 | { |
|
9cefc8360bff
[gaim-migrate @ 14243]
Eoin Coffey <ecoffey@soc.pidgin.im>
parents:
11786
diff
changeset
|
69 | MonoObject *ret, *exception; |
|
9cefc8360bff
[gaim-migrate @ 14243]
Eoin Coffey <ecoffey@soc.pidgin.im>
parents:
11786
diff
changeset
|
70 | |
|
9cefc8360bff
[gaim-migrate @ 14243]
Eoin Coffey <ecoffey@soc.pidgin.im>
parents:
11786
diff
changeset
|
71 | ret = mono_runtime_invoke(method, obj, params, &exception); |
|
9cefc8360bff
[gaim-migrate @ 14243]
Eoin Coffey <ecoffey@soc.pidgin.im>
parents:
11786
diff
changeset
|
72 | if (exception) { |
|
9cefc8360bff
[gaim-migrate @ 14243]
Eoin Coffey <ecoffey@soc.pidgin.im>
parents:
11786
diff
changeset
|
73 | gaim_debug(GAIM_DEBUG_ERROR, "mono", "caught exception: %s\n", mono_class_get_name(mono_object_get_class(exception))); |
|
9cefc8360bff
[gaim-migrate @ 14243]
Eoin Coffey <ecoffey@soc.pidgin.im>
parents:
11786
diff
changeset
|
74 | } |
|
9cefc8360bff
[gaim-migrate @ 14243]
Eoin Coffey <ecoffey@soc.pidgin.im>
parents:
11786
diff
changeset
|
75 | |
|
9cefc8360bff
[gaim-migrate @ 14243]
Eoin Coffey <ecoffey@soc.pidgin.im>
parents:
11786
diff
changeset
|
76 | return ret; |
|
9cefc8360bff
[gaim-migrate @ 14243]
Eoin Coffey <ecoffey@soc.pidgin.im>
parents:
11786
diff
changeset
|
77 | } |
| 11660 | 78 | |
|
11980
fe5c2c58508c
[gaim-migrate @ 14273]
Eoin Coffey <ecoffey@soc.pidgin.im>
parents:
11952
diff
changeset
|
79 | MonoClass* ml_find_plugin_class(MonoImage *image) |
| 11660 | 80 | { |
| 81 | MonoClass *klass, *pklass = NULL; | |
| 82 | int i, total; | |
| 83 | ||
| 84 | total = mono_image_get_table_rows (image, MONO_TABLE_TYPEDEF); | |
| 85 | for (i = 1; i <= total; ++i) { | |
| 86 | klass = mono_class_get (image, MONO_TOKEN_TYPE_DEF | i); | |
| 87 | pklass = mono_class_get_parent(klass); | |
| 88 | if (pklass) | |
| 89 | if (strcmp("GaimPlugin", mono_class_get_name(pklass)) == 0) | |
| 90 | return klass; | |
| 91 | } | |
| 92 | ||
| 93 | return NULL; | |
| 94 | } | |
| 95 | ||
|
11980
fe5c2c58508c
[gaim-migrate @ 14273]
Eoin Coffey <ecoffey@soc.pidgin.im>
parents:
11952
diff
changeset
|
96 | void ml_set_prop_string(MonoObject *obj, char *field, char *data) |
| 11660 | 97 | { |
| 98 | MonoClass *klass; | |
| 99 | MonoProperty *prop; | |
| 100 | MonoString *str; | |
| 101 | gpointer args[1]; | |
| 102 | ||
| 103 | klass = mono_object_get_class(obj); | |
| 104 | ||
| 105 | prop = mono_class_get_property_from_name(klass, field); | |
| 106 | ||
|
11980
fe5c2c58508c
[gaim-migrate @ 14273]
Eoin Coffey <ecoffey@soc.pidgin.im>
parents:
11952
diff
changeset
|
107 | str = mono_string_new(ml_get_domain(), data); |
| 11660 | 108 | |
| 109 | args[0] = str; | |
| 110 | ||
| 111 | mono_property_set_value(prop, obj, args, NULL); | |
| 112 | } | |
| 113 | ||
|
11980
fe5c2c58508c
[gaim-migrate @ 14273]
Eoin Coffey <ecoffey@soc.pidgin.im>
parents:
11952
diff
changeset
|
114 | gchar* ml_get_prop_string(MonoObject *obj, char *field) |
| 11660 | 115 | { |
| 116 | MonoClass *klass; | |
| 117 | MonoProperty *prop; | |
| 118 | MonoString *str; | |
| 119 | ||
| 120 | klass = mono_object_get_class(obj); | |
| 121 | ||
| 122 | prop = mono_class_get_property_from_name(klass, field); | |
| 123 | ||
| 124 | str = (MonoString*)mono_property_get_value(prop, obj, NULL, NULL); | |
| 125 | ||
| 126 | return mono_string_to_utf8(str); | |
| 127 | } | |
| 128 | ||
|
11980
fe5c2c58508c
[gaim-migrate @ 14273]
Eoin Coffey <ecoffey@soc.pidgin.im>
parents:
11952
diff
changeset
|
129 | gboolean ml_is_api_dll(MonoImage *image) |
| 11660 | 130 | { |
| 131 | MonoClass *klass; | |
| 132 | int i, total; | |
| 133 | ||
| 134 | total = mono_image_get_table_rows (image, MONO_TABLE_TYPEDEF); | |
| 135 | for (i = 1; i <= total; ++i) { | |
| 136 | klass = mono_class_get (image, MONO_TOKEN_TYPE_DEF | i); | |
| 137 | if (strcmp(mono_class_get_name(klass), "Debug") == 0) | |
| 138 | if (strcmp(mono_class_get_namespace(klass), "Gaim") == 0) { | |
|
11980
fe5c2c58508c
[gaim-migrate @ 14273]
Eoin Coffey <ecoffey@soc.pidgin.im>
parents:
11952
diff
changeset
|
139 | ml_set_api_image(image); |
| 11660 | 140 | return TRUE; |
| 141 | } | |
| 142 | } | |
| 143 | ||
| 144 | return FALSE; | |
| 145 | } | |
| 146 | ||
|
11980
fe5c2c58508c
[gaim-migrate @ 14273]
Eoin Coffey <ecoffey@soc.pidgin.im>
parents:
11952
diff
changeset
|
147 | MonoObject* ml_object_from_gaim_type(GaimType type, gpointer data) |
| 11660 | 148 | { |
| 149 | return NULL; | |
| 150 | } | |
| 151 | ||
|
11980
fe5c2c58508c
[gaim-migrate @ 14273]
Eoin Coffey <ecoffey@soc.pidgin.im>
parents:
11952
diff
changeset
|
152 | MonoObject* ml_object_from_gaim_subtype(GaimSubType type, gpointer data) |
| 11660 | 153 | { |
|
11776
58fd4b78788a
[gaim-migrate @ 14067]
Gary Kramlich <grim@reaperworld.com>
parents:
11660
diff
changeset
|
154 | MonoObject *obj = NULL; |
| 11660 | 155 | |
| 156 | switch (type) { | |
| 157 | case GAIM_SUBTYPE_BLIST_BUDDY: | |
| 158 | obj = gaim_blist_build_buddy_object(data); | |
| 159 | break; | |
|
11980
fe5c2c58508c
[gaim-migrate @ 14273]
Eoin Coffey <ecoffey@soc.pidgin.im>
parents:
11952
diff
changeset
|
160 | case GAIM_SUBTYPE_STATUS: |
|
fe5c2c58508c
[gaim-migrate @ 14273]
Eoin Coffey <ecoffey@soc.pidgin.im>
parents:
11952
diff
changeset
|
161 | obj = gaim_status_build_status_object(data); |
|
fe5c2c58508c
[gaim-migrate @ 14273]
Eoin Coffey <ecoffey@soc.pidgin.im>
parents:
11952
diff
changeset
|
162 | break; |
| 11660 | 163 | default: |
| 164 | break; | |
| 165 | } | |
| 166 | ||
| 167 | return obj; | |
| 168 | } | |
| 169 | ||
|
11786
d0067c6e542a
[gaim-migrate @ 14077]
Gary Kramlich <grim@reaperworld.com>
parents:
11776
diff
changeset
|
170 | static MonoDomain *_domain = NULL; |
| 11660 | 171 | |
|
11980
fe5c2c58508c
[gaim-migrate @ 14273]
Eoin Coffey <ecoffey@soc.pidgin.im>
parents:
11952
diff
changeset
|
172 | MonoDomain* ml_get_domain(void) |
| 11660 | 173 | { |
| 174 | return _domain; | |
| 175 | } | |
| 176 | ||
|
11980
fe5c2c58508c
[gaim-migrate @ 14273]
Eoin Coffey <ecoffey@soc.pidgin.im>
parents:
11952
diff
changeset
|
177 | void ml_set_domain(MonoDomain *d) |
| 11660 | 178 | { |
| 179 | _domain = d; | |
| 180 | } | |
| 181 | ||
| 182 | static MonoImage *_api_image = NULL; | |
| 183 | ||
|
11980
fe5c2c58508c
[gaim-migrate @ 14273]
Eoin Coffey <ecoffey@soc.pidgin.im>
parents:
11952
diff
changeset
|
184 | void ml_set_api_image(MonoImage *image) |
| 11660 | 185 | { |
| 186 | _api_image = image; | |
| 187 | } | |
| 188 | ||
|
11980
fe5c2c58508c
[gaim-migrate @ 14273]
Eoin Coffey <ecoffey@soc.pidgin.im>
parents:
11952
diff
changeset
|
189 | MonoImage* ml_get_api_image() |
| 11660 | 190 | { |
| 191 | return _api_image; | |
| 192 | } | |
| 193 | ||
|
11980
fe5c2c58508c
[gaim-migrate @ 14273]
Eoin Coffey <ecoffey@soc.pidgin.im>
parents:
11952
diff
changeset
|
194 | void ml_init_internal_calls(void) |
| 11660 | 195 | { |
| 196 | mono_add_internal_call("Gaim.Debug::_debug", gaim_debug_glue); | |
| 197 | mono_add_internal_call("Gaim.Signal::_connect", gaim_signal_connect_glue); | |
| 198 | mono_add_internal_call("Gaim.BuddyList::_get_handle", gaim_blist_get_handle_glue); | |
| 199 | } | |
| 200 | ||
|
11980
fe5c2c58508c
[gaim-migrate @ 14273]
Eoin Coffey <ecoffey@soc.pidgin.im>
parents:
11952
diff
changeset
|
201 | void ml_destroy_signal_data(gpointer data, gpointer user_data) |
|
fe5c2c58508c
[gaim-migrate @ 14273]
Eoin Coffey <ecoffey@soc.pidgin.im>
parents:
11952
diff
changeset
|
202 | { |
|
fe5c2c58508c
[gaim-migrate @ 14273]
Eoin Coffey <ecoffey@soc.pidgin.im>
parents:
11952
diff
changeset
|
203 | g_free(data); |
|
fe5c2c58508c
[gaim-migrate @ 14273]
Eoin Coffey <ecoffey@soc.pidgin.im>
parents:
11952
diff
changeset
|
204 | } |
|
fe5c2c58508c
[gaim-migrate @ 14273]
Eoin Coffey <ecoffey@soc.pidgin.im>
parents:
11952
diff
changeset
|
205 | |
| 11660 | 206 | static GHashTable *plugins_hash = NULL; |
| 207 | ||
|
11980
fe5c2c58508c
[gaim-migrate @ 14273]
Eoin Coffey <ecoffey@soc.pidgin.im>
parents:
11952
diff
changeset
|
208 | void ml_add_plugin(GaimMonoPlugin *plugin) |
| 11660 | 209 | { |
| 210 | if (!plugins_hash) | |
| 211 | plugins_hash = g_hash_table_new(NULL, NULL); | |
| 212 | ||
| 213 | g_hash_table_insert(plugins_hash, plugin->klass, plugin); | |
| 214 | } | |
| 215 | ||
|
11980
fe5c2c58508c
[gaim-migrate @ 14273]
Eoin Coffey <ecoffey@soc.pidgin.im>
parents:
11952
diff
changeset
|
216 | gboolean ml_remove_plugin(GaimMonoPlugin *plugin) |
| 11660 | 217 | { |
| 218 | return g_hash_table_remove(plugins_hash, plugin->klass); | |
| 219 | } | |
| 220 | ||
|
11980
fe5c2c58508c
[gaim-migrate @ 14273]
Eoin Coffey <ecoffey@soc.pidgin.im>
parents:
11952
diff
changeset
|
221 | gpointer ml_find_plugin(GaimMonoPlugin *plugin) |
| 11660 | 222 | { |
| 223 | return g_hash_table_lookup(plugins_hash, plugin->klass); | |
| 224 | } | |
| 225 | ||
|
11980
fe5c2c58508c
[gaim-migrate @ 14273]
Eoin Coffey <ecoffey@soc.pidgin.im>
parents:
11952
diff
changeset
|
226 | gpointer ml_find_plugin_by_class(MonoClass *klass) |
| 11660 | 227 | { |
| 228 | return g_hash_table_lookup(plugins_hash, klass); | |
| 229 | } | |
| 230 | ||
|
11980
fe5c2c58508c
[gaim-migrate @ 14273]
Eoin Coffey <ecoffey@soc.pidgin.im>
parents:
11952
diff
changeset
|
231 | GHashTable* ml_get_plugin_hash() |
| 11660 | 232 | { |
| 233 | return plugins_hash; | |
| 234 | } |