Skip to content

Commit

Permalink
Refactoring continues:
Browse files Browse the repository at this point in the history
* AbstractFile now holds an instance of LumpInfo. All file types
  now share this common base component which stores file/lump
  metadata such as absolute path, size, last-modification time etc...
* LumpFile no longer needs to hold an instance of LumpInfo as this
  is now provided by the abstract base.
* Moved DFILE file open flag up into AbstractFile. Convention is
  that if AbstractFile cannot return a handle then the resource
  is not presently "open".
* Renamed GL_DestroyImagePixels as GL_DestroyImage (standardize)
* Fixed duplicate Loading <resourcename>... log messages output
  when verbose >= 1 due to reentrancy into F_Open when testing
  for attempts to load duplicate files.

Todo: F_Open being recursively reentrant is not exactly ideal,
despite the elegance of the solution. A fair amount of work gets
done twice for every file-open with the no-duplicate predicate.
  • Loading branch information
danij-deng committed Sep 9, 2011
1 parent c22a4ab commit 6cbd6a6
Show file tree
Hide file tree
Showing 21 changed files with 235 additions and 164 deletions.
13 changes: 9 additions & 4 deletions doomsday/engine/portable/include/abstractfile.h
Expand Up @@ -28,6 +28,7 @@
#include <stdio.h>

#include "sys_file.h"
#include "lumpinfo.h"

