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