-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmapupgr01to02.c
More file actions
137 lines (124 loc) · 2.72 KB
/
mapupgr01to02.c
File metadata and controls
137 lines (124 loc) · 2.72 KB
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
#include <stdio.h>
int main(int argc, char **argv)
{
int result;
FILE *in, *out;
#pragma pack(push,1)
union {
char buf[8];
struct {
int version;
int numremoves;
int numobjects;
} data;
} headerv1;
union {
char buf[28];
struct {
int version;
int numremoves;
int numobjects;
int numzones;
float streamin;
float streamout;
float drawdistance;
} data;
} headerv2;
union {
int model;
int data[2];
struct {
int model;
float x, y, z, rx, ry, rz;
float drawdistance;
} objectv1;
struct {
int model;
float x, y, z, rx, ry, rz;
} objectv2;
struct {
int model;
float x, y, z, radius;
} removev1;
struct {
int model;
float x, y, z, radius;
} removev2;
} data;
#pragma pack(pop)
result = 1;
if (argc < 3) {
puts("need inputfile and outputfile");
return 1;
}
if (!(in = fopen(argv[1], "rb"))) {
printf("can't open file '%s' for reading\n", argv[1]);
return 1;
}
if (!(out = fopen(argv[2], "wb"))) {
printf("can't open file '%s' for writing\n", argv[2]);
fclose(in);
return 1;
}
headerv2.data.version = 0x0250414D;
if (headerv2.buf[0] != 'M' || headerv2.buf[3] != 2) {
puts("wrong endianness or structs are not packed");
goto ret;
}
headerv2.data.numremoves = 0;
headerv2.data.numobjects = 0;
headerv2.data.numzones = 0;
headerv2.data.streamin = 500.0f;
headerv2.data.streamout = 600.0f;
headerv2.data.drawdistance = 1500.0f;
if (!fread(&headerv1, sizeof(headerv1), 1, in)) {
goto corrupted;
}
headerv2.data.numremoves = headerv1.data.numremoves;
headerv2.data.numobjects = headerv1.data.numobjects;
fwrite(&headerv2, sizeof(headerv2), 1, out);
if (headerv1.data.numremoves == 0) {
goto skipremoves;
}
while (fread(&data.model, sizeof(data.model), 1, in)) {
if (data.model < 0) {
if (!fread(data.data + 1, sizeof(data.removev1) - sizeof(data.model), 1, in)) {
goto corrupted;
}
if (data.model != -1) {
data.model = -data.model;
}
fwrite(&data.removev2, sizeof(data.removev2), 1, out);
} else {
if (!fread(data.data + 1, sizeof(data.objectv1) - sizeof(data.model), 1, in)) {
goto corrupted;
}
}
}
if (fseek(in, sizeof(headerv1), SEEK_SET)) {
puts("failed to fseek");
goto ret;
}
skipremoves:
while (fread(&data.model, sizeof(data.model), 1, in)) {
if (data.model < 0) {
if (!fread(data.data + 1, sizeof(data.removev1) - sizeof(data.model), 1, in)) {
goto corrupted;
}
} else {
if (!fread(data.data + 1, sizeof(data.objectv1) - sizeof(data.model), 1, in)) {
goto corrupted;
}
fwrite(&data.objectv2, sizeof(data.objectv2), 1, out);
}
}
result = 0;
ret:
fclose(in);
fclose(out);
return result;
corrupted:
puts("input file is corrupted");
result = 1;
goto ret;
}