]> git.draconx.ca Git - upkg.git/blobdiff - src/upkg.c
upkg: Add some error checking around upkg_export_get_class.
[upkg.git] / src / upkg.c
index 02d8e5737e1a2c2ceab18c9d12425341b3cb940c..50f5ddab8491cab0787dd53f12025b2dbd8008e6 100644 (file)
@@ -1,10 +1,10 @@
 /*
  *  upkg: tool for manipulating Unreal Tournament packages.
- *  Copyright (C) 2009 Nick Bowler
+ *  Copyright © 2009-2011 Nick Bowler
  *
- *  This program is free software; you can redistribute it and/or modify
+ *  This program is free software: you can redistribute it and/or modify
  *  it under the terms of the GNU General Public License as published by
- *  the Free Software Foundation; either version 2 of the License, or
+ *  the Free Software Foundation, either version 3 of the License, or
  *  (at your option) any later version.
  *
  *  This program is distributed in the hope that it will be useful,
  *  GNU General Public License for more details.
  *
  *  You should have received a copy of the GNU General Public License
- *  along with this program; if not, write to the Free Software
- *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
  */
 
+#include <config.h>
 #include <stdio.h>
 #include <stdlib.h>
+#include <string.h>
+#include <errno.h>
+#include <assert.h>
+#include <getopt.h>
+
+#include <glib-object.h>
+
 #include "upkg.h"
+#include <uobject/uobject.h>
+#include <uobject/exportable.h>
+#include <uobject/loadable.h>
+#include <uobject/module.h>
+#include <uobject/package.h>
+#include <uobject/vfs.h>
+
+enum {
+       MODE_INFO,
+       MODE_LIST,
+       MODE_EXPORT,
+       MODE_MAX
+};
+
+int verbose = 0;
+
+static const char *progname = "upkg";
+static const char *sopts = "ilxvf:VH";
+static const struct option lopts[] = {
+       { "info",     0, NULL, 'i' },
+       { "list",     0, NULL, 'l' },
+       { "export",   0, NULL, 'x' },
+       { "file",     1, NULL, 'f' },
+       { "verbose",  0, NULL, 'v' },
+       { "version",  0, NULL, 'V' },
+       { "help",     0, NULL, 'H' },
+       { 0 }
+};
+
+void print_version(void)
+{
+       printf("%s\n", PACKAGE_STRING);
+       puts("\
+Copyright (C) 2009 Nick Bowler.\n\
+License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.\n\
+This is free software: you are free to change and redistribute it.\n\
+There is NO WARRANTY, to the extent permitted by law."
+       );
+}
+
+void print_usage(FILE *f)
+{
+       fprintf(f, "Usage: %s [options] [object ...]\n", progname);
+}
+
+void print_help(void)
+{
+       print_usage(stdout);
+       puts("Detailed help coming soon.  Until then, I'll just list my options.");
+       for (unsigned i = 0; lopts[i].name; i++) {
+               const struct option *o = &lopts[i];
+               printf("\t--%s", o->name);
+
+               if (o->has_arg == 1) {
+                       printf("=val");
+               } else if (o->has_arg == 2) {
+                       printf("[=val]");
+               }
+
+               printf(" (-%c)\n", o->val);
+       }
+}
 
-void print_upkg_flags(const char *prefix, unsigned long flags)
+static void print_upkg_flags(const char *prefix, unsigned long flags)
 {
        if (flags & UPKG_FLAG_ALLOW_DOWNLOAD)
                printf("%sAllowDownload\n", prefix);
@@ -37,36 +106,502 @@ void print_upkg_flags(const char *prefix, unsigned long flags)
                printf("%sRequired\n", prefix);
 }
 
