Skip to content

Commit

Permalink
Refactor|FileSys: Files are now located, opened and then later indexed
Browse files Browse the repository at this point in the history
  • Loading branch information
danij-deng committed Oct 23, 2012
1 parent a6579c5 commit e93428d
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 120 deletions.
37 changes: 13 additions & 24 deletions doomsday/engine/portable/include/fs_main.h
Expand Up @@ -56,6 +56,9 @@ namespace de
}

/**
* 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.
*
* Internally the lump index has two parts: the Primary index (which is populated
* with lumps from loaded data files) and the Auxiliary index (used to temporarily
* open a file that is not considered part of the filesystem).
Expand Down Expand Up @@ -141,8 +144,8 @@ namespace de
/**
* Maintains a list of identifiers already seen.
*
* @return @c true if the given file can be opened, or
* @c false, if it has already been opened.
* @return @c true if the given file can be opened, or
* @c false if it has already been opened.
*/
bool checkFileId(char const* path);

Expand All @@ -152,25 +155,19 @@ namespace de
bool accessFile(char const* path);

/**
* 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.
* Indexes @a file (which must have been opened with this file system) into
* this file system and adds it to the list of loaded files.
*
* @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 file The file to index. Assumed to have not yet been indexed!
*
* @return Newly added file instance if the operation is successful, else @c NULL.
* @return This instance.
*/
File1* addFile(char const* path, size_t baseOffset = 0);

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

/**
* Attempt to remove a file from the virtual file system.
*
* @return @c true if the operation is successful.
*
* @return @c true if the operation is successful.
*/
bool removeFile(File1& file);

Expand Down Expand Up @@ -343,13 +340,6 @@ namespace de
*/
File1& interpret(FileHandle& hndl, char const* path, FileInfo const& info);

/**
* Adds a file to any relevant indexes.
*
* @param file File to index.
*/
void index(File1& file);

/**
* Removes a file from any lump indexes.
*
Expand Down Expand Up @@ -403,10 +393,9 @@ int F_LumpCount(void);

int F_Access(char const* path);

struct file1_s* F_AddFile2(char const* path, size_t baseOffset);
struct file1_s* F_AddFile(char const* path/*, baseOffset = 0*/);
void F_Index(struct file1_s* file);

boolean F_RemoveFile(char const* path);
void F_RemoveFile(struct file1_s* file);

FileHandle* F_Open3(char const* path, char const* mode, size_t baseOffset, boolean allowDuplicate);
FileHandle* F_Open2(char const* path, char const* mode, size_t baseOffset/*, allowDuplicate = true */);
Expand Down
62 changes: 51 additions & 11 deletions doomsday/engine/portable/src/dd_main.cpp
Expand Up @@ -69,6 +69,18 @@ static int DD_DummyWorker(void* parameters);
static void DD_AutoLoad();
static void initPathMappings();

/**
* @param path Path to the file to be loaded. Either a "real" file in
* the local file system, or a "virtual" file.
* @param baseOffset Offset from the start of the file in bytes to begin.
* @param file Write the address of the loaded file here if not @c NULL.
*
* @return @c true iff the referenced file was loaded.
*/
static bool tryLoadFile(char const* path, size_t baseOffset = 0, de::File1** file = 0);

static bool tryUnloadFile(char const* path);

extern int renderTextures;
extern int monochrome;

Expand Down Expand Up @@ -194,7 +206,7 @@ static void parseStartupFilePathsAndAddFiles(const char* pathString)
token = strtok(buffer, ATWSEPS);
while(token)
{
App_FileSystem()->addFile(token);
tryLoadFile(token);
token = strtok(NULL, ATWSEPS);
}
free(buffer);
Expand Down Expand Up @@ -293,7 +305,7 @@ static int addFilesFromAutoData(void)
if(i->attrib & A_SUBDIR) continue;

