-
Notifications
You must be signed in to change notification settings - Fork 0
/
mapupgr02to03.c
150 lines (134 loc) · 3.33 KB
/
mapupgr02to03.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
#include <stdio.h>
int main(int argc, char **argv)
{
int result;
FILE *in, *out;
#pragma pack(push,1)
struct {
int version;
int numremoves;
int numobjects;
int numzones;
float streamin;
float streamout;
float drawdistance;
} headerv2;
struct {
union {
int num;
char buf[4];
} version;
int numremoves;
int numobjects;
int objectdata_size;
int numzones;
float streamin;
float streamout;
float drawdistance;
} headerv3;
struct {
short objectdata_size;
struct {
int model;
float x, y, z, rx, ry, rz;
} objectv2;
float drawdistance;
char no_camera_col;
short attached_object;
short attached_vehicle;
char num_materials;
/*Note that objectv3 can have more data, but there is none
in this case because we're upgrading from objectv2.*/
} objectv3;
#pragma pack(pop)
char bigbuffer[1000];
int i;
#define SIZEOF_REMOVE 20
#define SIZEOF_ZONE 20
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;
}
headerv3.version.num = 0x0350414D;
if (headerv3.version.buf[0] != 'M' || headerv3.version.buf[3] != 3) {
puts("wrong endianness or structs are not packed");
goto ret;
}
if (!fread(&headerv2, sizeof(headerv2), 1, in)) {
puts("failed to read header");
goto ret;
}
if (headerv2.version != 0x0250414D) {
puts("input file is not v2");
goto ret;
}
headerv3.numremoves = headerv2.numremoves;
headerv3.numobjects = headerv2.numobjects;
headerv3.objectdata_size = headerv2.numobjects * sizeof(objectv3);
headerv3.numzones = headerv2.numzones;
headerv3.streamin = headerv2.streamin;
headerv3.streamout = headerv2.streamout;
headerv3.drawdistance = headerv2.drawdistance;
fwrite(&headerv3, sizeof(headerv3), 1, out);
for (i = 0; i < headerv2.numremoves; i++) {
if (!fread(bigbuffer, SIZEOF_REMOVE, 1, in)) {
puts("eof while reading removes");
goto ret;
}
fwrite(bigbuffer, SIZEOF_REMOVE, 1, out);
}
if (fseek(in, headerv2.numobjects * sizeof(objectv3.objectv2), SEEK_CUR)) {
puts("failed to fseek to zones");
goto ret;
}
for (i = 0; i < headerv2.numzones; i++) {
if (!fread(bigbuffer, SIZEOF_ZONE, 1, in)) {
puts("eof while reading removes");
goto ret;
}
fwrite(bigbuffer, SIZEOF_ZONE, 1, out);
}
if (fseek(in, sizeof(headerv2) + headerv2.numremoves * SIZEOF_REMOVE, SEEK_SET)) {
puts("failed to fseek to objects");
goto ret;
}
for (i = 0; i < headerv2.numobjects; i++) {
if (!fread(&objectv3.objectv2, sizeof(objectv3.objectv2), 1, in)) {
puts("eof while reading objects");
goto ret;
}
objectv3.objectdata_size = sizeof(objectv3);
objectv3.drawdistance = headerv3.drawdistance;
objectv3.no_camera_col = 0;
objectv3.attached_object = -1;
objectv3.attached_vehicle = -1;
objectv3.num_materials = 0;
fwrite(&objectv3, sizeof(objectv3), 1, out);
}
if (fseek(in, headerv2.numzones * SIZEOF_ZONE, SEEK_CUR)) {
puts("failed to fseek to eof");
goto ret;
}
if (fread(bigbuffer, 1, 1, in) || !feof(in)) {
puts("more data at the end of the file");
goto ret;
}
result = 0;
ret:
if (result) {
printf("failed for %s\n", argv[2]);
}
fclose(in);
fclose(out);
return result;
}