Skip to content

Commit

Permalink
Refactor|ResourceNamespace: ResourceNamespace API now uses de::String
Browse files Browse the repository at this point in the history
  • Loading branch information
danij-deng committed Nov 9, 2012
1 parent 64381ad commit 32eb688
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 100 deletions.
1 change: 1 addition & 0 deletions doomsday/engine/portable/include/file.h
Expand Up @@ -26,6 +26,7 @@

#ifdef __cplusplus

#include "dd_share.h"
#include <de/Error>
#include <de/str.h>
#include "filehandle.h"
Expand Down
44 changes: 6 additions & 38 deletions doomsday/engine/portable/include/resourcenamespace.h
Expand Up @@ -30,10 +30,10 @@

/**
* @defgroup searchPathFlags Search Path Flags
* @{
*/
///@{
#define SPF_NO_DESCEND 0x1 ///< Do not decend into branches when populating paths.
/**@}*/
///@}

namespace de
{
Expand Down Expand Up @@ -111,11 +111,11 @@ namespace de
typedef QList<PathTree::Node*> ResourceList;

public:
explicit ResourceNamespace(char const* symbolicName = "");
explicit ResourceNamespace(String symbolicName);
~ResourceNamespace();

/// @return Symbolic name of this namespace (e.g., "Models").
ddstring_t const* name() const;
String const& name() const;

/**
* Rebuild this namespace by re-scanning for resources on all search
Expand Down Expand Up @@ -151,7 +151,7 @@ namespace de
*
* @return Number of found resources.
*/
int findAll(ddstring_t const* searchPath, ResourceList& found);
int findAll(String searchPath, ResourceList& found);

/**
* Add a new search path to this namespace. Newer paths have priority
Expand All @@ -178,39 +178,7 @@ namespace de
void clearSearchPaths();

/**
* Append to @a pathList all resolved search paths in @a group, in
* descending (i.e., most recent) order.
*
* @param group Group of paths to append.
* @param pathList String to receive the search path list.
* @param delimiter Discreet paths will be delimited by this character.
*
* @return Same as @a pathList for caller convenience.
*/
ddstring_t* listSearchPaths(PathGroup group, ddstring_t* pathList,
char delimiter = ';') const;

/**
* Append to @a pathList all resolved search paths from all groups,
* firstly in priority group order (from Override to Fallback) and
* secondly in descending (i.e., most recent) order.
*
* @param pathList String to receive the search paths.
* @param delimiter Discreet paths will be delimited by this character.
*
* @return Same as @a pathList for caller convenience.
*/
ddstring_t* listSearchPaths(ddstring_t* pathList, char delimiter = ';') const
{
listSearchPaths(OverridePaths, pathList, delimiter);
listSearchPaths(ExtraPaths, pathList, delimiter);
listSearchPaths(DefaultPaths, pathList, delimiter);
listSearchPaths(FallbackPaths, pathList, delimiter);
return pathList;
}

/**
* Provides access to the search paths for efficent traversals.
* Provides access to the search paths for efficient traversals.
*/
SearchPaths const& searchPaths() const;

Expand Down
2 changes: 0 additions & 2 deletions doomsday/engine/portable/include/sys_reslocator.h
Expand Up @@ -122,8 +122,6 @@ void F_ResetResourceNamespace(resourcenamespaceid_t rni);

void F_CreateNamespacesForFileResourcePaths(void);

ddstring_t const* F_ResourceNamespaceName(resourcenamespaceid_t rni);

/// @return Number of resource namespaces.
uint F_NumResourceNamespaces(void);

Expand Down
48 changes: 12 additions & 36 deletions doomsday/engine/portable/src/resourcenamespace.cpp
Expand Up @@ -177,6 +177,8 @@ struct ResourceNamespace::Instance
{
ResourceNamespace& self;

/// Symbolic name of this namespace.
String name;
/// Associated path directory for this namespace.
/// @todo It should not be necessary for a unique directory per namespace.
PathTree directory;
Expand All @@ -194,20 +196,9 @@ struct ResourceNamespace::Instance
/// Each set is in order of greatest-importance, right to left.
ResourceNamespace::SearchPaths searchPaths;

/// Symbolic name of this namespace.
ddstring_t name;

Instance(ResourceNamespace& d, char const* _name)
: self(d), directory(), rootNode(0), nameHash(), nameHashIsDirty(true)
{
Str_InitStd(&name);
if(_name) Str_Set(&name, _name);
}

~Instance()
{
Str_Free(&name);
}
Instance(ResourceNamespace& d, String _name)
: self(d), name(_name), directory(), rootNode(0), nameHash(), nameHashIsDirty(true)
{}

NameHash::Node* findResourceInNameHash(NameHash::key_type key,
PathTree::Node const& ptNode)
Expand Down Expand Up @@ -361,7 +352,7 @@ struct ResourceNamespace::Instance
}
};

