-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmaps.c
More file actions
337 lines (283 loc) · 7.63 KB
/
Copy pathmaps.c
File metadata and controls
337 lines (283 loc) · 7.63 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
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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "maps.h"
#include "StormLib.h"
typedef struct string_entry
{
unsigned int str_id; // the string id from the map TRIGSTR_%i
unsigned int offset; // offset into the ELF-style string table
} string_entry;
typedef struct string_table
{
int size; // size of strings
int max_entries; // size of index
int count; // number of entries
char* strings; // string data
string_entry* index; // lookup table to find offset into string data
} string_table;
static int get_string_id(const char* s)
{
char* num = strchr(s, '_');
if (!num)
return 0;
return atoi(++num);
}
const char* lookup_string(int id, string_table* tbl)
{
//printf("%i entries\n", tbl->count);
int i;
for (i=0; i < tbl->count; i++)
{
if (tbl->index[i].str_id == id)
{
//printf("[%i|%i] %s (%i)\n", tbl->index[i].str_id, tbl->index[i].offset, tbl->strings + tbl->index[i].offset, strlen(tbl->strings + tbl->index[i].offset));
return tbl->strings + tbl->index[i].offset;
}
}
return NULL;
}
// wts contains the string table
static int wts_read(HANDLE h, string_table* tbl)
{
int ret=0;
int fsize = SFileGetFileSize(h, NULL);
char* buf = malloc(fsize);
int read;
if (!SFileReadFile(h, buf, fsize, &read, 0))
{
printf("Error reading wts\n");
ret = -1;
goto err;
}
// dump to external file
FILE* outfile = fopen("strings.txt", "wb");
if (outfile)
{
fwrite(buf, fsize, 1, outfile);
fclose(outfile);
free(buf);
}
// build a string table like ELF, plus an index to find the entries
tbl->size = 65536; // allocate 64KB for strings - that should be enough for anyone
tbl->max_entries = 256;
tbl->count = 0;
tbl->strings = malloc(tbl->size);
tbl->index = malloc(sizeof(string_entry) * tbl->max_entries);
tbl->strings[0] = 0; // leave the first byte null for the null string
char* entry = tbl->strings + 1;
string_entry* index = tbl->index;
char* tok = strtok(buf, " ");
while (tok)
{
tok = strtok(NULL, "{");
if (!tok) break;
int id = atoi(tok);
//printf("id: %i\n", id);
// take the value and peel off any leading or trailing whitespace
tok = strtok(NULL, "}");
if (!tok) break;
while (isspace(*tok)) tok++;
char* end = tok + strlen(tok) - 1;
while(end > tok && isspace(*end)) end--;
*(end+1) = 0;
//printf("val: %s\n", tok);
// add the entry to the index
index = &tbl->index[tbl->count++];
index->str_id = id;
index->offset = 0; // point to the 0-string
if (strlen(tok) > 0)
{
if (entry - tbl->strings + strlen(tok) + 1 > tbl->size)
printf("Error: Overflow in string table\n");
index->offset = entry - tbl->strings;
// add the string to the table
//printf("Adding %s (%i) to string table\n", tok, strlen(tok));
strcpy(entry, tok);
entry += strlen(tok) + 1;
}
tok = strtok(NULL, " ");
}
printf("Done\n");
err:
return ret;
}
// blp contains the minimap
static int blp_read(HANDLE h)
{
char* buf;
int ret=0;
blp_header* header;
printf("Reading minimap\n");
buf = malloc(256);
header = (blp_header*)buf;
int read;
if (!SFileReadFile(h, buf, sizeof(blp_header_jpg), &read, 0))
{
printf("Error reading blp\n");
ret = -1;
goto err;
}
if (memcmp(header->magic, BLP_MAGIC, 4) != 0)
{
printf("blp bad magic %s\n", header->magic);
ret = -2;
goto err;
}
if (header->type & BLP_TYPE_PALETTE)
printf("paletted\n");
else
{
blp_header_jpg* blp_jpg = (blp_header_jpg*)buf;
//printf("jpg header size 0x%x\n", blp_jpg->jpg_header_size);
char* jpg = malloc(blp_jpg->jpg_header_size + blp_jpg->a.mipmap_size[0]); // JPEG header + data
if (!SFileReadFile(h, jpg, blp_jpg->jpg_header_size, &read, 0))
{
printf("Error reading jpg header\n");
ret = -3;
goto err;
}
SFileSetFilePointer(h, blp_jpg->a.mipmap_offset[0], NULL, FILE_BEGIN);
if (!SFileReadFile(h, jpg+blp_jpg->jpg_header_size, blp_jpg->a.mipmap_size[0], &read, 0))
{
printf("Error reading jpg data\n");
}
// dump to external file
FILE* mmjpg = fopen("minimap.jpg", "wb");
if (mmjpg)
{
fwrite(jpg, blp_jpg->jpg_header_size + blp_jpg->a.mipmap_size[0], 1, mmjpg);
fclose(mmjpg);
free(jpg);
}
printf("Done\n");
}
err:
free(buf);
return ret;
}
// w3i contains map info
static int w3i_read(HANDLE h, string_table* tbl)
{
char* buf;
int ret = -1;
unsigned int len = SFileGetFileSize(h, 0);
printf("war3map.w3i size: %u\n", len);
buf = malloc(len);
int read;
if (!SFileReadFile(h, buf, len, &read, 0))
{
printf("Error reading w3i\n");
ret = -1;
goto err;
}
w3i_header_cmn* header = (w3i_header_cmn*)buf;
w3i_roc_header roc;
switch(header->format)
{
case W3I_FORMAT_ROC:
printf("Reign Of Chaos map\n");
roc.a = header;
roc.name = buf + sizeof(w3i_header_cmn);
roc.author = roc.name + strlen(roc.name) + 1;
roc.description = roc.author + strlen(roc.author) + 1;
roc.rnp = roc.description + strlen(roc.description) + 1;
roc.b = (w3i_header_roc_1*)roc.rnp + strlen(roc.rnp) + 1;
roc.ml_text = (char*)(roc.b + sizeof(w3i_header_roc_1));
roc.ml_title = roc.ml_text + strlen(roc.ml_text) + 1;
roc.ml_subtitle = roc.ml_title + strlen(roc.ml_title) + 1;
printf("name: %s\n", lookup_string(get_string_id(roc.name), tbl));
printf("author: %s\n", lookup_string(get_string_id(roc.author), tbl));
printf("description: %s\n", lookup_string(get_string_id(roc.description), tbl));
printf("recommended players: %s\n", lookup_string(get_string_id(roc.rnp), tbl));
//printf("Size: %i %i %i %i\n", roc.b->width[0], roc.b->width[1], roc.b->width[2], roc.b->width[3]);
//printf("ml text: %s\n", roc.ml_text);
break;
case W3I_FORMAT_TFT:
printf("Frozen Throne map\n");
break;
default:
printf("Unknown map format %i\n", header->format);
ret = -2;
goto err;
}
ret = 0;
err:
//free(buf);
return ret;
}
int load_map(const char* path, const char* base)
{
int bufsize = 512;
char* buf;
w3m_file_header* header;
w3m_file_header_2* header2;
string_table trigstr;
char* fullpath = malloc(strlen(path) + strlen(base) + 3);
if (base)
{
strcpy(fullpath, base);
if (fullpath[strlen(fullpath)-1] != '/')
strcat(fullpath, "/");
}
strcat(fullpath, path);
// fix up the path for POSIX from Windows
char* slash = strchr(fullpath, '\\');
while (slash)
{
*slash = '/';
slash = strchr(fullpath, '\\');
}
printf("Loading map from %s\n", fullpath);
int size;
HANDLE MapMPQ;
HANDLE SubFile;
FILE* mf = fopen(fullpath, "rb");
if (!mf)
{
printf("Error opening map at %s\n", fullpath);
return -1;
}
printf("map is open!\n");
fseek(mf, 0, SEEK_END);
size = ftell(mf);
printf("map is %i bytes\n", size);
fseek(mf, 0, SEEK_SET);
buf = malloc(bufsize);
fread(buf, bufsize, 1, mf);
header = (w3m_file_header*)buf;
header2 = (w3m_file_header_2*)(buf + sizeof(w3m_file_header) + strlen(header->name) + 1);
printf("Magic: %s\n", header->magic);
printf("Map Name: %s\n", header->name);
printf("Max Players: %i\n", header2->max_players);
printf("Flags: 0x%x\n", header2->flags);
fclose(mf);
if (!SFileOpenArchive(fullpath, 0, MPQ_OPEN_FORCE_MPQ_V1, &MapMPQ))
return -2;
//if (!SFileOpenFileEx(MapMPQ, "Scripts\\common.j", 0, &SubFile))
// return -3;
//printf("found common.j\n");
// read the string table
printf("Reading wts\n");
if (SFileOpenFileEx(MapMPQ, "war3map.wts", 0, &SubFile))
{
wts_read(SubFile, &trigstr);
SFileCloseFile(SubFile);
}
// read the map info
printf("Reading w3i\n");
if (SFileOpenFileEx(MapMPQ, "war3map.w3i", 0, &SubFile))
{
w3i_read(SubFile, &trigstr);
SFileCloseFile(SubFile);
}
// read the minimap
printf("Reading blp\n");
if (SFileOpenFileEx(MapMPQ, "war3mapMap.blp", 0, &SubFile))
{
blp_read(SubFile);
SFileCloseFile(SubFile);
}
SFileCloseArchive(MapMPQ);
return size;
}