Skip to content

Commit

Permalink
AURORA: Use ScopedPtr in the ResourceManager
Browse files Browse the repository at this point in the history
  • Loading branch information
DrMcCoy committed Oct 26, 2016
1 parent 58a6176 commit cf7d245
Showing 1 changed file with 66 additions and 65 deletions.
131 changes: 66 additions & 65 deletions src/aurora/resman.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@

#include <cassert>

#include <boost/scope_exit.hpp>

#include "src/common/util.h"
#include "src/common/scopedptr.h"
#include "src/common/error.h"
#include "src/common/readstream.h"
#include "src/common/filepath.h"
Expand Down Expand Up @@ -302,54 +305,48 @@ void ResourceManager::indexArchive(const Common::UString &file, uint32 priority,
if (changeID)
change = newChangeSet(*changeID);

Archive *archive = 0;
try {
Common::SeekableReadStream *archiveStream = openArchiveStream(*knownArchive);

switch (knownArchive->type) {
case kArchiveKEY:
indexKEY(archiveStream, priority, change);
break;

case kArchiveNDS:
archive = new NDSFile(archiveStream);
break;
Common::SeekableReadStream *archiveStream = openArchiveStream(*knownArchive);

case kArchiveHERF:
archive = new HERFFile(archiveStream);
break;
Common::ScopedPtr<Archive> archive;
switch (knownArchive->type) {
case kArchiveKEY:
indexKEY(archiveStream, priority, change);
break;

case kArchiveERF:
archive = new ERFFile(archiveStream, password);
break;
case kArchiveNDS:
archive.reset(new NDSFile(archiveStream));
break;

case kArchiveRIM:
archive = new RIMFile(archiveStream);
break;
case kArchiveHERF:
archive.reset(new HERFFile(archiveStream));
break;

case kArchiveZIP:
archive = new ZIPFile(archiveStream);
break;
case kArchiveERF:
archive.reset(new ERFFile(archiveStream, password));
break;

case kArchiveEXE:
archive = new PEFile(archiveStream, _cursorRemap);
break;
case kArchiveRIM:
archive.reset(new RIMFile(archiveStream));
break;

case kArchiveNSBTX:
archive = new NSBTXFile(archiveStream);
break;
case kArchiveZIP:
archive.reset(new ZIPFile(archiveStream));
break;

default:
throw Common::Exception("Invalid archive type %d", knownArchive->type);
}
case kArchiveEXE:
archive.reset(new PEFile(archiveStream, _cursorRemap));
break;

if (archive)
indexArchive(*knownArchive, archive, priority, change);
case kArchiveNSBTX:
archive.reset(new NSBTXFile(archiveStream));
break;

} catch (...) {
delete archive;
throw;
default:
throw Common::Exception("Invalid archive type %d", knownArchive->type);
}

if (archive)
indexArchive(*knownArchive, archive.release(), priority, change);
}

void ResourceManager::indexArchive(const Common::UString &file, uint32 priority, Common::ChangeID *changeID) {
Expand All @@ -361,35 +358,35 @@ void ResourceManager::indexArchive(const Common::UString &file, uint32 priority,
uint32 ResourceManager::openKEYBIFs(Common::SeekableReadStream *keyStream,
std::vector<KnownArchive *> &archives,
std::vector<BIFFile *> &bifs) {
try {

KEYFile key(*keyStream);

const KEYFile::BIFList &keyBIFs = key.getBIFs();
archives.resize(keyBIFs.size(), 0);
bifs.resize(keyBIFs.size(), 0);
bool success = false;
BOOST_SCOPE_EXIT( (&success) (&archives) (&bifs) ) {
if (!success) {
for (std::vector<BIFFile *>::iterator b = bifs.begin(); b != bifs.end(); ++b)
delete *b;

for (uint32 i = 0; i < keyBIFs.size(); i++) {
archives[i] = findArchive(keyBIFs[i], _knownArchives[kArchiveBIF]);
if (!archives[i])
throw Common::Exception("BIF \"%s\" not found", keyBIFs[i].c_str());

bifs[i] = new BIFFile(openArchiveStream(*archives[i]));
bifs[i]->mergeKEY(key, i);
bifs.clear();
archives.clear();
}
} BOOST_SCOPE_EXIT_END

Common::ScopedPtr<Common::SeekableReadStream> stream(keyStream);
KEYFile key(*keyStream);

} catch (...) {
delete keyStream;
const KEYFile::BIFList &keyBIFs = key.getBIFs();
archives.resize(keyBIFs.size(), 0);
bifs.resize(keyBIFs.size(), 0);

for (std::vector<BIFFile *>::iterator b = bifs.begin(); b != bifs.end(); ++b)
delete *b;
for (uint32 i = 0; i < keyBIFs.size(); i++) {
archives[i] = findArchive(keyBIFs[i], _knownArchives[kArchiveBIF]);
if (!archives[i])
throw Common::Exception("BIF \"%s\" not found", keyBIFs[i].c_str());

bifs.clear();
archives.clear();
throw;
bifs[i] = new BIFFile(openArchiveStream(*archives[i]));
bifs[i]->mergeKEY(key, i);
}

delete keyStream;
success = true;
return archives.size();
}

Expand All @@ -411,14 +408,18 @@ void ResourceManager::indexArchive(KnownArchive &knownArchive, Archive *archive,
throw Common::Exception("ResourceManager::indexArchive(): Archive uses a different name hashing "
"algorithm than we do (%d vs. %d)", (int) hashAlgo, (int) _hashAlgo);

bool couldSet = false;
_openedArchives.push_back(OpenedArchive());

try {
_openedArchives.back().set(knownArchive, *archive);
} catch (...) {
_openedArchives.pop_back();
throw;
}
BOOST_SCOPE_EXIT( (&couldSet) (&_openedArchives) (&archive) ) {
if (!couldSet) {
_openedArchives.pop_back();
delete archive;
}
} BOOST_SCOPE_EXIT_END

_openedArchives.back().set(knownArchive, *archive);
couldSet = true;

// Add the information of the new archive to the change set
if (change)
Expand Down

0 comments on commit cf7d245

Please sign in to comment.