Skip to content

Commit

Permalink
Refactor|FileSys: Minor cleanup refactorings to the libdeng1 file system
Browse files Browse the repository at this point in the history
  • Loading branch information
danij-deng committed Oct 6, 2012
1 parent e9c28e2 commit e71fd76
Show file tree
Hide file tree
Showing 3 changed files with 131 additions and 81 deletions.
81 changes: 48 additions & 33 deletions doomsday/engine/portable/include/fs_main.h
Expand Up @@ -110,44 +110,11 @@ lumpnum_t F_OpenAuxiliary(char const* fileName); /* baseOffset = 0 */

void F_CloseAuxiliary(void);

/**
* Files with a .wad extension are archived data files with multiple 'lumps',
* other files are single lumps whose base filename will become the lump name.
*
* @param path Path to the file to be opened. Either a "real" file in the local
* file system, or a "virtual" file in the virtual file system.
* @param baseOffset Offset from the start of the file in bytes to begin.
* @param allowDuplicate @c true = allow opening multiple copies of the same file.
* @return Handle to the opened file if the operation is successful, else @c NULL.
*/
DFile* F_AddFile(char const* path, size_t baseOffset, boolean allowDuplicate);

/**
* Attempt to remove a file from the virtual file system.
*
* @param permitRequired @c true= allow removal of resources marked as "required"
* by the currently loaded Game.
* @return @c true if the operation is successful.
*/
boolean F_RemoveFile(char const* path, boolean permitRequired);

boolean F_AddFiles(char const* const* paths, int num, boolean allowDuplicate);
boolean F_RemoveFiles(char const* const* paths, int num, boolean permitRequired);

/**
* @return @c true if the file can be opened for reading.
*/
int F_Access(char const* path);

/**
* Try to locate the specified lump for reading.
*
* @param lumpNum Absolute index of the lump to open.
*
* @return Handle to the opened file if found.
*/
DFile* F_OpenLump(lumpnum_t lumpNum);

/**
* Write the data associated with the specified lump index to @a fileName.
*
Expand Down Expand Up @@ -225,6 +192,32 @@ namespace de {
class FS
{
public:
/**
* Files with a .wad extension are archived data files with multiple 'lumps',
* other files are single lumps whose base filename will become the lump name.
*
* @param path Path to the file to be opened. Either a "real" file in the local
* file system, or a "virtual" file in the virtual file system.
* @param baseOffset Offset from the start of the file in bytes to begin.
*
* @return Newly added file instance if the operation is successful, else @c NULL.
*/
static AbstractFile* addFile(char const* path, size_t baseOffset = 0);

/// @note All files are added with baseOffset = @c 0.
static int addFiles(char const* const* paths, int num);

/**
* Attempt to remove a file from the virtual file system.
*
* @param permitRequired @c true= allow removal of resources marked as "required"
* by the currently loaded Game.
* @return @c true if the operation is successful.
*/
static bool removeFile(char const* path, bool permitRequired = false);

static int removeFiles(char const* const* paths, int num, bool permitRequired = false);

/**
* Opens the given file (will be translated) for reading.
*
Expand All @@ -244,6 +237,15 @@ class FS
static DFile* openFile(char const* path, char const* mode, size_t baseOffset = 0,
bool allowDuplicate = true);

/**
* Try to locate the specified lump for reading.
*
* @param absoluteLumpNum Logical lumpnum associated to the file being looked up.
*
* @return Handle to the opened file if found.
*/
static DFile* openLump(lumpnum_t absoluteLumpNum);

/**
* Find a lump in the Zip LumpDirectory.
*
Expand Down Expand Up @@ -285,10 +287,23 @@ extern "C" {
* C wrapper API:
*/

struct abstractfile_s* F_AddFile2(char const* path, size_t baseOffset);
struct abstractfile_s* F_AddFile(char const* path/*, baseOffset = 0*/);

boolean F_RemoveFile2(char const* path, boolean permitRequired);
boolean F_RemoveFile(char const* path/*, permitRequired = false */);

int F_AddFiles(char const* const* paths, int num);

int F_RemoveFiles3(char const* const* paths, int num, boolean permitRequired);
int F_RemoveFiles(char const* const* paths, int num/*, permitRequired = false */);

DFile* F_Open3(char const* path, char const* mode, size_t baseOffset, boolean allowDuplicate);
DFile* F_Open2(char const* path, char const* mode, size_t baseOffset/*, allowDuplicate = true */);
DFile* F_Open(char const* path, char const* mode/*, baseOffset = 0 */);

DFile* F_OpenLump(lumpnum_t absoluteLumpNum);

boolean F_IsValidLumpNum(lumpnum_t absoluteLumpNum);

