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