From ef5c12bc7341e9abcb4897ac3f8b1a5cabc0ab9c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jaakko=20Ker=C3=A4nen?= Date: Mon, 5 May 2014 19:19:56 +0300 Subject: [PATCH] Refactor|Cleanup: Cleaning up the old DED sources --- doomsday/client/src/def_main.cpp | 31 +---- .../libdoomsday/include/doomsday/defs/ded.h | 7 +- .../include/doomsday/defs/dedfile.h | 10 ++ .../include/doomsday/defs/dedparser.h | 9 +- doomsday/libdoomsday/src/defs/ded.cpp | 31 +++++ doomsday/libdoomsday/src/defs/dedfile.cpp | 64 ++++++++++ doomsday/libdoomsday/src/defs/dedparser.cpp | 115 ++++-------------- 7 files changed, 143 insertions(+), 124 deletions(-) diff --git a/doomsday/client/src/def_main.cpp b/doomsday/client/src/def_main.cpp index ba5e03129d..f2b8eeda12 100644 --- a/doomsday/client/src/def_main.cpp +++ b/doomsday/client/src/def_main.cpp @@ -110,6 +110,7 @@ int Def_GetGameClasses(void) xgClassLinks = &nullXgClassLinks; } + // Let the parser know of the XG classes. DED_SetXGClassLinks(xgClassLinks); return 1; @@ -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(*_uri); - - if(uri.scheme().compareWithoutCase("Values")) return 0; - return Def_GetValueById(uri.pathCStr()); + return defs.getValueByUri(*reinterpret_cast(_uri)); } ded_mapinfo_t* Def_GetMapInfo(struct uri_s const *_uri) @@ -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 @@ -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); } /** diff --git a/doomsday/libdoomsday/include/doomsday/defs/ded.h b/doomsday/libdoomsday/include/doomsday/defs/ded.h index f19ec663de..94e11244e2 100644 --- a/doomsday/libdoomsday/include/doomsday/defs/ded.h +++ b/doomsday/libdoomsday/include/doomsday/defs/ded.h @@ -677,7 +677,6 @@ struct LIBDOOMSDAY_PUBLIC ded_s { ded_material_t* materials; // Models. - //ded_model_t* models; typedef std::vector Models; Models models; @@ -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; @@ -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; diff --git a/doomsday/libdoomsday/include/doomsday/defs/dedfile.h b/doomsday/libdoomsday/include/doomsday/defs/dedfile.h index 7bfdb6c8ba..37b1947bab 100644 --- a/doomsday/libdoomsday/include/doomsday/defs/dedfile.h +++ b/doomsday/libdoomsday/include/doomsday/defs/dedfile.h @@ -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 diff --git a/doomsday/libdoomsday/include/doomsday/defs/dedparser.h b/doomsday/libdoomsday/include/doomsday/defs/dedparser.h index 42066c3d35..1e04e7f030 100644 --- a/doomsday/libdoomsday/include/doomsday/defs/dedparser.h +++ b/doomsday/libdoomsday/include/doomsday/defs/dedparser.h @@ -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 diff --git a/doomsday/libdoomsday/src/defs/ded.cpp b/doomsday/libdoomsday/src/defs/ded.cpp index eeeff3c89c..98b037e6e4 100644 --- a/doomsday/libdoomsday/src/defs/ded.cpp +++ b/doomsday/libdoomsday/src/defs/ded.cpp @@ -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"); @@ -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; @@ -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; +} diff --git a/doomsday/libdoomsday/src/defs/dedfile.cpp b/doomsday/libdoomsday/src/defs/dedfile.cpp index 9f555e814b..9bb0a6ac78 100644 --- a/doomsday/libdoomsday/src/defs/dedfile.cpp +++ b/doomsday/libdoomsday/src/defs/dedfile.cpp @@ -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; +} diff --git a/doomsday/libdoomsday/src/defs/dedparser.cpp b/doomsday/libdoomsday/src/defs/dedparser.cpp index 03bd15c1c1..f1ad208cdc 100644 --- a/doomsday/libdoomsday/src/defs/dedparser.cpp +++ b/doomsday/libdoomsday/src/defs/dedparser.cpp @@ -76,15 +76,15 @@ using namespace de; #define ISTOKEN(X) (!stricmp(token, X)) #define READSTR(X) if(!ReadString(X, sizeof(X))) { \ - SetError("Syntax error in string value."); \ + DED_SetError("Syntax error in string value."); \ retVal = false; goto ded_end_read; } #define READURI(X, SHM) if(!ReadUri(X, SHM)) { \ - SetError("Syntax error parsing resource path."); \ + DED_SetError("Syntax error parsing resource path."); \ retVal = false; goto ded_end_read; } -#define MISSING_SC_ERROR SetError("Missing semicolon."); \ +#define MISSING_SC_ERROR DED_SetError("Missing semicolon."); \ retVal = false; goto ded_end_read; #define CHECKSC if(source->version <= 5) { ReadToken(); if(!ISTOKEN(";")) { MISSING_SC_ERROR; } } @@ -155,7 +155,7 @@ static char* sdup(char const* str) return newstr; } -static void SetError(char const* str) +void DED_SetError(char const *str) { sprintf(dedReadError, "Error in %s:\n Line %i: %s", source ? source->fileName : "?", source ? source->lineNumber : 0, @@ -445,7 +445,7 @@ static int ReadByte(unsigned char* dest) ReadToken(); if(ISTOKEN(";")) { - SetError("Missing integer value."); + DED_SetError("Missing integer value."); return false; } @@ -461,7 +461,7 @@ static int ReadInt(int* dest, int unsign) ReadToken(); if(ISTOKEN(";")) { - SetError("Missing integer value."); + DED_SetError("Missing integer value."); return false; } @@ -477,7 +477,7 @@ static int ReadFloat(float* dest) ReadToken(); if(ISTOKEN(";")) { - SetError("Missing float value."); + DED_SetError("Missing float value."); return false; } @@ -493,7 +493,7 @@ static int ReadFlags(ded_t *ded, int *dest, char const *prefix) ReadToken(); if(ISTOKEN(";")) { - SetError("Missing flags value."); + DED_SetError("Missing flags value."); return false; } if(ISTOKEN("0")) @@ -606,7 +606,7 @@ static int ReadLabel(char* label) ReadToken(); if(source->atEnd) { - SetError("Unexpected end of file."); + DED_SetError("Unexpected end of file."); return false; } if(ISTOKEN("}")) // End block. @@ -618,7 +618,7 @@ static int ReadLabel(char* label) { if(source->version <= 5) { - SetError("Label without value."); + DED_SetError("Label without value."); return false; } continue; // Semicolons are optional in v6. @@ -702,7 +702,7 @@ static dd_bool DED_CheckCondition(char const *cond, dd_bool expected) * @param buffer The data to be read, must be null-terminated. * @param _sourceFile Just FYI. */ -static int DED_ReadData(ded_t* ded, const char* buffer, const char* _sourceFile) +int DED_ReadData(ded_t* ded, const char* buffer, const char* _sourceFile) { char dummy[128], label[128], tmp[256]; int dummyInt, idx, retVal = true; @@ -895,7 +895,7 @@ static int DED_ReadData(ded_t* ded, const char* buffer, const char* _sourceFile) } else { - SetError("Cannot both Copy(Previous) and Modify."); + DED_SetError("Cannot both Copy(Previous) and Modify."); retVal = false; goto ded_end_read; } @@ -995,7 +995,7 @@ static int DED_ReadData(ded_t* ded, const char* buffer, const char* _sourceFile) } else { - SetError("Cannot both Copy(Previous) and Modify."); + DED_SetError("Cannot both Copy(Previous) and Modify."); retVal = false; goto ded_end_read; } @@ -1179,7 +1179,7 @@ static int DED_ReadData(ded_t* ded, const char* buffer, const char* _sourceFile) } else { - SetError("Cannot both Copy(Previous) and Modify."); + DED_SetError("Cannot both Copy(Previous) and Modify."); retVal = false; goto ded_end_read; } @@ -1257,7 +1257,7 @@ static int DED_ReadData(ded_t* ded, const char* buffer, const char* _sourceFile) if(layer >= DED_MAX_MATERIAL_LAYERS) { - SetError("Too many Material layers."); + DED_SetError("Too many Material layers."); retVal = false; goto ded_end_read; } @@ -1320,7 +1320,7 @@ static int DED_ReadData(ded_t* ded, const char* buffer, const char* _sourceFile) if(light == DED_MAX_MATERIAL_DECORATIONS) { - SetError("Too many lights in material."); + DED_SetError("Too many lights in material."); retVal = false; goto ded_end_read; } @@ -1640,7 +1640,7 @@ static int DED_ReadData(ded_t* ded, const char* buffer, const char* _sourceFile) ded_skymodel_t *sm = &sky->models[sub]; if(sub == NUM_SKY_MODELS) { // Too many! - SetError("Too many Sky models."); + DED_SetError("Too many Sky models."); retVal = false; goto ded_end_read; } @@ -1751,7 +1751,7 @@ static int DED_ReadData(ded_t* ded, const char* buffer, const char* _sourceFile) ded_skymodel_t *sm = &mi->sky.models[sub]; if(sub == NUM_SKY_MODELS) { // Too many! - SetError("Too many Sky models."); + DED_SetError("Too many Sky models."); retVal = false; goto ded_end_read; } @@ -1801,7 +1801,7 @@ static int DED_ReadData(ded_t* ded, const char* buffer, const char* _sourceFile) } else { - SetError("Syntax error in Text value."); + DED_SetError("Syntax error in Text value."); retVal = false; goto ded_end_read; } @@ -1875,7 +1875,7 @@ static int DED_ReadData(ded_t* ded, const char* buffer, const char* _sourceFile) int ascii = atoi(label); if(ascii < 0 || ascii > 255) { - SetError("Invalid ascii code."); + DED_SetError("Invalid ascii code."); retVal = false; goto ded_end_read; } @@ -1919,7 +1919,7 @@ static int DED_ReadData(ded_t* ded, const char* buffer, const char* _sourceFile) READLABEL_NOBREAK; if(strchr(label, '|')) { - SetError("Value labels can not include '|' characters (ASCII 124)."); + DED_SetError("Value labels can not include '|' characters (ASCII 124)."); retVal = false; goto ded_end_read; } @@ -1946,7 +1946,7 @@ static int DED_ReadData(ded_t* ded, const char* buffer, const char* _sourceFile) } else { - SetError("Syntax error in Value string."); + DED_SetError("Syntax error in Value string."); retVal = false; goto ded_end_read; } @@ -1989,7 +1989,7 @@ static int DED_ReadData(ded_t* ded, const char* buffer, const char* _sourceFile) else { // Only the above characters are allowed. - SetError("Illegal token."); + DED_SetError("Illegal token."); retVal = false; goto ded_end_read; } @@ -2304,7 +2304,7 @@ static int DED_ReadData(ded_t* ded, const char* buffer, const char* _sourceFile) { if(sub == DED_DECOR_NUM_LIGHTS) { - SetError("Too many lights in decoration."); + DED_SetError("Too many lights in decoration."); retVal = false; goto ded_end_read; } @@ -2713,73 +2713,6 @@ static int DED_ReadData(ded_t* ded, const char* buffer, const char* _sourceFile) } /* *INDENT-ON* */ -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) - { - 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) - { - 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; -} - -/** - * Reads definitions from the given lump. - */ -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; - } - SetError("Bad lump number."); - return false; -} - void DED_Include(ded_t *ded, const char* fileName, const char* parentDirectory) { ddstring_t tmp;