* 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.
This commit is contained in:
parent
97bf1c4793
commit
1e5b463434
@ -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
|
||||
|
||||
|
||||
@ -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<std::string,DefScript*>::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<Def_Block> Blocks;
|
||||
CmdSet mySet;
|
||||
std::string line;
|
||||
|
||||
for(unsigned int i=0;i<sc->GetLines();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)
|
||||
|
||||
@ -80,6 +80,7 @@ typedef std::deque<std::string> DefList;
|
||||
typedef std::map<std::string,DefList*> DefListMap;
|
||||
|
||||
class DefScript {
|
||||
friend class DefScriptPackage;
|
||||
public:
|
||||
DefScript(DefScriptPackage *p);
|
||||
~DefScript();
|
||||
@ -97,7 +98,7 @@ public:
|
||||
|
||||
|
||||
private:
|
||||
std::deque<std::string> 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&);
|
||||
|
||||
@ -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)
|
||||
|
||||
@ -1,54 +0,0 @@
|
||||
#include "TypeStorage.h"
|
||||
|
||||
template<class T> bool TypeStorage<T>::Exists(std::string s)
|
||||
{
|
||||
for(DefListMap::iterator it = _storage.begin(); it != _storage.end(); it++)
|
||||
{
|
||||
if(it->first == s)
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
template<class T> T *TypeStorage<T>::_Create(std::string s)
|
||||
{
|
||||
T *l = new DefList();
|
||||
_storage[s] = l;
|
||||
return l;
|
||||
}
|
||||
|
||||
template<class T> void TypeStorage<T>::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 <class T> T *TypeStorage<T>::GetNoCreate(std::string s)
|
||||
{
|
||||
for(std::map<std::string,T*>::iterator it = _storage.begin(); it != _storage.end(); it++)
|
||||
if(it->first == s)
|
||||
return it->second;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
template<class T> T *TypeStorage<T>::Get(std::string s)
|
||||
{
|
||||
DefList *l = GetNoCreate(s);
|
||||
return l ? l : _Create(s);
|
||||
}
|
||||
|
||||
template<class T> TypeStorage<T>::~TypeStorage()
|
||||
{
|
||||
for(DefListMap::iterator it = _storage.begin(); it != _storage.end();)
|
||||
{
|
||||
delete it->second;
|
||||
_storage.erase(it++);
|
||||
}
|
||||
}
|
||||
@ -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<std::string,T*> _storage;
|
||||
};
|
||||
|
||||
|
||||
// check whether an object with this name is already present
|
||||
template<class T> bool TypeStorage<T>::Exists(std::string s)
|
||||
{
|
||||
for(std::map<std::string,T*>::iterator it = _storage.begin(); it != _storage.end(); it++)
|
||||
@ -30,6 +31,7 @@ template<class T> bool TypeStorage<T>::Exists(std::string s)
|
||||
return false;
|
||||
}
|
||||
|
||||
// helper to create and assign a new object
|
||||
template<class T> T *TypeStorage<T>::_Create(std::string s)
|
||||
{
|
||||
T *elem = new T();
|
||||
@ -37,6 +39,7 @@ template<class T> T *TypeStorage<T>::_Create(std::string s)
|
||||
return elem;
|
||||
}
|
||||
|
||||
// delete object with that name, if present
|
||||
template<class T> void TypeStorage<T>::Delete(std::string s)
|
||||
{
|
||||
for(std::map<std::string,T*>::iterator it = _storage.begin(); it != _storage.end(); it++)
|
||||
@ -50,6 +53,7 @@ template<class T> void TypeStorage<T>::Delete(std::string s)
|
||||
}
|
||||
}
|
||||
|
||||
// return the the object with that name. return NULL if not found
|
||||
template <class T> T *TypeStorage<T>::GetNoCreate(std::string s)
|
||||
{
|
||||
for(std::map<std::string,T*>::iterator it = _storage.begin(); it != _storage.end(); it++)
|
||||
@ -58,12 +62,14 @@ template <class T> T *TypeStorage<T>::GetNoCreate(std::string s)
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// return the the object with that name. create and return new object if not found
|
||||
template<class T> T *TypeStorage<T>::Get(std::string s)
|
||||
{
|
||||
T *elem = GetNoCreate(s);
|
||||
return elem ? elem : _Create(s);
|
||||
}
|
||||
|
||||
// when destroying the TypeStorage, delete all stored objects
|
||||
template<class T> TypeStorage<T>::~TypeStorage()
|
||||
{
|
||||
for(std::map<std::string,T*>::iterator it = _storage.begin(); it != _storage.end();)
|
||||
@ -73,12 +79,19 @@ template<class T> TypeStorage<T>::~TypeStorage()
|
||||
}
|
||||
}
|
||||
|
||||
// stores an already existing object's pointer under a specific name; deletes and overwrites old of present
|
||||
template<class T> void TypeStorage<T>::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<class T> void TypeStorage<T>::Unlink(std::string s)
|
||||
{
|
||||
_storage.erase(s);
|
||||
}
|
||||
|
||||
|
||||
#endif
|
||||
|
||||
@ -783,10 +783,14 @@ DefReturnResult DefScriptPackage::SCGetFileList(CmdSet& Set)
|
||||
DefReturnResult DefScriptPackage::SCPrintScript(CmdSet &Set)
|
||||
{
|
||||
DefScript *sc = GetScript(DefScriptTools::stringToLower(Set.defaultarg));
|
||||
if(sc)
|
||||
{
|
||||
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 "";
|
||||
}
|
||||
|
||||
|
||||
@ -242,15 +242,6 @@
|
||||
<File
|
||||
RelativePath=".\Client\DefScript\DynamicEvent.h">
|
||||
</File>
|
||||
<File
|
||||
RelativePath=".\Client\DefScript\TypeStorage.cpp">
|
||||
<FileConfiguration
|
||||
Name="Debug|Win32"
|
||||
ExcludedFromBuild="TRUE">
|
||||
<Tool
|
||||
Name="VCCLCompilerTool"/>
|
||||
</FileConfiguration>
|
||||
</File>
|
||||
<File
|
||||
RelativePath=".\Client\DefScript\TypeStorage.h">
|
||||
</File>
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user