Skip to content

Commit

Permalink
Refactor: Start using the C++ class inteface of de::Uri
Browse files Browse the repository at this point in the history
ResourceNamespace, FileDirectory and various high level mechanisms
now use the C++ class interface of de::Uri.
  • Loading branch information
danij-deng committed Nov 2, 2012
1 parent 6b0c20f commit 0c22faf
Show file tree
Hide file tree
Showing 7 changed files with 110 additions and 196 deletions.
46 changes: 11 additions & 35 deletions doomsday/engine/portable/include/filedirectory.h
Expand Up @@ -51,6 +51,17 @@ class FileDirectory : private PathTree
explicit FileDirectory(char const* basePath = 0);
~FileDirectory();

/**
* Add a new path. Duplicates are automatically pruned.
*
* @param flags @ref searchPathFlags
* @param path Path to be added.
* @param callback Callback to make if the path was added to this directory.
* @param parameters Passed to the callback.
*/
void addPath(int flags, de::Uri const& searchPath,
int (*callback) (Node& node, void* parameters), void* parameters = 0);

/**
* Clear this file directory's contents.
*/
Expand All @@ -71,41 +82,6 @@ class FileDirectory : private PathTree
bool find(NodeType type, char const* searchPath, char searchDelimiter = '/',
ddstring_t* foundPath = 0, char foundDelimiter = '/');

/**
* Add a new path. Duplicates are automatically pruned.
*
* @param flags @ref searchPathFlags
* @param path Path to be added.
* @param callback Callback to make if the path was added to this directory.
* @param parameters Passed to the callback.
*/
void addPath(int flags, uri_s const* searchPath,
int (*callback) (Node& node, void* parameters), void* parameters = 0);

/**
* Add a new set of paths. Duplicates are automatically pruned.
*
* @param flags @ref searchPathFlags
* @param paths One or more paths.
* @param pathsCount Number of elements in @a paths.
* @param callback Callback to make for each path added to this directory.
* @param parameters Passed to the callback.
*/
void addPaths(int flags, uri_s const* const* searchPaths, uint searchPathsCount,
int (*callback) (Node& node, void* parameters) = 0,
void* parameters = 0);

/**
* Add a new set of paths from a path list. Duplicates are automatically pruned.
*
* @param flags @ref searchPathFlags
* @param pathList One or more paths separated by semicolons.
* @param callback Callback to make for each path added to this directory.
* @param parameters Passed to the callback.
*/
void addPathList(int flags, char const* pathList,
int (*callback) (Node& node, void* parameters) = 0, void* parameters = 0);

/**
* Collate all referenced paths in the hierarchy into a list.
*
Expand Down
10 changes: 5 additions & 5 deletions doomsday/engine/portable/include/resourcenamespace.h
Expand Up @@ -78,7 +78,7 @@ namespace de
* @param _uri Unresolved search URI (may include symbolic names or
* other symbol references). SearchPath takes ownership.
*/
SearchPath(int _flags, uri_s* _uri);
SearchPath(int _flags, Uri& _uri);

/**
* Construct a copy from @a other. This is a "deep copy".
Expand All @@ -97,14 +97,14 @@ namespace de
SearchPath& setFlags(int flags);

/// @return Unresolved URI.
uri_s const* uri() const;
Uri const& uri() const;

private:
/// @see searchPathFlags
int flags_;

/// Unresolved search URI.
uri_s* uri_;
Uri* uri_;
};

typedef QMultiMap<PathGroup, SearchPath> SearchPaths;
Expand Down Expand Up @@ -158,12 +158,12 @@ namespace de
* over previously added paths.
*
* @param group Group to add this path to. @see PathGroup
* @param path New unresolved path to add.
* @param path New unresolved path to add. A copy is made.
* @param flags @see searchPathFlags
*
* @return @c true if @a path was well-formed and subsequently added.
*/
bool addSearchPath(PathGroup group, uri_s const* path, int flags);
bool addSearchPath(PathGroup group, Uri const& path, int flags);

/**
* Clear search paths in @a group from this namespace.
Expand Down
130 changes: 58 additions & 72 deletions doomsday/engine/portable/src/dd_main.cpp
Expand Up @@ -274,12 +274,14 @@ void DD_StartTitle(void)
}

/**
* Files with the extensions wad, lmp, pk3, zip and deh in the automatical data directory
* are loaded to the file system.
* Find all game data file paths in the auto directory with the extensions
* wad, lmp, pk3, zip and deh.
*
* @return Number of new files that were loaded.
* @param found List of paths to be populated.
*
* @return Number of paths added to @a found.
*/
static int addFilesFromAutoData(void)
static int findAllGameDataPaths(FS1::PathList& found)
{
static const char* extensions[] = {
"wad", "lmp", "pk3", "zip", "deh",
Expand All @@ -288,81 +290,68 @@ static int addFilesFromAutoData(void)
#endif
0
};

int count = 0;
int const numFoundSoFar = found.count();
for(uint extIdx = 0; extensions[extIdx]; ++extIdx)
{
QByteArray patternUtf8 = String("$(App.DataPath)/$(GamePlugin.Name)/auto/*.%1").arg(extensions[extIdx]).toUtf8();
QByteArray pattern = String("$(App.DataPath)/$(GamePlugin.Name)/auto/*.%1").arg(extensions[extIdx]).toUtf8();
ddstring_t* resolvedPattern = de::Uri(pattern.constData(), RC_NULL).resolved();
if(resolvedPattern)
{
App_FileSystem()->findAllPaths(Str_Text(resolvedPattern), 0, found);
Str_Delete(resolvedPattern);
}
}
return found.count() - numFoundSoFar;
}

