From 69b1485a22dc2b8a2cfe0bd10edcbaad0da0cf6e Mon Sep 17 00:00:00 2001 From: strangerke Date: Thu, 12 May 2011 01:13:57 +0200 Subject: GIT: Clean up: Suppress SVN tags, now useless --- engines/scumm/resource.cpp | 3 --- 1 file changed, 3 deletions(-) (limited to 'engines/scumm/resource.cpp') diff --git a/engines/scumm/resource.cpp b/engines/scumm/resource.cpp index c872a83d14..164b671c97 100644 --- a/engines/scumm/resource.cpp +++ b/engines/scumm/resource.cpp @@ -18,9 +18,6 @@ * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * - * $URL$ - * $Id$ - * */ #include "common/str.h" -- cgit v1.2.3 From 7773a17b0a1b1e7488490e96cba361ac060c0dd7 Mon Sep 17 00:00:00 2001 From: Max Horn Date: Wed, 11 May 2011 15:37:00 +0200 Subject: SCUMM: Unify & extend index block related debug messages --- engines/scumm/resource.cpp | 18 ++++-------------- 1 file changed, 4 insertions(+), 14 deletions(-) (limited to 'engines/scumm/resource.cpp') diff --git a/engines/scumm/resource.cpp b/engines/scumm/resource.cpp index 164b671c97..ac238abae5 100644 --- a/engines/scumm/resource.cpp +++ b/engines/scumm/resource.cpp @@ -164,8 +164,6 @@ void ScummEngine::deleteRoomOffsets() { /** Read room offsets */ void ScummEngine::readRoomsOffsets() { - debug(9, "readRoomOffsets()"); - if (_game.features & GF_SMALL_HEADER) { _fileHandle->seek(12, SEEK_SET); // Directly searching for the room offset block would be more generic... } else { @@ -300,6 +298,7 @@ void ScummEngine::readIndexFile() { break; numblock++; + debug(2, "Reading index block of type '%s', size %d", tag2str(blocktype), itemsize); readIndexBlock(blocktype, itemsize); } @@ -349,7 +348,6 @@ void ScummEngine_v7::readIndexBlock(uint32 blocktype, uint32 itemsize) { char *ptr; switch (blocktype) { case MKTAG('A','N','A','M'): // Used by: The Dig, FT - debug(9, "found ANAM block, reading audio names"); num = _fileHandle->readUint16LE(); ptr = (char*)malloc(num * 9); _fileHandle->read(ptr, num * 9); @@ -418,7 +416,6 @@ void ScummEngine::readIndexBlock(uint32 blocktype, uint32 itemsize) { break; case MKTAG('D','O','B','J'): - debug(9, "found DOBJ block, reading object table"); readGlobalObjects(); break; @@ -489,8 +486,6 @@ int ScummEngine::readResTypeList(int id) { int num; int i; - debug(9, "readResTypeList(%s)", resTypeFromId(id)); - if (_game.version == 8) num = _fileHandle->readUint32LE(); else @@ -500,6 +495,9 @@ int ScummEngine::readResTypeList(int id) { error("Invalid number of %ss (%d) in directory", resTypeFromId(id), num); } + debug(2, " readResTypeList(%s): %d entries", resTypeFromId(id), num); + + for (i = 0; i < num; i++) { _res->roomno[id][i] = _fileHandle->readByte(); } @@ -1066,8 +1064,6 @@ void ResourceManager::resourceStats() { } void ScummEngine_v5::readMAXS(int blockSize) { - debug(9, "ScummEngine_v5 readMAXS: MAXS has blocksize %d", blockSize); - _numVariables = _fileHandle->readUint16LE(); // 800 _fileHandle->readUint16LE(); // 16 _numBitVariables = _fileHandle->readUint16LE(); // 2048 @@ -1096,8 +1092,6 @@ void ScummEngine_v5::readMAXS(int blockSize) { #ifdef ENABLE_SCUMM_7_8 void ScummEngine_v8::readMAXS(int blockSize) { - debug(9, "ScummEngine_v8 readMAXS: MAXS has blocksize %d", blockSize); - _fileHandle->seek(50, SEEK_CUR); // Skip over SCUMM engine version _fileHandle->seek(50, SEEK_CUR); // Skip over data file version _numVariables = _fileHandle->readUint32LE(); // 1500 @@ -1126,8 +1120,6 @@ void ScummEngine_v8::readMAXS(int blockSize) { } void ScummEngine_v7::readMAXS(int blockSize) { - debug(9, "ScummEngine_v7 readMAXS: MAXS has blocksize %d", blockSize); - _fileHandle->seek(50, SEEK_CUR); // Skip over SCUMM engine version _fileHandle->seek(50, SEEK_CUR); // Skip over data file version _numVariables = _fileHandle->readUint16LE(); @@ -1161,8 +1153,6 @@ void ScummEngine_v7::readMAXS(int blockSize) { void ScummEngine_v6::readMAXS(int blockSize) { if (blockSize == 38) { - debug(0, "ScummEngine_v6 readMAXS: MAXS has blocksize %d", blockSize); - _numVariables = _fileHandle->readUint16LE(); _fileHandle->readUint16LE(); _numBitVariables = _fileHandle->readUint16LE(); -- cgit v1.2.3 From 45e65d7ea04b10cf7bb5282bd66b8df609700a63 Mon Sep 17 00:00:00 2001 From: Max Horn Date: Wed, 11 May 2011 16:23:26 +0200 Subject: SCUMM: Refactor how resource (types) are srepresented Previously, we had a couple of arrays of size N (where N = number of resource types), one for each attribute of a resource type (such as as the number of resources of that type. Now, we have one array of size N, whose elements are a record aggregating all the attributes of each resource type. --- engines/scumm/resource.cpp | 144 ++++++++++++++++++++++----------------------- 1 file changed, 72 insertions(+), 72 deletions(-) (limited to 'engines/scumm/resource.cpp') diff --git a/engines/scumm/resource.cpp b/engines/scumm/resource.cpp index ac238abae5..76b6830137 100644 --- a/engines/scumm/resource.cpp +++ b/engines/scumm/resource.cpp @@ -83,8 +83,8 @@ void ScummEngine::openRoom(const int room) { // Load the disk numer / room offs (special case for room 0 exists because // room 0 contains the data which is used to create the roomno / roomoffs // tables -- hence obviously we mustn't use those when loading room 0. - const uint32 diskNumber = room ? _res->roomno[rtRoom][room] : 0; - const uint32 room_offs = room ? _res->roomoffs[rtRoom][room] : 0; + const uint32 diskNumber = room ? _res->_types[rtRoom].roomno[room] : 0; + const uint32 room_offs = room ? _res->_types[rtRoom].roomoffs[room] : 0; // FIXME: Since room_offs is const, clearly the following loop either // is never entered, or loops forever (if it wasn't for the return/error @@ -94,7 +94,7 @@ void ScummEngine::openRoom(const int room) { while (room_offs != RES_INVALID_OFFSET) { if (room_offs != 0 && room != 0 && _game.heversion < 98) { - _fileOffset = _res->roomoffs[rtRoom][room]; + _fileOffset = _res->_types[rtRoom].roomoffs[room]; return; } @@ -122,7 +122,7 @@ void ScummEngine::openRoom(const int room) { return; deleteRoomOffsets(); readRoomsOffsets(); - _fileOffset = _res->roomoffs[rtRoom][room]; + _fileOffset = _res->_types[rtRoom].roomoffs[room]; if (_fileOffset != 8) return; @@ -157,8 +157,8 @@ void ScummEngine::closeRoom() { /** Delete the currently loaded room offsets. */ void ScummEngine::deleteRoomOffsets() { for (int i = 0; i < _numRooms; i++) { - if (_res->roomoffs[rtRoom][i] != RES_INVALID_OFFSET) - _res->roomoffs[rtRoom][i] = 0; + if (_res->_types[rtRoom].roomoffs[i] != RES_INVALID_OFFSET) + _res->_types[rtRoom].roomoffs[i] = 0; } } @@ -174,8 +174,8 @@ void ScummEngine::readRoomsOffsets() { while (num--) { int room = _fileHandle->readByte(); int offset = _fileHandle->readUint32LE(); - if (_res->roomoffs[rtRoom][room] != RES_INVALID_OFFSET) { - _res->roomoffs[rtRoom][room] = offset; + if (_res->_types[rtRoom].roomoffs[room] != RES_INVALID_OFFSET) { + _res->_types[rtRoom].roomoffs[room] = offset; } } } @@ -491,7 +491,7 @@ int ScummEngine::readResTypeList(int id) { else num = _fileHandle->readUint16LE(); - if (num != _res->num[id]) { + if (num != _res->_types[id].num) { error("Invalid number of %ss (%d) in directory", resTypeFromId(id), num); } @@ -499,10 +499,10 @@ int ScummEngine::readResTypeList(int id) { for (i = 0; i < num; i++) { - _res->roomno[id][i] = _fileHandle->readByte(); + _res->_types[id].roomno[i] = _fileHandle->readByte(); } for (i = 0; i < num; i++) { - _res->roomoffs[id][i] = _fileHandle->readUint32LE(); + _res->_types[id].roomoffs[i] = _fileHandle->readUint32LE(); } return num; @@ -516,11 +516,11 @@ int ScummEngine_v70he::readResTypeList(int id) { if (id == rtRoom) for (i = 0; i < num; i++) { - _heV7RoomIntOffsets[i] = _res->roomoffs[rtRoom][i]; + _heV7RoomIntOffsets[i] = _res->_types[rtRoom].roomoffs[i]; } for (i = 0; i < num; i++) { - _res->globsize[id][i] = _fileHandle->readUint32LE(); + _res->_types[id].globsize[i] = _fileHandle->readUint32LE(); } return num; @@ -528,26 +528,26 @@ int ScummEngine_v70he::readResTypeList(int id) { void ResourceManager::allocResTypeData(int id, uint32 tag, int num_, const char *name_, int mode_) { debug(9, "allocResTypeData(%s/%s,%s,%d,%d)", resTypeFromId(id), name_, tag2str(TO_BE_32(tag)), num_, mode_); - assert(id >= 0 && id < (int)(ARRAYSIZE(this->mode))); + assert(id >= 0 && id < (int)(ARRAYSIZE(_types))); if (num_ >= 8000) error("Too many %ss (%d) in directory", name_, num_); - mode[id] = mode_; - num[id] = num_; - tags[id] = tag; - name[id] = name_; - address[id] = (byte **)calloc(num_, sizeof(void *)); - flags[id] = (byte *)calloc(num_, sizeof(byte)); - status[id] = (byte *)calloc(num_, sizeof(byte)); + _types[id].mode = mode_; + _types[id].num = num_; + _types[id].tags = tag; + _types[id].name = name_; + _types[id].address = (byte **)calloc(num_, sizeof(void *)); + _types[id].flags = (byte *)calloc(num_, sizeof(byte)); + _types[id].status = (byte *)calloc(num_, sizeof(byte)); if (mode_) { - roomno[id] = (byte *)calloc(num_, sizeof(byte)); - roomoffs[id] = (uint32 *)calloc(num_, sizeof(uint32)); + _types[id].roomno = (byte *)calloc(num_, sizeof(byte)); + _types[id].roomoffs = (uint32 *)calloc(num_, sizeof(uint32)); } if (_vm->_game.heversion >= 70) { - globsize[id] = (uint32 *)calloc(num_, sizeof(uint32)); + _types[id].globsize = (uint32 *)calloc(num_, sizeof(uint32)); } } @@ -606,8 +606,8 @@ void ScummEngine::ensureResourceLoaded(int type, int i) { if (type != rtCharset && i == 0) return; - if (i <= _res->num[type]) - addr = _res->address[type][i]; + if (i <= _res->_types[type].num) + addr = _res->_types[type].address[i]; if (addr) return; @@ -632,8 +632,8 @@ int ScummEngine::loadResource(int type, int idx) { roomNr = getResourceRoomNr(type, idx); - if (idx >= _res->num[type]) - error("%s %d undefined %d %d", _res->name[type], idx, _res->num[type], roomNr); + if (idx >= _res->_types[type].num) + error("%s %d undefined %d %d", _res->_types[type].name, idx, _res->_types[type].num, roomNr); if (roomNr == 0) roomNr = _roomResource; @@ -669,9 +669,9 @@ int ScummEngine::loadResource(int type, int idx) { tag = _fileHandle->readUint32BE(); - if (tag != _res->tags[type] && _game.heversion < 70) { + if (tag != _res->_types[type].tags && _game.heversion < 70) { error("%s %d not in room %d at %d+%d in file %s", - _res->name[type], idx, roomNr, + _res->_types[type].name, idx, roomNr, _fileOffset, fileOffs, _fileHandle->getName()); } @@ -695,21 +695,21 @@ int ScummEngine::loadResource(int type, int idx) { int ScummEngine::getResourceRoomNr(int type, int idx) { if (type == rtRoom && _game.heversion < 70) return idx; - return _res->roomno[type][idx]; + return _res->_types[type].roomno[idx]; } uint32 ScummEngine::getResourceRoomOffset(int type, int idx) { if (type == rtRoom) { return (_game.version == 8) ? 8 : 0; } - return _res->roomoffs[type][idx]; + return _res->_types[type].roomoffs[idx]; } uint32 ScummEngine_v70he::getResourceRoomOffset(int type, int idx) { if (type == rtRoom) { return _heV7RoomIntOffsets[idx]; } - return _res->roomoffs[type][idx]; + return _res->_types[type].roomoffs[idx]; } int ScummEngine::getResourceSize(int type, int idx) { @@ -729,16 +729,16 @@ byte *ScummEngine::getResourceAddress(int type, int idx) { if (!_res->validateResource("getResourceAddress", type, idx)) return NULL; - if (!_res->address[type]) { - debugC(DEBUG_RESOURCE, "getResourceAddress(%s,%d), _res->address[type] == NULL", resTypeFromId(type), idx); + if (!_res->_types[type].address) { + debugC(DEBUG_RESOURCE, "getResourceAddress(%s,%d), _res->_types[type].address == NULL", resTypeFromId(type), idx); return NULL; } - if (_res->mode[type] && !_res->address[type][idx]) { + if (_res->_types[type].mode && !_res->_types[type].address[idx]) { ensureResourceLoaded(type, idx); } - if (!(ptr = (byte *)_res->address[type][idx])) { + if (!(ptr = (byte *)_res->_types[type].address[idx])) { debugC(DEBUG_RESOURCE, "getResourceAddress(%s,%d) == NULL", resTypeFromId(type), idx); return NULL; } @@ -777,8 +777,8 @@ void ResourceManager::increaseResourceCounter() { byte counter; for (i = rtFirst; i <= rtLast; i++) { - for (j = num[i]; --j >= 0;) { - counter = flags[i][j] & RF_USAGE; + for (j = _types[i].num; --j >= 0;) { + counter = _types[i].flags[j] & RF_USAGE; if (counter && counter < RF_USAGE_MAX) { setResourceCounter(i, j, counter + 1); } @@ -787,8 +787,8 @@ void ResourceManager::increaseResourceCounter() { } void ResourceManager::setResourceCounter(int type, int idx, byte flag) { - flags[type][idx] &= ~RF_USAGE; - flags[type][idx] |= flag; + _types[type].flags[idx] &= ~RF_USAGE; + _types[type].flags[idx] |= flag; } /* 2 bytes safety area to make "precaching" of bytes in the gdi drawer easier */ @@ -805,8 +805,8 @@ byte *ResourceManager::createResource(int type, int idx, uint32 size) { // cases. For instance, Zak tries to reload the intro music // while it's playing. See bug #1253171. - if (address[type][idx] && (type == rtSound || type == rtScript || type == rtCostume)) - return address[type][idx] + sizeof(MemBlkHeader); + if (_types[type].address[idx] && (type == rtSound || type == rtScript || type == rtCostume)) + return _types[type].address[idx] + sizeof(MemBlkHeader); } nukeResource(type, idx); @@ -820,7 +820,7 @@ byte *ResourceManager::createResource(int type, int idx, uint32 size) { _allocatedSize += size; - address[type][idx] = (byte *)ptr; + _types[type].address[idx] = (byte *)ptr; ((MemBlkHeader *)ptr)->size = size; setResourceCounter(type, idx, 1); return (byte *)ptr + sizeof(MemBlkHeader); /* skip header */ @@ -844,7 +844,7 @@ void ResourceManager::setHeapThreshold(int min, int max) { } bool ResourceManager::validateResource(const char *str, int type, int idx) const { - if (type < rtFirst || type > rtLast || (uint) idx >= (uint)num[type]) { + if (type < rtFirst || type > rtLast || (uint) idx >= (uint)_types[type].num) { error("%s Illegal Glob type %s (%d) num %d", str, resTypeFromId(type), type, idx); return false; } @@ -854,17 +854,17 @@ bool ResourceManager::validateResource(const char *str, int type, int idx) const void ResourceManager::nukeResource(int type, int idx) { byte *ptr; - if (!address[type]) + if (!_types[type].address) return; - assert(idx >= 0 && idx < num[type]); + assert(idx >= 0 && idx < _types[type].num); - ptr = address[type][idx]; + ptr = _types[type].address[idx]; if (ptr != NULL) { debugC(DEBUG_RESOURCE, "nukeResource(%s,%d)", resTypeFromId(type), idx); - address[type][idx] = 0; - flags[type][idx] = 0; - status[type][idx] &= ~RS_MODIFIED; + _types[type].address[idx] = 0; + _types[type].flags[idx] = 0; + _types[type].status[idx] &= ~RS_MODIFIED; _allocatedSize -= ((MemBlkHeader *)ptr)->size; free(ptr); } @@ -898,19 +898,19 @@ int ScummEngine::getResourceDataSize(const byte *ptr) const { void ResourceManager::lock(int type, int i) { if (!validateResource("Locking", type, i)) return; - flags[type][i] |= RF_LOCK; + _types[type].flags[i] |= RF_LOCK; } void ResourceManager::unlock(int type, int i) { if (!validateResource("Unlocking", type, i)) return; - flags[type][i] &= ~RF_LOCK; + _types[type].flags[i] &= ~RF_LOCK; } bool ResourceManager::isLocked(int type, int i) const { if (!validateResource("isLocked", type, i)) return false; - return (flags[type][i] & RF_LOCK) != 0; + return (_types[type].flags[i] & RF_LOCK) != 0; } bool ScummEngine::isResourceInUse(int type, int i) const { @@ -947,13 +947,13 @@ bool ScummEngine::isResourceInUse(int type, int i) const { void ResourceManager::setModified(int type, int i) { if (!validateResource("Modified", type, i)) return; - status[type][i] |= RS_MODIFIED; + _types[type].status[i] |= RS_MODIFIED; } bool ResourceManager::isModified(int type, int i) const { if (!validateResource("isModified", type, i)) return false; - return (status[type][i] & RS_MODIFIED) != 0; + return (_types[type].status[i] & RS_MODIFIED) != 0; } void ResourceManager::expireResources(uint32 size) { @@ -978,10 +978,10 @@ void ResourceManager::expireResources(uint32 size) { best_counter = 2; for (i = rtFirst; i <= rtLast; i++) - if (mode[i]) { - for (j = num[i]; --j >= 0;) { - flag = flags[i][j]; - if (!(flag & RF_LOCK) && flag >= best_counter && address[i][j] && !_vm->isResourceInUse(i, j)) { + if (_types[i].mode) { + for (j = _types[i].num; --j >= 0;) { + flag = _types[i].flags[j]; + if (!(flag & RF_LOCK) && flag >= best_counter && _types[i].address[j] && !_vm->isResourceInUse(i, j)) { best_counter = flag; best_type = i; best_res = j; @@ -1002,17 +1002,17 @@ void ResourceManager::expireResources(uint32 size) { void ResourceManager::freeResources() { int i, j; for (i = rtFirst; i <= rtLast; i++) { - for (j = num[i]; --j >= 0;) { + for (j = _types[i].num; --j >= 0;) { if (isResourceLoaded(i, j)) nukeResource(i, j); } - free(address[i]); - free(flags[i]); - free(status[i]); - free(roomno[i]); - free(roomoffs[i]); + free(_types[i].address); + free(_types[i].flags); + free(_types[i].status); + free(_types[i].roomno); + free(_types[i].roomoffs); - free(globsize[i]); + free(_types[i].globsize); } } @@ -1043,7 +1043,7 @@ void ScummEngine::loadPtrToResource(int type, int resindex, const byte *source) bool ResourceManager::isResourceLoaded(int type, int idx) const { if (!validateResource("isResourceLoaded", type, idx)) return false; - return address[type][idx] != NULL; + return _types[type].address[idx] != NULL; } void ResourceManager::resourceStats() { @@ -1052,10 +1052,10 @@ void ResourceManager::resourceStats() { byte flag; for (i = rtFirst; i <= rtLast; i++) - for (j = num[i]; --j >= 0;) { - flag = flags[i][j]; - if (flag & RF_LOCK && address[i][j]) { - lockedSize += ((MemBlkHeader *)address[i][j])->size; + for (j = _types[i].num; --j >= 0;) { + flag = _types[i].flags[j]; + if (flag & RF_LOCK && _types[i].address[j]) { + lockedSize += ((MemBlkHeader *)_types[i].roomoffs[j])->size; lockedNum++; } } -- cgit v1.2.3 From 26efa39d2caa0b6bb7da8414382eb5643c108261 Mon Sep 17 00:00:00 2001 From: Max Horn Date: Wed, 11 May 2011 17:07:31 +0200 Subject: SCUMM: Document and cleanup resource type mode --- engines/scumm/resource.cpp | 80 ++++++++++++++++++++++++---------------------- 1 file changed, 42 insertions(+), 38 deletions(-) (limited to 'engines/scumm/resource.cpp') diff --git a/engines/scumm/resource.cpp b/engines/scumm/resource.cpp index 76b6830137..7f88def0ad 100644 --- a/engines/scumm/resource.cpp +++ b/engines/scumm/resource.cpp @@ -526,28 +526,28 @@ int ScummEngine_v70he::readResTypeList(int id) { return num; } -void ResourceManager::allocResTypeData(int id, uint32 tag, int num_, const char *name_, int mode_) { - debug(9, "allocResTypeData(%s/%s,%s,%d,%d)", resTypeFromId(id), name_, tag2str(TO_BE_32(tag)), num_, mode_); +void ResourceManager::allocResTypeData(int id, uint32 tag, int num, const char *name, ResTypeMode mode) { + debug(9, "allocResTypeData(%s/%s,%s,%d,%d)", resTypeFromId(id), name, tag2str(TO_BE_32(tag)), num, mode); assert(id >= 0 && id < (int)(ARRAYSIZE(_types))); - if (num_ >= 8000) - error("Too many %ss (%d) in directory", name_, num_); + if (num >= 8000) + error("Too many %ss (%d) in directory", name, num); - _types[id].mode = mode_; - _types[id].num = num_; - _types[id].tags = tag; - _types[id].name = name_; - _types[id].address = (byte **)calloc(num_, sizeof(void *)); - _types[id].flags = (byte *)calloc(num_, sizeof(byte)); - _types[id].status = (byte *)calloc(num_, sizeof(byte)); + _types[id]._mode = mode; + _types[id].num = num; + _types[id].tag = tag; + _types[id].name = name; + _types[id].address = (byte **)calloc(num, sizeof(void *)); + _types[id].flags = (byte *)calloc(num, sizeof(byte)); + _types[id].status = (byte *)calloc(num, sizeof(byte)); - if (mode_) { - _types[id].roomno = (byte *)calloc(num_, sizeof(byte)); - _types[id].roomoffs = (uint32 *)calloc(num_, sizeof(uint32)); + if (mode) { + _types[id].roomno = (byte *)calloc(num, sizeof(byte)); + _types[id].roomoffs = (uint32 *)calloc(num, sizeof(uint32)); } if (_vm->_game.heversion >= 70) { - _types[id].globsize = (uint32 *)calloc(num_, sizeof(uint32)); + _types[id].globsize = (uint32 *)calloc(num, sizeof(uint32)); } } @@ -669,7 +669,7 @@ int ScummEngine::loadResource(int type, int idx) { tag = _fileHandle->readUint32BE(); - if (tag != _res->_types[type].tags && _game.heversion < 70) { + if (tag != _res->_types[type].tag && _game.heversion < 70) { error("%s %d not in room %d at %d+%d in file %s", _res->_types[type].name, idx, roomNr, _fileOffset, fileOffs, _fileHandle->getName()); @@ -734,11 +734,13 @@ byte *ScummEngine::getResourceAddress(int type, int idx) { return NULL; } - if (_res->_types[type].mode && !_res->_types[type].address[idx]) { + // If the resource is missing, but loadable from the game data files, try to do so. + if (!_res->_types[type].address[idx] && _res->_types[type]._mode != kDynamicResTypeMode) { ensureResourceLoaded(type, idx); } - if (!(ptr = (byte *)_res->_types[type].address[idx])) { + ptr = (byte *)_res->_types[type].address[idx]; + if (!ptr) { debugC(DEBUG_RESOURCE, "getResourceAddress(%s,%d) == NULL", resTypeFromId(type), idx); return NULL; } @@ -978,7 +980,9 @@ void ResourceManager::expireResources(uint32 size) { best_counter = 2; for (i = rtFirst; i <= rtLast; i++) - if (_types[i].mode) { + if (_types[i]._mode != kDynamicResTypeMode) { + // Resources of this type can be reloaded from the data files, + // so we can potentially unload them to free memory. for (j = _types[i].num; --j >= 0;) { flag = _types[i].flags[j]; if (!(flag & RF_LOCK) && flag >= best_counter && _types[i].address[j] && !_vm->isResourceInUse(i, j)) { @@ -1275,30 +1279,30 @@ void ScummEngine::allocateArrays() { } _res->allocResTypeData(rtCostume, (_game.features & GF_NEW_COSTUMES) ? MKTAG('A','K','O','S') : MKTAG('C','O','S','T'), - _numCostumes, "costume", 1); - _res->allocResTypeData(rtRoom, MKTAG('R','O','O','M'), _numRooms, "room", 1); - _res->allocResTypeData(rtRoomImage, MKTAG('R','M','I','M'), _numRooms, "room image", 1); - _res->allocResTypeData(rtRoomScripts, MKTAG('R','M','S','C'), _numRooms, "room script", 1); - _res->allocResTypeData(rtSound, MKTAG('S','O','U','N'), _numSounds, "sound", 2); - _res->allocResTypeData(rtScript, MKTAG('S','C','R','P'), _numScripts, "script", 1); - _res->allocResTypeData(rtCharset, MKTAG('C','H','A','R'), _numCharsets, "charset", 1); - _res->allocResTypeData(rtObjectName, 0, _numNewNames, "new name", 0); - _res->allocResTypeData(rtInventory, 0, _numInventory, "inventory", 0); - _res->allocResTypeData(rtTemp, 0, 10, "temp", 0); - _res->allocResTypeData(rtScaleTable, 0, 5, "scale table", 0); - _res->allocResTypeData(rtActorName, 0, _numActors, "actor name", 0); - _res->allocResTypeData(rtVerb, 0, _numVerbs, "verb", 0); - _res->allocResTypeData(rtString, 0, _numArray, "array", 0); - _res->allocResTypeData(rtFlObject, 0, _numFlObject, "flobject", 0); - _res->allocResTypeData(rtMatrix, 0, 10, "boxes", 0); - _res->allocResTypeData(rtImage, MKTAG('A','W','I','Z'), _numImages, "images", 1); - _res->allocResTypeData(rtTalkie, MKTAG('T','L','K','E'), _numTalkies, "talkie", 1); + _numCostumes, "costume", kStaticResTypeMode); + _res->allocResTypeData(rtRoom, MKTAG('R','O','O','M'), _numRooms, "room", kStaticResTypeMode); + _res->allocResTypeData(rtRoomImage, MKTAG('R','M','I','M'), _numRooms, "room image", kStaticResTypeMode); + _res->allocResTypeData(rtRoomScripts, MKTAG('R','M','S','C'), _numRooms, "room script", kStaticResTypeMode); + _res->allocResTypeData(rtSound, MKTAG('S','O','U','N'), _numSounds, "sound", kSoundResTypeMode); + _res->allocResTypeData(rtScript, MKTAG('S','C','R','P'), _numScripts, "script", kStaticResTypeMode); + _res->allocResTypeData(rtCharset, MKTAG('C','H','A','R'), _numCharsets, "charset", kStaticResTypeMode); + _res->allocResTypeData(rtObjectName, 0, _numNewNames, "new name", kDynamicResTypeMode); + _res->allocResTypeData(rtInventory, 0, _numInventory, "inventory", kDynamicResTypeMode); + _res->allocResTypeData(rtTemp, 0, 10, "temp", kDynamicResTypeMode); + _res->allocResTypeData(rtScaleTable, 0, 5, "scale table", kDynamicResTypeMode); + _res->allocResTypeData(rtActorName, 0, _numActors, "actor name", kDynamicResTypeMode); + _res->allocResTypeData(rtVerb, 0, _numVerbs, "verb", kDynamicResTypeMode); + _res->allocResTypeData(rtString, 0, _numArray, "array", kDynamicResTypeMode); + _res->allocResTypeData(rtFlObject, 0, _numFlObject, "flobject", kDynamicResTypeMode); + _res->allocResTypeData(rtMatrix, 0, 10, "boxes", kDynamicResTypeMode); + _res->allocResTypeData(rtImage, MKTAG('A','W','I','Z'), _numImages, "images", kStaticResTypeMode); + _res->allocResTypeData(rtTalkie, MKTAG('T','L','K','E'), _numTalkies, "talkie", kStaticResTypeMode); } void ScummEngine_v70he::allocateArrays() { ScummEngine::allocateArrays(); - _res->allocResTypeData(rtSpoolBuffer, 0, 9, "spool buffer", 1); + _res->allocResTypeData(rtSpoolBuffer, 0, 9, "spool buffer", kStaticResTypeMode); _heV7RoomIntOffsets = (uint32 *)calloc(_numRooms, sizeof(uint32)); } -- cgit v1.2.3 From 0af2f71c71e39df2d61dd7cc9faed7c5e6a50fab Mon Sep 17 00:00:00 2001 From: Max Horn Date: Wed, 11 May 2011 17:19:26 +0200 Subject: SCUMM: Clean up ResourceManager initialization --- engines/scumm/resource.cpp | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) (limited to 'engines/scumm/resource.cpp') diff --git a/engines/scumm/resource.cpp b/engines/scumm/resource.cpp index 7f88def0ad..a00630f1ec 100644 --- a/engines/scumm/resource.cpp +++ b/engines/scumm/resource.cpp @@ -828,10 +828,18 @@ byte *ResourceManager::createResource(int type, int idx, uint32 size) { return (byte *)ptr + sizeof(MemBlkHeader); /* skip header */ } -ResourceManager::ResourceManager(ScummEngine *vm) { - memset(this, 0, sizeof(ResourceManager)); - _vm = vm; -// _allocatedSize = 0; +ResourceManager::ResTypeData::ResTypeData() { + memset(this, 0, sizeof(this)); +} + +ResourceManager::ResTypeData::~ResTypeData() { +} + +ResourceManager::ResourceManager(ScummEngine *vm) : _vm(vm) { + _allocatedSize = 0; + _maxHeapThreshold = 0; + _minHeapThreshold = 0; + _expireCounter = 0; } ResourceManager::~ResourceManager() { -- cgit v1.2.3 From 75b9deb1856bae8355403faa5f55857f3929adb6 Mon Sep 17 00:00:00 2001 From: Max Horn Date: Wed, 11 May 2011 18:06:30 +0200 Subject: SCUMM: Get rid of the MemBlkHeader hack This uncovered at least one potentially serious bug in the inventory code, which still needs to be investigated and fixed. --- engines/scumm/resource.cpp | 70 ++++++++++++++++++++++------------------------ 1 file changed, 33 insertions(+), 37 deletions(-) (limited to 'engines/scumm/resource.cpp') diff --git a/engines/scumm/resource.cpp b/engines/scumm/resource.cpp index a00630f1ec..009fd5c11d 100644 --- a/engines/scumm/resource.cpp +++ b/engines/scumm/resource.cpp @@ -537,9 +537,10 @@ void ResourceManager::allocResTypeData(int id, uint32 tag, int num, const char * _types[id].num = num; _types[id].tag = tag; _types[id].name = name; - _types[id].address = (byte **)calloc(num, sizeof(void *)); + _types[id]._address = (byte **)calloc(num, sizeof(byte *)); + _types[id]._size = (uint32 *)calloc(num, sizeof(uint32)); _types[id].flags = (byte *)calloc(num, sizeof(byte)); - _types[id].status = (byte *)calloc(num, sizeof(byte)); + _types[id]._status = (byte *)calloc(num, sizeof(byte)); if (mode) { _types[id].roomno = (byte *)calloc(num, sizeof(byte)); @@ -584,8 +585,6 @@ void ScummEngine::nukeCharset(int i) { } void ScummEngine::ensureResourceLoaded(int type, int i) { - void *addr = NULL; - debugC(DEBUG_RESOURCE, "ensureResourceLoaded(%s,%d)", resTypeFromId(type), i); if ((type == rtRoom) && i > 0x7F && _game.version < 7 && _game.heversion <= 71) { @@ -606,10 +605,7 @@ void ScummEngine::ensureResourceLoaded(int type, int i) { if (type != rtCharset && i == 0) return; - if (i <= _res->_types[type].num) - addr = _res->_types[type].address[i]; - - if (addr) + if (i <= _res->_types[type].num && _res->_types[type]._address[i]) return; loadResource(type, i); @@ -715,9 +711,7 @@ uint32 ScummEngine_v70he::getResourceRoomOffset(int type, int idx) { int ScummEngine::getResourceSize(int type, int idx) { byte *ptr = getResourceAddress(type, idx); assert(ptr); - MemBlkHeader *hdr = (MemBlkHeader *)(ptr - sizeof(MemBlkHeader)); - - return hdr->size; + return _res->_types[type]._size[idx]; } byte *ScummEngine::getResourceAddress(int type, int idx) { @@ -729,17 +723,17 @@ byte *ScummEngine::getResourceAddress(int type, int idx) { if (!_res->validateResource("getResourceAddress", type, idx)) return NULL; - if (!_res->_types[type].address) { - debugC(DEBUG_RESOURCE, "getResourceAddress(%s,%d), _res->_types[type].address == NULL", resTypeFromId(type), idx); + if (!_res->_types[type]._address) { + debugC(DEBUG_RESOURCE, "getResourceAddress(%s,%d), _res->_types[type]._address == NULL", resTypeFromId(type), idx); return NULL; } // If the resource is missing, but loadable from the game data files, try to do so. - if (!_res->_types[type].address[idx] && _res->_types[type]._mode != kDynamicResTypeMode) { + if (!_res->_types[type]._address[idx] && _res->_types[type]._mode != kDynamicResTypeMode) { ensureResourceLoaded(type, idx); } - ptr = (byte *)_res->_types[type].address[idx]; + ptr = (byte *)_res->_types[type]._address[idx]; if (!ptr) { debugC(DEBUG_RESOURCE, "getResourceAddress(%s,%d) == NULL", resTypeFromId(type), idx); return NULL; @@ -747,8 +741,8 @@ byte *ScummEngine::getResourceAddress(int type, int idx) { _res->setResourceCounter(type, idx, 1); - debugC(DEBUG_RESOURCE, "getResourceAddress(%s,%d) == %p", resTypeFromId(type), idx, ptr + sizeof(MemBlkHeader)); - return ptr + sizeof(MemBlkHeader); + debugC(DEBUG_RESOURCE, "getResourceAddress(%s,%d) == %p", resTypeFromId(type), idx, ptr); + return ptr; } byte *ScummEngine::getStringAddress(int i) { @@ -807,29 +801,29 @@ byte *ResourceManager::createResource(int type, int idx, uint32 size) { // cases. For instance, Zak tries to reload the intro music // while it's playing. See bug #1253171. - if (_types[type].address[idx] && (type == rtSound || type == rtScript || type == rtCostume)) - return _types[type].address[idx] + sizeof(MemBlkHeader); + if (_types[type]._address[idx] && (type == rtSound || type == rtScript || type == rtCostume)) + return _types[type]._address[idx]; } nukeResource(type, idx); expireResources(size); - void *ptr = calloc(size + sizeof(MemBlkHeader) + SAFETY_AREA, 1); + byte *ptr = (byte *)calloc(size + SAFETY_AREA, 1); if (ptr == NULL) { error("createResource(%s,%d): Out of memory while allocating %d", resTypeFromId(type), idx, size); } _allocatedSize += size; - _types[type].address[idx] = (byte *)ptr; - ((MemBlkHeader *)ptr)->size = size; + _types[type]._address[idx] = ptr; + _types[type]._size[idx] = size; setResourceCounter(type, idx, 1); - return (byte *)ptr + sizeof(MemBlkHeader); /* skip header */ + return ptr; } ResourceManager::ResTypeData::ResTypeData() { - memset(this, 0, sizeof(this)); + memset(this, 0, sizeof(*this)); } ResourceManager::ResTypeData::~ResTypeData() { @@ -864,18 +858,19 @@ bool ResourceManager::validateResource(const char *str, int type, int idx) const void ResourceManager::nukeResource(int type, int idx) { byte *ptr; - if (!_types[type].address) + if (!_types[type]._address) return; assert(idx >= 0 && idx < _types[type].num); - ptr = _types[type].address[idx]; + ptr = _types[type]._address[idx]; if (ptr != NULL) { debugC(DEBUG_RESOURCE, "nukeResource(%s,%d)", resTypeFromId(type), idx); - _types[type].address[idx] = 0; + _types[type]._address[idx] = 0; + _types[type]._size[idx] = 0; _types[type].flags[idx] = 0; - _types[type].status[idx] &= ~RS_MODIFIED; - _allocatedSize -= ((MemBlkHeader *)ptr)->size; + _types[type]._status[idx] &= ~RS_MODIFIED; + _allocatedSize -= _types[type]._size[idx]; free(ptr); } } @@ -957,13 +952,13 @@ bool ScummEngine::isResourceInUse(int type, int i) const { void ResourceManager::setModified(int type, int i) { if (!validateResource("Modified", type, i)) return; - _types[type].status[i] |= RS_MODIFIED; + _types[type]._status[i] |= RS_MODIFIED; } bool ResourceManager::isModified(int type, int i) const { if (!validateResource("isModified", type, i)) return false; - return (_types[type].status[i] & RS_MODIFIED) != 0; + return (_types[type]._status[i] & RS_MODIFIED) != 0; } void ResourceManager::expireResources(uint32 size) { @@ -993,7 +988,7 @@ void ResourceManager::expireResources(uint32 size) { // so we can potentially unload them to free memory. for (j = _types[i].num; --j >= 0;) { flag = _types[i].flags[j]; - if (!(flag & RF_LOCK) && flag >= best_counter && _types[i].address[j] && !_vm->isResourceInUse(i, j)) { + if (!(flag & RF_LOCK) && flag >= best_counter && _types[i]._address[j] && !_vm->isResourceInUse(i, j)) { best_counter = flag; best_type = i; best_res = j; @@ -1018,9 +1013,10 @@ void ResourceManager::freeResources() { if (isResourceLoaded(i, j)) nukeResource(i, j); } - free(_types[i].address); + free(_types[i]._address); + free(_types[i]._size); free(_types[i].flags); - free(_types[i].status); + free(_types[i]._status); free(_types[i].roomno); free(_types[i].roomoffs); @@ -1055,7 +1051,7 @@ void ScummEngine::loadPtrToResource(int type, int resindex, const byte *source) bool ResourceManager::isResourceLoaded(int type, int idx) const { if (!validateResource("isResourceLoaded", type, idx)) return false; - return _types[type].address[idx] != NULL; + return _types[type]._address[idx] != NULL; } void ResourceManager::resourceStats() { @@ -1066,8 +1062,8 @@ void ResourceManager::resourceStats() { for (i = rtFirst; i <= rtLast; i++) for (j = _types[i].num; --j >= 0;) { flag = _types[i].flags[j]; - if (flag & RF_LOCK && _types[i].address[j]) { - lockedSize += ((MemBlkHeader *)_types[i].roomoffs[j])->size; + if (flag & RF_LOCK && _types[i]._address[j]) { + lockedSize += _types[i]._size[j]; lockedNum++; } } -- cgit v1.2.3 From 0342ab3f1b2d67de6fa0112311b973b55d509f1c Mon Sep 17 00:00:00 2001 From: Max Horn Date: Wed, 11 May 2011 18:11:37 +0200 Subject: SCUMM: Rename ResTypeData::num to _num --- engines/scumm/resource.cpp | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) (limited to 'engines/scumm/resource.cpp') diff --git a/engines/scumm/resource.cpp b/engines/scumm/resource.cpp index 009fd5c11d..d0dd077c2c 100644 --- a/engines/scumm/resource.cpp +++ b/engines/scumm/resource.cpp @@ -491,7 +491,7 @@ int ScummEngine::readResTypeList(int id) { else num = _fileHandle->readUint16LE(); - if (num != _res->_types[id].num) { + if (num != _res->_types[id]._num) { error("Invalid number of %ss (%d) in directory", resTypeFromId(id), num); } @@ -534,7 +534,7 @@ void ResourceManager::allocResTypeData(int id, uint32 tag, int num, const char * error("Too many %ss (%d) in directory", name, num); _types[id]._mode = mode; - _types[id].num = num; + _types[id]._num = num; _types[id].tag = tag; _types[id].name = name; _types[id]._address = (byte **)calloc(num, sizeof(byte *)); @@ -605,7 +605,7 @@ void ScummEngine::ensureResourceLoaded(int type, int i) { if (type != rtCharset && i == 0) return; - if (i <= _res->_types[type].num && _res->_types[type]._address[i]) + if (i <= _res->_types[type]._num && _res->_types[type]._address[i]) return; loadResource(type, i); @@ -628,8 +628,8 @@ int ScummEngine::loadResource(int type, int idx) { roomNr = getResourceRoomNr(type, idx); - if (idx >= _res->_types[type].num) - error("%s %d undefined %d %d", _res->_types[type].name, idx, _res->_types[type].num, roomNr); + if (idx >= _res->_types[type]._num) + error("%s %d undefined %d %d", _res->_types[type].name, idx, _res->_types[type]._num, roomNr); if (roomNr == 0) roomNr = _roomResource; @@ -773,7 +773,7 @@ void ResourceManager::increaseResourceCounter() { byte counter; for (i = rtFirst; i <= rtLast; i++) { - for (j = _types[i].num; --j >= 0;) { + for (j = _types[i]._num; --j >= 0;) { counter = _types[i].flags[j] & RF_USAGE; if (counter && counter < RF_USAGE_MAX) { setResourceCounter(i, j, counter + 1); @@ -848,7 +848,7 @@ void ResourceManager::setHeapThreshold(int min, int max) { } bool ResourceManager::validateResource(const char *str, int type, int idx) const { - if (type < rtFirst || type > rtLast || (uint) idx >= (uint)_types[type].num) { + if (type < rtFirst || type > rtLast || (uint) idx >= (uint)_types[type]._num) { error("%s Illegal Glob type %s (%d) num %d", str, resTypeFromId(type), type, idx); return false; } @@ -861,7 +861,7 @@ void ResourceManager::nukeResource(int type, int idx) { if (!_types[type]._address) return; - assert(idx >= 0 && idx < _types[type].num); + assert(idx >= 0 && idx < _types[type]._num); ptr = _types[type]._address[idx]; if (ptr != NULL) { @@ -986,7 +986,7 @@ void ResourceManager::expireResources(uint32 size) { if (_types[i]._mode != kDynamicResTypeMode) { // Resources of this type can be reloaded from the data files, // so we can potentially unload them to free memory. - for (j = _types[i].num; --j >= 0;) { + for (j = _types[i]._num; --j >= 0;) { flag = _types[i].flags[j]; if (!(flag & RF_LOCK) && flag >= best_counter && _types[i]._address[j] && !_vm->isResourceInUse(i, j)) { best_counter = flag; @@ -1009,7 +1009,7 @@ void ResourceManager::expireResources(uint32 size) { void ResourceManager::freeResources() { int i, j; for (i = rtFirst; i <= rtLast; i++) { - for (j = _types[i].num; --j >= 0;) { + for (j = _types[i]._num; --j >= 0;) { if (isResourceLoaded(i, j)) nukeResource(i, j); } @@ -1060,7 +1060,7 @@ void ResourceManager::resourceStats() { byte flag; for (i = rtFirst; i <= rtLast; i++) - for (j = _types[i].num; --j >= 0;) { + for (j = _types[i]._num; --j >= 0;) { flag = _types[i].flags[j]; if (flag & RF_LOCK && _types[i]._address[j]) { lockedSize += _types[i]._size[j]; -- cgit v1.2.3 From 82a6f2ee3ce1cd11c7b186b0ec728e48589445ae Mon Sep 17 00:00:00 2001 From: Max Horn Date: Wed, 11 May 2011 18:19:05 +0200 Subject: SCUMM: Rename ResTypeData::tag to _tag, get rid of ResTypeData::name --- engines/scumm/resource.cpp | 55 +++++++++++++++++++++++----------------------- 1 file changed, 27 insertions(+), 28 deletions(-) (limited to 'engines/scumm/resource.cpp') diff --git a/engines/scumm/resource.cpp b/engines/scumm/resource.cpp index d0dd077c2c..b9f88b2389 100644 --- a/engines/scumm/resource.cpp +++ b/engines/scumm/resource.cpp @@ -526,17 +526,16 @@ int ScummEngine_v70he::readResTypeList(int id) { return num; } -void ResourceManager::allocResTypeData(int id, uint32 tag, int num, const char *name, ResTypeMode mode) { - debug(9, "allocResTypeData(%s/%s,%s,%d,%d)", resTypeFromId(id), name, tag2str(TO_BE_32(tag)), num, mode); +void ResourceManager::allocResTypeData(int id, uint32 tag, int num, ResTypeMode mode) { + debug(2, "allocResTypeData(%s,%s,%d,%d)", resTypeFromId(id), tag2str(TO_BE_32(tag)), num, mode); assert(id >= 0 && id < (int)(ARRAYSIZE(_types))); if (num >= 8000) - error("Too many %ss (%d) in directory", name, num); + error("Too many %s resources (%d) in directory", resTypeFromId(id), num); _types[id]._mode = mode; _types[id]._num = num; - _types[id].tag = tag; - _types[id].name = name; + _types[id]._tag = tag; _types[id]._address = (byte **)calloc(num, sizeof(byte *)); _types[id]._size = (uint32 *)calloc(num, sizeof(uint32)); _types[id].flags = (byte *)calloc(num, sizeof(byte)); @@ -629,7 +628,7 @@ int ScummEngine::loadResource(int type, int idx) { roomNr = getResourceRoomNr(type, idx); if (idx >= _res->_types[type]._num) - error("%s %d undefined %d %d", _res->_types[type].name, idx, _res->_types[type]._num, roomNr); + error("%s %d undefined %d %d", resTypeFromId(type), idx, _res->_types[type]._num, roomNr); if (roomNr == 0) roomNr = _roomResource; @@ -663,11 +662,11 @@ int ScummEngine::loadResource(int type, int idx) { return readSoundResource(idx); } + // Sanity check: Is this the right tag for this resource type? tag = _fileHandle->readUint32BE(); - - if (tag != _res->_types[type].tag && _game.heversion < 70) { + if (tag != _res->_types[type]._tag && _game.heversion < 70) { error("%s %d not in room %d at %d+%d in file %s", - _res->_types[type].name, idx, roomNr, + resTypeFromId(type), idx, roomNr, _fileOffset, fileOffs, _fileHandle->getName()); } @@ -1283,30 +1282,30 @@ void ScummEngine::allocateArrays() { } _res->allocResTypeData(rtCostume, (_game.features & GF_NEW_COSTUMES) ? MKTAG('A','K','O','S') : MKTAG('C','O','S','T'), - _numCostumes, "costume", kStaticResTypeMode); - _res->allocResTypeData(rtRoom, MKTAG('R','O','O','M'), _numRooms, "room", kStaticResTypeMode); - _res->allocResTypeData(rtRoomImage, MKTAG('R','M','I','M'), _numRooms, "room image", kStaticResTypeMode); - _res->allocResTypeData(rtRoomScripts, MKTAG('R','M','S','C'), _numRooms, "room script", kStaticResTypeMode); - _res->allocResTypeData(rtSound, MKTAG('S','O','U','N'), _numSounds, "sound", kSoundResTypeMode); - _res->allocResTypeData(rtScript, MKTAG('S','C','R','P'), _numScripts, "script", kStaticResTypeMode); - _res->allocResTypeData(rtCharset, MKTAG('C','H','A','R'), _numCharsets, "charset", kStaticResTypeMode); - _res->allocResTypeData(rtObjectName, 0, _numNewNames, "new name", kDynamicResTypeMode); - _res->allocResTypeData(rtInventory, 0, _numInventory, "inventory", kDynamicResTypeMode); - _res->allocResTypeData(rtTemp, 0, 10, "temp", kDynamicResTypeMode); - _res->allocResTypeData(rtScaleTable, 0, 5, "scale table", kDynamicResTypeMode); - _res->allocResTypeData(rtActorName, 0, _numActors, "actor name", kDynamicResTypeMode); - _res->allocResTypeData(rtVerb, 0, _numVerbs, "verb", kDynamicResTypeMode); - _res->allocResTypeData(rtString, 0, _numArray, "array", kDynamicResTypeMode); - _res->allocResTypeData(rtFlObject, 0, _numFlObject, "flobject", kDynamicResTypeMode); - _res->allocResTypeData(rtMatrix, 0, 10, "boxes", kDynamicResTypeMode); - _res->allocResTypeData(rtImage, MKTAG('A','W','I','Z'), _numImages, "images", kStaticResTypeMode); - _res->allocResTypeData(rtTalkie, MKTAG('T','L','K','E'), _numTalkies, "talkie", kStaticResTypeMode); + _numCostumes, kStaticResTypeMode); + _res->allocResTypeData(rtRoom, MKTAG('R','O','O','M'), _numRooms, kStaticResTypeMode); + _res->allocResTypeData(rtRoomImage, MKTAG('R','M','I','M'), _numRooms, kStaticResTypeMode); + _res->allocResTypeData(rtRoomScripts, MKTAG('R','M','S','C'), _numRooms, kStaticResTypeMode); + _res->allocResTypeData(rtSound, MKTAG('S','O','U','N'), _numSounds, kSoundResTypeMode); + _res->allocResTypeData(rtScript, MKTAG('S','C','R','P'), _numScripts, kStaticResTypeMode); + _res->allocResTypeData(rtCharset, MKTAG('C','H','A','R'), _numCharsets, kStaticResTypeMode); + _res->allocResTypeData(rtObjectName, 0, _numNewNames, kDynamicResTypeMode); + _res->allocResTypeData(rtInventory, 0, _numInventory, kDynamicResTypeMode); + _res->allocResTypeData(rtTemp, 0, 10, kDynamicResTypeMode); + _res->allocResTypeData(rtScaleTable, 0, 5, kDynamicResTypeMode); + _res->allocResTypeData(rtActorName, 0, _numActors, kDynamicResTypeMode); + _res->allocResTypeData(rtVerb, 0, _numVerbs, kDynamicResTypeMode); + _res->allocResTypeData(rtString, 0, _numArray, kDynamicResTypeMode); + _res->allocResTypeData(rtFlObject, 0, _numFlObject, kDynamicResTypeMode); + _res->allocResTypeData(rtMatrix, 0, 10, kDynamicResTypeMode); + _res->allocResTypeData(rtImage, MKTAG('A','W','I','Z'), _numImages, kStaticResTypeMode); + _res->allocResTypeData(rtTalkie, MKTAG('T','L','K','E'), _numTalkies, kStaticResTypeMode); } void ScummEngine_v70he::allocateArrays() { ScummEngine::allocateArrays(); - _res->allocResTypeData(rtSpoolBuffer, 0, 9, "spool buffer", kStaticResTypeMode); + _res->allocResTypeData(rtSpoolBuffer, 0, 9, kStaticResTypeMode); _heV7RoomIntOffsets = (uint32 *)calloc(_numRooms, sizeof(uint32)); } -- cgit v1.2.3 From 107e25ff5bc65ccca62c8fe0a51cf978ee86dae4 Mon Sep 17 00:00:00 2001 From: Max Horn Date: Thu, 12 May 2011 15:03:58 +0200 Subject: SCUMM: Document resource usage count / expiry a little bit --- engines/scumm/resource.cpp | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) (limited to 'engines/scumm/resource.cpp') diff --git a/engines/scumm/resource.cpp b/engines/scumm/resource.cpp index b9f88b2389..3b278a6b80 100644 --- a/engines/scumm/resource.cpp +++ b/engines/scumm/resource.cpp @@ -762,12 +762,13 @@ byte *ScummEngine::getStringAddressVar(int i) { } void ResourceManager::increaseExpireCounter() { - if (!(++_expireCounter)) { - increaseResourceCounter(); + ++_expireCounter; + if (_expireCounter == 0) { // overflow? + increaseResourceCounters(); } } -void ResourceManager::increaseResourceCounter() { +void ResourceManager::increaseResourceCounters() { int i, j; byte counter; @@ -781,9 +782,9 @@ void ResourceManager::increaseResourceCounter() { } } -void ResourceManager::setResourceCounter(int type, int idx, byte flag) { - _types[type].flags[idx] &= ~RF_USAGE; - _types[type].flags[idx] |= flag; +void ResourceManager::setResourceCounter(int type, int idx, byte counter) { + _types[type].flags[idx] &= RF_LOCK; // Clear lower 7 bits, preserve the lock bit. + _types[type].flags[idx] |= counter; // Update the usage counter } /* 2 bytes safety area to make "precaching" of bytes in the gdi drawer easier */ @@ -969,7 +970,7 @@ void ResourceManager::expireResources(uint32 size) { if (_expireCounter != 0xFF) { _expireCounter = 0xFF; - increaseResourceCounter(); + increaseResourceCounters(); } if (size + _allocatedSize < _maxHeapThreshold) @@ -1000,7 +1001,7 @@ void ResourceManager::expireResources(uint32 size) { nukeResource(best_type, best_res); } while (size + _allocatedSize > _minHeapThreshold); - increaseResourceCounter(); + increaseResourceCounters(); debugC(DEBUG_RESOURCE, "Expired resources, mem %d -> %d", oldAllocatedSize, _allocatedSize); } -- cgit v1.2.3 From c02420df43bec4ec523c18d8e6fdb381af29c1b5 Mon Sep 17 00:00:00 2001 From: Max Horn Date: Fri, 13 May 2011 11:45:42 +0200 Subject: SCUMM: Add a Resource class, refactor res code around it --- engines/scumm/resource.cpp | 175 ++++++++++++++++++++++++++------------------- 1 file changed, 102 insertions(+), 73 deletions(-) (limited to 'engines/scumm/resource.cpp') diff --git a/engines/scumm/resource.cpp b/engines/scumm/resource.cpp index 3b278a6b80..8767e1b315 100644 --- a/engines/scumm/resource.cpp +++ b/engines/scumm/resource.cpp @@ -83,8 +83,8 @@ void ScummEngine::openRoom(const int room) { // Load the disk numer / room offs (special case for room 0 exists because // room 0 contains the data which is used to create the roomno / roomoffs // tables -- hence obviously we mustn't use those when loading room 0. - const uint32 diskNumber = room ? _res->_types[rtRoom].roomno[room] : 0; - const uint32 room_offs = room ? _res->_types[rtRoom].roomoffs[room] : 0; + const uint32 diskNumber = room ? _res->_types[rtRoom]._resources[room]._roomno : 0; + const uint32 room_offs = room ? _res->_types[rtRoom]._resources[room]._roomoffs : 0; // FIXME: Since room_offs is const, clearly the following loop either // is never entered, or loops forever (if it wasn't for the return/error @@ -94,7 +94,7 @@ void ScummEngine::openRoom(const int room) { while (room_offs != RES_INVALID_OFFSET) { if (room_offs != 0 && room != 0 && _game.heversion < 98) { - _fileOffset = _res->_types[rtRoom].roomoffs[room]; + _fileOffset = _res->_types[rtRoom]._resources[room]._roomoffs; return; } @@ -122,7 +122,7 @@ void ScummEngine::openRoom(const int room) { return; deleteRoomOffsets(); readRoomsOffsets(); - _fileOffset = _res->_types[rtRoom].roomoffs[room]; + _fileOffset = _res->_types[rtRoom]._resources[room]._roomoffs; if (_fileOffset != 8) return; @@ -157,8 +157,8 @@ void ScummEngine::closeRoom() { /** Delete the currently loaded room offsets. */ void ScummEngine::deleteRoomOffsets() { for (int i = 0; i < _numRooms; i++) { - if (_res->_types[rtRoom].roomoffs[i] != RES_INVALID_OFFSET) - _res->_types[rtRoom].roomoffs[i] = 0; + if (_res->_types[rtRoom]._resources[i]._roomoffs != RES_INVALID_OFFSET) + _res->_types[rtRoom]._resources[i]._roomoffs = 0; } } @@ -174,8 +174,8 @@ void ScummEngine::readRoomsOffsets() { while (num--) { int room = _fileHandle->readByte(); int offset = _fileHandle->readUint32LE(); - if (_res->_types[rtRoom].roomoffs[room] != RES_INVALID_OFFSET) { - _res->_types[rtRoom].roomoffs[room] = offset; + if (_res->_types[rtRoom]._resources[room]._roomoffs != RES_INVALID_OFFSET) { + _res->_types[rtRoom]._resources[room]._roomoffs = offset; } } } @@ -499,10 +499,10 @@ int ScummEngine::readResTypeList(int id) { for (i = 0; i < num; i++) { - _res->_types[id].roomno[i] = _fileHandle->readByte(); + _res->_types[id]._resources[i]._roomno = _fileHandle->readByte(); } for (i = 0; i < num; i++) { - _res->_types[id].roomoffs[i] = _fileHandle->readUint32LE(); + _res->_types[id]._resources[i]._roomoffs = _fileHandle->readUint32LE(); } return num; @@ -516,11 +516,11 @@ int ScummEngine_v70he::readResTypeList(int id) { if (id == rtRoom) for (i = 0; i < num; i++) { - _heV7RoomIntOffsets[i] = _res->_types[rtRoom].roomoffs[i]; + _heV7RoomIntOffsets[i] = _res->_types[rtRoom]._resources[i]._roomoffs; } for (i = 0; i < num; i++) { - _res->_types[id].globsize[i] = _fileHandle->readUint32LE(); + _res->_types[id]._resources[i]._globsize = _fileHandle->readUint32LE(); } return num; @@ -536,11 +536,11 @@ void ResourceManager::allocResTypeData(int id, uint32 tag, int num, ResTypeMode _types[id]._mode = mode; _types[id]._num = num; _types[id]._tag = tag; - _types[id]._address = (byte **)calloc(num, sizeof(byte *)); - _types[id]._size = (uint32 *)calloc(num, sizeof(uint32)); - _types[id].flags = (byte *)calloc(num, sizeof(byte)); - _types[id]._status = (byte *)calloc(num, sizeof(byte)); + _types[id]._resources.resize(num); +/* + TODO: Use multiple Resource subclasses, one for each res mode; then, + given them serializability. if (mode) { _types[id].roomno = (byte *)calloc(num, sizeof(byte)); _types[id].roomoffs = (uint32 *)calloc(num, sizeof(uint32)); @@ -549,7 +549,7 @@ void ResourceManager::allocResTypeData(int id, uint32 tag, int num, ResTypeMode if (_vm->_game.heversion >= 70) { _types[id].globsize = (uint32 *)calloc(num, sizeof(uint32)); } - +*/ } void ScummEngine::loadCharset(int no) { @@ -604,7 +604,7 @@ void ScummEngine::ensureResourceLoaded(int type, int i) { if (type != rtCharset && i == 0) return; - if (i <= _res->_types[type]._num && _res->_types[type]._address[i]) + if (i <= _res->_types[type]._num && _res->_types[type]._resources[i]._address) return; loadResource(type, i); @@ -690,27 +690,27 @@ int ScummEngine::loadResource(int type, int idx) { int ScummEngine::getResourceRoomNr(int type, int idx) { if (type == rtRoom && _game.heversion < 70) return idx; - return _res->_types[type].roomno[idx]; + return _res->_types[type]._resources[idx]._roomno; } uint32 ScummEngine::getResourceRoomOffset(int type, int idx) { if (type == rtRoom) { return (_game.version == 8) ? 8 : 0; } - return _res->_types[type].roomoffs[idx]; + return _res->_types[type]._resources[idx]._roomoffs; } uint32 ScummEngine_v70he::getResourceRoomOffset(int type, int idx) { if (type == rtRoom) { return _heV7RoomIntOffsets[idx]; } - return _res->_types[type].roomoffs[idx]; + return _res->_types[type]._resources[idx]._roomoffs; } int ScummEngine::getResourceSize(int type, int idx) { byte *ptr = getResourceAddress(type, idx); assert(ptr); - return _res->_types[type]._size[idx]; + return _res->_types[type]._resources[idx]._size; } byte *ScummEngine::getResourceAddress(int type, int idx) { @@ -722,17 +722,12 @@ byte *ScummEngine::getResourceAddress(int type, int idx) { if (!_res->validateResource("getResourceAddress", type, idx)) return NULL; - if (!_res->_types[type]._address) { - debugC(DEBUG_RESOURCE, "getResourceAddress(%s,%d), _res->_types[type]._address == NULL", resTypeFromId(type), idx); - return NULL; - } - // If the resource is missing, but loadable from the game data files, try to do so. - if (!_res->_types[type]._address[idx] && _res->_types[type]._mode != kDynamicResTypeMode) { + if (!_res->_types[type]._resources[idx]._address && _res->_types[type]._mode != kDynamicResTypeMode) { ensureResourceLoaded(type, idx); } - ptr = (byte *)_res->_types[type]._address[idx]; + ptr = (byte *)_res->_types[type]._resources[idx]._address; if (!ptr) { debugC(DEBUG_RESOURCE, "getResourceAddress(%s,%d) == NULL", resTypeFromId(type), idx); return NULL; @@ -774,7 +769,7 @@ void ResourceManager::increaseResourceCounters() { for (i = rtFirst; i <= rtLast; i++) { for (j = _types[i]._num; --j >= 0;) { - counter = _types[i].flags[j] & RF_USAGE; + counter = _types[i]._resources[j].getResourceCounter(); if (counter && counter < RF_USAGE_MAX) { setResourceCounter(i, j, counter + 1); } @@ -783,8 +778,16 @@ void ResourceManager::increaseResourceCounters() { } void ResourceManager::setResourceCounter(int type, int idx, byte counter) { - _types[type].flags[idx] &= RF_LOCK; // Clear lower 7 bits, preserve the lock bit. - _types[type].flags[idx] |= counter; // Update the usage counter + _types[type]._resources[idx].setResourceCounter(counter); +} + +void ResourceManager::Resource::setResourceCounter(byte counter) { + _flags &= RF_LOCK; // Clear lower 7 bits, preserve the lock bit. + _flags |= counter; // Update the usage counter +} + +byte ResourceManager::Resource::getResourceCounter() const { + return _flags & RF_USAGE; } /* 2 bytes safety area to make "precaching" of bytes in the gdi drawer easier */ @@ -801,8 +804,8 @@ byte *ResourceManager::createResource(int type, int idx, uint32 size) { // cases. For instance, Zak tries to reload the intro music // while it's playing. See bug #1253171. - if (_types[type]._address[idx] && (type == rtSound || type == rtScript || type == rtCostume)) - return _types[type]._address[idx]; + if (_types[type]._resources[idx]._address && (type == rtSound || type == rtScript || type == rtCostume)) + return _types[type]._resources[idx]._address; } nukeResource(type, idx); @@ -816,14 +819,39 @@ byte *ResourceManager::createResource(int type, int idx, uint32 size) { _allocatedSize += size; - _types[type]._address[idx] = ptr; - _types[type]._size[idx] = size; + _types[type]._resources[idx]._address = ptr; + _types[type]._resources[idx]._size = size; setResourceCounter(type, idx, 1); return ptr; } +ResourceManager::Resource::Resource() { + _address = 0; + _size = 0; + _flags = 0; + _status = 0; + _roomno = 0; + _roomoffs = 0; + _globsize = 0; +} + +ResourceManager::Resource::~Resource() { + delete _address; + _address = 0; +} + +void ResourceManager::Resource::nuke() { + delete _address; + _address = 0; + _size = 0; + _flags = 0; + _status &= ~RS_MODIFIED; +} + ResourceManager::ResTypeData::ResTypeData() { - memset(this, 0, sizeof(*this)); + _mode = kDynamicResTypeMode; + _num = 0; + _tag = 0; } ResourceManager::ResTypeData::~ResTypeData() { @@ -856,22 +884,11 @@ bool ResourceManager::validateResource(const char *str, int type, int idx) const } void ResourceManager::nukeResource(int type, int idx) { - byte *ptr; - - if (!_types[type]._address) - return; - - assert(idx >= 0 && idx < _types[type]._num); - - ptr = _types[type]._address[idx]; + byte *ptr = _types[type]._resources[idx]._address; if (ptr != NULL) { debugC(DEBUG_RESOURCE, "nukeResource(%s,%d)", resTypeFromId(type), idx); - _types[type]._address[idx] = 0; - _types[type]._size[idx] = 0; - _types[type].flags[idx] = 0; - _types[type]._status[idx] &= ~RS_MODIFIED; - _allocatedSize -= _types[type]._size[idx]; - free(ptr); + _allocatedSize -= _types[type]._resources[idx]._size; + _types[type]._resources[idx].nuke(); } } @@ -903,19 +920,31 @@ int ScummEngine::getResourceDataSize(const byte *ptr) const { void ResourceManager::lock(int type, int i) { if (!validateResource("Locking", type, i)) return; - _types[type].flags[i] |= RF_LOCK; + _types[type]._resources[i].lock(); } void ResourceManager::unlock(int type, int i) { if (!validateResource("Unlocking", type, i)) return; - _types[type].flags[i] &= ~RF_LOCK; + _types[type]._resources[i].unlock(); } bool ResourceManager::isLocked(int type, int i) const { if (!validateResource("isLocked", type, i)) return false; - return (_types[type].flags[i] & RF_LOCK) != 0; + return _types[type]._resources[i].isLocked(); +} + +void ResourceManager::Resource::lock() { + _flags |= RF_LOCK; +} + +void ResourceManager::Resource::unlock() { + _flags &= ~RF_LOCK; +} + +bool ResourceManager::Resource::isLocked() const { + return (_flags & RF_LOCK) != 0; } bool ScummEngine::isResourceInUse(int type, int i) const { @@ -952,18 +981,25 @@ bool ScummEngine::isResourceInUse(int type, int i) const { void ResourceManager::setModified(int type, int i) { if (!validateResource("Modified", type, i)) return; - _types[type]._status[i] |= RS_MODIFIED; + _types[type]._resources[i].setModified(); } bool ResourceManager::isModified(int type, int i) const { if (!validateResource("isModified", type, i)) return false; - return (_types[type]._status[i] & RS_MODIFIED) != 0; + return _types[type]._resources[i].isModified(); +} + +void ResourceManager::Resource::setModified() { + _status |= RS_MODIFIED; +} + +bool ResourceManager::Resource::isModified() const { + return (_status & RS_MODIFIED) != 0; } void ResourceManager::expireResources(uint32 size) { int i, j; - byte flag; byte best_counter; int best_type, best_res = 0; uint32 oldAllocatedSize; @@ -987,9 +1023,10 @@ void ResourceManager::expireResources(uint32 size) { // Resources of this type can be reloaded from the data files, // so we can potentially unload them to free memory. for (j = _types[i]._num; --j >= 0;) { - flag = _types[i].flags[j]; - if (!(flag & RF_LOCK) && flag >= best_counter && _types[i]._address[j] && !_vm->isResourceInUse(i, j)) { - best_counter = flag; + Resource &tmp = _types[i]._resources[j]; + byte counter = tmp.getResourceCounter(); + if (!tmp.isLocked() && counter >= best_counter && tmp._address && !_vm->isResourceInUse(i, j)) { + best_counter = counter; best_type = i; best_res = j; } @@ -1013,14 +1050,7 @@ void ResourceManager::freeResources() { if (isResourceLoaded(i, j)) nukeResource(i, j); } - free(_types[i]._address); - free(_types[i]._size); - free(_types[i].flags); - free(_types[i]._status); - free(_types[i].roomno); - free(_types[i].roomoffs); - - free(_types[i].globsize); + _types[i]._resources.clear(); } } @@ -1051,19 +1081,18 @@ void ScummEngine::loadPtrToResource(int type, int resindex, const byte *source) bool ResourceManager::isResourceLoaded(int type, int idx) const { if (!validateResource("isResourceLoaded", type, idx)) return false; - return _types[type]._address[idx] != NULL; + return _types[type]._resources[idx]._address != NULL; } void ResourceManager::resourceStats() { int i, j; uint32 lockedSize = 0, lockedNum = 0; - byte flag; for (i = rtFirst; i <= rtLast; i++) for (j = _types[i]._num; --j >= 0;) { - flag = _types[i].flags[j]; - if (flag & RF_LOCK && _types[i]._address[j]) { - lockedSize += _types[i]._size[j]; + Resource &tmp = _types[i]._resources[j]; + if (tmp.isLocked() && tmp._address) { + lockedSize += tmp._size; lockedNum++; } } -- cgit v1.2.3 From 649f8e0a84804a9d1272ba16b146ea2b9da501fe Mon Sep 17 00:00:00 2001 From: Max Horn Date: Fri, 13 May 2011 14:02:53 +0200 Subject: SCUMM: Rename ResTypes->ResType, introduce ResId typedef, change code to use both --- engines/scumm/resource.cpp | 240 ++++++++++++++++++++++----------------------- 1 file changed, 119 insertions(+), 121 deletions(-) (limited to 'engines/scumm/resource.cpp') diff --git a/engines/scumm/resource.cpp b/engines/scumm/resource.cpp index 8767e1b315..2eedf5e144 100644 --- a/engines/scumm/resource.cpp +++ b/engines/scumm/resource.cpp @@ -52,7 +52,7 @@ enum { -extern const char *resTypeFromId(int id); +extern const char *resTypeFromId(int type); static uint16 newTag2Old(uint32 newTag); static const byte *findResourceSmall(uint32 tag, const byte *searchin); @@ -482,72 +482,72 @@ void ScummEngine::readArrayFromIndexFile() { error("readArrayFromIndexFile() not supported in pre-V6 games"); } -int ScummEngine::readResTypeList(int id) { - int num; - int i; +int ScummEngine::readResTypeList(ResType type) { + uint num; + ResId idx; if (_game.version == 8) num = _fileHandle->readUint32LE(); else num = _fileHandle->readUint16LE(); - if (num != _res->_types[id]._num) { - error("Invalid number of %ss (%d) in directory", resTypeFromId(id), num); + if (num != _res->_types[type]._num) { + error("Invalid number of %ss (%d) in directory", resTypeFromId(type), num); } - debug(2, " readResTypeList(%s): %d entries", resTypeFromId(id), num); + debug(2, " readResTypeList(%s): %d entries", resTypeFromId(type), num); - for (i = 0; i < num; i++) { - _res->_types[id]._resources[i]._roomno = _fileHandle->readByte(); + for (idx = 0; idx < num; idx++) { + _res->_types[type]._resources[idx]._roomno = _fileHandle->readByte(); } - for (i = 0; i < num; i++) { - _res->_types[id]._resources[i]._roomoffs = _fileHandle->readUint32LE(); + for (idx = 0; idx < num; idx++) { + _res->_types[type]._resources[idx]._roomoffs = _fileHandle->readUint32LE(); } return num; } -int ScummEngine_v70he::readResTypeList(int id) { - int num; - int i; +int ScummEngine_v70he::readResTypeList(ResType type) { + uint num; + ResId idx; - num = ScummEngine::readResTypeList(id); + num = ScummEngine::readResTypeList(type); - if (id == rtRoom) - for (i = 0; i < num; i++) { - _heV7RoomIntOffsets[i] = _res->_types[rtRoom]._resources[i]._roomoffs; + if (type == rtRoom) + for (idx = 0; idx < num; idx++) { + _heV7RoomIntOffsets[idx] = _res->_types[rtRoom]._resources[idx]._roomoffs; } - for (i = 0; i < num; i++) { - _res->_types[id]._resources[i]._globsize = _fileHandle->readUint32LE(); + for (idx = 0; idx < num; idx++) { + _res->_types[type]._resources[idx]._globsize = _fileHandle->readUint32LE(); } return num; } -void ResourceManager::allocResTypeData(int id, uint32 tag, int num, ResTypeMode mode) { - debug(2, "allocResTypeData(%s,%s,%d,%d)", resTypeFromId(id), tag2str(TO_BE_32(tag)), num, mode); - assert(id >= 0 && id < (int)(ARRAYSIZE(_types))); +void ResourceManager::allocResTypeData(ResType type, uint32 tag, int num, ResTypeMode mode) { + debug(2, "allocResTypeData(%s,%s,%d,%d)", resTypeFromId(type), tag2str(TO_BE_32(tag)), num, mode); + assert(type >= 0 && type < (int)(ARRAYSIZE(_types))); if (num >= 8000) - error("Too many %s resources (%d) in directory", resTypeFromId(id), num); + error("Too many %s resources (%d) in directory", resTypeFromId(type), num); - _types[id]._mode = mode; - _types[id]._num = num; - _types[id]._tag = tag; - _types[id]._resources.resize(num); + _types[type]._mode = mode; + _types[type]._num = num; + _types[type]._tag = tag; + _types[type]._resources.resize(num); /* TODO: Use multiple Resource subclasses, one for each res mode; then, given them serializability. if (mode) { - _types[id].roomno = (byte *)calloc(num, sizeof(byte)); - _types[id].roomoffs = (uint32 *)calloc(num, sizeof(uint32)); + _types[type].roomno = (byte *)calloc(num, sizeof(byte)); + _types[type].roomoffs = (uint32 *)calloc(num, sizeof(uint32)); } if (_vm->_game.heversion >= 70) { - _types[id].globsize = (uint32 *)calloc(num, sizeof(uint32)); + _types[type].globsize = (uint32 *)calloc(num, sizeof(uint32)); } */ } @@ -583,14 +583,14 @@ void ScummEngine::nukeCharset(int i) { _res->nukeResource(rtCharset, i); } -void ScummEngine::ensureResourceLoaded(int type, int i) { - debugC(DEBUG_RESOURCE, "ensureResourceLoaded(%s,%d)", resTypeFromId(type), i); +void ScummEngine::ensureResourceLoaded(ResType type, ResId idx) { + debugC(DEBUG_RESOURCE, "ensureResourceLoaded(%s,%d)", resTypeFromId(type), idx); - if ((type == rtRoom) && i > 0x7F && _game.version < 7 && _game.heversion <= 71) { - i = _resourceMapper[i & 0x7F]; + if ((type == rtRoom) && idx > 0x7F && _game.version < 7 && _game.heversion <= 71) { + idx = _resourceMapper[idx & 0x7F]; } - // FIXME: This check used to be "i==0". However, that causes + // FIXME: This check used to be "idx==0". However, that causes // problems when using this function to ensure charset 0 is loaded. // This is done for many games, e.g. Zak256 or Indy3 (EGA and VGA). // For now we restrict the check to anything which is not a charset. @@ -601,19 +601,19 @@ void ScummEngine::ensureResourceLoaded(int type, int i) { // our code base? After all we also have to add special cases for many // of our script opcodes that check for the (invalid) actor 0... so // maybe both issues are related... - if (type != rtCharset && i == 0) + if (type != rtCharset && idx == 0) return; - if (i <= _res->_types[type]._num && _res->_types[type]._resources[i]._address) + if (idx <= _res->_types[type]._num && _res->_types[type]._resources[idx]._address) return; - loadResource(type, i); + loadResource(type, idx); - if (_game.version == 5 && type == rtRoom && i == _roomResource) + if (_game.version == 5 && type == rtRoom && (int)idx == _roomResource) VAR(VAR_ROOM_FLAG) = 1; } -int ScummEngine::loadResource(int type, int idx) { +int ScummEngine::loadResource(ResType type, ResId idx) { int roomNr; uint32 fileOffs; uint32 size, tag; @@ -687,33 +687,33 @@ int ScummEngine::loadResource(int type, int idx) { return 1; } -int ScummEngine::getResourceRoomNr(int type, int idx) { +int ScummEngine::getResourceRoomNr(ResType type, ResId idx) { if (type == rtRoom && _game.heversion < 70) return idx; return _res->_types[type]._resources[idx]._roomno; } -uint32 ScummEngine::getResourceRoomOffset(int type, int idx) { +uint32 ScummEngine::getResourceRoomOffset(ResType type, ResId idx) { if (type == rtRoom) { return (_game.version == 8) ? 8 : 0; } return _res->_types[type]._resources[idx]._roomoffs; } -uint32 ScummEngine_v70he::getResourceRoomOffset(int type, int idx) { +uint32 ScummEngine_v70he::getResourceRoomOffset(ResType type, ResId idx) { if (type == rtRoom) { return _heV7RoomIntOffsets[idx]; } return _res->_types[type]._resources[idx]._roomoffs; } -int ScummEngine::getResourceSize(int type, int idx) { +int ScummEngine::getResourceSize(ResType type, ResId idx) { byte *ptr = getResourceAddress(type, idx); assert(ptr); return _res->_types[type]._resources[idx]._size; } -byte *ScummEngine::getResourceAddress(int type, int idx) { +byte *ScummEngine::getResourceAddress(ResType type, ResId idx) { byte *ptr; if (_game.heversion >= 80 && type == rtString) @@ -739,13 +739,13 @@ byte *ScummEngine::getResourceAddress(int type, int idx) { return ptr; } -byte *ScummEngine::getStringAddress(int i) { - byte *addr = getResourceAddress(rtString, i); +byte *ScummEngine::getStringAddress(ResId idx) { + byte *addr = getResourceAddress(rtString, idx); return addr; } -byte *ScummEngine_v6::getStringAddress(int i) { - byte *addr = getResourceAddress(rtString, i); +byte *ScummEngine_v6::getStringAddress(ResId idx) { + byte *addr = getResourceAddress(rtString, idx); if (addr == NULL) return NULL; // Skip over the ArrayHeader @@ -764,20 +764,18 @@ void ResourceManager::increaseExpireCounter() { } void ResourceManager::increaseResourceCounters() { - int i, j; - byte counter; - - for (i = rtFirst; i <= rtLast; i++) { - for (j = _types[i]._num; --j >= 0;) { - counter = _types[i]._resources[j].getResourceCounter(); + for (ResType type = rtFirst; type <= rtLast; type = ResType(type + 1)) { + ResId idx = _types[type]._num; + while (idx-- > 0) { + byte counter = _types[type]._resources[idx].getResourceCounter(); if (counter && counter < RF_USAGE_MAX) { - setResourceCounter(i, j, counter + 1); + setResourceCounter(type, idx, counter + 1); } } } } -void ResourceManager::setResourceCounter(int type, int idx, byte counter) { +void ResourceManager::setResourceCounter(ResType type, ResId idx, byte counter) { _types[type]._resources[idx].setResourceCounter(counter); } @@ -793,7 +791,7 @@ byte ResourceManager::Resource::getResourceCounter() const { /* 2 bytes safety area to make "precaching" of bytes in the gdi drawer easier */ #define SAFETY_AREA 2 -byte *ResourceManager::createResource(int type, int idx, uint32 size) { +byte *ResourceManager::createResource(ResType type, ResId idx, uint32 size) { debugC(DEBUG_RESOURCE, "_res->createResource(%s,%d,%d)", resTypeFromId(type), idx, size); if (!validateResource("allocating", type, idx)) @@ -875,15 +873,15 @@ void ResourceManager::setHeapThreshold(int min, int max) { _minHeapThreshold = min; } -bool ResourceManager::validateResource(const char *str, int type, int idx) const { - if (type < rtFirst || type > rtLast || (uint) idx >= (uint)_types[type]._num) { +bool ResourceManager::validateResource(const char *str, ResType type, ResId idx) const { + if (type < rtFirst || type > rtLast || (uint)idx >= (uint)_types[type]._num) { error("%s Illegal Glob type %s (%d) num %d", str, resTypeFromId(type), type, idx); return false; } return true; } -void ResourceManager::nukeResource(int type, int idx) { +void ResourceManager::nukeResource(ResType type, ResId idx) { byte *ptr = _types[type]._resources[idx]._address; if (ptr != NULL) { debugC(DEBUG_RESOURCE, "nukeResource(%s,%d)", resTypeFromId(type), idx); @@ -917,22 +915,22 @@ int ScummEngine::getResourceDataSize(const byte *ptr) const { return READ_BE_UINT32(ptr - 4) - _resourceHeaderSize; } -void ResourceManager::lock(int type, int i) { - if (!validateResource("Locking", type, i)) +void ResourceManager::lock(ResType type, ResId idx) { + if (!validateResource("Locking", type, idx)) return; - _types[type]._resources[i].lock(); + _types[type]._resources[idx].lock(); } -void ResourceManager::unlock(int type, int i) { - if (!validateResource("Unlocking", type, i)) +void ResourceManager::unlock(ResType type, ResId idx) { + if (!validateResource("Unlocking", type, idx)) return; - _types[type]._resources[i].unlock(); + _types[type]._resources[idx].unlock(); } -bool ResourceManager::isLocked(int type, int i) const { - if (!validateResource("isLocked", type, i)) +bool ResourceManager::isLocked(ResType type, ResId idx) const { + if (!validateResource("isLocked", type, idx)) return false; - return _types[type]._resources[i].isLocked(); + return _types[type]._resources[idx].isLocked(); } void ResourceManager::Resource::lock() { @@ -947,47 +945,47 @@ bool ResourceManager::Resource::isLocked() const { return (_flags & RF_LOCK) != 0; } -bool ScummEngine::isResourceInUse(int type, int i) const { - if (!_res->validateResource("isResourceInUse", type, i)) +bool ScummEngine::isResourceInUse(ResType type, ResId idx) const { + if (!_res->validateResource("isResourceInUse", type, idx)) return false; switch (type) { case rtRoom: - return _roomResource == (byte)i; + return _roomResource == (byte)idx; case rtRoomImage: - return _roomResource == (byte)i; + return _roomResource == (byte)idx; case rtRoomScripts: - return _roomResource == (byte)i; + return _roomResource == (byte)idx; case rtScript: - return isScriptInUse(i); + return isScriptInUse(idx); case rtCostume: - return isCostumeInUse(i); + return isCostumeInUse(idx); case rtSound: // Sound resource 1 is used for queued speech - if (_game.heversion >= 60 && i == 1) + if (_game.heversion >= 60 && idx == 1) return true; else - return _sound->isSoundInUse(i); + return _sound->isSoundInUse(idx); case rtCharset: - return _charset->getCurID() == i; + return _charset->getCurID() == (int)idx; case rtImage: - return _res->isModified(type, i) != 0; + return _res->isModified(type, idx) != 0; case rtSpoolBuffer: - return _sound->isSoundRunning(10000 + i) != 0; + return _sound->isSoundRunning(10000 + idx) != 0; default: return false; } } -void ResourceManager::setModified(int type, int i) { - if (!validateResource("Modified", type, i)) +void ResourceManager::setModified(ResType type, ResId idx) { + if (!validateResource("Modified", type, idx)) return; - _types[type]._resources[i].setModified(); + _types[type]._resources[idx].setModified(); } -bool ResourceManager::isModified(int type, int i) const { - if (!validateResource("isModified", type, i)) +bool ResourceManager::isModified(ResType type, ResId idx) const { + if (!validateResource("isModified", type, idx)) return false; - return _types[type]._resources[i].isModified(); + return _types[type]._resources[idx].isModified(); } void ResourceManager::Resource::setModified() { @@ -999,9 +997,9 @@ bool ResourceManager::Resource::isModified() const { } void ResourceManager::expireResources(uint32 size) { - int i, j; byte best_counter; - int best_type, best_res = 0; + ResType best_type; + int best_res = 0; uint32 oldAllocatedSize; if (_expireCounter != 0xFF) { @@ -1015,23 +1013,25 @@ void ResourceManager::expireResources(uint32 size) { oldAllocatedSize = _allocatedSize; do { - best_type = 0; + best_type = rtInvalid; best_counter = 2; - for (i = rtFirst; i <= rtLast; i++) - if (_types[i]._mode != kDynamicResTypeMode) { + for (ResType type = rtFirst; type <= rtLast; type = ResType(type + 1)) { + if (_types[type]._mode != kDynamicResTypeMode) { // Resources of this type can be reloaded from the data files, // so we can potentially unload them to free memory. - for (j = _types[i]._num; --j >= 0;) { - Resource &tmp = _types[i]._resources[j]; + ResId idx = _types[type]._num; + while (idx-- > 0) { + Resource &tmp = _types[type]._resources[idx]; byte counter = tmp.getResourceCounter(); - if (!tmp.isLocked() && counter >= best_counter && tmp._address && !_vm->isResourceInUse(i, j)) { + if (!tmp.isLocked() && counter >= best_counter && tmp._address && !_vm->isResourceInUse(type, idx)) { best_counter = counter; - best_type = i; - best_res = j; + best_type = type; + best_res = idx; } } } + } if (!best_type) break; @@ -1044,28 +1044,27 @@ void ResourceManager::expireResources(uint32 size) { } void ResourceManager::freeResources() { - int i, j; - for (i = rtFirst; i <= rtLast; i++) { - for (j = _types[i]._num; --j >= 0;) { - if (isResourceLoaded(i, j)) - nukeResource(i, j); + for (ResType type = rtFirst; type <= rtLast; type = ResType(type + 1)) { + ResId idx = _types[type]._num; + while (idx-- > 0) { + if (isResourceLoaded(type, idx)) + nukeResource(type, idx); } - _types[i]._resources.clear(); + _types[type]._resources.clear(); } } -void ScummEngine::loadPtrToResource(int type, int resindex, const byte *source) { +void ScummEngine::loadPtrToResource(ResType type, ResId idx, const byte *source) { byte *alloced; int len; - _res->nukeResource(type, resindex); + _res->nukeResource(type, idx); len = resStrLen(source) + 1; - if (len <= 0) return; - alloced = _res->createResource(type, resindex, len); + alloced = _res->createResource(type, idx, len); if (!source) { // Need to refresh the script pointer, since createResource may @@ -1078,24 +1077,25 @@ void ScummEngine::loadPtrToResource(int type, int resindex, const byte *source) } } -bool ResourceManager::isResourceLoaded(int type, int idx) const { +bool ResourceManager::isResourceLoaded(ResType type, ResId idx) const { if (!validateResource("isResourceLoaded", type, idx)) return false; return _types[type]._resources[idx]._address != NULL; } void ResourceManager::resourceStats() { - int i, j; uint32 lockedSize = 0, lockedNum = 0; - for (i = rtFirst; i <= rtLast; i++) - for (j = _types[i]._num; --j >= 0;) { - Resource &tmp = _types[i]._resources[j]; + for (ResType type = rtFirst; type <= rtLast; type = ResType(type + 1)) { + ResId idx = _types[type]._num; + while (idx-- > 0) { + Resource &tmp = _types[type]._resources[idx]; if (tmp.isLocked() && tmp._address) { lockedSize += tmp._size; lockedNum++; } } + } debug(1, "Total allocated size=%d, locked=%d(%d)", _allocatedSize, lockedSize, lockedNum); } @@ -1340,7 +1340,7 @@ void ScummEngine_v70he::allocateArrays() { } -void ScummEngine::dumpResource(const char *tag, int idx, const byte *ptr, int length) { +void ScummEngine::dumpResource(const char *tag, int id, const byte *ptr, int length) { char buf[256]; Common::DumpFile out; @@ -1354,7 +1354,7 @@ void ScummEngine::dumpResource(const char *tag, int idx, const byte *ptr, int le else size = READ_BE_UINT32(ptr + 4); - sprintf(buf, "dumps/%s%d.dmp", tag, idx); + sprintf(buf, "dumps/%s%d.dmp", tag, id); out.open(buf); if (out.isOpen() == false) @@ -1523,10 +1523,10 @@ uint16 newTag2Old(uint32 newTag) { } } -const char *resTypeFromId(int id) { +const char *resTypeFromId(int type) { static char buf[100]; - switch (id) { + switch (type) { case rtRoom: return "Room"; case rtScript: @@ -1569,10 +1569,8 @@ const char *resTypeFromId(int id) { return "Talkie"; case rtSpoolBuffer: return "SpoolBuffer"; - case rtNumTypes: - return "NumTypes"; default: - sprintf(buf, "%d", id); + sprintf(buf, "rt%d", type); return buf; } } -- cgit v1.2.3 From dfd61124039dc04540207a09f7f26e2693599ded Mon Sep 17 00:00:00 2001 From: Max Horn Date: Fri, 13 May 2011 14:04:59 +0200 Subject: SCUMM: Rename resTypeFromId -> nameOfResType --- engines/scumm/resource.cpp | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) (limited to 'engines/scumm/resource.cpp') diff --git a/engines/scumm/resource.cpp b/engines/scumm/resource.cpp index 2eedf5e144..b24c172a33 100644 --- a/engines/scumm/resource.cpp +++ b/engines/scumm/resource.cpp @@ -52,7 +52,7 @@ enum { -extern const char *resTypeFromId(int type); +extern const char *nameOfResType(ResType type); static uint16 newTag2Old(uint32 newTag); static const byte *findResourceSmall(uint32 tag, const byte *searchin); @@ -492,10 +492,10 @@ int ScummEngine::readResTypeList(ResType type) { num = _fileHandle->readUint16LE(); if (num != _res->_types[type]._num) { - error("Invalid number of %ss (%d) in directory", resTypeFromId(type), num); + error("Invalid number of %ss (%d) in directory", nameOfResType(type), num); } - debug(2, " readResTypeList(%s): %d entries", resTypeFromId(type), num); + debug(2, " readResTypeList(%s): %d entries", nameOfResType(type), num); for (idx = 0; idx < num; idx++) { @@ -527,11 +527,11 @@ int ScummEngine_v70he::readResTypeList(ResType type) { } void ResourceManager::allocResTypeData(ResType type, uint32 tag, int num, ResTypeMode mode) { - debug(2, "allocResTypeData(%s,%s,%d,%d)", resTypeFromId(type), tag2str(TO_BE_32(tag)), num, mode); + debug(2, "allocResTypeData(%s,%s,%d,%d)", nameOfResType(type), tag2str(TO_BE_32(tag)), num, mode); assert(type >= 0 && type < (int)(ARRAYSIZE(_types))); if (num >= 8000) - error("Too many %s resources (%d) in directory", resTypeFromId(type), num); + error("Too many %s resources (%d) in directory", nameOfResType(type), num); _types[type]._mode = mode; _types[type]._num = num; @@ -584,7 +584,7 @@ void ScummEngine::nukeCharset(int i) { } void ScummEngine::ensureResourceLoaded(ResType type, ResId idx) { - debugC(DEBUG_RESOURCE, "ensureResourceLoaded(%s,%d)", resTypeFromId(type), idx); + debugC(DEBUG_RESOURCE, "ensureResourceLoaded(%s,%d)", nameOfResType(type), idx); if ((type == rtRoom) && idx > 0x7F && _game.version < 7 && _game.heversion <= 71) { idx = _resourceMapper[idx & 0x7F]; @@ -618,7 +618,7 @@ int ScummEngine::loadResource(ResType type, ResId idx) { uint32 fileOffs; uint32 size, tag; - debugC(DEBUG_RESOURCE, "loadResource(%s,%d)", resTypeFromId(type), idx); + debugC(DEBUG_RESOURCE, "loadResource(%s,%d)", nameOfResType(type), idx); if (type == rtCharset && (_game.features & GF_SMALL_HEADER)) { loadCharset(idx); @@ -628,7 +628,7 @@ int ScummEngine::loadResource(ResType type, ResId idx) { roomNr = getResourceRoomNr(type, idx); if (idx >= _res->_types[type]._num) - error("%s %d undefined %d %d", resTypeFromId(type), idx, _res->_types[type]._num, roomNr); + error("%s %d undefined %d %d", nameOfResType(type), idx, _res->_types[type]._num, roomNr); if (roomNr == 0) roomNr = _roomResource; @@ -666,7 +666,7 @@ int ScummEngine::loadResource(ResType type, ResId idx) { tag = _fileHandle->readUint32BE(); if (tag != _res->_types[type]._tag && _game.heversion < 70) { error("%s %d not in room %d at %d+%d in file %s", - resTypeFromId(type), idx, roomNr, + nameOfResType(type), idx, roomNr, _fileOffset, fileOffs, _fileHandle->getName()); } @@ -729,13 +729,13 @@ byte *ScummEngine::getResourceAddress(ResType type, ResId idx) { ptr = (byte *)_res->_types[type]._resources[idx]._address; if (!ptr) { - debugC(DEBUG_RESOURCE, "getResourceAddress(%s,%d) == NULL", resTypeFromId(type), idx); + debugC(DEBUG_RESOURCE, "getResourceAddress(%s,%d) == NULL", nameOfResType(type), idx); return NULL; } _res->setResourceCounter(type, idx, 1); - debugC(DEBUG_RESOURCE, "getResourceAddress(%s,%d) == %p", resTypeFromId(type), idx, ptr); + debugC(DEBUG_RESOURCE, "getResourceAddress(%s,%d) == %p", nameOfResType(type), idx, ptr); return ptr; } @@ -792,7 +792,7 @@ byte ResourceManager::Resource::getResourceCounter() const { #define SAFETY_AREA 2 byte *ResourceManager::createResource(ResType type, ResId idx, uint32 size) { - debugC(DEBUG_RESOURCE, "_res->createResource(%s,%d,%d)", resTypeFromId(type), idx, size); + debugC(DEBUG_RESOURCE, "_res->createResource(%s,%d,%d)", nameOfResType(type), idx, size); if (!validateResource("allocating", type, idx)) return NULL; @@ -812,7 +812,7 @@ byte *ResourceManager::createResource(ResType type, ResId idx, uint32 size) { byte *ptr = (byte *)calloc(size + SAFETY_AREA, 1); if (ptr == NULL) { - error("createResource(%s,%d): Out of memory while allocating %d", resTypeFromId(type), idx, size); + error("createResource(%s,%d): Out of memory while allocating %d", nameOfResType(type), idx, size); } _allocatedSize += size; @@ -875,7 +875,7 @@ void ResourceManager::setHeapThreshold(int min, int max) { bool ResourceManager::validateResource(const char *str, ResType type, ResId idx) const { if (type < rtFirst || type > rtLast || (uint)idx >= (uint)_types[type]._num) { - error("%s Illegal Glob type %s (%d) num %d", str, resTypeFromId(type), type, idx); + error("%s Illegal Glob type %s (%d) num %d", str, nameOfResType(type), type, idx); return false; } return true; @@ -884,7 +884,7 @@ bool ResourceManager::validateResource(const char *str, ResType type, ResId idx) void ResourceManager::nukeResource(ResType type, ResId idx) { byte *ptr = _types[type]._resources[idx]._address; if (ptr != NULL) { - debugC(DEBUG_RESOURCE, "nukeResource(%s,%d)", resTypeFromId(type), idx); + debugC(DEBUG_RESOURCE, "nukeResource(%s,%d)", nameOfResType(type), idx); _allocatedSize -= _types[type]._resources[idx]._size; _types[type]._resources[idx].nuke(); } @@ -1523,7 +1523,7 @@ uint16 newTag2Old(uint32 newTag) { } } -const char *resTypeFromId(int type) { +const char *nameOfResType(ResType type) { static char buf[100]; switch (type) { -- cgit v1.2.3 From 45207a52d480cdd70f6bf08bbad76ec3d23a5688 Mon Sep 17 00:00:00 2001 From: Max Horn Date: Fri, 13 May 2011 14:12:00 +0200 Subject: SCUMM: Remove ResTypeData::_num --- engines/scumm/resource.cpp | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) (limited to 'engines/scumm/resource.cpp') diff --git a/engines/scumm/resource.cpp b/engines/scumm/resource.cpp index b24c172a33..b5cfa40534 100644 --- a/engines/scumm/resource.cpp +++ b/engines/scumm/resource.cpp @@ -491,7 +491,7 @@ int ScummEngine::readResTypeList(ResType type) { else num = _fileHandle->readUint16LE(); - if (num != _res->_types[type]._num) { + if (num != _res->_types[type]._resources.size()) { error("Invalid number of %ss (%d) in directory", nameOfResType(type), num); } @@ -534,7 +534,6 @@ void ResourceManager::allocResTypeData(ResType type, uint32 tag, int num, ResTyp error("Too many %s resources (%d) in directory", nameOfResType(type), num); _types[type]._mode = mode; - _types[type]._num = num; _types[type]._tag = tag; _types[type]._resources.resize(num); @@ -604,7 +603,7 @@ void ScummEngine::ensureResourceLoaded(ResType type, ResId idx) { if (type != rtCharset && idx == 0) return; - if (idx <= _res->_types[type]._num && _res->_types[type]._resources[idx]._address) + if (idx <= _res->_types[type]._resources.size() && _res->_types[type]._resources[idx]._address) return; loadResource(type, idx); @@ -627,8 +626,8 @@ int ScummEngine::loadResource(ResType type, ResId idx) { roomNr = getResourceRoomNr(type, idx); - if (idx >= _res->_types[type]._num) - error("%s %d undefined %d %d", nameOfResType(type), idx, _res->_types[type]._num, roomNr); + if (idx >= _res->_types[type]._resources.size()) + error("%s %d undefined %d %d", nameOfResType(type), idx, _res->_types[type]._resources.size(), roomNr); if (roomNr == 0) roomNr = _roomResource; @@ -765,7 +764,7 @@ void ResourceManager::increaseExpireCounter() { void ResourceManager::increaseResourceCounters() { for (ResType type = rtFirst; type <= rtLast; type = ResType(type + 1)) { - ResId idx = _types[type]._num; + ResId idx = _types[type]._resources.size(); while (idx-- > 0) { byte counter = _types[type]._resources[idx].getResourceCounter(); if (counter && counter < RF_USAGE_MAX) { @@ -848,7 +847,6 @@ void ResourceManager::Resource::nuke() { ResourceManager::ResTypeData::ResTypeData() { _mode = kDynamicResTypeMode; - _num = 0; _tag = 0; } @@ -874,7 +872,7 @@ void ResourceManager::setHeapThreshold(int min, int max) { } bool ResourceManager::validateResource(const char *str, ResType type, ResId idx) const { - if (type < rtFirst || type > rtLast || (uint)idx >= (uint)_types[type]._num) { + if (type < rtFirst || type > rtLast || (uint)idx >= (uint)_types[type]._resources.size()) { error("%s Illegal Glob type %s (%d) num %d", str, nameOfResType(type), type, idx); return false; } @@ -1020,7 +1018,7 @@ void ResourceManager::expireResources(uint32 size) { if (_types[type]._mode != kDynamicResTypeMode) { // Resources of this type can be reloaded from the data files, // so we can potentially unload them to free memory. - ResId idx = _types[type]._num; + ResId idx = _types[type]._resources.size(); while (idx-- > 0) { Resource &tmp = _types[type]._resources[idx]; byte counter = tmp.getResourceCounter(); @@ -1045,7 +1043,7 @@ void ResourceManager::expireResources(uint32 size) { void ResourceManager::freeResources() { for (ResType type = rtFirst; type <= rtLast; type = ResType(type + 1)) { - ResId idx = _types[type]._num; + ResId idx = _types[type]._resources.size(); while (idx-- > 0) { if (isResourceLoaded(type, idx)) nukeResource(type, idx); @@ -1087,7 +1085,7 @@ void ResourceManager::resourceStats() { uint32 lockedSize = 0, lockedNum = 0; for (ResType type = rtFirst; type <= rtLast; type = ResType(type + 1)) { - ResId idx = _types[type]._num; + ResId idx = _types[type]._resources.size(); while (idx-- > 0) { Resource &tmp = _types[type]._resources[idx]; if (tmp.isLocked() && tmp._address) { -- cgit v1.2.3 From d63c2d17c210f2cc0b5bf5ddc914f827b42b8511 Mon Sep 17 00:00:00 2001 From: Max Horn Date: Fri, 13 May 2011 14:26:35 +0200 Subject: SCUMM: Improve and document res tag sanity check --- engines/scumm/resource.cpp | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) (limited to 'engines/scumm/resource.cpp') diff --git a/engines/scumm/resource.cpp b/engines/scumm/resource.cpp index b5cfa40534..8eb41ecdfb 100644 --- a/engines/scumm/resource.cpp +++ b/engines/scumm/resource.cpp @@ -662,9 +662,17 @@ int ScummEngine::loadResource(ResType type, ResId idx) { } // Sanity check: Is this the right tag for this resource type? + // + // Currently disabled for newer HE games because they use different + // tags. For example, for rtRoom, 'ROOM' changed to 'RMDA'; and for + // rtImage, 'AWIZ' and 'MULT' can both occur simultaneously. + // On the long run, it would be preferable to not turn this check off, + // but instead to explicitly support the variations in the HE games. tag = _fileHandle->readUint32BE(); if (tag != _res->_types[type]._tag && _game.heversion < 70) { - error("%s %d not in room %d at %d+%d in file %s", + error("Unknown res tag '%s' encountered (expected '%s') " + "while trying to load res (%s,%d) in room %d at %d+%d in file %s", + tag2str(tag), tag2str(_res->_types[type]._tag), nameOfResType(type), idx, roomNr, _fileOffset, fileOffs, _fileHandle->getName()); } -- cgit v1.2.3 From 749a1beb350d33fb1e48494134bdca70bc8c2ad8 Mon Sep 17 00:00:00 2001 From: Max Horn Date: Fri, 13 May 2011 14:39:34 +0200 Subject: SCUMM: Remove useless allocateArrays call, and make sure all resources are properly killed upon restart --- engines/scumm/resource.cpp | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'engines/scumm/resource.cpp') diff --git a/engines/scumm/resource.cpp b/engines/scumm/resource.cpp index 8eb41ecdfb..10f28516b3 100644 --- a/engines/scumm/resource.cpp +++ b/engines/scumm/resource.cpp @@ -535,6 +535,10 @@ void ResourceManager::allocResTypeData(ResType type, uint32 tag, int num, ResTyp _types[type]._mode = mode; _types[type]._tag = tag; + + // If there was data in there, let's clear it out completely. This is important + // in case we are restarting the game. + _types[type]._resources.clear(); _types[type]._resources.resize(num); /* -- cgit v1.2.3 From cf513e3ed64556d05383209664a0cd3909f45399 Mon Sep 17 00:00:00 2001 From: Max Horn Date: Fri, 13 May 2011 14:40:45 +0200 Subject: SCUMM: Get rid of the unused Resource::_globSize field --- engines/scumm/resource.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'engines/scumm/resource.cpp') diff --git a/engines/scumm/resource.cpp b/engines/scumm/resource.cpp index 10f28516b3..85addeb9c3 100644 --- a/engines/scumm/resource.cpp +++ b/engines/scumm/resource.cpp @@ -520,7 +520,8 @@ int ScummEngine_v70he::readResTypeList(ResType type) { } for (idx = 0; idx < num; idx++) { - _res->_types[type]._resources[idx]._globsize = _fileHandle->readUint32LE(); + // The globsize is currently not being used + /*_res->_types[type]._resources[idx]._globsize =*/ _fileHandle->readUint32LE(); } return num; @@ -841,7 +842,6 @@ ResourceManager::Resource::Resource() { _status = 0; _roomno = 0; _roomoffs = 0; - _globsize = 0; } ResourceManager::Resource::~Resource() { -- cgit v1.2.3 From 9ec64a66fe2f20eb1be0811b5f733332efff1b93 Mon Sep 17 00:00:00 2001 From: Max Horn Date: Fri, 13 May 2011 14:48:01 +0200 Subject: SCUMM: Turned ResTypeData into a Common::Array, subsuming its _resource member --- engines/scumm/resource.cpp | 94 +++++++++++++++++++++++----------------------- 1 file changed, 47 insertions(+), 47 deletions(-) (limited to 'engines/scumm/resource.cpp') diff --git a/engines/scumm/resource.cpp b/engines/scumm/resource.cpp index 85addeb9c3..0448f60593 100644 --- a/engines/scumm/resource.cpp +++ b/engines/scumm/resource.cpp @@ -83,8 +83,8 @@ void ScummEngine::openRoom(const int room) { // Load the disk numer / room offs (special case for room 0 exists because // room 0 contains the data which is used to create the roomno / roomoffs // tables -- hence obviously we mustn't use those when loading room 0. - const uint32 diskNumber = room ? _res->_types[rtRoom]._resources[room]._roomno : 0; - const uint32 room_offs = room ? _res->_types[rtRoom]._resources[room]._roomoffs : 0; + const uint32 diskNumber = room ? _res->_types[rtRoom][room]._roomno : 0; + const uint32 room_offs = room ? _res->_types[rtRoom][room]._roomoffs : 0; // FIXME: Since room_offs is const, clearly the following loop either // is never entered, or loops forever (if it wasn't for the return/error @@ -94,7 +94,7 @@ void ScummEngine::openRoom(const int room) { while (room_offs != RES_INVALID_OFFSET) { if (room_offs != 0 && room != 0 && _game.heversion < 98) { - _fileOffset = _res->_types[rtRoom]._resources[room]._roomoffs; + _fileOffset = _res->_types[rtRoom][room]._roomoffs; return; } @@ -122,7 +122,7 @@ void ScummEngine::openRoom(const int room) { return; deleteRoomOffsets(); readRoomsOffsets(); - _fileOffset = _res->_types[rtRoom]._resources[room]._roomoffs; + _fileOffset = _res->_types[rtRoom][room]._roomoffs; if (_fileOffset != 8) return; @@ -157,8 +157,8 @@ void ScummEngine::closeRoom() { /** Delete the currently loaded room offsets. */ void ScummEngine::deleteRoomOffsets() { for (int i = 0; i < _numRooms; i++) { - if (_res->_types[rtRoom]._resources[i]._roomoffs != RES_INVALID_OFFSET) - _res->_types[rtRoom]._resources[i]._roomoffs = 0; + if (_res->_types[rtRoom][i]._roomoffs != RES_INVALID_OFFSET) + _res->_types[rtRoom][i]._roomoffs = 0; } } @@ -174,8 +174,8 @@ void ScummEngine::readRoomsOffsets() { while (num--) { int room = _fileHandle->readByte(); int offset = _fileHandle->readUint32LE(); - if (_res->_types[rtRoom]._resources[room]._roomoffs != RES_INVALID_OFFSET) { - _res->_types[rtRoom]._resources[room]._roomoffs = offset; + if (_res->_types[rtRoom][room]._roomoffs != RES_INVALID_OFFSET) { + _res->_types[rtRoom][room]._roomoffs = offset; } } } @@ -491,7 +491,7 @@ int ScummEngine::readResTypeList(ResType type) { else num = _fileHandle->readUint16LE(); - if (num != _res->_types[type]._resources.size()) { + if (num != _res->_types[type].size()) { error("Invalid number of %ss (%d) in directory", nameOfResType(type), num); } @@ -499,10 +499,10 @@ int ScummEngine::readResTypeList(ResType type) { for (idx = 0; idx < num; idx++) { - _res->_types[type]._resources[idx]._roomno = _fileHandle->readByte(); + _res->_types[type][idx]._roomno = _fileHandle->readByte(); } for (idx = 0; idx < num; idx++) { - _res->_types[type]._resources[idx]._roomoffs = _fileHandle->readUint32LE(); + _res->_types[type][idx]._roomoffs = _fileHandle->readUint32LE(); } return num; @@ -516,12 +516,12 @@ int ScummEngine_v70he::readResTypeList(ResType type) { if (type == rtRoom) for (idx = 0; idx < num; idx++) { - _heV7RoomIntOffsets[idx] = _res->_types[rtRoom]._resources[idx]._roomoffs; + _heV7RoomIntOffsets[idx] = _res->_types[rtRoom][idx]._roomoffs; } for (idx = 0; idx < num; idx++) { // The globsize is currently not being used - /*_res->_types[type]._resources[idx]._globsize =*/ _fileHandle->readUint32LE(); + /*_res->_types[type][idx]._globsize =*/ _fileHandle->readUint32LE(); } return num; @@ -539,8 +539,8 @@ void ResourceManager::allocResTypeData(ResType type, uint32 tag, int num, ResTyp // If there was data in there, let's clear it out completely. This is important // in case we are restarting the game. - _types[type]._resources.clear(); - _types[type]._resources.resize(num); + _types[type].clear(); + _types[type].resize(num); /* TODO: Use multiple Resource subclasses, one for each res mode; then, @@ -608,7 +608,7 @@ void ScummEngine::ensureResourceLoaded(ResType type, ResId idx) { if (type != rtCharset && idx == 0) return; - if (idx <= _res->_types[type]._resources.size() && _res->_types[type]._resources[idx]._address) + if (idx <= _res->_types[type].size() && _res->_types[type][idx]._address) return; loadResource(type, idx); @@ -631,8 +631,8 @@ int ScummEngine::loadResource(ResType type, ResId idx) { roomNr = getResourceRoomNr(type, idx); - if (idx >= _res->_types[type]._resources.size()) - error("%s %d undefined %d %d", nameOfResType(type), idx, _res->_types[type]._resources.size(), roomNr); + if (idx >= _res->_types[type].size()) + error("%s %d undefined %d %d", nameOfResType(type), idx, _res->_types[type].size(), roomNr); if (roomNr == 0) roomNr = _roomResource; @@ -702,27 +702,27 @@ int ScummEngine::loadResource(ResType type, ResId idx) { int ScummEngine::getResourceRoomNr(ResType type, ResId idx) { if (type == rtRoom && _game.heversion < 70) return idx; - return _res->_types[type]._resources[idx]._roomno; + return _res->_types[type][idx]._roomno; } uint32 ScummEngine::getResourceRoomOffset(ResType type, ResId idx) { if (type == rtRoom) { return (_game.version == 8) ? 8 : 0; } - return _res->_types[type]._resources[idx]._roomoffs; + return _res->_types[type][idx]._roomoffs; } uint32 ScummEngine_v70he::getResourceRoomOffset(ResType type, ResId idx) { if (type == rtRoom) { return _heV7RoomIntOffsets[idx]; } - return _res->_types[type]._resources[idx]._roomoffs; + return _res->_types[type][idx]._roomoffs; } int ScummEngine::getResourceSize(ResType type, ResId idx) { byte *ptr = getResourceAddress(type, idx); assert(ptr); - return _res->_types[type]._resources[idx]._size; + return _res->_types[type][idx]._size; } byte *ScummEngine::getResourceAddress(ResType type, ResId idx) { @@ -735,11 +735,11 @@ byte *ScummEngine::getResourceAddress(ResType type, ResId idx) { return NULL; // If the resource is missing, but loadable from the game data files, try to do so. - if (!_res->_types[type]._resources[idx]._address && _res->_types[type]._mode != kDynamicResTypeMode) { + if (!_res->_types[type][idx]._address && _res->_types[type]._mode != kDynamicResTypeMode) { ensureResourceLoaded(type, idx); } - ptr = (byte *)_res->_types[type]._resources[idx]._address; + ptr = (byte *)_res->_types[type][idx]._address; if (!ptr) { debugC(DEBUG_RESOURCE, "getResourceAddress(%s,%d) == NULL", nameOfResType(type), idx); return NULL; @@ -777,9 +777,9 @@ void ResourceManager::increaseExpireCounter() { void ResourceManager::increaseResourceCounters() { for (ResType type = rtFirst; type <= rtLast; type = ResType(type + 1)) { - ResId idx = _types[type]._resources.size(); + ResId idx = _types[type].size(); while (idx-- > 0) { - byte counter = _types[type]._resources[idx].getResourceCounter(); + byte counter = _types[type][idx].getResourceCounter(); if (counter && counter < RF_USAGE_MAX) { setResourceCounter(type, idx, counter + 1); } @@ -788,7 +788,7 @@ void ResourceManager::increaseResourceCounters() { } void ResourceManager::setResourceCounter(ResType type, ResId idx, byte counter) { - _types[type]._resources[idx].setResourceCounter(counter); + _types[type][idx].setResourceCounter(counter); } void ResourceManager::Resource::setResourceCounter(byte counter) { @@ -814,8 +814,8 @@ byte *ResourceManager::createResource(ResType type, ResId idx, uint32 size) { // cases. For instance, Zak tries to reload the intro music // while it's playing. See bug #1253171. - if (_types[type]._resources[idx]._address && (type == rtSound || type == rtScript || type == rtCostume)) - return _types[type]._resources[idx]._address; + if (_types[type][idx]._address && (type == rtSound || type == rtScript || type == rtCostume)) + return _types[type][idx]._address; } nukeResource(type, idx); @@ -829,8 +829,8 @@ byte *ResourceManager::createResource(ResType type, ResId idx, uint32 size) { _allocatedSize += size; - _types[type]._resources[idx]._address = ptr; - _types[type]._resources[idx]._size = size; + _types[type][idx]._address = ptr; + _types[type][idx]._size = size; setResourceCounter(type, idx, 1); return ptr; } @@ -884,7 +884,7 @@ void ResourceManager::setHeapThreshold(int min, int max) { } bool ResourceManager::validateResource(const char *str, ResType type, ResId idx) const { - if (type < rtFirst || type > rtLast || (uint)idx >= (uint)_types[type]._resources.size()) { + if (type < rtFirst || type > rtLast || (uint)idx >= (uint)_types[type].size()) { error("%s Illegal Glob type %s (%d) num %d", str, nameOfResType(type), type, idx); return false; } @@ -892,11 +892,11 @@ bool ResourceManager::validateResource(const char *str, ResType type, ResId idx) } void ResourceManager::nukeResource(ResType type, ResId idx) { - byte *ptr = _types[type]._resources[idx]._address; + byte *ptr = _types[type][idx]._address; if (ptr != NULL) { debugC(DEBUG_RESOURCE, "nukeResource(%s,%d)", nameOfResType(type), idx); - _allocatedSize -= _types[type]._resources[idx]._size; - _types[type]._resources[idx].nuke(); + _allocatedSize -= _types[type][idx]._size; + _types[type][idx].nuke(); } } @@ -928,19 +928,19 @@ int ScummEngine::getResourceDataSize(const byte *ptr) const { void ResourceManager::lock(ResType type, ResId idx) { if (!validateResource("Locking", type, idx)) return; - _types[type]._resources[idx].lock(); + _types[type][idx].lock(); } void ResourceManager::unlock(ResType type, ResId idx) { if (!validateResource("Unlocking", type, idx)) return; - _types[type]._resources[idx].unlock(); + _types[type][idx].unlock(); } bool ResourceManager::isLocked(ResType type, ResId idx) const { if (!validateResource("isLocked", type, idx)) return false; - return _types[type]._resources[idx].isLocked(); + return _types[type][idx].isLocked(); } void ResourceManager::Resource::lock() { @@ -989,13 +989,13 @@ bool ScummEngine::isResourceInUse(ResType type, ResId idx) const { void ResourceManager::setModified(ResType type, ResId idx) { if (!validateResource("Modified", type, idx)) return; - _types[type]._resources[idx].setModified(); + _types[type][idx].setModified(); } bool ResourceManager::isModified(ResType type, ResId idx) const { if (!validateResource("isModified", type, idx)) return false; - return _types[type]._resources[idx].isModified(); + return _types[type][idx].isModified(); } void ResourceManager::Resource::setModified() { @@ -1030,9 +1030,9 @@ void ResourceManager::expireResources(uint32 size) { if (_types[type]._mode != kDynamicResTypeMode) { // Resources of this type can be reloaded from the data files, // so we can potentially unload them to free memory. - ResId idx = _types[type]._resources.size(); + ResId idx = _types[type].size(); while (idx-- > 0) { - Resource &tmp = _types[type]._resources[idx]; + Resource &tmp = _types[type][idx]; byte counter = tmp.getResourceCounter(); if (!tmp.isLocked() && counter >= best_counter && tmp._address && !_vm->isResourceInUse(type, idx)) { best_counter = counter; @@ -1055,12 +1055,12 @@ void ResourceManager::expireResources(uint32 size) { void ResourceManager::freeResources() { for (ResType type = rtFirst; type <= rtLast; type = ResType(type + 1)) { - ResId idx = _types[type]._resources.size(); + ResId idx = _types[type].size(); while (idx-- > 0) { if (isResourceLoaded(type, idx)) nukeResource(type, idx); } - _types[type]._resources.clear(); + _types[type].clear(); } } @@ -1090,16 +1090,16 @@ void ScummEngine::loadPtrToResource(ResType type, ResId idx, const byte *source) bool ResourceManager::isResourceLoaded(ResType type, ResId idx) const { if (!validateResource("isResourceLoaded", type, idx)) return false; - return _types[type]._resources[idx]._address != NULL; + return _types[type][idx]._address != NULL; } void ResourceManager::resourceStats() { uint32 lockedSize = 0, lockedNum = 0; for (ResType type = rtFirst; type <= rtLast; type = ResType(type + 1)) { - ResId idx = _types[type]._resources.size(); + ResId idx = _types[type].size(); while (idx-- > 0) { - Resource &tmp = _types[type]._resources[idx]; + Resource &tmp = _types[type][idx]; if (tmp.isLocked() && tmp._address) { lockedSize += tmp._size; lockedNum++; -- cgit v1.2.3