From 0aaf7b45d6d5db6465bfdf2be909d22d611202bd Mon Sep 17 00:00:00 2001 From: "False.Genesis" Date: Fri, 23 Mar 2007 21:00:20 +0000 Subject: [PATCH] * implemented usage of if/loop statements in #onload blocks * check for if/loop/endif/endloop match in scripts * check for brackets match (same amount of opening/closing brackets) at line load. skip corrupt lines * some code cleanups --- src/Client/Auth/AuthCrypt.cpp | 10 --- src/Client/Auth/AuthCrypt.h | 3 - src/Client/DefScript/DefScript.cpp | 107 +++++++++++++++++++++++++---- src/Client/DefScript/DefScript.h | 3 +- src/Client/DefScriptInterface.cpp | 22 +++--- src/Client/PseuWoW.cpp | 13 ---- src/Client/PseuWoW.h | 5 +- src/Client/SCPDatabase.cpp | 13 ++++ src/Client/SCPDatabase.h | 12 ++++ src/Client/World/WorldSession.cpp | 2 +- src/Client/World/WorldSocket.cpp | 8 +-- 11 files changed, 138 insertions(+), 60 deletions(-) diff --git a/src/Client/Auth/AuthCrypt.cpp b/src/Client/Auth/AuthCrypt.cpp index ffa56d6..5122693 100644 --- a/src/Client/Auth/AuthCrypt.cpp +++ b/src/Client/Auth/AuthCrypt.cpp @@ -35,9 +35,6 @@ void AuthCrypt::DecryptRecv(uint8 *data, size_t len) if (!_initialized) return; if (len < CRYPTED_RECV_LEN) return; - _recv_i2=_recv_i; - _recv_j2=_recv_j; - for (size_t t = 0; t < CRYPTED_RECV_LEN; t++) { _recv_i %= _key.size(); @@ -70,13 +67,6 @@ void AuthCrypt::SetKey(uint8 *key, size_t len) std::copy(key, key + len, _key.begin()); } -void AuthCrypt::DecryptRescue(void) -{ - // undos the last decrypt operation - _recv_i=_recv_i2; - _recv_j=_recv_j2; -} - AuthCrypt::~AuthCrypt() { } diff --git a/src/Client/Auth/AuthCrypt.h b/src/Client/Auth/AuthCrypt.h index 59a081e..2c09f16 100644 --- a/src/Client/Auth/AuthCrypt.h +++ b/src/Client/Auth/AuthCrypt.h @@ -40,12 +40,9 @@ class AuthCrypt bool IsInitialized() { return _initialized; } - void DecryptRescue(void); - private: std::vector _key; uint8 _send_i, _send_j, _recv_i, _recv_j; - uint8 _send_i2, _send_j2, _recv_i2, _recv_j2; bool _initialized; }; #endif diff --git a/src/Client/DefScript/DefScript.cpp b/src/Client/DefScript/DefScript.cpp index 784e136..869f939 100644 --- a/src/Client/DefScript/DefScript.cpp +++ b/src/Client/DefScript/DefScript.cpp @@ -10,6 +10,8 @@ using namespace DefScriptTools; +#define SN_ONLOAD "?onload?" + enum DefScriptBlockType { BLOCK_IF, @@ -136,13 +138,18 @@ bool DefScriptPackage::ScriptExists(std::string name) for (std::map::iterator i = Script.begin();i != Script.end();i++) if(i->first == name && i->second != NULL) return true; - // actually, this part can lead to bugs, need to fix if it gets unstable - //for(unsigned int i=0;i<_functable.size();i++) - // if(name == _functable[i].name) - // return true; return false; } +void DefScriptPackage::DeleteScript(std::string sn) +{ + if(ScriptExists(sn)) + { + delete GetScript(sn); + Script.erase(sn); + } +} + bool DefScriptPackage::LoadByName(std::string name){ return LoadScriptFromFile((scPath+name).append(".def"),name); } @@ -152,18 +159,21 @@ bool DefScriptPackage::LoadScriptFromFile(std::string fn, std::string sn){ std::string label, value, line; std::fstream f; - bool load_debug=false,load_notify=false, exec=false; + bool load_debug=false,load_notify=false, exec=false, cantload=false; char z; f.open(fn.c_str(),std::ios_base::in); if(!f.is_open()) return false; + std::deque Blocks; + if(GetScript(sn)) delete GetScript(sn); DefScript *newscript = new DefScript(this); Script[sn] = newscript; Script[sn]->SetName(sn); // necessary that the script knows its own name + DeleteScript(SN_ONLOAD); while(!f.eof()){ line.clear(); while (true) { @@ -205,24 +215,92 @@ bool DefScriptPackage::LoadScriptFromFile(std::string fn, std::string sn){ } // help with loading lines where a space or tab have accidently been put after the cmd if(memcmp(line.c_str(),"else ",5)==0 || memcmp(line.c_str(),"else\t",5)==0) line="else"; - if(memcmp(line.c_str(),"endif ",6)==0 || memcmp(line.c_str(),"endif\t",5)==0) line="endif"; - if(memcmp(line.c_str(),"loop ",5)==0 || memcmp(line.c_str(),"loop\t",5)==0) line="loop"; - if(memcmp(line.c_str(),"endloop ",8)==0 || memcmp(line.c_str(),"endloop\t",8)==0) line="endloop"; + else if(memcmp(line.c_str(),"endif ",6)==0 || memcmp(line.c_str(),"endif\t",5)==0) line="endif"; + else if(memcmp(line.c_str(),"loop ",5)==0 || memcmp(line.c_str(),"loop\t",5)==0) line="loop"; + else if(memcmp(line.c_str(),"endloop ",8)==0 || memcmp(line.c_str(),"endloop\t",8)==0) line="endloop"; + if(line=="else" || line=="endif" || line=="loop" || line=="endloop") line=stringToLower(line); + // check for correct block match + if(memcmp(line.c_str(),"if ",3)==0) + Blocks.push_back(BLOCK_IF); + else if(line=="else") + { + if(Blocks.empty() || Blocks.back()!=BLOCK_IF) + { + cantload=true; + break; + } + } + else if(line=="endif") + { + if(Blocks.empty() || Blocks.back()!=BLOCK_IF) + { + cantload=true; + break; + } + Blocks.pop_back(); + } + else if(line=="loop") + Blocks.push_back(BLOCK_LOOP); + else if(line=="endloop") + { + if(Blocks.empty() || Blocks.back()!=BLOCK_LOOP) + { + cantload=true; + break; + } + Blocks.pop_back(); + } + + // check for correct bracket match + unsigned int bopen=0; + bool mismatch=false; + for(unsigned int bpos=0;bposAddLine(line); else { - this->RunSingleLineFromScript(line,Script[sn]); - } - - + if(!ScriptExists(SN_ONLOAD)) + Script[SN_ONLOAD] = new DefScript(this); + Script[SN_ONLOAD]->AddLine(line); + } } f.close(); + if(cantload) + { + printf("DefScript: Error loading script '%s'\n",sn.c_str()); + DeleteScript(sn); + return false; + } + if(ScriptExists(SN_ONLOAD)) + { + RunScript(SN_ONLOAD,NULL,sn); + DeleteScript(SN_ONLOAD); + } if(load_notify) std::cout << "+> Script '" << sn << "' [" << fn << "] successfully loaded.\n"; @@ -320,7 +398,7 @@ bool DefScriptPackage::BoolRunScript(std::string name, CmdSet *pSet) } // the referred pSet is the parent from which RunScript() has been called -DefReturnResult DefScriptPackage::RunScript(std::string name, CmdSet *pSet) +DefReturnResult DefScriptPackage::RunScript(std::string name, CmdSet *pSet,std::string override_name) { DefReturnResult r; if( (!ScriptExists(name)) && (!HasFunc(name)) ) @@ -339,6 +417,9 @@ DefReturnResult DefScriptPackage::RunScript(std::string name, CmdSet *pSet) return r; } + if(!override_name.empty()) + name=override_name; + CmdSet temp; if(!pSet) { diff --git a/src/Client/DefScript/DefScript.h b/src/Client/DefScript/DefScript.h index d49c8d8..1db10a3 100644 --- a/src/Client/DefScript/DefScript.h +++ b/src/Client/DefScript/DefScript.h @@ -102,11 +102,12 @@ public: DefScript *GetScript(std::string); unsigned int GetScripts(void); bool LoadScriptFromFile(std::string,std::string); - DefReturnResult RunScript(std::string,CmdSet*); + DefReturnResult RunScript(std::string name,CmdSet* pSet,std::string override_name=""); bool BoolRunScript(std::string,CmdSet*); unsigned int GetScriptID(std::string); DefReturnResult RunSingleLine(std::string); bool ScriptExists(std::string); + void DeleteScript(std::string); VarSet variables; void SetPath(std::string); bool LoadByName(std::string); diff --git a/src/Client/DefScriptInterface.cpp b/src/Client/DefScriptInterface.cpp index c86345b..82be6ef 100644 --- a/src/Client/DefScriptInterface.cpp +++ b/src/Client/DefScriptInterface.cpp @@ -281,7 +281,7 @@ DefReturnResult DefScriptPackage::SCloadscp(CmdSet& Set) return false; std::string dbname = stringToLower(Set.arg[0]); // TODO: remove db if loading was not successful - uint32 sections=((PseuInstance*)parentMethod)->GetSCPDatabase(dbname).LoadFromFile((char*)Set.defaultarg.c_str()); + uint32 sections=((PseuInstance*)parentMethod)->dbmgr.GetDB(dbname).LoadFromFile((char*)Set.defaultarg.c_str()); if(sections) { logdetail("Loaded SCP: \"%s\" [%s] (%u sections)",dbname.c_str(),Set.defaultarg.c_str(),sections); @@ -296,7 +296,7 @@ DefReturnResult DefScriptPackage::SCloadscp(CmdSet& Set) DefReturnResult DefScriptPackage::SCScpExists(CmdSet& Set) { - return (!Set.defaultarg.empty()) && ((PseuInstance*)parentMethod)->HasSCPDatabase(Set.defaultarg); + return (!Set.defaultarg.empty()) && ((PseuInstance*)parentMethod)->dbmgr.HasDB(Set.defaultarg); } DefReturnResult DefScriptPackage::SCScpSectionExists(CmdSet& Set) @@ -305,8 +305,8 @@ DefReturnResult DefScriptPackage::SCScpSectionExists(CmdSet& Set) if(!Set.arg[0].empty()) dbname=Set.arg[0]; return (!Set.defaultarg.empty()) && (!dbname.empty()) - && ((PseuInstance*)parentMethod)->HasSCPDatabase(dbname) - && ((PseuInstance*)parentMethod)->GetSCPDatabase(dbname).HasField((uint32)DefScriptTools::toNumber(Set.defaultarg)); + && ((PseuInstance*)parentMethod)->dbmgr.HasDB(dbname) + && ((PseuInstance*)parentMethod)->dbmgr.GetDB(dbname).HasField((uint32)DefScriptTools::toNumber(Set.defaultarg)); } DefReturnResult DefScriptPackage::SCScpEntryExists(CmdSet& Set) @@ -318,9 +318,9 @@ DefReturnResult DefScriptPackage::SCScpEntryExists(CmdSet& Set) if(!Set.arg[1].empty()) keyid=(uint32)DefScriptTools::toNumber(Set.arg[1]); return (!Set.defaultarg.empty()) && (!dbname.empty()) - && ((PseuInstance*)parentMethod)->HasSCPDatabase(dbname) - && ((PseuInstance*)parentMethod)->GetSCPDatabase(dbname).HasField(keyid) - && ((PseuInstance*)parentMethod)->GetSCPDatabase(dbname).GetField(keyid).HasEntry(Set.defaultarg); + && ((PseuInstance*)parentMethod)->dbmgr.HasDB(dbname) + && ((PseuInstance*)parentMethod)->dbmgr.GetDB(dbname).HasField(keyid) + && ((PseuInstance*)parentMethod)->dbmgr.GetDB(dbname).GetField(keyid).HasEntry(Set.defaultarg); } @@ -340,11 +340,11 @@ DefReturnResult DefScriptPackage::SCGetScpValue(CmdSet& Set) if(!Set.defaultarg.empty()) entry=Set.defaultarg; if( (!entry.empty()) && (!dbname.empty()) - && ((PseuInstance*)parentMethod)->HasSCPDatabase(dbname) - && ((PseuInstance*)parentMethod)->GetSCPDatabase(dbname).HasField(keyid) - && ((PseuInstance*)parentMethod)->GetSCPDatabase(dbname).GetField(keyid).HasEntry(entry)) + && ((PseuInstance*)parentMethod)->dbmgr.HasDB(dbname) + && ((PseuInstance*)parentMethod)->dbmgr.GetDB(dbname).HasField(keyid) + && ((PseuInstance*)parentMethod)->dbmgr.GetDB(dbname).GetField(keyid).HasEntry(entry)) { - r.ret = ((PseuInstance*)parentMethod)->GetSCPDatabase(dbname).GetField(keyid).GetString(entry); + r.ret = ((PseuInstance*)parentMethod)->dbmgr.GetDB(dbname).GetField(keyid).GetString(entry); } else { diff --git a/src/Client/PseuWoW.cpp b/src/Client/PseuWoW.cpp index 25e13ee..1ece5b6 100644 --- a/src/Client/PseuWoW.cpp +++ b/src/Client/PseuWoW.cpp @@ -238,19 +238,6 @@ void PseuInstance::Sleep(uint32 msecs) GetRunnable()->sleep(msecs); } -SCPDatabase& PseuInstance::GetSCPDatabase(std::string dbname) -{ - return _dbmap[dbname]; -} - -bool PseuInstance::HasSCPDatabase(std::string dbname) -{ - for(std::map::iterator i = _dbmap.begin(); i != _dbmap.end(); i++) - if(i->first == dbname) - return true; - return false; -} - PseuInstanceConf::PseuInstanceConf() { enablecli=false; diff --git a/src/Client/PseuWoW.h b/src/Client/PseuWoW.h index 72a7cb3..2990408 100644 --- a/src/Client/PseuWoW.h +++ b/src/Client/PseuWoW.h @@ -72,8 +72,7 @@ class PseuInstance void SetSessionKey(BigNumber key) { _sessionkey = key; } BigNumber GetSessionKey(void) { return _sessionkey; } void SetError(void) { _error = true; } - SCPDatabase& GetSCPDatabase(std::string); - bool HasSCPDatabase(std::string); + SCPDatabaseMgr dbmgr; bool Init(); void SaveAllCache(void); @@ -103,8 +102,6 @@ class PseuInstance SocketHandler _sh; CliRunnable *_cli; ZThread::Thread _clithread; - std::map _dbmap; - }; diff --git a/src/Client/SCPDatabase.cpp b/src/Client/SCPDatabase.cpp index 7288c41..aec86cc 100644 --- a/src/Client/SCPDatabase.cpp +++ b/src/Client/SCPDatabase.cpp @@ -80,4 +80,17 @@ uint32 SCPDatabase::GetFieldByValue(std::string entry, std::string value) if(fm->second.HasEntry(entry) && fm->second.GetString(entry)==value) return fm->first; return 0; +} + +bool SCPDatabaseMgr::HasDB(std::string n) +{ + for(SCPDatabaseMap::iterator i = _map.begin(); i != _map.end(); i++) + if(i->first == n) + return true; + return false; +} + +SCPDatabase& SCPDatabaseMgr::GetDB(std::string n) +{ + return _map[n]; } \ No newline at end of file diff --git a/src/Client/SCPDatabase.h b/src/Client/SCPDatabase.h index 905e5df..ccc6016 100644 --- a/src/Client/SCPDatabase.h +++ b/src/Client/SCPDatabase.h @@ -40,4 +40,16 @@ private: }; +typedef std::map SCPDatabaseMap; + +class SCPDatabaseMgr +{ +public: + bool HasDB(std::string); + SCPDatabase& GetDB(std::string); + +private: + SCPDatabaseMap _map; +}; + #endif diff --git a/src/Client/World/WorldSession.cpp b/src/Client/World/WorldSession.cpp index c1550a1..c89ec4c 100644 --- a/src/Client/World/WorldSession.cpp +++ b/src/Client/World/WorldSession.cpp @@ -481,7 +481,7 @@ void WorldSession::_HandleMessageChatOpcode(WorldPacket& recvPacket) else if(msg.length()<15 && (msg.find("omg")!=std::string::npos || msg.find("omfg")!=std::string::npos) ) SendChatMessage(CHAT_MSG_SAY,lang,"OMG a bot logged in, you don't believe it :O",""); else if(msg.find("from")!=std::string::npos || msg.find("download")!=std::string::npos) - SendChatMessage(CHAT_MSG_SAY,lang,"you can dl me from http://my.opera.com/PseuWoW",""); + SendChatMessage(CHAT_MSG_SAY,lang,"http://my.opera.com/PseuWoW",""); else if(msg.find("Genesis")!=std::string::npos || msg.find("genesis")!=std::string::npos) SendChatMessage(CHAT_MSG_YELL,lang,"False.Genesis, they are calling you!! Come here, master xD",""); } diff --git a/src/Client/World/WorldSocket.cpp b/src/Client/World/WorldSocket.cpp index 5c4b758..94a425f 100644 --- a/src/Client/World/WorldSocket.cpp +++ b/src/Client/World/WorldSocket.cpp @@ -48,7 +48,7 @@ void WorldSocket::OnRead() ASSERT(_remaining > 0); // case pktsize==0 is handled below if(ibuf.GetLength() < _remaining) { - logerror("Delaying WorldPacket generation, bufsize is %u but should be >= %u",ibuf.GetLength(),_remaining); + DEBUG(logdebug("Delaying WorldPacket generation, bufsize is %u but should be >= %u",ibuf.GetLength(),_remaining)); break; } _gothdr=false; @@ -62,7 +62,7 @@ void WorldSocket::OnRead() { if(ibuf.GetLength() < sizeof(ServerPktHeader)) { - logerror("Delaying header reading, bufsize is %u but should be >= %u",ibuf.GetLength(),sizeof(ServerPktHeader)); + DEBUG(logdebug("Delaying header reading, bufsize is %u but should be >= %u",ibuf.GetLength(),sizeof(ServerPktHeader))); break; } ServerPktHeader hdr; @@ -72,8 +72,8 @@ void WorldSocket::OnRead() _opcode = hdr.cmd; if(_opcode > 800) // no opcode has yet a number over 800 { - logcritical("CRYPT ERROR: opcode=%u, remain=%u",_opcode,_remaining); - //GetSession()->GetInstance()->SetError(); // please test if this is stable!!! + logcritical("CRYPT ERROR: opcode=%u, remain=%u",_opcode,_remaining); // this should never be the case! + GetSession()->GetInstance()->SetError(); // no way to recover the crypt, must exit // if the crypt gets messy its hardly possible to recover it, especially if we dont know // the lentgh of the following data part // TODO: invent some way how to recover the crypt (reconnect?)