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
25 #include <glib-object.h>
28 #include <uobject/package.h>
30 #define U_PKG_GET_PRIV(o) \
31 G_TYPE_INSTANCE_GET_PRIVATE(o, U_PKG_TYPE, struct upkg_priv)
37 G_DEFINE_TYPE(UPkg, u_pkg, G_TYPE_TYPE_MODULE);
39 static char *str_cpy_lower(char *dst, const char *src)
43 for (i = 0; src[i]; i++)
44 dst[i] = tolower(src[i]);
50 static void dl_print_errors(const char *prefix)
53 while ((err = lt_dlerror())) {
54 if (prefix) fprintf(stderr, "%s: ", prefix);
55 fprintf(stderr, "%s\n", err);
59 static gboolean u_pkg_load(GTypeModule *m)
61 struct upkg_priv *priv = U_PKG_GET_PRIV(m);
62 int (*init_func)(GTypeModule *);
64 priv->native = lt_dlopenext(m->name);
66 dl_print_errors(m->name);
70 init_func = lt_dlsym(priv->native, "init");
71 if (!init_func || init_func(m) != 0) {
72 dl_print_errors(__func__);
73 lt_dlclose(priv->native);
80 static void u_pkg_unload(GTypeModule *m)
82 struct upkg_priv *priv = U_PKG_GET_PRIV(m);
83 void (*exit_func)(GTypeModule *);
86 exit_func = lt_dlsym(priv->native, "exit");
91 if (lt_dlclose(priv->native) != 0) {
92 dl_print_errors(__func__);
97 static void u_pkg_init(UPkg *pkg)
102 static void u_pkg_class_init(UPkgClass *class)
104 GTypeModuleClass *modclass = G_TYPE_MODULE_CLASS(class);
105 const char *modpath = getenv("UOBJECT_MODULE_PATH");
107 g_type_class_add_private(class, sizeof (struct upkg_priv));
109 if (lt_dlinit() != 0) {
110 dl_print_errors(__func__);
113 if (modpath && lt_dlsetsearchpath(modpath) != 0) {
114 dl_print_errors(__func__);
116 if (lt_dladdsearchdir(PKGLIBDIR) != 0) {
117 dl_print_errors(__func__);
120 modclass->load = u_pkg_load;
121 modclass->unload = u_pkg_unload;
124 GTypeModule *u_pkg_new(const char *name)
126 g_return_val_if_fail(name != NULL, NULL);
128 char *pkgname = malloc(strlen(name)+1);
133 GTypeModule *mod = g_object_new(U_PKG_TYPE, NULL);
139 mod->name = str_cpy_lower(pkgname, name);