]> git.draconx.ca Git - upkg.git/blob - src/upkg.c
upkg: Print package export info.
[upkg.git] / src / upkg.c
1 /*
2  *  upkg: tool for manipulating Unreal Tournament packages.
3  *  Copyright (C) 2009 Nick Bowler
4  *
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 2 of the License, or
8  *  (at your option) any later version.
9  *
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.
14  *
15  *  You should have received a copy of the GNU General Public License
16  *  along with this program; if not, write to the Free Software
17  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18  */
19
20 #include <config.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <getopt.h>
24
25 #include <glib-object.h>
26
27 #include "upkg.h"
28 #include "module.h"
29 #include "uobject.h"
30 #include "exportable.h"
31
32 enum {
33         MODE_INFO,
34         MODE_EXPORT,
35         MODE_MAX
36 };
37
38 int verbose = 0;
39
40 static const char *progname = "upkg";
41 static const char *sopts = "ivVH";
42 static const struct option lopts[] = {
43         { "info",     0, NULL, 'i' },
44         { "verbose",  0, NULL, 'v' },
45         { "version",  0, NULL, 'V' },
46         { "help",     0, NULL, 'H' },
47         { 0 }
48 };
49
50 void print_version(void)
51 {
52         printf("%s\n", PACKAGE_STRING);
53         puts("\
54 Copyright (C) 2009 Nick Bowler.\n\
55 This is free software: you are free to change and redistribute it.\n\
56 There is NO WARRANTY, to the extent permitted by law."
57         );
58 }
59
60 void print_usage(void)
61 {
62         printf("Usage: %s [options] package\n", progname);
63 }
64
65 void print_help(void)
66 {
67         print_usage();
68         puts("Detailed help coming soon.  Until then, I'll just list my options.");
69         for (unsigned i = 0; lopts[i].name; i++) {
70                 const struct option *o = &lopts[i];
71                 printf("\t--%s", o->name);
72
73                 if (o->has_arg == 1) {
74                         printf("=val");
75                 } else if (o->has_arg == 2) {
76                         printf("[=val]");
77                 }
78
79                 printf(" (-%c)\n", o->val);
80         }
81 }
82
83 void print_upkg_flags(const char *prefix, unsigned long flags)
84 {
85         if (flags & UPKG_FLAG_ALLOW_DOWNLOAD)
86                 printf("%sAllowDownload\n", prefix);
87         if (flags & UPKG_FLAG_CLIENT_OPTIONAL)
88                 printf("%sClientOptional\n", prefix);
89         if (flags & UPKG_FLAG_SERVER_ONLY)
90                 printf("%sServerOnly\n", prefix);
91         if (flags & UPKG_FLAG_BROKEN_LINKS)
92                 printf("%sBrokenLinks\n", prefix);
93         if (flags & UPKG_FLAG_INSECURE)
94                 printf("%sInsecure\n", prefix);
95         if (flags & UPKG_FLAG_REQUIRED)
96                 printf("%sRequired\n", prefix);
97 }
98
99 void print_guid(unsigned char guid[static 16])
100 {
101         for (unsigned i = 0; i < 16; i++) {
102                 printf("%02hhx", guid[i]);
103                 if (i == 15)
104                         putchar('\n');
105                 else
106                         putchar(' ');
107         }
108 }
109
110 void print_upkg_exports(struct upkg *pkg)
111 {
112         for (unsigned i = 0; i < pkg->export_count; i++) {
113                 const char *name, *package, *group, *class;
114
115                 name  = upkg_export_name(pkg, i);
116                 class = upkg_export_class(pkg, i, &package, &group);
117
118                 if (!name || !class)
119                         continue;
120
121                 printf("%u - %s%s%s (%s.%s)\n", i+1,
122                         group ? group : "", group ? "." : "", name,
123                         package, class);
124         }
125 }
126
127 int package_info(struct upkg *pkg)
128 {
129         printf("Version: %u\n",  pkg->version);
130         printf("License: %u\n",  pkg->license);
131         printf("GUID: ");
132         print_guid(pkg->guid);
133
134         printf("Flags:   %lx\n", pkg->flags);
135         if (verbose >= 1) {
136                 print_upkg_flags("\t", pkg->flags);
137         }
138
139         if (verbose >= 1) {
140                 printf("Names:   %lu\n", pkg->name_count);
141                 if (verbose >= 2) {
142                         for (unsigned long i = 0; i < pkg->name_count; i++) {
143                                 printf("\t%s\n", upkg_get_name(pkg, i));
144                         }
145                 }
146         }
147
148         printf("Exports: %lu\n", pkg->export_count);
149         if (verbose >= 1) print_upkg_exports(pkg);
150         printf("Imports: %lu\n", pkg->import_count);
151
152         return EXIT_SUCCESS;
153 }
154
155 int main(int argc, char **argv)
156 {
157         struct upkg *pkg;
158         unsigned mode = MODE_INFO;
159         int opt, rc = EXIT_FAILURE;
160
161         if (argc > 0) progname = argv[0];
162
163         while ((opt = getopt_long(argc, argv, sopts, lopts, NULL)) != -1) {
164                 switch (opt) {
165                 case 'i':
166                         mode = MODE_INFO;
167                         break;
168                 case 'v':
169                         verbose++;
170                         break;
171                 case 'V':
172                         print_version();
173                         return EXIT_SUCCESS;
174                 case 'H':
175                         print_help();
176                         return EXIT_SUCCESS;
177                 default:
178                         print_usage();
179                         return EXIT_FAILURE;
180                 }
181         }
182
183         if (argv[optind] == NULL) {
184                 print_usage();
185                 return EXIT_FAILURE;
186         }
187
188         if (module_init() != 0)
189                 return EXIT_FAILURE;
190
191         pkg = upkg_fopen(argv[optind]);
192         if (!pkg) {
193                 fprintf(stderr, "failed to open package!\n");
194                 return EXIT_FAILURE;
195         }
196
197         switch (mode) {
198         case MODE_INFO:
199                 rc = package_info(pkg);
200                 break;
201         }
202
203         upkg_close(pkg);
204         module_exit();
205         return rc;
206 }