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>
30 #include "exportable.h"
40 static const char *progname = "upkg";
41 static const char *sopts = "ixvVH";
42 static const struct option lopts[] = {
43 { "info", 0, NULL, 'i' },
44 { "export", 0, NULL, 'x' },
45 { "verbose", 0, NULL, 'v' },
46 { "version", 0, NULL, 'V' },
47 { "help", 0, NULL, 'H' },
51 void print_version(void)
53 printf("%s\n", PACKAGE_STRING);
55 Copyright (C) 2009 Nick Bowler.\n\
56 This is free software: you are free to change and redistribute it.\n\
57 There is NO WARRANTY, to the extent permitted by law."
61 void print_usage(FILE *f)
63 fprintf(f, "Usage: %s [options] package\n", progname);
69 puts("Detailed help coming soon. Until then, I'll just list my options.");
70 for (unsigned i = 0; lopts[i].name; i++) {
71 const struct option *o = &lopts[i];
72 printf("\t--%s", o->name);
74 if (o->has_arg == 1) {
76 } else if (o->has_arg == 2) {
80 printf(" (-%c)\n", o->val);
84 void print_upkg_flags(const char *prefix, unsigned long flags)
86 if (flags & UPKG_FLAG_ALLOW_DOWNLOAD)
87 printf("%sAllowDownload\n", prefix);
88 if (flags & UPKG_FLAG_CLIENT_OPTIONAL)
89 printf("%sClientOptional\n", prefix);
90 if (flags & UPKG_FLAG_SERVER_ONLY)
91 printf("%sServerOnly\n", prefix);
92 if (flags & UPKG_FLAG_BROKEN_LINKS)
93 printf("%sBrokenLinks\n", prefix);
94 if (flags & UPKG_FLAG_INSECURE)
95 printf("%sInsecure\n", prefix);
96 if (flags & UPKG_FLAG_REQUIRED)
97 printf("%sRequired\n", prefix);
100 void print_guid(unsigned char guid[static 16])
102 for (unsigned i = 0; i < 16; i++) {
103 printf("%02hhx", guid[i]);
111 void print_upkg_exports(struct upkg *pkg)
113 for (unsigned i = 0; i < pkg->export_count; i++) {
114 const char *name, *package, *class;
116 name = upkg_export_name(pkg, i);
117 class = upkg_export_class(pkg, i, &package);
122 printf("%u - %s (%s.%s)\n", i+1, name, package, class);
126 int package_info(struct upkg *pkg)
128 printf("Version: %u\n", pkg->version);
129 printf("License: %u\n", pkg->license);
131 print_guid(pkg->guid);
133 printf("Flags: %lx\n", pkg->flags);
135 print_upkg_flags("\t", pkg->flags);
139 printf("Names: %lu\n", pkg->name_count);
141 for (unsigned long i = 0; i < pkg->name_count; i++) {
142 printf("\t%s\n", upkg_get_name(pkg, i));
147 printf("Exports: %lu\n", pkg->export_count);
148 if (verbose >= 1) print_upkg_exports(pkg);
149 printf("Imports: %lu\n", pkg->import_count);
154 static int export(struct upkg *pkg, GObject *obj, unsigned idx)
156 struct upkg_file *f = upkg_export_open(pkg, idx);
161 if (u_object_deserialize(obj, f) != 0) {
164 u_object_export_name(obj, name, sizeof name);
166 printf("exporting %s to %s\n", upkg_export_name(pkg, idx), name);
167 of = fopen(name, "wb");
173 rc = u_object_export(obj, of);
175 if (fclose(of) != 0) {
182 upkg_export_close(f);
186 int package_export(struct upkg *pkg)
188 const char *class, *package;
191 int rc = EXIT_SUCCESS;
193 for (unsigned i = 0; i < pkg->export_count; i++) {
194 class = upkg_export_class(pkg, i, &package);
196 type = module_get_class(package, class);
199 obj = g_object_new(type, NULL);
200 if (U_OBJECT_IS_EXPORTABLE(obj)) {
201 if (export(pkg, obj, i) != 0) {
211 int main(int argc, char **argv)
214 unsigned mode = MODE_INFO;
215 int opt, rc = EXIT_FAILURE;
217 if (argc > 0) progname = argv[0];
219 while ((opt = getopt_long(argc, argv, sopts, lopts, NULL)) != -1) {
242 if (argv[optind] == NULL) {
247 if (module_init() != 0)
250 pkg = upkg_fopen(argv[optind]);
252 fprintf(stderr, "failed to open package!\n");
258 rc = package_info(pkg);
261 rc = package_export(pkg);