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>
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 /* Package search path. */
40 static char *search_path;
41 static size_t search_path_sz;
43 /* List of package file extensions, in descending order of precedence. */
44 static const char u_pkg_exts[][5] = { ".u", ".utx", ".umx", ".uax", ".unr" };
46 static char *str_cpy_lower(char *dst, const char *src)
50 for (i = 0; src[i]; i++)
51 dst[i] = tolower(src[i]);
57 static int str_cmp_lower(const char *s1, const char *s2)
61 for (i = 0; s1[i] && s2[i]; i++) {
62 int c1 = tolower(s1[i]), c2 = tolower(s2[i]);
76 static void dl_print_errors(const char *prefix)
79 while ((err = lt_dlerror())) {
80 if (prefix) fprintf(stderr, "%s: ", prefix);
81 fprintf(stderr, "%s\n", err);
86 * Function for use with lt_dlforeachfile. The user data must point to
87 * a UPkg with the GTypeModule name field initialized. If a suitable file
88 * is found and could be successfully opened for reading, the pkg class member
91 static int find_package_by_name(const char *filename, void *data)
93 GTypeModule *m = G_TYPE_MODULE(data);
94 UPkg *upkg = U_PKG(data);
100 base = strrchr(filename, '/');
107 if (str_cmp_lower(base, m->name) != 0)
110 len = strlen(filename);
111 buf = malloc(len + sizeof **u_pkg_exts);
114 strcpy(buf, filename);
116 for (unsigned i = 0; i < sizeof u_pkg_exts / sizeof *u_pkg_exts; i++) {
117 strcpy(buf+len, u_pkg_exts[i]);
118 upkg->pkg = upkg_fopen(buf);
129 static gboolean u_pkg_load(GTypeModule *m)
131 struct upkg_priv *priv = U_PKG_GET_PRIV(m);
132 int (*init_func)(GTypeModule *);
135 priv->native = lt_dlopenext(m->name);
137 dl_print_errors(m->name);
141 init_func = lt_dlsym(priv->native, "init");
142 if (!init_func || init_func(m) != 0) {
143 dl_print_errors(__func__);
144 lt_dlclose(priv->native);
149 if (!U_PKG(m)->pkg) {
150 lt_dlforeachfile(u_pkg_get_search_path(), find_package_by_name, m);
156 static void u_pkg_unload(GTypeModule *m)
158 struct upkg_priv *priv = U_PKG_GET_PRIV(m);
159 void (*exit_func)(GTypeModule *);
160 UPkg *upkg = U_PKG(m);
163 exit_func = lt_dlsym(priv->native, "exit");
168 if (lt_dlclose(priv->native) != 0) {
169 dl_print_errors(__func__);
174 upkg_close(upkg->pkg);
179 static void u_pkg_init(UPkg *pkg)
184 static void u_pkg_finalize(GObject *o)
186 UPkg *upkg = U_PKG(o);
189 upkg_close(upkg->pkg);
193 static void u_pkg_class_init(UPkgClass *class)
195 GTypeModuleClass *modclass = G_TYPE_MODULE_CLASS(class);
196 GObjectClass *objclass = G_OBJECT_CLASS(class);
198 const char *modpath = getenv("UOBJECT_MODULE_PATH");
200 g_type_class_add_private(class, sizeof (struct upkg_priv));
202 if (lt_dlinit() != 0) {
203 dl_print_errors(__func__);
206 if (modpath && lt_dlsetsearchpath(modpath) != 0) {
207 dl_print_errors(__func__);
209 if (lt_dladdsearchdir(PKGLIBDIR) != 0) {
210 dl_print_errors(__func__);
213 modclass->load = u_pkg_load;
214 modclass->unload = u_pkg_unload;
215 objclass->finalize = u_pkg_finalize;
218 static int expand_search_path(size_t need)
220 size_t want = search_path_sz;
221 if (want == 0) want = 1;
226 if (want > search_path_sz) {
227 char *new = realloc(search_path, want);
233 search_path_sz = want;
239 const char *u_pkg_get_search_path(void)
241 return search_path ? search_path : "";
244 int u_pkg_set_search_path(const char *path)
246 if (expand_search_path(strlen(path)+1) != 0)
248 strcpy(search_path, path);
252 int u_pkg_add_search_dir(const char *path)
254 size_t end = search_path ? strlen(search_path) : 0;
257 return u_pkg_set_search_path(path);
260 if (expand_search_path(end + strlen(path) + 2) != 0)
262 search_path[end] = LT_PATHSEP_CHAR;
263 strcpy(search_path+end+1, path);
267 GTypeModule *u_pkg_new_by_name(const char *name)
269 g_return_val_if_fail(name != NULL, NULL);
271 char *pkgname = malloc(strlen(name)+1);
276 GTypeModule *mod = g_object_new(U_PKG_TYPE, NULL);
282 mod->name = str_cpy_lower(pkgname, name);
286 GTypeModule *u_pkg_new_by_file(const char *filename)
288 struct upkg *pkg = upkg_fopen(filename);
293 GTypeModule *mod = g_object_new(U_PKG_TYPE, NULL);
300 U_PKG(mod)->pkg = pkg;