Mercurial > pidgin
annotate plugins/mono/loader/mono.c @ 11856:fa7b84231ece
[gaim-migrate @ 14147]
Merge my changes for the gtkconv buddy_updated_cb stuff. Nothing to see here, sadrul covered the important stuff.
committer: Tailor Script <tailor@pidgin.im>
author | Richard Laager <rlaager@wiktel.com> |
---|---|
date | Thu, 27 Oct 2005 19:29:15 +0000 |
parents | 2c8216659a84 |
children | 565d2e437c04 |
rev | line source |
---|---|
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
2c8216659a84
[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 | |
21 /* This is where our code executes */ | |
11786
2c8216659a84
[gaim-migrate @ 14077]
Gary Kramlich <grim@reaperworld.com>
parents:
11679
diff
changeset
|
22 static MonoDomain *domain = NULL; |
11660 | 23 |
11786
2c8216659a84
[gaim-migrate @ 14077]
Gary Kramlich <grim@reaperworld.com>
parents:
11679
diff
changeset
|
24 /****************************************************************************** |
2c8216659a84
[gaim-migrate @ 14077]
Gary Kramlich <grim@reaperworld.com>
parents:
11679
diff
changeset
|
25 * Loader Stuff |
2c8216659a84
[gaim-migrate @ 14077]
Gary Kramlich <grim@reaperworld.com>
parents:
11679
diff
changeset
|
26 *****************************************************************************/ |
11660 | 27 /* probes the given plugin to determine if its a plugin */ |
28 static gboolean probe_mono_plugin(GaimPlugin *plugin) | |
29 { | |
30 MonoAssembly *assm; | |
31 MonoMethod *m = NULL; | |
32 MonoMethod *info_method = NULL; | |
33 MonoObject *plugin_info; | |
34 gboolean found_load = FALSE, found_unload = FALSE, found_destroy = FALSE, found_info = FALSE; | |
35 gpointer iter = NULL; | |
11786
2c8216659a84
[gaim-migrate @ 14077]
Gary Kramlich <grim@reaperworld.com>
parents:
11679
diff
changeset
|
36 |
11660 | 37 GaimPluginInfo *info; |
38 GaimMonoPlugin *mplug; | |
11786
2c8216659a84
[gaim-migrate @ 14077]
Gary Kramlich <grim@reaperworld.com>
parents:
11679
diff
changeset
|
39 |
11660 | 40 char *file = plugin->path; |
11786
2c8216659a84
[gaim-migrate @ 14077]
Gary Kramlich <grim@reaperworld.com>
parents:
11679
diff
changeset
|
41 |
11660 | 42 assm = mono_domain_assembly_open(domain, file); |
11786
2c8216659a84
[gaim-migrate @ 14077]
Gary Kramlich <grim@reaperworld.com>
parents:
11679
diff
changeset
|
43 |
11660 | 44 if (!assm) { |
45 return FALSE; | |
46 } | |
11786
2c8216659a84
[gaim-migrate @ 14077]
Gary Kramlich <grim@reaperworld.com>
parents:
11679
diff
changeset
|
47 |
11660 | 48 gaim_debug(GAIM_DEBUG_INFO, "mono", "Probing plugin\n"); |
11786
2c8216659a84
[gaim-migrate @ 14077]
Gary Kramlich <grim@reaperworld.com>
parents:
11679
diff
changeset
|
49 |
11660 | 50 if (mono_loader_is_api_dll(mono_assembly_get_image(assm))) { |
51 gaim_debug(GAIM_DEBUG_INFO, "mono", "Found our GaimAPI.dll\n"); | |
52 return FALSE; | |
53 } | |
11786
2c8216659a84
[gaim-migrate @ 14077]
Gary Kramlich <grim@reaperworld.com>
parents:
11679
diff
changeset
|
54 |
11660 | 55 info = g_new0(GaimPluginInfo, 1); |
56 mplug = g_new0(GaimMonoPlugin, 1); | |
11786
2c8216659a84
[gaim-migrate @ 14077]
Gary Kramlich <grim@reaperworld.com>
parents:
11679
diff
changeset
|
57 |
11660 | 58 mplug->assm = assm; |
11786
2c8216659a84
[gaim-migrate @ 14077]
Gary Kramlich <grim@reaperworld.com>
parents:
11679
diff
changeset
|
59 |
11660 | 60 mplug->klass = mono_loader_find_plugin_class(mono_assembly_get_image(mplug->assm)); |
61 if (!mplug->klass) { | |
62 gaim_debug(GAIM_DEBUG_ERROR, "mono", "no plugin class in \'%s\'\n", file); | |
63 return FALSE; | |
64 } | |
11786
2c8216659a84
[gaim-migrate @ 14077]
Gary Kramlich <grim@reaperworld.com>
parents:
11679
diff
changeset
|
65 |
11660 | 66 mplug->obj = mono_object_new(domain, mplug->klass); |
67 if (!mplug->obj) { | |
68 gaim_debug(GAIM_DEBUG_ERROR, "mono", "obj not valid\n"); | |
69 return FALSE; | |
70 } | |
11786
2c8216659a84
[gaim-migrate @ 14077]
Gary Kramlich <grim@reaperworld.com>
parents:
11679
diff
changeset
|
71 |
11660 | 72 mono_runtime_object_init(mplug->obj); |
11786
2c8216659a84
[gaim-migrate @ 14077]
Gary Kramlich <grim@reaperworld.com>
parents:
11679
diff
changeset
|
73 |
11660 | 74 while ((m = mono_class_get_methods(mplug->klass, &iter))) { |
75 if (strcmp(mono_method_get_name(m), "Load") == 0) { | |
76 mplug->load = m; | |
77 found_load = TRUE; | |
78 } else if (strcmp(mono_method_get_name(m), "Unload") == 0) { | |
79 mplug->unload = m; | |
80 found_unload = TRUE; | |
81 } else if (strcmp(mono_method_get_name(m), "Destroy") == 0) { | |
82 mplug->destroy = m; | |
83 found_destroy = TRUE; | |
84 } else if (strcmp(mono_method_get_name(m), "Info") == 0) { | |
85 info_method = m; | |
86 found_info = TRUE; | |
87 } | |
88 } | |
11786
2c8216659a84
[gaim-migrate @ 14077]
Gary Kramlich <grim@reaperworld.com>
parents:
11679
diff
changeset
|
89 |
11660 | 90 if (!(found_load && found_unload && found_destroy && found_info)) { |
91 gaim_debug(GAIM_DEBUG_ERROR, "mono", "did not find the required methods\n"); | |
92 return FALSE; | |
93 } | |
11786
2c8216659a84
[gaim-migrate @ 14077]
Gary Kramlich <grim@reaperworld.com>
parents:
11679
diff
changeset
|
94 |
11660 | 95 plugin_info = mono_runtime_invoke(info_method, mplug->obj, NULL, NULL); |
11786
2c8216659a84
[gaim-migrate @ 14077]
Gary Kramlich <grim@reaperworld.com>
parents:
11679
diff
changeset
|
96 |
11660 | 97 /* now that the methods are filled out we can populate |
98 the info struct with all the needed info */ | |
11786
2c8216659a84
[gaim-migrate @ 14077]
Gary Kramlich <grim@reaperworld.com>
parents:
11679
diff
changeset
|
99 |
11660 | 100 info->name = mono_loader_get_prop_string(plugin_info, "Name"); |
101 info->version = mono_loader_get_prop_string(plugin_info, "Version"); | |
102 info->summary = mono_loader_get_prop_string(plugin_info, "Summary"); | |
103 info->description = mono_loader_get_prop_string(plugin_info, "Description"); | |
104 info->author = mono_loader_get_prop_string(plugin_info, "Author"); | |
105 info->homepage = mono_loader_get_prop_string(plugin_info, "Homepage"); | |
11786
2c8216659a84
[gaim-migrate @ 14077]
Gary Kramlich <grim@reaperworld.com>
parents:
11679
diff
changeset
|
106 |
11660 | 107 info->magic = GAIM_PLUGIN_MAGIC; |
108 info->major_version = GAIM_MAJOR_VERSION; | |
109 info->minor_version = GAIM_MINOR_VERSION; | |
110 info->type = GAIM_PLUGIN_STANDARD; | |
11786
2c8216659a84
[gaim-migrate @ 14077]
Gary Kramlich <grim@reaperworld.com>
parents:
11679
diff
changeset
|
111 |
11660 | 112 /* this plugin depends on us; duh */ |
113 info->dependencies = g_list_append(info->dependencies, MONO_PLUGIN_ID); | |
114 mplug->plugin = plugin; | |
115 | |
116 plugin->info = info; | |
117 info->extra_info = mplug; | |
11786
2c8216659a84
[gaim-migrate @ 14077]
Gary Kramlich <grim@reaperworld.com>
parents:
11679
diff
changeset
|
118 |
11660 | 119 mono_loader_add_plugin(mplug); |
11786
2c8216659a84
[gaim-migrate @ 14077]
Gary Kramlich <grim@reaperworld.com>
parents:
11679
diff
changeset
|
120 |
11660 | 121 return gaim_plugin_register(plugin); |
122 } | |
123 | |
124 /* Loads a Mono Plugin by calling 'load' in the class */ | |
125 static gboolean load_mono_plugin(GaimPlugin *plugin) | |
126 { | |
127 GaimMonoPlugin *mplug; | |
11786
2c8216659a84
[gaim-migrate @ 14077]
Gary Kramlich <grim@reaperworld.com>
parents:
11679
diff
changeset
|
128 |
11660 | 129 gaim_debug(GAIM_DEBUG_INFO, "mono", "Loading plugin\n"); |
11786
2c8216659a84
[gaim-migrate @ 14077]
Gary Kramlich <grim@reaperworld.com>
parents:
11679
diff
changeset
|
130 |
11660 | 131 mplug = (GaimMonoPlugin*)plugin->info->extra_info; |
11786
2c8216659a84
[gaim-migrate @ 14077]
Gary Kramlich <grim@reaperworld.com>
parents:
11679
diff
changeset
|
132 |
11660 | 133 mono_runtime_invoke(mplug->load, mplug->obj, NULL, NULL); |
11786
2c8216659a84
[gaim-migrate @ 14077]
Gary Kramlich <grim@reaperworld.com>
parents:
11679
diff
changeset
|
134 |
11660 | 135 return TRUE; |
136 } | |
137 | |
138 /* Unloads a Mono Plugin by calling 'unload' in the class */ | |
139 static gboolean unload_mono_plugin(GaimPlugin *plugin) | |
140 { | |
141 GaimMonoPlugin *mplug; | |
11786
2c8216659a84
[gaim-migrate @ 14077]
Gary Kramlich <grim@reaperworld.com>
parents:
11679
diff
changeset
|
142 |
11660 | 143 gaim_debug(GAIM_DEBUG_INFO, "mono", "Unloading plugin\n"); |
11786
2c8216659a84
[gaim-migrate @ 14077]
Gary Kramlich <grim@reaperworld.com>
parents:
11679
diff
changeset
|
144 |
11660 | 145 mplug = (GaimMonoPlugin*)plugin->info->extra_info; |
11786
2c8216659a84
[gaim-migrate @ 14077]
Gary Kramlich <grim@reaperworld.com>
parents:
11679
diff
changeset
|
146 |
11660 | 147 gaim_signals_disconnect_by_handle((gpointer)mplug->klass); |
11786
2c8216659a84
[gaim-migrate @ 14077]
Gary Kramlich <grim@reaperworld.com>
parents:
11679
diff
changeset
|
148 |
11660 | 149 mono_runtime_invoke(mplug->unload, mplug->obj, NULL, NULL); |
11786
2c8216659a84
[gaim-migrate @ 14077]
Gary Kramlich <grim@reaperworld.com>
parents:
11679
diff
changeset
|
150 |
11660 | 151 return TRUE; |
152 } | |
153 | |
154 /* Destroys a Mono Plugin by calling 'destroy' in the class, | |
155 and cleaning up all the malloced memory */ | |
11679
f05542391cd2
[gaim-migrate @ 13965]
Gary Kramlich <grim@reaperworld.com>
parents:
11660
diff
changeset
|
156 static void destroy_mono_plugin(GaimPlugin *plugin) |
11660 | 157 { |
158 GaimMonoPlugin *mplug; | |
11786
2c8216659a84
[gaim-migrate @ 14077]
Gary Kramlich <grim@reaperworld.com>
parents:
11679
diff
changeset
|
159 |
11660 | 160 gaim_debug(GAIM_DEBUG_INFO, "mono", "Destroying plugin\n"); |
11786
2c8216659a84
[gaim-migrate @ 14077]
Gary Kramlich <grim@reaperworld.com>
parents:
11679
diff
changeset
|
161 |
11660 | 162 mplug = (GaimMonoPlugin*)plugin->info->extra_info; |
11786
2c8216659a84
[gaim-migrate @ 14077]
Gary Kramlich <grim@reaperworld.com>
parents:
11679
diff
changeset
|
163 |
11660 | 164 mono_runtime_invoke(mplug->destroy, mplug->obj, NULL, NULL); |
11786
2c8216659a84
[gaim-migrate @ 14077]
Gary Kramlich <grim@reaperworld.com>
parents:
11679
diff
changeset
|
165 |
11660 | 166 if (plugin->info) { |
11786
2c8216659a84
[gaim-migrate @ 14077]
Gary Kramlich <grim@reaperworld.com>
parents:
11679
diff
changeset
|
167 g_free(plugin->info->name); |
2c8216659a84
[gaim-migrate @ 14077]
Gary Kramlich <grim@reaperworld.com>
parents:
11679
diff
changeset
|
168 g_free(plugin->info->version); |
2c8216659a84
[gaim-migrate @ 14077]
Gary Kramlich <grim@reaperworld.com>
parents:
11679
diff
changeset
|
169 g_free(plugin->info->summary); |
2c8216659a84
[gaim-migrate @ 14077]
Gary Kramlich <grim@reaperworld.com>
parents:
11679
diff
changeset
|
170 g_free(plugin->info->description); |
2c8216659a84
[gaim-migrate @ 14077]
Gary Kramlich <grim@reaperworld.com>
parents:
11679
diff
changeset
|
171 g_free(plugin->info->author); |
2c8216659a84
[gaim-migrate @ 14077]
Gary Kramlich <grim@reaperworld.com>
parents:
11679
diff
changeset
|
172 g_free(plugin->info->homepage); |
11660 | 173 } |
11786
2c8216659a84
[gaim-migrate @ 14077]
Gary Kramlich <grim@reaperworld.com>
parents:
11679
diff
changeset
|
174 |
11660 | 175 if (mplug) { |
176 if (mplug->assm) { | |
177 mono_assembly_close(mplug->assm); | |
178 } | |
11786
2c8216659a84
[gaim-migrate @ 14077]
Gary Kramlich <grim@reaperworld.com>
parents:
11679
diff
changeset
|
179 |
11660 | 180 g_free(mplug); |
181 mplug = NULL; | |
182 } | |
183 } | |
184 | |
11786
2c8216659a84
[gaim-migrate @ 14077]
Gary Kramlich <grim@reaperworld.com>
parents:
11679
diff
changeset
|
185 /****************************************************************************** |
2c8216659a84
[gaim-migrate @ 14077]
Gary Kramlich <grim@reaperworld.com>
parents:
11679
diff
changeset
|
186 * Plugin Stuff |
2c8216659a84
[gaim-migrate @ 14077]
Gary Kramlich <grim@reaperworld.com>
parents:
11679
diff
changeset
|
187 *****************************************************************************/ |
11679
f05542391cd2
[gaim-migrate @ 13965]
Gary Kramlich <grim@reaperworld.com>
parents:
11660
diff
changeset
|
188 static void plugin_destroy(GaimPlugin *plugin) |
11660 | 189 { |
11786
2c8216659a84
[gaim-migrate @ 14077]
Gary Kramlich <grim@reaperworld.com>
parents:
11679
diff
changeset
|
190 mono_jit_cleanup(domain); |
11660 | 191 } |
192 | |
193 static GaimPluginLoaderInfo loader_info = | |
194 { | |
195 NULL, | |
196 probe_mono_plugin, | |
197 load_mono_plugin, | |
198 unload_mono_plugin, | |
199 destroy_mono_plugin | |
200 }; | |
201 | |
202 static GaimPluginInfo info = | |
203 { | |
204 GAIM_PLUGIN_MAGIC, | |
205 GAIM_MAJOR_VERSION, | |
206 GAIM_MINOR_VERSION, | |
207 GAIM_PLUGIN_LOADER, | |
208 NULL, | |
209 0, | |
210 NULL, | |
211 GAIM_PRIORITY_DEFAULT, | |
212 MONO_PLUGIN_ID, | |
213 N_("Mono Plugin Loader"), | |
214 VERSION, | |
215 N_("Loads .NET plugins with Mono."), | |
216 N_("Loads .NET plugins with Mono."), | |
217 "Eoin Coffey <ecoffey@simla.colostate.edu>", | |
218 GAIM_WEBSITE, | |
219 NULL, | |
220 NULL, | |
221 plugin_destroy, | |
222 NULL, | |
223 &loader_info, | |
224 NULL, | |
225 NULL | |
226 }; | |
227 | |
228 /* Creates the domain to execute in, and setups our CS Gaim API (note: | |
229 in the future the 'mono_add_internal_call' will be spread through out | |
230 the source to whatever module is exposing the API; this function will | |
231 simply call helper functions to do so) */ | |
232 static void init_plugin(GaimPlugin *plugin) | |
233 { | |
234 domain = mono_jit_init("gaim"); | |
11786
2c8216659a84
[gaim-migrate @ 14077]
Gary Kramlich <grim@reaperworld.com>
parents:
11679
diff
changeset
|
235 |
11660 | 236 mono_loader_set_domain(domain); |
11786
2c8216659a84
[gaim-migrate @ 14077]
Gary Kramlich <grim@reaperworld.com>
parents:
11679
diff
changeset
|
237 |
11660 | 238 mono_loader_init_internal_calls(); |
11786
2c8216659a84
[gaim-migrate @ 14077]
Gary Kramlich <grim@reaperworld.com>
parents:
11679
diff
changeset
|
239 |
11660 | 240 loader_info.exts = g_list_append(loader_info.exts, "dll"); |
241 } | |
242 | |
243 GAIM_INIT_PLUGIN(mono, init_plugin, info) |