From 46fee2e3f3ddfb578c8e493daa766ee967396dfb Mon Sep 17 00:00:00 2001 From: danij Date: Fri, 12 Oct 2012 09:54:37 +0100 Subject: [PATCH] Refactor|FileSys: Ordain WadFile lumps named "DEHACKED" a ".deh" extension Now that all files support an extension including those loaded from WAD files, it is no longer necessary for special case kludges so that these are picked up by the DehRead plugin. Simply ordain any lump named "DEHACKED" with a ".deh" extension when reading the lump directory in WadFile and it will be detected in the same way as real files. --- doomsday/engine/portable/src/fs_main.cpp | 122 ++++++++--------------- doomsday/engine/portable/src/wadfile.cpp | 6 +- doomsday/plugins/dehread/src/dehread.cpp | 14 +-- 3 files changed, 48 insertions(+), 94 deletions(-) diff --git a/doomsday/engine/portable/src/fs_main.cpp b/doomsday/engine/portable/src/fs_main.cpp index faff4c4dbd..33e7fccc72 100644 --- a/doomsday/engine/portable/src/fs_main.cpp +++ b/doomsday/engine/portable/src/fs_main.cpp @@ -80,6 +80,8 @@ typedef QList PathMappings; static bool applyPathMapping(ddstring_t* path, PathMapping const& vdm); +static de::DFile* tryOpenFile3(de::DFile& file, const char* path, LumpInfo const& info); + struct FS::Instance { /// Base for indicies in the auxiliary lump index. @@ -1020,83 +1022,37 @@ de::AbstractFile* FS::findLumpFile(char const* path, int* lumpIdx) return NULL; } -static de::DFile* tryOpenFile3(de::DFile* file, const char* path, const LumpInfo* info); - -static de::DFile* openAsLumpFile(de::AbstractFile* container, int lumpIdx, - const char* _absPath, bool isDehackedPatch, bool /*dontBuffer*/) +static de::DFile* tryOpenAsZipFile(de::DFile& hndl, char const* path, LumpInfo const& info) { - ddstring_t absPath; Str_Init(&absPath); - - // Prepare the name of this single-lump file. - if(isDehackedPatch) - { - // Copy the path up to and including the last directory separator if present. - char const* slash = strrchr(_absPath, '/'); - if(slash) - { - Str_PartAppend(&absPath, _absPath, 0, (slash - _absPath) + 1); - } - Str_Append(&absPath, "DEHACKED.lmp"); - } - else - { - Str_Append(&absPath, _absPath); - } - - // Get a handle to the lump we intend to open. - /// @todo The way this buffering works is nonsensical it should not be done here - /// but should instead be deferred until the content of the lump is read. - de::DFile* hndl = DFileBuilder::fromFileLump(*container, lumpIdx, false/*dontBuffer*/); - - // Prepare a the temporary info descriptor. - LumpInfo info = container->lumpInfo(lumpIdx); - - // Try to open the referenced file as a specialised file type. - de::DFile* file = tryOpenFile3(hndl, Str_Text(&absPath), &info); - - // If not opened; assume its a generic LumpFile. - if(!file) + if(ZipFile::recognise(hndl)) { - LumpFile* lump = new LumpFile(*hndl, Str_Text(&absPath), info); - file = DFileBuilder::fromFile(*lump); - } - DENG_ASSERT(file); - - Str_Free(&absPath); - return file; -} - -static de::DFile* tryOpenAsZipFile(de::DFile* hndl, char const* path, LumpInfo const* info) -{ - if(ZipFile::recognise(*hndl)) - { - ZipFile* zip = new ZipFile(*hndl, path, *info); + ZipFile* zip = new ZipFile(hndl, path, info); return DFileBuilder::fromFile(*zip); } return NULL; } -static de::DFile* tryOpenAsWadFile(de::DFile* hndl, char const* path, LumpInfo const* info) +static de::DFile* tryOpenAsWadFile(de::DFile& hndl, char const* path, LumpInfo const& info) { - if(WadFile::recognise(*hndl)) + if(WadFile::recognise(hndl)) { - WadFile* wad = new WadFile(*hndl, path, *info); + WadFile* wad = new WadFile(hndl, path, info); return DFileBuilder::fromFile(*wad); } return NULL; } -static de::DFile* tryOpenFile3(de::DFile* file, char const* path, LumpInfo const* info) +static de::DFile* tryOpenFile3(de::DFile& file, char const* path, LumpInfo const& info) { static const struct filehandler_s { resourcetype_t resourceType; - de::DFile* (*tryOpenFile)(de::DFile* file, char const* path, LumpInfo const* info); + de::DFile* (*tryOpenFile)(de::DFile& file, char const* path, LumpInfo const& info); } handlers[] = { { RT_ZIP, tryOpenAsZipFile }, { RT_WAD, tryOpenAsWadFile }, { RT_NONE, NULL } }, *hdlr = NULL; - DENG_ASSERT(file && path && path[0] && info); + DENG_ASSERT(path && path[0]); resourcetype_t resourceType = F_GuessResourceTypeByName(path); @@ -1130,53 +1086,62 @@ de::DFile* FS::tryOpenFile2(char const* path, char const* mode, size_t baseOffse if(!path || !path[0]) return 0; if(!mode) mode = ""; - bool dontBuffer = !!strchr(mode, 'x'); + //bool dontBuffer = !!strchr(mode, 'x'); bool reqRealFile = !!strchr(mode, 'f'); // Make it a full path. - ddstring_t searchPath; Str_Init(&searchPath); - Str_Set(&searchPath, path); - F_FixSlashes(&searchPath, &searchPath); - F_ExpandBasePath(&searchPath, &searchPath); + AutoStr* searchPath = AutoStr_NewStd(); + Str_Set(searchPath, path); + F_FixSlashes(searchPath, searchPath); + F_ExpandBasePath(searchPath, searchPath); - DEBUG_VERBOSE2_Message(("tryOpenFile2: trying to open %s\n", Str_Text(&searchPath))); + DEBUG_VERBOSE2_Message(("tryOpenFile2: trying to open %s\n", Str_Text(searchPath))); // First check for lumps? if(!reqRealFile) { int lumpIdx; - AbstractFile* container = findLumpFile(Str_Text(&searchPath), &lumpIdx); + AbstractFile* container = findLumpFile(Str_Text(searchPath), &lumpIdx); if(container) { // Do not read files twice. - if(!allowDuplicate && !checkFileId(Str_Text(&searchPath))) return 0; + if(!allowDuplicate && !checkFileId(Str_Text(searchPath))) return 0; - // DeHackEd patch files require special handling... - resourcetype_t type = F_GuessResourceTypeByName(path); + // Get a handle to the lump we intend to open. + /// @todo The way this buffering works is nonsensical it should not be done here + /// but should instead be deferred until the content of the lump is read. + de::DFile* hndl = DFileBuilder::fromFileLump(*container, lumpIdx, false/*dontBuffer*/); - DFile* dfile = openAsLumpFile(container, lumpIdx, Str_Text(&searchPath), (type == RT_DEH), dontBuffer); - Str_Free(&searchPath); - return dfile; + // Prepare a temporary info descriptor. + LumpInfo info = container->lumpInfo(lumpIdx); + + // Try to open the referenced file as a specialised file type. + de::DFile* file = tryOpenFile3(*hndl, Str_Text(searchPath), info); + + // If not opened; assume its a generic LumpFile. + if(!file) + { + LumpFile* lump = new LumpFile(*hndl, Str_Text(searchPath), info); + file = DFileBuilder::fromFile(*lump); + } + DENG_ASSERT(file); + + return file; } } // Try to open as a real file then. We must have an absolute path, so // prepend the current working directory if necessary. - F_PrependWorkPath(&searchPath, &searchPath); + F_PrependWorkPath(searchPath, searchPath); ddstring_t* foundPath = 0; - FILE* nativeFile = findRealFile(Str_Text(&searchPath), mode, &foundPath); - if(!nativeFile) - { - Str_Free(&searchPath); - return 0; - } + FILE* nativeFile = findRealFile(Str_Text(searchPath), mode, &foundPath); + if(!nativeFile) return 0; // Do not read files twice. if(!allowDuplicate && !checkFileId(Str_Text(foundPath))) { fclose(nativeFile); Str_Delete(foundPath); - Str_Free(&searchPath); return NULL; } @@ -1190,17 +1155,16 @@ de::DFile* FS::tryOpenFile2(char const* path, char const* mode, size_t baseOffse // been mapped to another location. We want the file to be attributed with // the path it is to be known by throughout the virtual file system. - DFile* dfile = tryOpenFile3(hndl, Str_Text(&searchPath), &info); + DFile* dfile = tryOpenFile3(*hndl, Str_Text(searchPath), info); // If still not loaded; this an unknown format. if(!dfile) { - GenericFile* file = new GenericFile(*hndl, Str_Text(&searchPath), info); + GenericFile* file = new GenericFile(*hndl, Str_Text(searchPath), info); dfile = DFileBuilder::fromFile(*file); } DENG_ASSERT(dfile); Str_Delete(foundPath); - Str_Free(&searchPath); return dfile; } diff --git a/doomsday/engine/portable/src/wadfile.cpp b/doomsday/engine/portable/src/wadfile.cpp index 8306c0c63d..bd77826e67 100644 --- a/doomsday/engine/portable/src/wadfile.cpp +++ b/doomsday/engine/portable/src/wadfile.cpp @@ -219,10 +219,12 @@ struct WadFile::Instance Str_Set(normName, "________"); } - // All lumps are ordained with the .lmp extension if they don't have one. + // All lumps are ordained with an extension if they don't have one. char const* ext = F_FindFileExtension(Str_Text(normName)); if(!(ext && Str_Length(normName) > ext - Str_Text(normName) + 1)) - Str_Append(normName, ".lmp"); + { + Str_Append(normName, !Str_CompareIgnoreCase(normName, "DEHACKED")? ".deh" : ".lmp"); + } } void readLumpDirectory() diff --git a/doomsday/plugins/dehread/src/dehread.cpp b/doomsday/plugins/dehread/src/dehread.cpp index a8e0e750b8..4fa7f9d3f0 100644 --- a/doomsday/plugins/dehread/src/dehread.cpp +++ b/doomsday/plugins/dehread/src/dehread.cpp @@ -47,20 +47,8 @@ static bool recognisePatchLumpByName(lumpnum_t lumpNum) const char* lumpName = W_LumpName(lumpNum); if(!lumpName) return false; - // Perhaps an in-WAD patch? - if(!qstrnicmp(lumpName, "DEHACKED", 8)) - { - return true; - } - - // Maybe a patch from some other virtual file? const char* ext = F_FindFileExtension(lumpName); - if(ext && !qstricmp(ext, "deh")) - { - return true; - } - - return false; + return (ext && !qstricmp(ext, "deh")); } static void readLump(lumpnum_t lumpNum)