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>
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 /* Package search path. */
41 static char *search_path;
42 static size_t search_path_sz;
44 /* List of package file extensions, in descending order of precedence. */
45 static const char u_pkg_exts[][5] = { ".u", ".utx", ".umx", ".uax", ".unr" };
47 static char *str_cpy_lower(char *dst, const char *src)
51 for (i = 0; src[i]; i++)
52 dst[i] = tolower(src[i]);
58 static int str_cmp_lower(const char *s1, const char *s2)
62 for (i = 0; s1[i] && s2[i]; i++) {
63 int c1 = tolower(s1[i]), c2 = tolower(s2[i]);
77 static void dl_print_errors(const char *prefix)
80 while ((err = lt_dlerror())) {
81 if (prefix) fprintf(stderr, "%s: ", prefix);
82 fprintf(stderr, "%s\n", err);
86 static gboolean u_pkg_load(GTypeModule *m)
88 struct upkg_priv *priv = U_PKG_GET_PRIV(m);
89 int (*init_func)(GTypeModule *);
92 file = u_pkg_vfs_lookup(m->name);
94 U_PKG(m)->pkg = upkg_fopen(file);
100 priv->native = lt_dlopenext(m->name);
102 init_func = lt_dlsym(priv->native, "init");
103 if (!init_func || init_func(m) != 0) {
104 dl_print_errors(__func__);
105 lt_dlclose(priv->native);
106 upkg_close(U_PKG(m)->pkg);
114 static void u_pkg_unload(GTypeModule *m)
116 struct upkg_priv *priv = U_PKG_GET_PRIV(m);
117 void (*exit_func)(GTypeModule *);
118 UPkg *upkg = U_PKG(m);
121 exit_func = lt_dlsym(priv->native, "exit");
126 if (lt_dlclose(priv->native) != 0) {
127 dl_print_errors(__func__);
134 upkg_close(upkg->pkg);
139 static void u_pkg_init(UPkg *pkg)
144 static void u_pkg_finalize(GObject *o)
146 u_pkg_unload(G_TYPE_MODULE(o));
149 static void u_pkg_class_init(UPkgClass *class)
151 GTypeModuleClass *modclass = G_TYPE_MODULE_CLASS(class);
152 GObjectClass *objclass = G_OBJECT_CLASS(class);
154 const char *modpath = getenv("UOBJECT_MODULE_PATH");
156 g_type_class_add_private(class, sizeof (struct upkg_priv));
158 if (lt_dlinit() != 0) {
159 dl_print_errors(__func__);
162 if (modpath && lt_dlsetsearchpath(modpath) != 0) {
163 dl_print_errors(__func__);
165 if (lt_dladdsearchdir(PKGLIBDIR) != 0) {
166 dl_print_errors(__func__);
169 modclass->load = u_pkg_load;
170 modclass->unload = u_pkg_unload;
171 objclass->finalize = u_pkg_finalize;
174 static int expand_search_path(size_t need)
176 size_t want = search_path_sz;
177 if (want == 0) want = 1;
182 if (want > search_path_sz) {
183 char *new = realloc(search_path, want);
189 search_path_sz = want;
195 const char *u_pkg_get_search_path(void)
197 return search_path ? search_path : "";
200 int u_pkg_set_search_path(const char *path)
202 if (expand_search_path(strlen(path)+1) != 0)
204 strcpy(search_path, path);
208 int u_pkg_add_search_dir(const char *path)
210 size_t end = search_path ? strlen(search_path) : 0;
213 return u_pkg_set_search_path(path);
216 if (expand_search_path(end + strlen(path) + 2) != 0)
218 search_path[end] = LT_PATHSEP_CHAR;
219 strcpy(search_path+end+1, path);
223 GTypeModule *u_pkg_open(const char *name)
225 g_return_val_if_fail(name != NULL, NULL);
227 char *pkgname = malloc(strlen(name)+1);
232 GTypeModule *mod = g_object_new(U_PKG_TYPE, NULL);
238 mod->name = str_cpy_lower(pkgname, name);