Skip to content

Commit

Permalink
Refactor|Client|Server: ResourceSystem has ownership of ResourceClasses
Browse files Browse the repository at this point in the history
  • Loading branch information
danij-deng committed Nov 9, 2013
1 parent c5d40b7 commit 0667e79
Show file tree
Hide file tree
Showing 16 changed files with 95 additions and 106 deletions.
24 changes: 0 additions & 24 deletions doomsday/client/include/dd_main.h
Expand Up @@ -131,14 +131,8 @@ boolean DD_ExchangeGamePluginEntryPoints(pluginid_t pluginId);
*/
void* DD_FindEntryPoint(pluginid_t pluginId, const char* fn);

void DD_CreateResourceClasses();

void DD_ClearResourceClasses();

namespace de
{
typedef QList<ResourceClass*> ResourceClasses;

/// Map of symbolic file type names to file types.
typedef QMap<String, FileType*> FileTypes;
}
Expand All @@ -162,24 +156,6 @@ de::FileType& DD_GuessFileTypeFromFileName(de::String name);
/// Returns the registered file types for efficient traversal.
de::FileTypes const& DD_FileTypes();

/**
* Lookup a ResourceClass by id.
*
* @todo Refactor away.
*
* @param classId Unique identifier of the class.
* @return ResourceClass associated with @a id.
*/
de::ResourceClass& DD_ResourceClassById(resourceclassid_t classId);

/**
* Lookup a ResourceClass by symbolic name.
*
* @param name Symbolic name of the class.
* @return ResourceClass associated with @a name; otherwise @c 0 (not found).
*/
de::ResourceClass& DD_ResourceClassByName(de::String name);

/// @return Symbolic name of the material scheme associated with @a textureSchemeName.
de::String DD_MaterialSchemeNameForTextureScheme(de::String textureSchemeName);

Expand Down
21 changes: 20 additions & 1 deletion doomsday/client/include/resource/resourcesystem.h
Expand Up @@ -20,20 +20,39 @@
#define DENG_RESOURCESYSTEM_H

#include <de/System>
#include <de/Error>
#include "resourceclass.h"
#include "Textures"

/**
* Logical resources; materials, packages, textures, etc... @ingroup resource
*/
class ResourceSystem : public de::System
{
public:
/// An unknown resource class identifier was specified. @ingroup errors
DENG2_ERROR(UnknownResourceClass);

public:
ResourceSystem();

/**
* Lookup a ResourceClass by symbolic @a name.
*/
de::ResourceClass &resClass(de::String name);

/**
* Lookup a ResourceClass by @a id.
* @todo Refactor away.
*/
de::ResourceClass &resClass(resourceclassid_t id);

/**
* Provides access to the Textures collection.
*/
de::Textures &textures();

void clearRuntimeTextureSchemes();

void clearSystemTextureSchemes();

// System.
Expand Down
5 changes: 3 additions & 2 deletions doomsday/client/include/resourceclass.h
Expand Up @@ -27,7 +27,7 @@
#include "api_resourceclass.h"

#ifdef __cplusplus
#ifndef DENG2_C_API_ONLY
//#ifndef DENG2_C_API_ONLY

#include <QList>
#include <de/String>
Expand Down Expand Up @@ -128,7 +128,8 @@ namespace de
}

} // namespace de
#endif // DENG2_C_API_ONLY

//#endif // DENG2_C_API_ONLY
#endif // __cplusplus

#endif /* LIBDENG_RESOURCECLASS_H */
2 changes: 1 addition & 1 deletion doomsday/client/src/audio/s_cache.cpp
Expand Up @@ -556,7 +556,7 @@ static sfxsample_t *cacheSample(int id, sfxinfo_t const *info)
try
{
String foundPath = App_FileSystem().findPath(de::Uri(info->lumpName, RC_SOUND),
RLF_DEFAULT, DD_ResourceClassById(RC_SOUND));
RLF_DEFAULT, App_ResourceSystem().resClass(RC_SOUND));
foundPath = App_BasePath() / foundPath; // Ensure the path is absolute.

data = WAV_Load(foundPath.toUtf8().constData(), &bytesPer, &rate, &numSamples);
Expand Down
2 changes: 1 addition & 1 deletion doomsday/client/src/audio/s_mus.cpp
Expand Up @@ -254,7 +254,7 @@ int Mus_GetExt(ded_music_t *def, ddstring_t *retPath)
try
{
String foundPath = App_FileSystem().findPath(de::Uri(def->lumpName, RC_MUSIC), RLF_DEFAULT,
DD_ResourceClassById(RC_MUSIC));
App_ResourceSystem().resClass(RC_MUSIC));
foundPath = App_BasePath() / foundPath; // Ensure the path is absolute.

