]> git.draconx.ca Git - upkg.git/blob - src/upkg.c
5a51c2e2203717e6eaa822a94aeb9c338ca092f9
[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 void print_upkg_exports(struct upkg *pkg)
185 {
186         for (unsigned i = 0; i < pkg->export_count; i++) {
187                 const struct upkg_export *export = upkg_get_export(pkg, i);
188                 const char *package, *class;
189
190                 class = upkg_export_class(pkg, i, &package);
191
192                 if (!class)
193                         continue;
194
195                 printf("%u - %s (%s.%s)\n", i+1, export->name, package, class);
196                 printf("  Flags: %lx\n", export->flags);
197                 if (verbose >= 2) {
198                         print_upkg_object_flags("    ", export->flags);
199                 }
200         }
201 }
202
203 int package_info(struct upkg *pkg)
204 {
205         printf("Version: %u\n",  pkg->version);
206         printf("License: %u\n",  pkg->license);
207         printf("GUID: ");
208         print_guid(pkg->guid);
209
210         printf("Flags:   %lx\n", pkg->flags);
211         if (verbose >= 2) {
212                 print_upkg_flags("        ", pkg->flags);
213         }
214
215         printf("Names:   %lu\n", pkg->name_count);
216         if (verbose >= 3) {
217                 for (unsigned long i = 0; i < pkg->name_count; i++) {
218                         printf("%lu - %s\n", i, upkg_get_name(pkg, i));
219                 }
220         }
221
222         printf("Exports: %lu\n", pkg->export_count);
223         if (verbose >= 1) print_upkg_exports(pkg);
224         printf("Imports: %lu\n", pkg->import_count);
225
226         return 0;
227 }
228
229 static void export_print_name(struct upkg *upkg, const struct upkg_export *e)
230 {
231         if (e) {
232                 export_print_name(upkg, e->parent);
233                 printf(".%s", e->name);
234         }
235 }
236
237 static void
238 export_print_fullname(GTypeModule *pkg, const struct upkg_export *export)
239 {
240         printf("%s", pkg->name);
241         export_print_name(U_PKG(pkg)->pkg, export);
242 }
243
244 static int package_list(GTypeModule *pkg, long current)
245 {
246         struct upkg *upkg = U_PKG(pkg)->pkg;
247         const struct upkg_export *parent = NULL;
248
249         if (current >= 0) {
250                 parent = upkg_get_export(upkg, current);
251                 assert(parent != NULL);
252         }
253
254         for (unsigned i = 0; i < upkg->export_count; i++) {
255                 const struct upkg_export *export = upkg_get_export(upkg, i);
256
257                 if (export->parent != parent)
258                         continue;
259
260                 export_print_fullname(pkg, export);
261
262                 if (verbose >= 1) {
263                         const char *class, *package;
264
265                         class = upkg_export_class(upkg, i, &package);
266                         printf(" (%s.%s)", package, class);
267                 }
268
269                 putchar('\n');
270         }
271
272         return 0;
273 }
274
275 static int object_info(GTypeModule *pkg, unsigned long idx)
276 {
277         struct upkg *upkg = U_PKG(pkg)->pkg;
278         const struct upkg_export *export;
279         const char *class, *package;
280
281         export = upkg_get_export(upkg, idx);
282         export_print_fullname(pkg, export);
283
284         class = upkg_export_class(upkg, idx, &package);
285         printf(" (%s.%s)\n", package, class);
286
287         /* Print out object properties. */
288         if (verbose >= 1) {
289                 GParamSpec **props;
290                 GObject *obj;
291                 unsigned n;
292
293                 obj = u_object_new_from_package(upkg, idx);
294                 if (!obj) {
295                         fprintf(stderr, "%s: failed to load object.\n",
296                                         progname);
297                         return -1;
298                 }
299
300                 props = g_object_class_list_properties(G_OBJECT_GET_CLASS(obj), &n);
301                 for (unsigned i = 0; i < n; i++) {
302                         GValue val = {0};
303                         char *valstr;
304
305                         printf("  property %s:", props[i]->name);
306                         fflush(stdout);
307
308                         g_value_init(&val, props[i]->value_type);
309                         g_object_get_property(obj, props[i]->name, &val);
310
311                         if (G_VALUE_HOLDS(&val, U_TYPE_OBJECT)) {
312                                 UObject *obj = g_value_get_object(&val);
313
314                                 /* TODO: Display full object names here. */
315                                 if (obj->pkg_name) {
316                                         printf(" %s", obj->pkg_name);
317                                 }
318                         }
319
320                         valstr = g_strdup_value_contents(&val);
321                         printf(" %s\n", valstr);
322                         g_free(valstr);
323                 }
324                 free(props);
325
326                 g_object_unref(obj);
327         }
328
329         /* Print raw file info. */
330         if (verbose >= 3) {
331                 struct upkg_file *f = upkg_export_open(upkg, idx);
332                 if (!f) {
333                         fprintf(stderr, "%s: failed to open export.\n",
334                                         progname);
335                         return -1;
336                 }
337
338                 printf("  file size: %lu\n", f->len);
339                 printf("  file start: %#lx\n", f->base);
340                 printf("  file end: %#lx\n", f->base + f->len);
341
342                 upkg_export_close(f);
343         }
344
345         return 0;
346 }
347
348 static int export(GTypeModule *pkg, GObject *obj, unsigned idx)
349 {
350         struct upkg *upkg = U_PKG(pkg)->pkg;
351         char name[256];
352         FILE *of;
353         int rc;
354
355         if (U_OBJECT_IS_LOADABLE(obj) && u_object_load(obj) != 0) {
356                 fprintf(stderr, "%s: failed to load export data.\n", progname);
357                 return -1;
358         }
359
360         rc = u_object_export_name(obj, name, sizeof name);
361         if (rc <= 0) {
362                 /* XXX: We could use a default name here. */
363                 fprintf(stderr, "%s: failed to determine export filename.\n",
364                                 progname);
365                 return -1;
366         }
367
368         printf("exporting ");
369         export_print_fullname(pkg, upkg_get_export(upkg, idx));
370         printf(" to %s\n", name);
371
372         of = fopen(name, "wb");
373         if (!of) {
374                 perror(name);
375                 return -1;
376         }
377
378         u_object_export(obj, of);
379
380         if (fclose(of) != 0) {
381                 perror(name);
382                 return -1;
383         }
384
385         if (U_OBJECT_IS_LOADABLE(obj)) {
386                 u_object_unload(obj);
387         }
388
389         return 0;
390 }
391
392 static int object_export(GTypeModule *pkg, unsigned long idx)
393 {
394         struct upkg *upkg = U_PKG(pkg)->pkg;
395         GObject *obj;
396         int ret = -1;
397
398         obj = u_object_new_from_package(upkg, idx);
399         if (!obj) {
400                 fprintf(stderr, "%s: failed to load object.\n", progname);
401                 return -1;
402         }
403
404         if (!U_OBJECT_IS_EXPORTABLE(obj)) {
405                 fprintf(stderr, "%s: object is not exportable.\n", progname);
406                 goto out;
407         }
408
409         ret = export(pkg, obj, idx);
410 out:
411         g_object_unref(obj);
412         return ret;
413 }
414
415 int package_export(GTypeModule *pkg)
416 {
417         struct upkg *upkg = U_PKG(pkg)->pkg;
418         const char *class, *package;
419         GObject *obj;
420         GType type;
421         int ret = 0;
422
423         for (unsigned i = 0; i < upkg->export_count; i++) {
424                 class = upkg_export_class(upkg, i, &package);
425                 if (!class) {
426                         fprintf(stderr, "error getting class information.\n");
427                         return -1;
428                 }
429
430                 type = u_object_module_get_class(package, class);
431                 if (!type)
432                         continue;
433
434                 obj = g_object_new(type, NULL);
435                 if (U_OBJECT_IS_EXPORTABLE(obj)) {
436                         if (u_object_deserialize(obj, upkg, i) != 0
437                             || export(pkg, obj, i) != 0) {
438                                 ret = -1;
439                         }
440                 }
441                 g_object_unref(obj);
442         }
443
444         return ret;
445 }
446
447 static int process_object(int mode, const char *objname)
448 {
449         char work[strlen(objname)+1], *p = work, *c;
450         GTypeModule *pkg;
451         long current = -1;
452         int ret = 0;
453
454         strcpy(work, objname);
455
456         /* First, find and open the actual package. */
457         c = strchr(p, '.');
458         if (c) *c = 0;
459
460         pkg = u_pkg_open(p);
461         if (!pkg || !g_type_module_use(pkg)) {
462                 fprintf(stderr, "%s: %s: failed to open package.\n",
463                                 progname, work);
464                 goto out;
465         }
466
467         if (!U_PKG(pkg)->pkg) {
468                 if (u_pkg_is_native(pkg)) {
469                         fprintf(stderr, "%s: %s: not a UObject package.\n",
470                                         progname, work);
471                 } else {
472                         fprintf(stderr, "%s: %s: package not found.\n",
473                                         progname, work);
474                 }
475
476                 goto out_unuse;
477         }
478
479         /* Resolve the hierarchical reference. */
480         while (c) {
481                 p = c+1;
482                 c = strchr(p, '.');
483                 if (c) *c = 0;
484
485                 current = upkg_export_find(U_PKG(pkg)->pkg, current, p);
486                 if (current == -1) {
487                         /* We want to print the full name up to this point. */
488                         strcpy(work, objname);
489                         if (c) *c = 0;
490
491                         fprintf(stderr, "%s: %s: object not found.\n",
492                                         progname, work);
493                         goto out_unuse;
494                 }
495         }
496
497         switch (mode) {
498         case MODE_INFO:
499                 if (current < 0)
500                         ret = package_info(U_PKG(pkg)->pkg);
501                 else
502                         ret = object_info(pkg, current);
503                 break;
504         case MODE_LIST:
505                 ret = package_list(pkg, current);
506                 break;
507         case MODE_EXPORT:
508                 if (current < 0)
509                         ret = package_export(pkg);
510                 else
511                         ret = object_export(pkg, current);
512                 break;
513         default:
514                 abort();
515         }
516
517 out_unuse:
518         g_type_module_unuse(pkg);
519 out:
520         return ret;
521 }
522
523 int main(int argc, char **argv)
524 {
525         const char *pkgname = NULL;
526         unsigned mode = MODE_INFO;
527         int opt, rc;
528
529         if (argc > 0) progname = argv[0];
530
531         if (u_pkg_vfs_init() != 0)
532                 return EXIT_FAILURE;
533         if (u_object_module_init() != 0)
534                 return EXIT_FAILURE;
535
536         while ((opt = getopt_long(argc, argv, sopts, lopts, NULL)) != -1) {
537                 switch (opt) {
538                 case 'f':
539                         pkgname = u_pkg_vfs_add_local(NULL, optarg);
540                         if (!pkgname) {
541                                 fprintf(stderr, "failed to add package `%s'.\n", optarg);
542                                 return EXIT_FAILURE;
543                         }
544                         break;
545                 case 'i':
546                         mode = MODE_INFO;
547                         break;
548                 case 'l':
549                         mode = MODE_LIST;
550                         break;
551                 case 'x':
552                         mode = MODE_EXPORT;
553                         break;
554                 case 'v':
555                         verbose++;
556                         break;
557                 case 'V':
558                         print_version();
559                         return EXIT_SUCCESS;
560                 case 'H':
561                         print_help();
562                         return EXIT_SUCCESS;
563                 default:
564                         print_usage(stderr);
565                         return EXIT_FAILURE;
566                 }
567         }
568
569         if (!pkgname && !argv[optind]) {
570                 print_usage(stderr);
571                 return EXIT_FAILURE;
572         }
573
574         rc = EXIT_SUCCESS;
575         if (argv[optind]) {
576                 for (int i = optind; i < argc; i++) {
577                         if (process_object(mode, argv[i]) != 0)
578                                 rc = EXIT_FAILURE;
579                 }
580         } else {
581                 if (process_object(mode, pkgname) != 0)
582                         rc = EXIT_FAILURE;
583         }
584
585         u_object_module_exit();
586         u_pkg_vfs_exit();
587         return rc;
588 }