QByteArray foundPath = i->path.toUtf8();
if(App_FileSystem()->addFile(foundPath.constData()))
if(tryLoadFile(foundPath.constData()))
{
count += 1;
}
Expand Down Expand Up @@ -379,8 +391,8 @@ static void loadResource(AbstractResource* res)
ddstring_t const* path = AbstractResource_ResolvedPath(res, false/*do not locate resource*/);
if(path)
{
de::File1* file = App_FileSystem()->addFile(Str_Text(path));
if(file)
de::File1* file;
if(tryLoadFile(Str_Text(path), 0/*base offset*/, &file))
{
// Mark this as an original game resource.
file->setCustom(false);
Expand Down Expand Up @@ -497,7 +509,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(App_FileSystem()->addFile(Str_Text((*list)[i])))
if(tryLoadFile(Str_Text((*list)[i])))
{
count += 1;
}
Expand Down Expand Up @@ -1650,7 +1662,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) ||
!App_FileSystem()->addFile(Str_Text(&foundPath)))
!tryLoadFile(Str_Text(&foundPath)))
{
Con_Error("DD_StartupWorker: Failed to locate required resource \"doomsday.pk3\".");
}
Expand Down Expand Up @@ -2322,7 +2334,7 @@ D_CMD(Load)
Str_Strip(&searchPath);

if(F_FindResource3(RC_PACKAGE, Str_Text(&searchPath), &foundPath, RLF_MATCH_EXTENSION) != 0 &&
App_FileSystem()->addFile(Str_Text(&foundPath)))
tryLoadFile(Str_Text(&foundPath)))
didLoadResource = true;
}
Str_Free(&foundPath);
Expand All @@ -2334,7 +2346,31 @@ D_CMD(Load)
return (didLoadGame || didLoadResource);
}

static void tryUnloadFile(char const* path, bool* didUnloadFiles = 0)
static bool tryLoadFile(char const* path, size_t baseOffset, de::File1** file)
{
try
{
de::FileHandle& hndl = App_FileSystem()->openFile(path, "rb", baseOffset, false /* no duplicates */);

VERBOSE( Con_Message("Loading \"%s\"...\n", F_PrettyPath(Str_Text(hndl.file().composePath()))) )
App_FileSystem()->index(hndl.file());

if(file) *file = &hndl.file();
return true;
}
catch(FS1::NotFoundError const&)
{
if(App_FileSystem()->accessFile(path))
{
// Must already be loaded.
Con_Message("\"%s\" already loaded.\n", F_PrettyPath(path));
}
}
if(file) *file = 0;
return false;
}

static bool tryUnloadFile(char const* path)
{
try
{
Expand All @@ -2346,18 +2382,19 @@ static void tryUnloadFile(char const* path, bool* didUnloadFiles = 0)
Con_Message("\"%s\" is required by the current game.\n"
"Required game files cannot be unloaded in isolation.\n",
F_PrettyPath(Str_Text(file.composePath())));
return;
return false;
}

VERBOSE2( Con_Message("Unloading \"%s\"...\n", F_PrettyPath(Str_Text(file.composePath()))) )
if(App_FileSystem()->removeFile(file))
{
VERBOSE2( Con_Message("Done unloading \"%s\".\n", F_PrettyPath(Str_Text(file.composePath()))) )
if(didUnloadFiles) *didUnloadFiles = true;
return true;
}
}
catch(FS1::NotFoundError const&)
{} // Ignore.
return false;
}

D_CMD(Unload)
Expand Down Expand Up @@ -2421,7 +2458,10 @@ D_CMD(Unload)
{
if(!F_FindResource2(RC_PACKAGE, argv[i], &searchPath)) continue;

tryUnloadFile(Str_Text(&searchPath), &didUnloadFiles);
if(tryUnloadFile(Str_Text(&searchPath)))
{
didUnloadFiles = true;
}
}

if(didUnloadFiles)
Expand Down

0 comments on commit e93428d

Please sign in to comment.