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