Skip to content

Commit

Permalink
Refactor|Cleanup: Cleaning up the old DED sources
Browse files Browse the repository at this point in the history
  • Loading branch information
skyjake committed May 5, 2014
1 parent c9b0462 commit ef5c12b
Show file tree
Hide file tree
Showing 7 changed files with 143 additions and 124 deletions.
31 changes: 4 additions & 27 deletions doomsday/client/src/def_main.cpp
Expand Up @@ -110,6 +110,7 @@ int Def_GetGameClasses(void)
xgClassLinks = &nullXgClassLinks;
}

// Let the parser know of the XG classes.
DED_SetXGClassLinks(xgClassLinks);

return 1;
Expand Down Expand Up @@ -279,10 +280,7 @@ ded_value_t* Def_GetValueById(char const* id)
ded_value_t* Def_GetValueByUri(struct uri_s const *_uri)
{
if(!_uri) return 0;
de::Uri const& uri = reinterpret_cast<de::Uri const&>(*_uri);

if(uri.scheme().compareWithoutCase("Values")) return 0;
return Def_GetValueById(uri.pathCStr());
return defs.getValueByUri(*reinterpret_cast<de::Uri const *>(_uri));
}

ded_mapinfo_t* Def_GetMapInfo(struct uri_s const *_uri)
Expand Down Expand Up @@ -417,20 +415,9 @@ ded_ptcgen_t* Def_GetDamageGenerator(int mobjType)
return 0;
}

/**
* Attempts to retrieve a flag by its prefix and value.
* Returns a ptr to the text string of the first flag it
* finds that matches the criteria, else NULL.
*/
const char* Def_GetFlagTextByPrefixVal(const char* prefix, int val)
{
int i;
for(i = defs.count.flags.num - 1; i >= 0; i--)
{
if(strnicmp(defs.flags[i].id, prefix, strlen(prefix)) == 0 && defs.flags[i].value == val)
return defs.flags[i].text;
}
return NULL;
return defs.getFlagTextByPrefixVal(prefix, val);
}

#undef Def_EvalFlags
Expand All @@ -441,17 +428,7 @@ int Def_EvalFlags(char *ptr)

int Def_GetTextNumForName(const char* name)
{
int idx = -1;
if(name && name[0] && defs.count.text.num)
{
int i = 0;
do
{
if(!stricmp(defs.text[i].id, name))
idx = i;
} while(idx == -1 && ++i < defs.count.text.num);
}
return idx;
return defs.getTextNumForName(name);
}

/**
Expand Down
7 changes: 6 additions & 1 deletion doomsday/libdoomsday/include/doomsday/defs/ded.h
Expand Up @@ -677,7 +677,6 @@ struct LIBDOOMSDAY_PUBLIC ded_s {
ded_material_t* materials;

// Models.
//ded_model_t* models;
typedef std::vector<ded_model_t> Models;
Models models;

Expand Down Expand Up @@ -763,8 +762,12 @@ struct LIBDOOMSDAY_PUBLIC ded_s {

ded_flag_t *getFlag(char const *flag) const;

const char* getFlagTextByPrefixVal(const char* prefix, int val) const;

int evalFlags2(char const *ptr) const;

int getTextNumForName(const char* name) const;

ded_material_t *findMaterialDef(de::Uri const &uri) const;

ded_material_t *getMaterial(char const *uriCString) const;
Expand Down Expand Up @@ -794,6 +797,8 @@ struct LIBDOOMSDAY_PUBLIC ded_s {

ded_value_t* getValueById(char const* id) const;

ded_value_t* getValueByUri(de::Uri const &uri) const;

ded_mapinfo_t *getMapInfo(de::Uri const *uri) const;

ded_sky_t* getSky(char const* id) const;
Expand Down
10 changes: 10 additions & 0 deletions doomsday/libdoomsday/include/doomsday/defs/dedfile.h
Expand Up @@ -26,4 +26,14 @@

LIBDOOMSDAY_PUBLIC void Def_ReadProcessDED(ded_t *defs, char const* path);

/**
* Reads definitions from the given lump.
*/
LIBDOOMSDAY_PUBLIC int DED_ReadLump(ded_t* ded, lumpnum_t lumpNum);

/**
* @return @c true, if the file was successfully loaded.
*/
int DED_Read(ded_t* ded, const char* path);

#endif // LIBDOOMSDAY_DEFS_DED_H
9 changes: 4 additions & 5 deletions doomsday/libdoomsday/include/doomsday/defs/dedparser.h
Expand Up @@ -24,15 +24,14 @@
#include "../libdoomsday.h"
#include "ded.h"

