2 * upkg: tool for manipulating Unreal Tournament packages.
3 * Copyright © 2009-2011 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>
28 #include <uobject/vfs.h>
31 #define U_PKG_GET_PRIV(o) \
32 G_TYPE_INSTANCE_GET_PRIVATE(o, U_PKG_TYPE, struct upkg_priv)
38 G_DEFINE_TYPE(UPkg, u_pkg, G_TYPE_TYPE_MODULE);
40 static char *str_cpy_lower(char *dst, const char *src)
44 for (i = 0; src[i]; i++)
45 dst[i] = tolower(src[i]);
51 static void dl_print_errors(const char *prefix)
54 while ((err = lt_dlerror())) {
55 if (prefix) fprintf(stderr, "%s: ", prefix);
56 fprintf(stderr, "%s\n", err);
60 static gboolean u_pkg_load(GTypeModule *m)
62 struct upkg_priv *priv = U_PKG_GET_PRIV(m);
63 int (*init_func)(GTypeModule *);
65 /* Ignore failure here until we get rid of native-only packages. */
66 U_PKG(m)->pkg = u_pkg_vfs_open_by_name(m->name);
68 priv->native = lt_dlopenext(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);
74 upkg_close(U_PKG(m)->pkg);
82 static void u_pkg_unload(GTypeModule *m)
84 struct upkg_priv *priv = U_PKG_GET_PRIV(m);
85 void (*exit_func)(GTypeModule *);
86 UPkg *upkg = U_PKG(m);
89 exit_func = lt_dlsym(priv->native, "exit");
94 if (lt_dlclose(priv->native) != 0) {
95 dl_print_errors(__func__);
102 upkg_close(upkg->pkg);
107 static void u_pkg_init(UPkg *pkg)
112 static void u_pkg_finalize(GObject *o)
114 u_pkg_unload(G_TYPE_MODULE(o));
117 static void u_pkg_class_init(UPkgClass *class)
119 GTypeModuleClass *modclass = G_TYPE_MODULE_CLASS(class);
120 GObjectClass *objclass = G_OBJECT_CLASS(class);
122 const char *modpath = getenv("UOBJECT_MODULE_PATH");
124 g_type_class_add_private(class, sizeof (struct upkg_priv));
126 if (lt_dlinit() != 0) {
127 dl_print_errors(__func__);
130 if (modpath && lt_dlsetsearchpath(modpath) != 0) {
131 dl_print_errors(__func__);
133 if (lt_dladdsearchdir(PKGLIBDIR) != 0) {
134 dl_print_errors(__func__);
137 modclass->load = u_pkg_load;
138 modclass->unload = u_pkg_unload;
139 objclass->finalize = u_pkg_finalize;
142 GTypeModule *u_pkg_open(const char *name)
144 g_return_val_if_fail(name != NULL, NULL);
146 char *pkgname = malloc(strlen(name)+1);
151 GTypeModule *mod = g_object_new(U_PKG_TYPE, NULL);
157 mod->name = str_cpy_lower(pkgname, name);
161 bool u_pkg_is_native(GTypeModule *m)
163 struct upkg_priv *priv = U_PKG_GET_PRIV(m);