]> git.draconx.ca Git - upkg.git/blob - src/upkg.c
upkg: Add support for querying specific objects.
[upkg.git] / src / upkg.c
1 /*
2  *  upkg: tool for manipulating Unreal Tournament packages.
3  *  Copyright (C) 2009 Nick Bowler
4  *
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.
9  *
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.
14  *
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/>.
17  */
18
19 #include <config.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <errno.h>
24 #include <getopt.h>
25
26 #include <glib-object.h>
27
28 #include "upkg.h"
29 #include <uobject/uobject.h>
30 #include <uobject/exportable.h>
31 #include <uobject/loadable.h>
32 #include <uobject/module.h>
33 #include <uobject/package.h>
34 #include <uobject/vfs.h>
35
36 enum {
37         MODE_INFO,
38         MODE_EXPORT,
39         MODE_MAX
40 };
41
42 int verbose = 0;
43
44 static const char *progname = "upkg";
45 static const char *sopts = "ixvf:VH";
46 static const struct option lopts[] = {
47         { "info",     0, NULL, 'i' },
48         { "export",   0, NULL, 'x' },
49         { "file",     1, NULL, 'f' },
50         { "verbose",  0, NULL, 'v' },
51         { "version",  0, NULL, 'V' },
52         { "help",     0, NULL, 'H' },
53         { 0 }
54 };
55
56 void print_version(void)
57 {
58         printf("%s\n", PACKAGE_STRING);
59         puts("\
60 Copyright (C) 2009 Nick Bowler.\n\
61 License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.\n\
62 This is free software: you are free to change and redistribute it.\n\
63 There is NO WARRANTY, to the extent permitted by law."
64         );
65 }
66
67 void print_usage(FILE *f)
68 {
69         fprintf(f, "Usage: %s [options] [object ...]\n", progname);
70 }
71
72 void print_help(void)
73 {
74         print_usage(stdout);
75         puts("Detailed help coming soon.  Until then, I'll just list my options.");
76         for (unsigned i = 0; lopts[i].name; i++) {
77                 const struct option *o = &lopts[i];
78                 printf("\t--%s", o->name);
79
80                 if (o->has_arg == 1) {
81                         printf("=val");
82                 } else if (o->has_arg == 2) {
83                         printf("[=val]");
84                 }
85
86                 printf(" (-%c)\n", o->val);
87         }
88 }
89
90 static void print_upkg_flags(const char *prefix, unsigned long flags)
91 {
92         if (flags & UPKG_FLAG_ALLOW_DOWNLOAD)
93                 printf("%sAllowDownload\n", prefix);
94         if (flags & UPKG_FLAG_CLIENT_OPTIONAL)
95                 printf("%sClientOptional\n", prefix);
96         if (flags & UPKG_FLAG_SERVER_ONLY)
97                 printf("%sServerOnly\n", prefix);
98         if (flags & UPKG_FLAG_BROKEN_LINKS)
99                 printf("%sBrokenLinks\n", prefix);
100         if (flags & UPKG_FLAG_INSECURE)
101                 printf("%sInsecure\n", prefix);
102         if (flags & UPKG_FLAG_REQUIRED)
103                 printf("%sRequired\n", prefix);
104 }
105
106 static void print_upkg_object_flags(const char *prefix, unsigned long flags)
107 {
108         if (flags & UPKG_OBJ_FLAG_TRANSACTIONAL)
109                 printf("%sTransactional\n", prefix);
110         if (flags & UPKG_OBJ_FLAG_UNREACHABLE)
111                 printf("%sUnreachable\n", prefix);
112         if (flags & UPKG_OBJ_FLAG_PUBLIC)
113                 printf("%sPublic\n", prefix);
114         if (flags & UPKG_OBJ_FLAG_TAG_IMPORT)
115                 printf("%sImport\n", prefix);
116         if (flags & UPKG_OBJ_FLAG_TAG_EXPORT)
117                 printf("%sTagExport\n", prefix);
118         if (flags & UPKG_OBJ_FLAG_SOURCE_MODIFIED)
119                 printf("%sSourceModified\n", prefix);
120         if (flags & UPKG_OBJ_FLAG_TAG_GARBAGE)
121                 printf("%sTagGarbage\n", prefix);
122         if (flags & UPKG_OBJ_FLAG_NEED_LOAD)
123                 printf("%sNeedLoad\n", prefix);
124         if (flags & UPKG_OBJ_FLAG_HIGHLIGHT_NAME)
125                 printf("%sHighlightName\n", prefix);
126         if (flags & UPKG_OBJ_FLAG_IN_SINGULAR_FUNC)
127                 printf("%sInSingularFunc\n", prefix);
128         if (flags & UPKG_OBJ_FLAG_SUPPRESSED)
129                 printf("%sSuppressed\n", prefix);
130         if (flags & UPKG_OBJ_FLAG_IN_END_STATE)
131                 printf("%sInEndState\n", prefix);
132         if (flags & UPKG_OBJ_FLAG_TRANSIENT)
133                 printf("%sTransient\n", prefix);
134         if (flags & UPKG_OBJ_FLAG_PRELOADING)
135                 printf("%sPreloading\n", prefix);
136         if (flags & UPKG_OBJ_FLAG_LOAD_FOR_CLIENT)
137                 printf("%sLoadForClient\n", prefix);
138         if (flags & UPKG_OBJ_FLAG_LOAD_FOR_SERVER)
139                 printf("%sLoadForServer\n", prefix);
140         if (flags & UPKG_OBJ_FLAG_LOAD_FOR_EDIT)
141                 printf("%sLoadForEdit\n", prefix);
142         if (flags & UPKG_OBJ_FLAG_STANDALONE)
143                 printf("%sStandalone\n", prefix);
144         if (flags & UPKG_OBJ_FLAG_NOT_FOR_CLIENT)
145                 printf("%sNotForClient\n", prefix);
146         if (flags & UPKG_OBJ_FLAG_NOT_FOR_SERVER)
147                 printf("%sNotForServer\n", prefix);
148         if (flags & UPKG_OBJ_FLAG_NOT_FOR_EDIT)
149                 printf("%sNotForEdit\n", prefix);
150         if (flags & UPKG_OBJ_FLAG_DESTROYED)
151                 printf("%sDestroyed\n", prefix);
152         if (flags & UPKG_OBJ_FLAG_NEED_POST_LOAD)
153                 printf("%sNeedPostLoad\n", prefix);
154         if (flags & UPKG_OBJ_FLAG_HAS_STACK)
155                 printf("%sHasStack\n", prefix);
156         if (flags & UPKG_OBJ_FLAG_NATIVE)
157                 printf("%sNative\n", prefix);
158         if (flags & UPKG_OBJ_FLAG_MARKED)
159                 printf("%sMarked\n", prefix);
160         if (flags & UPKG_OBJ_FLAG_ERROR_SHUTDOWN)
161                 printf("%sErrorShutdown\n", prefix);
162         if (flags & UPKG_OBJ_FLAG_DEBUG_POST_LOAD)
163                 printf("%sDebugPostLoad\n", prefix);
164         if (flags & UPKG_OBJ_FLAG_DEBUG_SERIALIZE)
165                 printf("%sDebugSerialize\n", prefix);
166         if (flags & UPKG_OBJ_FLAG_DEBUG_DESTROY)
167                 printf("%sDebugDestroy\n", prefix);
168 }
169
170 void print_guid(unsigned char guid[static 16])
171 {
172         for (unsigned i = 0; i < 16; i++) {
173                 printf("%02hhx", guid[i]);
174                 if (i == 15)
175                         putchar('\n');
176                 else
177                         putchar(' ');
178         }
179 }
180
181 void print_upkg_exports(struct upkg *pkg)
182 {
183         for (unsigned i = 0; i < pkg->export_count; i++) {
184                 const struct upkg_export *export = upkg_get_export(pkg, i);
185                 const char *package, *class;
186
187                 class = upkg_export_class(pkg, i, &package);
188
189                 if (!class)
190                         continue;
191
192                 printf("%u - %s (%s.%s)\n", i+1, export->name, package, class);
193                 printf("  Flags: %lx\n", export->flags);
194                 if (verbose >= 2) {
195                         print_upkg_object_flags("    ", export->flags);
196                 }
197         }
198 }
199
200 int package_info(struct upkg *pkg)
201 {
202         printf("Version: %u\n",  pkg->version);
203         printf("License: %u\n",  pkg->license);
204         printf("GUID: ");
205         print_guid(pkg->guid);
206
207         printf("Flags:   %lx\n", pkg->flags);
208         if (verbose >= 2) {
209                 print_upkg_flags("        ", pkg->flags);
210         }
211
212         printf("Names:   %lu\n", pkg->name_count);
213         if (verbose >= 3) {
214                 for (unsigned long i = 0; i < pkg->name_count; i++) {
215                         printf("\t%s\n", upkg_get_name(pkg, i));
216                 }
217         }
218
219         printf("Exports: %lu\n", pkg->export_count);
220         if (verbose >= 1) print_upkg_exports(pkg);
221         printf("Imports: %lu\n", pkg->import_count);
222
223         return 0;
224 }
225
226 static void export_print_name(struct upkg *upkg, const struct upkg_export *e)
227 {
228         if (e) {
229                 export_print_name(upkg, upkg_get_export(upkg, e->package-1));
230                 printf(".%s", e->name);
231         }
232 }
233
234 static int export_info(GTypeModule *pkg, unsigned long idx)
235 {
236         struct upkg *upkg = U_PKG(pkg)->pkg;
237         const struct upkg_export *export;
238
239         export = upkg_get_export(upkg, idx);
240
241         printf("%s", pkg->name);
242         export_print_name(upkg, export);
243         putchar('\n');
244
245         return 0;
246 }
247
248 static int export(struct upkg *pkg, GObject *obj, unsigned idx)
249 {
250         const struct upkg_export *export = upkg_get_export(pkg, idx);
251         char name[256];
252         FILE *of;
253
254         if (u_object_deserialize(obj, pkg, idx) != 0) {
255                 return -1;
256         }
257
258         if (U_OBJECT_IS_LOADABLE(obj) && u_object_load(obj) != 0) {
259                 return -1;
260         }
261
262         u_object_export_name(obj, name, sizeof name);
263
264         printf("exporting %s to %s\n", export->name, name);
265         of = fopen(name, "wb");
266         if (!of) {
267                 perror(name);
268                 return -1;
269         }
270
271         u_object_export(obj, of);
272
273         if (fclose(of) != 0) {
274                 perror(name);
275                 return -1;
276         }
277
278         if (U_OBJECT_IS_LOADABLE(obj)) {
279                 u_object_unload(obj);
280         }
281
282         return 0;
283 }
284
285 int package_export(struct upkg *pkg)
286 {
287         const char *class, *package;
288         GObject *obj;
289         GType type;
290         int ret = 0;
291
292         for (unsigned i = 0; i < pkg->export_count; i++) {
293                 class = upkg_export_class(pkg, i, &package);
294                 if (!class) {
295                         fprintf(stderr, "error getting class information.\n");
296                         return -1;
297                 }
298
299                 type = u_object_module_get_class(package, class);
300                 if (!type) continue;
301
302                 obj = g_object_new(type, NULL);
303                 if (U_OBJECT_IS_EXPORTABLE(obj)) {
304                         if (export(pkg, obj, i) != 0) {
305                                 ret = -1;
306                         }
307                 }
308                 g_object_unref(obj);
309         }
310
311         return ret;
312 }
313
314 static int process_object(int mode, const char *objname)
315 {
316         char work[strlen(objname)+1], *p = work, *c;
317         GTypeModule *pkg;
318         long current = -1;
319         int ret = 0;
320
321         strcpy(work, objname);
322
323         /* First, find and open the actual package. */
324         c = strchr(p, '.');
325         if (c) *c = 0;
326
327         pkg = u_pkg_open(p);
328         if (!pkg || !g_type_module_use(pkg)) {
329                 fprintf(stderr, "%s: %s: failed to open package.\n",
330                                 progname, work);
331                 goto out;
332         }
333
334         if (!U_PKG(pkg)->pkg) {
335                 if (u_pkg_is_native(pkg)) {
336                         fprintf(stderr, "%s: %s: not a UObject package.\n",
337                                         progname, work);
338                 } else {
339                         fprintf(stderr, "%s: %s: package not found.\n",
340                                         progname, work);
341                 }
342
343                 goto out_unuse;
344         }
345
346         /* Resolve the hierarchical reference. */
347         while (c) {
348                 p = c+1;
349                 c = strchr(p, '.');
350                 if (c) *c = 0;
351
352                 current = upkg_export_find(U_PKG(pkg)->pkg, current, p);
353                 if (current == -1) {
354                         /* We want to print the full name up to this point. */
355                         strcpy(work, objname);
356                         if (c) *c = 0;
357
358                         fprintf(stderr, "%s: %s: object not found.\n",
359                                         progname, work);
360                         goto out_unuse;
361                 }
362         }
363
364         switch (mode) {
365         case MODE_INFO:
366                 if (current < 0)
367                         ret = package_info(U_PKG(pkg)->pkg);
368                 else
369                         ret = export_info(pkg, current);
370                 break;
371         case MODE_EXPORT:
372                 ret = package_export(U_PKG(pkg)->pkg);
373                 break;
374         default:
375                 abort();
376         }
377
378 out_unuse:
379         g_type_module_unuse(pkg);
380 out:
381         return ret;
382 }
383
384 int main(int argc, char **argv)
385 {
386         const char *pkgname = NULL;
387         unsigned mode = MODE_INFO;
388         int opt, rc;
389
390         if (argc > 0) progname = argv[0];
391
392         if (u_pkg_vfs_init() != 0)
393                 return EXIT_FAILURE;
394         if (u_object_module_init() != 0)
395                 return EXIT_FAILURE;
396
397         while ((opt = getopt_long(argc, argv, sopts, lopts, NULL)) != -1) {
398                 switch (opt) {
399                 case 'f':
400                         pkgname = u_pkg_vfs_add_local(NULL, optarg);
401                         if (!pkgname) {
402                                 fprintf(stderr, "failed to add package `%s'.\n", optarg);
403                                 return EXIT_FAILURE;
404                         }
405                         break;
406                 case 'i':
407                         mode = MODE_INFO;
408                         break;
409                 case 'x':
410                         mode = MODE_EXPORT;
411                         break;
412                 case 'v':
413                         verbose++;
414                         break;
415                 case 'V':
416                         print_version();
417                         return EXIT_SUCCESS;
418                 case 'H':
419                         print_help();
420                         return EXIT_SUCCESS;
421                 default:
422                         print_usage(stderr);
423                         return EXIT_FAILURE;
424                 }
425         }
426
427         if (!pkgname && !argv[optind]) {
428                 print_usage(stderr);
429                 return EXIT_FAILURE;
430         }
431
432         rc = EXIT_SUCCESS;
433         if (argv[optind]) {
434                 for (int i = optind; i < argc; i++) {
435                         if (process_object(mode, argv[i]) != 0)
436                                 rc = EXIT_FAILURE;
437                 }
438         } else {
439                 if (process_object(mode, pkgname) != 0)
440                         rc = EXIT_FAILURE;
441         }
442
443         u_object_module_exit();
444         u_pkg_vfs_exit();
445         return rc;
446 }