Menu

[dede95]: / src / FileClasses / Pakfile.cpp  Maximize  Restore  History

Download this file

401 lines (329 with data), 12.4 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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
/*
* This file is part of Dune Legacy.
*
* Dune Legacy is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* Dune Legacy is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Dune Legacy. If not, see <http://www.gnu.org/licenses/>.
*/
#include <FileClasses/Pakfile.h>
#include <misc/exceptions.h>
#include <stdlib.h>
#include <string>
#include <SDL_endian.h>
#include <SDL.h>
/// Constructor for Pakfile
/**
The PAK-File to be read/write is specified by the pakfilename-parameter. If write==false the file is opened for
read-only and is closed in the destructor. If write==true the file is opened write-only and written out in the
destructor.
\param pakfilename Filename of the *.pak-File.
\param write Specified if the PAK-File is opened for reading or writing (default is false).
*/
Pakfile::Pakfile(const std::string& pakfilename, bool write)
: write(write), fPakFile(nullptr), filename(pakfilename), writeOutData(nullptr), numWriteOutData(0) {
if(write == false) {
// Open for reading
if( (fPakFile = SDL_RWFromFile(filename.c_str(), "rb")) == nullptr) {
THROW(std::invalid_argument, "Pakfile::Pakfile(): Cannot open " + pakfilename + "!");
}
try {
readIndex();
} catch (std::exception&) {
SDL_RWclose(fPakFile);
throw;
}
} else {
// Open for writing
if( (fPakFile = SDL_RWFromFile(filename.c_str(), "wb")) == nullptr) {
THROW(std::invalid_argument, "Pakfile::Pakfile(): Cannot open " + pakfilename + "!");
}
}
}
/// Destructor
/**
Closes the filehandle and releases all memory.
*/
Pakfile::~Pakfile()
{
if(write == true) {
// calculate header size
int headersize = 0;
for(unsigned int i = 0; i < fileEntries.size(); i++) {
headersize += 4;
headersize += fileEntries[i].filename.length() + 1;
}
headersize += 4;
// write out header
for(unsigned int i = 0; i < fileEntries.size(); i++) {
#if SDL_BYTEORDER == SDL_BIG_ENDIAN
Uint32 startoffset = SDL_Swap32(fileEntries[i].startOffset + headersize);
#else
Uint32 startoffset = fileEntries[i].startOffset + headersize;
#endif
SDL_RWwrite(fPakFile,(char*) &startoffset,sizeof(Uint32),1);
SDL_RWwrite(fPakFile,fileEntries[i].filename.c_str(), fileEntries[i].filename.length() + 1,1);
}
Uint32 tmp = 0;
SDL_RWwrite(fPakFile,(char*) &tmp, sizeof(Uint32), 1);
// write out data
SDL_RWwrite(fPakFile,writeOutData,numWriteOutData,1);
}
if(fPakFile != nullptr) {
SDL_RWclose(fPakFile);
}
if(writeOutData != nullptr) {
free(writeOutData);
writeOutData = nullptr;
numWriteOutData = 0;
}
}
/// Returns the name of the nth file in this pak-File.
/**
Returns the name of the nth file in this pak-File. index specifies which file.
\param index Index in pak-File
\return name of the file specified by index
*/
const std::string& Pakfile::getFilename(unsigned int index) const {
if(index >= fileEntries.size()) {
THROW(std::invalid_argument, "Pakfile::getFilename(%ud): This Pakfile has only %ud entries!", index, fileEntries.size());
}
return fileEntries[index].filename;
}
/// Adds a file to this PAK-File
/**
This methods adds the SDL_RWop File to this PAK-File. The used name is specified by filename. If the Pakfile
is read-only this method has no effect.
\param rwop Data to add (the SDL_RWop can be read-only but must support seeking)
\param filename This is the filename the data is added with
*/
void Pakfile::addFile(SDL_RWops* rwop, const std::string& filename) {
if(write == false) {
THROW(std::runtime_error, "Pakfile::addFile(): Pakfile is opened for read-only!");
}
if(rwop == nullptr) {
THROW(std::invalid_argument, "Pakfile::addFile(): rwop==nullptr is not allowed!");
}
size_t filelength = static_cast<size_t>(SDL_RWsize(rwop));
char* extendedBuffer;
if((extendedBuffer = (char*) realloc(writeOutData,numWriteOutData+filelength)) == nullptr) {
throw std::bad_alloc();
} else {
writeOutData = extendedBuffer;
}
if(SDL_RWread(rwop,writeOutData + numWriteOutData,1,filelength) != filelength) {
// revert the buffer to the original size
char* shrinkedBuffer;
if((shrinkedBuffer = (char*) realloc(writeOutData,numWriteOutData)) == nullptr) {
// shrinking the buffer should not fail
THROW(std::runtime_error, "Pakfile::addFile(): realloc failed!");
}
writeOutData = shrinkedBuffer;
THROW(std::runtime_error, "Pakfile::addFile(): SDL_RWread failed!");
}
PakFileEntry newPakFileEntry;
newPakFileEntry.startOffset = numWriteOutData;
newPakFileEntry.endOffset = numWriteOutData + filelength - 1;
newPakFileEntry.filename = filename;
fileEntries.push_back(newPakFileEntry);
numWriteOutData += filelength;
SDL_RWseek(rwop,0,SEEK_SET);
}
/// Opens a file in this PAK-File.
/**
This method opens the file specified by filename. It is only allowed if the Pakfile is opened for reading.
The returned SDL_RWops-structure can be used readonly with SDL_RWread, SDL_RWsize, SDL_RWseek and SDL_RWclose. No writing
is supported. If the Pakfile is opened for writing this method returns nullptr.<br>
NOTICE: The returned SDL_RWops-Structure is only valid as long as this Pakfile-Object exists. It gets
invalid as soon as Pakfile:~Pakfile() is executed.
\param filename The name of this file
\return SDL_RWops for this file
*/
SDL_RWops* Pakfile::openFile(const std::string& filename) {
if(write == true) {
// reading files is not allowed
return nullptr;
}
// find file
int index = -1;
for(unsigned int i=0;i<fileEntries.size();i++) {
if(filename == fileEntries[i].filename) {
index = i;
break;
}
}
if(index == -1) {
return nullptr;
}
// alloc RWop
SDL_RWops *pRWop;
if((pRWop = SDL_AllocRW()) == nullptr) {
return nullptr;
}
// alloc RWopData
RWopData* pRWopData = new RWopData();
pRWop->type = PAKFILE_RWOP_TYPE;
pRWopData->curPakfile = this;
pRWopData->fileOffset = 0;
pRWopData->fileIndex = index;
pRWop->read = Pakfile::ReadFile;
pRWop->write = Pakfile::WriteFile;
pRWop->size = Pakfile::SizeFile;
pRWop->seek = Pakfile::SeekFile;
pRWop->close = Pakfile::CloseFile;
pRWop->hidden.unknown.data1 = (void*) pRWopData;
return pRWop;
}
bool Pakfile::exists(const std::string& filename) const {
for(unsigned int i=0;i<fileEntries.size();i++) {
if(filename == fileEntries[i].filename) {
return true;
}
}
return false;
}
size_t Pakfile::ReadFile(SDL_RWops* pRWop, void *ptr, size_t size, size_t n) {
if((pRWop == nullptr) || (ptr == nullptr) || (pRWop->hidden.unknown.data1 == nullptr)
|| (pRWop->type != PAKFILE_RWOP_TYPE)) {
return 0;
}
int bytes2read = size*n;
RWopData* pRWopData = static_cast<RWopData*>(pRWop->hidden.unknown.data1);
Pakfile* pPakfile = pRWopData->curPakfile;
if(pPakfile == nullptr) {
return 0;
}
if(pRWopData->fileIndex >= pPakfile->fileEntries.size()) {
return 0;
}
uint32_t readstartoffset = pPakfile->fileEntries[pRWopData->fileIndex].startOffset + pRWopData->fileOffset;
if(readstartoffset > pPakfile->fileEntries[pRWopData->fileIndex].endOffset) {
return 0;
}
if(readstartoffset + bytes2read > pPakfile->fileEntries[pRWopData->fileIndex].endOffset) {
bytes2read = pPakfile->fileEntries[pRWopData->fileIndex].endOffset + 1 - readstartoffset;
// round to last full block
bytes2read /= size;
bytes2read *= size;
if(bytes2read == 0) {
return 0;
}
}
if(SDL_RWseek(pPakfile->fPakFile,readstartoffset,SEEK_SET) < 0) {
return 0;
}
if(SDL_RWread(pPakfile->fPakFile,ptr,bytes2read,1) != 1) {
return 0;
}
pRWopData->fileOffset += bytes2read;
return bytes2read/size;
}
size_t Pakfile::WriteFile(SDL_RWops *pRWop, const void *ptr, size_t size, size_t n) {
return 0;
}
Sint64 Pakfile::SizeFile(SDL_RWops *pRWop) {
if((pRWop == nullptr) || (pRWop->hidden.unknown.data1 == nullptr)
|| (pRWop->type != PAKFILE_RWOP_TYPE)) {
return -1;
}
RWopData* pRWopData = static_cast<RWopData*>(pRWop->hidden.unknown.data1);
Pakfile* pPakfile = pRWopData->curPakfile;
if(pPakfile == nullptr) {
return -1;
}
if(pRWopData->fileIndex >= pPakfile->fileEntries.size()) {
return -1;
}
return static_cast<Sint64>(pPakfile->fileEntries[pRWopData->fileIndex].endOffset - pPakfile->fileEntries[pRWopData->fileIndex].startOffset + 1);
}
Sint64 Pakfile::SeekFile(SDL_RWops *pRWop, Sint64 offset, int whence) {
if((pRWop == nullptr) || (pRWop->hidden.unknown.data1 == nullptr)
|| (pRWop->type != PAKFILE_RWOP_TYPE)) {
return -1;
}
RWopData* pRWopData = static_cast<RWopData*>(pRWop->hidden.unknown.data1);
Pakfile* pPakfile = pRWopData->curPakfile;
if(pPakfile == nullptr) {
return -1;
}
if(pRWopData->fileIndex >= pPakfile->fileEntries.size()) {
return -1;
}
Sint64 newOffset;
switch(whence) {
case SEEK_SET:
{
newOffset = offset;
} break;
case SEEK_CUR:
{
newOffset = pRWopData->fileOffset + offset;
} break;
case SEEK_END:
{
newOffset = pPakfile->fileEntries[pRWopData->fileIndex].endOffset - pPakfile->fileEntries[pRWopData->fileIndex].startOffset + 1 + offset;
} break;
default:
{
return -1;
} break;
}
if(newOffset > (pPakfile->fileEntries[pRWopData->fileIndex].endOffset - pPakfile->fileEntries[pRWopData->fileIndex].startOffset + 1)) {
return -1;
}
pRWopData->fileOffset = static_cast<size_t>(newOffset);
return newOffset;
}
int Pakfile::CloseFile(SDL_RWops *pRWop) {
if((pRWop == nullptr) || (pRWop->hidden.unknown.data1 == nullptr)
|| (pRWop->type != PAKFILE_RWOP_TYPE)) {
return -1;
}
RWopData* pRWopData = static_cast<RWopData*>(pRWop->hidden.unknown.data1);
delete pRWopData;
pRWop->hidden.unknown.data1 = nullptr;
SDL_FreeRW(pRWop);
return 0;
}
void Pakfile::readIndex()
{
while(1) {
PakFileEntry newEntry;
if(SDL_RWread(fPakFile,(void*) &newEntry.startOffset, sizeof(newEntry.startOffset), 1) != 1) {
THROW(std::runtime_error, "Pakfile::readIndex(): SDL_RWread() failed!");
}
//pak-files are always little endian encoded
newEntry.startOffset = SDL_SwapLE32(newEntry.startOffset);
if(newEntry.startOffset == 0) {
break;
}
while(1) {
char tmp;
if(SDL_RWread(fPakFile,&tmp,1,1) != 1) {
THROW(std::runtime_error, "Pakfile::readIndex(): SDL_RWread() failed!");
}
if(tmp == '\0') {
break;
} else {
newEntry.filename += tmp;
}
}
if(fileEntries.empty() == false) {
fileEntries.back().endOffset = newEntry.startOffset - 1;
}
fileEntries.push_back(newEntry);
}
Sint64 filesize = SDL_RWsize(fPakFile);
if(filesize < 0) {
THROW(std::runtime_error, "Pakfile::readIndex(): SDL_RWsize() failed!");
}
fileEntries.back().endOffset = static_cast<size_t>(filesize) - 1;
}