2 * upkg: tool for manipulating Unreal Tournament packages.
3 * Copyright (C) 2009 Nick Bowler
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.
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.
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
25 #include <glib-object.h>
28 #include <uobject/uobject.h>
29 #include <uobject/exportable.h>
30 #include <uobject/loadable.h>
31 #include <uobject/module.h>
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' },
52 void print_version(void)
54 printf("%s\n", PACKAGE_STRING);
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."
62 void print_usage(FILE *f)
64 fprintf(f, "Usage: %s [options] package\n", progname);
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);
75 if (o->has_arg == 1) {
77 } else if (o->has_arg == 2) {
81 printf(" (-%c)\n", o->val);
85 void print_upkg_flags(const char *prefix, unsigned long flags)
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);
101 void print_guid(unsigned char guid[static 16])
103 for (unsigned i = 0; i < 16; i++) {
104 printf("%02hhx", guid[i]);
112 void print_upkg_exports(struct upkg *pkg)
114 for (unsigned i = 0; i < pkg->export_count; i++) {
115 const char *name, *package, *class;
117 name = upkg_export_name(pkg, i);
118 class = upkg_export_class(pkg, i, &package);
123 printf("%u - %s (%s.%s)\n", i+1, name, package, class);
127 int package_info(struct upkg *pkg)
129 printf("Version: %u\n", pkg->version);
130 printf("License: %u\n", pkg->license);
132 print_guid(pkg->guid);
134 printf("Flags: %lx\n", pkg->flags);
136 print_upkg_flags("\t", pkg->flags);
140 printf("Names: %lu\n", pkg->name_count);
142 for (unsigned long i = 0; i < pkg->name_count; i++) {
143 printf("\t%s\n", upkg_get_name(pkg, i));
148 printf("Exports: %lu\n", pkg->export_count);
149 if (verbose >= 1) print_upkg_exports(pkg);
150 printf("Imports: %lu\n", pkg->import_count);
155 static int export(struct upkg *pkg, GObject *obj, unsigned idx)
160 if (u_object_deserialize(obj, pkg, idx) != 0) {
164 if (U_OBJECT_IS_LOADABLE(obj) && u_object_load(obj) != 0) {
168 u_object_export_name(obj, name, sizeof name);
170 printf("exporting %s to %s\n", upkg_export_name(pkg, idx), name);
171 of = fopen(name, "wb");
177 u_object_export(obj, of);
179 if (fclose(of) != 0) {
184 if (U_OBJECT_IS_LOADABLE(obj)) {
185 u_object_unload(obj);
191 int package_export(struct upkg *pkg)
193 const char *class, *package;
196 int rc = EXIT_SUCCESS;
198 for (unsigned i = 0; i < pkg->export_count; i++) {
199 class = upkg_export_class(pkg, i, &package);
201 fprintf(stderr, "error getting class information.\n");
205 type = u_object_module_get_class(package, class);
208 obj = g_object_new(type, NULL);
209 if (U_OBJECT_IS_EXPORTABLE(obj)) {
210 if (export(pkg, obj, i) != 0) {
220 int main(int argc, char **argv)
223 unsigned mode = MODE_INFO;
224 int opt, rc = EXIT_FAILURE;
226 if (argc > 0) progname = argv[0];
228 while ((opt = getopt_long(argc, argv, sopts, lopts, NULL)) != -1) {
251 if (argv[optind] == NULL) {
256 if (u_object_module_init() != 0)
259 pkg = upkg_fopen(argv[optind]);
261 fprintf(stderr, "failed to open package!\n");
267 rc = package_info(pkg);
270 rc = package_export(pkg);
275 u_object_module_exit();