]> git.draconx.ca Git - upkg.git/blob - src/upkg.c
uobject: Clean up the module loader namespace.
[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 <uobject/uobject.h>
29 #include <uobject/exportable.h>
30 #include <uobject/loadable.h>
31 #include <uobject/module.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         char name[256];
158         FILE *of;
159
160         if (u_object_deserialize(obj, pkg, idx) != 0) {
161                 return -1;
162         }
163
164         if (U_OBJECT_IS_LOADABLE(obj) && u_object_load(obj) != 0) {
165                 return -1;
166         }
167
168         u_object_export_name(obj, name, sizeof name);
169
170         printf("exporting %s to %s\n", upkg_export_name(pkg, idx), name);
171         of = fopen(name, "wb");
172         if (!of) {
173                 perror(name);
174                 return -1;
175         }
176
177         u_object_export(obj, of);
178
179         if (fclose(of) != 0) {
180                 perror(name);
181                 return -1;
182         }
183
184         if (U_OBJECT_IS_LOADABLE(obj)) {
185                 u_object_unload(obj);
186         }
187
188         return 0;
189 }
190
191 int package_export(struct upkg *pkg)
192 {
193         const char *class, *package;
194         GObject *obj;
195         GType type;
196         int rc = EXIT_SUCCESS;
197
198         for (unsigned i = 0; i < pkg->export_count; i++) {
199                 class = upkg_export_class(pkg, i, &package);
200                 if (!class) {
201                         fprintf(stderr, "error getting class information.\n");
202                         return EXIT_FAILURE;
203                 }
204
205                 type = uobject_module_get_class(package, class);
206                 if (!type) continue;
207
208                 obj = g_object_new(type, NULL);
209                 if (U_OBJECT_IS_EXPORTABLE(obj)) {
210                         if (export(pkg, obj, i) != 0) {
211                                 rc = EXIT_FAILURE;
212                         }
213                 }
214                 g_object_unref(obj);
215         }
216
217         return rc;
218 }
219
220 int main(int argc, char **argv)
221 {
222         struct upkg *pkg;
223         unsigned mode = MODE_INFO;
224         int opt, rc = EXIT_FAILURE;
225
226         if (argc > 0) progname = argv[0];
227
228         while ((opt = getopt_long(argc, argv, sopts, lopts, NULL)) != -1) {
229                 switch (opt) {
230                 case 'i':
231                         mode = MODE_INFO;
232                         break;
233                 case 'x':
234                         mode = MODE_EXPORT;
235                         break;
236                 case 'v':
237                         verbose++;
238                         break;
239                 case 'V':
240                         print_version();
241                         return EXIT_SUCCESS;
242                 case 'H':
243                         print_help();
244                         return EXIT_SUCCESS;
245                 default:
246                         print_usage(stderr);
247                         return EXIT_FAILURE;
248                 }
249         }
250
251         if (argv[optind] == NULL) {
252                 print_usage(stderr);
253                 return EXIT_FAILURE;
254         }
255
256         if (uobject_module_init() != 0)
257                 return EXIT_FAILURE;
258
259         pkg = upkg_fopen(argv[optind]);
260         if (!pkg) {
261                 fprintf(stderr, "failed to open package!\n");
262                 return EXIT_FAILURE;
263         }
264
265         switch (mode) {
266         case MODE_INFO:
267                 rc = package_info(pkg);
268                 break;
269         case MODE_EXPORT:
270                 rc = package_export(pkg);
271                 break;
272         }
273
274         upkg_close(pkg);
275         uobject_module_exit();
276         return rc;
277 }