9 /*********** FileTable ***********/
11 FileTable::FileTable (uint8_t* mohawkIn
, uint8_t* tableIn
) {
14 count
= Common::swapBytes (*(uint32_t*) table
);
17 uint32_t FileTable::getSize (int 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
) {
33 assert ((uint32_t) index
< count
);
35 return mohawk
+ Common::swapBytes (*(uint32_t*) (table
+ 4 + (index
* 10)));
38 void FileTable::printTable () {
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') };
71 Resource::Resource (uint8_t *resourceDirIn
, Type typeIn
) {
72 resourceDir
= resourceDirIn
;
75 int typeCount
= Common::swapBytes (*(uint16_t*) (resourceDir
+ 2));
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
));
87 cout
<< "Mohawk: resource type " << type
<< " was not found\n";
93 int Resource::getFileIndex (int id
) {
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));
104 /************ Mohawk ***********/
106 Mohawk::Mohawk (string fileName
) {
108 int handle
= open (fileName
.c_str (), O_RDONLY
);
109 int size
= Common::getFileSize (handle
);
112 file
= (uint8_t*) mmap (NULL
, size
, PROT_READ
, MAP_PRIVATE
, handle
, 0);
114 if (*(uint32_t*) file
!= 0x4b57484d) // MHWK
117 resourceDir
= file
+ Common::swapBytes (*(uint32_t*) (file
+ 20));
118 fileTable
= new FileTable (file
+0, resourceDir
+ Common::swapBytes (*(uint16_t*) (file
+ 24)));
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
));