]> git.draconx.ca Git - upkg.git/blob - src/upkg.c
upkg: Improve error reporting during package load.
[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 <getopt.h>
23
24 #include <glib-object.h>
25
26 #include "upkg.h"
27 #include <uobject/uobject.h>
28 #include <uobject/exportable.h>
29 #include <uobject/loadable.h>
30 #include <uobject/module.h>
31 #include <uobject/package.h>
32 #include <uobject/vfs.h>
33
34 enum {
35         MODE_INFO,
36         MODE_EXPORT,
37         MODE_MAX
38 };
39
40 int verbose = 0;
41
42 static const char *progname = "upkg";
43 static const char *sopts = "ixvf:VH";
44 static const struct option lopts[] = {
45         { "info",     0, NULL, 'i' },
46         { "export",   0, NULL, 'x' },
47         { "file",     1, NULL, 'f' },
48         { "verbose",  0, NULL, 'v' },
49         { "version",  0, NULL, 'V' },
50         { "help",     0, NULL, 'H' },
51         { 0 }
52 };
53
54 void print_version(void)
55 {
56         printf("%s\n", PACKAGE_STRING);
57         puts("\
58 Copyright (C) 2009 Nick Bowler.\n\
59 License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.\n\
60 This is free software: you are free to change and redistribute it.\n\
61 There is NO WARRANTY, to the extent permitted by law."
62         );
63 }
64
65 void print_usage(FILE *f)
66 {
67         fprintf(f, "Usage: %s [options]\n", progname);
68 }
69
70 void print_help(void)
71 {
72         print_usage(stdout);
73         puts("Detailed help coming soon.  Until then, I'll just list my options.");
74         for (unsigned i = 0; lopts[i].name; i++) {
75                 const struct option *o = &lopts[i];
76                 printf("\t--%s", o->name);
77
78                 if (o->has_arg == 1) {
79                         printf("=val");
80                 } else if (o->has_arg == 2) {
81                         printf("[=val]");
82                 }
83
84                 printf(" (-%c)\n", o->val);
85         }
86 }
87
88 static void print_upkg_flags(const char *prefix, unsigned long flags)
89 {
90         if (flags & UPKG_FLAG_ALLOW_DOWNLOAD)
91                 printf("%sAllowDownload\n", prefix);
92         if (flags & UPKG_FLAG_CLIENT_OPTIONAL)
93                 printf("%sClientOptional\n", prefix);
94         if (flags & UPKG_FLAG_SERVER_ONLY)
95                 printf("%sServerOnly\n", prefix);
96         if (flags & UPKG_FLAG_BROKEN_LINKS)
97                 printf("%sBrokenLinks\n", prefix);
98         if (flags & UPKG_FLAG_INSECURE)
99                 printf("%sInsecure\n", prefix);
100         if (flags & UPKG_FLAG_REQUIRED)
101                 printf("%sRequired\n", prefix);
102 }
103
104 static void print_upkg_object_flags(const char *prefix, unsigned long flags)
105 {
106         if (flags & UPKG_OBJ_FLAG_TRANSACTIONAL)
107                 printf("%sTransactional\n", prefix);
108         if (flags & UPKG_OBJ_FLAG_UNREACHABLE)
109                 printf("%sUnreachable\n", prefix);
110         if (flags & UPKG_OBJ_FLAG_PUBLIC)
111                 printf("%sPublic\n", prefix);
112         if (flags & UPKG_OBJ_FLAG_TAG_IMPORT)
113                 printf("%sImport\n", prefix);
114         if (flags & UPKG_OBJ_FLAG_TAG_EXPORT)
115                 printf("%sTagExport\n", prefix);
116         if (flags & UPKG_OBJ_FLAG_SOURCE_MODIFIED)
117                 printf("%sSourceModified\n", prefix);
118         if (flags & UPKG_OBJ_FLAG_TAG_GARBAGE)
119                 printf("%sTagGarbage\n", prefix);
120         if (flags & UPKG_OBJ_FLAG_NEED_LOAD)
121                 printf("%sNeedLoad\n", prefix);
122         if (flags & UPKG_OBJ_FLAG_HIGHLIGHT_NAME)
123                 printf("%sHighlightName\n", prefix);
124         if (flags & UPKG_OBJ_FLAG_IN_SINGULAR_FUNC)
125                 printf("%sInSingularFunc\n", prefix);
126         if (flags & UPKG_OBJ_FLAG_SUPPRESSED)
127                 printf("%sSuppressed\n", prefix);
128         if (flags & UPKG_OBJ_FLAG_IN_END_STATE)
129                 printf("%sInEndState\n", prefix);
130         if (flags & UPKG_OBJ_FLAG_TRANSIENT)
131                 printf("%sTransient\n", prefix);
132         if (flags & UPKG_OBJ_FLAG_PRELOADING)
133                 printf("%sPreloading\n", prefix);
134         if (flags & UPKG_OBJ_FLAG_LOAD_FOR_CLIENT)
135                 printf("%sLoadForClient\n", prefix);
136         if (flags & UPKG_OBJ_FLAG_LOAD_FOR_SERVER)
137                 printf("%sLoadForServer\n", prefix);
138         if (flags & UPKG_OBJ_FLAG_LOAD_FOR_EDIT)
139                 printf("%sLoadForEdit\n", prefix);
140         if (flags & UPKG_OBJ_FLAG_STANDALONE)
141                 printf("%sStandalone\n", prefix);
142         if (flags & UPKG_OBJ_FLAG_NOT_FOR_CLIENT)
143                 printf("%sNotForClient\n", prefix);
144         if (flags & UPKG_OBJ_FLAG_NOT_FOR_SERVER)
145                 printf("%sNotForServer\n", prefix);
146         if (flags & UPKG_OBJ_FLAG_NOT_FOR_EDIT)
147                 printf("%sNotForEdit\n", prefix);
148         if (flags & UPKG_OBJ_FLAG_DESTROYED)
149                 printf("%sDestroyed\n", prefix);
150         if (flags & UPKG_OBJ_FLAG_NEED_POST_LOAD)
151                 printf("%sNeedPostLoad\n", prefix);
152         if (flags & UPKG_OBJ_FLAG_HAS_STACK)
153                 printf("%sHasStack\n", prefix);
154         if (flags & UPKG_OBJ_FLAG_NATIVE)
155                 printf("%sNative\n", prefix);
156         if (flags & UPKG_OBJ_FLAG_MARKED)
157                 printf("%sMarked\n", prefix);
158         if (flags & UPKG_OBJ_FLAG_ERROR_SHUTDOWN)
159                 printf("%sErrorShutdown\n", prefix);
160         if (flags & UPKG_OBJ_FLAG_DEBUG_POST_LOAD)
161                 printf("%sDebugPostLoad\n", prefix);
162         if (flags & UPKG_OBJ_FLAG_DEBUG_SERIALIZE)
163                 printf("%sDebugSerialize\n", prefix);
164         if (flags & UPKG_OBJ_FLAG_DEBUG_DESTROY)
165                 printf("%sDebugDestroy\n", prefix);
166 }
167
168 void print_guid(unsigned char guid[static 16])
169 {
170         for (unsigned i = 0; i < 16; i++) {
171                 printf("%02hhx", guid[i]);
172                 if (i == 15)
173                         putchar('\n');
174                 else
175                         putchar(' ');
176         }
177 }
178
179 void print_upkg_exports(struct upkg *pkg)
180 {
181         for (unsigned i = 0; i < pkg->export_count; i++) {
182                 const char *name, *package, *class;
183
184                 name  = upkg_export_name(pkg, i);
185                 class = upkg_export_class(pkg, i, &package);
186
187                 if (!name || !class)
188                         continue;
189
190                 printf("%u - %s (%s.%s)\n", i+1, name, package, class);
191                 printf("  Flags: %lx\n", upkg_export_flags(pkg, i));
192                 if (verbose >= 2) {
193                         print_upkg_object_flags("    ", upkg_export_flags(pkg, i));
194                 }
195         }
196 }
197
198 int package_info(struct upkg *pkg)
199 {
200         printf("Version: %u\n",  pkg->version);
201         printf("License: %u\n",  pkg->license);
202         printf("GUID: ");
203         print_guid(pkg->guid);
204
205         printf("Flags:   %lx\n", pkg->flags);
206         if (verbose >= 2) {
207                 print_upkg_flags("        ", pkg->flags);
208         }
209
210         printf("Names:   %lu\n", pkg->name_count);
211         if (verbose >= 3) {
212                 for (unsigned long i = 0; i < pkg->name_count; i++) {
213                         printf("\t%s\n", upkg_get_name(pkg, i));
214                 }
215         }
216
217         printf("Exports: %lu\n", pkg->export_count);
218         if (verbose >= 1) print_upkg_exports(pkg);
219         printf("Imports: %lu\n", pkg->import_count);
220
221         return EXIT_SUCCESS;
222 }
223
224 static int export(struct upkg *pkg, GObject *obj, unsigned idx)
225 {
226         char name[256];
227         FILE *of;
228
229         if (u_object_deserialize(obj, pkg, idx) != 0) {
230                 return -1;
231         }
232
233         if (U_OBJECT_IS_LOADABLE(obj) && u_object_load(obj) != 0) {
234                 return -1;
235         }
236
237         u_object_export_name(obj, name, sizeof name);
238
239         printf("exporting %s to %s\n", upkg_export_name(pkg, idx), name);
240         of = fopen(name, "wb");
241         if (!of) {
242                 perror(name);
243                 return -1;
244         }
245
246         u_object_export(obj, of);
247
248         if (fclose(of) != 0) {
249                 perror(name);
250                 return -1;
251         }
252
253         if (U_OBJECT_IS_LOADABLE(obj)) {
254                 u_object_unload(obj);
255         }
256
257         return 0;
258 }
259
260 int package_export(struct upkg *pkg)
261 {
262         const char *class, *package;
263         GObject *obj;
264         GType type;
265         int rc = EXIT_SUCCESS;
266
267         for (unsigned i = 0; i < pkg->export_count; i++) {
268                 class = upkg_export_class(pkg, i, &package);
269                 if (!class) {
270                         fprintf(stderr, "error getting class information.\n");
271                         return EXIT_FAILURE;
272                 }
273
274                 type = u_object_module_get_class(package, class);
275                 if (!type) continue;
276
277                 obj = g_object_new(type, NULL);
278                 if (U_OBJECT_IS_EXPORTABLE(obj)) {
279                         if (export(pkg, obj, i) != 0) {
280                                 rc = EXIT_FAILURE;
281                         }
282                 }
283                 g_object_unref(obj);
284         }
285
286         return rc;
287 }
288
289 int main(int argc, char **argv)
290 {
291         GTypeModule *pkg;
292         const char *pkgname = NULL;
293         unsigned mode = MODE_INFO;
294         int opt, rc = EXIT_FAILURE;
295
296         if (argc > 0) progname = argv[0];
297
298         if (u_pkg_vfs_init() != 0)
299                 return EXIT_FAILURE;
300         if (u_object_module_init() != 0)
301                 return EXIT_FAILURE;
302
303         while ((opt = getopt_long(argc, argv, sopts, lopts, NULL)) != -1) {
304                 switch (opt) {
305                 case 'f':
306                         pkgname = u_pkg_vfs_add_local(NULL, optarg);
307                         if (!pkgname) {
308                                 fprintf(stderr, "failed to add package `%s'.\n", optarg);
309                                 return EXIT_FAILURE;
310                         }
311                         break;
312                 case 'i':
313                         mode = MODE_INFO;
314                         break;
315                 case 'x':
316                         mode = MODE_EXPORT;
317                         break;
318                 case 'v':
319                         verbose++;
320                         break;
321                 case 'V':
322                         print_version();
323                         return EXIT_SUCCESS;
324                 case 'H':
325                         print_help();
326                         return EXIT_SUCCESS;
327                 default:
328                         print_usage(stderr);
329                         return EXIT_FAILURE;
330                 }
331         }
332
333         if (!pkgname) {
334                 print_usage(stderr);
335                 return EXIT_FAILURE;
336         }
337
338         pkg = u_pkg_open(pkgname);
339         if (!pkg || !g_type_module_use(pkg)) {
340                 fprintf(stderr, "%s: %s: failed to open package.\n",
341                                 progname, pkgname);
342                 return EXIT_FAILURE;
343         }
344
345         if (!U_PKG(pkg)->pkg) {
346                 if (u_pkg_is_native(pkg)) {
347                         fprintf(stderr, "%s: %s: not a UObject package.\n",
348                                         progname, pkgname);
349                 } else {
350                         fprintf(stderr, "%s: %s: package not found.\n",
351                                         progname, pkgname);
352                 }
353
354                 return EXIT_FAILURE;
355         }
356
357         switch (mode) {
358         case MODE_INFO:
359                 rc = package_info(U_PKG(pkg)->pkg);
360                 break;
361         case MODE_EXPORT:
362                 rc = package_export(U_PKG(pkg)->pkg);
363                 break;
364         }
365
366         g_type_module_unuse(pkg);
367         u_object_module_exit();
368         u_pkg_vfs_exit();
369         return rc;
370 }