1 | #include "dbcfile.h" |
---|
2 | #include "mpq.h" |
---|
3 | #include "Stormlib.h" |
---|
4 | #define __STORMLIB_SELF__ |
---|
5 | |
---|
6 | DBCFile::DBCFile(const std::string &filename) : filename(filename) |
---|
7 | { |
---|
8 | data = NULL; |
---|
9 | } |
---|
10 | |
---|
11 | bool DBCFile::open() |
---|
12 | { |
---|
13 | MPQFile f(filename.c_str()); |
---|
14 | |
---|
15 | // Need some error checking, otherwise an unhandled exception error occurs |
---|
16 | // if people screw with the data path. |
---|
17 | if (f.isEof() == true) |
---|
18 | return false; |
---|
19 | |
---|
20 | unsigned char header[4]; |
---|
21 | unsigned int na,nb,es,ss; |
---|
22 | |
---|
23 | f.read(header,4); // File Header |
---|
24 | |
---|
25 | if (header[0]!='W' || header[1]!='D' || header[2]!='B' || header[3] != 'C') { |
---|
26 | |
---|
27 | f.close(); |
---|
28 | data = NULL; |
---|
29 | printf("Critical Error: An error occured while trying to read the DBCFile %s.", filename.c_str()); |
---|
30 | return false; |
---|
31 | } |
---|
32 | |
---|
33 | //assert(header[0]=='W' && header[1]=='D' && header[2]=='B' && header[3] == 'C'); |
---|
34 | |
---|
35 | f.read(&na,4); // Number of records |
---|
36 | f.read(&nb,4); // Number of fields |
---|
37 | f.read(&es,4); // Size of a record |
---|
38 | f.read(&ss,4); // String size |
---|
39 | |
---|
40 | recordSize = es; |
---|
41 | recordCount = na; |
---|
42 | fieldCount = nb; |
---|
43 | stringSize = ss; |
---|
44 | //assert(fieldCount*4 == recordSize); |
---|
45 | assert(fieldCount*4 >= recordSize); |
---|
46 | |
---|
47 | data = new unsigned char[recordSize*recordCount+stringSize]; |
---|
48 | stringTable = data + recordSize*recordCount; |
---|
49 | f.read(data,recordSize*recordCount+stringSize); |
---|
50 | f.close(); |
---|
51 | return true; |
---|
52 | } |
---|
53 | |
---|
54 | DBCFile::~DBCFile() |
---|
55 | { |
---|
56 | delete [] data; |
---|
57 | } |
---|
58 | |
---|
59 | DBCFile::Record DBCFile::getRecord(size_t id) |
---|
60 | { |
---|
61 | assert(data); |
---|
62 | return Record(*this, data + id*recordSize); |
---|
63 | } |
---|
64 | |
---|
65 | DBCFile::Iterator DBCFile::begin() |
---|
66 | { |
---|
67 | assert(data); |
---|
68 | return Iterator(*this, data); |
---|
69 | } |
---|
70 | DBCFile::Iterator DBCFile::end() |
---|
71 | { |
---|
72 | assert(data); |
---|
73 | return Iterator(*this, stringTable); |
---|
74 | } |
---|
75 | |
---|