Wed, 02 Nov 2005 05:23:00 +0000
[gaim-migrate @ 14243]
Another patch attached to sf #1285501, from Eoin Coffey
"the loader will now attempt to catch exceptions, so
hopefully this will resolve some of the issues we were having."
This looked fairly innocuous.
committer: Mark Doliner <markdoliner@pidgin.im>
| 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 | |
|
11786
d0067c6e542a
[gaim-migrate @ 14077]
Gary Kramlich <grim@reaperworld.com>
parents:
11679
diff
changeset
|
10 | # include <config.h> |
| 11660 | 11 | #endif |
| 12 | ||
| 13 | #include "internal.h" | |
| 14 | #include "debug.h" | |
| 15 | #include "plugin.h" | |
| 16 | #include "version.h" | |
| 17 | #include "mono-helper.h" | |
| 18 | ||
| 19 | #define MONO_PLUGIN_ID "core-mono" | |
| 20 | ||
|
11786
d0067c6e542a
[gaim-migrate @ 14077]
Gary Kramlich <grim@reaperworld.com>
parents:
11679
diff
changeset
|
21 | /****************************************************************************** |
|
d0067c6e542a
[gaim-migrate @ 14077]
Gary Kramlich <grim@reaperworld.com>
parents:
11679
diff
changeset
|
22 | * Loader Stuff |
|
d0067c6e542a
[gaim-migrate @ 14077]
Gary Kramlich <grim@reaperworld.com>
parents:
11679
diff
changeset
|
23 | *****************************************************************************/ |
| 11660 | 24 | /* probes the given plugin to determine if its a plugin */ |
| 25 | static gboolean probe_mono_plugin(GaimPlugin *plugin) | |
| 26 | { | |
| 27 | MonoAssembly *assm; | |
| 28 | MonoMethod *m = NULL; | |
| 29 | MonoMethod *info_method = NULL; | |
| 30 | MonoObject *plugin_info; | |
| 31 | gboolean found_load = FALSE, found_unload = FALSE, found_destroy = FALSE, found_info = FALSE; | |
| 32 | gpointer iter = NULL; | |
|
11786
d0067c6e542a
[gaim-migrate @ 14077]
Gary Kramlich <grim@reaperworld.com>
parents:
11679
diff
changeset
|
33 | |
| 11660 | 34 | GaimPluginInfo *info; |
| 35 | GaimMonoPlugin *mplug; | |
|
11786
d0067c6e542a
[gaim-migrate @ 14077]
Gary Kramlich <grim@reaperworld.com>
parents:
11679
diff
changeset
|
36 | |
| 11660 | 37 | char *file = plugin->path; |
|
11786
d0067c6e542a
[gaim-migrate @ 14077]
Gary Kramlich <grim@reaperworld.com>
parents:
11679
diff
changeset
|
38 | |
|
11952
9cefc8360bff
[gaim-migrate @ 14243]
Eoin Coffey <ecoffey@soc.pidgin.im>
parents:
11786
diff
changeset
|
39 | assm = mono_domain_assembly_open(mono_loader_get_domain(), file); |
|
11786
d0067c6e542a
[gaim-migrate @ 14077]
Gary Kramlich <grim@reaperworld.com>
parents:
11679
diff
changeset
|
40 | |
| 11660 | 41 | if (!assm) { |
| 42 | return FALSE; | |
| 43 | } | |
|
11786
d0067c6e542a
[gaim-migrate @ 14077]
Gary Kramlich <grim@reaperworld.com>
parents:
11679
diff
changeset
|
44 | |
| 11660 | 45 | gaim_debug(GAIM_DEBUG_INFO, "mono", "Probing plugin\n"); |
|
11786
d0067c6e542a
[gaim-migrate @ 14077]
Gary Kramlich <grim@reaperworld.com>
parents:
11679
diff
changeset
|
46 | |
| 11660 | 47 | if (mono_loader_is_api_dll(mono_assembly_get_image(assm))) { |
| 48 | gaim_debug(GAIM_DEBUG_INFO, "mono", "Found our GaimAPI.dll\n"); | |
| 49 | return FALSE; | |
| 50 | } | |
|
11786
d0067c6e542a
[gaim-migrate @ 14077]
Gary Kramlich <grim@reaperworld.com>
parents:
11679
diff
changeset
|
51 | |
| 11660 | 52 | info = g_new0(GaimPluginInfo, 1); |
| 53 | mplug = g_new0(GaimMonoPlugin, 1); | |
|
11786
d0067c6e542a
[gaim-migrate @ 14077]
Gary Kramlich <grim@reaperworld.com>
parents:
11679
diff
changeset
|
54 | |
| 11660 | 55 | mplug->assm = assm; |
|
11786
d0067c6e542a
[gaim-migrate @ 14077]
Gary Kramlich <grim@reaperworld.com>
parents:
11679
diff
changeset
|
56 | |
| 11660 | 57 | mplug->klass = mono_loader_find_plugin_class(mono_assembly_get_image(mplug->assm)); |
| 58 | if (!mplug->klass) { | |
| 59 | gaim_debug(GAIM_DEBUG_ERROR, "mono", "no plugin class in \'%s\'\n", file); | |
| 60 | return FALSE; | |
| 61 | } | |
|
11786
d0067c6e542a
[gaim-migrate @ 14077]
Gary Kramlich <grim@reaperworld.com>
parents:
11679
diff
changeset
|
62 | |
|
11952
9cefc8360bff
[gaim-migrate @ 14243]
Eoin Coffey <ecoffey@soc.pidgin.im>
parents:
11786
diff
changeset
|
63 | mplug->obj = mono_object_new(mono_loader_get_domain(), mplug->klass); |
| 11660 | 64 | if (!mplug->obj) { |
| 65 | gaim_debug(GAIM_DEBUG_ERROR, "mono", "obj not valid\n"); | |
| 66 | return FALSE; | |
| 67 | } | |
|
11786
d0067c6e542a
[gaim-migrate @ 14077]
Gary Kramlich <grim@reaperworld.com>
parents:
11679
diff
changeset
|
68 | |
| 11660 | 69 | mono_runtime_object_init(mplug->obj); |
|
11786
d0067c6e542a
[gaim-migrate @ 14077]
Gary Kramlich <grim@reaperworld.com>
parents:
11679
diff
changeset
|
70 | |
| 11660 | 71 | while ((m = mono_class_get_methods(mplug->klass, &iter))) { |
| 72 | if (strcmp(mono_method_get_name(m), "Load") == 0) { | |
| 73 | mplug->load = m; | |
| 74 | found_load = TRUE; | |
| 75 | } else if (strcmp(mono_method_get_name(m), "Unload") == 0) { | |
| 76 | mplug->unload = m; | |
| 77 | found_unload = TRUE; | |
| 78 | } else if (strcmp(mono_method_get_name(m), "Destroy") == 0) { | |
| 79 | mplug->destroy = m; | |
| 80 | found_destroy = TRUE; | |
| 81 | } else if (strcmp(mono_method_get_name(m), "Info") == 0) { | |
| 82 | info_method = m; | |
| 83 | found_info = TRUE; | |
| 84 | } | |
| 85 | } | |
|
11786
d0067c6e542a
[gaim-migrate @ 14077]
Gary Kramlich <grim@reaperworld.com>
parents:
11679
diff
changeset
|
86 | |
| 11660 | 87 | if (!(found_load && found_unload && found_destroy && found_info)) { |
| 88 | gaim_debug(GAIM_DEBUG_ERROR, "mono", "did not find the required methods\n"); | |
| 89 | return FALSE; | |
| 90 | } | |
|
11786
d0067c6e542a
[gaim-migrate @ 14077]
Gary Kramlich <grim@reaperworld.com>
parents:
11679
diff
changeset
|
91 | |
|
11952
9cefc8360bff
[gaim-migrate @ 14243]
Eoin Coffey <ecoffey@soc.pidgin.im>
parents:
11786
diff
changeset
|
92 | plugin_info = mono_loader_invoke(info_method, mplug->obj, NULL); |
|
11786
d0067c6e542a
[gaim-migrate @ 14077]
Gary Kramlich <grim@reaperworld.com>
parents:
11679
diff
changeset
|
93 | |
| 11660 | 94 | /* now that the methods are filled out we can populate |
| 95 | the info struct with all the needed info */ | |
|
11786
d0067c6e542a
[gaim-migrate @ 14077]
Gary Kramlich <grim@reaperworld.com>
parents:
11679
diff
changeset
|
96 | |
| 11660 | 97 | info->name = mono_loader_get_prop_string(plugin_info, "Name"); |
| 98 | info->version = mono_loader_get_prop_string(plugin_info, "Version"); | |
| 99 | info->summary = mono_loader_get_prop_string(plugin_info, "Summary"); | |
| 100 | info->description = mono_loader_get_prop_string(plugin_info, "Description"); | |
| 101 | info->author = mono_loader_get_prop_string(plugin_info, "Author"); | |
| 102 | info->homepage = mono_loader_get_prop_string(plugin_info, "Homepage"); | |
|
11786
d0067c6e542a
[gaim-migrate @ 14077]
Gary Kramlich <grim@reaperworld.com>
parents:
11679
diff
changeset
|
103 | |
| 11660 | 104 | info->magic = GAIM_PLUGIN_MAGIC; |
| 105 | info->major_version = GAIM_MAJOR_VERSION; | |
| 106 | info->minor_version = GAIM_MINOR_VERSION; | |
| 107 | info->type = GAIM_PLUGIN_STANDARD; | |
|
11786
d0067c6e542a
[gaim-migrate @ 14077]
Gary Kramlich <grim@reaperworld.com>
parents:
11679
diff
changeset
|
108 | |
| 11660 | 109 | /* this plugin depends on us; duh */ |
| 110 | info->dependencies = g_list_append(info->dependencies, MONO_PLUGIN_ID); | |
| 111 | mplug->plugin = plugin; | |
| 112 | ||
| 113 | plugin->info = info; | |
| 114 | info->extra_info = mplug; | |
|
11786
d0067c6e542a
[gaim-migrate @ 14077]
Gary Kramlich <grim@reaperworld.com>
parents:
11679
diff
changeset
|
115 | |
| 11660 | 116 | mono_loader_add_plugin(mplug); |
|
11786
d0067c6e542a
[gaim-migrate @ 14077]
Gary Kramlich <grim@reaperworld.com>
parents:
11679
diff
changeset
|
117 | |
| 11660 | 118 | return gaim_plugin_register(plugin); |
| 119 | } | |
| 120 | ||
| 121 | /* Loads a Mono Plugin by calling 'load' in the class */ | |
| 122 | static gboolean load_mono_plugin(GaimPlugin *plugin) | |
| 123 | { | |
| 124 | GaimMonoPlugin *mplug; | |
|
11786
d0067c6e542a
[gaim-migrate @ 14077]
Gary Kramlich <grim@reaperworld.com>
parents:
11679
diff
changeset
|
125 | |
| 11660 | 126 | gaim_debug(GAIM_DEBUG_INFO, "mono", "Loading plugin\n"); |
|
11786
d0067c6e542a
[gaim-migrate @ 14077]
Gary Kramlich <grim@reaperworld.com>
parents:
11679
diff
changeset
|
127 | |
| 11660 | 128 | mplug = (GaimMonoPlugin*)plugin->info->extra_info; |
|
11786
d0067c6e542a
[gaim-migrate @ 14077]
Gary Kramlich <grim@reaperworld.com>
parents:
11679
diff
changeset
|
129 | |
|
11952
9cefc8360bff
[gaim-migrate @ 14243]
Eoin Coffey <ecoffey@soc.pidgin.im>
parents:
11786
diff
changeset
|
130 | mono_loader_invoke(mplug->load, mplug->obj, NULL); |
|
11786
d0067c6e542a
[gaim-migrate @ 14077]
Gary Kramlich <grim@reaperworld.com>
parents:
11679
diff
changeset
|
131 | |
| 11660 | 132 | return TRUE; |
| 133 | } | |
| 134 | ||
| 135 | /* Unloads a Mono Plugin by calling 'unload' in the class */ | |
| 136 | static gboolean unload_mono_plugin(GaimPlugin *plugin) | |
| 137 | { | |
| 138 | GaimMonoPlugin *mplug; | |
|
11786
d0067c6e542a
[gaim-migrate @ 14077]
Gary Kramlich <grim@reaperworld.com>
parents:
11679
diff
changeset
|
139 | |
| 11660 | 140 | gaim_debug(GAIM_DEBUG_INFO, "mono", "Unloading plugin\n"); |
|
11786
d0067c6e542a
[gaim-migrate @ 14077]
Gary Kramlich <grim@reaperworld.com>
parents:
11679
diff
changeset
|
141 | |
| 11660 | 142 | mplug = (GaimMonoPlugin*)plugin->info->extra_info; |
|
11786
d0067c6e542a
[gaim-migrate @ 14077]
Gary Kramlich <grim@reaperworld.com>
parents:
11679
diff
changeset
|
143 | |
| 11660 | 144 | gaim_signals_disconnect_by_handle((gpointer)mplug->klass); |
|
11786
d0067c6e542a
[gaim-migrate @ 14077]
Gary Kramlich <grim@reaperworld.com>
parents:
11679
diff
changeset
|
145 | |
|
11952
9cefc8360bff
[gaim-migrate @ 14243]
Eoin Coffey <ecoffey@soc.pidgin.im>
parents:
11786
diff
changeset
|
146 | mono_loader_invoke(mplug->unload, mplug->obj, NULL); |
|
11786
d0067c6e542a
[gaim-migrate @ 14077]
Gary Kramlich <grim@reaperworld.com>
parents:
11679
diff
changeset
|
147 | |
| 11660 | 148 | return TRUE; |
| 149 | } | |
| 150 | ||
|
11679
4d9f3af1ab05
[gaim-migrate @ 13965]
Gary Kramlich <grim@reaperworld.com>
parents:
11660
diff
changeset
|
151 | static void destroy_mono_plugin(GaimPlugin *plugin) |
| 11660 | 152 | { |
| 153 | GaimMonoPlugin *mplug; | |
|
11786
d0067c6e542a
[gaim-migrate @ 14077]
Gary Kramlich <grim@reaperworld.com>
parents:
11679
diff
changeset
|
154 | |
| 11660 | 155 | gaim_debug(GAIM_DEBUG_INFO, "mono", "Destroying plugin\n"); |
|
11786
d0067c6e542a
[gaim-migrate @ 14077]
Gary Kramlich <grim@reaperworld.com>
parents:
11679
diff
changeset
|
156 | |
| 11660 | 157 | mplug = (GaimMonoPlugin*)plugin->info->extra_info; |
|
11786
d0067c6e542a
[gaim-migrate @ 14077]
Gary Kramlich <grim@reaperworld.com>
parents:
11679
diff
changeset
|
158 | |
|
11952
9cefc8360bff
[gaim-migrate @ 14243]
Eoin Coffey <ecoffey@soc.pidgin.im>
parents:
11786
diff
changeset
|
159 | mono_loader_invoke(mplug->destroy, mplug->obj, NULL); |
|
11786
d0067c6e542a
[gaim-migrate @ 14077]
Gary Kramlich <grim@reaperworld.com>
parents:
11679
diff
changeset
|
160 | |
| 11660 | 161 | if (plugin->info) { |
|
11786
d0067c6e542a
[gaim-migrate @ 14077]
Gary Kramlich <grim@reaperworld.com>
parents:
11679
diff
changeset
|
162 | g_free(plugin->info->name); |
|
d0067c6e542a
[gaim-migrate @ 14077]
Gary Kramlich <grim@reaperworld.com>
parents:
11679
diff
changeset
|
163 | g_free(plugin->info->version); |
|
d0067c6e542a
[gaim-migrate @ 14077]
Gary Kramlich <grim@reaperworld.com>
parents:
11679
diff
changeset
|
164 | g_free(plugin->info->summary); |
|
d0067c6e542a
[gaim-migrate @ 14077]
Gary Kramlich <grim@reaperworld.com>
parents:
11679
diff
changeset
|
165 | g_free(plugin->info->description); |
|
d0067c6e542a
[gaim-migrate @ 14077]
Gary Kramlich <grim@reaperworld.com>
parents:
11679
diff
changeset
|
166 | g_free(plugin->info->author); |
|
d0067c6e542a
[gaim-migrate @ 14077]
Gary Kramlich <grim@reaperworld.com>
parents:
11679
diff
changeset
|
167 | g_free(plugin->info->homepage); |
| 11660 | 168 | } |
|
11786
d0067c6e542a
[gaim-migrate @ 14077]
Gary Kramlich <grim@reaperworld.com>
parents:
11679
diff
changeset
|
169 | |
| 11660 | 170 | if (mplug) { |
| 171 | if (mplug->assm) { | |
| 172 | mono_assembly_close(mplug->assm); | |
| 173 | } | |
|
11786
d0067c6e542a
[gaim-migrate @ 14077]
Gary Kramlich <grim@reaperworld.com>
parents:
11679
diff
changeset
|
174 | |
| 11660 | 175 | g_free(mplug); |
| 176 | mplug = NULL; | |
| 177 | } | |
| 178 | } | |
| 179 | ||
|
11786
d0067c6e542a
[gaim-migrate @ 14077]
Gary Kramlich <grim@reaperworld.com>
parents:
11679
diff
changeset
|
180 | /****************************************************************************** |
|
d0067c6e542a
[gaim-migrate @ 14077]
Gary Kramlich <grim@reaperworld.com>
parents:
11679
diff
changeset
|
181 | * Plugin Stuff |
|
d0067c6e542a
[gaim-migrate @ 14077]
Gary Kramlich <grim@reaperworld.com>
parents:
11679
diff
changeset
|
182 | *****************************************************************************/ |
|
11679
4d9f3af1ab05
[gaim-migrate @ 13965]
Gary Kramlich <grim@reaperworld.com>
parents:
11660
diff
changeset
|
183 | static void plugin_destroy(GaimPlugin *plugin) |
| 11660 | 184 | { |
|
11952
9cefc8360bff
[gaim-migrate @ 14243]
Eoin Coffey <ecoffey@soc.pidgin.im>
parents:
11786
diff
changeset
|
185 | mono_jit_cleanup(mono_loader_get_domain()); |
| 11660 | 186 | } |
| 187 | ||
| 188 | static GaimPluginLoaderInfo loader_info = | |
| 189 | { | |
| 190 | NULL, | |
| 191 | probe_mono_plugin, | |
| 192 | load_mono_plugin, | |
| 193 | unload_mono_plugin, | |
| 194 | destroy_mono_plugin | |
| 195 | }; | |
| 196 | ||
| 197 | static GaimPluginInfo info = | |
| 198 | { | |
| 199 | GAIM_PLUGIN_MAGIC, | |
| 200 | GAIM_MAJOR_VERSION, | |
| 201 | GAIM_MINOR_VERSION, | |
| 202 | GAIM_PLUGIN_LOADER, | |
| 203 | NULL, | |
| 204 | 0, | |
| 205 | NULL, | |
| 206 | GAIM_PRIORITY_DEFAULT, | |
| 207 | MONO_PLUGIN_ID, | |
| 208 | N_("Mono Plugin Loader"), | |
| 209 | VERSION, | |
| 210 | N_("Loads .NET plugins with Mono."), | |
| 211 | N_("Loads .NET plugins with Mono."), | |
| 212 | "Eoin Coffey <ecoffey@simla.colostate.edu>", | |
| 213 | GAIM_WEBSITE, | |
| 214 | NULL, | |
| 215 | NULL, | |
| 216 | plugin_destroy, | |
| 217 | NULL, | |
| 218 | &loader_info, | |
| 219 | NULL, | |
| 220 | NULL | |
| 221 | }; | |
| 222 | ||
| 223 | static void init_plugin(GaimPlugin *plugin) | |
| 224 | { | |
|
11952
9cefc8360bff
[gaim-migrate @ 14243]
Eoin Coffey <ecoffey@soc.pidgin.im>
parents:
11786
diff
changeset
|
225 | MonoDomain *domain = mono_jit_init("gaim"); |
|
11786
d0067c6e542a
[gaim-migrate @ 14077]
Gary Kramlich <grim@reaperworld.com>
parents:
11679
diff
changeset
|
226 | |
| 11660 | 227 | mono_loader_set_domain(domain); |
|
11786
d0067c6e542a
[gaim-migrate @ 14077]
Gary Kramlich <grim@reaperworld.com>
parents:
11679
diff
changeset
|
228 | |
| 11660 | 229 | mono_loader_init_internal_calls(); |
|
11786
d0067c6e542a
[gaim-migrate @ 14077]
Gary Kramlich <grim@reaperworld.com>
parents:
11679
diff
changeset
|
230 | |
| 11660 | 231 | loader_info.exts = g_list_append(loader_info.exts, "dll"); |
| 232 | } | |
| 233 | ||
| 234 | GAIM_INIT_PLUGIN(mono, init_plugin, info) |