uri_s* pattern = Uri_NewWithPath2(patternUtf8.constData(), RC_NULL);
if(ddstring_t const* resolvedPattern = Uri_ResolvedConst(pattern))
/**
* Find and try to load all game data file paths in auto directory.
*
* @return Number of new files that were loaded.
*/
static int loadFilesFromDataGameAuto()
{
FS1::PathList found;
findAllGameDataPaths(found);

int numLoaded = 0;
DENG2_FOR_EACH_CONST(FS1::PathList, i, found)
{
// Ignore directories.
if(i->attrib & A_SUBDIR) continue;

QByteArray foundPath = i->path.toUtf8();
if(tryLoadFile(foundPath.constData()))
{
FS1::PathList found;
if(App_FileSystem()->findAllPaths(Str_Text(resolvedPattern), 0, found))
{
DENG2_FOR_EACH_CONST(FS1::PathList, i, found)
{
// Ignore directories.
if(i->attrib & A_SUBDIR) continue;

QByteArray foundPath = i->path.toUtf8();
if(tryLoadFile(foundPath.constData()))
{
count += 1;
}
}
}
numLoaded += 1;
}
Uri_Delete(pattern);
}
return count;
return numLoaded;
}

/**
* Files with the extensions wad, lmp, pk3, zip and deh in the automatical data
* directory are added to the specified file list.
* Find and list all game data file paths in the auto directory.
*
* @return Number of new files that were added to the list.
*/
static int listFilesFromAutoData(ddstring_t*** list, size_t* listSize)
static int listFilesFromDataGameAuto(ddstring_t*** list, size_t* listSize)
{
static const char* extensions[] = {
"wad", "lmp", "pk3", "zip", "deh",
#ifdef UNIX
"WAD", "LMP", "PK3", "ZIP", "DEH", // upper case alternatives
#endif
0
};

if(!list || !listSize) return 0;

ddstring_t pattern; Str_InitStd(&pattern);
FS1::PathList found;
findAllGameDataPaths(found);

uint numFilesAdded = 0;
for(uint extIdx = 0; extensions[extIdx]; ++extIdx)
int numFilesAdded = 0;
DENG2_FOR_EACH_CONST(FS1::PathList, i, found)
{
QByteArray patternUtf8 = String("$(App.DataPath)/$(GamePlugin.Name)/auto/*.%1").arg(extensions[extIdx]).toUtf8();
// Ignore directories.
if(i->attrib & A_SUBDIR) continue;

uri_s* pattern = Uri_NewWithPath2(patternUtf8.constData(), RC_NULL);
if(ddstring_t const* resolvedPattern = Uri_ResolvedConst(pattern))
{
FS1::PathList found;
if(App_FileSystem()->findAllPaths(Str_Text(resolvedPattern), 0, found))
{
DENG2_FOR_EACH_CONST(FS1::PathList, i, found)
{
// Ignore directories.
if(i->attrib & A_SUBDIR) continue;
QByteArray foundPath = i->path.toUtf8();
addToPathList(list, listSize, foundPath.constData());

QByteArray foundPath = i->path.toUtf8();
addToPathList(list, listSize, foundPath.constData());
numFilesAdded += 1;
}
}
}
Uri_Delete(pattern);
numFilesAdded += 1;
}

return numFilesAdded;
}

Expand Down Expand Up @@ -463,20 +452,18 @@ static int DD_LoadGameStartupResourcesWorker(void* parameters)
// Create default Auto mappings in the runtime directory.

// Data class resources.
uri_s* dataPath = Uri_NewWithPath2("$(App.DataPath)/$(GamePlugin.Name)/auto", RC_NULL);
if(ddstring_t const* resolvedPath = Uri_ResolvedConst(dataPath))
de::Uri dataPath = de::Uri("$(App.DataPath)/$(GamePlugin.Name)/auto/", RC_NULL);
if(ddstring_t const* resolvedPath = dataPath.resolvedConst())
{
F_AddVirtualDirectoryMapping("auto", Str_Text(resolvedPath));
F_AddVirtualDirectoryMapping("auto/", Str_Text(resolvedPath));
}
Uri_Delete(dataPath);

