Skip to content

Commit

Permalink
Refactor: Throw NotFoundErrors from relevant de::Textures methods
Browse files Browse the repository at this point in the history
Plus minor cleaned up.
  • Loading branch information
danij-deng committed Dec 7, 2012
1 parent f22126d commit c409d63
Show file tree
Hide file tree
Showing 8 changed files with 276 additions and 210 deletions.
50 changes: 47 additions & 3 deletions doomsday/engine/include/resource/textures.h
Expand Up @@ -60,6 +60,12 @@ class Textures
typedef class TextureManifest Manifest;
typedef class TextureScheme Scheme;

/**
* ResourceClass encapsulates the properties and logics belonging to a logical
* class of resource.
*
* @todo Derive from de::ResourceClass -ds
*/
struct ResourceClass
{
/**
Expand All @@ -82,10 +88,25 @@ class Textures
Texture::Flags flags, void *userData = 0);
};

/**
* Flags determining URI validation logic.
*
* @see validateUri()
*/
enum UriValidationFlag
{
AnyScheme = 0x1, ///< The scheme of the URI may be of zero-length; signifying "any scheme".
NotUrn = 0x2 ///< Do not accept a URN.
};
Q_DECLARE_FLAGS(UriValidationFlags, UriValidationFlag)

/// Texture system subspace schemes.
typedef QList<Scheme*> Schemes;

public:
/// The referenced texture was not found. @ingroup errors
DENG2_ERROR(NotFoundError);

/// An unknown scheme was referenced. @ingroup errors
DENG2_ERROR(UnknownSchemeError);

Expand Down Expand Up @@ -139,12 +160,33 @@ class Textures
}

/**
* Find a single declared texture.
* Validate @a uri to determine if it is well-formed and is usable as a
* search argument.
*
* @param uri Uri to be validated.
* @param flags Validation flags.
* @param quiet @c true= Do not output validation remarks to the log.
*
* @return @c true if @a Uri passes validation.
*
* @todo Should throw de::Error exceptions -ds
*/
bool validateUri(Uri const &uri, UriValidationFlags flags = 0,
bool quiet = false) const;

/**
* Determines if a manifest exists for a declared texture on @a path.
* @return @c true, if a manifest exists; otherwise @a false.
*/
bool has(Uri const &path) const;

/**
* Find the manifest for a declared texture.
*
* @param search The search term.
* @return Found unique identifier; otherwise @c NOTEXTUREID.
* @return Found unique identifier.
*/
Manifest *find(Uri const &search) const;
Manifest &find(Uri const &search) const;

/**
* Declare a texture in the collection, producing a manifest for a logical
Expand Down Expand Up @@ -218,6 +260,8 @@ class Textures
Instance *d;
};

Q_DECLARE_OPERATORS_FOR_FLAGS(Textures::UriValidationFlags)

} // namespace de

de::Textures* App_Textures();
Expand Down
12 changes: 5 additions & 7 deletions doomsday/engine/src/dd_main.cpp
Expand Up @@ -2603,21 +2603,19 @@ materialid_t DD_MaterialForTextureUri(uri_s const *_textureUri)
{
if(!_textureUri) return NOMATERIALID;

de::Uri const &textureUri = reinterpret_cast<de::Uri const &>(*_textureUri);
try
{
if(TextureManifest *manifest = App_Textures()->find(textureUri))
{
de::Uri uri = manifest->composeUri();
uri.setScheme(Str_Text(DD_MaterialSchemeNameForTextureScheme(uri.scheme())));
return Materials_ResolveUri2(reinterpret_cast<uri_s*>(&uri), true/*quiet please*/);
}
de::Uri uri = App_Textures()->find(reinterpret_cast<de::Uri const &>(*_textureUri)).composeUri();
uri.setScheme(Str_Text(DD_MaterialSchemeNameForTextureScheme(uri.scheme())));
return Materials_ResolveUri2(reinterpret_cast<uri_s*>(&uri), true/*quiet please*/);
}
catch(Textures::UnknownSchemeError const &er)
{
// Log but otherwise ignore this error.
LOG_WARNING(er.asText() + ", ignoring.");
}
catch(Textures::NotFoundError const &)
{} // Ignore this error.

