]> git.draconx.ca Git - upkg.git/blob - src/upkg.c
upkg: Implement batch export.
[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 2 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, write to the Free Software
17  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18  */
19
20 #include <config.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <getopt.h>
24
25 #include <glib-object.h>
26
27 #include "upkg.h"
28 #include "module.h"
29 #include "uobject.h"
30 #include "exportable.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 This is free software: you are free to change and redistribute it.\n\
57 There is NO WARRANTY, to the extent permitted by law."
58         );
59 }
60
61 void print_usage(FILE *f)
62 {
63         fprintf(f, "Usage: %s [options] package\n", progname);
64 }
65
66 void print_help(void)
67 {
68         print_usage(stdout);
69         puts("Detailed help coming soon.  Until then, I'll just list my options.");
70         for (unsigned i = 0; lopts[i].name; i++) {
71                 const struct option *o = &lopts[i];
72                 printf("\t--%s", o->name);
73
74                 if (o->has_arg == 1) {
75                         printf("=val");
76                 } else if (o->has_arg == 2) {
77                         printf("[=val]");
78                 }
79
80                 printf(" (-%c)\n", o->val);
81         }
82 }
83
84 void print_upkg_flags(const char *prefix, unsigned long flags)
85 {
86         if (flags & UPKG_FLAG_ALLOW_DOWNLOAD)
87                 printf("%sAllowDownload\n", prefix);
88         if (flags & UPKG_FLAG_CLIENT_OPTIONAL)
89                 printf("%sClientOptional\n", prefix);
90         if (flags & UPKG_FLAG_SERVER_ONLY)
91                 printf("%sServerOnly\n", prefix);
92         if (flags & UPKG_FLAG_BROKEN_LINKS)
93                 printf("%sBrokenLinks\n", prefix);
94         if (flags & UPKG_FLAG_INSECURE)
95                 printf("%sInsecure\n", prefix);
96         if (flags & UPKG_FLAG_REQUIRED)
97                 printf("%sRequired\n", prefix);
98 }
99
100 void print_guid(unsigned char guid[static 16])
101 {
102         for (unsigned i = 0; i < 16; i++) {
103                 printf("%02hhx", guid[i]);
104                 if (i == 15)
105                         putchar('\n');
106                 else
107                         putchar(' ');
108         }
109 }
110
111 void print_upkg_exports(struct upkg *pkg)
112 {
113         for (unsigned i = 0; i < pkg->export_count; i++) {
114                 const char *name, *package, *class;
115
116                 name  = upkg_export_name(pkg, i);
117                 class = upkg_export_class(pkg, i, &package);
118
119                 if (!name || !class)
120                         continue;
121
122                 printf("%u - %s (%s.%s)\n", i+1, name, package, class);
123         }
124 }
125
126 int package_info(struct upkg *pkg)
127 {
128         printf("Version: %u\n",  pkg->version);
129         printf("License: %u\n",  pkg->license);
130         printf("GUID: ");
131         print_guid(pkg->guid);
132
133         printf("Flags:   %lx\n", pkg->flags);
134         if (verbose >= 1) {
135                 print_upkg_flags("\t", pkg->flags);
136         }
137
138         if (verbose >= 1) {
139                 printf("Names:   %lu\n", pkg->name_count);
140                 if (verbose >= 2) {
141                         for (unsigned long i = 0; i < pkg->name_count; i++) {
142                                 printf("\t%s\n", upkg_get_name(pkg, i));
143                         }
144                 }
145         }
146
147         printf("Exports: %lu\n", pkg->export_count);
148         if (verbose >= 1) print_upkg_exports(pkg);
149         printf("Imports: %lu\n", pkg->import_count);
150
151         return EXIT_SUCCESS;
152 }
153
154 static int export(struct upkg *pkg, GObject *obj, unsigned idx)
155 {
156         struct upkg_file *f = upkg_export_open(pkg, idx);
157         char name[256];
158         FILE *of;
159         int rc;
160
161         if (u_object_deserialize(obj, f) != 0) {
162                 return -1;
163         }
164         u_object_export_name(obj, name, sizeof name);
165
166         printf("exporting %s to %s\n", upkg_export_name(pkg, idx), name);
167         of = fopen(name, "wb");
168         if (!of) {
169                 perror(name);
170                 return -1;
171         }
172
173         rc = u_object_export(obj, of);
174
175         if (fclose(of) != 0) {
176                 perror(name);
177                 return -1;
178         }
179
180         return rc;
181 }
182
183 int package_export(struct upkg *pkg)
184 {
185         const char *class, *package;
186         GObject *obj;
187         GType type;
188         int rc = EXIT_SUCCESS;
189
190         for (unsigned i = 0; i < pkg->export_count; i++) {
191                 class = upkg_export_class(pkg, i, &package);
192
193                 type = module_get_class(package, class);
194                 if (!type) continue;
195
196                 obj = g_object_new(type, NULL);
197                 if (U_OBJECT_IS_EXPORTABLE(obj)) {
198                         if (export(pkg, obj, i) != 0) {
199                                 rc = EXIT_FAILURE;
200                         }
201                 }
202                 g_object_unref(obj);
203         }
204
205         return rc;
206 }
207
208 int main(int argc, char **argv)
209 {
210         struct upkg *pkg;
211         unsigned mode = MODE_INFO;
212         int opt, rc = EXIT_FAILURE;
213
214         if (argc > 0) progname = argv[0];
215
216         while ((opt = getopt_long(argc, argv, sopts, lopts, NULL)) != -1) {
217                 switch (opt) {
218                 case 'i':
219                         mode = MODE_INFO;
220                         break;
221                 case 'x':
222                         mode = MODE_EXPORT;
223                         break;
224                 case 'v':
225                         verbose++;
226                         break;
227                 case 'V':
228                         print_version();
229                         return EXIT_SUCCESS;
230                 case 'H':
231                         print_help();
232                         return EXIT_SUCCESS;
233                 default:
234                         print_usage(stderr);
235                         return EXIT_FAILURE;
236                 }
237         }
238
239         if (argv[optind] == NULL) {
240                 print_usage(stderr);
241                 return EXIT_FAILURE;
242         }
243
244         if (module_init() != 0)
245                 return EXIT_FAILURE;
246
247         pkg = upkg_fopen(argv[optind]);
248         if (!pkg) {
249                 fprintf(stderr, "failed to open package!\n");
250                 return EXIT_FAILURE;
251         }
252
253         switch (mode) {
254         case MODE_INFO:
255                 rc = package_info(pkg);
256                 break;
257         case MODE_EXPORT:
258                 rc = package_export(pkg);
259                 break;
260         }
261
262         upkg_close(pkg);
263         module_exit();
264         return rc;
265 }