ResourceNamespace::ResourceNamespace(char const* symbolicName)
ResourceNamespace::ResourceNamespace(String symbolicName)
{
d = new Instance(*this, symbolicName);
}
Expand All @@ -371,9 +362,9 @@ ResourceNamespace::~ResourceNamespace()
delete d;
}

ddstring_t const* ResourceNamespace::name() const
String const& ResourceNamespace::name() const
{
return &d->name;
return d->name;
}

void ResourceNamespace::clear()
Expand All @@ -393,7 +384,6 @@ void ResourceNamespace::rebuild()
uint startTime;
VERBOSE( Con_Message("Rebuilding ResourceNamespace '%s'...\n", Str_Text(d->name)) )
VERBOSE2( startTime = Sys_GetRealTime() )
VERBOSE2( Con_PrintPathList(Str_Text(listSearchPaths(AutoStr_NewStd()))) )
#endif*/

// (Re)populate the directory and add found resources.
Expand Down Expand Up @@ -492,21 +482,6 @@ void ResourceNamespace::clearSearchPaths()
d->searchPaths.clear();
}

ddstring_t* ResourceNamespace::listSearchPaths(PathGroup group, ddstring_t* pathList,
char delimiter) const
{
if(pathList)
{
for(ResourceNamespace::SearchPaths::const_iterator i = d->searchPaths.find(group);
i != d->searchPaths.end() && i.key() == group; ++i)
{
AutoStr* path = i.value().uri().compose();
Str_Appendf(pathList, "%s%c", Str_Text(path), delimiter);
}
}
return pathList;
}

ResourceNamespace::SearchPaths const& ResourceNamespace::searchPaths() const
{
return d->searchPaths;
Expand Down Expand Up @@ -537,14 +512,15 @@ void ResourceNamespace::debugPrint() const
}
#endif