+static void print_upkg_object_flags(const char *prefix, unsigned long flags)
+{
+       if (flags & UPKG_OBJ_FLAG_TRANSACTIONAL)
+               printf("%sTransactional\n", prefix);
+       if (flags & UPKG_OBJ_FLAG_UNREACHABLE)
+               printf("%sUnreachable\n", prefix);
+       if (flags & UPKG_OBJ_FLAG_PUBLIC)
+               printf("%sPublic\n", prefix);
+       if (flags & UPKG_OBJ_FLAG_TAG_IMPORT)
+               printf("%sImport\n", prefix);
+       if (flags & UPKG_OBJ_FLAG_TAG_EXPORT)
+               printf("%sTagExport\n", prefix);
+       if (flags & UPKG_OBJ_FLAG_SOURCE_MODIFIED)
+               printf("%sSourceModified\n", prefix);
+       if (flags & UPKG_OBJ_FLAG_TAG_GARBAGE)
+               printf("%sTagGarbage\n", prefix);
+       if (flags & UPKG_OBJ_FLAG_NEED_LOAD)
+               printf("%sNeedLoad\n", prefix);
+       if (flags & UPKG_OBJ_FLAG_HIGHLIGHT_NAME)
+               printf("%sHighlightName\n", prefix);
+       if (flags & UPKG_OBJ_FLAG_IN_SINGULAR_FUNC)
+               printf("%sInSingularFunc\n", prefix);
+       if (flags & UPKG_OBJ_FLAG_SUPPRESSED)
+               printf("%sSuppressed\n", prefix);
+       if (flags & UPKG_OBJ_FLAG_IN_END_STATE)
+               printf("%sInEndState\n", prefix);
+       if (flags & UPKG_OBJ_FLAG_TRANSIENT)
+               printf("%sTransient\n", prefix);
+       if (flags & UPKG_OBJ_FLAG_PRELOADING)
+               printf("%sPreloading\n", prefix);
+       if (flags & UPKG_OBJ_FLAG_LOAD_FOR_CLIENT)
+               printf("%sLoadForClient\n", prefix);
+       if (flags & UPKG_OBJ_FLAG_LOAD_FOR_SERVER)
+               printf("%sLoadForServer\n", prefix);
+       if (flags & UPKG_OBJ_FLAG_LOAD_FOR_EDIT)
+               printf("%sLoadForEdit\n", prefix);
+       if (flags & UPKG_OBJ_FLAG_STANDALONE)
+               printf("%sStandalone\n", prefix);
+       if (flags & UPKG_OBJ_FLAG_NOT_FOR_CLIENT)
+               printf("%sNotForClient\n", prefix);
+       if (flags & UPKG_OBJ_FLAG_NOT_FOR_SERVER)
+               printf("%sNotForServer\n", prefix);
+       if (flags & UPKG_OBJ_FLAG_NOT_FOR_EDIT)
+               printf("%sNotForEdit\n", prefix);
+       if (flags & UPKG_OBJ_FLAG_DESTROYED)
+               printf("%sDestroyed\n", prefix);
+       if (flags & UPKG_OBJ_FLAG_NEED_POST_LOAD)
+               printf("%sNeedPostLoad\n", prefix);
+       if (flags & UPKG_OBJ_FLAG_HAS_STACK)
+               printf("%sHasStack\n", prefix);
+       if (flags & UPKG_OBJ_FLAG_NATIVE)
+               printf("%sNative\n", prefix);
+       if (flags & UPKG_OBJ_FLAG_MARKED)
+               printf("%sMarked\n", prefix);
+       if (flags & UPKG_OBJ_FLAG_ERROR_SHUTDOWN)
+               printf("%sErrorShutdown\n", prefix);
+       if (flags & UPKG_OBJ_FLAG_DEBUG_POST_LOAD)
+               printf("%sDebugPostLoad\n", prefix);
+       if (flags & UPKG_OBJ_FLAG_DEBUG_SERIALIZE)
+               printf("%sDebugSerialize\n", prefix);
+       if (flags & UPKG_OBJ_FLAG_DEBUG_DESTROY)
+               printf("%sDebugDestroy\n", prefix);
+}
 
-int main(int argc, char **argv)
+void print_guid(unsigned char guid[static 16])
 {
-       struct upkg *pkg;
+       for (unsigned i = 0; i < 16; i++) {
+               printf("%02hhx", guid[i]);
+               if (i == 15)
+                       putchar('\n');
+               else
+                       putchar(' ');
+       }
+}
 
