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>
30 #define UPKG_MODULE_TYPE (upkg_module_get_type())
31 #define UPKG_MODULE(obj) \
32 G_TYPE_CHECK_INSTANCE_CAST(obj, UPKG_MODULE_TYPE, UPkgModule)
33 #define UPKG_MODULE_CLASS(class) \
34 G_TYPE_CHECK_CLASS_CAST(class, UPKG_MODULE_TYPE, UPkgModuleClass)
35 #define IS_UPKG_MODULE(obj) \
36 G_TYPE_CHECK_INSTANCE_TYPE(obj, UPKG_MODULE_TYPE)
37 #define IS_UPKG_MODULE_CLASS(class) \
38 G_TYPE_CHECK_CLASS_TYPE(class, UPKG_MODULE_TYPE, UPkgModuleClass)
40 typedef struct UPkgModule UPkgModule;
41 typedef struct UPkgModuleClass UPkgModuleClass;
49 struct UPkgModuleClass {
50 GTypeModuleClass parent;
53 static unsigned initialized;
54 static struct avl_table *package_tree;
56 static char *str_cpy_lower(char *dst, const char *src)
60 for (i = 0; src[i]; i++)
61 dst[i] = tolower(src[i]);
67 G_DEFINE_TYPE(UPkgModule, upkg_module, G_TYPE_TYPE_MODULE);
69 static void dl_print_errors(const char *prefix)
72 while ((err = lt_dlerror())) {
73 if (prefix) fprintf(stderr, "%s: ", prefix);
78 static gboolean module_load(GTypeModule *m)
80 UPkgModule *mod = UPKG_MODULE(m);
81 int (*init_func)(GTypeModule *);
83 mod->dl = lt_dlopenext(G_TYPE_MODULE(m)->name);
85 dl_print_errors(G_TYPE_MODULE(m)->name);
89 init_func = lt_dlsym(mod->dl, "init");
90 if (!init_func || init_func(m) != 0) {
91 dl_print_errors(__func__);
99 static void module_unload(GTypeModule *m)
101 UPkgModule *mod = UPKG_MODULE(m);
103 if (lt_dlclose(mod->dl) != 0) {
104 dl_print_errors(__func__);
108 static void upkg_module_init(UPkgModule *mod)
112 static void upkg_module_class_init(UPkgModuleClass *class)
114 GTypeModuleClass *modclass = G_TYPE_MODULE_CLASS(class);
116 modclass->load = module_load;
117 modclass->unload = module_unload;
120 static UPkgModule *uobject_module_new(const char *name)
128 name2 = malloc(strlen(name)+1);
133 UPkgModule *mod = g_object_new(UPKG_MODULE_TYPE, NULL);
139 G_TYPE_MODULE(mod)->name = str_cpy_lower(name2, name);
143 static int modcmp(const void *a, const void *b, void *_data)
145 const GTypeModule *ma = a;
146 const GTypeModule *mb = b;
148 return strcmp(ma->name, mb->name);
151 int uobject_module_init(void)
154 package_tree = avl_create(modcmp, NULL, NULL);
156 fprintf(stderr, "%s: failed to create package tree.\n", __func__);
160 if (lt_dlinit() != 0) {
161 avl_destroy(package_tree, NULL);
162 dl_print_errors(__func__);
173 int uobject_module_exit(void)
178 if (lt_dlexit() != 0) {
179 dl_print_errors(__func__);
186 GType uobject_module_get_class(const char *package, const char *class)
188 char buf[strlen(package) + strlen(class) + 1];
189 GTypeModule search = { .name = str_cpy_lower(buf, package) };
191 GTypeModule *mod = avl_find(package_tree, &search);
195 mod = G_TYPE_MODULE(uobject_module_new(package));
200 p = avl_probe(package_tree, mod);
207 if (!g_type_module_use(mod))
210 str_cpy_lower(buf+strlen(package), class);
211 buf[0] = toupper(buf[0]);
212 buf[strlen(package)] = toupper(buf[strlen(package)]);
213 return g_type_from_name(buf);