2 * upkg: tool for manipulating Unreal Tournament packages.
3 * Copyright © 2009-2012, 2022 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 <https://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>
48 static const char *progname = "upkg";
51 static const char sopts[] = SOPT_STRING;
52 static const struct option lopts[] = { LOPTS_INITIALIZER, {0} };
54 static void print_version(void)
56 printf("%s\n", PACKAGE_STRING);
57 puts("Copyright (C) 2022 Nick Bowler.");
58 puts("License GPLv3+: GNU GPL version 3 or any later version.");
59 puts("This is free software: you are free to change and redistribute it.");
60 puts("There is NO WARRANTY, to the extent permitted by law.");
63 static void print_usage(FILE *f)
65 fprintf(f, "Usage: %s [options] [object ...]\n", progname);
67 fprintf(f, "Try %s --help for more information.\n", progname);
70 static void print_help(void)
72 const struct option *opt;
78 for (opt = lopts; opt->name; opt++) {
79 struct lopt_help help;
81 if (!lopt_get_help(opt, &help))
84 help_print_option(opt, help.arg, help.desc, 20);
88 puts("For more information, see the upkg(1) man page.");
91 printf("Report bugs to <%s>.\n", PACKAGE_BUGREPORT);
94 static void print_upkg_flags(const char *prefix, unsigned long flags)
96 if (flags & UPKG_FLAG_ALLOW_DOWNLOAD)
97 printf("%sAllowDownload\n", prefix);
98 if (flags & UPKG_FLAG_CLIENT_OPTIONAL)
99 printf("%sClientOptional\n", prefix);
100 if (flags & UPKG_FLAG_SERVER_ONLY)
101 printf("%sServerOnly\n", prefix);
102 if (flags & UPKG_FLAG_BROKEN_LINKS)
103 printf("%sBrokenLinks\n", prefix);
104 if (flags & UPKG_FLAG_INSECURE)
105 printf("%sInsecure\n", prefix);
106 if (flags & UPKG_FLAG_REQUIRED)
107 printf("%sRequired\n", prefix);
110 static void print_upkg_object_flags(const char *prefix, unsigned long flags)
112 if (flags & UPKG_OBJ_FLAG_TRANSACTIONAL)
113 printf("%sTransactional\n", prefix);
114 if (flags & UPKG_OBJ_FLAG_UNREACHABLE)
115 printf("%sUnreachable\n", prefix);
116 if (flags & UPKG_OBJ_FLAG_PUBLIC)
117 printf("%sPublic\n", prefix);
118 if (flags & UPKG_OBJ_FLAG_TAG_IMPORT)
119 printf("%sImport\n", prefix);
120 if (flags & UPKG_OBJ_FLAG_TAG_EXPORT)
121 printf("%sTagExport\n", prefix);
122 if (flags & UPKG_OBJ_FLAG_SOURCE_MODIFIED)
123 printf("%sSourceModified\n", prefix);
124 if (flags & UPKG_OBJ_FLAG_TAG_GARBAGE)
125 printf("%sTagGarbage\n", prefix);
126 if (flags & UPKG_OBJ_FLAG_NEED_LOAD)
127 printf("%sNeedLoad\n", prefix);
128 if (flags & UPKG_OBJ_FLAG_HIGHLIGHT_NAME)
129 printf("%sHighlightName\n", prefix);
130 if (flags & UPKG_OBJ_FLAG_IN_SINGULAR_FUNC)
131 printf("%sInSingularFunc\n", prefix);
132 if (flags & UPKG_OBJ_FLAG_SUPPRESSED)
133 printf("%sSuppressed\n", prefix);
134 if (flags & UPKG_OBJ_FLAG_IN_END_STATE)
135 printf("%sInEndState\n", prefix);
136 if (flags & UPKG_OBJ_FLAG_TRANSIENT)
137 printf("%sTransient\n", prefix);
138 if (flags & UPKG_OBJ_FLAG_PRELOADING)
139 printf("%sPreloading\n", prefix);
140 if (flags & UPKG_OBJ_FLAG_LOAD_FOR_CLIENT)
141 printf("%sLoadForClient\n", prefix);
142 if (flags & UPKG_OBJ_FLAG_LOAD_FOR_SERVER)
143 printf("%sLoadForServer\n", prefix);
144 if (flags & UPKG_OBJ_FLAG_LOAD_FOR_EDIT)
145 printf("%sLoadForEdit\n", prefix);
146 if (flags & UPKG_OBJ_FLAG_STANDALONE)
147 printf("%sStandalone\n", prefix);
148 if (flags & UPKG_OBJ_FLAG_NOT_FOR_CLIENT)
149 printf("%sNotForClient\n", prefix);
150 if (flags & UPKG_OBJ_FLAG_NOT_FOR_SERVER)
151 printf("%sNotForServer\n", prefix);
152 if (flags & UPKG_OBJ_FLAG_NOT_FOR_EDIT)
153 printf("%sNotForEdit\n", prefix);
154 if (flags & UPKG_OBJ_FLAG_DESTROYED)
155 printf("%sDestroyed\n", prefix);
156 if (flags & UPKG_OBJ_FLAG_NEED_POST_LOAD)
157 printf("%sNeedPostLoad\n", prefix);
158 if (flags & UPKG_OBJ_FLAG_HAS_STACK)
159 printf("%sHasStack\n", prefix);
160 if (flags & UPKG_OBJ_FLAG_NATIVE)
161 printf("%sNative\n", prefix);
162 if (flags & UPKG_OBJ_FLAG_MARKED)
163 printf("%sMarked\n", prefix);
164 if (flags & UPKG_OBJ_FLAG_ERROR_SHUTDOWN)
165 printf("%sErrorShutdown\n", prefix);
166 if (flags & UPKG_OBJ_FLAG_DEBUG_POST_LOAD)
167 printf("%sDebugPostLoad\n", prefix);
168 if (flags & UPKG_OBJ_FLAG_DEBUG_SERIALIZE)
169 printf("%sDebugSerialize\n", prefix);
170 if (flags & UPKG_OBJ_FLAG_DEBUG_DESTROY)
171 printf("%sDebugDestroy\n", prefix);
174 static void print_guid(unsigned char *guid)
176 for (unsigned i = 0; i < 16; i++) {
177 printf("%02hhx", guid[i]);
185 static void export_print_name(const struct upkg_export *e)
188 export_print_name(e->parent);
189 printf(".%s", e->name);
194 export_print_fullname(GTypeModule *pkg, const struct upkg_export *export)
196 printf("%s", pkg->name);
197 export_print_name(export);
200 static void import_print_fullname(const struct upkg_import *import)
203 import_print_fullname(import->parent);
206 printf("%s", import->name);
211 export_print_class(GTypeModule *pkg, const struct upkg_export *export)
213 struct upkg *upkg = U_PKG(pkg)->pkg;
215 if (export->class < 0) {
216 const struct upkg_import *class;
218 class = upkg_get_import(upkg, -(export->class+1));
219 import_print_fullname(class);
220 } else if (export->class > 0) {
221 const struct upkg_export *class;
223 class = upkg_get_export(upkg, export->class-1);
224 export_print_fullname(pkg, class);
226 printf("Core.Class");
230 void print_upkg_exports(GTypeModule *pkg)
232 struct upkg *upkg = U_PKG(pkg)->pkg;
234 for (unsigned i = 0; i < upkg->export_count; i++) {
235 const struct upkg_export *export = upkg_get_export(upkg, i);
237 printf("%u - ", i+1);
238 export_print_fullname(pkg, export);
240 export_print_class(pkg, export);
243 printf(" Flags: %lx\n", export->flags);
245 print_upkg_object_flags(" ", export->flags);
250 void print_upkg_imports(struct upkg *pkg)
252 for (unsigned i = 0; i < pkg->import_count; i++) {
253 const struct upkg_import *import = upkg_get_import(pkg, i);
256 import_print_fullname(import);
257 printf(" (%s.%s)\n", import->class_package, import->class_name);
261 int package_info(GTypeModule *pkg)
263 struct upkg *upkg = U_PKG(pkg)->pkg;
265 printf("Version: %u\n", upkg->version);
266 printf("License: %u\n", upkg->license);
268 print_guid(upkg->guid);
270 printf("Flags: %lx\n", upkg->flags);
272 print_upkg_flags(" ", upkg->flags);
275 printf("Names: %lu\n", upkg->name_count);
277 for (unsigned long i = 0; i < upkg->name_count; i++) {
278 printf("%lu - %s\n", i, upkg_get_name(upkg, i));
282 printf("Exports: %lu\n", upkg->export_count);
283 if (verbose >= 1) print_upkg_exports(pkg);
284 printf("Imports: %lu\n", upkg->import_count);
285 if (verbose >= 1) print_upkg_imports(upkg);
290 static int package_list(GTypeModule *pkg, long current)
292 struct upkg *upkg = U_PKG(pkg)->pkg;
293 const struct upkg_export *parent = NULL;
296 parent = upkg_get_export(upkg, current);
297 assert(parent != NULL);
300 for (unsigned i = 0; i < upkg->export_count; i++) {
301 const struct upkg_export *export = upkg_get_export(upkg, i);
303 if (export->parent != parent)
306 export_print_fullname(pkg, export);
310 export_print_class(pkg, export);
320 static int object_dump_properties(GTypeModule *pkg, unsigned long idx)
326 obj = u_object_new_from_package(pkg, idx);
328 fprintf(stderr, "%s: failed to load object.\n", progname);
332 props = g_object_class_list_properties(G_OBJECT_GET_CLASS(obj), &n);
333 for (unsigned i = 0; i < n; i++) {
337 printf(" property %s:", props[i]->name);
340 g_value_init(&val, props[i]->value_type);
341 g_object_get_property(obj, props[i]->name, &val);
343 if (G_VALUE_HOLDS(&val, U_TYPE_OBJECT)) {
344 UObject *obj = g_value_get_object(&val);
346 if (obj && obj->pkg_name) {
347 printf(" %s", obj->pkg_name);
351 valstr = g_strdup_value_contents(&val);
352 printf(" %s\n", valstr);
362 static int object_dump_offsets(GTypeModule *pkg, unsigned long idx)
366 f = upkg_export_open(U_PKG(pkg)->pkg, idx);
368 fprintf(stderr, "%s: failed to open export.\n", progname);
372 printf(" file size: %lu\n", f->len);
373 printf(" file start: %#lx\n", f->base);
374 printf(" file end: %#lx\n", f->base + f->len);
376 upkg_export_close(f);
381 static int object_info(GTypeModule *pkg, unsigned long idx)
383 struct upkg *upkg = U_PKG(pkg)->pkg;
384 const struct upkg_export *export;
387 export = upkg_get_export(upkg, idx);
388 export_print_fullname(pkg, export);
391 export_print_class(pkg, export);
394 if (verbose >= 1 && object_dump_properties(pkg, idx) != 0)
397 if (verbose >= 3 && object_dump_offsets(pkg, idx))
403 static int export(GTypeModule *pkg, GObject *obj, unsigned idx)
405 struct upkg *upkg = U_PKG(pkg)->pkg;
410 if (U_OBJECT_IS_LOADABLE(obj) && u_object_load(obj) != 0) {
411 fprintf(stderr, "%s: failed to load export data.\n", progname);
415 rc = u_object_export_name(obj, name, sizeof name);
417 /* XXX: We could use a default name here. */
418 fprintf(stderr, "%s: failed to determine export filename.\n",
423 printf("exporting ");
424 export_print_fullname(pkg, upkg_get_export(upkg, idx));
425 printf(" to %s\n", name);
427 of = fopen(name, "wb");
433 u_object_export(obj, of);
435 if (fclose(of) != 0) {
440 if (U_OBJECT_IS_LOADABLE(obj)) {
441 u_object_unload(obj);
447 static int object_export(GTypeModule *pkg, unsigned long idx)
452 obj = u_object_new_from_package(pkg, idx);
454 fprintf(stderr, "%s: failed to load object.\n", progname);
458 if (!U_OBJECT_IS_EXPORTABLE(obj)) {
459 fprintf(stderr, "%s: object is not exportable.\n", progname);
463 ret = export(pkg, obj, idx);
469 int package_export(GTypeModule *pkg)
471 struct upkg *upkg = U_PKG(pkg)->pkg;
475 for (unsigned i = 0; i < upkg->export_count; i++) {
476 obj = u_object_new_from_package(pkg, i);
477 if (U_OBJECT_IS_EXPORTABLE(obj) && export(pkg, obj, i) != 0) {
486 static int process_object(int mode, const char *objname)
488 char work[strlen(objname)+1], *p = work, *c;
493 strcpy(work, objname);
495 /* First, find and open the actual package. */
500 if (!pkg || !g_type_module_use(pkg)) {
501 fprintf(stderr, "%s: %s: failed to open package.\n",
506 if (!U_PKG(pkg)->pkg) {
507 if (u_pkg_is_native(pkg)) {
508 fprintf(stderr, "%s: %s: not a UObject package.\n",
511 fprintf(stderr, "%s: %s: package not found.\n",
518 /* Resolve the hierarchical reference. */
524 current = upkg_export_find(U_PKG(pkg)->pkg, current, p);
526 /* We want to print the full name up to this point. */
527 strcpy(work, objname);
530 fprintf(stderr, "%s: %s: object not found.\n",
539 ret = package_info(pkg);
541 ret = object_info(pkg, current);
544 ret = package_list(pkg, current);
548 ret = package_export(pkg);
550 ret = object_export(pkg, current);
557 g_type_module_unuse(pkg);
562 int main(int argc, char **argv)
564 const char *pkgname = NULL;
565 unsigned mode = MODE_INFO;
568 if (argc > 0) progname = argv[0];
570 if (u_pkg_vfs_init() != 0)
572 if (u_object_module_init() != 0)
575 while ((opt = getopt_long(argc, argv, sopts, lopts, NULL)) != -1) {
578 pkgname = u_pkg_vfs_add_local(NULL, optarg);
580 fprintf(stderr, "failed to add package `%s'.\n", optarg);
608 if (!pkgname && !argv[optind]) {
615 for (int i = optind; i < argc; i++) {
616 if (process_object(mode, argv[i]) != 0)
620 if (process_object(mode, pkgname) != 0)
624 u_object_module_exit();