-       if (argc < 2) {
-               fprintf(stderr, "usage: upkg file\n");
-               return EXIT_FAILURE;
+static void export_print_name(const struct upkg_export *e)
+{
+       if (e) {
+               export_print_name(e->parent);
+               printf(".%s", e->name);
        }
+}
 
-       pkg = upkg_fopen(argv[1]);
-       if (!pkg) {
-               fprintf(stderr, "failed to open package!\n");
-               return EXIT_FAILURE;
+static void
+export_print_fullname(GTypeModule *pkg, const struct upkg_export *export)
+{
+       printf("%s", pkg->name);
+       export_print_name(export);
+}
+
+static void import_print_fullname(const struct upkg_import *import)
+{
+       if (import) {
+               import_print_fullname(import->parent);
+               if (import->parent)
+                       putchar('.');
+               printf("%s", import->name);
+       }
+}
+
+void print_upkg_exports(struct upkg *pkg)
+{
+       for (unsigned i = 0; i < pkg->export_count; i++) {
+               const struct upkg_export *export = upkg_get_export(pkg, i);
+               const char *package, *class;
+
+               class = upkg_export_class(pkg, i, &package);
+
+               if (!class)
+                       continue;
+
+               printf("%u - %s (%s.%s)\n", i+1, export->name, package, class);
+               printf("  Flags: %lx\n", export->flags);
+               if (verbose >= 2) {
+                       print_upkg_object_flags("    ", export->flags);
+               }
+       }
+}
+
+void print_upkg_imports(struct upkg *pkg)
+{
+       for (unsigned i = 0; i < pkg->import_count; i++) {
+               const struct upkg_import *import = upkg_get_import(pkg, i);
+
+               printf("%u - ", i);
+               import_print_fullname(import);
+               printf(" (%s.%s)\n", import->class_package, import->class_name);
        }
+}
 
+int package_info(struct upkg *pkg)
+{
        printf("Version: %u\n",  pkg->version);
        printf("License: %u\n",  pkg->license);
+       printf("GUID: ");
+       print_guid(pkg->guid);
 
        printf("Flags:   %lx\n", pkg->flags);
-       print_upkg_flags("\t", pkg->flags);
+       if (verbose >= 2) {
+               print_upkg_flags("        ", pkg->flags);
+       }
 
        printf("Names:   %lu\n", pkg->name_count);
-       for (unsigned long i = 0; i < pkg->name_count; i++) {
-               printf("\t%s\n", upkg_get_name(pkg, i));
+       if (verbose >= 3) {
+               for (unsigned long i = 0; i < pkg->name_count; i++) {
+                       printf("%lu - %s\n", i, upkg_get_name(pkg, i));
+               }
        }
 
        printf("Exports: %lu\n", pkg->export_count);
+       if (verbose >= 1) print_upkg_exports(pkg);
        printf("Imports: %lu\n", pkg->import_count);
+       if (verbose >= 1) print_upkg_imports(pkg);
+
+       return 0;
+}
+
+static int package_list(GTypeModule *pkg, long current)
+{
+       struct upkg *upkg = U_PKG(pkg)->pkg;
+       const struct upkg_export *parent = NULL;
+
+       if (current >= 0) {
+               parent = upkg_get_export(upkg, current);
+               assert(parent != NULL);
+       }
+
+       for (unsigned i = 0; i < upkg->export_count; i++) {
+               const struct upkg_export *export = upkg_get_export(upkg, i);
+
+               if (export->parent != parent)
+                       continue;
+
+               export_print_fullname(pkg, export);
+
+               if (verbose >= 1) {
+                       const char *class, *package;
+
+                       class = upkg_export_class(upkg, i, &package);
+                       printf(" (%s.%s)", package, class);
+               }
+
+               putchar('\n');
+       }
 
-       upkg_close(pkg);
        return 0;
 }
+
+static int object_dump_properties(GTypeModule *pkg, unsigned long idx)
+{
+       GParamSpec **props;
+       GObject *obj;
+       unsigned n;
+
+       obj = u_object_new_from_package(pkg, idx);
+       if (!obj) {
+               fprintf(stderr, "%s: failed to load object.\n", progname);
+               return -1;
+       }
+
+       props = g_object_class_list_properties(G_OBJECT_GET_CLASS(obj), &n);
+       for (unsigned i = 0; i < n; i++) {
+               GValue val = {0};
+               char *valstr;
+
+               printf("  property %s:", props[i]->name);
+               fflush(stdout);
+
+               g_value_init(&val, props[i]->value_type);
+               g_object_get_property(obj, props[i]->name, &val);
+
+               if (G_VALUE_HOLDS(&val, U_TYPE_OBJECT)) {
+                       UObject *obj = g_value_get_object(&val);
+
+                       if (obj && obj->pkg_name) {
+                               printf(" %s", obj->pkg_name);
+                       }
+               }
+
+               valstr = g_strdup_value_contents(&val);
+               printf(" %s\n", valstr);
+               g_free(valstr);
+       }
+
+       g_free(props);
+       g_object_unref(obj);
+
+       return 0;
+}
+
+static int object_dump_offsets(GTypeModule *pkg, unsigned long idx)
+{
+       struct upkg_file *f;
+
+       f = upkg_export_open(U_PKG(pkg)->pkg, idx);
+       if (!f) {
+               fprintf(stderr, "%s: failed to open export.\n", progname);
+               return -1;
+       }
+
+       printf("  file size: %lu\n", f->len);
+       printf("  file start: %#lx\n", f->base);
+       printf("  file end: %#lx\n", f->base + f->len);
+
+       upkg_export_close(f);
+
+       return 0;
+}
+
+static int object_info(GTypeModule *pkg, unsigned long idx)
+{
+       struct upkg *upkg = U_PKG(pkg)->pkg;
+       const struct upkg_export *export;
+       const char *class, *package;
+       int ret = 0;
+
+       export = upkg_get_export(upkg, idx);
+       export_print_fullname(pkg, export);
+
+       class = upkg_export_class(upkg, idx, &package);
+       if (class)
+               printf(" (%s.%s)\n", package, class);
+       else
+               printf(" (failed to get class)\n");
+
+       if (verbose >= 1 && object_dump_properties(pkg, idx) != 0)
+               ret = -1;
+
+       if (verbose >= 3 && object_dump_offsets(pkg, idx))
+               ret = -1;
+
+       return ret;
+}
+
+static int export(GTypeModule *pkg, GObject *obj, unsigned idx)
+{
+       struct upkg *upkg = U_PKG(pkg)->pkg;
+       char name[256];
+       FILE *of;
+       int rc;
+
+       if (U_OBJECT_IS_LOADABLE(obj) && u_object_load(obj) != 0) {
+               fprintf(stderr, "%s: failed to load export data.\n", progname);
+               return -1;
+       }
+
+       rc = u_object_export_name(obj, name, sizeof name);
+       if (rc <= 0) {
+               /* XXX: We could use a default name here. */
+               fprintf(stderr, "%s: failed to determine export filename.\n",
+                               progname);
+               return -1;
+       }
+
+       printf("exporting ");
+       export_print_fullname(pkg, upkg_get_export(upkg, idx));
+       printf(" to %s\n", name);
+
+       of = fopen(name, "wb");
+       if (!of) {
+               perror(name);
+               return -1;
+       }
+
+       u_object_export(obj, of);
+
+       if (fclose(of) != 0) {
+               perror(name);
+               return -1;
+       }
+
+       if (U_OBJECT_IS_LOADABLE(obj)) {
+               u_object_unload(obj);
+       }
+
+       return 0;
+}
+
+static int object_export(GTypeModule *pkg, unsigned long idx)
+{
+       GObject *obj;
+       int ret = -1;
+
+       obj = u_object_new_from_package(pkg, idx);
+       if (!obj) {
+               fprintf(stderr, "%s: failed to load object.\n", progname);
+               return -1;
+       }
+
+       if (!U_OBJECT_IS_EXPORTABLE(obj)) {
+               fprintf(stderr, "%s: object is not exportable.\n", progname);
+               goto out;
+       }
+
+       ret = export(pkg, obj, idx);
+out:
+       g_object_unref(obj);
+       return ret;
+}
+
+int package_export(GTypeModule *pkg)
+{
+       struct upkg *upkg = U_PKG(pkg)->pkg;
+       GObject *obj;
+       int ret = 0;
+
+       for (unsigned i = 0; i < upkg->export_count; i++) {
+               obj = u_object_new_from_package(pkg, i);
+               if (U_OBJECT_IS_EXPORTABLE(obj) && export(pkg, obj, i) != 0) {
+                       ret = -1;
+               }
+               g_object_unref(obj);
+       }
+
+       return ret;
+}
+
+static int process_object(int mode, const char *objname)
+{
+       char work[strlen(objname)+1], *p = work, *c;
+       GTypeModule *pkg;
+       long current = -1;
+       int ret = 0;
+
+       strcpy(work, objname);
+
+       /* First, find and open the actual package. */
+       c = strchr(p, '.');
+       if (c) *c = 0;
+
+       pkg = u_pkg_open(p);
+       if (!pkg || !g_type_module_use(pkg)) {
+               fprintf(stderr, "%s: %s: failed to open package.\n",
+                               progname, work);
+               goto out;
+       }
+
+       if (!U_PKG(pkg)->pkg) {
+               if (u_pkg_is_native(pkg)) {
+                       fprintf(stderr, "%s: %s: not a UObject package.\n",
+                                       progname, work);
+               } else {
+                       fprintf(stderr, "%s: %s: package not found.\n",
+                                       progname, work);
+               }
+
+               goto out_unuse;
+       }
+
+       /* Resolve the hierarchical reference. */
+       while (c) {
+               p = c+1;
+               c = strchr(p, '.');
+               if (c) *c = 0;
+
+               current = upkg_export_find(U_PKG(pkg)->pkg, current, p);
+               if (current == -1) {
+                       /* We want to print the full name up to this point. */
+                       strcpy(work, objname);
+                       if (c) *c = 0;
+
+                       fprintf(stderr, "%s: %s: object not found.\n",
+                                       progname, work);
+                       goto out_unuse;
+               }
+       }
+
+       switch (mode) {
+       case MODE_INFO:
+               if (current < 0)
+                       ret = package_info(U_PKG(pkg)->pkg);
+               else
+                       ret = object_info(pkg, current);
+               break;
+       case MODE_LIST:
+               ret = package_list(pkg, current);
+               break;
+       case MODE_EXPORT:
+               if (current < 0)
+                       ret = package_export(pkg);
+               else
+                       ret = object_export(pkg, current);
+               break;
+       default:
+               abort();
+       }
+
+out_unuse:
+       g_type_module_unuse(pkg);
+out:
+       return ret;
+}
+
+int main(int argc, char **argv)
+{
+       const char *pkgname = NULL;
+       unsigned mode = MODE_INFO;
+       int opt, rc;
+
+       if (argc > 0) progname = argv[0];
+
+       if (u_pkg_vfs_init() != 0)
+               return EXIT_FAILURE;
+       if (u_object_module_init() != 0)
+               return EXIT_FAILURE;
+
+       while ((opt = getopt_long(argc, argv, sopts, lopts, NULL)) != -1) {
+               switch (opt) {
+               case 'f':
+                       pkgname = u_pkg_vfs_add_local(NULL, optarg);
+                       if (!pkgname) {
+                               fprintf(stderr, "failed to add package `%s'.\n", optarg);
+                               return EXIT_FAILURE;
+                       }
+                       break;
+               case 'i':
+                       mode = MODE_INFO;
+                       break;
+               case 'l':
+                       mode = MODE_LIST;
+                       break;
+               case 'x':
+                       mode = MODE_EXPORT;
+                       break;
+               case 'v':
+                       verbose++;
+                       break;
+               case 'V':
+                       print_version();
+                       return EXIT_SUCCESS;
+               case 'H':
+                       print_help();
+                       return EXIT_SUCCESS;
+               default:
+                       print_usage(stderr);
+                       return EXIT_FAILURE;
+               }
+       }
+
+       if (!pkgname && !argv[optind]) {
+               print_usage(stderr);
+               return EXIT_FAILURE;
+       }
+
+       rc = EXIT_SUCCESS;
+       if (argv[optind]) {
+               for (int i = optind; i < argc; i++) {
+                       if (process_object(mode, argv[i]) != 0)
+                               rc = EXIT_FAILURE;
+               }
+       } else {
+               if (process_object(mode, pkgname) != 0)
+                       rc = EXIT_FAILURE;
+       }
+
+       u_object_module_exit();
+       u_pkg_vfs_exit();
+       return rc;
+}