// Does the caller want to know the matched path?
Expand Down
72 changes: 12 additions & 60 deletions doomsday/client/src/dd_main.cpp
Expand Up @@ -165,8 +165,6 @@ finaleid_t titleFinale;
int gameDataFormat; // Use a game-specifc data format where applicable.

static NullFileType nullFileType;
static NullResourceClass nullResourceClass;
static ResourceClasses resourceClasses;

/// A symbolic name => file type map.
static FileTypes fileTypeMap;
Expand Down Expand Up @@ -227,59 +225,14 @@ void App_DeleteMaterials()
materials = 0;
}

void DD_CreateResourceClasses()
{
resourceClasses.push_back(new ResourceClass("RC_PACKAGE", "Packages"));
resourceClasses.push_back(new ResourceClass("RC_DEFINITION", "Defs"));
resourceClasses.push_back(new ResourceClass("RC_GRAPHIC", "Graphics"));
resourceClasses.push_back(new ResourceClass("RC_MODEL", "Models"));
resourceClasses.push_back(new ResourceClass("RC_SOUND", "Sfx"));
resourceClasses.push_back(new ResourceClass("RC_MUSIC", "Music"));
resourceClasses.push_back(new ResourceClass("RC_FONT", "Fonts"));
}

void DD_ClearResourceClasses()
{
DENG2_FOR_EACH(ResourceClasses, i, resourceClasses)
{
delete *i;
}
resourceClasses.clear();
}

ResourceClass& DD_ResourceClassByName(String name)
{
if(!name.isEmpty())
{
DENG2_FOR_EACH_CONST(ResourceClasses, i, resourceClasses)
{
ResourceClass& rclass = **i;
if(!rclass.name().compareWithoutCase(name))
return rclass;
}
}
return nullResourceClass; // Not found.
}

ResourceClass& DD_ResourceClassById(resourceclassid_t id)
{
if(id == RC_NULL) return nullResourceClass;
if(!VALID_RESOURCECLASSID(id))
{
QByteArray msg = String("DD_ResourceClassById: Invalid id '%1'").arg(int(id)).toUtf8();
App_FatalError(msg.constData());
}
return *resourceClasses[uint(id)];
}

void DD_CreateFileTypes()
{
FileType* ftype;

/*
* Packages types:
*/
ResourceClass& packageClass = DD_ResourceClassByName("RC_PACKAGE");
ResourceClass& packageClass = App_ResourceSystem().resClass("RC_PACKAGE");

ftype = new ZipFileType();
ftype->addKnownExtension(".pk3");
Expand All @@ -301,13 +254,13 @@ void DD_CreateFileTypes()
*/
ftype = new FileType("FT_DED", RC_DEFINITION);
ftype->addKnownExtension(".ded");
DD_ResourceClassByName("RC_DEFINITION").addFileType(*ftype);
App_ResourceSystem().resClass("RC_DEFINITION").addFileType(*ftype);
fileTypeMap.insert(ftype->name().toLower(), ftype);

/*
* Graphic fileTypes:
*/
ResourceClass& graphicClass = DD_ResourceClassByName("RC_GRAPHIC");
ResourceClass& graphicClass = App_ResourceSystem().resClass("RC_GRAPHIC");

ftype = new FileType("FT_PNG", RC_GRAPHIC);
ftype->addKnownExtension(".png");
Expand All @@ -332,7 +285,7 @@ void DD_CreateFileTypes()
/*
* Model fileTypes:
*/
ResourceClass& modelClass = DD_ResourceClassByName("RC_MODEL");
ResourceClass& modelClass = App_ResourceSystem().resClass("RC_MODEL");

ftype = new FileType("FT_DMD", RC_MODEL);
ftype->addKnownExtension(".dmd");
Expand All @@ -349,13 +302,13 @@ void DD_CreateFileTypes()
*/
ftype = new FileType("FT_WAV", RC_SOUND);
ftype->addKnownExtension(".wav");
DD_ResourceClassByName("RC_SOUND").addFileType(*ftype);
App_ResourceSystem().resClass("RC_SOUND").addFileType(*ftype);
fileTypeMap.insert(ftype->name().toLower(), ftype);

/*
* Music fileTypes:
*/
ResourceClass& musicClass = DD_ResourceClassByName("RC_MUSIC");
ResourceClass& musicClass = App_ResourceSystem().resClass("RC_MUSIC");

ftype = new FileType("FT_OGG", RC_MUSIC);
ftype->addKnownExtension(".ogg");
Expand All @@ -382,7 +335,7 @@ void DD_CreateFileTypes()
*/
ftype = new FileType("FT_DFN", RC_FONT);
ftype->addKnownExtension(".dfn");
DD_ResourceClassByName("RC_FONT").addFileType(*ftype);
App_ResourceSystem().resClass("RC_FONT").addFileType(*ftype);
fileTypeMap.insert(ftype->name().toLower(), ftype);

/*
Expand Down Expand Up @@ -569,7 +522,7 @@ ResourceSystem &App_ResourceSystem()
return ServerApp::resourceSystem();
}
#endif
throw Error("App_Textures", "App not yet initialized");
throw Error("App_ResourceSystem", "App not yet initialized");
}

de::Textures &App_Textures()
Expand Down Expand Up @@ -1855,7 +1808,6 @@ boolean DD_Init(void)

// Initialize the subsystems needed prior to entering busy mode for the first time.
Sys_Init();
DD_CreateResourceClasses();
DD_CreateFileTypes();
F_Init();
DD_CreateFileSystemSchemes();
Expand Down Expand Up @@ -1884,7 +1836,7 @@ boolean DD_Init(void)
DD_DummyWorker, 0, "Buffering...");

// Add resource paths specified using -iwad on the command line.
FS1::Scheme& scheme = App_FileSystem().scheme(DD_ResourceClassByName("RC_PACKAGE").defaultScheme());
FS1::Scheme& scheme = App_FileSystem().scheme(App_ResourceSystem().resClass("RC_PACKAGE").defaultScheme());
for(int p = 0; p < CommandLine_Count(); ++p)
{
if(!CommandLine_IsMatchingAlias("-iwad", CommandLine_At(p)))
Expand Down Expand Up @@ -2142,7 +2094,7 @@ static int DD_StartupWorker(void* /*parm*/)
* Add required engine resource files.
*/
String foundPath = App_FileSystem().findPath(de::Uri("doomsday.pk3", RC_PACKAGE),
RLF_DEFAULT, DD_ResourceClassById(RC_PACKAGE));
RLF_DEFAULT, App_ResourceSystem().resClass(RC_PACKAGE));
foundPath = App_BasePath() / foundPath; // Ensure the path is absolute.
de::File1 *loadedFile = tryLoadFile(de::Uri(foundPath, RC_NULL));
DENG2_ASSERT(loadedFile != 0);
Expand Down Expand Up @@ -2759,7 +2711,7 @@ D_CMD(Load)
try
{
String foundPath = App_FileSystem().findPath(de::Uri::fromNativePath(argv[arg], RC_PACKAGE),
RLF_MATCH_EXTENSION, DD_ResourceClassById(RC_PACKAGE));
RLF_MATCH_EXTENSION, App_ResourceSystem().resClass(RC_PACKAGE));
foundPath = App_BasePath() / foundPath; // Ensure the path is absolute.

