2 * upkg: tool for manipulating Unreal Tournament packages.
3 * Copyright (C) 2009 Nick Bowler
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 #include <glib-object.h>
27 #include <uobject/module.h>
31 static unsigned initialized;
32 static struct avl_table *package_tree;
34 static char *str_cpy_lower(char *dst, const char *src)
38 for (i = 0; src[i]; i++)
39 dst[i] = tolower(src[i]);
45 G_DEFINE_TYPE(UPkgModule, upkg_module, G_TYPE_TYPE_MODULE);
47 static void dl_print_errors(const char *prefix)
50 while ((err = lt_dlerror())) {
51 if (prefix) fprintf(stderr, "%s: ", prefix);
56 static gboolean module_load(GTypeModule *m)
58 UPkgModule *mod = UPKG_MODULE(m);
59 int (*init_func)(GTypeModule *);
61 mod->dl = lt_dlopenext(G_TYPE_MODULE(m)->name);
63 dl_print_errors(G_TYPE_MODULE(m)->name);
67 init_func = lt_dlsym(mod->dl, "init");
68 if (!init_func || init_func(m) != 0) {
69 dl_print_errors(__func__);
77 static void module_unload(GTypeModule *m)
79 UPkgModule *mod = UPKG_MODULE(m);
81 if (lt_dlclose(mod->dl) != 0) {
82 dl_print_errors(__func__);
86 static void upkg_module_init(UPkgModule *mod)
90 static void upkg_module_class_init(UPkgModuleClass *class)
92 GTypeModuleClass *modclass = G_TYPE_MODULE_CLASS(class);
94 modclass->load = module_load;
95 modclass->unload = module_unload;
98 static UPkgModule *uobject_module_new(const char *name)
106 name2 = malloc(strlen(name)+1);
111 UPkgModule *mod = g_object_new(UPKG_MODULE_TYPE, NULL);
117 G_TYPE_MODULE(mod)->name = str_cpy_lower(name2, name);
121 static int modcmp(const void *a, const void *b, void *_data)
123 const GTypeModule *ma = a;
124 const GTypeModule *mb = b;
126 return strcmp(ma->name, mb->name);
129 int uobject_module_init(void)
132 package_tree = avl_create(modcmp, NULL, NULL);
134 fprintf(stderr, "%s: failed to create package tree.\n", __func__);
138 if (lt_dlinit() != 0) {
139 avl_destroy(package_tree, NULL);
140 dl_print_errors(__func__);
151 int uobject_module_exit(void)
156 if (lt_dlexit() != 0) {
157 dl_print_errors(__func__);
164 GType uobject_module_get_class(const char *package, const char *class)
166 char buf[strlen(package) + strlen(class) + 1];
167 GTypeModule search = { .name = str_cpy_lower(buf, package) };
169 GTypeModule *mod = avl_find(package_tree, &search);
173 mod = G_TYPE_MODULE(uobject_module_new(package));
178 p = avl_probe(package_tree, mod);
185 if (!g_type_module_use(mod))
188 str_cpy_lower(buf+strlen(package), class);
189 buf[0] = toupper(buf[0]);
190 buf[strlen(package)] = toupper(buf[strlen(package)]);
191 return g_type_from_name(buf);