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