-
Notifications
You must be signed in to change notification settings - Fork 594
/
vtxdis.c
256 lines (223 loc) · 5.93 KB
/
vtxdis.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
#include <stdlib.h>
#include <stdint.h>
#include <getopt.h>
#include <stdio.h>
#include <string.h>
#include <inttypes.h>
#include <sys/stat.h>
#include <errno.h>
#define VTXDIS_VER "0.2"
#define SWAP16(x) (((x & 0xFF00) >> 8) | ((x & 0x00FF) << 8))
typedef struct {
int16_t pos[3]; /* 0x00 */
int16_t flag; /* 0x06 */
int16_t tpos[2]; /* 0x08 */
uint8_t cn[4]; /* 0x0C */
} Vtx; /* 0x10 */
static char *filename = NULL;
static char *data = NULL;
static int32_t offset = 0;
static int32_t data_len = 0;
static int16_t count = 0;
static const struct option cmdline_opts[] = {
{ "offset", required_argument, NULL, 'o', },
{ "length", required_argument, NULL, 'l', },
{ "file" , required_argument, NULL, 'f', },
{ "version", no_argument, NULL, '~', },
{ "help", no_argument, NULL, '?', },
{ "count", required_argument, NULL, 'c', },
{ 0, 0, 0, 0 },
};
static uint32_t parse_int(const char *num){
uint32_t ret;
char outnum[21];
if(strlen(num) > 2 && num[0] == '0' && (num[1] == 'x' || num[1] == 'X')) {
strncpy(outnum, &num[2], 20);
sscanf(outnum, "%"SCNx32, &ret);
} else if(strlen(num) == 0){
ret = -1;
} else {
strncpy(outnum, num, 20);
sscanf(outnum, "%"SCNu32, &ret);
}
return ret;
}
static inline void print_usage(void)
{
puts("vtxdis version " VTXDIS_VER "\n"
"Usage:\n"
" vtxdis -f/--file FILE [options]\n"
" vtxdis -?/--help\n"
" vtxdis --version\n"
"Options:\n"
" -f, --file The file path to extract vertex data from.\n"
" -c, --count The number of vertices to extract.\n"
" -l, --length The amount of data to extract vertices from.\n"
" -o, --offset The offset into file to start reading vertex data.\n"
" -?, --help Prints this help message\n"
" --version Prints the current version\n"
);
}
static inline void print_version(void){
puts("Version: " VTXDIS_VER);
}
static void print_vtx_data(Vtx *vtx, int vtx_cnt)
{
puts("{");
for(int i = 0; i < vtx_cnt; i++)
{
Vtx *v = &vtx[i];
printf(" VTX(%d, %d, %d, %d, %d, %d, %d, %d, %d),\n", v->pos[0], v->pos[1], v->pos[2], v->tpos[0], v->tpos[1], v->cn[0], v->cn[1], v->cn[2], v->cn[3]);
}
puts("}");
}
static void parse_file(void)
{
unsigned int alloc_size = 0;
struct stat sbuffer;
stat(filename, &sbuffer);
if(errno != 0){
perror("Count not stat file.");
exit(1);
}
/* sanity checks */
if(count > 0)
{
alloc_size = sizeof(Vtx) * count;
if((offset > 0 && (offset + alloc_size) > sbuffer.st_size) || alloc_size > sbuffer.st_size)
{
puts("Requested data is beyond file boundaries.");
exit(1);
}
}
else if(data_len > 0)
{
alloc_size = data_len;
if((offset > 0 && (offset + alloc_size) > sbuffer.st_size) || alloc_size > sbuffer.st_size)
{
puts("Requested data is beyond file boundaries.");
exit(1);
}
}
else
{
if (offset > 0)
{
alloc_size = sbuffer.st_size - offset;
}
else
{
alloc_size = sbuffer.st_size;
}
}
if(alloc_size % sizeof(Vtx) != 0)
{
printf("Requested data size is not a multiple of sizeof(Vtx). Requested size is %8x", alloc_size);
exit(1);
}
FILE *file = fopen(filename, "rb");
if(!file){
perror("Could not open file");
exit(1);
}
if(offset > 0){
if(fseek(file, offset, SEEK_SET)){
perror("Could not seek file");
fclose(file);
exit(1);
}
}
Vtx *data = malloc(alloc_size);
if(!data){
fclose(file);
perror("Could not allocate vtx data");
exit(1);
}
if(!fread(data, alloc_size, 1, file)){
perror("Could not read from file");
fclose(file);
free(data);
exit(1);
}
fclose(file);
unsigned int vtx_cnt = alloc_size / sizeof(Vtx);
for(unsigned int i = 0; i < vtx_cnt; i++){
Vtx *v = &data[i];
v->pos[0] = SWAP16(v->pos[0]);
v->pos[1] = SWAP16(v->pos[1]);
v->pos[2] = SWAP16(v->pos[2]);
v->flag = SWAP16(v->flag);
v->tpos[0] = SWAP16(v->tpos[0]);
v->tpos[1] = SWAP16(v->tpos[1]);
}
print_vtx_data(data, vtx_cnt);
free(data);
}
int main(int argc, char **argv)
{
int opt;
while(1){
opt = getopt_long(argc, argv, "o:l:f:c:v?", cmdline_opts, NULL);
if(opt == -1){
break;
}
switch(opt){
case 'd':
data = optarg;
break;
case '~':
print_version();
return 0;
case '?':
print_usage();
return 0;
case 'l':
data_len = parse_int(optarg);
break;
case 'o':
offset = parse_int(optarg);
break;
case 'f':
filename = optarg;
break;
case 'c':
count = parse_int(optarg);
break;
}
}
if (filename == NULL && data == NULL)
{
puts("Must specify -f or -d");
print_usage();
exit(1);
}
if(data_len < 0)
{
puts("Invalid -l/--length parameter passed.");
print_usage();
exit(1);
}
if(offset < 0)
{
puts("Invalid -o/--offset parameter passed.");
print_usage();
exit(1);
}
if(count < 0)
{
puts("Invalid -c/--count parameter passed.");
print_usage();
exit(1);
}
if(count > 0 && data_len > 0)
{
puts("Cannot specify both -c/--count and -l/--length.");
print_usage();
exit(1);
}
if(filename != NULL)
{
parse_file();
}
return 0;
}