return NOMATERIALID;
}
Expand Down
47 changes: 30 additions & 17 deletions doomsday/engine/src/resource/animgroups.cpp
Expand Up @@ -104,29 +104,42 @@ void R_AddAnimGroupFrame(int groupNum, uri_s const *textureUri, int tics, int ra
return;
}

TextureManifest *manifest = App_Textures()->find(reinterpret_cast<de::Uri const &>(*textureUri));
if(!manifest)
try
{
LOG_DEBUG("Invalid texture uri \"%s\", ignoring.") << reinterpret_cast<de::Uri const &>(*textureUri);
return;
}
TextureManifest &manifest = App_Textures()->find(reinterpret_cast<de::Uri const &>(*textureUri));

// Allocate a new animframe.
group->frames = (animframe_t *) Z_Realloc(group->frames, sizeof(*group->frames) * ++group->count, PU_APPSTATIC);
if(!group->frames) Con_Error("R_AddAnimGroupFrame: Failed on (re)allocation of %lu bytes enlarging AnimFrame list for group #%i.", (unsigned long) sizeof(*group->frames) * group->count, groupNum);
// Allocate a new animframe.
group->frames = (animframe_t *) Z_Realloc(group->frames, sizeof(*group->frames) * ++group->count, PU_APPSTATIC);
if(!group->frames) Con_Error("R_AddAnimGroupFrame: Failed on (re)allocation of %lu bytes enlarging AnimFrame list for group #%i.", (unsigned long) sizeof(*group->frames) * group->count, groupNum);

animframe_t *frame = &group->frames[group->count - 1];
frame->textureManifest = manifest;
frame->tics = tics;
frame->randomTics = randomTics;
animframe_t *frame = &group->frames[group->count - 1];
frame->textureManifest = &manifest;
frame->tics = tics;
frame->randomTics = randomTics;
}
catch(Textures::NotFoundError const &er)
{
// Log but otherwise ignore this error.
LOG_WARNING(er.asText() + ". Failed adding texture \"%s\" to group #%i, ignoring.")
<< reinterpret_cast<de::Uri const &>(*textureUri) << groupNum;
}
}

boolean R_IsTextureInAnimGroup(uri_s const *texture, int groupNum)
boolean R_IsTextureInAnimGroup(uri_s const *textureUri, int groupNum)
{
if(!texture) return false;
if(!textureUri) return false;
animgroup_t *group = getAnimGroup(groupNum);
if(!group) return false;
TextureManifest *manifest = App_Textures()->find(reinterpret_cast<de::Uri const &>(*texture));
if(!manifest) return false;
return isInAnimGroup(*group, *manifest);

try
{
TextureManifest &manifest = App_Textures()->find(reinterpret_cast<de::Uri const &>(*textureUri));
return isInAnimGroup(*group, manifest);
}
catch(Textures::NotFoundError const &er)
{
// Log but otherwise ignore this error.
LOG_WARNING(er.asText() + ", ignoring.");
}
return false;
}
7 changes: 5 additions & 2 deletions doomsday/engine/src/resource/material.cpp
Expand Up @@ -108,13 +108,16 @@ void Material_SetDefinition(material_t* mat, struct ded_material_s* def)
if(def->layers[0].stageCount.num > 0 && def->layers[0].stages[0].texture)
{
de::Uri *texUri = reinterpret_cast<de::Uri *>(def->layers[0].stages[0].texture);
if(de::TextureManifest *manifest = App_Textures()->find(*texUri))
try
{
if(de::Texture *tex = manifest->texture())
de::TextureManifest &manifest = App_Textures()->find(*texUri);
if(de::Texture *tex = manifest.texture())
{
mat->_isCustom = tex->isCustom();
}
}
catch(de::Textures::NotFoundError const &)
{} // Ignore this error.
}
}

