From 1e5b463434d266b8c25762409729b5e706a6d972 Mon Sep 17 00:00:00 2001 From: "False.Genesis" Date: Tue, 30 Oct 2007 18:14:19 +0000 Subject: [PATCH] * implemented in-runtime script modification. every script is now stored as list #DEFSCRIPT::SCRIPTS::scriptname * added little helper func "createdef" * fixed crash if "listscript" was passed a name of a nonexisting script * removed obsoelete file: TypeStorage.cpp, it was excluded from build since added. --- bin/scripts/__core_func.def | 18 ++++++- src/Client/DefScript/DefScript.cpp | 22 +++++++-- src/Client/DefScript/DefScript.h | 5 +- src/Client/DefScript/DefScriptFunctions.cpp | 13 ++++- src/Client/DefScript/TypeStorage.cpp | 54 --------------------- src/Client/DefScript/TypeStorage.h | 17 ++++++- src/Client/DefScriptInterface.cpp | 8 ++- src/PseuWoW.vcproj | 9 ---- 8 files changed, 71 insertions(+), 75 deletions(-) delete mode 100644 src/Client/DefScript/TypeStorage.cpp diff --git a/bin/scripts/__core_func.def b/bin/scripts/__core_func.def index f411506..2256c79 100644 --- a/bin/scripts/__core_func.def +++ b/bin/scripts/__core_func.def @@ -136,6 +136,20 @@ endloop ldelete mylist return ${outstr} - - +//-------------------------------------------------------- +#script=appenddef +//-------------------------------------------------------- +// purpose: append a line of code to a script +// args: @0: script name; @def: code +// be sure that the code you append is valid and working DefScript code! (no if/loop block mismatches, etc) +// if you need to use { or }, escape them: "appenddef,myscript log Myscript: finished, @def=$\{@def\}" +if ?{and,{?{strlen ${@def}}} ?{strlen ${@0}}} + if ?{not ?{ScriptExists ${@0}}} + createdef ${@0} + logdebug AppendDef: ${@0} didnt exist, created + endif + + lpushback,#DEFSCRIPT::SCRIPT::${@0} ${@def} + unset cmd +endif diff --git a/src/Client/DefScript/DefScript.cpp b/src/Client/DefScript/DefScript.cpp index a21fbaa..c01e91f 100644 --- a/src/Client/DefScript/DefScript.cpp +++ b/src/Client/DefScript/DefScript.cpp @@ -11,6 +11,7 @@ using namespace DefScriptTools; #define SN_ONLOAD "?onload?" +#define SCRIPT_NAMESPACE "DEFSCRIPT::SCRIPT::" enum DefScriptBlockType { @@ -74,6 +75,7 @@ void DefScriptPackage::Clear(void) { for(std::map::iterator i = Script.begin(); i != Script.end(); i++) { + lists.Unlink(SCRIPT_NAMESPACE + i->first); // remove name from the list storage delete i->second; // delete each script } @@ -89,6 +91,7 @@ void DefScriptPackage::_InitFunctions(void) AddFunc("shdn",&DefScriptPackage::func_shdn,false); AddFunc("loaddef",&DefScriptPackage::func_loaddef); AddFunc("reloaddef",&DefScriptPackage::func_reloaddef); + AddFunc("createdef",&DefScriptPackage::func_createdef); AddFunc("unloaddef",&DefScriptPackage::func_unloaddef); AddFunc("setscriptpermission",&DefScriptPackage::func_setscriptpermission); AddFunc("toint",&DefScriptPackage::func_toint); @@ -216,10 +219,12 @@ bool DefScriptPackage::ScriptExists(std::string name) void DefScriptPackage::DeleteScript(std::string sn) { + lists.Unlink(SCRIPT_NAMESPACE + sn); // remove name from the list storage if(ScriptExists(sn)) { - delete GetScript(sn); - Script.erase(sn); + + delete GetScript(sn); // delete the script itself + Script.erase(sn); // remove reference } } @@ -311,6 +316,10 @@ bool DefScriptPackage::LoadScriptFromFile(std::string fn){ DeleteScript(SN_ONLOAD); curScript=Script[sn]; } + else if(strcmp(line.c_str(),"tag")==0 || strcmp(line.c_str(),"mark")==0) + { + curScript->AddLine("#" + line); + } //... continue; // line was an option, not script content @@ -519,11 +528,14 @@ DefReturnResult DefScriptPackage::RunScript(std::string name, CmdSet *pSet,std:: pSet->myname=name; std::deque Blocks; + CmdSet mySet; + std::string line; for(unsigned int i=0;iGetLines();i++) { - CmdSet mySet; - std::string line=sc->GetLine(i); + line=sc->GetLine(i); + if(line.empty() || line[0] == '#') // skip markers and preload statements if not removed before + continue; if(line=="else") { if(!Blocks.size()) @@ -597,6 +609,7 @@ DefReturnResult DefScriptPackage::RunScript(std::string name, CmdSet *pSet,std:: //_DEFSC_DEBUG(printf("DefScript before: \"%s\"\n",line.c_str())); DefXChgResult final=ReplaceVars(line,pSet,0,true); //_DEFSC_DEBUG(printf("DefScript parsed: \"%s\"\n",final.str.c_str())); + mySet.Clear(); SplitLine(mySet,final.str); if(mySet.cmd=="if") { @@ -1052,6 +1065,7 @@ void DefScriptPackage::_UpdateOrCreateScriptByName(std::string sn) DefScript *newscript = new DefScript(this); newscript->SetName(sn); // necessary that the script knows its own name Script[sn] = newscript; + lists.Assign(SCRIPT_NAMESPACE + sn, &(newscript->Line)); } std::string DefScriptPackage::SecureString(std::string s) diff --git a/src/Client/DefScript/DefScript.h b/src/Client/DefScript/DefScript.h index 750efea..df9c8b6 100644 --- a/src/Client/DefScript/DefScript.h +++ b/src/Client/DefScript/DefScript.h @@ -80,6 +80,7 @@ typedef std::deque DefList; typedef std::map DefListMap; class DefScript { + friend class DefScriptPackage; public: DefScript(DefScriptPackage *p); ~DefScript(); @@ -97,7 +98,7 @@ public: private: - std::deque Line; + DefList Line; unsigned int lines; std::string scriptname; unsigned char permission; @@ -108,6 +109,7 @@ private: class DefScriptPackage { + friend class DefScript; public: DefScriptPackage(); ~DefScriptPackage(); @@ -180,6 +182,7 @@ private: DefReturnResult func_unset(CmdSet&); DefReturnResult func_loaddef(CmdSet&); DefReturnResult func_reloaddef(CmdSet&); + DefReturnResult func_createdef(CmdSet&); DefReturnResult func_unloaddef(CmdSet&); DefReturnResult func_out(CmdSet&); DefReturnResult func_eof(CmdSet&); diff --git a/src/Client/DefScript/DefScriptFunctions.cpp b/src/Client/DefScript/DefScriptFunctions.cpp index f1e10ce..28c038c 100644 --- a/src/Client/DefScript/DefScriptFunctions.cpp +++ b/src/Client/DefScript/DefScriptFunctions.cpp @@ -73,6 +73,17 @@ DefReturnResult DefScriptPackage::func_unloaddef(CmdSet& Set) return true; } +DefReturnResult DefScriptPackage::func_createdef(CmdSet& Set) +{ + std::string sn = stringToLower(Set.defaultarg); + if(!ScriptExists(sn)) + { + _UpdateOrCreateScriptByName(sn); + return true; + } + return false; +} + DefReturnResult DefScriptPackage::func_unset(CmdSet& Set){ DefReturnResult r; r.ret=Set.defaultarg; @@ -511,7 +522,7 @@ DefReturnResult DefScriptPackage::func_strfind(CmdSet& Set) DefReturnResult DefScriptPackage::func_scriptexists(CmdSet& Set) { - return ScriptExists(Set.defaultarg); + return ScriptExists(stringToLower(Set.defaultarg)); } DefReturnResult DefScriptPackage::func_funcexists(CmdSet& Set) diff --git a/src/Client/DefScript/TypeStorage.cpp b/src/Client/DefScript/TypeStorage.cpp deleted file mode 100644 index fe9cdae..0000000 --- a/src/Client/DefScript/TypeStorage.cpp +++ /dev/null @@ -1,54 +0,0 @@ -#include "TypeStorage.h" - -template bool TypeStorage::Exists(std::string s) -{ - for(DefListMap::iterator it = _storage.begin(); it != _storage.end(); it++) - { - if(it->first == s) - return true; - } - return false; -} - -template T *TypeStorage::_Create(std::string s) -{ - T *l = new DefList(); - _storage[s] = l; - return l; -} - -template void TypeStorage::Delete(std::string s) -{ - for(DefListMap::iterator it = _storage.begin(); it != _storage.end(); it++) - { - if(it->first == s) - { - delete it->second; - _storage.erase(it); - return; - } - } -} - -template T *TypeStorage::GetNoCreate(std::string s) -{ - for(std::map::iterator it = _storage.begin(); it != _storage.end(); it++) - if(it->first == s) - return it->second; - return NULL; -} - -template T *TypeStorage::Get(std::string s) -{ - DefList *l = GetNoCreate(s); - return l ? l : _Create(s); -} - -template TypeStorage::~TypeStorage() -{ - for(DefListMap::iterator it = _storage.begin(); it != _storage.end();) - { - delete it->second; - _storage.erase(it++); - } -} diff --git a/src/Client/DefScript/TypeStorage.h b/src/Client/DefScript/TypeStorage.h index 40d1c5e..8c93575 100644 --- a/src/Client/DefScript/TypeStorage.h +++ b/src/Client/DefScript/TypeStorage.h @@ -13,13 +13,14 @@ public: T *Get(std::string); T *GetNoCreate(std::string); void Assign(std::string,T*); + void Unlink(std::string); private: T *_Create(std::string); std::map _storage; }; - +// check whether an object with this name is already present template bool TypeStorage::Exists(std::string s) { for(std::map::iterator it = _storage.begin(); it != _storage.end(); it++) @@ -30,6 +31,7 @@ template bool TypeStorage::Exists(std::string s) return false; } +// helper to create and assign a new object template T *TypeStorage::_Create(std::string s) { T *elem = new T(); @@ -37,6 +39,7 @@ template T *TypeStorage::_Create(std::string s) return elem; } +// delete object with that name, if present template void TypeStorage::Delete(std::string s) { for(std::map::iterator it = _storage.begin(); it != _storage.end(); it++) @@ -50,6 +53,7 @@ template void TypeStorage::Delete(std::string s) } } +// return the the object with that name. return NULL if not found template T *TypeStorage::GetNoCreate(std::string s) { for(std::map::iterator it = _storage.begin(); it != _storage.end(); it++) @@ -58,12 +62,14 @@ template T *TypeStorage::GetNoCreate(std::string s) return NULL; } +// return the the object with that name. create and return new object if not found template T *TypeStorage::Get(std::string s) { T *elem = GetNoCreate(s); return elem ? elem : _Create(s); } +// when destroying the TypeStorage, delete all stored objects template TypeStorage::~TypeStorage() { for(std::map::iterator it = _storage.begin(); it != _storage.end();) @@ -73,12 +79,19 @@ template TypeStorage::~TypeStorage() } } +// stores an already existing object's pointer under a specific name; deletes and overwrites old of present template void TypeStorage::Assign(std::string s,T *elem) { if(Exists(s)) - Delete(s) + Delete(s); _storage[s] = elem; } +// removes the pointer from the storage without deleting it +template void TypeStorage::Unlink(std::string s) +{ + _storage.erase(s); +} + #endif diff --git a/src/Client/DefScriptInterface.cpp b/src/Client/DefScriptInterface.cpp index 418c0cd..967ddad 100644 --- a/src/Client/DefScriptInterface.cpp +++ b/src/Client/DefScriptInterface.cpp @@ -783,9 +783,13 @@ DefReturnResult DefScriptPackage::SCGetFileList(CmdSet& Set) DefReturnResult DefScriptPackage::SCPrintScript(CmdSet &Set) { DefScript *sc = GetScript(DefScriptTools::stringToLower(Set.defaultarg)); - for(uint32 i = 0; i < sc->GetLines(); i++) + if(sc) { - logcustom(0,GREEN,sc->GetLine(i).c_str()); + logcustom(0,GREEN,"== DefScript \"%s\", %u lines: ==",sc->GetName().c_str(),sc->GetLines()); + for(uint32 i = 0; i < sc->GetLines(); i++) + { + logcustom(0,GREEN,sc->GetLine(i).c_str()); + } } return ""; } diff --git a/src/PseuWoW.vcproj b/src/PseuWoW.vcproj index 239a57f..d18df29 100644 --- a/src/PseuWoW.vcproj +++ b/src/PseuWoW.vcproj @@ -242,15 +242,6 @@ - - - - -