Skip to content

Commit

Permalink
Refactor: Refactored away resourcenamespaceid_t plus minor cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
danij-deng committed Nov 14, 2012
1 parent 7bc1abc commit df14da3
Show file tree
Hide file tree
Showing 11 changed files with 115 additions and 203 deletions.
16 changes: 13 additions & 3 deletions doomsday/engine/include/resourcenamespace.h
Expand Up @@ -45,6 +45,9 @@ namespace de
* Resource Namespace.
*
* @ingroup resource
*
* @todo The symbolic name of the namespace and the path mapping template
* (@see applyPathMappings) should be defined externally. -ds
*/
class ResourceNamespace
{
Expand Down Expand Up @@ -140,11 +143,20 @@ namespace de
void rebuild();

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

/**
* Reset this namespace, returning it to an empty state and clearing any
* @ref ExtraPaths which have been registered since its construction.
*/
inline void reset() {
clearSearchPaths(ResourceNamespace::ExtraPaths);
clear();
}

/**
* Manually add a resource to this namespace. Duplicates are pruned
* automatically.
Expand Down Expand Up @@ -208,8 +220,6 @@ namespace de
* @param path The path to be mapped (applied in-place).
*
* @return @c true iff mapping was applied to the path.
*
* @todo The mapping template should be defined externally.
*/
bool applyPathMappings(String& path) const;

Expand Down
50 changes: 18 additions & 32 deletions doomsday/engine/include/sys_reslocator.h
Expand Up @@ -33,6 +33,9 @@
#include "uri.h"

#ifdef __cplusplus
#include <QList>

#include "resourcenamespace.h"
#include <de/String>

extern "C" {
Expand Down Expand Up @@ -68,15 +71,6 @@ typedef enum {
#define NUM_RESOURCE_TYPES (RT_LAST_INDEX-1)
#define VALID_RESOURCE_TYPE(v) ((v) >= RT_FIRST && (v) < RT_LAST_INDEX)

/**
* Unique identifier associated with resource namespaces managed by the
* resource locator.
*
* @ingroup core
* @see ResourceNamespace
*/
typedef uint resourcenamespaceid_t;

/**
* @defgroup resourceLocationFlags Resource Location Flags
*
Expand Down Expand Up @@ -107,24 +101,8 @@ void F_ShutdownResourceLocator(void);

void F_ResetAllResourceNamespaces(void);

void F_ResetResourceNamespace(resourcenamespaceid_t rni);

void F_CreateNamespacesForFileResourcePaths(void);

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

/// @return @c true iff @a value can be interpreted as a valid resource namespace id.
boolean F_IsValidResourceNamespaceId(int value);

/**
* @param rni Unique identifier of the namespace to add to.
* @param flags @see searchPathFlags
* @param searchPath Uri representing the search path to be added.
*/
boolean F_AddExtraSearchPathToResourceNamespace(resourcenamespaceid_t rni, int flags,
struct uri_s const* searchPath);

/**
* Attempt to locate a named resource.
*
Expand Down Expand Up @@ -165,19 +143,27 @@ uint F_FindResourceFromList(resourceclass_t rclass, char const* searchPaths,
*/
resourceclass_t F_DefaultResourceClassForType(resourcetype_t type);

#ifdef __cplusplus
} // extern "C"

namespace de {
typedef QList<ResourceNamespace*> ResourceNamespaces;
}

de::ResourceNamespaces const& F_ResourceNamespaces();

/**
* @return Unique identifier of the default namespace associated with @a rclass.
*/
resourcenamespaceid_t F_DefaultResourceNamespaceForClass(resourceclass_t rclass);

#ifdef __cplusplus
} // extern "C"
de::ResourceNamespace* F_DefaultResourceNamespaceForClass(resourceclass_t rclass);

/**
* @return Unique identifier of the resource namespace associated with @a name,
* else @c 0 (not found).
* Lookup a ResourceNamespace by symbolic name.
*
* @param name Symbolic name of the namespace.
* @return ResourceNamespace associated with @a name; otherwise @c 0 (not found).
*/
resourcenamespaceid_t F_SafeResourceNamespaceForName(de::String name);
de::ResourceNamespace* F_ResourceNamespaceByName(de::String name);

/**
* Attempts to determine which "type" should be attributed to a resource, solely
Expand Down
16 changes: 7 additions & 9 deletions doomsday/engine/src/dd_main.cpp
Expand Up @@ -29,7 +29,6 @@
# include <objbase.h>
#endif

#include <QDir>
#include <QStringList>

#include <de/NativePath>
Expand Down Expand Up @@ -513,8 +512,8 @@ static void initPathMappings()

if(i < argC - 1 && !CommandLine_IsOption(i + 1) && !CommandLine_IsOption(i + 2))
{
String source = QDir::fromNativeSeparators(NativePath(CommandLine_PathAt(i + 1)).expand());
String destination = QDir::fromNativeSeparators(NativePath(CommandLine_PathAt(i + 2)).expand());
String source = NativePath(CommandLine_PathAt(i + 1)).expand().withSeparators('/');
String destination = NativePath(CommandLine_PathAt(i + 2)).expand().withSeparators('/');
App_FileSystem()->mapPath(source, destination);
i += 2;
}
Expand Down Expand Up @@ -590,7 +589,7 @@ static bool parsePathLumpMappings(char const* buffer)
}
else
{
String destination = QDir::fromNativeSeparators(NativePath(Str_Text(&path)).expand());
String destination = NativePath(Str_Text(&path)).expand().withSeparators('/');
App_FileSystem()->mapPathToLump(lumpName, destination);
}
} while(*ch);
Expand Down Expand Up @@ -1282,8 +1281,6 @@ 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 @@ -1369,7 +1366,8 @@ boolean DD_Init(void)
DD_DummyWorker, 0, "Buffering...");

// Add resource paths specified using -iwad on the command line.
ResourceNamespace* rnamespace = F_ToResourceNamespace(F_DefaultResourceNamespaceForClass(RC_PACKAGE));
ResourceNamespace* rnamespace = F_DefaultResourceNamespaceForClass(RC_PACKAGE);
DENG_ASSERT(rnamespace);
for(int p = 0; p < CommandLine_Count(); ++p)
{
if(!CommandLine_IsMatchingAlias("-iwad", CommandLine_At(p)))
Expand Down Expand Up @@ -2297,7 +2295,7 @@ D_CMD(Load)
AutoStr* foundPath = AutoStr_NewStd();
for(; arg < argc; ++arg)
{
de::Uri searchPath = de::Uri(QDir::fromNativeSeparators(NativePath(argv[arg]).expand()), RC_PACKAGE);
de::Uri searchPath = de::Uri(NativePath(argv[arg]).expand().withSeparators('/'), RC_PACKAGE);
if(!F_FindResource3(RC_PACKAGE, reinterpret_cast<uri_s*>(&searchPath), foundPath, RLF_MATCH_EXTENSION)) continue;

if(tryLoadFile(Str_Text(foundPath)))
Expand Down Expand Up @@ -2428,7 +2426,7 @@ D_CMD(Unload)
AutoStr* foundPath = AutoStr_NewStd();
for(i = 1; i < argc; ++i)
{
de::Uri searchPath = de::Uri(QDir::fromNativeSeparators(NativePath(argv[1]).expand()), RC_PACKAGE);
de::Uri searchPath = de::Uri(NativePath(argv[1]).expand().withSeparators('/'), RC_PACKAGE);
if(!F_FindResource2(RC_PACKAGE, reinterpret_cast<uri_s*>(&searchPath), foundPath)) continue;

if(tryUnloadFile(Str_Text(foundPath)))
Expand Down
4 changes: 3 additions & 1 deletion doomsday/engine/src/def_main.cpp
Expand Up @@ -1077,7 +1077,9 @@ void Def_Read(void)
{
// We've already initialized the definitions once.
// Get rid of everything.
F_ResetResourceNamespace(F_DefaultResourceNamespaceForClass(RC_MODEL));
de::ResourceNamespace* rnamespace = F_DefaultResourceNamespaceForClass(RC_MODEL);
DENG_ASSERT(rnamespace);
rnamespace->reset();

Materials_ClearDefinitionLinks();
Fonts_ClearDefinitionLinks();
Expand Down
34 changes: 17 additions & 17 deletions doomsday/engine/src/def_read.cpp
Expand Up @@ -54,8 +54,11 @@
// XGClass.h is actually a part of the engine.
#include "../../../plugins/common/include/xgclass.h"

#include <de/NativePath>
#include <de/memory.h>

using namespace de;

#define MAX_RECUR_DEPTH 30
#define MAX_TOKEN_LEN 128

Expand Down Expand Up @@ -383,7 +386,7 @@ static int ReadAnyString(char** dest)
return true;
}

static int ReadUri(Uri** dest, const char* defaultScheme)
static int ReadUri(uri_s** dest, const char* defaultScheme)
{
char* buf = 0;
int result;
Expand Down Expand Up @@ -806,18 +809,15 @@ static int DED_ReadData(ded_t* ded, const char* buffer, const char* _sourceFile)

if(ISTOKEN("ModelPath"))
{
Uri* newSearchPath;
AutoStr* path;

READSTR(label);
CHECKSC;

path = AutoStr_FromTextStd(label);
F_AppendMissingSlash(path);
newSearchPath = Uri_NewWithPath2(Str_Text(path), RC_NULL);
F_AddExtraSearchPathToResourceNamespace(F_DefaultResourceNamespaceForClass(RC_MODEL),
0, newSearchPath);
Uri_Delete(newSearchPath);
String modelPath = NativePath(label).withSeparators('/') / "/"; // Ensure it has a terminating separator.
de::Uri newSearchPath = de::Uri(modelPath, RC_NULL);

ResourceNamespace* rnamespace = F_DefaultResourceNamespaceForClass(RC_MODEL);
DENG_ASSERT(rnamespace);
rnamespace->addSearchPath(ResourceNamespace::ExtraPaths, reinterpret_cast<de::Uri const&>(newSearchPath), 0);
}

if(ISTOKEN("Header"))
Expand Down Expand Up @@ -1156,7 +1156,7 @@ static int DED_ReadData(ded_t* ded, const char* buffer, const char* _sourceFile)
}
else if(!bCopyNext)
{
Uri* otherMat = NULL;
uri_s* otherMat = NULL;
AutoStr* otherMatPath;

READURI(&otherMat, NULL);
Expand Down Expand Up @@ -1195,7 +1195,7 @@ static int DED_ReadData(ded_t* ded, const char* buffer, const char* _sourceFile)
if(prevMaterialDefIdx >= 0 && bCopyNext)
{
const ded_material_t* prevMaterial = ded->materials + prevMaterialDefIdx;
Uri* uri = mat->uri;
uri_s* uri = mat->uri;

memcpy(mat, prevMaterial, sizeof(*mat));
mat->uri = uri;
Expand Down Expand Up @@ -1579,7 +1579,7 @@ static int DED_ReadData(ded_t* ded, const char* buffer, const char* _sourceFile)
if(prevMapInfoDefIdx >= 0 && bCopyNext)
{
const ded_mapinfo_t* prevMapInfo = ded->mapInfo + prevMapInfoDefIdx;
Uri* uri = mi->uri;
uri_s* uri = mi->uri;
int i;

memcpy(mi, prevMapInfo, sizeof(*mi));
Expand Down Expand Up @@ -1728,15 +1728,15 @@ static int DED_ReadData(ded_t* ded, const char* buffer, const char* _sourceFile)
FINDBEGIN;
for(;;)
{
Uri** mn;
uri_s** mn;

READLABEL;
RV_STR("ID", tenv->id)
if(ISLABEL("Material") || ISLABEL("Texture") || ISLABEL("Flat"))
{ // A new material path.
ddstring_t mnamespace; Str_Init(&mnamespace);
Str_Set(&mnamespace, ISLABEL("Material")? "" : ISLABEL("Texture")? MN_TEXTURES_NAME : MN_FLATS_NAME);
mn = (Uri**) DED_NewEntry((void**)&tenv->materials, &tenv->count, sizeof(*mn));
mn = (uri_s**) DED_NewEntry((void**)&tenv->materials, &tenv->count, sizeof(*mn));
FINDBEGIN;
for(;;)
{
Expand Down Expand Up @@ -2633,10 +2633,10 @@ static int DED_ReadData(ded_t* ded, const char* buffer, const char* _sourceFile)

int DED_Read(ded_t* ded, const char* path)
{
Str transPath;
ddstring_t transPath;
size_t bufferedDefSize;
char* bufferedDef;
FileHandle* file;
filehandle_s* file;
int result;

// Compose the (possibly-translated) path.
Expand Down
2 changes: 1 addition & 1 deletion doomsday/engine/src/fileid.cpp
Expand Up @@ -92,7 +92,7 @@ FileId::Md5Hash FileId::hash(String path)
// Ensure we've a normalized path.
if(QDir::isRelativePath(path))
{
String basePath = QDir::fromNativeSeparators(DENG2_APP->nativeBasePath());
String basePath = DENG2_APP->nativeBasePath().withSeparators('/');
path = basePath / path;
}

Expand Down

0 comments on commit df14da3

Please sign in to comment.