Expand Down
21 changes: 10 additions & 11 deletions doomsday/engine/src/resource/materials.cpp
Expand Up @@ -949,8 +949,6 @@ material_t* Materials_CreateFromDef(ded_material_t* def)
return bind->material();
}

de::Textures &textures = *App_Textures();

// Ensure the primary layer has a valid texture reference.
de::Texture *tex = 0;
if(def->layers[0].stageCount.num > 0)
Expand All @@ -959,16 +957,17 @@ material_t* Materials_CreateFromDef(ded_material_t* def)
de::Uri *texUri = reinterpret_cast<de::Uri *>(layer.stages[0].texture);
if(texUri) // Not unused.
{
if(de::TextureManifest *manifest = textures.find(*texUri))
try
{
tex = App_Textures()->find(*texUri).texture();
}
catch(de::Textures::NotFoundError const &er)
{
tex = manifest->texture();
if(!tex)
{
LOG_WARNING("Unknown texture \"%s\" in Material \"%s\" (layer %i stage %i).")
<< reinterpret_cast<de::Uri*>(layer.stages[0].texture)
<< reinterpret_cast<de::Uri*>(def->uri)
<< 0 << 0;
}
// Log but otherwise ignore this error.
LOG_WARNING(er.asText() + ". Unknown texture \"%s\" in Material \"%s\" (layer %i stage %i), ignoring.")
<< reinterpret_cast<de::Uri*>(layer.stages[0].texture)
<< reinterpret_cast<de::Uri*>(def->uri)
<< 0 << 0;
}
}
}
Expand Down
6 changes: 4 additions & 2 deletions doomsday/engine/src/resource/materialvariant.cpp
Expand Up @@ -49,10 +49,12 @@ MaterialVariant::MaterialVariant(material_t &generalCase,
de::Uri *texUri = reinterpret_cast<de::Uri *>(def.layers[i].stages[0].texture);
if(texUri)
{
if(TextureManifest *manifest = App_Textures()->find(*texUri))
try
{
layers[i].texture = reinterpret_cast<texture_s *>(manifest->texture());
layers[i].texture = reinterpret_cast<texture_s *>(App_Textures()->find(*texUri).texture());
}
catch(Textures::NotFoundError const &)
{} // Ignore this error.
}

layers[i].texOrigin[0] = def.layers[i].stages[0].texOrigin[0];
Expand Down
10 changes: 7 additions & 3 deletions doomsday/engine/src/resource/r_data.cpp
Expand Up @@ -128,11 +128,14 @@ patchid_t R_DeclarePatch(char const *name)
de::Uri uri("Patches", Path(QString(QByteArray(name, qstrlen(name)).toPercentEncoding())));

// Already defined as a patch?
if(TextureManifest *manifest = textures.find(uri))
try
{
TextureManifest &manifest = textures.find(uri);
/// @todo We should instead define Materials from patches and return the material id.
return patchid_t( manifest->uniqueId() );
return patchid_t( manifest.uniqueId() );
}
catch(Textures::NotFoundError const &)
{} // Ignore this error.

Path lumpPath = uri.path() + ".lmp";
lumpnum_t lumpNum = App_FileSystem()->nameIndex().lastIndexForPath(lumpPath);
Expand Down Expand Up @@ -717,7 +720,8 @@ void R_InitFlatTextures()
!percentEncodedName.compareWithoutCase("FF_END")) continue;

de::Uri uri = composeFlatUri(percentEncodedName);
if(!textures.find(uri)) // A new flat?

if(!textures.has(uri)) // A new flat?
{
/**
* Kludge Assume 64x64 else when the flat is loaded it will inherit the
Expand Down

0 comments on commit c409d63

Please sign in to comment.