2 * upkg: tool for manipulating Unreal Tournament packages.
3 * Copyright © 2009-2011 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 3 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, see <http://www.gnu.org/licenses/>.
26 #include <glib-object.h>
29 #include <uobject/uobject.h>
30 #include <uobject/exportable.h>
31 #include <uobject/loadable.h>
32 #include <uobject/module.h>
33 #include <uobject/package.h>
34 #include <uobject/vfs.h>
45 static const char *progname = "upkg";
46 static const char *sopts = "ilxvf:VH";
47 static const struct option lopts[] = {
48 { "info", 0, NULL, 'i' },
49 { "list", 0, NULL, 'l' },
50 { "export", 0, NULL, 'x' },
51 { "file", 1, NULL, 'f' },
52 { "verbose", 0, NULL, 'v' },
53 { "version", 0, NULL, 'V' },
54 { "help", 0, NULL, 'H' },
58 void print_version(void)
60 printf("%s\n", PACKAGE_STRING);
62 Copyright (C) 2009 Nick Bowler.\n\
63 License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.\n\
64 This is free software: you are free to change and redistribute it.\n\
65 There is NO WARRANTY, to the extent permitted by law."
69 void print_usage(FILE *f)
71 fprintf(f, "Usage: %s [options] [object ...]\n", progname);
77 puts("Detailed help coming soon. Until then, I'll just list my options.");
78 for (unsigned i = 0; lopts[i].name; i++) {
79 const struct option *o = &lopts[i];
80 printf("\t--%s", o->name);
82 if (o->has_arg == 1) {
84 } else if (o->has_arg == 2) {
88 printf(" (-%c)\n", o->val);
92 static void print_upkg_flags(const char *prefix, unsigned long flags)
94 if (flags & UPKG_FLAG_ALLOW_DOWNLOAD)
95 printf("%sAllowDownload\n", prefix);
96 if (flags & UPKG_FLAG_CLIENT_OPTIONAL)
97 printf("%sClientOptional\n", prefix);
98 if (flags & UPKG_FLAG_SERVER_ONLY)
99 printf("%sServerOnly\n", prefix);
100 if (flags & UPKG_FLAG_BROKEN_LINKS)
101 printf("%sBrokenLinks\n", prefix);
102 if (flags & UPKG_FLAG_INSECURE)
103 printf("%sInsecure\n", prefix);
104 if (flags & UPKG_FLAG_REQUIRED)
105 printf("%sRequired\n", prefix);
108 static void print_upkg_object_flags(const char *prefix, unsigned long flags)
110 if (flags & UPKG_OBJ_FLAG_TRANSACTIONAL)
111 printf("%sTransactional\n", prefix);
112 if (flags & UPKG_OBJ_FLAG_UNREACHABLE)
113 printf("%sUnreachable\n", prefix);
114 if (flags & UPKG_OBJ_FLAG_PUBLIC)
115 printf("%sPublic\n", prefix);
116 if (flags & UPKG_OBJ_FLAG_TAG_IMPORT)
117 printf("%sImport\n", prefix);
118 if (flags & UPKG_OBJ_FLAG_TAG_EXPORT)
119 printf("%sTagExport\n", prefix);
120 if (flags & UPKG_OBJ_FLAG_SOURCE_MODIFIED)
121 printf("%sSourceModified\n", prefix);
122 if (flags & UPKG_OBJ_FLAG_TAG_GARBAGE)
123 printf("%sTagGarbage\n", prefix);
124 if (flags & UPKG_OBJ_FLAG_NEED_LOAD)
125 printf("%sNeedLoad\n", prefix);
126 if (flags & UPKG_OBJ_FLAG_HIGHLIGHT_NAME)
127 printf("%sHighlightName\n", prefix);
128 if (flags & UPKG_OBJ_FLAG_IN_SINGULAR_FUNC)
129 printf("%sInSingularFunc\n", prefix);
130 if (flags & UPKG_OBJ_FLAG_SUPPRESSED)
131 printf("%sSuppressed\n", prefix);
132 if (flags & UPKG_OBJ_FLAG_IN_END_STATE)
133 printf("%sInEndState\n", prefix);
134 if (flags & UPKG_OBJ_FLAG_TRANSIENT)
135 printf("%sTransient\n", prefix);
136 if (flags & UPKG_OBJ_FLAG_PRELOADING)
137 printf("%sPreloading\n", prefix);
138 if (flags & UPKG_OBJ_FLAG_LOAD_FOR_CLIENT)
139 printf("%sLoadForClient\n", prefix);
140 if (flags & UPKG_OBJ_FLAG_LOAD_FOR_SERVER)
141 printf("%sLoadForServer\n", prefix);
142 if (flags & UPKG_OBJ_FLAG_LOAD_FOR_EDIT)
143 printf("%sLoadForEdit\n", prefix);
144 if (flags & UPKG_OBJ_FLAG_STANDALONE)
145 printf("%sStandalone\n", prefix);
146 if (flags & UPKG_OBJ_FLAG_NOT_FOR_CLIENT)
147 printf("%sNotForClient\n", prefix);
148 if (flags & UPKG_OBJ_FLAG_NOT_FOR_SERVER)
149 printf("%sNotForServer\n", prefix);
150 if (flags & UPKG_OBJ_FLAG_NOT_FOR_EDIT)
151 printf("%sNotForEdit\n", prefix);
152 if (flags & UPKG_OBJ_FLAG_DESTROYED)
153 printf("%sDestroyed\n", prefix);
154 if (flags & UPKG_OBJ_FLAG_NEED_POST_LOAD)
155 printf("%sNeedPostLoad\n", prefix);
156 if (flags & UPKG_OBJ_FLAG_HAS_STACK)
157 printf("%sHasStack\n", prefix);
158 if (flags & UPKG_OBJ_FLAG_NATIVE)
159 printf("%sNative\n", prefix);
160 if (flags & UPKG_OBJ_FLAG_MARKED)
161 printf("%sMarked\n", prefix);
162 if (flags & UPKG_OBJ_FLAG_ERROR_SHUTDOWN)
163 printf("%sErrorShutdown\n", prefix);
164 if (flags & UPKG_OBJ_FLAG_DEBUG_POST_LOAD)
165 printf("%sDebugPostLoad\n", prefix);
166 if (flags & UPKG_OBJ_FLAG_DEBUG_SERIALIZE)
167 printf("%sDebugSerialize\n", prefix);
168 if (flags & UPKG_OBJ_FLAG_DEBUG_DESTROY)
169 printf("%sDebugDestroy\n", prefix);
172 void print_guid(unsigned char guid[static 16])
174 for (unsigned i = 0; i < 16; i++) {
175 printf("%02hhx", guid[i]);
183 void print_upkg_exports(struct upkg *pkg)
185 for (unsigned i = 0; i < pkg->export_count; i++) {
186 const struct upkg_export *export = upkg_get_export(pkg, i);
187 const char *package, *class;
189 class = upkg_export_class(pkg, i, &package);
194 printf("%u - %s (%s.%s)\n", i+1, export->name, package, class);
195 printf(" Flags: %lx\n", export->flags);
197 print_upkg_object_flags(" ", export->flags);
202 int package_info(struct upkg *pkg)
204 printf("Version: %u\n", pkg->version);
205 printf("License: %u\n", pkg->license);
207 print_guid(pkg->guid);
209 printf("Flags: %lx\n", pkg->flags);
211 print_upkg_flags(" ", pkg->flags);
214 printf("Names: %lu\n", pkg->name_count);
216 for (unsigned long i = 0; i < pkg->name_count; i++) {
217 printf("%lu - %s\n", i, upkg_get_name(pkg, i));
221 printf("Exports: %lu\n", pkg->export_count);
222 if (verbose >= 1) print_upkg_exports(pkg);
223 printf("Imports: %lu\n", pkg->import_count);
228 static void export_print_name(struct upkg *upkg, const struct upkg_export *e)
231 export_print_name(upkg, upkg_get_export(upkg, e->package-1));
232 printf(".%s", e->name);
237 export_print_fullname(GTypeModule *pkg, const struct upkg_export *export)
239 printf("%s", pkg->name);
240 export_print_name(U_PKG(pkg)->pkg, export);
243 static int package_list(GTypeModule *pkg, long parent)
245 struct upkg *upkg = U_PKG(pkg)->pkg;
246 const struct upkg_export *export;
248 for (unsigned i = 0; i < upkg->export_count; i++) {
249 export = upkg_get_export(upkg, i);
251 if (export->package != parent)
254 export_print_fullname(pkg, export);
257 const char *class, *package;
259 class = upkg_export_class(upkg, i, &package);
260 printf(" (%s.%s)", package, class);
269 static int object_info(GTypeModule *pkg, unsigned long idx)
271 struct upkg *upkg = U_PKG(pkg)->pkg;
272 const struct upkg_export *export;
273 const char *class, *package;
275 export = upkg_get_export(upkg, idx);
276 export_print_fullname(pkg, export);
278 class = upkg_export_class(upkg, idx, &package);
279 printf(" (%s.%s)\n", package, class);
281 /* Print out object properties. */
287 obj = u_object_new_from_package(upkg, idx);
289 fprintf(stderr, "%s: failed to load object.\n",
294 props = g_object_class_list_properties(G_OBJECT_GET_CLASS(obj), &n);
295 for (unsigned i = 0; i < n; i++) {
299 printf(" property %s: ", props[i]->name);
302 g_value_init(&val, props[i]->value_type);
303 g_object_get_property(obj, props[i]->name, &val);
304 valstr = g_strdup_value_contents(&val);
305 printf("%s\n", valstr);
312 /* Print raw file info. */
314 struct upkg_file *f = upkg_export_open(upkg, idx);
316 fprintf(stderr, "%s: failed to open export.\n",
321 printf(" file size: %lu\n", f->len);
322 printf(" file start: %#lx\n", f->base);
323 printf(" file end: %#lx\n", f->base + f->len);
325 upkg_export_close(f);
331 static int export(GTypeModule *pkg, GObject *obj, unsigned idx)
333 struct upkg *upkg = U_PKG(pkg)->pkg;
338 if (U_OBJECT_IS_LOADABLE(obj) && u_object_load(obj) != 0) {
339 fprintf(stderr, "%s: failed to load export data.\n", progname);
343 rc = u_object_export_name(obj, name, sizeof name);
345 /* XXX: We could use a default name here. */
346 fprintf(stderr, "%s: failed to determine export filename.\n",
351 printf("exporting ");
352 export_print_fullname(pkg, upkg_get_export(upkg, idx));
353 printf(" to %s\n", name);
355 of = fopen(name, "wb");
361 u_object_export(obj, of);
363 if (fclose(of) != 0) {
368 if (U_OBJECT_IS_LOADABLE(obj)) {
369 u_object_unload(obj);
375 static int object_export(GTypeModule *pkg, unsigned long idx)
377 struct upkg *upkg = U_PKG(pkg)->pkg;
381 obj = u_object_new_from_package(upkg, idx);
383 fprintf(stderr, "%s: failed to load object.\n", progname);
387 if (!U_OBJECT_IS_EXPORTABLE(obj)) {
388 fprintf(stderr, "%s: object is not exportable.\n", progname);
392 ret = export(pkg, obj, idx);
398 int package_export(GTypeModule *pkg)
400 struct upkg *upkg = U_PKG(pkg)->pkg;
401 const char *class, *package;
406 for (unsigned i = 0; i < upkg->export_count; i++) {
407 class = upkg_export_class(upkg, i, &package);
409 fprintf(stderr, "error getting class information.\n");
413 type = u_object_module_get_class(package, class);
417 obj = g_object_new(type, NULL);
418 if (U_OBJECT_IS_EXPORTABLE(obj)) {
419 if (u_object_deserialize(obj, upkg, i) != 0
420 || export(pkg, obj, i) != 0) {
430 static int process_object(int mode, const char *objname)
432 char work[strlen(objname)+1], *p = work, *c;
437 strcpy(work, objname);
439 /* First, find and open the actual package. */
444 if (!pkg || !g_type_module_use(pkg)) {
445 fprintf(stderr, "%s: %s: failed to open package.\n",
450 if (!U_PKG(pkg)->pkg) {
451 if (u_pkg_is_native(pkg)) {
452 fprintf(stderr, "%s: %s: not a UObject package.\n",
455 fprintf(stderr, "%s: %s: package not found.\n",
462 /* Resolve the hierarchical reference. */
468 current = upkg_export_find(U_PKG(pkg)->pkg, current, p);
470 /* We want to print the full name up to this point. */
471 strcpy(work, objname);
474 fprintf(stderr, "%s: %s: object not found.\n",
483 ret = package_info(U_PKG(pkg)->pkg);
485 ret = object_info(pkg, current);
488 ret = package_list(pkg, current+1);
492 ret = package_export(pkg);
494 ret = object_export(pkg, current);
501 g_type_module_unuse(pkg);
506 int main(int argc, char **argv)
508 const char *pkgname = NULL;
509 unsigned mode = MODE_INFO;
512 if (argc > 0) progname = argv[0];
514 if (u_pkg_vfs_init() != 0)
516 if (u_object_module_init() != 0)
519 while ((opt = getopt_long(argc, argv, sopts, lopts, NULL)) != -1) {
522 pkgname = u_pkg_vfs_add_local(NULL, optarg);
524 fprintf(stderr, "failed to add package `%s'.\n", optarg);
552 if (!pkgname && !argv[optind]) {
559 for (int i = optind; i < argc; i++) {
560 if (process_object(mode, argv[i]) != 0)
564 if (process_object(mode, pkgname) != 0)
568 u_object_module_exit();