// Definition class resources.
uri_s* defsPath = Uri_NewWithPath2("$(App.DefsPath)/$(GamePlugin.Name)/auto", RC_NULL);
if(ddstring_t const* resolvedPath = Uri_ResolvedConst(defsPath))
de::Uri defsPath = de::Uri("$(App.DefsPath)/$(GamePlugin.Name)/auto/", RC_NULL);
if(ddstring_t const* resolvedPath = defsPath.resolvedConst())
{
F_AddVirtualDirectoryMapping("auto", Str_Text(resolvedPath));
F_AddVirtualDirectoryMapping("auto/", Str_Text(resolvedPath));
}
Uri_Delete(defsPath);
}

/**
Expand Down Expand Up @@ -691,7 +678,7 @@ static int DD_LoadAddonResourcesWorker(void* parameters)
* Phase 3: Add real files from the Auto directory.
* First ZIPs then WADs (they may contain WAD files).
*/
listFilesFromAutoData(&sessionResourceFileList, &numSessionResourceFileList);
listFilesFromDataGameAuto(&sessionResourceFileList, &numSessionResourceFileList);
if(numSessionResourceFileList > 0)
{
addListFiles(&sessionResourceFileList, &numSessionResourceFileList, RT_ZIP);
Expand Down Expand Up @@ -1245,7 +1232,7 @@ static void DD_AutoLoad(void)
* exist in the auto-load directory.
*/
int numNewFiles;
while((numNewFiles = addFilesFromAutoData()) > 0)
while((numNewFiles = loadFilesFromDataGameAuto()) > 0)
{
VERBOSE( Con_Message("Autoload round completed with %i new files.\n", numNewFiles) );
}
Expand Down Expand Up @@ -1440,11 +1427,10 @@ boolean DD_Init(void)
/// to the "packages" ResourceNamespace.

directory_t* dir = Dir_FromText(CommandLine_PathAt(p));
uri_s* searchPath = Uri_NewWithPath2(Dir_Path(dir), RC_PACKAGE);
de::Uri uri = de::Uri(Dir_Path(dir), RC_PACKAGE);

rnamespace->addSearchPath(ResourceNamespace::DefaultPaths, searchPath, SPF_NO_DESCEND);
rnamespace->addSearchPath(ResourceNamespace::DefaultPaths, uri, SPF_NO_DESCEND);

Uri_Delete(searchPath);
Dir_Delete(dir);
}

Expand Down
51 changes: 3 additions & 48 deletions doomsday/engine/portable/src/filedirectory.cpp
Expand Up @@ -243,65 +243,20 @@ void FileDirectory::clear()
d->baseNode = NULL;
}

#if _DEBUG
static void printUriList(uri_s const* const* pathList, size_t pathCount, int indent)
{
if(!pathList) return;

uri_s const* const* pathsIt = pathList;
for(size_t i = 0; i < pathCount && (*pathsIt); ++i, pathsIt++)
{
Uri_DebugPrint(*pathsIt, indent);
}
}
#endif

void FileDirectory::addPath(int flags, uri_s const* searchPath,
void FileDirectory::addPath(int flags, de::Uri const& searchPath,
int (*callback) (Node& node, void* parameters), void* parameters)
{
if(!searchPath)
{
DEBUG_Message(("Warning: FileDirectory::AddPath: Attempt to add zero-length path, ignoring.\n"));
return;
}

#if _DEBUG
VERBOSE2( Con_Message("Adding path to FileDirectory...\n");
Uri_DebugPrint(searchPath, 2/*indent*/) )
searchPath.debugPrint(2/*indent*/) )
#endif

ddstring_t const* resolvedSearchPath = Uri_ResolvedConst(searchPath);
ddstring_t const* resolvedSearchPath = searchPath.resolvedConst();
if(!resolvedSearchPath) return;

// Add new nodes on this path and/or re-process previously seen nodes.
d->addPathNodesAndMaybeDescendBranch(true/*do descend*/, resolvedSearchPath, true/*is-directory*/,
flags, callback, parameters);
}

void FileDirectory::addPaths(int flags,
uri_s const* const* searchPaths, uint searchPathsCount,
int (*callback) (Node& node, void* parameters), void* parameters)
{
if(!searchPaths || searchPathsCount == 0)
{
DEBUG_Message(("Warning: FileDirectory::AddPaths: Attempt to add zero-sized path list, ignoring.\n"));
return;
}

#if _DEBUG
VERBOSE2( Con_Message("Adding paths to FileDirectory...\n");
printUriList(searchPaths, searchPathsCount, 2/*indent*/) )
#endif

for(uint i = 0; i < searchPathsCount; ++i)
{
ddstring_t const* searchPath = Uri_ResolvedConst(searchPaths[i]);
if(!searchPath) continue;

// Add new nodes on this path and/or re-process previously seen nodes.
d->addPathNodesAndMaybeDescendBranch(true/*do descend*/, searchPath, true/*is-directory*/,
flags, callback, parameters);
}
}

} // namespace de

0 comments on commit 0c22faf

Please sign in to comment.