-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmpq_libmpq.cpp
216 lines (180 loc) · 3.26 KB
/
mpq_libmpq.cpp
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
#include "mpq_libmpq.h"
#include "wowmapview.h"
#include <vector>
typedef std::vector<mpq_archive*> ArchiveSet;
ArchiveSet gOpenArchives;
MPQArchive::MPQArchive(const char* filename)
{
int result = libmpq_archive_open(&mpq_a, (unsigned char*)filename);
gLog("Opening %s\n", filename);
if(result) {
gLog("Error opening archive %s\n", filename);
return;
}
gOpenArchives.push_back(&mpq_a);
}
void MPQArchive::close()
{
//gOpenArchives.erase(erase(&mpq_a);
libmpq_archive_close(&mpq_a);
}
MPQFile::MPQFile(const char* filename):
eof(false),
buffer(0),
pointer(0),
size(0)
{
for(ArchiveSet::iterator i=gOpenArchives.begin(); i!=gOpenArchives.end();++i)
{
mpq_archive &mpq_a = **i;
int fileno = libmpq_file_number(&mpq_a, filename);
if(fileno == LIBMPQ_EFILE_NOT_FOUND)
continue;
// Found!
size = libmpq_file_info(&mpq_a, LIBMPQ_FILE_UNCOMPRESSED_SIZE, fileno);
// HACK: in patch.mpq some files don't want to open and give 1 for filesize
if (size<=1) {
eof = true;
buffer = 0;
return;
}
buffer = new char[size];
libmpq_file_getdata(&mpq_a, fileno, (unsigned char*)buffer);
return;
}
eof = true;
buffer = 0;
}
MPQFile::~MPQFile()
{
close();
}
size_t MPQFile::read(void* dest, size_t bytes)
{
if (eof) return 0;
size_t rpos = pointer + bytes;
if (rpos > size) {
bytes = size - pointer;
eof = true;
}
memcpy(dest, &(buffer[pointer]), bytes);
pointer = rpos;
return bytes;
}
bool MPQFile::isEof()
{
return eof;
}
void MPQFile::seek(int offset)
{
pointer = offset;
eof = (pointer >= size);
}
void MPQFile::seekRelative(int offset)
{
pointer += offset;
eof = (pointer >= size);
}
void MPQFile::close()
{
if (buffer) delete[] buffer;
buffer = 0;
eof = true;
}
size_t MPQFile::getSize()
{
return size;
}
size_t MPQFile::getPos()
{
return pointer;
}
char* MPQFile::getBuffer()
{
return buffer;
}
char* MPQFile::getPointer()
{
return buffer + pointer;
}
#if 0
int _id=1;
MPQArchive::MPQArchive(const char* filename)
{
BOOL succ = SFileOpenArchive(filename, _id++, 0, &handle);
if (succ) {
MPQARCHIVE *ar = (MPQARCHIVE*)(handle);
succ = true;
} else {
gLog("Error opening archive %s\n", filename);
}
}
void MPQArchive::close()
{
SFileCloseArchive(handle);
}
MPQFile::MPQFile(const char* filename)
{
BOOL succ = SFileOpenFile(filename, &handle);
pointer = 0;
if (succ) {
DWORD s = SFileGetFileSize(handle, 0);
buffer = new char[s];
SFileReadFile(handle, buffer, s, 0, 0);
SFileCloseFile(handle);
size = (size_t)s;
eof = false;
} else {
eof = true;
buffer = 0;
}
}
size_t MPQFile::read(void* dest, size_t bytes)
{
if (eof) return 0;
size_t rpos = pointer + bytes;
if (rpos > size) {
bytes = size - pointer;
eof = true;
}
memcpy(dest, &(buffer[pointer]), bytes);
pointer = rpos;
return bytes;
}
bool MPQFile::isEof()
{
return eof;
}
void MPQFile::seek(int offset)
{
pointer = offset;
eof = (pointer >= size);
}
void MPQFile::seekRelative(int offset)
{
pointer += offset;
eof = (pointer >= size);
}
void MPQFile::close()
{
if (buffer) delete[] buffer;
buffer = 0;
eof = true;
}
size_t MPQFile::getSize()
{
return size;
}
size_t MPQFile::getPos()
{
return pointer;
}
char* MPQFile::getBuffer()
{
return buffer;
}
char* MPQFile::getPointer()
{
return buffer + pointer;
}
#endif