From 3ac02c1196875ff28e4aa9d2804bef811f8decb6 Mon Sep 17 00:00:00 2001 From: Filippos Karapetis Date: Fri, 4 Dec 2015 22:06:47 +0200 Subject: LAB: Simplify file handling code --- engines/lab/labsets.cpp | 16 ++++++---------- engines/lab/map.cpp | 2 -- engines/lab/music.cpp | 24 ++++++------------------ engines/lab/resource.cpp | 26 ++++++++++---------------- 4 files changed, 22 insertions(+), 46 deletions(-) (limited to 'engines') diff --git a/engines/lab/labsets.cpp b/engines/lab/labsets.cpp index 21c495d246..259858763e 100644 --- a/engines/lab/labsets.cpp +++ b/engines/lab/labsets.cpp @@ -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; } diff --git a/engines/lab/map.cpp b/engines/lab/map.cpp index f5746baa42..36591d759b 100644 --- a/engines/lab/map.cpp +++ b/engines/lab/map.cpp @@ -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(); diff --git a/engines/lab/music.cpp b/engines/lab/music.cpp index b6b00cb23c..0f42064a5c 100644 --- a/engines/lab/music.cpp +++ b/engines/lab/music.cpp @@ -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); } /*****************************************************************************/ diff --git a/engines/lab/resource.cpp b/engines/lab/resource.cpp index 5b4c0c101e..aaac1cd261 100644 --- a/engines/lab/resource.cpp +++ b/engines/lab/resource.cpp @@ -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; -- cgit v1.2.3