]> git.draconx.ca Git - upkg.git/blob - src/upkg.c
upkg: Pass the UPkg module struct into package_info.
[upkg.git] / src / upkg.c
1 /*
2  *  upkg: tool for manipulating Unreal Tournament packages.
3  *  Copyright © 2009-2011 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 <assert.h>
25 #include <getopt.h>
26
27 #include <glib-object.h>
28
29 #include "upkg.h"
30 #include <uobject/uobject.h>
31 #include <uobject/exportable.h>
32 #include <uobject/loadable.h>
33 #include <uobject/module.h>
34 #include <uobject/package.h>
35 #include <uobject/vfs.h>
36
37 enum {
38         MODE_INFO,
39         MODE_LIST,
40         MODE_EXPORT,
41         MODE_MAX
42 };
43
44 int verbose = 0;
45
46 static const char *progname = "upkg";
47 static const char *sopts = "ilxvf:VH";
48 static const struct option lopts[] = {
49         { "info",     0, NULL, 'i' },
50         { "list",     0, NULL, 'l' },
51         { "export",   0, NULL, 'x' },
52         { "file",     1, NULL, 'f' },
53         { "verbose",  0, NULL, 'v' },
54         { "version",  0, NULL, 'V' },
55         { "help",     0, NULL, 'H' },
56         { 0 }
57 };
58
59 void print_version(void)
60 {
61         printf("%s\n", PACKAGE_STRING);
62         puts("\
63 Copyright (C) 2009 Nick Bowler.\n\
64 License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.\n\
65 This is free software: you are free to change and redistribute it.\n\
66 There is NO WARRANTY, to the extent permitted by law."
67         );
68 }
69
70 void print_usage(FILE *f)
71 {
72         fprintf(f, "Usage: %s [options] [object ...]\n", progname);
73 }
74
75 void print_help(void)
76 {
77         print_usage(stdout);
78         puts("Detailed help coming soon.  Until then, I'll just list my options.");
79         for (unsigned i = 0; lopts[i].name; i++) {
80                 const struct option *o = &lopts[i];
81                 printf("\t--%s", o->name);
82
83                 if (o->has_arg == 1) {
84                         printf("=val");
85                 } else if (o->has_arg == 2) {
86                         printf("[=val]");
87                 }
88
89                 printf(" (-%c)\n", o->val);
90         }
91 }
92
93 static void print_upkg_flags(const char *prefix, unsigned long flags)
94 {
95         if (flags & UPKG_FLAG_ALLOW_DOWNLOAD)
96                 printf("%sAllowDownload\n", prefix);
97         if (flags & UPKG_FLAG_CLIENT_OPTIONAL)
98                 printf("%sClientOptional\n", prefix);
99         if (flags & UPKG_FLAG_SERVER_ONLY)
100                 printf("%sServerOnly\n", prefix);
101         if (flags & UPKG_FLAG_BROKEN_LINKS)
102                 printf("%sBrokenLinks\n", prefix);
103         if (flags & UPKG_FLAG_INSECURE)
104                 printf("%sInsecure\n", prefix);
105         if (flags & UPKG_FLAG_REQUIRED)
106                 printf("%sRequired\n", prefix);
107 }
108
109 static void print_upkg_object_flags(const char *prefix, unsigned long flags)
110 {
111         if (flags & UPKG_OBJ_FLAG_TRANSACTIONAL)
112                 printf("%sTransactional\n", prefix);
113         if (flags & UPKG_OBJ_FLAG_UNREACHABLE)
114                 printf("%sUnreachable\n", prefix);
115         if (flags & UPKG_OBJ_FLAG_PUBLIC)
116                 printf("%sPublic\n", prefix);
117         if (flags & UPKG_OBJ_FLAG_TAG_IMPORT)
118                 printf("%sImport\n", prefix);
119         if (flags & UPKG_OBJ_FLAG_TAG_EXPORT)
120                 printf("%sTagExport\n", prefix);
121         if (flags & UPKG_OBJ_FLAG_SOURCE_MODIFIED)
122                 printf("%sSourceModified\n", prefix);
123         if (flags & UPKG_OBJ_FLAG_TAG_GARBAGE)
124                 printf("%sTagGarbage\n", prefix);
125         if (flags & UPKG_OBJ_FLAG_NEED_LOAD)
126                 printf("%sNeedLoad\n", prefix);
127         if (flags & UPKG_OBJ_FLAG_HIGHLIGHT_NAME)
128                 printf("%sHighlightName\n", prefix);
129         if (flags & UPKG_OBJ_FLAG_IN_SINGULAR_FUNC)
130                 printf("%sInSingularFunc\n", prefix);
131         if (flags & UPKG_OBJ_FLAG_SUPPRESSED)
132                 printf("%sSuppressed\n", prefix);
133         if (flags & UPKG_OBJ_FLAG_IN_END_STATE)
134                 printf("%sInEndState\n", prefix);
135         if (flags & UPKG_OBJ_FLAG_TRANSIENT)
136                 printf("%sTransient\n", prefix);
137         if (flags & UPKG_OBJ_FLAG_PRELOADING)
138                 printf("%sPreloading\n", prefix);
139         if (flags & UPKG_OBJ_FLAG_LOAD_FOR_CLIENT)
140                 printf("%sLoadForClient\n", prefix);
141         if (flags & UPKG_OBJ_FLAG_LOAD_FOR_SERVER)
142                 printf("%sLoadForServer\n", prefix);
143         if (flags & UPKG_OBJ_FLAG_LOAD_FOR_EDIT)
144                 printf("%sLoadForEdit\n", prefix);
145         if (flags & UPKG_OBJ_FLAG_STANDALONE)
146                 printf("%sStandalone\n", prefix);
147         if (flags & UPKG_OBJ_FLAG_NOT_FOR_CLIENT)
148                 printf("%sNotForClient\n", prefix);
149         if (flags & UPKG_OBJ_FLAG_NOT_FOR_SERVER)
150                 printf("%sNotForServer\n", prefix);
151         if (flags & UPKG_OBJ_FLAG_NOT_FOR_EDIT)
152                 printf("%sNotForEdit\n", prefix);
153         if (flags & UPKG_OBJ_FLAG_DESTROYED)
154                 printf("%sDestroyed\n", prefix);
155         if (flags & UPKG_OBJ_FLAG_NEED_POST_LOAD)
156                 printf("%sNeedPostLoad\n", prefix);
157         if (flags & UPKG_OBJ_FLAG_HAS_STACK)
158                 printf("%sHasStack\n", prefix);
159         if (flags & UPKG_OBJ_FLAG_NATIVE)
160                 printf("%sNative\n", prefix);
161         if (flags & UPKG_OBJ_FLAG_MARKED)
162                 printf("%sMarked\n", prefix);
163         if (flags & UPKG_OBJ_FLAG_ERROR_SHUTDOWN)
164                 printf("%sErrorShutdown\n", prefix);
165         if (flags & UPKG_OBJ_FLAG_DEBUG_POST_LOAD)
166                 printf("%sDebugPostLoad\n", prefix);
167         if (flags & UPKG_OBJ_FLAG_DEBUG_SERIALIZE)
168                 printf("%sDebugSerialize\n", prefix);
169         if (flags & UPKG_OBJ_FLAG_DEBUG_DESTROY)
170                 printf("%sDebugDestroy\n", prefix);
171 }
172
173 void print_guid(unsigned char guid[static 16])
174 {
175         for (unsigned i = 0; i < 16; i++) {
176                 printf("%02hhx", guid[i]);
177                 if (i == 15)
178                         putchar('\n');
179                 else
180                         putchar(' ');
181         }
182 }
183
184 static void export_print_name(const struct upkg_export *e)
185 {
186         if (e) {
187                 export_print_name(e->parent);
188                 printf(".%s", e->name);
189         }
190 }
191
192 static void
193 export_print_fullname(GTypeModule *pkg, const struct upkg_export *export)
194 {
195         printf("%s", pkg->name);
196         export_print_name(export);
197 }
198
199 static void import_print_fullname(const struct upkg_import *import)
200 {
201         if (import) {
202                 import_print_fullname(import->parent);
203                 if (import->parent)
204                         putchar('.');
205                 printf("%s", import->name);
206         }
207 }
208
209 void print_upkg_exports(struct upkg *pkg)
210 {
211         for (unsigned i = 0; i < pkg->export_count; i++) {
212                 const struct upkg_export *export = upkg_get_export(pkg, i);
213                 const char *package, *class;
214
215                 class = upkg_export_class(pkg, i, &package);
216
217                 if (!class)
218                         continue;
219
220                 printf("%u - %s (%s.%s)\n", i+1, export->name, package, class);
221                 printf("  Flags: %lx\n", export->flags);
222                 if (verbose >= 2) {
223                         print_upkg_object_flags("    ", export->flags);
224                 }
225         }
226 }
227
228 void print_upkg_imports(struct upkg *pkg)
229 {
230         for (unsigned i = 0; i < pkg->import_count; i++) {
231                 const struct upkg_import *import = upkg_get_import(pkg, i);
232
233                 printf("%u - ", i);
234                 import_print_fullname(import);
235                 printf(" (%s.%s)\n", import->class_package, import->class_name);
236         }
237 }
238
239 int package_info(GTypeModule *pkg)
240 {
241         struct upkg *upkg = U_PKG(pkg)->pkg;
242
243         printf("Version: %u\n", upkg->version);
244         printf("License: %u\n", upkg->license);
245         printf("GUID: ");
246         print_guid(upkg->guid);
247
248         printf("Flags:   %lx\n", upkg->flags);
249         if (verbose >= 2) {
250                 print_upkg_flags("        ", upkg->flags);
251         }
252
253         printf("Names:   %lu\n", upkg->name_count);
254         if (verbose >= 3) {
255                 for (unsigned long i = 0; i < upkg->name_count; i++) {
256                         printf("%lu - %s\n", i, upkg_get_name(upkg, i));
257                 }
258         }
259
260         printf("Exports: %lu\n", upkg->export_count);
261         if (verbose >= 1) print_upkg_exports(upkg);
262         printf("Imports: %lu\n", upkg->import_count);
263         if (verbose >= 1) print_upkg_imports(upkg);
264
265         return 0;
266 }
267
268 static int package_list(GTypeModule *pkg, long current)
269 {
270         struct upkg *upkg = U_PKG(pkg)->pkg;
271         const struct upkg_export *parent = NULL;
272
273         if (current >= 0) {
274                 parent = upkg_get_export(upkg, current);
275                 assert(parent != NULL);
276         }
277
278         for (unsigned i = 0; i < upkg->export_count; i++) {
279                 const struct upkg_export *export = upkg_get_export(upkg, i);
280
281                 if (export->parent != parent)
282                         continue;
283
284                 export_print_fullname(pkg, export);
285
286                 if (verbose >= 1) {
287                         const char *class, *package;
288
289                         class = upkg_export_class(upkg, i, &package);
290                         printf(" (%s.%s)", package, class);
291                 }
292
293                 putchar('\n');
294         }
295
296         return 0;
297 }
298
299 static int object_dump_properties(GTypeModule *pkg, unsigned long idx)
300 {
301         GParamSpec **props;
302         GObject *obj;
303         unsigned n;
304
305         obj = u_object_new_from_package(pkg, idx);
306         if (!obj) {
307                 fprintf(stderr, "%s: failed to load object.\n", progname);
308                 return -1;
309         }
310
311         props = g_object_class_list_properties(G_OBJECT_GET_CLASS(obj), &n);
312         for (unsigned i = 0; i < n; i++) {
313                 GValue val = {0};
314                 char *valstr;
315
316                 printf("  property %s:", props[i]->name);
317                 fflush(stdout);
318
319                 g_value_init(&val, props[i]->value_type);
320                 g_object_get_property(obj, props[i]->name, &val);
321
322                 if (G_VALUE_HOLDS(&val, U_TYPE_OBJECT)) {
323                         UObject *obj = g_value_get_object(&val);
324
325                         if (obj && obj->pkg_name) {
326                                 printf(" %s", obj->pkg_name);
327                         }
328                 }
329
330                 valstr = g_strdup_value_contents(&val);
331                 printf(" %s\n", valstr);
332                 g_free(valstr);
333         }
334
335         g_free(props);
336         g_object_unref(obj);
337
338         return 0;
339 }
340
341 static int object_dump_offsets(GTypeModule *pkg, unsigned long idx)
342 {
343         struct upkg_file *f;
344
345         f = upkg_export_open(U_PKG(pkg)->pkg, idx);
346         if (!f) {
347                 fprintf(stderr, "%s: failed to open export.\n", progname);
348                 return -1;
349         }
350
351         printf("  file size: %lu\n", f->len);
352         printf("  file start: %#lx\n", f->base);
353         printf("  file end: %#lx\n", f->base + f->len);
354
355         upkg_export_close(f);
356
357         return 0;
358 }
359
360 static int object_info(GTypeModule *pkg, unsigned long idx)
361 {
362         struct upkg *upkg = U_PKG(pkg)->pkg;
363         const struct upkg_export *export;
364         const char *class, *package;
365         int ret = 0;
366
367         export = upkg_get_export(upkg, idx);
368         export_print_fullname(pkg, export);
369
370         class = upkg_export_class(upkg, idx, &package);
371         if (class)
372                 printf(" (%s.%s)\n", package, class);
373         else
374                 printf(" (failed to get class)\n");
375
376         if (verbose >= 1 && object_dump_properties(pkg, idx) != 0)
377                 ret = -1;
378
379         if (verbose >= 3 && object_dump_offsets(pkg, idx))
380                 ret = -1;
381
382         return ret;
383 }
384
385 static int export(GTypeModule *pkg, GObject *obj, unsigned idx)
386 {
387         struct upkg *upkg = U_PKG(pkg)->pkg;
388         char name[256];
389         FILE *of;
390         int rc;
391
392         if (U_OBJECT_IS_LOADABLE(obj) && u_object_load(obj) != 0) {
393                 fprintf(stderr, "%s: failed to load export data.\n", progname);
394                 return -1;
395         }
396
397         rc = u_object_export_name(obj, name, sizeof name);
398         if (rc <= 0) {
399                 /* XXX: We could use a default name here. */
400                 fprintf(stderr, "%s: failed to determine export filename.\n",
401                                 progname);
402                 return -1;
403         }
404
405         printf("exporting ");
406         export_print_fullname(pkg, upkg_get_export(upkg, idx));
407         printf(" to %s\n", name);
408
409         of = fopen(name, "wb");
410         if (!of) {
411                 perror(name);
412                 return -1;
413         }
414
415         u_object_export(obj, of);
416
417         if (fclose(of) != 0) {
418                 perror(name);
419                 return -1;
420         }
421
422         if (U_OBJECT_IS_LOADABLE(obj)) {
423                 u_object_unload(obj);
424         }
425
426         return 0;
427 }
428
429 static int object_export(GTypeModule *pkg, unsigned long idx)
430 {
431         GObject *obj;
432         int ret = -1;
433
434         obj = u_object_new_from_package(pkg, idx);
435         if (!obj) {
436                 fprintf(stderr, "%s: failed to load object.\n", progname);
437                 return -1;
438         }
439
440         if (!U_OBJECT_IS_EXPORTABLE(obj)) {
441                 fprintf(stderr, "%s: object is not exportable.\n", progname);
442                 goto out;
443         }
444
445         ret = export(pkg, obj, idx);
446 out:
447         g_object_unref(obj);
448         return ret;
449 }
450
451 int package_export(GTypeModule *pkg)
452 {
453         struct upkg *upkg = U_PKG(pkg)->pkg;
454         GObject *obj;
455         int ret = 0;
456
457         for (unsigned i = 0; i < upkg->export_count; i++) {
458                 obj = u_object_new_from_package(pkg, i);
459                 if (U_OBJECT_IS_EXPORTABLE(obj) && export(pkg, obj, i) != 0) {
460                         ret = -1;
461                 }
462                 g_object_unref(obj);
463         }
464
465         return ret;
466 }
467
468 static int process_object(int mode, const char *objname)
469 {
470         char work[strlen(objname)+1], *p = work, *c;
471         GTypeModule *pkg;
472         long current = -1;
473         int ret = 0;
474
475         strcpy(work, objname);
476
477         /* First, find and open the actual package. */
478         c = strchr(p, '.');
479         if (c) *c = 0;
480
481         pkg = u_pkg_open(p);
482         if (!pkg || !g_type_module_use(pkg)) {
483                 fprintf(stderr, "%s: %s: failed to open package.\n",
484                                 progname, work);
485                 goto out;
486         }
487
488         if (!U_PKG(pkg)->pkg) {
489                 if (u_pkg_is_native(pkg)) {
490                         fprintf(stderr, "%s: %s: not a UObject package.\n",
491                                         progname, work);
492                 } else {
493                         fprintf(stderr, "%s: %s: package not found.\n",
494                                         progname, work);
495                 }
496
497                 goto out_unuse;
498         }
499
500         /* Resolve the hierarchical reference. */
501         while (c) {
502                 p = c+1;
503                 c = strchr(p, '.');
504                 if (c) *c = 0;
505
506                 current = upkg_export_find(U_PKG(pkg)->pkg, current, p);
507                 if (current == -1) {
508                         /* We want to print the full name up to this point. */
509                         strcpy(work, objname);
510                         if (c) *c = 0;
511
512                         fprintf(stderr, "%s: %s: object not found.\n",
513                                         progname, work);
514                         goto out_unuse;
515                 }
516         }
517
518         switch (mode) {
519         case MODE_INFO:
520                 if (current < 0)
521                         ret = package_info(pkg);
522                 else
523                         ret = object_info(pkg, current);
524                 break;
525         case MODE_LIST:
526                 ret = package_list(pkg, current);
527                 break;
528         case MODE_EXPORT:
529                 if (current < 0)
530                         ret = package_export(pkg);
531                 else
532                         ret = object_export(pkg, current);
533                 break;
534         default:
535                 abort();
536         }
537
538 out_unuse:
539         g_type_module_unuse(pkg);
540 out:
541         return ret;
542 }
543
544 int main(int argc, char **argv)
545 {
546         const char *pkgname = NULL;
547         unsigned mode = MODE_INFO;
548         int opt, rc;
549
550         if (argc > 0) progname = argv[0];
551
552         if (u_pkg_vfs_init() != 0)
553                 return EXIT_FAILURE;
554         if (u_object_module_init() != 0)
555                 return EXIT_FAILURE;
556
557         while ((opt = getopt_long(argc, argv, sopts, lopts, NULL)) != -1) {
558                 switch (opt) {
559                 case 'f':
560                         pkgname = u_pkg_vfs_add_local(NULL, optarg);
561                         if (!pkgname) {
562                                 fprintf(stderr, "failed to add package `%s'.\n", optarg);
563                                 return EXIT_FAILURE;
564                         }
565                         break;
566                 case 'i':
567                         mode = MODE_INFO;
568                         break;
569                 case 'l':
570                         mode = MODE_LIST;
571                         break;
572                 case 'x':
573                         mode = MODE_EXPORT;
574                         break;
575                 case 'v':
576                         verbose++;
577                         break;
578                 case 'V':
579                         print_version();
580                         return EXIT_SUCCESS;
581                 case 'H':
582                         print_help();
583                         return EXIT_SUCCESS;
584                 default:
585                         print_usage(stderr);
586                         return EXIT_FAILURE;
587                 }
588         }
589
590         if (!pkgname && !argv[optind]) {
591                 print_usage(stderr);
592                 return EXIT_FAILURE;
593         }
594
595         rc = EXIT_SUCCESS;
596         if (argv[optind]) {
597                 for (int i = optind; i < argc; i++) {
598                         if (process_object(mode, argv[i]) != 0)
599                                 rc = EXIT_FAILURE;
600                 }
601         } else {
602                 if (process_object(mode, pkgname) != 0)
603                         rc = EXIT_FAILURE;
604         }
605
606         u_object_module_exit();
607         u_pkg_vfs_exit();
608         return rc;
609 }