added support for a ton of stuff, fixed lots
[riven-wahrk.git] / src / Mohawk.cpp
blob31596498247f2f3c0978f30b2f3a9cd16b8c607f
2 #include <iostream>
3 #include <assert.h>
5 using namespace std;
7 #include "Mohawk.h"
9 /*********** FileTable ***********/
11 FileTable::FileTable (uint8_t* mohawkIn, uint8_t* tableIn) {
12 mohawk = mohawkIn;
13 table = tableIn;
14 count = Common::swapBytes (*(uint32_t*) table);
17 uint32_t FileTable::getSize (int index) {
18 if (index == -1)
19 return NULL;
21 index--;
22 assert ((uint32_t) index < count);
24 uint32_t size = *(uint32_t*) (table + 4 + (index * 10) + 4);
25 return ((size & 0xff0000) | ((size & 0xff) << 8) | ((size & 0xff00) >> 8));
29 uint8_t *FileTable::getPointer (int index) {
30 if (index == -1)
31 return NULL;
32 index--;
33 assert ((uint32_t) index < count);
35 return mohawk + Common::swapBytes (*(uint32_t*) (table + 4 + (index * 10)));
38 void FileTable::printTable () {
39 uint32_t i;
40 for (i=1; i<count; i++) {
41 cout << "file " << i << std::hex << " " << (uint64_t) getPointer (i) << " " << getSize (i) << endl;
45 /************ Resource **********/
48 #define shift(a, b, c, d) (((d) << 24) | ((c) << 16) | ((b) << 8) | (a))
50 // this must align with Resource::Type
51 static const uint32_t types[RESOURCE_COUNT] = {
52 shift ('B', 'L', 'S', 'T'),
53 shift ('C', 'A', 'R', 'D'),
54 shift ('F', 'L', 'S', 'T'),
55 shift ('H', 'S', 'P', 'T'),
56 shift ('M', 'L', 'S', 'T'),
57 shift ('N', 'A', 'M', 'E'),
58 shift ('P', 'L', 'S', 'T'),
59 shift ('R', 'M', 'A', 'P'),
60 shift ('S', 'F', 'X', 'E'),
61 shift ('S', 'L', 'S', 'T'),
62 shift ('V', 'A', 'R', 'S'),
63 shift ('V', 'E', 'R', 'S'),
64 shift ('Z', 'I', 'P', 'S'),
65 shift ('t', 'B', 'M', 'P'),
66 shift ('t', 'M', 'O', 'V'),
67 shift ('t', 'W', 'A', 'V') };
69 #undef shift
71 Resource::Resource (uint8_t *resourceDirIn, Type typeIn) {
72 resourceDir = resourceDirIn;
73 type = typeIn;
75 int typeCount = Common::swapBytes (*(uint16_t*) (resourceDir + 2));
76 int i;
77 for (i=0; i<typeCount; i++) {
78 if (*(uint32_t*) (resourceDir + 4 + i*8) == types[type]) {
79 resourceTable = resourceDir + Common::swapBytes (*(uint16_t*) (resourceDir + 4 + i*8 + 4));
81 count = Common::swapBytes (*(uint16_t*) (resourceTable));
83 return;
87 cout << "Mohawk: resource type " << type << " was not found\n";
88 resourceTable = 0;
89 count = 0;
93 int Resource::getFileIndex (int id) {
94 int i;
95 for (i=0; i<count; i++)
96 if (id == Common::swapBytes (*(uint16_t*) (resourceTable + 2 + 4*i)))
97 return Common::swapBytes (*(uint16_t*) (resourceTable + 2 + 4*i + 2));
100 return -1;
104 /************ Mohawk ***********/
106 Mohawk::Mohawk (string fileName) {
108 int handle = open (fileName.c_str (), O_RDONLY);
109 int size = Common::getFileSize (handle);
110 size -= size % 4096;
111 size += 4096;
112 file = (uint8_t*) mmap (NULL, size, PROT_READ, MAP_PRIVATE, handle, 0);
114 if (*(uint32_t*) file != 0x4b57484d) // MHWK
115 return;
117 resourceDir = file + Common::swapBytes (*(uint32_t*) (file + 20));
118 fileTable = new FileTable (file+0, resourceDir + Common::swapBytes (*(uint16_t*) (file + 24)));
120 int i;
121 for (i=0; i<RESOURCE_COUNT; i++)
122 resource[i] = (Resource*) NULL;
127 uint8_t* Mohawk::getFile (Resource::Type type, int id) {
129 if (resource[type] == NULL)
130 resource[type] = new Resource (resourceDir, type);
132 return fileTable->getPointer (resource[type]->getFileIndex (id));
137 int Mohawk::getSize (Resource::Type type, int id) {
139 if (resource[type] == NULL)
140 resource[type] = new Resource (resourceDir, type);
142 return fileTable->getSize (resource[type]->getFileIndex (id));