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 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>
44 static const char *progname = "upkg";
45 static const char *sopts = "ixvf:VH";
46 static const struct option lopts[] = {
47 { "info", 0, NULL, 'i' },
48 { "export", 0, NULL, 'x' },
49 { "file", 1, NULL, 'f' },
50 { "verbose", 0, NULL, 'v' },
51 { "version", 0, NULL, 'V' },
52 { "help", 0, NULL, 'H' },
56 void print_version(void)
58 printf("%s\n", PACKAGE_STRING);
60 Copyright (C) 2009 Nick Bowler.\n\
61 License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.\n\
62 This is free software: you are free to change and redistribute it.\n\
63 There is NO WARRANTY, to the extent permitted by law."
67 void print_usage(FILE *f)
69 fprintf(f, "Usage: %s [options] [object ...]\n", progname);
75 puts("Detailed help coming soon. Until then, I'll just list my options.");
76 for (unsigned i = 0; lopts[i].name; i++) {
77 const struct option *o = &lopts[i];
78 printf("\t--%s", o->name);
80 if (o->has_arg == 1) {
82 } else if (o->has_arg == 2) {
86 printf(" (-%c)\n", o->val);
90 static void print_upkg_flags(const char *prefix, unsigned long flags)
92 if (flags & UPKG_FLAG_ALLOW_DOWNLOAD)
93 printf("%sAllowDownload\n", prefix);
94 if (flags & UPKG_FLAG_CLIENT_OPTIONAL)
95 printf("%sClientOptional\n", prefix);
96 if (flags & UPKG_FLAG_SERVER_ONLY)
97 printf("%sServerOnly\n", prefix);
98 if (flags & UPKG_FLAG_BROKEN_LINKS)
99 printf("%sBrokenLinks\n", prefix);
100 if (flags & UPKG_FLAG_INSECURE)
101 printf("%sInsecure\n", prefix);
102 if (flags & UPKG_FLAG_REQUIRED)
103 printf("%sRequired\n", prefix);
106 static void print_upkg_object_flags(const char *prefix, unsigned long flags)
108 if (flags & UPKG_OBJ_FLAG_TRANSACTIONAL)
109 printf("%sTransactional\n", prefix);
110 if (flags & UPKG_OBJ_FLAG_UNREACHABLE)
111 printf("%sUnreachable\n", prefix);
112 if (flags & UPKG_OBJ_FLAG_PUBLIC)
113 printf("%sPublic\n", prefix);
114 if (flags & UPKG_OBJ_FLAG_TAG_IMPORT)
115 printf("%sImport\n", prefix);
116 if (flags & UPKG_OBJ_FLAG_TAG_EXPORT)
117 printf("%sTagExport\n", prefix);
118 if (flags & UPKG_OBJ_FLAG_SOURCE_MODIFIED)
119 printf("%sSourceModified\n", prefix);
120 if (flags & UPKG_OBJ_FLAG_TAG_GARBAGE)
121 printf("%sTagGarbage\n", prefix);
122 if (flags & UPKG_OBJ_FLAG_NEED_LOAD)
123 printf("%sNeedLoad\n", prefix);
124 if (flags & UPKG_OBJ_FLAG_HIGHLIGHT_NAME)
125 printf("%sHighlightName\n", prefix);
126 if (flags & UPKG_OBJ_FLAG_IN_SINGULAR_FUNC)
127 printf("%sInSingularFunc\n", prefix);
128 if (flags & UPKG_OBJ_FLAG_SUPPRESSED)
129 printf("%sSuppressed\n", prefix);
130 if (flags & UPKG_OBJ_FLAG_IN_END_STATE)
131 printf("%sInEndState\n", prefix);
132 if (flags & UPKG_OBJ_FLAG_TRANSIENT)
133 printf("%sTransient\n", prefix);
134 if (flags & UPKG_OBJ_FLAG_PRELOADING)
135 printf("%sPreloading\n", prefix);
136 if (flags & UPKG_OBJ_FLAG_LOAD_FOR_CLIENT)
137 printf("%sLoadForClient\n", prefix);
138 if (flags & UPKG_OBJ_FLAG_LOAD_FOR_SERVER)
139 printf("%sLoadForServer\n", prefix);
140 if (flags & UPKG_OBJ_FLAG_LOAD_FOR_EDIT)
141 printf("%sLoadForEdit\n", prefix);
142 if (flags & UPKG_OBJ_FLAG_STANDALONE)
143 printf("%sStandalone\n", prefix);
144 if (flags & UPKG_OBJ_FLAG_NOT_FOR_CLIENT)
145 printf("%sNotForClient\n", prefix);
146 if (flags & UPKG_OBJ_FLAG_NOT_FOR_SERVER)
147 printf("%sNotForServer\n", prefix);
148 if (flags & UPKG_OBJ_FLAG_NOT_FOR_EDIT)
149 printf("%sNotForEdit\n", prefix);
150 if (flags & UPKG_OBJ_FLAG_DESTROYED)
151 printf("%sDestroyed\n", prefix);
152 if (flags & UPKG_OBJ_FLAG_NEED_POST_LOAD)
153 printf("%sNeedPostLoad\n", prefix);
154 if (flags & UPKG_OBJ_FLAG_HAS_STACK)
155 printf("%sHasStack\n", prefix);
156 if (flags & UPKG_OBJ_FLAG_NATIVE)
157 printf("%sNative\n", prefix);
158 if (flags & UPKG_OBJ_FLAG_MARKED)
159 printf("%sMarked\n", prefix);
160 if (flags & UPKG_OBJ_FLAG_ERROR_SHUTDOWN)
161 printf("%sErrorShutdown\n", prefix);
162 if (flags & UPKG_OBJ_FLAG_DEBUG_POST_LOAD)
163 printf("%sDebugPostLoad\n", prefix);
164 if (flags & UPKG_OBJ_FLAG_DEBUG_SERIALIZE)
165 printf("%sDebugSerialize\n", prefix);
166 if (flags & UPKG_OBJ_FLAG_DEBUG_DESTROY)
167 printf("%sDebugDestroy\n", prefix);
170 void print_guid(unsigned char guid[static 16])
172 for (unsigned i = 0; i < 16; i++) {
173 printf("%02hhx", guid[i]);
181 void print_upkg_exports(struct upkg *pkg)
183 for (unsigned i = 0; i < pkg->export_count; i++) {
184 const struct upkg_export *export = upkg_get_export(pkg, i);
185 const char *package, *class;
187 class = upkg_export_class(pkg, i, &package);
192 printf("%u - %s (%s.%s)\n", i+1, export->name, package, class);
193 printf(" Flags: %lx\n", export->flags);
195 print_upkg_object_flags(" ", export->flags);
200 int package_info(struct upkg *pkg)
202 printf("Version: %u\n", pkg->version);
203 printf("License: %u\n", pkg->license);
205 print_guid(pkg->guid);
207 printf("Flags: %lx\n", pkg->flags);
209 print_upkg_flags(" ", pkg->flags);
212 printf("Names: %lu\n", pkg->name_count);
214 for (unsigned long i = 0; i < pkg->name_count; i++) {
215 printf("\t%s\n", upkg_get_name(pkg, i));
219 printf("Exports: %lu\n", pkg->export_count);
220 if (verbose >= 1) print_upkg_exports(pkg);
221 printf("Imports: %lu\n", pkg->import_count);
226 static void export_print_name(struct upkg *upkg, const struct upkg_export *e)
229 export_print_name(upkg, upkg_get_export(upkg, e->package-1));
230 printf(".%s", e->name);
234 static int export_info(GTypeModule *pkg, unsigned long idx)
236 struct upkg *upkg = U_PKG(pkg)->pkg;
237 const struct upkg_export *export;
239 export = upkg_get_export(upkg, idx);
241 printf("%s", pkg->name);
242 export_print_name(upkg, export);
248 static int export(struct upkg *pkg, GObject *obj, unsigned idx)
250 const struct upkg_export *export = upkg_get_export(pkg, idx);
254 if (u_object_deserialize(obj, pkg, idx) != 0) {
258 if (U_OBJECT_IS_LOADABLE(obj) && u_object_load(obj) != 0) {
262 u_object_export_name(obj, name, sizeof name);
264 printf("exporting %s to %s\n", export->name, name);
265 of = fopen(name, "wb");
271 u_object_export(obj, of);
273 if (fclose(of) != 0) {
278 if (U_OBJECT_IS_LOADABLE(obj)) {
279 u_object_unload(obj);
285 int package_export(struct upkg *pkg)
287 const char *class, *package;
292 for (unsigned i = 0; i < pkg->export_count; i++) {
293 class = upkg_export_class(pkg, i, &package);
295 fprintf(stderr, "error getting class information.\n");
299 type = u_object_module_get_class(package, class);
302 obj = g_object_new(type, NULL);
303 if (U_OBJECT_IS_EXPORTABLE(obj)) {
304 if (export(pkg, obj, i) != 0) {
314 static int process_object(int mode, const char *objname)
316 char work[strlen(objname)+1], *p = work, *c;
321 strcpy(work, objname);
323 /* First, find and open the actual package. */
328 if (!pkg || !g_type_module_use(pkg)) {
329 fprintf(stderr, "%s: %s: failed to open package.\n",
334 if (!U_PKG(pkg)->pkg) {
335 if (u_pkg_is_native(pkg)) {
336 fprintf(stderr, "%s: %s: not a UObject package.\n",
339 fprintf(stderr, "%s: %s: package not found.\n",
346 /* Resolve the hierarchical reference. */
352 current = upkg_export_find(U_PKG(pkg)->pkg, current, p);
354 /* We want to print the full name up to this point. */
355 strcpy(work, objname);
358 fprintf(stderr, "%s: %s: object not found.\n",
367 ret = package_info(U_PKG(pkg)->pkg);
369 ret = export_info(pkg, current);
372 ret = package_export(U_PKG(pkg)->pkg);
379 g_type_module_unuse(pkg);
384 int main(int argc, char **argv)
386 const char *pkgname = NULL;
387 unsigned mode = MODE_INFO;
390 if (argc > 0) progname = argv[0];
392 if (u_pkg_vfs_init() != 0)
394 if (u_object_module_init() != 0)
397 while ((opt = getopt_long(argc, argv, sopts, lopts, NULL)) != -1) {
400 pkgname = u_pkg_vfs_add_local(NULL, optarg);
402 fprintf(stderr, "failed to add package `%s'.\n", optarg);
427 if (!pkgname && !argv[optind]) {
434 for (int i = optind; i < argc; i++) {
435 if (process_object(mode, argv[i]) != 0)
439 if (process_object(mode, pkgname) != 0)
443 u_object_module_exit();