This repository has been archived by the owner on Nov 7, 2023. It is now read-only.
/
ResourceFile.cpp
333 lines (262 loc) · 8.57 KB
/
ResourceFile.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
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
#include "ResourceFile.h"
void swapEndianness(char * bytes)
{
char swap;
swap = bytes[0];
bytes[0] = bytes[3];
bytes[3] = swap;
swap = bytes[1];
bytes[1] = bytes[2];
bytes[2] = swap;
}
void convertToLittleEndian(char * bytes)
{
// if the host is big-endian, swap the bits around
if(O32_HOST_ORDER == O32_BIG_ENDIAN)
{
swapEndianness(bytes);
}
}
void convertLittleEndianToNative(char * bytes)
{
// has the same exact function as convertToLittleEndian, but is replicated for clarity
if(O32_HOST_ORDER == O32_BIG_ENDIAN)
{
swapEndianness(bytes);
}
}
void arrayCopy(char * source, char * destination, int byteCount, int sourceOffset = 0, int destinationOffset = 0)
{
for(int i = 0; i < byteCount; i++)
{
destination[sourceOffset + i] = source[destinationOffset + i];
}
}
union intComponents
{
// allows direct access to the bytes of an int
unsigned int number;
char bytes[4];
};
bool fileExists(const string &fileName)
{
ifstream file(fileName.c_str(), ifstream::in);
bool exists = file.is_open();
file.close();
return exists;
}
int getFileSize(const string &fileName)
{
ifstream file(fileName.c_str(), ifstream::in | ifstream::binary);
if(!file.is_open())
{
return -1;
}
file.seekg(0, ios::end);
int fileSize = (int)file.tellg();
file.close();
return fileSize;
}
ResourceFileEntry::ResourceFileEntry(string name, string filePath, unsigned int fileSize)
{
myName = name;
myFilePath = filePath;
myFileSize = fileSize;
myOffset = -1;
}
string ResourceFileEntry::name()
{
return myName;
}
string ResourceFileEntry::filePath()
{
return myFilePath;
}
unsigned int ResourceFileEntry::fileSize()
{
return myFileSize;
}
unsigned int ResourceFileEntry::offset()
{
return myOffset;
}
void ResourceFileEntry::setOffset(unsigned int offset)
{
myOffset = offset;
}
// magic word is 10E1A44A = JOELANNA :)
unsigned int ResourceFileBase::magicWord = 1252319504u;
ResourceFileBase::ResourceFileBase(string filePath)
{
myFilePath = filePath;
myReady = false;
}
vector<string> ResourceFileBase::getEntryNames()
{
vector<string> entryNamesList;
for(map<string, ResourceFileEntry *>::iterator i = myEntries.begin(); i != myEntries.end(); i++)
{
entryNamesList.push_back((*i).first);
}
return entryNamesList;
}
ResourceFileWriter::ResourceFileWriter(string filePath) : ResourceFileBase(filePath)
{
}
bool ResourceFileWriter::write()
{
ofstream outputFile(myFilePath.c_str(), ios::out | ios::binary);
if(!outputFile.is_open()) // make sure the file is open
{
return false;
}
int headerSize = getHeaderSize(); // calculate how many bytes the header will be
// write the header information
writeNumber(outputFile, magicWord);
writeNumber(outputFile, headerSize);
writeNumber(outputFile, myEntries.size());
// write the file table
int currentOffset = headerSize;
for(map<string, ResourceFileEntry *>::iterator i = myEntries.begin(); i != myEntries.end(); i++)
{
ResourceFileEntry * entry = (*i).second;
entry->setOffset(currentOffset);
outputFile << entry->name() << '\0';
writeNumber(outputFile, entry->fileSize());
writeNumber(outputFile, currentOffset);
currentOffset += entry->fileSize();
}
// input all source files
ifstream inputFile;
for(map<string, ResourceFileEntry *>::iterator i = myEntries.begin(); i != myEntries.end(); i++)
{
ResourceFileEntry * entry = (*i).second;
inputFile.open(entry->filePath().c_str(), ios::in | ios::binary);
outputFile << inputFile.rdbuf();
inputFile.close();
}
outputFile.close();
return true;
}
bool ResourceFileWriter::addEntry(string entryName, string entryFilePath)
{
if(!fileExists(entryFilePath)) // make sure the input file exists
{
return false;
}
if(myEntries.find(entryName) != myEntries.end()) // make sure the name is unique
{
return false;
}
// create the entry
ResourceFileEntry * entry = new ResourceFileEntry(entryName, entryFilePath, getFileSize(entryFilePath));
// add the entry to the map
myEntries[entryName] = entry;
return true;
}
int ResourceFileWriter::getHeaderSize()
{
// the magic word, header size, and the number of files
int headerSize = 3 * sizeof(int);
for(map<string, ResourceFileEntry *>::iterator i = myEntries.begin(); i != myEntries.end(); i++)
{
ResourceFileEntry * entry = (*i).second;
// the number of characters in the name plus a null character
headerSize += entry->name().size() + 1;
// the file size and the offset
headerSize += 2 * sizeof(int);
}
return headerSize;
}
void ResourceFileWriter::writeNumber(ofstream &outputFile, unsigned int number)
{
// convert the number to little-endian
intComponents numberComponents;
numberComponents.number = number;
convertToLittleEndian(&numberComponents.bytes[0]);
// dump the number into the output stream
outputFile << numberComponents.bytes[0];
outputFile << numberComponents.bytes[1];
outputFile << numberComponents.bytes[2];
outputFile << numberComponents.bytes[3];
}
ResourceFileReader::ResourceFileReader(string filePath) : ResourceFileBase(filePath)
{
}
bool ResourceFileReader::read()
{
ifstream inputFile(myFilePath.c_str(), ios::in | ios::binary);
if(!inputFile.is_open()) // make sure the file is open
{
return false;
}
// read in the first four bytes, where the magic word should be
intComponents currentMagicWord;
currentMagicWord.number = 0;
inputFile.read(currentMagicWord.bytes, 4);
convertLittleEndianToNative(currentMagicWord.bytes);
if(currentMagicWord.number != magicWord) // make sure the magic word matches
{
return false;
}
// read in the header size and convert from little-endian
intComponents headerSize;
headerSize.number = 0;
inputFile.read(headerSize.bytes, 4);
convertLittleEndianToNative(headerSize.bytes);
// read in the file count and convert from little-endian
intComponents fileCount;
fileCount.number = 0;
inputFile.read(fileCount.bytes, 4);
convertLittleEndianToNative(fileCount.bytes);
// read the entire file table block into memory
int fileTableSize = headerSize.number - 3 * sizeof(int);
char * headerData = new char[fileTableSize];
inputFile.read(headerData, fileTableSize);
// parse the file table
int fileTableOffset = 0;
unsigned int fileIndex = 0;
while(true)
{
string name(headerData + fileTableOffset);
fileTableOffset += name.size() + 1;
// read in the entry's file size
intComponents fileSize;
fileSize.number = 0;
arrayCopy(headerData + fileTableOffset, fileSize.bytes, sizeof(int));
convertLittleEndianToNative(fileSize.bytes);
// read in the entry's offset
intComponents offset;
offset.number = 0;
arrayCopy(headerData + fileTableOffset + sizeof(int), offset.bytes, sizeof(int));
convertLittleEndianToNative(offset.bytes);
fileTableOffset += 2 * sizeof(int);
// add the entry to the entry list
ResourceFileEntry * entry = new ResourceFileEntry(name, "", fileSize.number);
entry->setOffset(offset.number);
myEntries[name] = entry;
fileIndex++;
if(fileIndex == fileCount.number || fileTableOffset >= fileTableSize) // if we've found enough files or we're at the end of the file table
{
break;
}
}
delete headerData;
return true;
}
unsigned int ResourceFileReader::getEntryFileSize(const string &entryName)
{
return myEntries[entryName]->fileSize();
}
char * ResourceFileReader::getFile(const string &entryName)
{
ResourceFileEntry * entry = myEntries[entryName];
// seek to the beginning of the file we want
ifstream inputFile(myFilePath.c_str(), ifstream::in | ifstream::binary);
inputFile.seekg(entry->offset(), ios::beg);
// read the entire file into memory
char * fileData = new char[entry->fileSize()];
inputFile.read(fileData, entry->fileSize());
inputFile.close();
return fileData;
}