Skip to content

Commit

Permalink
Filesys|Fixed: Do not assume a resource is found until it is validated
Browse files Browse the repository at this point in the history
Previously it was possible to use a dummy file with the same name as
that of a Package resource required for startup, to coax the engine
into thinking the game was playable. Game resource files are now only
marked found once they pass validation.
  • Loading branch information
danij-deng committed Feb 10, 2012
1 parent 98b889e commit 22b1342
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 31 deletions.
1 change: 1 addition & 0 deletions doomsday/engine/api/dd_share.h
Expand Up @@ -385,6 +385,7 @@ typedef struct gameinfo_s {
*/
///@{
#define RF_STARTUP 0x1 ///< A required resource needed for and loaded during game start up (can't be a virtual file).
#define RF_FOUND 0x2 ///< Resource has been located.
///@}

//------------------------------------------------------------------------
Expand Down
9 changes: 9 additions & 0 deletions doomsday/engine/portable/include/abstractresource.h
Expand Up @@ -67,6 +67,15 @@ void AbstractResource_Print(AbstractResource* resource, boolean printStatus);
*/
ddstring_t* AbstractResource_NameStringList(AbstractResource* resource);

/**
* Update the "found" status for this resource.
*
* @param resource AbstractResource instance.
* @param yes @c true = mark as found, else mark not-found.
* @return Same as @a resource for caller convenience.
*/
AbstractResource* AbstractResource_MarkAsFound(AbstractResource* r, boolean yes);

/**
* Accessor methods.
*/
Expand Down
18 changes: 14 additions & 4 deletions doomsday/engine/portable/src/abstractresource.c
Expand Up @@ -96,7 +96,7 @@ AbstractResource* AbstractResource_NewWithName(resourceclass_t rclass, int flags
if(!r) Con_Error("AbstractResource::NewWithName: Failed on allocation of %lu bytes.", (unsigned long) sizeof(*r));

r->rclass = rclass;
r->flags = flags;
r->flags = flags & ~RF_FOUND;
r->names = 0;
r->namesCount = 0;
r->identityKeys = 0;
Expand Down Expand Up @@ -207,6 +207,7 @@ const ddstring_t* AbstractResource_ResolvedPath(AbstractResource* r, boolean can
{
r->searchPathUsed = F_FindResourceForRecord(r, &r->foundPath);
}
/// @todo Move resource validation here.
if(r->searchPathUsed != 0)
{
return &r->foundPath;
Expand All @@ -226,6 +227,14 @@ int AbstractResource_ResourceFlags(AbstractResource* r)
return r->flags;
}

AbstractResource* AbstractResource_MarkAsFound(AbstractResource* r, boolean yes)
{
assert(r);
if(yes) r->flags |= RF_FOUND;
else r->flags &= ~RF_FOUND;
return r;
}

ddstring_t* const* AbstractResource_IdentityKeys(AbstractResource* r)
{
assert(r);
Expand All @@ -235,16 +244,17 @@ ddstring_t* const* AbstractResource_IdentityKeys(AbstractResource* r)
void AbstractResource_Print(AbstractResource* r, boolean printStatus)
{
ddstring_t* searchPaths = AbstractResource_NameStringList(r);
const boolean markedFound = !!(r->flags & RF_FOUND);

if(printStatus)
Con_Printf("%s", r->searchPathUsed == 0? " ! ":" ");
Con_Printf("%s", !markedFound? " ! ":" ");

Con_PrintPathList4(Str_Text(searchPaths), ';', " or ", PPF_TRANSFORM_PATH_MAKEPRETTY);

if(printStatus)
{
Con_Printf(" %s%s", r->searchPathUsed == 0? "- missing" : "- found ",
r->searchPathUsed == 0? "" : F_PrettyPath(Str_Text(AbstractResource_ResolvedPath(r, false))));
Con_Printf(" %s%s", !markedFound? "- missing" : "- found ",
!markedFound? "" : F_PrettyPath(Str_Text(AbstractResource_ResolvedPath(r, false))));
}
Con_Printf("\n");
Str_Delete(searchPaths);
Expand Down
60 changes: 40 additions & 20 deletions doomsday/engine/portable/src/dd_main.c
Expand Up @@ -484,36 +484,50 @@ void DD_StartTitle(void)
static boolean recognizeWAD(const char* filePath, void* data)
{
lumpnum_t auxLumpBase = F_OpenAuxiliary3(filePath, 0, true);
boolean result;
boolean result = false;

if(auxLumpBase == -1) return false;

// Ensure all identity lumps are present.
result = true;
if(data)
if(auxLumpBase >= 0)
{
const ddstring_t* const* lumpNames = (const ddstring_t* const*) data;
for(; result && *lumpNames; lumpNames++)
// Ensure all identity lumps are present.
if(data)
{
lumpnum_t lumpNum = F_CheckLumpNumForName2(Str_Text(*lumpNames), true);
if(lumpNum == -1)
const ddstring_t* const* lumpNames = (const ddstring_t* const*) data;
result = true;
for(; result && *lumpNames; lumpNames++)
{
result = false;
lumpnum_t lumpNum = F_CheckLumpNumForName2(Str_Text(*lumpNames), true);
if(lumpNum < 0)
{
result = false;
}
}
}
}
else
{
// Matched.
result = true;
}

F_CloseAuxiliary();
F_CloseAuxiliary();
}
return result;
}

/// @return @c true, iff the resource appears to be what we think it is.
static boolean recognizeZIP(const char* filePath, void* data)
{
/// \todo dj: write me.
return F_FileExists(filePath);
DFile* dfile = F_Open(filePath, "bf");
boolean result = false;
if(dfile)
{
result = ZipFile_Recognise(dfile);
/// @todo Check files. We should implement an auxiliary zip lumpdirectory...
F_Close(dfile);
}
return result;
}

/// @todo This logic should be encapsulated by AbstractResource.
static int validateResource(AbstractResource* rec, void* paramaters)
{
int validated = false;
Expand All @@ -533,10 +547,15 @@ static int validateResource(AbstractResource* rec, void* paramaters)
validated = true;
}
break;
default: break;
default:
// Other resource types are not validated.
validated = true;
break;
}
}

AbstractResource_MarkAsFound(rec, validated);

return validated;
}

Expand Down Expand Up @@ -595,9 +614,9 @@ static boolean allGameResourcesFound(Game* game)
for(recordIt = records; *recordIt; recordIt++)
{
AbstractResource* rec = *recordIt;
const int flags = AbstractResource_ResourceFlags(rec);

if((AbstractResource_ResourceFlags(rec) & RF_STARTUP) &&
!AbstractResource_ResolvedPath(rec, false))
if((flags & RF_STARTUP) && !(flags & RF_FOUND))
return false;
}
}
Expand Down Expand Up @@ -769,6 +788,7 @@ static boolean exchangeEntryPoints(pluginid_t pluginId)
static void loadResource(AbstractResource* res)
{
if(!res) return;

switch(AbstractResource_ResourceClass(res))
{
case RC_PACKAGE: {
Expand All @@ -777,8 +797,8 @@ static void loadResource(AbstractResource* res)
{
F_AddFile(Str_Text(path), 0, false);
}
break;
}
break; }

default: Con_Error("loadGameResource: No resource loader found for %s.",
F_ResourceClassStr(AbstractResource_ResourceClass(res)));
}
Expand Down
9 changes: 5 additions & 4 deletions doomsday/engine/portable/src/def_main.c
Expand Up @@ -837,19 +837,20 @@ static void readAllDefinitions(void)
for(recordIt = records; *recordIt; recordIt++)
{
AbstractResource* rec = *recordIt;
const ddstring_t* resolvedPath = AbstractResource_ResolvedPath(rec, true);
const ddstring_t* path;

if(!resolvedPath)
if(!(AbstractResource_ResourceFlags(rec) & RF_FOUND))
{
ddstring_t* names = AbstractResource_NameStringList(rec);
Con_Error("readAllDefinitions: Error, failed to locate required game definition \"%s\".", Str_Text(names));
// Unreachable.
Str_Delete(names);
}

VERBOSE( Con_Message(" Processing '%s'...\n", F_PrettyPath(Str_Text(resolvedPath))) )
path = AbstractResource_ResolvedPath(rec, true);
VERBOSE( Con_Message(" Processing '%s'...\n", F_PrettyPath(Str_Text(path))) )

readDefinitionFile(Str_Text(resolvedPath));
readDefinitionFile(Str_Text(path));
}
}

Expand Down
6 changes: 3 additions & 3 deletions doomsday/engine/portable/src/fs_main.c
Expand Up @@ -754,7 +754,7 @@ lumpnum_t F_OpenAuxiliary3(const char* path, size_t baseOffset, boolean silent)
// We must have an absolute path, so prepend the current working directory if necessary.
F_PrependWorkPath(&searchPath, &searchPath);

/// \todo Allow opening WAD/ZIP files from lumps in other containers.
/// @todo Allow opening WAD/ZIP files from lumps in other containers.
file = findRealFile(Str_Text(&searchPath), "rb", &foundPath);
Str_Free(&searchPath);
if(!file)
Expand Down Expand Up @@ -807,8 +807,8 @@ lumpnum_t F_OpenAuxiliary3(const char* path, size_t baseOffset, boolean silent)
Con_Message("Warning:F_OpenAuxiliary: Cannot open a resource with neither a path nor a handle to it.\n");
}

if(foundPath)
Str_Delete(foundPath);
if(foundPath) Str_Delete(foundPath);
DFile_Delete(dfile, true);
return -1;
}

Expand Down

0 comments on commit 22b1342

Please sign in to comment.