Skip to content

Commit

Permalink
Refactor: Moved path property out of LumpInfo
Browse files Browse the repository at this point in the history
Now that derivatives of AbstractFile store all their lump records
in their own PathDirectory instances, it is no longer necessary
for LumpInfo to store a copy of the absolute path.

WadFile, ZipFile and LumpFile can now dynamically compose the full
vfs path to a lump they contain, should it be needed.

Todo: It should not be necessary to compose an absolute path when
instantiating LumpFile, or when pruning duplicates from the zip
LumpDirectory, or when resolving resource searches.

Todo: LumpInfo::name is now redundant, it can be refactored away.
  • Loading branch information
danij-deng committed Jan 9, 2012
1 parent 255d2dd commit c1f4739
Show file tree
Hide file tree
Showing 18 changed files with 329 additions and 217 deletions.
6 changes: 5 additions & 1 deletion doomsday/engine/portable/include/abstractfile.h
Expand Up @@ -57,6 +57,9 @@ typedef struct abstractfile_s {
/// protected: File stream handle/wrapper.
DFile* _file;

/// Absolute variable-length path in the vfs.
ddstring_t _path;

/// Info descriptor (file metadata).
LumpInfo _info;

Expand All @@ -68,12 +71,13 @@ typedef struct abstractfile_s {
* 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 info Lump info descriptor for the file. A copy is made.
* @return Same as @a af for convenience (chaining).
*/
abstractfile_t* AbstractFile_Init(abstractfile_t* af, filetype_t type,
DFile* file, const LumpInfo* info);
const char* path, DFile* file, const LumpInfo* info);

/**
* Release all memory acquired for objects linked with this resource.
Expand Down
26 changes: 16 additions & 10 deletions doomsday/engine/portable/include/fs_main.h
Expand Up @@ -208,6 +208,16 @@ DFile* F_Open(const char* path, const char* mode); /* baseOffset = 0 */
*/
DFile* F_OpenLump(lumpnum_t lumpNum);

/**
* Write the data associated with the specified lump index to @a fileName.
*
* @param lumpNum Absolute index of the lump to open.
* @param fileName If not @c NULL write the associated data to this path.
* Can be @c NULL in which case the fileName will be chosen automatically.
* @return @c true iff successful.
*/
boolean F_DumpLump(lumpnum_t lumpNum, const char* fileName);

/**
* @return The time when the file was last modified, as seconds since
* the Epoch else zero if the file is not found.
Expand Down Expand Up @@ -256,6 +266,12 @@ void F_Close(DFile* file);
/// Completely destroy this file; close if open, clear references and any acquired identifiers.
void F_Delete(DFile* file);

/// @return Must be free'd with Str_Delete
ddstring_t* F_ComposeLumpPath2(abstractfile_t* fsObject, int lumpIdx, char delimiter);
ddstring_t* F_ComposeLumpPath(abstractfile_t* fsObject, int lumpIdx); /*delimiter='/'*/

struct pathdirectorynode_s* F_LumpDirectoryNode(const LumpInfo* lumpInfo);

const LumpInfo* F_LumpInfo(abstractfile_t* file, int lumpIdx);

size_t F_ReadLumpSection(abstractfile_t* file, int lumpIdx, uint8_t* buffer,
Expand All @@ -265,16 +281,6 @@ const uint8_t* F_CacheLump(abstractfile_t* file, int lumpIdx, int tag);

void F_CacheChangeTag(abstractfile_t* file, int lumpIdx, int tag);

/**
* Write the data associated with the specified lump index to @a fileName.
*
* @param lumpIdx Index of the lump data being dumped.
* @param fileName If not @c NULL write the associated data to this path.
* Can be @c NULL in which case the fileName will be chosen automatically.
* @return @c true iff successful.
*/
boolean F_DumpLump(abstractfile_t* file, int lumpIdx, const char* fileName);

/**
* Parm is passed on to the callback, which is called for each file
* matching the filespec. Absolute path names are given to the callback.
Expand Down
4 changes: 3 additions & 1 deletion doomsday/engine/portable/include/lumpfile.h
Expand Up @@ -41,11 +41,13 @@ typedef struct lumpfile_s {
void** _cacheData;
} LumpFile;

LumpFile* LumpFile_New(DFile* file, const LumpInfo* info);
LumpFile* LumpFile_New(DFile* file, const char* path, const LumpInfo* info);
void LumpFile_Delete(LumpFile* lump);

int LumpFile_PublishLumpsToDirectory(LumpFile* lump, struct lumpdirectory_s* directory);

ddstring_t* LumpFile_ComposeLumpPath(LumpFile* lump, int lumpIdx, char delimiter);

const LumpInfo* LumpFile_LumpInfo(LumpFile* lump, int lumpIdx);

/**
Expand Down
1 change: 0 additions & 1 deletion doomsday/engine/portable/include/lumpinfo.h
Expand Up @@ -40,7 +40,6 @@ typedef struct {
size_t compressedSize; /// Size of the original file compressed.
struct abstractfile_s* container; /// Owning package else @c NULL.
lumpname_t name; /// Ends in '\0'. Used with WAD lumps.
ddstring_t path; /// Absolute variable-length path in the vfs.
} LumpInfo;

void F_InitLumpInfo(LumpInfo* info);
Expand Down
8 changes: 5 additions & 3 deletions doomsday/engine/portable/include/wadfile.h
Expand Up @@ -39,14 +39,16 @@ struct pathdirectorynode_s;
struct wadfile_s; // The wadfile instance (opaque)
typedef struct wadfile_s WadFile;

WadFile* WadFile_New(DFile* file, const LumpInfo* info);
WadFile* WadFile_New(DFile* file, const char* path, const LumpInfo* info);
void WadFile_Delete(WadFile* wad);

int WadFile_PublishLumpsToDirectory(WadFile* file, struct lumpdirectory_s* directory);
int WadFile_PublishLumpsToDirectory(WadFile* wad, struct lumpdirectory_s* directory);

struct pathdirectorynode_s* WadFile_DirectoryNodeForLump(WadFile* wad, int lumpIdx);

const LumpInfo* WadFile_LumpInfo(WadFile* file, int lumpIdx);
ddstring_t* WadFile_ComposeLumpPath(WadFile* wad, int lumpIdx, char delimiter);

const LumpInfo* WadFile_LumpInfo(WadFile* wad, int lumpIdx);

/**
* Read the data associated with the specified lump index into @a buffer.
Expand Down
4 changes: 3 additions & 1 deletion doomsday/engine/portable/include/zipfile.h
Expand Up @@ -41,13 +41,15 @@ struct pathdirectorynode_s;
struct zipfile_s; // The zipfile instance (opaque)
typedef struct zipfile_s ZipFile;

ZipFile* ZipFile_New(DFile* file, const LumpInfo* info);
ZipFile* ZipFile_New(DFile* file, const char* path, const LumpInfo* info);
void ZipFile_Delete(ZipFile* zip);

int ZipFile_PublishLumpsToDirectory(ZipFile* zip, struct lumpdirectory_s* directory);

struct pathdirectorynode_s* ZipFile_DirectoryNodeForLump(ZipFile* zip, int lumpIdx);

ddstring_t* ZipFile_ComposeLumpPath(ZipFile* zip, int lumpIdx, char delimiter);

const LumpInfo* ZipFile_LumpInfo(ZipFile* zip, int lumpIdx);

/**
Expand Down
6 changes: 4 additions & 2 deletions doomsday/engine/portable/src/abstractfile.c
Expand Up @@ -28,7 +28,7 @@
#include "abstractfile.h"

abstractfile_t* AbstractFile_Init(abstractfile_t* af, filetype_t type,
DFile* file, const LumpInfo* info)
const char* path, DFile* file, const LumpInfo* info)
{
// Used to favor newer files when duplicates are pruned.
static uint fileCounter = 0;
Expand All @@ -39,6 +39,7 @@ abstractfile_t* AbstractFile_Init(abstractfile_t* af, filetype_t type,
af->_file = file;
af->_flags.startup = false;
af->_flags.iwad = false;
Str_Init(&af->_path); Str_Set(&af->_path, path);
F_CopyLumpInfo(&af->_info, info);

return af;
Expand All @@ -47,6 +48,7 @@ abstractfile_t* AbstractFile_Init(abstractfile_t* af, filetype_t type,
void AbstractFile_Destroy(abstractfile_t* af)
{
assert(af);
Str_Free(&af->_path);
F_DestroyLumpInfo(&af->_info);
if(af->_file)
DFile_Delete(af->_file, true);
Expand Down Expand Up @@ -85,7 +87,7 @@ DFile* AbstractFile_Handle(abstractfile_t* af)
const ddstring_t* AbstractFile_Path(const abstractfile_t* af)
{
assert(af);
return &af->_info.path;
return &af->_path;
}

uint AbstractFile_LoadOrderIndex(const abstractfile_t* af)
Expand Down
4 changes: 1 addition & 3 deletions doomsday/engine/portable/src/dd_main.c
Expand Up @@ -1556,9 +1556,7 @@ int DD_Main(void)
lumpnum_t absoluteLumpNum = F_CheckLumpNumForName(name);
if(absoluteLumpNum >= 0)
{
int lumpIdx;
abstractfile_t* fsObject = F_FindFileForLumpNum2(absoluteLumpNum, &lumpIdx);
F_DumpLump(fsObject, lumpIdx, NULL);
F_DumpLump(absoluteLumpNum, NULL);
}
}

Expand Down
6 changes: 3 additions & 3 deletions doomsday/engine/portable/src/dd_wad.c
Expand Up @@ -48,13 +48,13 @@ size_t W_LumpLength(lumpnum_t absoluteLumpNum)

const char* W_LumpName(lumpnum_t absoluteLumpNum)
{
const LumpInfo* info = F_FindInfoForLumpNum(absoluteLumpNum);
if(!info)
const char* lumpName = F_LumpName(absoluteLumpNum);
if(!lumpName[0])
{
W_Error("W_LumpName: Invalid lumpnum %i.", absoluteLumpNum);
return NULL;
}
return info->name;
return lumpName;
}

uint W_LumpLastModified(lumpnum_t absoluteLumpNum)
Expand Down
10 changes: 7 additions & 3 deletions doomsday/engine/portable/src/dfile.c
Expand Up @@ -120,9 +120,13 @@ DFile* DFileBuilder_NewFromAbstractFileLump(abstractfile_t* container, int lumpI
Con_Error("DFileBuilder::NewFromAbstractFileLump: Failed on allocation of %lu bytes for data buffer.",
(unsigned long) file->_size);
#if _DEBUG
VERBOSE2( Con_Printf("DFile [%p] buffering \"%s:%s\"...\n", (void*)file,
F_PrettyPath(Str_Text(AbstractFile_Path(container))),
(info->name[0]? info->name : F_PrettyPath(Str_Text(&info->path)))) )
VERBOSE2(
ddstring_t* path = F_ComposeLumpPath(container, lumpIdx);
Con_Printf("DFile [%p] buffering \"%s:%s\"...\n", (void*)file,
F_PrettyPath(Str_Text(AbstractFile_Path(container))),
F_PrettyPath(Str_Text(path)));
Str_Delete(path);
)
#endif
F_ReadLumpSection(container, lumpIdx, (uint8_t*)file->_data, 0, info->size);
}
Expand Down

0 comments on commit c1f4739

Please sign in to comment.