LAB: Simplify file handling code

This commit is contained in:
Filippos Karapetis 2015-12-04 22:06:47 +02:00 committed by Willem Jan Palenstijn
parent 9ba30835a2
commit 3ac02c1196
4 changed files with 22 additions and 46 deletions

View File

@ -58,19 +58,15 @@ void LargeSet::exclElement(uint16 element) {
}
bool LargeSet::readInitialConditions(const char *fileName) {
Common::File *file;
Common::File *file = _vm->_resource->openDataFile(fileName, MKTAG('C', 'O', 'N', '0'));
if ((file = _vm->_resource->openDataFile(fileName, MKTAG('C', 'O', 'N', '0')))) {
uint16 conditions = file->readUint16LE();
for (int i = 0; i < conditions; i++) {
inclElement(file->readUint16LE());
}
delete file;
return true;
uint16 conditions = file->readUint16LE();
for (int i = 0; i < conditions; i++) {
inclElement(file->readUint16LE());
}
return false;
delete file;
return true;
}

View File

@ -171,8 +171,6 @@ static bool loadMapData() {
}
Common::File *mapFile = g_lab->_resource->openDataFile("Lab:Maps", MKTAG('M', 'A', 'P', '0'));
if (!mapFile)
error("Corrupt map file");
g_lab->_music->updateMusic();
if (!g_lab->_music->_doNotFilestopSoundEffect)
g_lab->_music->stopSoundEffect();

View File

@ -176,14 +176,8 @@ bool Music::initMusic() {
filename = "Music:BackGrou";
_file = g_lab->_resource->openDataFile(filename);
if (_file) {
startMusic(true);
return true;
}
_musicOn = false;
return false;
startMusic(true);
return true;
}
/*****************************************************************************/
@ -278,16 +272,10 @@ void Music::changeMusic(const char *newmusic) {
}
_file = g_lab->_resource->openDataFile(newmusic);
if (_file) {
_musicOn = true; /* turn it off */
setMusic(false);
_musicOn = false; /* turn it back on */
setMusic(true);
} else {
_file = _tFile;
_tFile = 0;
}
_musicOn = true; /* turn it off */
setMusic(false);
_musicOn = false; /* turn it back on */
setMusic(true);
}
/*****************************************************************************/

View File

@ -34,7 +34,6 @@
namespace Lab {
static uint16 allocroom;
extern RoomData *_rooms;
extern uint16 NumInv, ManyRooms, HighestCondition;
@ -91,9 +90,7 @@ char *Resource::getText(const char *fileName) {
}
bool Resource::readRoomData(const char *fileName) {
Common::File *dataFile;
if (!(dataFile = openDataFile(fileName, MKTAG('D', 'O', 'R', '1'))))
return false;
Common::File *dataFile = openDataFile(fileName, MKTAG('D', 'O', 'R', '1'));
ManyRooms = dataFile->readUint16LE();
HighestCondition = dataFile->readUint16LE();
@ -120,9 +117,7 @@ bool Resource::readRoomData(const char *fileName) {
}
InventoryData *Resource::readInventory(const char *fileName) {
Common::File *dataFile;
if (!(dataFile = openDataFile(fileName, MKTAG('I', 'N', 'V', '1'))))
return nullptr;
Common::File *dataFile = openDataFile(fileName, MKTAG('I', 'N', 'V', '1'));
NumInv = dataFile->readUint16LE();
InventoryData *inventory = (InventoryData *)malloc((NumInv + 1) * sizeof(InventoryData));
@ -140,11 +135,7 @@ InventoryData *Resource::readInventory(const char *fileName) {
bool Resource::readViews(uint16 roomNum) {
Common::String fileName = "LAB:Rooms/" + Common::String::format("%d", roomNum);
Common::File *dataFile;
if (!(dataFile = openDataFile(fileName.c_str(), MKTAG('R', 'O', 'M', '4'))))
return false;
allocroom = roomNum;
Common::File *dataFile = openDataFile(fileName.c_str(), MKTAG('R', 'O', 'M', '4'));
_rooms[roomNum]._roomMsg = readString(dataFile);
_rooms[roomNum]._view[NORTH] = readView(dataFile);
@ -191,11 +182,14 @@ Common::File *Resource::openDataFile(const char *fileName, uint32 fileHeader) {
Common::File *dataFile = new Common::File();
dataFile->open(translateFileName(fileName));
if (!dataFile->isOpen())
error("openDataFile couldn't open %s (%s)", translateFileName(fileName), fileName);
error("openDataFile: Couldn't open %s (%s)", translateFileName(fileName), fileName);
if (fileHeader > 0 && dataFile->readUint32BE() != fileHeader) {
dataFile->close();
return nullptr;
if (fileHeader > 0) {
uint32 headerTag = dataFile->readUint32BE();
if (headerTag != fileHeader) {
dataFile->close();
error("openDataFile: Unexpected header in %s (%s) - expected: %d, got: %d", translateFileName(fileName), fileName, fileHeader, headerTag);
}
}
return dataFile;