// File types.
typedef enum {
Expand All @@ -48,26 +49,30 @@ typedef struct abstractfile_s {
filetype_t _type;

struct abstractfile_flags_s {
uint open:1; // Presently open.
uint startup:1; // Loaded during the startup process.
uint iwad:1; // Owned by or is an "iwad" resource.
} _flags;

/// File stream abstraction wrapper/handle for this resource.
/// File stream abstraction wrapper/handle.
DFILE _dfile;

/// Absolute path to this resource in the vfs.
ddstring_t _path;
/// Info descriptor (file metadata).
lumpinfo_t _info;

/// Load order depth index.
uint _order;
} abstractfile_t;

/// Initialize this resource.
void AbstractFile_Init(abstractfile_t* file, filetype_t type, const char* absolutePath);
void AbstractFile_Init(abstractfile_t* file, filetype_t type, const lumpinfo_t* info);

/// @return Type of this resource @see filetype_t
filetype_t AbstractFile_Type(const abstractfile_t* file);

/// @return Immutable copy of the info descriptor for this resource.
const lumpinfo_t* AbstractFile_Info(abstractfile_t* file);

/**
* Accessors:
*/
Expand Down
4 changes: 4 additions & 0 deletions doomsday/engine/portable/include/fs_main.h
Expand Up @@ -186,6 +186,10 @@ int F_Access(const char* path);
/**
* Opens the given file (will be translated) for reading.
*
* \post If @a allowDuplicate = @c false a new file ID for this will have been
* added to the list of known file identifiers if this file hasn't yet been
* opened. It is the responsibility of the caller to release this identifier when done.
*
* @param path Possibly relative or mapped path to the resource being opened.
* @param mode
* 't' = text mode (with real files, lumps are always binary)
Expand Down
2 changes: 1 addition & 1 deletion doomsday/engine/portable/include/image.h
Expand Up @@ -74,7 +74,7 @@ void GL_PrintImageMetadata(const image_t* image);
uint8_t* GL_LoadImageFromFile(image_t* image, abstractfile_t* file);

/// Release image pixel data.
void GL_DestroyImagePixels(image_t* image);
void GL_DestroyImage(image_t* image);

/// @return @c true if the image pixel data contains alpha information.
boolean GL_ImageHasAlpha(const image_t* image);
Expand Down
3 changes: 1 addition & 2 deletions doomsday/engine/portable/include/lumpfile.h
Expand Up @@ -38,11 +38,10 @@ struct lumpdirectory_s;
typedef struct lumpfile_s {
// Base file.
abstractfile_t _base;
lumpinfo_t _info;
void** _cacheData;
} lumpfile_t;

lumpfile_t* LumpFile_New(const char* absolutePath, const lumpname_t name, size_t lumpSize);
lumpfile_t* LumpFile_New(const lumpinfo_t* info);
void LumpFile_Delete(lumpfile_t* lump);

/// Close this file if open and release any acquired file identifiers.
Expand Down
6 changes: 5 additions & 1 deletion doomsday/engine/portable/include/lumpinfo.h
Expand Up @@ -33,11 +33,15 @@
*/
typedef struct {
lumpname_t name; /// Ends in '\0'. Used with WAD lumps.
ddstring_t path; /// Full variable-length path.
ddstring_t path; /// Absolute variable-length path in the vfs.
size_t baseOffset; /// Offset from start of owning package.
size_t size; /// Size of the uncompressed file.
size_t compressedSize; /// Size of the original file compressed.
uint lastModified; /// Unix timestamp.
} lumpinfo_t;

void F_InitLumpInfo(lumpinfo_t* info);
void F_CopyLumpInfo(lumpinfo_t* dst, const lumpinfo_t* src);
void F_DestroyLumpInfo(lumpinfo_t* info);

#endif /* LIBDENG_FILESYS_LUMPINFO_H */
6 changes: 1 addition & 5 deletions doomsday/engine/portable/include/sys_file.h
Expand Up @@ -43,15 +43,11 @@
* higher level abstractfile_t derived objects.
*/
typedef struct {
struct DFILE_flags_s {
unsigned char open:1;
unsigned char eof:1;
} flags;
boolean eof;
size_t size;
FILE* hndl;
uint8_t* data;
uint8_t* pos;
uint lastModified;
} DFILE;

/// @return The length of the file, in bytes.
Expand Down
2 changes: 1 addition & 1 deletion doomsday/engine/portable/include/wadfile.h
Expand Up @@ -46,7 +46,7 @@ typedef struct wadfile_s {
void** _lumpCache;
} wadfile_t;

wadfile_t* WadFile_New(DFILE* handle, const char* absolutePath);
wadfile_t* WadFile_New(DFILE* handle, const lumpinfo_t* info);
void WadFile_Delete(wadfile_t* wad);

/// Close this file if open and release any acquired file identifiers.
Expand Down
2 changes: 1 addition & 1 deletion doomsday/engine/portable/include/zipfile.h
Expand Up @@ -47,7 +47,7 @@ typedef struct {
void** _lumpCache;
} zipfile_t;

zipfile_t* ZipFile_New(DFILE* hndl, const char* absolutePath);
zipfile_t* ZipFile_New(DFILE* hndl, const lumpinfo_t* info);
void ZipFile_Delete(zipfile_t* zip);

/// Close this file if open and release any acquired file identifiers.
Expand Down
26 changes: 15 additions & 11 deletions doomsday/engine/portable/src/abstractfile.c
Expand Up @@ -27,28 +27,25 @@

#include "abstractfile.h"

void AbstractFile_Init(abstractfile_t* file, filetype_t type, const char* absolutePath)
void AbstractFile_Init(abstractfile_t* file, filetype_t type, const lumpinfo_t* info)
{
// Used to favor newer files when duplicates are pruned.
static uint fileCounter = 0;
assert(NULL != file && NULL != absolutePath);
assert(NULL != file && NULL != info);

file->_order = fileCounter++;
file->_type = type;
file->_flags.open = false;
file->_flags.startup = false;
file->_flags.iwad = false;
Str_Init(&file->_path);
Str_Set(&file->_path, absolutePath);
Str_Strip(&file->_path);
F_FixSlashes(&file->_path, &file->_path);

file->_dfile.flags.eof = false;
file->_dfile.flags.open = false;
F_CopyLumpInfo(&file->_info, info);

file->_dfile.eof = false;
file->_dfile.size = 0;
file->_dfile.hndl = NULL;
file->_dfile.data = NULL;
file->_dfile.pos = 0;
file->_dfile.lastModified = 0;
}

filetype_t AbstractFile_Type(const abstractfile_t* file)
Expand All @@ -57,10 +54,16 @@ filetype_t AbstractFile_Type(const abstractfile_t* file)
return file->_type;
}

