]> git.draconx.ca Git - upkg.git/blob - src/upkg.c
upkg: Add gnulib getopt_long support and implement --help/--version.
[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 static const char *progname = "upkg";
33
34 static const char *sopts = "VH";
35 static const struct option lopts[] = {
36         { "version",  0, NULL, 'V' },
37         { "help",     0, NULL, 'H' },
38         { 0 }
39 };
40
41 void print_version(void)
42 {
43         printf("%s\n", PACKAGE_STRING);
44         puts("\
45 Copyright (C) 2009 Nick Bowler.\n\
46 This is free software: you are free to change and redistribute it.\n\
47 There is NO WARRANTY, to the extent permitted by law."
48         );
49 }
50
51 void print_usage(void)
52 {
53         printf("Usage: %s [options] package\n", progname);
54 }
55
56 void print_help(void)
57 {
58         print_usage();
59         puts("Detailed help coming soon.  Until then, I'll just list my options.");
60         for (unsigned i = 0; lopts[i].name; i++) {
61                 const struct option *o = &lopts[i];
62                 printf("\t--%s", o->name);
63
64                 if (o->has_arg == 1) {
65                         printf("=val");
66                 } else if (o->has_arg == 2) {
67                         printf("[=val]");
68                 }
69
70                 printf(" (-%c)\n", o->val);
71         }
72 }
73
74 void print_upkg_flags(const char *prefix, unsigned long flags)
75 {
76         if (flags & UPKG_FLAG_ALLOW_DOWNLOAD)
77                 printf("%sAllowDownload\n", prefix);
78         if (flags & UPKG_FLAG_CLIENT_OPTIONAL)
79                 printf("%sClientOptional\n", prefix);
80         if (flags & UPKG_FLAG_SERVER_ONLY)
81                 printf("%sServerOnly\n", prefix);
82         if (flags & UPKG_FLAG_BROKEN_LINKS)
83                 printf("%sBrokenLinks\n", prefix);
84         if (flags & UPKG_FLAG_INSECURE)
85                 printf("%sInsecure\n", prefix);
86         if (flags & UPKG_FLAG_REQUIRED)
87                 printf("%sRequired\n", prefix);
88 }
89
90 void print_guid(unsigned char guid[static 16])
91 {
92         for (unsigned i = 0; i < 16; i++) {
93                 printf("%02hhx", guid[i]);
94                 if (i == 15)
95                         putchar('\n');
96                 else
97                         putchar(' ');
98         }
99 }
100
101 int main(int argc, char **argv)
102 {
103         struct upkg *pkg;
104         int opt;
105
106         if (argc > 0) progname = argv[0];
107
108         while ((opt = getopt_long(argc, argv, sopts, lopts, NULL)) != -1) {
109                 switch (opt) {
110                 case 'V':
111                         print_version();
112                         return EXIT_SUCCESS;
113                 case 'H':
114                         print_help();
115                         return EXIT_SUCCESS;
116                 default:
117                         print_usage();
118                         return EXIT_FAILURE;
119                 }
120         }
121
122         if (argc < 2) {
123                 fprintf(stderr, "usage: upkg file\n");
124                 return EXIT_FAILURE;
125         }
126
127         if (module_init() != 0)
128                 return EXIT_FAILURE;
129
130         pkg = upkg_fopen(argv[1]);
131         if (!pkg) {
132                 fprintf(stderr, "failed to open package!\n");
133                 return EXIT_FAILURE;
134         }
135
136         printf("Version: %u\n",  pkg->version);
137         printf("License: %u\n",  pkg->license);
138         printf("GUID: ");
139         print_guid(pkg->guid);
140
141         printf("Flags:   %lx\n", pkg->flags);
142         print_upkg_flags("\t", pkg->flags);
143
144         printf("Names:   %lu\n", pkg->name_count);
145         for (unsigned long i = 0; i < pkg->name_count; i++) {
146                 printf("\t%s\n", upkg_get_name(pkg, i));
147         }
148
149         printf("Exports: %lu\n", pkg->export_count);
150         printf("Imports: %lu\n", pkg->import_count);
151
152         GObject *music = g_object_new(module_get_class("Engine", "Music"), NULL);
153         if (!music)
154                 return EXIT_FAILURE;
155         struct upkg_file *f = upkg_export_open(pkg, 0);
156         u_object_deserialize(music, f);
157
158         char name[256];
159         u_object_export_name(music, name, sizeof name);
160         FILE *fp = fopen(name, "wb");
161         if (!fp) return EXIT_FAILURE;
162         u_object_export(music, fp);
163         fclose(fp);
164         printf("Wrote %s\n", name);
165
166         upkg_close(pkg);
167         module_exit();
168
169         return 0;
170 }