Skip to content

Commit

Permalink
Refactor|libdoomsday: Implement pimpl in Wad using DENG2_PRIVATE, cle…
Browse files Browse the repository at this point in the history
…anup
  • Loading branch information
danij-deng committed Jun 14, 2014
1 parent 096c7fa commit 7e73027
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 67 deletions.
2 changes: 1 addition & 1 deletion doomsday/client/include/resource/compositetexture.h
Expand Up @@ -56,7 +56,7 @@ class CompositeTexture
* Archived format variants.
*/
enum ArchiveFormat {
DoomFormat, ///< Format used by most id-tech 1 games.
DoomFormat, ///< Format used by most id Tech 1 games.
StrifeFormat ///< Differs slightly from DoomFormat (omits unused values).
};

Expand Down
12 changes: 4 additions & 8 deletions doomsday/client/src/dd_wad.cpp
Expand Up @@ -55,9 +55,7 @@ AutoStr *W_LumpName(lumpnum_t lumpNum)
{
try
{
String const &name = App_FileSystem().lump(lumpNum).name();
QByteArray nameUtf8 = name.toUtf8();
return AutoStr_FromTextStd(nameUtf8.constData());
return AutoStr_FromTextStd(App_FileSystem().lump(lumpNum).name().toUtf8().constData());
}
catch(FS1::NotFoundError const &er)
{
Expand Down Expand Up @@ -87,8 +85,8 @@ AutoStr *W_LumpSourceFile(lumpnum_t lumpNum)
{
try
{
QByteArray path = App_FileSystem().lump(lumpNum).container().composePath().toUtf8();
return AutoStr_FromText(path.constData());
de::File1 const &container = App_FileSystem().lump(lumpNum).container();
return AutoStr_FromText(container.composePath().toUtf8().constData());
}
catch(LumpIndex::NotFoundError const &er)
{
Expand Down Expand Up @@ -116,14 +114,12 @@ dd_bool W_LumpIsCustom(lumpnum_t lumpNum)
#undef W_CheckLumpNumForName
lumpnum_t W_CheckLumpNumForName(char const *name)
{
lumpnum_t lumpNum;
if(!name || !name[0])
{
LOGDEV_RES_WARNING("W_CheckLumpNumForName: Empty lump name, returning invalid lumpnum");
return -1;
}
lumpNum = App_FileSystem().lumpNumForName(name);
return lumpNum;
return App_FileSystem().lumpNumForName(name);
}

#undef W_GetLumpNumForName
Expand Down
3 changes: 1 addition & 2 deletions doomsday/client/src/resource/resourcesystem.cpp
Expand Up @@ -1455,7 +1455,7 @@ DENG2_PIMPL(ResourceSystem)

int uniqueId = 1/*1-based index*/;

/// @todo fixme: Order here does not respect id tech1 logic.
/// @todo fixme: Order here does not respect id Tech 1 logic.
ddstack_t *stack = Stack_New();

LumpIndex const &index = fileSys().nameIndex();
Expand Down Expand Up @@ -2258,7 +2258,6 @@ DENG2_PIMPL(ResourceSystem)

void runTask()
{
/// @todo fixme: Concurrent active plugins!!
DD_CallHooks(HOOK_SAVEGAME_CONVERT, 0, &parm);
}
};
Expand Down
23 changes: 11 additions & 12 deletions doomsday/libdoomsday/include/doomsday/filesys/wad.h
@@ -1,9 +1,7 @@
/** @file wad.h WAD Archive (File).
/** @file wad.h WAD Archive (File).
*
* @todo This should be replaced with a FS2 based WadFolder class.
*
* @author Copyright &copy; 2003-2013 Jaakko Keränen <jaakko.keranen@iki.fi>
* @author Copyright &copy; 2006-2013 Daniel Swanson <danij@dengine.net>
* @author Copyright © 2003-2014 Jaakko Keränen <jaakko.keranen@iki.fi>
* @author Copyright © 2006-2014 Daniel Swanson <danij@dengine.net>
*
* @par License
* GPL: http://www.gnu.org/licenses/gpl.html
Expand All @@ -20,21 +18,24 @@
* 02110-1301 USA</small>
*/

#ifndef LIBDENG_RESOURCE_WAD_H
#define LIBDENG_RESOURCE_WAD_H
#ifndef LIBDOOMSDAY_FILESYS_WAD_H
#define LIBDOOMSDAY_FILESYS_WAD_H

#include "../libdoomsday.h"
#include "file.h"
#include "fileinfo.h"
#include <de/Error>
#include <de/PathTree>

namespace de {

/**
* WAD archive file format.
* @ingroup resource
* @ingroup fs
*
* @see file.h, File1
*
* @todo This should be replaced with a FS2 based WadFolder class.
*/
class LIBDOOMSDAY_PUBLIC Wad : public File1
{
Expand All @@ -47,7 +48,6 @@ class LIBDOOMSDAY_PUBLIC Wad : public File1

public:
Wad(FileHandle &hndl, String path, FileInfo const &info, File1 *container = 0);
~Wad();

/// @return @c true= @a lumpIdx is a valid logical index for a lump in this file.
bool isValidIndex(int lumpIdx) const;
Expand Down Expand Up @@ -166,10 +166,9 @@ class LIBDOOMSDAY_PUBLIC Wad : public File1
static bool recognise(FileHandle &file);

private:
struct Instance;
Instance *d;
DENG2_PRIVATE(d)
};

} // namespace de

#endif /* LIBDENG_RESOURCE_WAD_H */
#endif // LIBDOOMSDAY_FILESYS_WAD_H
75 changes: 31 additions & 44 deletions doomsday/libdoomsday/src/filesys/wad.cpp
@@ -1,9 +1,9 @@
/** @file wad.cpp WAD Archive.
/** @file wad.cpp WAD Archive (file).
*
* @authors Copyright &copy; 2003-2013 Jaakko Keränen <jaakko.keranen@iki.fi>
* @authors Copyright &copy; 2006-2013 Daniel Swanson <danij@dengine.net>
* @authors Copyright &copy; 2006 Jamie Jones <jamie_jones_au@yahoo.com.au>
* @authors Copyright &copy; 1993-1996 by id Software, Inc.
* @authors Copyright © 2003-2014 Jaakko Keränen <jaakko.keranen@iki.fi>
* @authors Copyright © 2006-2014 Daniel Swanson <danij@dengine.net>
* @authors Copyright © 2006 Jamie Jones <jamie_jones_au@yahoo.com.au>
* @authors Copyright © 1993-1996 id Software, Inc.
*
* @par License
* GPL: http://www.gnu.org/licenses/gpl.html
Expand Down Expand Up @@ -167,33 +167,25 @@ class WadFile : public File1
uint crc_;
};

struct Wad::Instance
DENG2_PIMPL(Wad)
{
Wad *self;

/// Number of lump records in the archived wad.
int arcRecordsCount;

/// Offset to the lump record table in the archived wad.
size_t arcRecordsOffset;

/// Directory containing structure and info records for all lumps.
UserDataPathTree *lumpDirectory;
int arcRecordsCount; ///< Number of lump records in the archived wad.
size_t arcRecordsOffset; ///< Offset to the lump record table in the archived wad.
UserDataPathTree *lumpDirectory; ///< Directory structure and info records for all lumps.

/// LUT which maps logical lump indices to PathTreeNodes.
typedef std::vector<UserDataNode *> LumpNodeLut;
LumpNodeLut *lumpNodeLut;

/// Lump data cache.
LumpCache *lumpCache;
LumpCache *lumpCache; ///< Data payload cache.

Instance(Wad *d, FileHandle &file, String path)
: self(d),
arcRecordsCount(0),
arcRecordsOffset(0),
lumpDirectory(0),
lumpNodeLut(0),
lumpCache(0)
Instance(Public *i, FileHandle &file, String path)
: Base(i)
, arcRecordsCount (0)
, arcRecordsOffset(0)
, lumpDirectory (0)
, lumpNodeLut (0)
, lumpCache (0)
{
// Seek to the start of the header.
file.seek(0, SeekSet);
Expand All @@ -208,17 +200,19 @@ struct Wad::Instance

~Instance()
{
self.clearLumpCache();

if(lumpDirectory)
{
lumpDirectory->traverse(PathTree::NoBranch, NULL, PathTree::no_hash, clearWadFileWorker);
delete lumpDirectory;
}

if(lumpNodeLut) delete lumpNodeLut;
if(lumpCache) delete lumpCache;
delete lumpNodeLut;
delete lumpCache;
}

static int clearWadFileWorker(UserDataNode &node, void * /*parameters*/)
static int clearWadFileWorker(UserDataNode &node, void *)
{
WadFile *lump = reinterpret_cast<WadFile *>(node.userPointer());
if(lump)
Expand Down Expand Up @@ -302,8 +296,8 @@ struct Wad::Instance
// We'll load the lump directory using one continous read into a temporary
// local buffer before we process it into our runtime representation.
wadlumprecord_t *arcRecords = new wadlumprecord_t[arcRecordsCount];
self->handle_->seek(arcRecordsOffset, SeekSet);
self->handle_->read((uint8_t *)arcRecords, arcRecordsCount * sizeof(*arcRecords));
self.handle_->seek(arcRecordsOffset, SeekSet);
self.handle_->read((uint8_t *)arcRecords, arcRecordsCount * sizeof(*arcRecords));

// Reserve a small work buffer for processing archived lump names.
ddstring_t absPath;
Expand All @@ -326,12 +320,12 @@ struct Wad::Instance
WadFile *lump =
new WadFile(*dummy,
Str_Text(&absPath),
FileInfo(self->lastModified(), // Inherited from the container (note recursion).
FileInfo(self.lastModified(), // Inherited from the container (note recursion).
i,
littleEndianByteOrder.toNative(arcRecord->filePos),
littleEndianByteOrder.toNative(arcRecord->size),
littleEndianByteOrder.toNative(arcRecord->size)),
self);
thisPublic);
UserDataNode *node = &lumpDirectory->insert(Path(Str_Text(&absPath)));
node->setUserPointer(lump);
}
Expand All @@ -346,7 +340,7 @@ struct Wad::Instance
{
Instance *wadInst = (Instance *)parameters;
WadFile *lump = reinterpret_cast<WadFile *>(node.userPointer());
DENG2_ASSERT(lump && wadInst->self->isValidIndex(lump->info().lumpIdx)); // Sanity check.
DENG2_ASSERT(lump && wadInst->self.isValidIndex(lump->info().lumpIdx)); // Sanity check.
(*wadInst->lumpNodeLut)[lump->info().lumpIdx] = &node;
return 0; // Continue iteration.
}
Expand All @@ -357,24 +351,17 @@ struct Wad::Instance
// Been here already?
if(lumpNodeLut) return;

lumpNodeLut = new LumpNodeLut(self->lumpCount());
lumpNodeLut = new LumpNodeLut(self.lumpCount());
if(!lumpDirectory) return;

lumpDirectory->traverse(PathTree::NoBranch, NULL, PathTree::no_hash, buildLumpNodeLutWorker, (void*)this);
lumpDirectory->traverse(PathTree::NoBranch, NULL, PathTree::no_hash, buildLumpNodeLutWorker, this);
}
};

Wad::Wad(FileHandle &hndl, String path, FileInfo const &info, File1 *container)
: File1(hndl, path, info, container)
{
d = new Instance(this, hndl, path);
}

Wad::~Wad()
{
clearLumpCache();
delete d;
}
, d(new Instance(this, hndl, path))
{}

bool Wad::isValidIndex(int lumpIdx) const
{
Expand Down

0 comments on commit 7e73027

Please sign in to comment.