int ResourceNamespace::findAll(ddstring_t const* searchPath, ResourceList& found)
int ResourceNamespace::findAll(String searchPath, ResourceList& found)
{
int numFoundSoFar = found.count();

AutoStr* name = 0;
if(searchPath && !Str_IsEmpty(searchPath))
if(!searchPath.isEmpty())
{
name = composeResourceName(Str_Text(searchPath));
QByteArray searchPathUtf8 = searchPath.toUtf8();
name = composeResourceName(searchPathUtf8.constData());
}

NameHash::key_type from, to;
Expand Down
38 changes: 16 additions & 22 deletions doomsday/engine/portable/src/sys_reslocator.cpp
Expand Up @@ -150,14 +150,14 @@ static inline ResourceNamespaceInfo* getNamespaceInfoForId(resourcenamespaceid_t
return &namespaces[((uint)rni)-1];
}

static resourcenamespaceid_t findNamespaceForName(char const* name)
static resourcenamespaceid_t findNamespaceForName(String name)
{
if(name && name[0])
if(!name.isEmpty())
{
for(uint i = 0; i < numNamespaces; ++i)
{
ResourceNamespaceInfo* info = &namespaces[i];
if(!stricmp(Str_Text(info->rnamespace->name()), name))
if(!info->rnamespace->name().compareWithoutCase(name))
return (resourcenamespaceid_t)(i+1);
}
}
Expand Down Expand Up @@ -195,9 +195,9 @@ static void resetAllNamespaces(void)
* @return The found PathTree node which represents the resource else @c NULL.
*/
static PathTree::Node* findResourceInNamespace(ResourceNamespace& rnamespace,
ddstring_t const* searchPath, char delimiter)
String searchPath, QChar delimiter = '/')
{
if(!searchPath || Str_IsEmpty(searchPath)) return 0;
if(searchPath.isEmpty()) return 0;

// Ensure the namespace is up to date.
rnamespace.rebuild();
Expand All @@ -208,7 +208,8 @@ static PathTree::Node* findResourceInNamespace(ResourceNamespace& rnamespace,
if(rnamespace.findAll(searchPath, foundResources))
{
// There is at least one name-matched (perhaps partially) resource.
PathMap searchPattern = PathMap(PathTree::hashPathFragment, Str_Text(searchPath), delimiter);
QByteArray searchPathUtf8 = searchPath.toUtf8();
PathMap searchPattern = PathMap(PathTree::hashPathFragment, searchPathUtf8.constData(), delimiter.toLatin1());

DENG2_FOR_EACH_CONST(ResourceNamespace::ResourceList, i, foundResources)
{
Expand All @@ -235,7 +236,7 @@ static bool tryFindResource2(resourceclass_t rclass, ddstring_t const* rawSearch
if(rnInfo)
{
ResourceNamespace& rnamespace = *rnInfo->rnamespace;
if(PathTree::Node* found = findResourceInNamespace(rnamespace, searchPath, '/'))
if(PathTree::Node* found = findResourceInNamespace(rnamespace, Str_Text(searchPath), '/'))
{
// Does the caller want to know the matched path?
if(foundPath)
Expand Down Expand Up @@ -320,9 +321,8 @@ static bool findResource2(resourceclass_t rclass, ddstring_t const* searchPath,
{
DENG_ASSERT(inited && searchPath && !Str_IsEmpty(searchPath));

#if _DEBUG
VERBOSE2( Con_Message("Using rnamespace '%s'...\n", rnamespaceInfo? Str_Text(rnamespaceInfo->rnamespace->name()) : "None") )
#endif
LOG_AS("findResource2");
LOG_TRACE("Using namespace '%s'...") << (rnamespaceInfo? rnamespaceInfo->rnamespace->name() : "None");

bool found = false;

Expand Down Expand Up @@ -694,16 +694,14 @@ ResourceNamespace* F_CreateResourceNamespace(char const* name, byte flags)
DENG_ASSERT(name);
errorIfNotInited("F_CreateResourceNamespace");

if(strlen(name) < RESOURCENAMESPACE_MINNAMELENGTH)
if(qstrlen(name) < RESOURCENAMESPACE_MINNAMELENGTH)
Con_Error("F_CreateResourceNamespace: Invalid name '%s' (min length:%i)", name, (int)RESOURCENAMESPACE_MINNAMELENGTH);

ResourceNamespace* rn = new ResourceNamespace(name);
ResourceNamespace* rn = new ResourceNamespace(String(name));

// Add this new namespace to the global list.
namespaces = (ResourceNamespaceInfo*) M_Realloc(namespaces, sizeof *namespaces * ++numNamespaces);
if(!namespaces)
Con_Error("F_CreateResourceNamespace: Failed on (re)allocation of %lu bytes for new resource namespace\n",
(unsigned long) sizeof *namespaces * numNamespaces);
if(!namespaces) Con_Error("F_CreateResourceNamespace: Failed on (re)allocation of %lu bytes for new resource namespace\n", (unsigned long) sizeof *namespaces * numNamespaces);
ResourceNamespaceInfo* info = &namespaces[numNamespaces-1];

info->rnamespace = rn;
Expand All @@ -723,11 +721,6 @@ boolean F_AddExtraSearchPathToResourceNamespace(resourcenamespaceid_t rni, int f
return rnamespace.addSearchPath(ResourceNamespace::ExtraPaths, reinterpret_cast<de::Uri const&>(*searchPath), flags);
}

ddstring_t const* F_ResourceNamespaceName(resourcenamespaceid_t rni)
{
return (getNamespaceInfoForId(rni))->rnamespace->name();
}

uri_s** F_CreateUriList2(resourceclass_t rclass, char const* searchPaths, int* count)
{
if(!searchPaths || !searchPaths[0])
Expand Down Expand Up @@ -1035,11 +1028,12 @@ boolean F_MapGameResourcePath(resourcenamespaceid_t rni, ddstring_t* path)
ResourceNamespace& rnamespace = *info->rnamespace;
if(info->flags & RNF_USE_VMAP)
{
int const nameLen = Str_Length(rnamespace.name());
QByteArray rnamespaceName = rnamespace.name().toUtf8();
int const nameLen = rnamespaceName.length();
int const pathLen = Str_Length(path);

if(nameLen <= pathLen && Str_At(path, nameLen) == '/' &&
!strnicmp(Str_Text(rnamespace.name()), Str_Text(path), nameLen))
!qstrnicmp(rnamespaceName.constData(), Str_Text(path), nameLen))
{
Str_Prepend(path, "$(App.DataPath)/$(GamePlugin.Name)/");
return true;
Expand Down
9 changes: 7 additions & 2 deletions doomsday/engine/portable/src/uri.cpp
Expand Up @@ -26,6 +26,7 @@
#include "de_base.h"
#include "fs_util.h"
#include "game.h"
#include "resourcenamespace.h"
#include "sys_reslocator.h"

#include <de/Error>
Expand All @@ -34,6 +35,8 @@

#include "uri.h"

extern de::ResourceNamespace* F_ToResourceNamespace(resourcenamespaceid_t rni);

namespace de {

struct Uri::Instance
Expand Down Expand Up @@ -99,8 +102,10 @@ struct Uri::Instance

if(VALID_RESOURCE_CLASS(defaultResourceClass))
{
ddstring_t const* name = F_ResourceNamespaceName(F_DefaultResourceNamespaceForClass(defaultResourceClass));
Str_Copy(&scheme, name);
ResourceNamespace* rnamespace = F_ToResourceNamespace(F_DefaultResourceNamespaceForClass(defaultResourceClass));
DENG_ASSERT(rnamespace);
QByteArray rnamespaceName = rnamespace->name().toUtf8();
Str_Set(&scheme, rnamespaceName.constData());
}
}

Expand Down

0 comments on commit 32eb688

Please sign in to comment.