Skip to content

Commit

Permalink
Refactor|ResourceNamespace: Continued improving de::ResourceNamespace
Browse files Browse the repository at this point in the history
Revised SearchPath implementation, relocated various logics from
sys_reslocator.cpp into ResourceNamespace and refactored away the
now unnecessary callback mechanisms.

ResourceNamespace now has ownership of the FileDirectory it uses.
  • Loading branch information
danij-deng committed Oct 30, 2012
1 parent 6572e5b commit 06d8280
Show file tree
Hide file tree
Showing 6 changed files with 350 additions and 318 deletions.
102 changes: 65 additions & 37 deletions doomsday/engine/portable/include/resourcenamespace.h
Expand Up @@ -28,9 +28,6 @@
#include "pathtree.h"
#include "uri.h"

// Number of entries in the hash table.
#define RESOURCENAMESPACE_HASHSIZE 512

/**
* @defgroup searchPathFlags Search Path Flags
* @{
Expand Down Expand Up @@ -73,73 +70,104 @@ namespace de
FallbackPaths
};

typedef unsigned short NameHashKey;

typedef NameHashKey (HashNameFunc) (ddstring_t const* name);

struct SearchPath
{
int flags; /// @see searchPathFlags
Uri* uri;
public:
/**
* @param _flags @ref searchPathFlags
* @param _uri Unresolved search URI (may include symbolic names or
* other symbol references). SearchPath takes ownership.
*/
SearchPath(int _flags, Uri* _uri);

/**
* Construct a copy from @a other. This is a "deep copy".
*/
SearchPath(SearchPath const& other);

~SearchPath();

SearchPath& operator = (SearchPath other);

friend void swap(SearchPath& first, SearchPath& second); // nothrow

/// @return @ref searchPathFlags
int flags() const;

SearchPath& setFlags(int flags);

SearchPath(int _flags, Uri const* _uri)
: flags(_flags), uri(Uri_NewCopy(_uri))
{}
/// @return Unresolved URI.
Uri const* uri() const;

~SearchPath()
{
Uri_Delete(uri);
}
private:
/// @see searchPathFlags
int flags_;

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

typedef QMultiMap<PathGroup, SearchPath> SearchPaths;
typedef QList<PathTree::Node*> ResourceList;

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

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

/**
* Rebuild this namespace by re-scanning for resources on all search
* paths and re-populating the internal database.
*
* @note Any manually added resources will not be present after this.
*/
void rebuild();

/**
* Reset the namespace back to it's "empty" state (i.e., no known symbols).
* Reset this namespace back to it's "empty" state (i.e., no resources).
* The search path groups are unaffected.
*/
void clear();

/**
* Add a new named resource into the namespace. Multiple names for a given
* resource may coexist however duplicates are automatically pruned.
* Manually add a resource to this namespace. Duplicates are pruned
* automatically.
*
* @post Name hash may have been rebuilt.
*
* @param name Name of the resource being added.
* @param node PathTree node which represents the resource.
*
* @return @c true iff the namespace did not already contain this resource.
* @return @c true iff this namespace did not already contain the resource.
*/
bool add(ddstring_t const* name, PathTree::Node& node);
bool add(PathTree::Node& node);

/**
* Finds all resources in this namespace..
* Finds all resources in this namespace.
*
* @param name If not @c NULL, only consider resources with this name.
* @param searchpath If not @c NULL, only consider resources whose
* name matches that which will be extracted from
* this path.
* @param found Set of resources that match the result.
*
* @return Number of found resources.
*/
int findAll(ddstring_t const* name, ResourceList& found);
int findAll(ddstring_t const* searchPath, ResourceList& found);

/**
* Add a new path to this namespace.
* Add a new search path to this namespace. Newer paths have priority
* over previously added paths.
*
* @param group Group to add this path to. @see SearchPathGroup
* @param group Group to add this path to. @see PathGroup
* @param path New unresolved path to add.
* @param flags @see searchPathFlags
*
* @return @c true if the path is correctly formed and present in the namespace.
* @return @c true if @a path was well-formed and subsequently added.
*/
bool addSearchPath(PathGroup group, Uri const* path, int flags);