const lumpinfo_t* AbstractFile_Info(abstractfile_t* file)
{
assert(NULL != file);
return &file->_info;
}

const ddstring_t* AbstractFile_Path(abstractfile_t* file)
{
assert(NULL != file);
return &file->_path;
return &file->_info.path;
}

uint AbstractFile_LoadOrderIndex(abstractfile_t* file)
Expand All @@ -72,12 +75,13 @@ uint AbstractFile_LoadOrderIndex(abstractfile_t* file)
uint AbstractFile_LastModified(abstractfile_t* file)
{
assert(NULL != file);
return file->_dfile.lastModified;
return file->_info.lastModified;
}

DFILE* AbstractFile_Handle(abstractfile_t* file)
{
assert(NULL != file);
if(!file->_flags.open) return NULL;
return &file->_dfile;
}

Expand Down
9 changes: 6 additions & 3 deletions doomsday/engine/portable/src/bitmapfont.c
Expand Up @@ -401,17 +401,20 @@ void BitmapFont_Prepare(font_t* font)
file = F_Open(Str_Text(&bf->_filePath), "rb");
if(NULL != file)
{
DFILE* hndl = AbstractFile_Handle(file);
assert(hndl);

BitmapFont_DeleteGLDisplayLists(font);
BitmapFont_DeleteGLTexture(font);

// Load the font glyph map from the file.
version = inByte(AbstractFile_Handle(file));
version = inByte(hndl);
switch(version)
{
// Original format.
case 0: image = readFormat0(font, AbstractFile_Handle(file)); break;
case 0: image = readFormat0(font, hndl); break;
// Enhanced format.
case 2: image = readFormat2(font, AbstractFile_Handle(file)); break;
case 2: image = readFormat2(font, hndl); break;
default: break;
}
if(!image)
Expand Down
4 changes: 2 additions & 2 deletions doomsday/engine/portable/src/con_busy.c
Expand Up @@ -254,13 +254,13 @@ static void Con_BusyPrepareResources(void)
if(GL_LoadImage(&image, "}data/graphics/loading1.png"))
{
texLoading[0] = GL_NewTextureWithParams(DGL_RGBA, image.width, image.height, image.pixels, TXCF_NEVER_DEFER);
GL_DestroyImagePixels(&image);
GL_DestroyImage(&image);
}

if(GL_LoadImage(&image, "}data/graphics/loading2.png"))
{
texLoading[1] = GL_NewTextureWithParams(DGL_RGBA, image.width, image.height, image.pixels, TXCF_NEVER_DEFER);
GL_DestroyImagePixels(&image);
GL_DestroyImage(&image);
}
}

Expand Down
6 changes: 3 additions & 3 deletions doomsday/engine/portable/src/con_config.c
Expand Up @@ -187,16 +187,16 @@ boolean Con_ParseCommands(const char* fileName, boolean setdefault)
}

// Open the file.
if(!(file = F_Open(fileName, "rt")))
return false;

file = F_Open(fileName, "rt");
if(!file) return false;

VERBOSE(Con_Printf("Con_ParseCommands: %s (def:%i)\n", F_PrettyPath(fileName), setdefault));

// This file is filled with console commands.
// Each line is a command.
{ int line = 1;
DFILE* hndl = AbstractFile_Handle(file);
assert(hndl);
for(;;)
{
M_ReadLine(buff, 512, hndl);
Expand Down
1 change: 1 addition & 0 deletions doomsday/engine/portable/src/dd_help.c
Expand Up @@ -137,6 +137,7 @@ static int DH_ReadStrings(const char* fileName)
}

hndl = AbstractFile_Handle(file);
assert(hndl);
while(!F_AtEnd(hndl))
{
M_ReadLine(line, sizeof(line), hndl);
Expand Down

0 comments on commit 6cbd6a6

Please sign in to comment.