Skip to content

Commit

Permalink
Refactor|FS1: Reworked findLumpFile() similarly to lumpInfo()
Browse files Browse the repository at this point in the history
The logic of FS1::findLumpFile() was actually semantically closer to
that of FS1::lumpInfo() however were the former searched the Zip lump
index the latter used the active Wad lump index.

Renamed to FS1::zipLumpInfo() - this method is now a logical parallel
of lumpInfo() using the Zip lump index.
  • Loading branch information
danij-deng committed Oct 17, 2012
1 parent c1b97ad commit 7f0c06b
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 74 deletions.
26 changes: 14 additions & 12 deletions doomsday/engine/portable/include/fs_main.h
Expand Up @@ -205,17 +205,29 @@ namespace de
}

/**
* Retrieve the LumpInfo metadata structure for a lump.
* Retrieve the LumpInfo metadata record for a lump in the Wad lump index.
*
* @post The active LumpIndex may have changed!
*
* @param absoluteLumpNum Logical lumpnum associated to the file being looked up.
* @param lumpIdx If not @c NULL the translated lumpnum within the owning
* file object is written here.
* @return
*
* @return Metadata record for the lump else @c NULL.
*/
LumpInfo const* lumpInfo(lumpnum_t absoluteLumpNum, int* lumpIdx = 0);

/**
* Retrieve the LumpInfo metadata record for a lump in the Zip lump index.
*
* @param path Path to search for (made absolute if necessary).
* @param lumpIdx If not @c NULL the translated lumpnum within the owning
* file object is written here.
*
* @return Metadata record for the lump else @c NULL.
*/
LumpInfo const* zipLumpInfo(char const* path, int* lumpIdx = 0);

// Convenient lookup method for when only the length property is needed from a LumpInfo.
inline size_t lumpLength(lumpnum_t absoluteLumpNum) {
if(LumpInfo const* info = lumpInfo(absoluteLumpNum)) return info->size;
Expand Down Expand Up @@ -365,16 +377,6 @@ namespace de
bool allowDuplicate, LumpInfo& info);

AbstractFile* tryOpenFile(char const* path, char const* mode, size_t baseOffset, bool allowDuplicate);

public:
/**
* Find a lump in the Zip LumpIndex.
*
* @param path Path to search for. Relative paths are made absolute if necessary.
* @param lumpIdx If not @c NULL the translated lumpnum within the owning file object is written here.
* @return File system object representing the file which contains the found lump else @c NULL.
*/
AbstractFile* findLumpFile(char const* path, int* lumpIdx = 0);
};

} // namespace de
Expand Down
113 changes: 54 additions & 59 deletions doomsday/engine/portable/src/fs_main.cpp
Expand Up @@ -621,17 +621,64 @@ lumpnum_t FS1::lumpNumForName(char const* name, bool silent)

LumpInfo const* FS1::lumpInfo(lumpnum_t absoluteLumpNum, int* lumpIdx)
{
if(lumpIdx) *lumpIdx = -1;

d->selectWadLumpIndex(absoluteLumpNum); // No longer absolute after this call.
if(!d->ActiveWadLumpIndex->isValidIndex(absoluteLumpNum))
{
if(lumpIdx) *lumpIdx = -1;
return 0;
}
if(!d->ActiveWadLumpIndex->isValidIndex(absoluteLumpNum)) return 0;

LumpInfo const& info = d->ActiveWadLumpIndex->lumpInfo(absoluteLumpNum);
if(lumpIdx) *lumpIdx = info.lumpIdx;
return &info;
}

LumpInfo const* FS1::zipLumpInfo(char const* path, int* lumpIdx)
{
if(lumpIdx) *lumpIdx = -1;
if(!path || !path[0]) return 0;

/*
* First check the Zip directory.
*/

// Convert to an absolute path.
AutoStr* absSearchPath = AutoStr_FromTextStd(path);
F_PrependBasePath(absSearchPath, absSearchPath);

// Perform the search.
lumpnum_t lumpNum = d->zipLumpIndex.indexForPath(Str_Text(absSearchPath));
if(lumpNum >= 0)
{
LumpInfo const& info = d->zipLumpIndex.lumpInfo(lumpNum);
if(lumpIdx) *lumpIdx = info.lumpIdx;
return &info;
}

/*
* Next try the dir/WAD redirects.
*/
if(!d->lumpMappings.empty())
{
// We must have an absolute path - prepend the CWD if necessary.
Str_Set(absSearchPath, path);
F_PrependWorkPath(absSearchPath, absSearchPath);

DENG2_FOR_EACH(i, d->lumpMappings, LumpMappings::const_iterator)
{
LumpMapping const& found = *i;
QByteArray foundPathUtf8 = found.first.toUtf8();
if(qstricmp(foundPathUtf8.constData(), Str_Text(absSearchPath))) continue;

QByteArray foundLumpNameUtf8 = found.second.toUtf8();
lumpnum_t absoluteLumpNum = lumpNumForName(foundLumpNameUtf8.constData());
if(absoluteLumpNum < 0) continue;

return lumpInfo(absoluteLumpNum, lumpIdx);
}
}

return 0; // Not found.
}