if(tryLoadFile(de::Uri(foundPath, RC_NULL)))
Expand Down Expand Up @@ -2886,7 +2838,7 @@ D_CMD(Unload)
try
{
String foundPath = App_FileSystem().findPath(de::Uri::fromNativePath(argv[1], RC_PACKAGE),
RLF_MATCH_EXTENSION, DD_ResourceClassById(RC_PACKAGE));
RLF_MATCH_EXTENSION, App_ResourceSystem().resClass(RC_PACKAGE));
foundPath = App_BasePath() / foundPath; // Ensure the path is absolute.

if(tryUnloadFile(de::Uri(foundPath, RC_NULL)))
Expand Down
1 change: 0 additions & 1 deletion doomsday/client/src/dd_pinit.cpp
Expand Up @@ -168,6 +168,5 @@ void DD_ShutdownAll(void)

Def_Destroy();
F_Shutdown();
DD_ClearResourceClasses();
Libdeng_Shutdown();
}
4 changes: 2 additions & 2 deletions doomsday/client/src/def_main.cpp
Expand Up @@ -840,7 +840,7 @@ static void readAllDefinitions()
* Start with engine's own top-level definition file.
*/
String foundPath = App_FileSystem().findPath(de::Uri("doomsday.ded", RC_DEFINITION),
RLF_DEFAULT, DD_ResourceClassById(RC_DEFINITION));
RLF_DEFAULT, App_ResourceSystem().resClass(RC_DEFINITION));
foundPath = App_BasePath() / foundPath; // Ensure the path is absolute.

readDefinitionFile(foundPath);
Expand Down Expand Up @@ -1429,7 +1429,7 @@ void Def_Read()
{
// We've already initialized the definitions once.
// Get rid of everything.
FS1::Scheme &scheme = App_FileSystem().scheme(DD_ResourceClassByName("RC_MODEL").defaultScheme());
FS1::Scheme &scheme = App_FileSystem().scheme(App_ResourceSystem().resClass("RC_MODEL").defaultScheme());
scheme.reset();

invalidateAllMaterials();
Expand Down
2 changes: 1 addition & 1 deletion doomsday/client/src/def_read.cpp
Expand Up @@ -813,7 +813,7 @@ static int DED_ReadData(ded_t* ded, const char* buffer, const char* _sourceFile)
CHECKSC;

de::Uri newSearchPath = de::Uri::fromNativeDirPath(NativePath(label));
FS1::Scheme& scheme = App_FileSystem().scheme(DD_ResourceClassByName("RC_MODEL").defaultScheme());
FS1::Scheme& scheme = App_FileSystem().scheme(App_ResourceSystem().resClass("RC_MODEL").defaultScheme());
scheme.addSearchPath(reinterpret_cast<de::Uri const&>(newSearchPath), FS1::ExtraPaths);
}