/**
* @return @c true, if the file was successfully loaded.
*/
LIBDOOMSDAY_PUBLIC int DED_Read(ded_t* ded, const char* path);

LIBDOOMSDAY_PUBLIC int DED_ReadLump(ded_t* ded, lumpnum_t lumpNum);

LIBDOOMSDAY_PUBLIC void DED_SetXGClassLinks(struct xgclass_s *links);

int DED_ReadData(ded_t* ded, const char* buffer, const char* _sourceFile);

void DED_Include(ded_t *ded, const char* fileName, const char* parentDirectory);

void DED_SetError(char const *str);

#endif // LIBDOOMSDAY_DED_V1_PARSER_H
31 changes: 31 additions & 0 deletions doomsday/libdoomsday/src/defs/ded.cpp
Expand Up @@ -992,6 +992,16 @@ ded_flag_t *ded_s::getFlag(char const *flag) const
return 0;
}

const char* ded_s::getFlagTextByPrefixVal(const char* prefix, int val) const
{
for(int i = count.flags.num - 1; i >= 0; i--)
{
if(!qstrnicmp(flags[i].id, prefix, strlen(prefix)) && flags[i].value == val)
return flags[i].text;
}
return 0;
}

int ded_s::evalFlags2(char const *ptr) const
{
LOG_AS("Def_EvalFlags");
Expand Down Expand Up @@ -1095,6 +1105,12 @@ ded_value_t* ded_s::getValueById(char const* id) const
return 0;
}

ded_value_t* ded_s::getValueByUri(de::Uri const &uri) const
{
if(uri.scheme().compareWithoutCase("Values")) return 0;
return getValueById(uri.pathCStr());
}

ded_mapinfo_t *ded_s::getMapInfo(de::Uri const *uri) const
{
if(!uri) return 0;
Expand Down Expand Up @@ -1158,3 +1174,18 @@ ded_compositefont_t* ded_s::getCompositeFont(char const* uriCString) const
}
return def;
}

int ded_s::getTextNumForName(const char* name) const
{
int idx = -1;
if(name && name[0] && count.text.num)
{
int i = 0;
do
{
if(!qstricmp(text[i].id, name))
idx = i;
} while(idx == -1 && ++i < count.text.num);
}
return idx;
}
64 changes: 64 additions & 0 deletions doomsday/libdoomsday/src/defs/dedfile.cpp
Expand Up @@ -54,3 +54,67 @@ void Def_ReadProcessDED(ded_t *defs, char const* path)
App_FatalError("Def_ReadProcessDED: %s\n", dedReadError);
}
}

int DED_ReadLump(ded_t* ded, lumpnum_t lumpNum)
{
int lumpIdx;
struct file1_s* file = F_FindFileForLumpNum2(lumpNum, &lumpIdx);
if(file)
{
if(F_LumpLength(lumpNum) != 0)
{
uint8_t const* lumpPtr = F_CacheLump(file, lumpIdx);
DED_ReadData(ded, (char const*)lumpPtr, Str_Text(F_ComposePath(file)));
F_UnlockLump(file, lumpIdx);
}
return true;
}
DED_SetError("Bad lump number.");
return false;
}

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

// Compose the (possibly-translated) path.
Str_InitStd(&transPath);
Str_Set(&transPath, path);
F_FixSlashes(&transPath, &transPath);
F_ExpandBasePath(&transPath, &transPath);

// Attempt to open a definition file on this path.
file = F_Open(Str_Text(&transPath), "rb");
if(!file)
{
DED_SetError("File could not be opened for reading.");
Str_Free(&transPath);
return false;
}

// We will buffer a local copy of the file. How large a buffer do we need?
FileHandle_Seek(file, 0, SeekEnd);
bufferedDefSize = FileHandle_Tell(file);
FileHandle_Rewind(file);
bufferedDef = (char*) calloc(1, bufferedDefSize + 1);
if(NULL == bufferedDef)
{
DED_SetError("Out of memory while trying to buffer file for reading.");
Str_Free(&transPath);
return false;
}

// Copy the file into the local buffer and parse definitions.
FileHandle_Read(file, (uint8_t*)bufferedDef, bufferedDefSize);
F_Delete(file);
result = DED_ReadData(ded, bufferedDef, Str_Text(&transPath));

// Done. Release temporary storage and return the result.
free(bufferedDef);
Str_Free(&transPath);
return result;
}

0 comments on commit ef5c12b

Please sign in to comment.