diff --git a/bin/_startup.def b/bin/_startup.def new file mode 100644 index 0000000..aab3396 --- /dev/null +++ b/bin/_startup.def @@ -0,0 +1,43 @@ +#permission=255 + +// PSEUWOW DEF_SCRIPT STARTUP FILE + +LOG * DefScript StartUp [${@version_short}]... + +// first, load all scripts in patch 'scripts' with extension .def +SET,fcount ?{LGETFILES,scriptlist,def scripts} +LOGDETAIL * Loading ${fcount} scripts. +// iterate over all files and load them; if counter i is equal to the amount of files we are done. +SET,i 0 +LOOP + IF ?{EQUAL,${i} ${fcount}} + EXITLOOP + ENDIF + SET,fn ./scripts/?{LINDEX,scriptlist ${i}} + LOGDEBUG * Loading script file [${fn}] + IF ?{NOT ?{LOADDEF ${fn}}} + LOGERROR Can't load script [${fn}] + ENDIF + ADD,i 1 +ENDLOOP + +UNSET tmp +UNSET fcount +UNSET i +UNSET fn +LDELETE scriptlist + +// loads & applies the configuration +CONFIG + +// set permissions for internal functions +INTERNAL_PERM + +// Load some SCP files +LOADALLSCP + +// do more stuff here in future... + +LOG * StartUp complete! + + diff --git a/bin/scripts/_startup.def b/bin/scripts/_startup.def deleted file mode 100644 index cee92a6..0000000 --- a/bin/scripts/_startup.def +++ /dev/null @@ -1,23 +0,0 @@ -#permission=255 - -// PSEUWOW DEF_SCRIPT STARTUP FILE - -LOG * DefScript StartUp [${@version_short}]... - -// loads & applies the configuration -CONFIG - -// set permissions for internal functions -INTERNAL_PERM - -// Load some SCP files -LOADALLSCP - -// do more stuff here in future... - -// load the uptime counter -LOADDEF uptime - -LOG * StartUp complete! - - diff --git a/src/Client/DefScript/DefScript.cpp b/src/Client/DefScript/DefScript.cpp index f0ed456..3b0414b 100644 --- a/src/Client/DefScript/DefScript.cpp +++ b/src/Client/DefScript/DefScript.cpp @@ -174,18 +174,19 @@ void DefScriptPackage::DeleteScript(std::string sn) } bool DefScriptPackage::LoadByName(std::string name){ - return LoadScriptFromFile((scPath+name).append(".def"),name); + return LoadScriptFromFile((scPath+name).append(".def")); } -bool DefScriptPackage::LoadScriptFromFile(std::string fn, std::string sn){ - if(fn.empty() || sn.empty()) +bool DefScriptPackage::LoadScriptFromFile(std::string fn){ + if(fn.empty()) return false; - std::string label, value, line; + std::string label, value, line, sn; std::fstream f; - bool load_debug=false,load_notify=false, exec=false, cantload=false; + bool load_debug=false,load_notify=false,cantload=false; char z; unsigned int absline=0; + DefScript *curScript = NULL; f.open(fn.c_str(),std::ios_base::in); if(!f.is_open()) @@ -193,15 +194,24 @@ bool DefScriptPackage::LoadScriptFromFile(std::string fn, std::string sn){ 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 + // auto-assign the scriptname as the plain filename without extension. can be changed while loading + unsigned int slashpos = fn.find_last_of("\\/"); + if(slashpos == std::string::npos) + slashpos = 0; + unsigned int ppos = fn.find_last_of("."); + if(ppos == std::string::npos) + ppos = fn.length(); + sn = fn.substr(slashpos+1,(ppos-slashpos-1)); + _UpdateOrCreateScriptByName(sn); + curScript=Script[sn]; + DeleteScript(SN_ONLOAD); - while(!f.eof()){ + + while(!f.eof()) + { line.clear(); - while (true) { + while (true) + { f.get(z); if(z=='\n' || f.eof()) break; @@ -225,20 +235,33 @@ bool DefScriptPackage::LoadScriptFromFile(std::string fn, std::string sn){ if(label=="permission") { scriptPermissionMap[sn] = atoi(value.c_str()); - } - if(line=="load_debug") - load_debug=true; - if(line=="load_notify") - load_notify=true; - if(line=="debug") - Script[sn]->SetDebug(true); - if(line=="onload") - exec=true; - if(line=="endonload" || line=="/onload") - exec=false; + } + else if(label=="script") + { + if(!curScript->GetLines()) // delete script if unused + DeleteScript(curScript->GetName()); + sn = value; + _UpdateOrCreateScriptByName(sn); + curScript=Script[sn]; + } + else if(line=="debug") + { + if(curScript) + curScript->SetDebug(true); + } + else if(line=="onload") + Script[SN_ONLOAD] = curScript = new DefScript(this); + else if(line=="endonload" || line=="/onload") + { + RunScript(SN_ONLOAD,NULL,sn); + DeleteScript(SN_ONLOAD); + curScript=Script[sn]; + } + //... continue; // line was an option, not script content } + // help with loading lines where a space or tab have accidently been put after the cmd std::string tline=stringToLower(line); if(memcmp(tline.c_str(),"else ",5)==0 || memcmp(tline.c_str(),"else\t",5)==0) tline="else"; @@ -304,31 +327,15 @@ bool DefScriptPackage::LoadScriptFromFile(std::string fn, std::string sn){ continue; // continue with next line without adding the current line to script } - if(load_debug) - std::cout<<"~LOAD: "<AddLine(line); - else - { - if(!ScriptExists(SN_ONLOAD)) - Script[SN_ONLOAD] = new DefScript(this); - Script[SN_ONLOAD]->AddLine(line); - } + curScript->AddLine(line); } f.close(); if(cantload || Blocks.size()) { - printf("DefScript: Error loading script '%s'. block mismatch?\n",sn.c_str()); + printf("DefScript: Error loading file '%s'. block mismatch?\n",fn.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"; // ... return true; @@ -346,7 +353,7 @@ DefScript::DefScript(DefScriptPackage *p) DefScript::~DefScript() { - Clear(); + Clear(); } void DefScript::Clear(void) @@ -387,7 +394,7 @@ std::string DefScript::GetLine(unsigned int id) bool DefScript::AddLine(std::string l){ if(l.empty()) return false; - Line.insert(Line.end(),l); + Line.push_back(l); return true; } @@ -427,12 +434,11 @@ DefReturnResult DefScriptPackage::RunScript(std::string name, CmdSet *pSet,std:: { DefReturnResult r; if( (!ScriptExists(name)) && (!HasFunc(name)) ) - if(!LoadByName(name)) - { - r.ok=false; // doesnt exist & cant be loaded - r.ret=""; - return r; - } + { + r.ok=false; // doesnt exist + r.ret=""; + return r; + } DefScript *sc = GetScript(name); if(!sc) @@ -933,4 +939,11 @@ DefReturnResult DefScriptPackage::Interpret(CmdSet& Set) return result; } - +void DefScriptPackage::_UpdateOrCreateScriptByName(std::string sn) +{ + if(GetScript(sn)) + DeleteScript(sn); + DefScript *newscript = new DefScript(this); + newscript->SetName(sn); // necessary that the script knows its own name + Script[sn] = newscript; +} diff --git a/src/Client/DefScript/DefScript.h b/src/Client/DefScript/DefScript.h index 94aa6ac..1b03621 100644 --- a/src/Client/DefScript/DefScript.h +++ b/src/Client/DefScript/DefScript.h @@ -106,7 +106,7 @@ public: void Clear(void); DefScript *GetScript(std::string); unsigned int GetScripts(void); - bool LoadScriptFromFile(std::string,std::string); + bool LoadScriptFromFile(std::string); DefReturnResult RunScript(std::string name,CmdSet* pSet,std::string override_name=""); bool BoolRunScript(std::string,CmdSet*); unsigned int GetScriptID(std::string); @@ -133,6 +133,7 @@ public: void My_Run(std::string line,std::string username); private: + void _UpdateOrCreateScriptByName(std::string); void _InitFunctions(void); DefXChgResult ReplaceVars(std::string str, CmdSet* pSet, unsigned char VarType, bool run_embedded); void SplitLine(CmdSet&,std::string); diff --git a/src/Client/DefScript/DefScriptFunctions.cpp b/src/Client/DefScript/DefScriptFunctions.cpp index 2e620a1..d63df54 100644 --- a/src/Client/DefScript/DefScriptFunctions.cpp +++ b/src/Client/DefScript/DefScriptFunctions.cpp @@ -27,37 +27,40 @@ DefReturnResult DefScriptPackage::func_out(CmdSet& Set){ } DefReturnResult DefScriptPackage::func_loaddef(CmdSet& Set){ - DefReturnResult r; if( ScriptExists(Set.defaultarg) ) { - r.ret="exists"; - return r; + return "exists"; } return func_reloaddef(Set); } +// supply either the filename in default scripts directory, without extension, +// OR provide the full file path WITH extension DefReturnResult DefScriptPackage::func_reloaddef(CmdSet& Set){ DefReturnResult r; bool result=false; std::string fn; - if(Set.arg[0].empty()) + + // if the filename does NOT contain any path, load from default script dir + std::string::size_type slashpos = Set.defaultarg.find_last_of("\\/"); + std::string::size_type ppos = Set.defaultarg.find_last_of("."); + if(slashpos == std::string::npos) { - result=LoadByName(Set.defaultarg); - fn=(scPath + Set.defaultarg).append(".def"); + fn=scPath+Set.defaultarg; } else { - std::string::size_type pos = Set.arg[0].find('/'); - if(pos == std::string::npos) - fn=scPath+Set.arg[0]; - else - fn=Set.arg[0]; - result=LoadScriptFromFile(fn,Set.defaultarg); + fn=Set.defaultarg; } + if(ppos==std::string::npos || ppos < slashpos) // even if there was neither / nor . they will be equal + fn+=".def"; + + result=LoadScriptFromFile(fn); + r.ret=fn; if(!result) { - std::cout << "Could not load script '" << Set.defaultarg << "' [" << fn << "]\n"; + //std::cout << "Could not load script '" << Set.defaultarg << "' [" << fn << "]\n"; r.ret=""; } return r; diff --git a/src/Client/PseuWoW.cpp b/src/Client/PseuWoW.cpp index d5cb96c..8b93901 100644 --- a/src/Client/PseuWoW.cpp +++ b/src/Client/PseuWoW.cpp @@ -108,37 +108,17 @@ bool PseuInstance::Init(void) { _scp->variables.Set("@version",_ver); _scp->variables.Set("@inworld","false"); - if(!_scp->BoolRunScript("_startup",NULL)) + if(!_scp->LoadScriptFromFile("./_startup.def")) + { + logerror("Error loading '_startup.def'"); + SetError(); + } + else if(!_scp->BoolRunScript("_startup",NULL)) { logerror("Error executing '_startup.def'"); SetError(); } - // load all .def files in scripts directory - log("Loading DefScripts from folder '%s'",_scpdir.c_str()); - std::deque fl = GetFileList(_scpdir); - for(uint32 i = 0; i < fl.size(); i++) - { - std::string fn = fl[i]; // do not lowercase filename because of linux case sensitivity - if(fn.length() <= 4) // must be at least "x.def" - continue; - std::string ext = stringToLower(fn.substr(fn.length()-4,4)); - std::string sn; - if(ext==".def") - sn = stringToLower(fn.substr(0,fn.length()-ext.length())); - if(sn.length()) - { - if(!_scp->ScriptExists(sn)) - { - std::string ffn = _scpdir + fn; - if(_scp->LoadScriptFromFile(ffn,sn)) - logdebug("-> Auto-loaded script [ %s ]",ffn.c_str()); - else - logerror("-> Can't auto-load script [ %s ]",ffn.c_str()); - } - } - } - // TODO: find a better loaction where to place this block! if(GetConf()->enablegui) { diff --git a/src/shared/Network/TcpSocket.cpp b/src/shared/Network/TcpSocket.cpp index 0b2940b..be0aeac 100644 --- a/src/shared/Network/TcpSocket.cpp +++ b/src/shared/Network/TcpSocket.cpp @@ -35,7 +35,7 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. #include #include #ifdef HAVE_OPENSSL -#include +#include "openssl/rand.h" #endif #include "SocketHandler.h" diff --git a/src/shared/Network/TcpSocket.h b/src/shared/Network/TcpSocket.h index 1da96bf..d652571 100644 --- a/src/shared/Network/TcpSocket.h +++ b/src/shared/Network/TcpSocket.h @@ -26,7 +26,7 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. #include "Socket.h" #include "CircularBuffer.h" #ifdef HAVE_OPENSSL -#include +#include "openssl/ssl.h" #ifdef _WIN32 // TODO: systray.exe?? #define RANDOM "systray.exe"