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>
30 static unsigned initialized;
31 static struct avl_table *package_tree;
33 static char *str_cpy_lower(char *dst, const char *src)
37 for (i = 0; src[i]; i++)
38 dst[i] = tolower(src[i]);
44 G_DEFINE_TYPE(UPkgModule, upkg_module, G_TYPE_TYPE_MODULE);
46 static void dl_print_errors(const char *prefix)
49 while ((err = lt_dlerror())) {
50 if (prefix) fprintf(stderr, "%s: ", prefix);
55 static gboolean module_load(GTypeModule *m)
57 UPkgModule *mod = UPKG_MODULE(m);
58 int (*init_func)(GTypeModule *);
60 mod->dl = lt_dlopenext(G_TYPE_MODULE(m)->name);
62 dl_print_errors(G_TYPE_MODULE(m)->name);
66 init_func = lt_dlsym(mod->dl, "init");
67 if (!init_func || init_func(m) != 0) {
68 dl_print_errors(__func__);
76 static void module_unload(GTypeModule *m)
78 UPkgModule *mod = UPKG_MODULE(m);
80 if (lt_dlclose(mod->dl) != 0) {
81 dl_print_errors(__func__);
85 static void upkg_module_init(UPkgModule *mod)
89 static void upkg_module_class_init(UPkgModuleClass *class)
91 GTypeModuleClass *modclass = G_TYPE_MODULE_CLASS(class);
93 modclass->load = module_load;
94 modclass->unload = module_unload;
97 UPkgModule *upkg_module_new(const char *name)
105 name2 = malloc(strlen(name)+1);
110 UPkgModule *mod = g_object_new(UPKG_MODULE_TYPE, NULL);
116 G_TYPE_MODULE(mod)->name = str_cpy_lower(name2, name);
120 static int modcmp(const void *a, const void *b, void *_data)
122 const GTypeModule *ma = a;
123 const GTypeModule *mb = b;
125 return strcmp(ma->name, mb->name);
128 int module_init(void)
131 package_tree = avl_create(modcmp, NULL, NULL);
133 fprintf(stderr, "%s: failed to create package tree.\n", __func__);
137 if (lt_dlinit() != 0) {
138 avl_destroy(package_tree, NULL);
139 dl_print_errors(__func__);
150 int module_exit(void)
155 if (lt_dlexit() != 0) {
156 dl_print_errors(__func__);
163 GType module_get_class(const char *package, const char *class)
165 char buf[strlen(package) + strlen(class) + 1];
166 GTypeModule search = { .name = str_cpy_lower(buf, package) };
168 GTypeModule *mod = avl_find(package_tree, &search);
172 mod = G_TYPE_MODULE(upkg_module_new(package));
177 p = avl_probe(package_tree, mod);
184 if (!g_type_module_use(mod))
187 str_cpy_lower(buf+strlen(package), class);
188 buf[0] = toupper(buf[0]);
189 buf[strlen(package)] = toupper(buf[strlen(package)]);
190 return g_type_from_name(buf);