]> git.draconx.ca Git - upkg.git/blob - src/upkg.c
libupkg: Add signed integer unpacking to fix incorrect signedness issue.
[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 #include "loadable.h"
32
33 enum {
34         MODE_INFO,
35         MODE_EXPORT,
36         MODE_MAX
37 };
38
39 int verbose = 0;
40
41 static const char *progname = "upkg";
42 static const char *sopts = "ixvVH";
43 static const struct option lopts[] = {
44         { "info",     0, NULL, 'i' },
45         { "export",   0, NULL, 'x' },
46         { "verbose",  0, NULL, 'v' },
47         { "version",  0, NULL, 'V' },
48         { "help",     0, NULL, 'H' },
49         { 0 }
50 };
51
52 void print_version(void)
53 {
54         printf("%s\n", PACKAGE_STRING);
55         puts("\
56 Copyright (C) 2009 Nick Bowler.\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 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 void print_guid(unsigned char guid[static 16])
102 {
103         for (unsigned i = 0; i < 16; i++) {
104                 printf("%02hhx", guid[i]);
105                 if (i == 15)
106                         putchar('\n');
107                 else
108                         putchar(' ');
109         }
110 }
111
112 void print_upkg_exports(struct upkg *pkg)
113 {
114         for (unsigned i = 0; i < pkg->export_count; i++) {
115                 const char *name, *package, *class;
116
117                 name  = upkg_export_name(pkg, i);
118                 class = upkg_export_class(pkg, i, &package);
119
120                 if (!name || !class)
121                         continue;
122
123                 printf("%u - %s (%s.%s)\n", i+1, name, package, class);
124         }
125 }
126
127 int package_info(struct upkg *pkg)
128 {
129         printf("Version: %u\n",  pkg->version);
130         printf("License: %u\n",  pkg->license);
131         printf("GUID: ");
132         print_guid(pkg->guid);
133
134         printf("Flags:   %lx\n", pkg->flags);
135         if (verbose >= 1) {
136                 print_upkg_flags("\t", pkg->flags);
137         }
138
139         if (verbose >= 1) {
140                 printf("Names:   %lu\n", pkg->name_count);
141                 if (verbose >= 2) {
142                         for (unsigned long i = 0; i < pkg->name_count; i++) {
143                                 printf("\t%s\n", upkg_get_name(pkg, i));
144                         }
145                 }
146         }
147
148         printf("Exports: %lu\n", pkg->export_count);
149         if (verbose >= 1) print_upkg_exports(pkg);
150         printf("Imports: %lu\n", pkg->import_count);
151
152         return EXIT_SUCCESS;
153 }
154
155 static int export(struct upkg *pkg, GObject *obj, unsigned idx)
156 {
157         struct upkg_file *f = upkg_export_open(pkg, idx);
158         char name[256];
159         FILE *of;
160         int rc = -1;
161
162         if (u_object_deserialize(obj, f) != 0) {
163                 goto out;
164         }
165
166         if (U_OBJECT_IS_LOADABLE(obj) && u_object_load(obj) != 0) {
167                 goto out;
168         }
169
170         u_object_export_name(obj, name, sizeof name);
171
172         printf("exporting %s to %s\n", upkg_export_name(pkg, idx), name);
173         of = fopen(name, "wb");
174         if (!of) {
175                 perror(name);
176                 goto out;
177         }
178
179         rc = u_object_export(obj, of);
180
181         if (fclose(of) != 0) {
182                 perror(name);
183                 rc = -1;
184                 goto out;
185         }
186
187         if (U_OBJECT_IS_LOADABLE(obj)) {
188                 u_object_unload(obj);
189         }
190
191 out:
192         upkg_export_close(f);
193         return rc;
194 }
195
196 int package_export(struct upkg *pkg)
197 {
198         const char *class, *package;
199         GObject *obj;
200         GType type;
201         int rc = EXIT_SUCCESS;
202
203         for (unsigned i = 0; i < pkg->export_count; i++) {
204                 class = upkg_export_class(pkg, i, &package);
205                 if (!class) {
206                         fprintf(stderr, "error getting class information.\n");
207                         return EXIT_FAILURE;
208                 }
209
210                 type = module_get_class(package, class);
211                 if (!type) continue;
212
213                 obj = g_object_new(type, NULL);
214                 if (U_OBJECT_IS_EXPORTABLE(obj)) {
215                         if (export(pkg, obj, i) != 0) {
216                                 rc = EXIT_FAILURE;
217                         }
218                 }
219                 g_object_unref(obj);
220         }
221
222         return rc;
223 }
224
225 int main(int argc, char **argv)
226 {
227         struct upkg *pkg;
228         unsigned mode = MODE_INFO;
229         int opt, rc = EXIT_FAILURE;
230
231         if (argc > 0) progname = argv[0];
232
233         while ((opt = getopt_long(argc, argv, sopts, lopts, NULL)) != -1) {
234                 switch (opt) {
235                 case 'i':
236                         mode = MODE_INFO;
237                         break;
238                 case 'x':
239                         mode = MODE_EXPORT;
240                         break;
241                 case 'v':
242                         verbose++;
243                         break;
244                 case 'V':
245                         print_version();
246                         return EXIT_SUCCESS;
247                 case 'H':
248                         print_help();
249                         return EXIT_SUCCESS;
250                 default:
251                         print_usage(stderr);
252                         return EXIT_FAILURE;
253                 }
254         }
255
256         if (argv[optind] == NULL) {
257                 print_usage(stderr);
258                 return EXIT_FAILURE;
259         }
260
261         if (module_init() != 0)
262                 return EXIT_FAILURE;
263
264         pkg = upkg_fopen(argv[optind]);
265         if (!pkg) {
266                 fprintf(stderr, "failed to open package!\n");
267                 return EXIT_FAILURE;
268         }
269
270         switch (mode) {
271         case MODE_INFO:
272                 rc = package_info(pkg);
273                 break;
274         case MODE_EXPORT:
275                 rc = package_export(pkg);
276                 break;
277         }
278
279         upkg_close(pkg);
280         module_exit();
281         return rc;
282 }