bool EffectParser::Parse(OverlayRenderer* renderer, vector<TextureRender*> *renderElms, std::string file, Json::Value data) { filebuf fb; fb.open (file,ios::in); istream is(&fb); if(!fb.is_open()){ FLog::Log(FLOG_ERROR, "EffectParser::Parse - Failed to load script file."); return false; } Json::Value root; Json::Reader reader; bool parsedSuccess = reader.parse(is, root, false); fb.close(); if(!parsedSuccess){ FLog::Log(FLOG_ERROR, "EffectParser::Parse - Failed to parse script json."); return false; } //check type exists if(!root.isMember("effects")){ FLog::Log(FLOG_ERROR, "EffectParser::Parse - No list of effects"); return false; } Json::Value e = root["effects"]; for(Json::ValueIterator it = e.begin(); it != e.end(); it++){ TextureRender *ren = ParseEffect(renderer, *it, &data); if(ren != NULL) renderElms->push_back(ren); } return true; }
std::shared_ptr<Spell> SpellParser::Parse(LPCTSTR pszText) { m_tokenizer = StringTokenizer(pszText); unsigned int uiSpellId = ParseUint(m_tokenizer.Next()); unsigned int uiCooldown = 0; unsigned int uiCastTime = 0; bool bStackable = false; unsigned int uiRange = 0; bool bArea = false; std::vector<std::shared_ptr<SpellEffect>> vecEffects; CString cszToken; while (!m_tokenizer.IsEmpty()) { cszToken = m_tokenizer.Next(); if (cszToken == _T("cooldown")) { uiCooldown = TimeToSeconds(m_tokenizer.Next()); } else if (cszToken == _T("cast-time")) { uiCastTime = TimeToSeconds(m_tokenizer.Next()); } else if (cszToken == _T("stackable")) { bStackable = true; } else if (cszToken == _T("range")) { uiRange = ParseUint(m_tokenizer.Next()); } else if (cszToken == _T("area")) { bArea = true; } else if (cszToken == _T("effect")) { ATLVERIFY(_T("[") == m_tokenizer.Next()); vecEffects.push_back(ParseEffect()); } } if (vecEffects.empty()) throw Exception(_T("error parsing spell: no effect specified"), __FILE__, __LINE__); std::shared_ptr<Spell> spSpell(new Spell(uiSpellId, vecEffects[0])); spSpell->Cooldown(uiCooldown); spSpell->CastTime(uiCastTime); spSpell->Stackable(bStackable); spSpell->Range(uiRange); spSpell->AreaSpell(bArea); if (vecEffects.size() > 2) throw Exception(_T("error parsing spell: too many effects specified"), __FILE__, __LINE__); if (vecEffects.size() == 2) spSpell->Effect2(vecEffects[1]); return spSpell; }
/*!*************************************************************************** @Function Parse @Output pReturnError error string @Return bool true for success parsing file @Description Parses a loaded PFX file. *****************************************************************************/ bool CPVRTPFXParser::Parse(CPVRTString * const pReturnError) { int nEndLine = 0; int nHeaderCounter = 0, nTexturesCounter = 0; unsigned int i,j,k; m_nNumVertShaders = 0; m_nNumFragShaders = 0; m_nNumEffects = 0; // Loop through the file for(unsigned int nLine=0; nLine < m_psContext->nNumLines; nLine++) { // Skip blank lines if(!*m_psContext->ppszEffectFile[nLine]) continue; if(strcmp("[HEADER]", m_psContext->ppszEffectFile[nLine]) == 0) { if(nHeaderCounter>0) { sprintf(errorMsg, "[HEADER] redefined on line %d\n", m_psContext->pnFileLineNumber[nLine]); *pReturnError = errorMsg; return false; } if(GetEndTag("HEADER", nLine, &nEndLine)) { if(ParseHeader(nLine, nEndLine, pReturnError)) nHeaderCounter++; else return false; } else { sprintf(errorMsg, "Missing [/HEADER] tag after [HEADER] on line %d\n", m_psContext->pnFileLineNumber[nLine]); *pReturnError = errorMsg; return false; } nLine = nEndLine; } else if(strcmp("[TEXTURES]", m_psContext->ppszEffectFile[nLine]) == 0) { if(nTexturesCounter>0) { sprintf(errorMsg, "[TEXTURES] redefined on line %d\n", m_psContext->pnFileLineNumber[nLine]); *pReturnError = errorMsg; return false; } if(GetEndTag("TEXTURES", nLine, &nEndLine)) { if(ParseTextures(nLine, nEndLine, pReturnError)) nTexturesCounter++; else return false; } else { sprintf(errorMsg, "Missing [/TEXTURES] tag after [TEXTURES] on line %d\n", m_psContext->pnFileLineNumber[nLine]); *pReturnError = errorMsg; return false; } nLine = nEndLine; } else if(strcmp("[VERTEXSHADER]", m_psContext->ppszEffectFile[nLine]) == 0) { if(GetEndTag("VERTEXSHADER", nLine, &nEndLine)) { if(m_nNumVertShaders >= m_nMaxVertShaders) { sprintf(errorMsg, "%d vertex shaders read, can't store any more, on line %d\n", m_nNumVertShaders, m_psContext->pnFileLineNumber[nLine]); *pReturnError = errorMsg; return false; } if(ParseShader(nLine, nEndLine, pReturnError, m_psVertexShader[m_nNumVertShaders], "VERTEXSHADER")) m_nNumVertShaders++; else { FREE(m_psVertexShader[m_nNumVertShaders].pszName); FREE(m_psVertexShader[m_nNumVertShaders].pszGLSLfile); FREE(m_psVertexShader[m_nNumVertShaders].pszGLSLcode); FREE(m_psVertexShader[m_nNumVertShaders].pszGLSLBinaryFile); return false; } } else { sprintf(errorMsg, "Missing [/VERTEXSHADER] tag after [VERTEXSHADER] on line %d\n", m_psContext->pnFileLineNumber[nLine]); *pReturnError = errorMsg; return false; } nLine = nEndLine; } else if(strcmp("[FRAGMENTSHADER]", m_psContext->ppszEffectFile[nLine]) == 0) { if(GetEndTag("FRAGMENTSHADER", nLine, &nEndLine)) { if(m_nNumFragShaders >= m_nMaxFragShaders) { sprintf(errorMsg, "%d fragment shaders read, can't store any more, on line %d\n", m_nNumFragShaders, m_psContext->pnFileLineNumber[nLine]); *pReturnError = errorMsg; return false; } if(ParseShader(nLine, nEndLine, pReturnError, m_psFragmentShader[m_nNumFragShaders], "FRAGMENTSHADER")) m_nNumFragShaders++; else { FREE(m_psFragmentShader[m_nNumFragShaders].pszName); FREE(m_psFragmentShader[m_nNumFragShaders].pszGLSLfile); FREE(m_psFragmentShader[m_nNumFragShaders].pszGLSLcode); FREE(m_psFragmentShader[m_nNumFragShaders].pszGLSLBinaryFile); return false; } } else { sprintf(errorMsg, "Missing [/FRAGMENTSHADER] tag after [FRAGMENTSHADER] on line %d\n", m_psContext->pnFileLineNumber[nLine]); *pReturnError = errorMsg; return false; } nLine = nEndLine; } else if(strcmp("[EFFECT]", m_psContext->ppszEffectFile[nLine]) == 0) { if(GetEndTag("EFFECT", nLine, &nEndLine)) { if(m_nNumEffects >= m_nMaxEffects) { sprintf(errorMsg, "%d effects read, can't store any more, on line %d\n", m_nNumEffects, m_psContext->pnFileLineNumber[nLine]); *pReturnError = errorMsg; return false; } if(ParseEffect(m_psEffect[m_nNumEffects], nLine, nEndLine, pReturnError)) m_nNumEffects++; else { for(i=0; i < m_psEffect[m_nNumEffects].nNumUniforms; ++i) { FREE(m_psEffect[m_nNumEffects].psUniform[i].pszName); FREE(m_psEffect[m_nNumEffects].psUniform[i].pszValue); } delete [] m_psEffect[m_nNumEffects].psUniform; for(i=0; i < m_psEffect[m_nNumEffects].nNumAttributes; ++i) { FREE(m_psEffect[m_nNumEffects].psAttribute[i].pszName); FREE(m_psEffect[m_nNumEffects].psAttribute[i].pszValue); } delete [] m_psEffect[m_nNumEffects].psAttribute; for(i=0; i < m_psEffect[m_nNumEffects].nNumTextures; ++i) { FREE(m_psEffect[m_nNumEffects].psTextures[i].pszName); } delete [] m_psEffect[m_nNumEffects].psTextures; FREE(m_psEffect[m_nNumEffects].pszFragmentShaderName); FREE(m_psEffect[m_nNumEffects].pszVertexShaderName); FREE(m_psEffect[m_nNumEffects].pszAnnotation); FREE(m_psEffect[m_nNumEffects].pszName); return false; } } else { sprintf(errorMsg, "Missing [/EFFECT] tag after [EFFECT] on line %d\n", m_psContext->pnFileLineNumber[nLine]); *pReturnError = errorMsg; return false; } nLine = nEndLine; } else { sprintf(errorMsg, "'%s' unexpected on line %d\n", m_psContext->ppszEffectFile[nLine], m_psContext->pnFileLineNumber[nLine]); *pReturnError = errorMsg; return false; } } if(m_nNumEffects < 1) { sprintf(errorMsg, "No [EFFECT] found. PFX file must have at least one defined.\n"); *pReturnError = errorMsg; return false; } if(m_nNumFragShaders < 1) { sprintf(errorMsg, "No [FRAGMENTSHADER] found. PFX file must have at least one defined.\n"); *pReturnError = errorMsg; return false; } if(m_nNumVertShaders < 1) { sprintf(errorMsg, "No [VERTEXSHADER] found. PFX file must have at least one defined.\n"); *pReturnError = errorMsg; return false; } for(i = 0; i < m_nNumEffects; ++i) { for(j = 0; j < m_psEffect[i].nNumTextures; ++j) { for(k = 0; k < m_nNumTextures; ++k) { if(strcmp(m_psEffect[i].psTextures[j].pszName, m_psTexture[k].pszName) == 0) break; } if(!m_nNumTextures || k == m_nNumTextures) { sprintf(errorMsg, "Error: TEXTURE '%s' is not defined in [TEXTURES].\n", m_psEffect[i].psTextures[j].pszName); *pReturnError = errorMsg; return false; } } } return true; }
TextureRender* EffectParser::ParseEffect(OverlayRenderer* renderer, Json::Value effect, Json::Value *data) { TextureRender* parent; bool hasParent = false; if(!effect["parent"].empty()) { parent = ParseEffect(renderer, effect["parent"], data); hasParent = true; } if(hasParent && !parent){ FLog::Log(FLOG_DEBUG, "EffectParser::ParseEffect - failed to load parent effect"); return NULL; } if(!effect.isMember("type") || !effect.isMember("data")){ FLog::Log(FLOG_DEBUG, "EffectParser::ParseEffect - missing vital data for the curret effect"); return parent; } std::string type = effect["type"].asString(); if(!hasParent){ //must be source effect if(type.compare("SolidTexture") == 0){ FLog::Log(FLOG_DEBUG, "EffectParser::ParseEffect (SolidTexture) - created"); return new SolidTexture(renderer); } else if(type.compare("TextTexture") == 0){ FLog::Log(FLOG_DEBUG, "EffectParser::ParseEffect (TextTexture) - created"); TextTexture *t = new TextTexture(renderer); Json::Value dat = effect["data"]; if(dat.isMember("name") && dat.isMember("posX") && dat.isMember("posY")){ int posX = dat["posX"].asInt(); int posY = dat["posY"].asInt(); string name = dat["name"].asString(); string value = name; if(data->isMember(name)){ value = data->get(name, dat["name"]).asString(); } else FLog::Log(FLOG_ERROR, "EffectParser::ParseEffect (TextTexture) - no text fill found for '%s'", name.c_str()); printf("hi - %s\n",value.c_str()); t->setText(value.c_str(), renderer->getDefaultCharSet(), posX, posY, 1.0f, 1.0f);//TODO dynamicify the rest of this } else FLog::Log(FLOG_ERROR, "EffectParser::ParseEffect (TextTexture) - incomplete data"); return t; } else if(type.compare("PNGTexture") == 0){ FLog::Log(FLOG_DEBUG, "EffectParser::ParseEffect (PNGTexture) - created"); PNGTexture *t = new PNGTexture(renderer); Json::Value dat = effect["data"]; if(dat.isMember("file")){ if(JsonUtil::FileExists(dat["file"].asString().c_str())) t->LoadPNG(dat["file"].asString()); else FLog::Log(FLOG_ERROR, "EffectParser::ParseEffect (PNGTexture) - file doesnt exist"); } else FLog::Log(FLOG_ERROR, "EffectParser::ParseEffect (PNGTexture) - file not defined"); return t; } else { FLog::Log(FLOG_DEBUG, "EffectParser::ParseEffect - unknown source"); return NULL; } } else { //must be an actual effect if(type.compare("ColourTexture") == 0){ FLog::Log(FLOG_DEBUG, "EffectParser::ParseEffect (ColourTexture) - create"); ColourTexture *t = new ColourTexture(renderer, parent); Json::Value dat = effect["data"]; if(dat.isMember("red") && dat.isMember("green") && dat.isMember("blue") && dat.isMember("alpha")){ float red = dat["red"].asFloat(); float green = dat["green"].asFloat(); float blue = dat["blue"].asFloat(); float alpha = dat["alpha"].asFloat(); t->SetColour(red, green, blue, alpha); } else FLog::Log(FLOG_ERROR, "EffectParser::ParseEffect (ColourTexture) - missing colour data"); return t; } else if(type.compare("FadeTexture") == 0){ FLog::Log(FLOG_DEBUG, "EffectParser::ParseEffect (FadeTexture) - create"); FadeTexture *t = new FadeTexture(renderer, parent); Json::Value dat = effect["data"]; if(dat.isMember("points")){ Json::Value points = dat["points"]; if(points.isArray() && points.size() >= 1){ for(Json::ValueIterator it = points.begin(); it != points.end(); it++){ Json::Value p = *it; if(p.isMember("field") && p.isMember("alpha")){ int field = p["field"].asInt(); float alpha = p["alpha"].asFloat(); t->addPoint(new FadePoint(field, alpha)); } else FLog::Log(FLOG_ERROR, "EffectParser::ParseEffect (FadeTexture) - data point is invalid"); } } else FLog::Log(FLOG_ERROR, "EffectParser::ParseEffect (FadeTexture) - no data array"); } else FLog::Log(FLOG_ERROR, "EffectParser::ParseEffect (FadeTexture) - no data points"); return t; } else if(type.compare("MaskTexture") == 0){ FLog::Log(FLOG_DEBUG, "EffectParser::ParseEffect (MaskTexture) - create"); MaskTexture *t = new MaskTexture(renderer, parent); Json::Value dat = effect["data"]; if(dat.isMember("points")){ Json::Value points = dat["points"]; if(points.isArray() && points.size() >= 1){ for(Json::ValueIterator it = points.begin(); it != points.end(); it++){ Json::Value p = *it; if(p.isMember("bl") && p.isMember("br") && p.isMember("tl") && p.isMember("tr") && p.isMember("field")){ Json::Value bl = p["bl"]; Json::Value br = p["br"]; Json::Value tl = p["tl"]; Json::Value tr = p["tr"]; if(bl.isMember("x") && bl.isMember("y") && br.isMember("x") && br.isMember("y") && tl.isMember("x") && tl.isMember("y") && tr.isMember("x") && tr.isMember("y")){ float blX = bl["x"].asFloat(); float blY = bl["y"].asFloat(); float brX = br["x"].asFloat(); float brY = br["y"].asFloat(); float tlX = tl["x"].asFloat(); float tlY = tl["y"].asFloat(); float trX = tr["x"].asFloat(); float trY = tr["y"].asFloat(); int field = p["field"].asInt(); t->addPoint(new MaskPoint(field, tlX, tlY, trX, trY, blX, blY, brX, brY)); } else FLog::Log(FLOG_ERROR, "EffectParser::ParseEffect (MaskTexture) - data point is invalid"); } else FLog::Log(FLOG_ERROR, "EffectParser::ParseEffect (MaskTexture) - data point is invalid"); } } } else FLog::Log(FLOG_ERROR, "EffectParser::ParseEffect (MaskTexture) - no data points"); return t; } else { FLog::Log(FLOG_DEBUG, "EffectParser::ParseEffect - unknown effect"); return NULL; } } }