Expand Down
2 changes: 1 addition & 1 deletion doomsday/client/src/filesys/fs_main.cpp
Expand Up @@ -618,7 +618,7 @@ String FS1::findPath(de::Uri const& search, int flags, ResourceClass& rclass)

String FS1::findPath(de::Uri const& search, int flags)
{
return findPath(search, flags, DD_ResourceClassById(RC_NULL));
return findPath(search, flags, App_ResourceSystem().resClass(RC_NULL));
}

#if _DEBUG
Expand Down
2 changes: 1 addition & 1 deletion doomsday/client/src/filesys/manifest.cpp
Expand Up @@ -272,7 +272,7 @@ ResourceManifest &ResourceManifest::locateFile()
try
{
String foundPath = App_FileSystem().findPath(de::Uri(*i, d->classId),
RLF_DEFAULT, DD_ResourceClassById(d->classId));
RLF_DEFAULT, App_ResourceSystem().resClass(d->classId));
foundPath = App_BasePath() / foundPath; // Ensure the path is absolute.

// Perform identity validation.
Expand Down
8 changes: 4 additions & 4 deletions doomsday/client/src/gl/gl_texmanager.cpp
Expand Up @@ -1760,7 +1760,7 @@ static TexSource loadExternalTexture(image_t &image, String encodedSearchPath,
try
{
String foundPath = App_FileSystem().findPath(de::Uri(encodedSearchPath + optionalSuffix, RC_GRAPHIC),
RLF_DEFAULT, DD_ResourceClassById(RC_GRAPHIC));
RLF_DEFAULT, App_ResourceSystem().resClass(RC_GRAPHIC));
// Ensure the found path is absolute.
foundPath = App_BasePath() / foundPath;

Expand All @@ -1775,7 +1775,7 @@ static TexSource loadExternalTexture(image_t &image, String encodedSearchPath,
try
{
String foundPath = App_FileSystem().findPath(de::Uri(encodedSearchPath, RC_GRAPHIC),
RLF_DEFAULT, DD_ResourceClassById(RC_GRAPHIC));
RLF_DEFAULT, App_ResourceSystem().resClass(RC_GRAPHIC));
// Ensure the found path is absolute.
foundPath = App_BasePath() / foundPath;

Expand Down Expand Up @@ -1904,7 +1904,7 @@ TexSource GL_LoadExtImage(image_t &image, char const *_searchPath, gfxmode_t mod
try
{
String foundPath = App_FileSystem().findPath(de::Uri(RC_GRAPHIC, _searchPath),
RLF_DEFAULT, DD_ResourceClassById(RC_GRAPHIC));
RLF_DEFAULT, App_ResourceSystem().resClass(RC_GRAPHIC));
// Ensure the found path is absolute.
foundPath = App_BasePath() / foundPath;

Expand Down Expand Up @@ -2182,7 +2182,7 @@ static TexSource loadRaw(image_t &image, rawtex_t const &raw)
try
{
String foundPath = App_FileSystem().findPath(de::Uri("Patches", Path(Str_Text(&raw.name))),
RLF_DEFAULT, DD_ResourceClassById(RC_GRAPHIC));
RLF_DEFAULT, App_ResourceSystem().resClass(RC_GRAPHIC));
// Ensure the found path is absolute.
foundPath = App_BasePath() / foundPath;

Expand Down
4 changes: 2 additions & 2 deletions doomsday/client/src/render/rend_particle.cpp
Expand Up @@ -111,7 +111,7 @@ static Path tryFindImage(String name)
try
{
String foundPath = App_FileSystem().findPath(de::Uri("Textures", name + "-ck"),
RLF_DEFAULT, DD_ResourceClassById(RC_GRAPHIC));
RLF_DEFAULT, App_ResourceSystem().resClass(RC_GRAPHIC));
// Ensure the path is absolute.
return App_BasePath() / foundPath;
}
Expand All @@ -124,7 +124,7 @@ static Path tryFindImage(String name)
try
{
String foundPath = App_FileSystem().findPath(de::Uri("Textures", name),
RLF_DEFAULT, DD_ResourceClassById(RC_GRAPHIC));
RLF_DEFAULT, App_ResourceSystem().resClass(RC_GRAPHIC));
// Ensure the path is absolute.
return App_BasePath() / foundPath;
}
Expand Down

0 comments on commit 0667e79

Please sign in to comment.