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