boolean F_LumpIsCustom(lumpnum_t absoluteLumpNum);
Expand Down
20 changes: 10 additions & 10 deletions doomsday/engine/portable/src/dd_main.c
Expand Up @@ -178,7 +178,7 @@ static void parseStartupFilePathsAndAddFiles(const char* pathString)
token = strtok(buffer, ATWSEPS);
while(token)
{
F_AddFile(token, 0, false);
F_AddFile(token);
token = strtok(NULL, ATWSEPS);
}
free(buffer);
Expand Down Expand Up @@ -254,7 +254,7 @@ static int autoDataAdder(char const* fileName, PathDirectoryNodeType type, void*
if(type == PT_LEAF)
{
int* count = (int*)parameters;
if(F_AddFile(fileName, 0, false))
if(F_AddFile(fileName))
{
if(count) *count += 1;
}
Expand Down Expand Up @@ -380,19 +380,19 @@ static void loadResource(AbstractResource* res)
switch(AbstractResource_ResourceClass(res))
{
case RC_PACKAGE: {
const ddstring_t* path = AbstractResource_ResolvedPath(res, false/*do not locate resource*/);
ddstring_t const* path = AbstractResource_ResolvedPath(res, false/*do not locate resource*/);
if(path)
{
DFile* file = F_AddFile(Str_Text(path), 0, false);
AbstractFile* file = F_AddFile(Str_Text(path));
if(file)
{
// Mark this as an original game resource.
AbstractFile_SetCustom(DFile_File(file), false);
AbstractFile_SetCustom(file, false);

// Print the 'CRC' number of IWADs, so they can be identified.
/// @todo fixme
//if(FT_WADFILE == AbstractFile_Type(DFile_File(file)))
// Con_Message(" IWAD identification: %08x\n", WadFile_CalculateCRC((WadFile*)DFile_File(file)));
//if(FT_WADFILE == AbstractFile_Type(file))
// Con_Message(" IWAD identification: %08x\n", WadFile_CalculateCRC((WadFile*)file));

}
}
Expand Down Expand Up @@ -500,7 +500,7 @@ static int addListFiles(ddstring_t*** list, size_t* listSize, resourcetype_t res
for(i = 0; i < *listSize; ++i)
{
if(resType != F_GuessResourceTypeByName(Str_Text((*list)[i]))) continue;
if(F_AddFile(Str_Text((*list)[i]), 0, false))
if(F_AddFile(Str_Text((*list)[i])))
{
count += 1;
}
Expand Down Expand Up @@ -1362,7 +1362,7 @@ static int DD_StartupWorker(void* parm)
// Add required engine resource files.
{ ddstring_t foundPath; Str_Init(&foundPath);
if(0 == F_FindResource2(RC_PACKAGE, "doomsday.pk3", &foundPath) ||
!F_AddFile(Str_Text(&foundPath), 0, false))
!F_AddFile(Str_Text(&foundPath)))
{
Con_Error("DD_StartupWorker: Failed to locate required resource \"doomsday.pk3\".");
}
Expand Down Expand Up @@ -2031,7 +2031,7 @@ D_CMD(Load)
Str_Strip(&searchPath);

if(F_FindResource3(RC_PACKAGE, Str_Text(&searchPath), &foundPath, RLF_MATCH_EXTENSION) != 0 &&
F_AddFile(Str_Text(&foundPath), 0, false))
F_AddFile(Str_Text(&foundPath)))
didLoadResource = true;
}
Str_Free(&foundPath);
Expand Down
111 changes: 73 additions & 38 deletions doomsday/engine/portable/src/fs_main.cpp
Expand Up @@ -1772,24 +1772,19 @@ static LumpDirectory* lumpDirectoryForFileType(filetype_t fileType)
}
}

struct dfile_s* F_AddFile(char const* path, size_t baseOffset, boolean allowDuplicate)
AbstractFile* FS::addFile(char const* path, size_t baseOffset)
{
DFile* hndl = FS::openFile(path, "rb", baseOffset, CPP_BOOL(allowDuplicate));
DFile* hndl = FS::openFile(path, "rb", baseOffset, false);
if(!hndl)
{
if(allowDuplicate)
{
Con_Message("Warning: F_AddFile: Resource \"%s\" not found, aborting.\n", path);
}
else if(F_Access(path))
if(F_Access(path))
{
Con_Message("\"%s\" already loaded.\n", F_PrettyPath(path));
}
return NULL;
return 0;
}

AbstractFile* file = hndl->file();

VERBOSE( Con_Message("Loading \"%s\"...\n", F_PrettyPath(Str_Text(file->path()))) )

if(loadingForStartup)
Expand All @@ -1806,19 +1801,20 @@ struct dfile_s* F_AddFile(char const* path, size_t baseOffset, boolean allowDupl
{
file->publishLumpsToDirectory(lumpDir);
}
return reinterpret_cast<struct dfile_s*>(hndl);
return file;
}

boolean F_AddFiles(char const* const* paths, int num, boolean allowDuplicate)
int FS::addFiles(char const* const* paths, int num)
{
bool succeeded = false;
if(!paths) return 0;

int addedFileCount = 0;
for(int i = 0; i < num; ++i)
{
if(F_AddFile(paths[i], 0, allowDuplicate))
if(FS::addFile(paths[i]))
{
VERBOSE2( Con_Message("Done loading %s\n", F_PrettyPath(paths[i])) )
succeeded = true; // At least one has been loaded.
addedFileCount += 1;
}
else
{
Expand All @@ -1827,61 +1823,60 @@ boolean F_AddFiles(char const* const* paths, int num, boolean allowDuplicate)
}

// A changed file list may alter the main lump directory.
if(succeeded)
if(addedFileCount)
{
DD_UpdateEngineState();
}
return succeeded;

return addedFileCount;
}

boolean F_RemoveFile(char const* path, boolean permitRequired)
bool FS::removeFile(char const* path, bool permitRequired)
{
bool unloadedResources = unloadFile(path, CPP_BOOL(permitRequired));
bool unloadedResources = unloadFile(path, permitRequired);
if(unloadedResources)
{
DD_UpdateEngineState();
}
return unloadedResources;
}

boolean F_RemoveFiles(char const* const* filenames, int num, boolean permitRequired)
int FS::removeFiles(char const* const* filenames, int num, bool permitRequired)
{
bool succeeded = false;
if(!filenames) return 0;

int removedFileCount = 0;
for(int i = 0; i < num; ++i)
{
if(unloadFile(filenames[i], CPP_BOOL(permitRequired)))
if(unloadFile(filenames[i], permitRequired))
{
VERBOSE2( Con_Message("Done unloading %s\n", F_PrettyPath(filenames[i])) )
succeeded = true; // At least one has been unloaded.
removedFileCount += 1;
}
}

// A changed file list may alter the main lump directory.
if(succeeded)
if(removedFileCount)
{
DD_UpdateEngineState();
}
return succeeded;
return removedFileCount;
}

struct dfile_s* F_OpenLump(lumpnum_t absoluteLumpNum)
DFile* FS::openLump(lumpnum_t absoluteLumpNum)
{
int lumpIdx;
AbstractFile* container = FS::findFileForLumpNum(absoluteLumpNum, &lumpIdx);
if(container)
{
LumpFile* lump = new LumpFile(*DFileBuilder::fromFileLump(*container, lumpIdx, false),
Str_Text(container->composeLumpPath(lumpIdx)),
*container->lumpInfo(lumpIdx));
if(lump)
{
DFile* openFileHndl = DFileBuilder::fromFile(*lump);
openFiles->push_back(openFileHndl); openFileHndl->setList(reinterpret_cast<struct filelist_s*>(openFiles));
return reinterpret_cast<struct dfile_s*>(openFileHndl);
}
}
return NULL;
if(!container) return 0;

LumpFile* lump = new LumpFile(*DFileBuilder::fromFileLump(*container, lumpIdx, false),
Str_Text(container->composeLumpPath(lumpIdx)),
*container->lumpInfo(lumpIdx));
if(!lump) return 0;

DFile* openFileHndl = DFileBuilder::fromFile(*lump);
openFiles->push_back(openFileHndl); openFileHndl->setList(reinterpret_cast<struct filelist_s*>(openFiles));
return openFileHndl;
}

static void clearLDMappings()
Expand Down Expand Up @@ -2412,6 +2407,41 @@ D_CMD(ListFiles)
* C Wrapper API
*/

struct abstractfile_s* F_AddFile2(char const* path, size_t baseOffset)
{
return reinterpret_cast<struct abstractfile_s*>(FS::addFile(path, baseOffset));
}

struct abstractfile_s* F_AddFile(char const* path)
{
return reinterpret_cast<struct abstractfile_s*>(FS::addFile(path));
}

boolean F_RemoveFile2(char const* path, boolean permitRequired)
{
return FS::removeFile(path, CPP_BOOL(permitRequired));
}

boolean F_RemoveFile(char const* path)
{
return FS::removeFile(path);
}

int F_AddFiles(char const* const* paths, int num)
{
return FS::addFiles(paths, num);
}

int F_RemoveFiles2(char const* const* filenames, int num, boolean permitRequired)
{
return FS::removeFiles(filenames, num, CPP_BOOL(permitRequired));
}

int F_RemoveFiles(char const* const* filenames, int num)
{
return FS::removeFiles(filenames, num);
}

void F_ReleaseFile(struct abstractfile_s* file)
{
FS::releaseFile(reinterpret_cast<AbstractFile*>(file));
Expand All @@ -2432,6 +2462,11 @@ struct dfile_s* F_Open(char const* path, char const* mode)
return reinterpret_cast<struct dfile_s*>(FS::openFile(path, mode));
}

struct dfile_s* F_OpenLump(lumpnum_t absoluteLumpNum)
{
return reinterpret_cast<struct dfile_s*>(FS::openLump(absoluteLumpNum));
}

void F_Close(struct dfile_s* hndl)
{
FS::closeFile(reinterpret_cast<DFile*>(hndl));
Expand Down

0 comments on commit e71fd76

Please sign in to comment.