]> git.draconx.ca Git - upkg.git/blob - src/upkg.c
upkg: Resolve hierarchical object references.
[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 char *name, *package, *class;
185
186                 name  = upkg_export_name(pkg, i);
187                 class = upkg_export_class(pkg, i, &package);
188
189                 if (!name || !class)
190                         continue;
191
192                 printf("%u - %s (%s.%s)\n", i+1, name, package, class);
193                 printf("  Flags: %lx\n", upkg_export_flags(pkg, i));
194                 if (verbose >= 2) {
195                         print_upkg_object_flags("    ", upkg_export_flags(pkg, i));
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 int export(struct upkg *pkg, GObject *obj, unsigned idx)
227 {
228         char name[256];
229         FILE *of;
230
231         if (u_object_deserialize(obj, pkg, idx) != 0) {
232                 return -1;
233         }
234
235         if (U_OBJECT_IS_LOADABLE(obj) && u_object_load(obj) != 0) {
236                 return -1;
237         }
238
239         u_object_export_name(obj, name, sizeof name);
240
241         printf("exporting %s to %s\n", upkg_export_name(pkg, idx), name);
242         of = fopen(name, "wb");
243         if (!of) {
244                 perror(name);
245                 return -1;
246         }
247
248         u_object_export(obj, of);
249
250         if (fclose(of) != 0) {
251                 perror(name);
252                 return -1;
253         }
254
255         if (U_OBJECT_IS_LOADABLE(obj)) {
256                 u_object_unload(obj);
257         }
258
259         return 0;
260 }
261
262 int package_export(struct upkg *pkg)
263 {
264         const char *class, *package;
265         GObject *obj;
266         GType type;
267         int ret = 0;
268
269         for (unsigned i = 0; i < pkg->export_count; i++) {
270                 class = upkg_export_class(pkg, i, &package);
271                 if (!class) {
272                         fprintf(stderr, "error getting class information.\n");
273                         return -1;
274                 }
275
276                 type = u_object_module_get_class(package, class);
277                 if (!type) continue;
278
279                 obj = g_object_new(type, NULL);
280                 if (U_OBJECT_IS_EXPORTABLE(obj)) {
281                         if (export(pkg, obj, i) != 0) {
282                                 ret = -1;
283                         }
284                 }
285                 g_object_unref(obj);
286         }
287
288         return ret;
289 }
290
291 static int process_object(int mode, const char *objname)
292 {
293         char work[strlen(objname)+1], *p = work, *c;
294         GTypeModule *pkg;
295         long current = -1;
296         int ret = 0;
297
298         strcpy(work, objname);
299
300         /* First, find and open the actual package. */
301         c = strchr(p, '.');
302         if (c) *c = 0;
303
304         pkg = u_pkg_open(p);
305         if (!pkg || !g_type_module_use(pkg)) {
306                 fprintf(stderr, "%s: %s: failed to open package.\n",
307                                 progname, work);
308                 goto out;
309         }
310
311         if (!U_PKG(pkg)->pkg) {
312                 if (u_pkg_is_native(pkg)) {
313                         fprintf(stderr, "%s: %s: not a UObject package.\n",
314                                         progname, work);
315                 } else {
316                         fprintf(stderr, "%s: %s: package not found.\n",
317                                         progname, work);
318                 }
319
320                 goto out_unuse;
321         }
322
323         /* Resolve the hierarchical reference. */
324         while (c) {
325                 p = c+1;
326                 c = strchr(p, '.');
327                 if (c) *c = 0;
328
329                 current = upkg_export_find(U_PKG(pkg)->pkg, current, p);
330                 if (current == -1) {
331                         /* We want to print the full name up to this point. */
332                         strcpy(work, objname);
333                         if (c) *c = 0;
334
335                         fprintf(stderr, "%s: %s: object not found.\n",
336                                         progname, work);
337                         goto out_unuse;
338                 }
339         }
340
341         switch (mode) {
342         case MODE_INFO:
343                 ret = package_info(U_PKG(pkg)->pkg);
344                 break;
345         case MODE_EXPORT:
346                 ret = package_export(U_PKG(pkg)->pkg);
347                 break;
348         default:
349                 abort();
350         }
351
352 out_unuse:
353         g_type_module_unuse(pkg);
354 out:
355         return ret;
356 }
357
358 int main(int argc, char **argv)
359 {
360         const char *pkgname = NULL;
361         unsigned mode = MODE_INFO;
362         int opt, rc;
363
364         if (argc > 0) progname = argv[0];
365
366         if (u_pkg_vfs_init() != 0)
367                 return EXIT_FAILURE;
368         if (u_object_module_init() != 0)
369                 return EXIT_FAILURE;
370
371         while ((opt = getopt_long(argc, argv, sopts, lopts, NULL)) != -1) {
372                 switch (opt) {
373                 case 'f':
374                         pkgname = u_pkg_vfs_add_local(NULL, optarg);
375                         if (!pkgname) {
376                                 fprintf(stderr, "failed to add package `%s'.\n", optarg);
377                                 return EXIT_FAILURE;
378                         }
379                         break;
380                 case 'i':
381                         mode = MODE_INFO;
382                         break;
383                 case 'x':
384                         mode = MODE_EXPORT;
385                         break;
386                 case 'v':
387                         verbose++;
388                         break;
389                 case 'V':
390                         print_version();
391                         return EXIT_SUCCESS;
392                 case 'H':
393                         print_help();
394                         return EXIT_SUCCESS;
395                 default:
396                         print_usage(stderr);
397                         return EXIT_FAILURE;
398                 }
399         }
400
401         if (!pkgname && !argv[optind]) {
402                 print_usage(stderr);
403                 return EXIT_FAILURE;
404         }
405
406         rc = EXIT_SUCCESS;
407         if (argv[optind]) {
408                 for (int i = optind; i < argc; i++) {
409                         if (process_object(mode, argv[i]) != 0)
410                                 rc = EXIT_FAILURE;
411                 }
412         } else {
413                 if (process_object(mode, pkgname) != 0)
414                         rc = EXIT_FAILURE;
415         }
416
417         u_object_module_exit();
418         u_pkg_vfs_exit();
419         return rc;
420 }