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/>.
27 #include <glib-object.h>
30 #include <uobject/uobject.h>
31 #include <uobject/exportable.h>
32 #include <uobject/loadable.h>
33 #include <uobject/module.h>
34 #include <uobject/package.h>
35 #include <uobject/vfs.h>
46 static const char *progname = "upkg";
47 static const char *sopts = "ilxvf:VH";
48 static const struct option lopts[] = {
49 { "info", 0, NULL, 'i' },
50 { "list", 0, NULL, 'l' },
51 { "export", 0, NULL, 'x' },
52 { "file", 1, NULL, 'f' },
53 { "verbose", 0, NULL, 'v' },
54 { "version", 0, NULL, 'V' },
55 { "help", 0, NULL, 'H' },
59 void print_version(void)
61 printf("%s\n", PACKAGE_STRING);
63 Copyright (C) 2009 Nick Bowler.\n\
64 License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.\n\
65 This is free software: you are free to change and redistribute it.\n\
66 There is NO WARRANTY, to the extent permitted by law."
70 void print_usage(FILE *f)
72 fprintf(f, "Usage: %s [options] [object ...]\n", progname);
78 puts("Detailed help coming soon. Until then, I'll just list my options.");
79 for (unsigned i = 0; lopts[i].name; i++) {
80 const struct option *o = &lopts[i];
81 printf("\t--%s", o->name);
83 if (o->has_arg == 1) {
85 } else if (o->has_arg == 2) {
89 printf(" (-%c)\n", o->val);
93 static void print_upkg_flags(const char *prefix, unsigned long flags)
95 if (flags & UPKG_FLAG_ALLOW_DOWNLOAD)
96 printf("%sAllowDownload\n", prefix);
97 if (flags & UPKG_FLAG_CLIENT_OPTIONAL)
98 printf("%sClientOptional\n", prefix);
99 if (flags & UPKG_FLAG_SERVER_ONLY)
100 printf("%sServerOnly\n", prefix);
101 if (flags & UPKG_FLAG_BROKEN_LINKS)
102 printf("%sBrokenLinks\n", prefix);
103 if (flags & UPKG_FLAG_INSECURE)
104 printf("%sInsecure\n", prefix);
105 if (flags & UPKG_FLAG_REQUIRED)
106 printf("%sRequired\n", prefix);
109 static void print_upkg_object_flags(const char *prefix, unsigned long flags)
111 if (flags & UPKG_OBJ_FLAG_TRANSACTIONAL)
112 printf("%sTransactional\n", prefix);
113 if (flags & UPKG_OBJ_FLAG_UNREACHABLE)
114 printf("%sUnreachable\n", prefix);
115 if (flags & UPKG_OBJ_FLAG_PUBLIC)
116 printf("%sPublic\n", prefix);
117 if (flags & UPKG_OBJ_FLAG_TAG_IMPORT)
118 printf("%sImport\n", prefix);
119 if (flags & UPKG_OBJ_FLAG_TAG_EXPORT)
120 printf("%sTagExport\n", prefix);
121 if (flags & UPKG_OBJ_FLAG_SOURCE_MODIFIED)
122 printf("%sSourceModified\n", prefix);
123 if (flags & UPKG_OBJ_FLAG_TAG_GARBAGE)
124 printf("%sTagGarbage\n", prefix);
125 if (flags & UPKG_OBJ_FLAG_NEED_LOAD)
126 printf("%sNeedLoad\n", prefix);
127 if (flags & UPKG_OBJ_FLAG_HIGHLIGHT_NAME)
128 printf("%sHighlightName\n", prefix);
129 if (flags & UPKG_OBJ_FLAG_IN_SINGULAR_FUNC)
130 printf("%sInSingularFunc\n", prefix);
131 if (flags & UPKG_OBJ_FLAG_SUPPRESSED)
132 printf("%sSuppressed\n", prefix);
133 if (flags & UPKG_OBJ_FLAG_IN_END_STATE)
134 printf("%sInEndState\n", prefix);
135 if (flags & UPKG_OBJ_FLAG_TRANSIENT)
136 printf("%sTransient\n", prefix);
137 if (flags & UPKG_OBJ_FLAG_PRELOADING)
138 printf("%sPreloading\n", prefix);
139 if (flags & UPKG_OBJ_FLAG_LOAD_FOR_CLIENT)
140 printf("%sLoadForClient\n", prefix);
141 if (flags & UPKG_OBJ_FLAG_LOAD_FOR_SERVER)
142 printf("%sLoadForServer\n", prefix);
143 if (flags & UPKG_OBJ_FLAG_LOAD_FOR_EDIT)
144 printf("%sLoadForEdit\n", prefix);
145 if (flags & UPKG_OBJ_FLAG_STANDALONE)
146 printf("%sStandalone\n", prefix);
147 if (flags & UPKG_OBJ_FLAG_NOT_FOR_CLIENT)
148 printf("%sNotForClient\n", prefix);
149 if (flags & UPKG_OBJ_FLAG_NOT_FOR_SERVER)
150 printf("%sNotForServer\n", prefix);
151 if (flags & UPKG_OBJ_FLAG_NOT_FOR_EDIT)
152 printf("%sNotForEdit\n", prefix);
153 if (flags & UPKG_OBJ_FLAG_DESTROYED)
154 printf("%sDestroyed\n", prefix);
155 if (flags & UPKG_OBJ_FLAG_NEED_POST_LOAD)
156 printf("%sNeedPostLoad\n", prefix);
157 if (flags & UPKG_OBJ_FLAG_HAS_STACK)
158 printf("%sHasStack\n", prefix);
159 if (flags & UPKG_OBJ_FLAG_NATIVE)
160 printf("%sNative\n", prefix);
161 if (flags & UPKG_OBJ_FLAG_MARKED)
162 printf("%sMarked\n", prefix);
163 if (flags & UPKG_OBJ_FLAG_ERROR_SHUTDOWN)
164 printf("%sErrorShutdown\n", prefix);
165 if (flags & UPKG_OBJ_FLAG_DEBUG_POST_LOAD)
166 printf("%sDebugPostLoad\n", prefix);
167 if (flags & UPKG_OBJ_FLAG_DEBUG_SERIALIZE)
168 printf("%sDebugSerialize\n", prefix);
169 if (flags & UPKG_OBJ_FLAG_DEBUG_DESTROY)
170 printf("%sDebugDestroy\n", prefix);
173 void print_guid(unsigned char guid[static 16])
175 for (unsigned i = 0; i < 16; i++) {
176 printf("%02hhx", guid[i]);
184 static void export_print_name(const struct upkg_export *e)
187 export_print_name(e->parent);
188 printf(".%s", e->name);
193 export_print_fullname(GTypeModule *pkg, const struct upkg_export *export)
195 printf("%s", pkg->name);
196 export_print_name(export);
199 static void import_print_fullname(const struct upkg_import *import)
202 import_print_fullname(import->parent);
205 printf("%s", import->name);
210 export_print_class(GTypeModule *pkg, const struct upkg_export *export)
212 struct upkg *upkg = U_PKG(pkg)->pkg;
214 if (export->class < 0) {
215 const struct upkg_import *class;
217 class = upkg_get_import(upkg, -(export->class+1));
218 import_print_fullname(class);
219 } else if (export->class > 0) {
220 const struct upkg_export *class;
222 class = upkg_get_export(upkg, export->class-1);
223 export_print_fullname(pkg, class);
225 printf("Core.Class");
229 void print_upkg_exports(GTypeModule *pkg)
231 struct upkg *upkg = U_PKG(pkg)->pkg;
233 for (unsigned i = 0; i < upkg->export_count; i++) {
234 const struct upkg_export *export = upkg_get_export(upkg, i);
236 printf("%u - ", i+1);
237 export_print_fullname(pkg, export);
239 export_print_class(pkg, export);
242 printf(" Flags: %lx\n", export->flags);
244 print_upkg_object_flags(" ", export->flags);
249 void print_upkg_imports(struct upkg *pkg)
251 for (unsigned i = 0; i < pkg->import_count; i++) {
252 const struct upkg_import *import = upkg_get_import(pkg, i);
255 import_print_fullname(import);
256 printf(" (%s.%s)\n", import->class_package, import->class_name);
260 int package_info(GTypeModule *pkg)
262 struct upkg *upkg = U_PKG(pkg)->pkg;
264 printf("Version: %u\n", upkg->version);
265 printf("License: %u\n", upkg->license);
267 print_guid(upkg->guid);
269 printf("Flags: %lx\n", upkg->flags);
271 print_upkg_flags(" ", upkg->flags);
274 printf("Names: %lu\n", upkg->name_count);
276 for (unsigned long i = 0; i < upkg->name_count; i++) {
277 printf("%lu - %s\n", i, upkg_get_name(upkg, i));
281 printf("Exports: %lu\n", upkg->export_count);
282 if (verbose >= 1) print_upkg_exports(pkg);
283 printf("Imports: %lu\n", upkg->import_count);
284 if (verbose >= 1) print_upkg_imports(upkg);
289 static int package_list(GTypeModule *pkg, long current)
291 struct upkg *upkg = U_PKG(pkg)->pkg;
292 const struct upkg_export *parent = NULL;
295 parent = upkg_get_export(upkg, current);
296 assert(parent != NULL);
299 for (unsigned i = 0; i < upkg->export_count; i++) {
300 const struct upkg_export *export = upkg_get_export(upkg, i);
302 if (export->parent != parent)
305 export_print_fullname(pkg, export);
309 export_print_class(pkg, export);
319 static int object_dump_properties(GTypeModule *pkg, unsigned long idx)
325 obj = u_object_new_from_package(pkg, idx);
327 fprintf(stderr, "%s: failed to load object.\n", progname);
331 props = g_object_class_list_properties(G_OBJECT_GET_CLASS(obj), &n);
332 for (unsigned i = 0; i < n; i++) {
336 printf(" property %s:", props[i]->name);
339 g_value_init(&val, props[i]->value_type);
340 g_object_get_property(obj, props[i]->name, &val);
342 if (G_VALUE_HOLDS(&val, U_TYPE_OBJECT)) {
343 UObject *obj = g_value_get_object(&val);
345 if (obj && obj->pkg_name) {
346 printf(" %s", obj->pkg_name);
350 valstr = g_strdup_value_contents(&val);
351 printf(" %s\n", valstr);
361 static int object_dump_offsets(GTypeModule *pkg, unsigned long idx)
365 f = upkg_export_open(U_PKG(pkg)->pkg, idx);
367 fprintf(stderr, "%s: failed to open export.\n", progname);
371 printf(" file size: %lu\n", f->len);
372 printf(" file start: %#lx\n", f->base);
373 printf(" file end: %#lx\n", f->base + f->len);
375 upkg_export_close(f);
380 static int object_info(GTypeModule *pkg, unsigned long idx)
382 struct upkg *upkg = U_PKG(pkg)->pkg;
383 const struct upkg_export *export;
386 export = upkg_get_export(upkg, idx);
387 export_print_fullname(pkg, export);
390 export_print_class(pkg, export);
393 if (verbose >= 1 && object_dump_properties(pkg, idx) != 0)
396 if (verbose >= 3 && object_dump_offsets(pkg, idx))
402 static int export(GTypeModule *pkg, GObject *obj, unsigned idx)
404 struct upkg *upkg = U_PKG(pkg)->pkg;
409 if (U_OBJECT_IS_LOADABLE(obj) && u_object_load(obj) != 0) {
410 fprintf(stderr, "%s: failed to load export data.\n", progname);
414 rc = u_object_export_name(obj, name, sizeof name);
416 /* XXX: We could use a default name here. */
417 fprintf(stderr, "%s: failed to determine export filename.\n",
422 printf("exporting ");
423 export_print_fullname(pkg, upkg_get_export(upkg, idx));
424 printf(" to %s\n", name);
426 of = fopen(name, "wb");
432 u_object_export(obj, of);
434 if (fclose(of) != 0) {
439 if (U_OBJECT_IS_LOADABLE(obj)) {
440 u_object_unload(obj);
446 static int object_export(GTypeModule *pkg, unsigned long idx)
451 obj = u_object_new_from_package(pkg, idx);
453 fprintf(stderr, "%s: failed to load object.\n", progname);
457 if (!U_OBJECT_IS_EXPORTABLE(obj)) {
458 fprintf(stderr, "%s: object is not exportable.\n", progname);
462 ret = export(pkg, obj, idx);
468 int package_export(GTypeModule *pkg)
470 struct upkg *upkg = U_PKG(pkg)->pkg;
474 for (unsigned i = 0; i < upkg->export_count; i++) {
475 obj = u_object_new_from_package(pkg, i);
476 if (U_OBJECT_IS_EXPORTABLE(obj) && export(pkg, obj, i) != 0) {
485 static int process_object(int mode, const char *objname)
487 char work[strlen(objname)+1], *p = work, *c;
492 strcpy(work, objname);
494 /* First, find and open the actual package. */
499 if (!pkg || !g_type_module_use(pkg)) {
500 fprintf(stderr, "%s: %s: failed to open package.\n",
505 if (!U_PKG(pkg)->pkg) {
506 if (u_pkg_is_native(pkg)) {
507 fprintf(stderr, "%s: %s: not a UObject package.\n",
510 fprintf(stderr, "%s: %s: package not found.\n",
517 /* Resolve the hierarchical reference. */
523 current = upkg_export_find(U_PKG(pkg)->pkg, current, p);
525 /* We want to print the full name up to this point. */
526 strcpy(work, objname);
529 fprintf(stderr, "%s: %s: object not found.\n",
538 ret = package_info(pkg);
540 ret = object_info(pkg, current);
543 ret = package_list(pkg, current);
547 ret = package_export(pkg);
549 ret = object_export(pkg, current);
556 g_type_module_unuse(pkg);
561 int main(int argc, char **argv)
563 const char *pkgname = NULL;
564 unsigned mode = MODE_INFO;
567 if (argc > 0) progname = argv[0];
569 if (u_pkg_vfs_init() != 0)
571 if (u_object_module_init() != 0)
574 while ((opt = getopt_long(argc, argv, sopts, lopts, NULL)) != -1) {
577 pkgname = u_pkg_vfs_add_local(NULL, optarg);
579 fprintf(stderr, "failed to add package `%s'.\n", optarg);
607 if (!pkgname && !argv[optind]) {
614 for (int i = optind; i < argc; i++) {
615 if (process_object(mode, argv[i]) != 0)
619 if (process_object(mode, pkgname) != 0)
623 u_object_module_exit();