Skip to content

Commit

Permalink
Refactor: LumpFile, WadFile and ZipFile can now inherit from Abstract…
Browse files Browse the repository at this point in the history
…File
  • Loading branch information
danij-deng committed Sep 28, 2012
1 parent 4a8c27a commit 1791793
Show file tree
Hide file tree
Showing 9 changed files with 64 additions and 106 deletions.
17 changes: 5 additions & 12 deletions doomsday/engine/portable/include/abstractfile.h
Expand Up @@ -82,21 +82,17 @@ class AbstractFile
uint _order;

/**
* Initialize this resource.
*
* @param type File type identifier.
* @param path Path to this file in the virtual file system.
* @param file Handle to the file. AbstractFile takes ownership of the handle.
* @param file Handle to the file. Ownership of the handle is given to this instance.
* @param info Lump info descriptor for the file. A copy is made.
*
* @return This instance.
*/
AbstractFile& init(filetype_t type, char const* path, DFile* file, LumpInfo const* info);
AbstractFile(filetype_t type_, char const* path_, DFile* file, LumpInfo const* info_);

/**
* Release all memory acquired for objects linked with this resource.
*/
void destroy();
~AbstractFile();

/**
* @return Type of this resource @see filetype_t
Expand Down Expand Up @@ -175,11 +171,6 @@ extern "C" {
struct abstractfile_s; // The abstractfile instance (opaque)
typedef struct abstractfile_s AbstractFile;

AbstractFile* AbstractFile_Init(AbstractFile* af, filetype_t type,
char const* path, DFile* file, LumpInfo const* info);

void AbstractFile_Destroy(AbstractFile* af);

filetype_t AbstractFile_Type(AbstractFile const* af);

LumpInfo const* AbstractFile_Info(AbstractFile const* af);
Expand Down Expand Up @@ -211,6 +202,8 @@ int AbstractFile_LumpCount(AbstractFile* af);
/// @todo Refactor away:
AbstractFile* UnknownFile_New(DFile* file, char const* path, LumpInfo const* info);

void UnknownFile_Delete(AbstractFile* af);

#ifdef __cplusplus
} // extern "C"
#endif
Expand Down
7 changes: 1 addition & 6 deletions doomsday/engine/portable/include/lumpfile.h
Expand Up @@ -37,13 +37,8 @@ namespace de {
/**
* LumpFile. Runtime representation of a lump-file for use with LumpDirectory
*/
class LumpFile
class LumpFile : public AbstractFile
{
public:
/// Base file instance.
/// @todo Inherit from this instead.
AbstractFile base;

public:
LumpFile(DFile& file, char const* path, LumpInfo const& info);
~LumpFile();
Expand Down
7 changes: 1 addition & 6 deletions doomsday/engine/portable/include/wadfile.h
Expand Up @@ -50,13 +50,8 @@ class PathDirectoryNode;
/**
* WadFile. Runtime representation of an opened WAD file.
*/
class WadFile
class WadFile : public AbstractFile
{
public:
/// Base file instance.
/// @todo Inherit from this instead.
AbstractFile base;

public:
WadFile(DFile& file, char const* path, LumpInfo const& info);
~WadFile();
Expand Down
7 changes: 1 addition & 6 deletions doomsday/engine/portable/include/zipfile.h
Expand Up @@ -52,13 +52,8 @@ class PathDirectoryNode;
/**
* ZipFile. Runtime representation of an opened ZIP file.
*/
class ZipFile
class ZipFile : AbstractFile
{
public:
/// Base file instance.
/// @todo Inherit from this instead.
AbstractFile base;

public:
ZipFile(DFile& file, char const* path, LumpInfo const& info);
~ZipFile();
Expand Down
45 changes: 19 additions & 26 deletions doomsday/engine/portable/src/abstractfile.cpp
Expand Up @@ -28,25 +28,21 @@

#include "abstractfile.h"

de::AbstractFile& de::AbstractFile::init(filetype_t type, char const* path,
DFile* file, const LumpInfo* info)
de::AbstractFile::AbstractFile(filetype_t type_, char const* path_, DFile* file, LumpInfo const* info_)
: _type(type_), _file(file)
{
// Used to favor newer files when duplicates are pruned.
static uint fileCounter = 0;
DENG2_ASSERT(VALID_FILETYPE(type) && info);

DENG2_ASSERT(VALID_FILETYPE(type_) && info_);
_order = fileCounter++;
_type = type;
_file = file;

_flags.startup = false;
_flags.custom = true;
Str_Init(&_path); Str_Set(&_path, path);
F_CopyLumpInfo(&_info, info);

return *this;
Str_Init(&_path); Str_Set(&_path, path_);
F_CopyLumpInfo(&_info, info_);
}

void de::AbstractFile::destroy()
de::AbstractFile::~AbstractFile()
{
Str_Free(&_path);
F_DestroyLumpInfo(&_info);
Expand Down Expand Up @@ -131,19 +127,6 @@ void de::AbstractFile::setCustom(bool yes)
DENG2_ASSERT(inst); \
de::AbstractFile const* self = TOINTERNAL_CONST(inst)

AbstractFile* AbstractFile_Init(AbstractFile* af, filetype_t type,
char const* path, DFile* file, const LumpInfo* info)
{
SELF(af);
return reinterpret_cast<AbstractFile*>(&self->init(type, path, file, info));
}

void AbstractFile_Destroy(AbstractFile* af)
{
SELF(af);
self->destroy();
}

filetype_t AbstractFile_Type(AbstractFile const* af)
{
SELF_CONST(af);
Expand Down Expand Up @@ -218,7 +201,17 @@ void AbstractFile_SetCustom(AbstractFile* af, boolean yes)

AbstractFile* UnknownFile_New(DFile* file, char const* path, LumpInfo const* info)
{
de::AbstractFile* af = new de::AbstractFile();
de::AbstractFile* af = new de::AbstractFile(FT_UNKNOWNFILE, path, file, info);
if(!af) LegacyCore_FatalError("UnknownFile_New: Failed to instantiate new AbstractFile.");
return reinterpret_cast<AbstractFile*>(&af->init(FT_UNKNOWNFILE, path, file, info));
return reinterpret_cast<AbstractFile*>(af);
}

void UnknownFile_Delete(AbstractFile* af)
{
if(af)
{
SELF(af);
F_ReleaseFile(reinterpret_cast<AbstractFile*>(self));
delete self;
}
}
13 changes: 4 additions & 9 deletions doomsday/engine/portable/src/fs_main.c
Expand Up @@ -959,15 +959,10 @@ void F_Delete(DFile* file)
DFile_Close(file);
switch(AbstractFile_Type(af))
{
case FT_UNKNOWNFILE:
F_ReleaseFile(af);
AbstractFile_Destroy(af);
free(af);
break;

case FT_ZIPFILE: ZipFile_Delete( ( ZipFile*)af); break;
case FT_WADFILE: WadFile_Delete( ( WadFile*)af); break;
case FT_LUMPFILE: LumpFile_Delete((LumpFile*)af); break;
case FT_UNKNOWNFILE: UnknownFile_Delete( af); break;
case FT_ZIPFILE: ZipFile_Delete( ( ZipFile*)af); break;
case FT_WADFILE: WadFile_Delete( ( WadFile*)af); break;
case FT_LUMPFILE: LumpFile_Delete((LumpFile*)af); break;
default:
Con_Error("F_Delete: Invalid file type %i.", AbstractFile_Type(af));
exit(1); // Unreachable.
Expand Down
8 changes: 3 additions & 5 deletions doomsday/engine/portable/src/lumpfile.cpp
Expand Up @@ -30,20 +30,18 @@
#include <de/memory.h>

de::LumpFile::LumpFile(DFile& file, char const* path, LumpInfo const& info)
{
reinterpret_cast<de::AbstractFile*>(this)->init(FT_LUMPFILE, path, &file, &info);
}
: AbstractFile(FT_LUMPFILE, path, &file, &info)
{}

de::LumpFile::~LumpFile()
{
F_ReleaseFile(reinterpret_cast<abstractfile_s*>(this));
reinterpret_cast<de::AbstractFile*>(this)->destroy();
}

LumpInfo const* de::LumpFile::lumpInfo(int /*lumpIdx*/)
{
// Lump files are special cases for this *is* the lump.
return reinterpret_cast<de::AbstractFile*>(this)->info();
return this->info();
}

int de::LumpFile::lumpCount()
Expand Down
22 changes: 10 additions & 12 deletions doomsday/engine/portable/src/wadfile.cpp
Expand Up @@ -209,8 +209,8 @@ struct de::WadFile::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];
DFile_Seek(self->base._file, arcRecordsOffset, SEEK_SET);
DFile_Read(self->base._file, (uint8_t*)arcRecords, arcRecordsCount * sizeof(*arcRecords));
DFile_Seek(self->_file, arcRecordsOffset, SEEK_SET);
DFile_Read(self->_file, (uint8_t*)arcRecords, arcRecordsCount * sizeof(*arcRecords));

// Reserve a small work buffer for processing archived lump names.
ddstring_t absPath;
Expand All @@ -228,9 +228,9 @@ struct de::WadFile::Instance
record->baseOffset = de::littleEndianByteOrder.toNative(arcRecord->filePos);
record->info.size = de::littleEndianByteOrder.toNative(arcRecord->size);
record->info.compressedSize = record->info.size;
record->info.container = reinterpret_cast<abstractfile_s*>(&self->base);
record->info.container = reinterpret_cast<abstractfile_s*>(self);
// The modification date is inherited from the file (note recursion).
record->info.lastModified = reinterpret_cast<de::AbstractFile*>(&self->base)->lastModified();
record->info.lastModified = self->lastModified();
record->info.lumpIdx = i;

// Determine the name for this lump in the VFS.
Expand Down Expand Up @@ -281,8 +281,8 @@ struct de::WadFile::Instance
};

de::WadFile::WadFile(DFile& file, char const* path, LumpInfo const& info)
: AbstractFile(FT_WADFILE, path, &file, &info)
{
reinterpret_cast<de::AbstractFile*>(this)->init(FT_WADFILE, path, &file, &info);
d = new Instance(this, file, path);
}

Expand All @@ -291,7 +291,6 @@ de::WadFile::~WadFile()
F_ReleaseFile(reinterpret_cast<abstractfile_s*>(this));
clearLumpCache();
delete d;
reinterpret_cast<de::AbstractFile*>(this)->destroy();
}

bool de::WadFile::isValidIndex(int lumpIdx)
Expand Down Expand Up @@ -414,7 +413,7 @@ uint8_t const* de::WadFile::cacheLump(int lumpIdx)

const LumpInfo* info = lumpInfo(lumpIdx);
LOG_TRACE("\"%s:%s\" (%lu bytes%s)")
<< F_PrettyPath(Str_Text(reinterpret_cast<de::AbstractFile*>(this)->path()))
<< F_PrettyPath(Str_Text(path()))
<< F_PrettyPath(Str_Text(composeLumpPath(lumpIdx, '/')))
<< (unsigned long) info->size
<< (info->compressedSize != info->size? ", compressed" : "");
Expand All @@ -441,8 +440,7 @@ de::WadFile& de::WadFile::unlockLump(int lumpIdx)
{
LOG_AS("WadFile::unlockLump");
LOG_TRACE("\"%s:%s\"")
<< F_PrettyPath(Str_Text(reinterpret_cast<de::AbstractFile*>(this)->path()))
<< F_PrettyPath(Str_Text(composeLumpPath(lumpIdx, '/')));
<< F_PrettyPath(Str_Text(path())) << F_PrettyPath(Str_Text(composeLumpPath(lumpIdx, '/')));

if(isValidIndex(lumpIdx))
{
Expand Down Expand Up @@ -471,7 +469,7 @@ size_t de::WadFile::readLumpSection(int lumpIdx, uint8_t* buffer, size_t startOf
if(!lrec) return 0;

LOG_TRACE("\"%s:%s\" (%lu bytes%s) [%lu +%lu]")
<< F_PrettyPath(Str_Text(reinterpret_cast<de::AbstractFile*>(this)->path()))
<< F_PrettyPath(Str_Text(path()))
<< F_PrettyPath(Str_Text(composeLumpPath(lumpIdx, '/')))
<< (unsigned long) lrec->info.size
<< (lrec->info.compressedSize != lrec->info.size? ", compressed" : "")
Expand All @@ -491,8 +489,8 @@ size_t de::WadFile::readLumpSection(int lumpIdx, uint8_t* buffer, size_t startOf
}
}

DFile_Seek(base._file, lrec->baseOffset + startOffset, SEEK_SET);
size_t readBytes = DFile_Read(base._file, buffer, length);
DFile_Seek(_file, lrec->baseOffset + startOffset, SEEK_SET);
size_t readBytes = DFile_Read(_file, buffer, length);

/// @todo Do not check the read length here.
if(readBytes < length)
Expand Down

0 comments on commit 1791793

Please sign in to comment.