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 3 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, see <http://www.gnu.org/licenses/>.
24 #include <glib-object.h>
27 #include <uobject/package.h>
29 #define U_PKG_GET_PRIV(o) \
30 G_TYPE_INSTANCE_GET_PRIVATE(o, U_PKG_TYPE, struct upkg_priv)
36 G_DEFINE_TYPE(UPkg, u_pkg, G_TYPE_TYPE_MODULE);
38 static char *str_cpy_lower(char *dst, const char *src)
42 for (i = 0; src[i]; i++)
43 dst[i] = tolower(src[i]);
49 static void dl_print_errors(const char *prefix)
52 while ((err = lt_dlerror())) {
53 if (prefix) fprintf(stderr, "%s: ", prefix);
54 fprintf(stderr, "%s\n", err);
58 static gboolean u_pkg_load(GTypeModule *m)
60 struct upkg_priv *priv = U_PKG_GET_PRIV(m);
61 int (*init_func)(GTypeModule *);
63 priv->native = lt_dlopenext(m->name);
65 dl_print_errors(m->name);
69 init_func = lt_dlsym(priv->native, "init");
70 if (!init_func || init_func(m) != 0) {
71 dl_print_errors(__func__);
72 lt_dlclose(priv->native);
79 static void u_pkg_unload(GTypeModule *m)
81 struct upkg_priv *priv = U_PKG_GET_PRIV(m);
82 void (*exit_func)(GTypeModule *);
85 exit_func = lt_dlsym(priv->native, "exit");
90 if (lt_dlclose(priv->native) != 0) {
91 dl_print_errors(__func__);
96 static void u_pkg_init(UPkg *pkg)
101 static void u_pkg_class_init(UPkgClass *class)
103 GTypeModuleClass *modclass = G_TYPE_MODULE_CLASS(class);
104 const char *modpath = getenv("UOBJECT_MODULE_PATH");
106 g_type_class_add_private(class, sizeof (struct upkg_priv));
108 if (lt_dlinit() != 0) {
109 dl_print_errors(__func__);
112 if (modpath && lt_dlsetsearchpath(modpath) != 0) {
113 dl_print_errors(__func__);
115 if (lt_dladdsearchdir(PKGLIBDIR) != 0) {
116 dl_print_errors(__func__);
119 modclass->load = u_pkg_load;
120 modclass->unload = u_pkg_unload;
123 GTypeModule *u_pkg_new(const char *name)
125 g_return_val_if_fail(name != NULL, NULL);
127 char *pkgname = malloc(strlen(name)+1);
132 GTypeModule *mod = g_object_new(U_PKG_TYPE, NULL);
138 mod->name = str_cpy_lower(pkgname, name);