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)
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_file member
89 * of struct upkg_priv will be filled in.
91 static int find_package_by_name(const char *filename, void *_pkg)
93 struct upkg_priv *priv = U_PKG_GET_PRIV(_pkg);
94 GTypeModule *m = _pkg;
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 priv->pkg_file = fopen(buf, "rb");
119 if (priv->pkg_file) {
129 static gboolean u_pkg_load(GTypeModule *m)
131 struct upkg_priv *priv = U_PKG_GET_PRIV(m);
132 int (*init_func)(GTypeModule *);
134 priv->native = lt_dlopenext(m->name);
136 dl_print_errors(m->name);
140 init_func = lt_dlsym(priv->native, "init");
141 if (!init_func || init_func(m) != 0) {
142 dl_print_errors(__func__);
143 lt_dlclose(priv->native);
147 lt_dlforeachfile(u_pkg_get_search_path(), find_package_by_name, m);
152 static void u_pkg_unload(GTypeModule *m)
154 struct upkg_priv *priv = U_PKG_GET_PRIV(m);
155 void (*exit_func)(GTypeModule *);
158 exit_func = lt_dlsym(priv->native, "exit");
163 if (lt_dlclose(priv->native) != 0) {
164 dl_print_errors(__func__);
168 if (priv->pkg_file) {
169 fclose(priv->pkg_file);
173 static void u_pkg_init(UPkg *pkg)
178 static void u_pkg_class_init(UPkgClass *class)
180 GTypeModuleClass *modclass = G_TYPE_MODULE_CLASS(class);
181 const char *modpath = getenv("UOBJECT_MODULE_PATH");
183 g_type_class_add_private(class, sizeof (struct upkg_priv));
185 if (lt_dlinit() != 0) {
186 dl_print_errors(__func__);
189 if (modpath && lt_dlsetsearchpath(modpath) != 0) {
190 dl_print_errors(__func__);
192 if (lt_dladdsearchdir(PKGLIBDIR) != 0) {
193 dl_print_errors(__func__);
196 modclass->load = u_pkg_load;
197 modclass->unload = u_pkg_unload;
200 static int expand_search_path(size_t need)
202 size_t want = search_path_sz;
203 if (want == 0) want = 1;
208 if (want > search_path_sz) {
209 char *new = realloc(search_path, want);
215 search_path_sz = want;
221 const char *u_pkg_get_search_path(void)
223 return search_path ? search_path : "";
226 int u_pkg_set_search_path(const char *path)
228 if (expand_search_path(strlen(path)+1) != 0)
230 strcpy(search_path, path);
234 int u_pkg_add_search_dir(const char *path)
236 size_t end = search_path ? strlen(search_path) : 0;
239 return u_pkg_set_search_path(path);
242 if (expand_search_path(end + strlen(path) + 2) != 0)
244 search_path[end] = LT_PATHSEP_CHAR;
245 strcpy(search_path+end+1, path);
249 GTypeModule *u_pkg_new(const char *name)
251 g_return_val_if_fail(name != NULL, NULL);
253 char *pkgname = malloc(strlen(name)+1);
258 GTypeModule *mod = g_object_new(U_PKG_TYPE, NULL);
264 mod->name = str_cpy_lower(pkgname, name);