/**
* Clear search paths in @a group in this namespace.
* Clear search paths in @a group from this namespace.
*
* @param group Search path group to be cleared.
*/
void clearSearchPaths(PathGroup group);
Expand All @@ -150,8 +178,8 @@ namespace de
void clearSearchPaths();

/**
* Append to @a pathList all resolved search paths in @a group, in descending,
* (i.e., most recent order), using @a delimiter to separate each.
* 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.
Expand All @@ -163,9 +191,9 @@ namespace de
char delimiter = ';') const;

/**
* Append to @a pathList all resolved search paths from all groups, firstly
* in group order (from Override to Fallback) and in descending, (i.e., most
* recent order), using @a delimiter to separate each.
* 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.
Expand Down
13 changes: 3 additions & 10 deletions doomsday/engine/portable/include/sys_reslocator.h
Expand Up @@ -125,12 +125,6 @@ void F_ResetResourceNamespace(resourcenamespaceid_t rni);

void F_CreateNamespacesForFileResourcePaths(void);

/**
* @return Newly created hash name. Ownership passes to the caller who should
* ensure to release it with Str_Delete when done.
*/
AutoStr* F_ComposeHashNameForFilePath(Str const* filePath);

ddstring_t const* F_ResourceNamespaceName(resourcenamespaceid_t rni);

/// @return Number of resource namespaces.
Expand All @@ -144,16 +138,15 @@ boolean F_IsValidResourceNamespaceId(int value);
* Given an id return the associated resource namespace object.
*/
ResourceNamespace* F_ToResourceNamespace(resourcenamespaceid_t rni);
#endif

/**
* @param rni Unique identifier of the namespace to add to.
* @param flags @see searchPathFlags
* @param searchPath Uri representing the search path to be added.
* @param group Group to add the new search path to.
*/
boolean F_AddSearchPathToResourceNamespace(resourcenamespaceid_t rni, int flags,
Uri const* searchPath, de::ResourceNamespace::SearchPathGroup group);
#endif
boolean F_AddExtraSearchPathToResourceNamespace(resourcenamespaceid_t rni, int flags,
Uri const* searchPath);

/**
* Attempt to locate a known resource.
Expand Down
8 changes: 5 additions & 3 deletions doomsday/engine/portable/src/dd_main.cpp
Expand Up @@ -1331,6 +1331,8 @@ void DD_FinishInitializationAfterWindowReady(void)
Window_SetDrawFunc(Window_Main(), DD_GameLoopDrawer);
}

extern ResourceNamespace* F_ToResourceNamespace(resourcenamespaceid_t rni);

/**
* Engine initialization. After completed, the game loop is ready to be started.
* Called from the app entrypoint function.
Expand Down Expand Up @@ -1424,12 +1426,12 @@ boolean DD_Init(void)

while(++p != CommandLine_Count() && !CommandLine_IsOption(p))
{
const char* filePath = CommandLine_PathAt(p);
char const* filePath = CommandLine_PathAt(p);
directory_t* dir;
Uri* searchPath;

/// @todo Do not add these as search paths, publish them directly to
/// the FileDirectory owned by the "packages" ResourceNamespace.
/// @todo Do not add these as search paths, publish them directly
/// to the "packages" ResourceNamespace.
dir = Dir_ConstructFromPathDir(filePath);
searchPath = Uri_NewWithPath2(Dir_Path(dir), RC_PACKAGE);

Expand Down
4 changes: 2 additions & 2 deletions doomsday/engine/portable/src/def_read.c
Expand Up @@ -845,8 +845,8 @@ static int DED_ReadData(ded_t* ded, const char* buffer, const char* _sourceFile)
CHECKSC;

newSearchPath = Uri_NewWithPath2(label, RC_NULL);
F_AddSearchPathToResourceNamespace(F_DefaultResourceNamespaceForClass(RC_MODEL),
0, newSearchPath, SPG_EXTRA);
F_AddExtraSearchPathToResourceNamespace(F_DefaultResourceNamespaceForClass(RC_MODEL),
0, newSearchPath);
Uri_Delete(newSearchPath);
}

Expand Down

0 comments on commit 06d8280

Please sign in to comment.