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