Skip to content

Commit

Permalink
Refactor: Manage ResourceClass and ResourceType dynamically
Browse files Browse the repository at this point in the history
The old resourceclass_t and resourcetype_t enumerations have been
replaced with dynamically managed structures which encapsulate their
properties and logics.

Additionally there are null-object versions of each.

Todo: Cleanup
  • Loading branch information
danij-deng committed Nov 16, 2012
1 parent 8793fed commit 08547f2
Show file tree
Hide file tree
Showing 22 changed files with 651 additions and 353 deletions.
6 changes: 0 additions & 6 deletions doomsday/engine/api/dd_share.h
Expand Up @@ -66,12 +66,6 @@ extern "C" {
/// Maximum number of players supported by the engine.
#define DDMAXPLAYERS 16

/// Base default paths for data files.
#define DD_BASEPATH_DATA "}data/"

/// Base default paths for definition files.
#define DD_BASEPATH_DEFS "}defs/"

// The case-independent strcmps have different names.
#if WIN32
# define strcasecmp _stricmp
Expand Down
23 changes: 2 additions & 21 deletions doomsday/engine/api/dd_types.h
Expand Up @@ -69,27 +69,6 @@ typedef char filename_t[FILENAME_T_MAXLEN];

typedef void (*con_textfilter_t) (char* text);

/**
* Resource Class.
*
* @ingroup fs
*/
typedef enum {
RC_NULL = -2, ///< Not a real class, used internally during resource locator init.
RC_UNKNOWN = -1, ///< Attempt to guess the class using heuristic evaluation of the path.
RESOURCECLASS_FIRST = 0,
RC_PACKAGE = RESOURCECLASS_FIRST,
RC_DEFINITION,
RC_GRAPHIC,
RC_MODEL,
RC_SOUND,
RC_MUSIC,
RC_FONT,
RESOURCECLASS_COUNT
} resourceclass_t;

#define VALID_RESOURCE_CLASS(n) ((n) >= RESOURCECLASS_FIRST && (n) < RESOURCECLASS_COUNT)

#define BAMS_BITS 16

#if BAMS_BITS == 32
Expand Down Expand Up @@ -121,6 +100,8 @@ struct surface_s;
struct material_s;

#include <de/str.h>
#include "resourceclass.h"
#include "resourcetype.h"
#include "uri.h"

#endif /* ENGINE_TYPES_H */
4 changes: 2 additions & 2 deletions doomsday/engine/api/doomsday.h
Expand Up @@ -160,7 +160,7 @@ gameid_t DD_GameIdForKey(char const* identityKey);
* recent.
*
* @param game Unique identifier/name of the game.
* @param rclass Class of resource being added.
* @param classId Class of resource being added.
* @param rflags Resource flags (see @ref resourceFlags).
* @param names One or more known potential names, seperated by semicolon
* (e.g., <pre> "name1;name2" </pre>). Valid names include
Expand All @@ -171,7 +171,7 @@ gameid_t DD_GameIdForKey(char const* identityKey);
* For package resources this may be C-String containing a
* semicolon delimited list of identity keys.
*/
void DD_AddGameResource(gameid_t game, resourceclass_t rclass, int rflags,
void DD_AddGameResource(gameid_t game, resourceclassid_t classId, int rflags,
const char* names, void* params);

/**
Expand Down
126 changes: 126 additions & 0 deletions doomsday/engine/api/resourceclass.h
@@ -0,0 +1,126 @@
/**
* @file resourceclass.h
*
* Resource Class.
*
* @ingroup base
*
* @author Copyright &copy; 2003-2012 Jaakko Keränen <jaakko.keranen@iki.fi>
* @author Copyright &copy; 2006-2012 Daniel Swanson <danij@dengine.net>
*
* @par License
* GPL: http://www.gnu.org/licenses/gpl.html
*
* <small>This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2 of the License, or (at your
* option) any later version. This program is distributed in the hope that it
* will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
* Public License for more details. You should have received a copy of the GNU
* General Public License along with this program; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA</small>
*/

#ifndef LIBDENG_RESOURCECLASS_H
#define LIBDENG_RESOURCECLASS_H

#ifdef __cplusplus
extern "C" {
#endif

/**
* Resource Class Identifier.
*
* @ingroup base
*/
typedef enum resourceclassid_e {
RC_NULL = -2, ///< Not a real class, used internally during resource locator init.
RC_UNKNOWN = -1, ///< Attempt to guess the class using heuristic evaluation of the path.
RESOURCECLASS_FIRST = 0,
RC_PACKAGE = RESOURCECLASS_FIRST,
RC_DEFINITION,
RC_GRAPHIC,
RC_MODEL,
RC_SOUND,
RC_MUSIC,
RC_FONT,
RESOURCECLASS_COUNT
} resourceclassid_t;

#define VALID_RESOURCE_CLASSID(n) ((n) >= RESOURCECLASS_FIRST && (n) < RESOURCECLASS_COUNT)

#ifdef __cplusplus
} // extern "C"
#endif

#ifdef __cplusplus
#ifndef DENG2_C_API_ONLY

#include <QList>
#include <de/String>

enum resourcetypeid_e;

namespace de
{
/**
* ResourceClass.
*
* @ingroup base
*/
struct ResourceClass
{
public:
typedef QList<resourcetypeid_e> Types;

public:
String name_;

String defaultNamespace_;

/// Recognized resource types (in order of importance, left to right).
Types searchTypeOrder;

ResourceClass(String _name, String _defaultNamespace)
: name_(_name), defaultNamespace_(_defaultNamespace)
{}

virtual ~ResourceClass() {};

String const& name() const {
return name_;
}

String const& defaultNamespace() const {
return defaultNamespace_;
}

ResourceClass& addResourceType(resourcetypeid_e rtype) {
searchTypeOrder.push_back(rtype);
return *this;
}
};

/**
* The special "null" ResourceClass object.
*
* @ingroup core
*/
struct NullResourceClass : public ResourceClass
{
NullResourceClass() : ResourceClass("RC_NULL", "")
{}
};

/// @return @c true= @a rtype is a "null-resourceclass" object (not a real resource class).
inline bool isNullResourceClass(ResourceClass const& rclass) {
return !!dynamic_cast<NullResourceClass const*>(&rclass);
}

} // namespace de
#endif // DENG2_C_API_ONLY
#endif // __cplusplus

#endif /* LIBDENG_RESOURCECLASS_H */
144 changes: 144 additions & 0 deletions doomsday/engine/api/resourcetype.h
@@ -0,0 +1,144 @@
/**
* @file resourcetype.h
*
* Resource Type.
*
* @ingroup core
*
* @author Copyright &copy; 2003-2012 Jaakko Keränen <jaakko.keranen@iki.fi>
* @author Copyright &copy; 2006-2012 Daniel Swanson <danij@dengine.net>
*
* @par License
* GPL: http://www.gnu.org/licenses/gpl.html
*
* <small>This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2 of the License, or (at your
* option) any later version. This program is distributed in the hope that it
* will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
* Public License for more details. You should have received a copy of the GNU
* General Public License along with this program; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA</small>
*/

#ifndef LIBDENG_RESOURCETYPE_H
#define LIBDENG_RESOURCETYPE_H

#ifdef __cplusplus
extern "C" {
#endif

/**
* Resource Type identifer attributable to resources (e.g., files).
*
* @ingroup core
*/
typedef enum resourcetypeid_e {
RT_NONE = 0,
RT_FIRST = 1,
RT_ZIP = RT_FIRST,
RT_WAD,
RT_LMP,
RT_DED,
RT_PNG,
RT_JPG,
RT_TGA,
RT_PCX,
RT_DMD,
RT_MD2,
RT_WAV,
RT_OGG,
RT_MP3,
RT_MOD,
RT_MID,
RT_DEH,
RT_DFN,
RT_LAST_INDEX
} resourcetypeid_t;

#define RESOURCETYPE_COUNT (RT_LAST_INDEX - 1)
#define VALID_RESOURCE_TYPEID(v) ((v) >= RT_FIRST && (v) < RT_LAST_INDEX)

#ifdef __cplusplus
} // extern "C"
#endif

#ifdef __cplusplus
#ifndef DENG2_C_API_ONLY

#include <QStringList>
#include <de/String>

enum resourceclassid_e;

namespace de
{
/**
* ResourceType.
*
* @ingroup core
*/
struct ResourceType
{
String name_;

/// Default class attributed to resources of this type.
resourceclassid_e defaultClass_;

/// List of known extensions for this resource type.
QStringList knownFileNameExtensions;

ResourceType(String _name, resourceclassid_e _defaultClass)
: name_(_name), defaultClass_(_defaultClass)
{}

virtual ~ResourceType() {};

String const& name() const {
return name_;
}

resourceclassid_e defaultClass() const {
return defaultClass_;
}

ResourceType& addKnownExtension(String ext) {
knownFileNameExtensions.push_back(ext);
return *this;
}

bool fileNameIsKnown(String path) const
{
// We require an extension for this.
String ext = path.fileNameExtension();
if(!ext.isEmpty())
{
return knownFileNameExtensions.contains(ext, Qt::CaseInsensitive);
}
return false;
}
};

/**
* The special "null" ResourceType object.
*
* @ingroup core
*/
struct NullResourceType : public ResourceType
{
NullResourceType() : ResourceType("RT_NONE", RC_UNKNOWN)
{}
};

/// @return @c true= @a rtype is a "null-resourcetype" object (not a real resource type).
inline bool isNullResourceType(ResourceType const& rtype) {
return !!dynamic_cast<NullResourceType const*>(&rtype);
}

} // namespace de
#endif // DENG2_C_API_ONLY
#endif // __cplusplus

#endif /* LIBDENG_RESOURCETYPE_H */
8 changes: 4 additions & 4 deletions doomsday/engine/api/uri.h
Expand Up @@ -151,7 +151,7 @@ namespace de
* is not @c RC_NULL, ask the resource locator whether it knows of an
* appropriate default scheme for this class of resource.
*/
Uri(String path, resourceclass_t defaultResourceClass = RC_UNKNOWN, QChar delimiter = '/');
Uri(String path, resourceclassid_t defaultResourceClass = RC_UNKNOWN, QChar delimiter = '/');

/**
* Construct a Uri instance by duplicating @a other.
Expand Down Expand Up @@ -249,7 +249,7 @@ namespace de
* this is not @c RC_NULL, ask the resource locator whether it knows
* of an appropriate default scheme for this class of resource.
*/
Uri& setUri(String newUri, resourceclass_t defaultResourceClass = RC_UNKNOWN,
Uri& setUri(String newUri, resourceclassid_t defaultResourceClass = RC_UNKNOWN,
QChar delimiter = '/');

/**
Expand Down Expand Up @@ -386,7 +386,7 @@ Uri* Uri_New(void);
* @param defaultResourceClass If no scheme is defined in @a path and this is not @c RC_NULL,
* look for an appropriate default scheme for this class of resource.
*/
Uri* Uri_NewWithPath2(char const* path, resourceclass_t defaultResourceClass);
Uri* Uri_NewWithPath2(char const* path, resourceclassid_t defaultResourceClass);
Uri* Uri_NewWithPath(char const* path);

/**
Expand Down Expand Up @@ -476,7 +476,7 @@ Uri* Uri_SetPath(Uri* uri, char const* path);
*
* @return Same as @a uri, for caller convenience.
*/
Uri* Uri_SetUri2(Uri* uri, char const* path, resourceclass_t defaultResourceClass);
Uri* Uri_SetUri2(Uri* uri, char const* path, resourceclassid_t defaultResourceClass);
Uri* Uri_SetUri(Uri* uri, char const* path/* defaultResourceClass = RC_UNKNOWN*/);

Uri* Uri_SetUriStr(Uri* uri, ddstring_t const* path);
Expand Down
2 changes: 2 additions & 0 deletions doomsday/engine/engine.pro
Expand Up @@ -110,6 +110,8 @@ DENG_API_HEADERS = \
api/doomsday.h \
api/filehandle.h \
api/materialarchive.h \
api/resourceclass.h \
api/resourcetype.h \
api/sys_audiod.h \
api/sys_audiod_mus.h \
api/sys_audiod_sfx.h \
Expand Down
2 changes: 1 addition & 1 deletion doomsday/engine/include/dd_main.h
Expand Up @@ -161,7 +161,7 @@ void DD_DestroyGames(void);

boolean DD_GameInfo(struct gameinfo_s* info);

void DD_AddGameResource(gameid_t game, resourceclass_t rclass, int rflags, char const* names, void* params);
void DD_AddGameResource(gameid_t game, resourceclassid_t classId, int rflags, char const* names, void* params);

gameid_t DD_DefineGame(struct gamedef_s const* def);

Expand Down

0 comments on commit 08547f2

Please sign in to comment.