PageRenderTime 710ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/engines/tsage/resources.cpp

https://github.com/St0rmcrow/scummvm
C++ | 501 lines | 347 code | 92 blank | 62 comment | 86 complexity | 68dd212b113bb32600cc08989fbe5176 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1
  1. /* ScummVM - Graphic Adventure Engine
  2. *
  3. * ScummVM is the legal property of its developers, whose names
  4. * are too numerous to list here. Please refer to the COPYRIGHT
  5. * file distributed with this source distribution.
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License
  9. * as published by the Free Software Foundation; either version 2
  10. * of the License, or (at your option) any later version.
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. *
  19. * $URL$
  20. * $Id$
  21. *
  22. */
  23. #include "common/scummsys.h"
  24. #include "common/endian.h"
  25. #include "common/file.h"
  26. #include "common/stack.h"
  27. #include "common/util.h"
  28. #include "tsage/resources.h"
  29. #include "tsage/tsage.h"
  30. namespace tSage {
  31. MemoryManager::MemoryManager() {
  32. _memoryPool = new MemoryHeader*[MEMORY_POOL_SIZE];
  33. Common::set_to(&_memoryPool[0], &_memoryPool[MEMORY_POOL_SIZE], (MemoryHeader *)NULL);
  34. }
  35. MemoryManager::~MemoryManager() {
  36. for (int i = 0; i < MEMORY_POOL_SIZE; ++i) {
  37. if (_memoryPool[i] != NULL)
  38. free(_memoryPool[i]);
  39. }
  40. delete[] _memoryPool;
  41. }
  42. uint16 MemoryManager::allocate(uint32 size) {
  43. int idx = 0;
  44. while ((idx < MEMORY_POOL_SIZE) && (_memoryPool[idx] != NULL))
  45. ++idx;
  46. if (idx == MEMORY_POOL_SIZE)
  47. error("Out of memory handles");
  48. // Create the new entry
  49. _memoryPool[idx] = (MemoryHeader *)malloc(sizeof(MemoryHeader) + size);
  50. _memoryPool[idx]->id = MEMORY_ENTRY_ID;
  51. _memoryPool[idx]->index = idx;
  52. _memoryPool[idx]->lockCtr = 0;
  53. _memoryPool[idx]->criticalCtr = 0;
  54. _memoryPool[idx]->tag = 0;
  55. _memoryPool[idx]->size = size;
  56. // Return it's index
  57. return idx;
  58. }
  59. byte *MemoryManager::allocate2(uint32 size) {
  60. uint32 idx = allocate(size);
  61. return lock(idx);
  62. }
  63. byte *MemoryManager::lock(uint32 handle) {
  64. assert((int)handle < MEMORY_POOL_SIZE);
  65. return (byte *)_memoryPool[handle] + sizeof(MemoryHeader);
  66. }
  67. int MemoryManager::indexOf(const byte *p) {
  68. for (int idx = 0; idx < MEMORY_POOL_SIZE; ++idx) {
  69. if (((byte *)_memoryPool[idx] + sizeof(MemoryHeader)) == p)
  70. return idx;
  71. }
  72. return -1;
  73. }
  74. void MemoryManager::deallocate(const byte *p) {
  75. if (!p)
  76. return;
  77. int idx = indexOf(p);
  78. assert(idx != -1);
  79. if (_memoryPool[idx]->lockCtr-- == 0) {
  80. free(_memoryPool[idx]);
  81. _memoryPool[idx] = NULL;
  82. }
  83. }
  84. uint32 MemoryManager::getSize(const byte *p) {
  85. int idx = indexOf(p);
  86. assert(idx >= 0);
  87. return _memoryPool[idx]->size;
  88. }
  89. void MemoryManager::incLocks(const byte *p) {
  90. int idx = indexOf(p);
  91. assert(idx >= 0);
  92. _memoryPool[idx]->lockCtr++;
  93. }
  94. /*-------------------------------------------------------------------------*/
  95. static uint16 bitMasks[4] = {0x1ff, 0x3ff, 0x7ff, 0xfff};
  96. uint16 BitReader::readToken() {
  97. assert((numBits >= 9) && (numBits <= 12));
  98. uint16 result = _remainder;
  99. int bitsLeft = numBits - _bitsLeft;
  100. int bitOffset = _bitsLeft;
  101. _bitsLeft = 0;
  102. while (bitsLeft >= 0) {
  103. _remainder = readByte();
  104. result |= _remainder << bitOffset;
  105. bitsLeft -= 8;
  106. bitOffset += 8;
  107. }
  108. _bitsLeft = -bitsLeft;
  109. _remainder >>= 8 - _bitsLeft;
  110. return result & bitMasks[numBits - 9];
  111. }
  112. /*-------------------------------------------------------------------------*/
  113. TLib::TLib(MemoryManager &memManager, const Common::String &filename) :
  114. _memoryManager(memManager) {
  115. // If the resource strings list isn't yet loaded, load them
  116. if (_resStrings.size() == 0) {
  117. Common::File f;
  118. if (f.open("tsage.cfg")) {
  119. while (!f.eos()) {
  120. _resStrings.push_back(f.readLine());
  121. }
  122. f.close();
  123. }
  124. }
  125. if (!_file.open(filename))
  126. error("Missing file %s", filename.c_str());
  127. loadIndex();
  128. }
  129. TLib::~TLib() {
  130. _resStrings.clear();
  131. }
  132. void TLib::loadSection(uint32 fileOffset) {
  133. _resources.clear();
  134. _file.seek(fileOffset);
  135. _sections.fileOffset = fileOffset;
  136. if (_file.readUint32BE() != 0x544D492D)
  137. error("Data block is not valid Rlb data");
  138. /*uint8 unknown1 = */_file.readByte();
  139. uint16 numEntries = _file.readByte();
  140. for (uint i = 0; i < numEntries; ++i) {
  141. uint16 id = _file.readUint16LE();
  142. uint16 size = _file.readUint16LE();
  143. uint16 uncSize = _file.readUint16LE();
  144. uint8 sizeHi = _file.readByte();
  145. uint8 type = _file.readByte() >> 5;
  146. assert(type <= 1);
  147. uint32 offset = _file.readUint32LE();
  148. ResourceEntry re;
  149. re.id = id;
  150. re.fileOffset = offset;
  151. re.isCompressed = type != 0;
  152. re.size = ((sizeHi & 0xF) << 16) | size;
  153. re.uncompressedSize = ((sizeHi & 0xF0) << 12) | uncSize;
  154. _resources.push_back(re);
  155. }
  156. }
  157. struct DecodeReference {
  158. uint16 vWord;
  159. uint8 vByte;
  160. };
  161. /**
  162. * Gets a resource from the currently loaded section
  163. */
  164. byte *TLib::getResource(uint16 id, bool suppressErrors) {
  165. // Scan for an entry for the given Id
  166. ResourceEntry *re = NULL;
  167. ResourceList::iterator iter;
  168. for (iter = _resources.begin(); iter != _resources.end(); ++iter) {
  169. if ((*iter).id == id) {
  170. re = &(*iter);
  171. break;
  172. }
  173. }
  174. if (!re) {
  175. if (suppressErrors)
  176. return NULL;
  177. error("Could not find resource Id #%d", id);
  178. }
  179. if (!re->isCompressed) {
  180. // Read in the resource data and return it
  181. byte *dataP = _memoryManager.allocate2(re->size);
  182. _file.seek(_sections.fileOffset + re->fileOffset);
  183. _file.read(dataP, re->size);
  184. return dataP;
  185. }
  186. /*
  187. * Decompress the data block
  188. */
  189. _file.seek(_sections.fileOffset + re->fileOffset);
  190. Common::ReadStream *compStream = _file.readStream(re->size);
  191. BitReader bitReader(*compStream);
  192. byte *dataOut = _memoryManager.allocate2(re->uncompressedSize);
  193. byte *destP = dataOut;
  194. uint bytesWritten = 0;
  195. uint16 ctrCurrent = 0x102, ctrMax = 0x200;
  196. uint16 word_48050 = 0, currentToken = 0, word_48054 =0;
  197. byte byte_49068 = 0, byte_49069 = 0;
  198. DecodeReference table[0x1000];
  199. for (int i = 0; i < 0x1000; ++i) {
  200. table[i].vByte = table[i].vWord = 0;
  201. }
  202. Common::Stack<uint16> tokenList;
  203. for (;;) {
  204. // Get the next decode token
  205. uint16 token = bitReader.readToken();
  206. // Handle the token
  207. if (token == 0x101) {
  208. // End of compressed stream
  209. break;
  210. } else if (token == 0x100) {
  211. // Reset bit-rate
  212. bitReader.numBits = 9;
  213. ctrMax = 0x200;
  214. ctrCurrent = 0x102;
  215. // Set variables with next token
  216. currentToken = word_48050 = bitReader.readToken();
  217. byte_49069 = byte_49068 = (byte)currentToken;
  218. ++bytesWritten;
  219. assert(bytesWritten <= re->uncompressedSize);
  220. *destP++ = byte_49069;
  221. } else {
  222. word_48054 = word_48050 = token;
  223. if (token >= ctrCurrent) {
  224. word_48050 = currentToken;
  225. tokenList.push(byte_49068);
  226. }
  227. while (word_48050 >= 0x100) {
  228. assert(word_48050 < 0x1000);
  229. tokenList.push(table[word_48050].vByte);
  230. word_48050 = table[word_48050].vWord;
  231. }
  232. byte_49069 = byte_49068 = (byte)word_48050;
  233. tokenList.push(word_48050);
  234. // Write out any cached tokens
  235. while (!tokenList.empty()) {
  236. ++bytesWritten;
  237. assert(bytesWritten <= re->uncompressedSize);
  238. *destP++ = tokenList.pop();
  239. }
  240. assert(ctrCurrent < 0x1000);
  241. table[ctrCurrent].vByte = byte_49069;
  242. table[ctrCurrent].vWord = currentToken;
  243. ++ctrCurrent;
  244. currentToken = word_48054;
  245. if ((ctrCurrent >= ctrMax) && (bitReader.numBits != 12)) {
  246. // Move to the next higher bit-rate
  247. ++bitReader.numBits;
  248. ctrMax <<= 1;
  249. }
  250. }
  251. }
  252. assert(bytesWritten == re->uncompressedSize);
  253. delete compStream;
  254. return dataOut;
  255. }
  256. /**
  257. * Finds the correct section and loads the specified resource within it
  258. */
  259. byte *TLib::getResource(ResourceType resType, uint16 resNum, uint16 rlbNum, bool suppressErrors) {
  260. SectionList::iterator i = _sections.begin();
  261. while ((i != _sections.end()) && ((*i).resType != resType || (*i).resNum != resNum))
  262. ++i;
  263. if (i == _sections.end()) {
  264. if (suppressErrors)
  265. return NULL;
  266. error("Unknown resource type %d num %d", resType, resNum);
  267. }
  268. loadSection((*i).fileOffset);
  269. return getResource(rlbNum, suppressErrors);
  270. }
  271. void TLib::loadIndex() {
  272. uint16 resNum, configId, fileOffset;
  273. // Load the root resources section
  274. loadSection(0);
  275. // Get the single resource from it
  276. const byte *pData = getResource(0);
  277. const byte *p = pData;
  278. _sections.clear();
  279. // Loop through reading the entries
  280. while ((resNum = READ_LE_UINT16(p)) != 0xffff) {
  281. configId = READ_LE_UINT16(p + 2);
  282. fileOffset = READ_LE_UINT16(p + 4);
  283. p += 6;
  284. SectionEntry se;
  285. se.resNum = resNum;
  286. se.resType = (ResourceType)(configId & 0x1f);
  287. se.fileOffset = (((configId >> 5) & 0x7ff) << 16) | fileOffset;
  288. _sections.push_back(se);
  289. }
  290. _memoryManager.deallocate(pData);
  291. }
  292. /**
  293. * Retrieves the specified palette resource and returns it's data
  294. *
  295. * @paletteNum Specefies the palette number
  296. */
  297. bool TLib::getPalette(int paletteNum, byte *palData, uint *startNum, uint *numEntries) {
  298. // Get the specified palette
  299. byte *dataIn = getResource(RES_PALETTE, 0, paletteNum, true);
  300. if (!dataIn)
  301. return false;
  302. *startNum = READ_LE_UINT16(dataIn);
  303. *numEntries = READ_LE_UINT16(dataIn + 2);
  304. assert((*startNum < 256) && ((*startNum + *numEntries) <= 256));
  305. // Copy over the data
  306. Common::copy(&dataIn[6], &dataIn[6 + *numEntries * 3], palData);
  307. _memoryManager.deallocate(dataIn);
  308. return true;
  309. }
  310. byte *TLib::getSubResource(int resNum, int rlbNum, int index, uint *size, bool suppressErrors) {
  311. // Get the specified image set
  312. byte *dataIn = getResource(RES_VISAGE, resNum, rlbNum);
  313. if (!dataIn) {
  314. if (suppressErrors)
  315. return NULL;
  316. error("Unknown sub resource %d/%d index %d", resNum, rlbNum, index);
  317. }
  318. int numEntries = READ_LE_UINT16(dataIn);
  319. uint32 entryOffset = READ_LE_UINT32(dataIn + 2 + (index - 1) * 4);
  320. uint32 nextOffset = (index == numEntries) ?
  321. _memoryManager.getSize(dataIn) : READ_LE_UINT32(dataIn + 2 + index * 4);
  322. *size = nextOffset - entryOffset;
  323. assert(*size < (1024 * 1024));
  324. byte *entry = _memoryManager.allocate2(*size);
  325. Common::copy(&dataIn[entryOffset], &dataIn[nextOffset], entry);
  326. _memoryManager.deallocate(dataIn);
  327. return entry;
  328. }
  329. /**
  330. * Retrieves a given message resource, and returns the specified message number
  331. */
  332. bool TLib::getMessage(int resNum, int lineNum, Common::String &result, bool suppressErrors) {
  333. byte *msgData = getResource(RES_MESSAGE, resNum, 0, true);
  334. if (!msgData) {
  335. if (suppressErrors)
  336. return false;
  337. error("Unknown message %d line %d", resNum, lineNum);
  338. }
  339. const char *srcP = (const char *)msgData;
  340. while (lineNum-- > 0)
  341. srcP += strlen(srcP) + 1;
  342. result = Common::String(srcP);
  343. _memoryManager.deallocate(msgData);
  344. return true;
  345. }
  346. /*--------------------------------------------------------------------------*/
  347. ResourceManager::~ResourceManager() {
  348. for (uint idx = 0; idx < _libList.size(); ++idx)
  349. delete _libList[idx];
  350. }
  351. void ResourceManager::addLib(const Common::String &libName) {
  352. assert(_libList.size() < 5);
  353. _libList.push_back(new TLib(_vm->_memoryManager, libName));
  354. }
  355. byte *ResourceManager::getResource(uint16 id, bool suppressErrors) {
  356. byte *result = NULL;
  357. for (uint idx = 0; idx < _libList.size(); ++idx) {
  358. result = _libList[idx]->getResource(id, true);
  359. if (result)
  360. return result;
  361. }
  362. if (!result && !suppressErrors)
  363. error("Could not find resource Id #%d", id);
  364. return NULL;
  365. }
  366. byte *ResourceManager::getResource(ResourceType resType, uint16 resNum, uint16 rlbNum, bool suppressErrors) {
  367. byte *result = NULL;
  368. for (uint idx = 0; idx < _libList.size(); ++idx) {
  369. result = _libList[idx]->getResource(resType, resNum, rlbNum, true);
  370. if (result)
  371. return result;
  372. }
  373. if (!result && !suppressErrors)
  374. error("Unknown resource type %d num %d", resType, resNum);
  375. return NULL;
  376. }
  377. void ResourceManager::getPalette(int paletteNum, byte *palData, uint *startNum, uint *numEntries, bool suppressErrors) {
  378. for (uint idx = 0; idx < _libList.size(); ++idx) {
  379. if (_libList[idx]->getPalette(paletteNum, palData, startNum, numEntries))
  380. return;
  381. }
  382. if (!suppressErrors)
  383. error("Unknown palette resource %d", paletteNum);
  384. *numEntries = 0;
  385. }
  386. byte *ResourceManager::getSubResource(int resNum, int rlbNum, int index, uint *size, bool suppressErrors) {
  387. byte *result = NULL;
  388. for (uint idx = 0; idx < _libList.size(); ++idx) {
  389. result = _libList[idx]->getSubResource(resNum, rlbNum, index, size, true);
  390. if (result)
  391. return result;
  392. }
  393. if (!result && !suppressErrors)
  394. error("Unknown resource %d/%d index %d", resNum, rlbNum, index);
  395. return NULL;
  396. }
  397. Common::String ResourceManager::getMessage(int resNum, int lineNum, bool suppressErrors) {
  398. Common::String result;
  399. for (uint idx = 0; idx < _libList.size(); ++idx) {
  400. if (_libList[idx]->getMessage(resNum, lineNum, result, true))
  401. return result;
  402. }
  403. if (!suppressErrors)
  404. error("Unknown message %d line %d", resNum, lineNum);
  405. return result;
  406. }
  407. } // end of namespace tSage