de::AbstractFile* FS1::lumpFile(lumpnum_t absoluteLumpNum, int* lumpIdx)
{
LumpInfo const* info = lumpInfo(absoluteLumpNum, lumpIdx);
Expand Down Expand Up @@ -892,59 +939,6 @@ int FS1::findAllPaths(char const* rawSearchPattern, int flags, FS1::PathList& fo
return found.count() - numFoundSoFar;
}

de::AbstractFile* FS1::findLumpFile(char const* path, int* lumpIdx)
{
if(lumpIdx) *lumpIdx = -1;
if(!path || !path[0]) return 0;

/**
* First check the Zip directory.
*/

// Convert to an absolute path.
ddstring_t absSearchPath;
Str_Init(&absSearchPath); Str_Set(&absSearchPath, path);
F_PrependBasePath(&absSearchPath, &absSearchPath);

// Perform the search.
lumpnum_t lumpNum = d->zipLumpIndex.indexForPath(Str_Text(&absSearchPath));
if(lumpNum >= 0)
{
Str_Free(&absSearchPath);

LumpInfo const& lumpInfo = d->zipLumpIndex.lumpInfo(lumpNum);
if(lumpIdx) *lumpIdx = lumpInfo.lumpIdx;
return reinterpret_cast<AbstractFile*>(lumpInfo.container);
}

/**
* Next try the dir/WAD redirects.
*/
if(!d->lumpMappings.empty())
{
// We must have an absolute path - prepend the CWD if necessary.
Str_Set(&absSearchPath, path);
F_PrependWorkPath(&absSearchPath, &absSearchPath);

DENG2_FOR_EACH(i, d->lumpMappings, LumpMappings::const_iterator)
{
LumpMapping const& found = *i;
QByteArray foundPathUtf8 = found.first.toUtf8();
if(qstricmp(foundPathUtf8.constData(), Str_Text(&absSearchPath))) continue;

QByteArray foundLumpNameUtf8 = found.second.toUtf8();
lumpnum_t absoluteLumpNum = lumpNumForName(foundLumpNameUtf8.constData());
if(absoluteLumpNum < 0) continue;

Str_Free(&absSearchPath);
return lumpFile(absoluteLumpNum, lumpIdx);
}
}

Str_Free(&absSearchPath);
return NULL;
}

de::AbstractFile& FS1::interpret(de::DFile& hndl, char const* path, LumpInfo const& info)
{
DENG_ASSERT(path && path[0]);
Expand Down Expand Up @@ -1002,7 +996,8 @@ de::DFile* FS1::tryOpenLump(char const* path, char const* /*mode*/, size_t /*bas
DENG_ASSERT(path && path[0]);

int lumpIdx;
AbstractFile* container = findLumpFile(path, &lumpIdx);
LumpInfo const* lumpInfo = zipLumpInfo(path, &lumpIdx);
AbstractFile* container = lumpInfo->container;
if(!container) return 0;

// Do not read files twice.
Expand Down
6 changes: 3 additions & 3 deletions doomsday/engine/portable/src/game.cpp
Expand Up @@ -162,11 +162,11 @@ bool Game::isRequiredResource(char const* absolutePath)
if(records)
{
// Is this resource from a container?
AbstractFile* file = App_FileSystem()->findLumpFile(absolutePath);
if(file)
LumpInfo const* info = App_FileSystem()->zipLumpInfo(absolutePath);
if(info)
{
// Yes; use the container's path instead.
absolutePath = Str_Text(file->path());
absolutePath = Str_Text(info->container->path());
}

for(AbstractResource* const* i = records; *i; i++)
Expand Down

0 comments on commit 7f0c06b

Please sign in to comment.