* 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:
False.Genesis 2007-10-30 18:14:19 +00:00
parent 97bf1c4793
commit 1e5b463434
8 changed files with 71 additions and 75 deletions

View File

@ -136,6 +136,20 @@ endloop
ldelete mylist ldelete mylist
return ${outstr} 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

View File

@ -11,6 +11,7 @@
using namespace DefScriptTools; using namespace DefScriptTools;
#define SN_ONLOAD "?onload?" #define SN_ONLOAD "?onload?"
#define SCRIPT_NAMESPACE "DEFSCRIPT::SCRIPT::"
enum DefScriptBlockType enum DefScriptBlockType
{ {
@ -74,6 +75,7 @@ void DefScriptPackage::Clear(void)
{ {
for(std::map<std::string,DefScript*>::iterator i = Script.begin(); i != Script.end(); i++) 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 delete i->second; // delete each script
} }
@ -89,6 +91,7 @@ void DefScriptPackage::_InitFunctions(void)
AddFunc("shdn",&DefScriptPackage::func_shdn,false); AddFunc("shdn",&DefScriptPackage::func_shdn,false);
AddFunc("loaddef",&DefScriptPackage::func_loaddef); AddFunc("loaddef",&DefScriptPackage::func_loaddef);
AddFunc("reloaddef",&DefScriptPackage::func_reloaddef); AddFunc("reloaddef",&DefScriptPackage::func_reloaddef);
AddFunc("createdef",&DefScriptPackage::func_createdef);
AddFunc("unloaddef",&DefScriptPackage::func_unloaddef); AddFunc("unloaddef",&DefScriptPackage::func_unloaddef);
AddFunc("setscriptpermission",&DefScriptPackage::func_setscriptpermission); AddFunc("setscriptpermission",&DefScriptPackage::func_setscriptpermission);
AddFunc("toint",&DefScriptPackage::func_toint); AddFunc("toint",&DefScriptPackage::func_toint);
@ -216,10 +219,12 @@ bool DefScriptPackage::ScriptExists(std::string name)
void DefScriptPackage::DeleteScript(std::string sn) void DefScriptPackage::DeleteScript(std::string sn)
{ {
lists.Unlink(SCRIPT_NAMESPACE + sn); // remove name from the list storage
if(ScriptExists(sn)) 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); DeleteScript(SN_ONLOAD);
curScript=Script[sn]; 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 continue; // line was an option, not script content
@ -519,11 +528,14 @@ DefReturnResult DefScriptPackage::RunScript(std::string name, CmdSet *pSet,std::
pSet->myname=name; pSet->myname=name;
std::deque<Def_Block> Blocks; std::deque<Def_Block> Blocks;
CmdSet mySet;
std::string line;
for(unsigned int i=0;i<sc->GetLines();i++) for(unsigned int i=0;i<sc->GetLines();i++)
{ {
CmdSet mySet; line=sc->GetLine(i);
std::string line=sc->GetLine(i); if(line.empty() || line[0] == '#') // skip markers and preload statements if not removed before
continue;
if(line=="else") if(line=="else")
{ {
if(!Blocks.size()) 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())); //_DEFSC_DEBUG(printf("DefScript before: \"%s\"\n",line.c_str()));
DefXChgResult final=ReplaceVars(line,pSet,0,true); DefXChgResult final=ReplaceVars(line,pSet,0,true);
//_DEFSC_DEBUG(printf("DefScript parsed: \"%s\"\n",final.str.c_str())); //_DEFSC_DEBUG(printf("DefScript parsed: \"%s\"\n",final.str.c_str()));
mySet.Clear();
SplitLine(mySet,final.str); SplitLine(mySet,final.str);
if(mySet.cmd=="if") if(mySet.cmd=="if")
{ {
@ -1052,6 +1065,7 @@ void DefScriptPackage::_UpdateOrCreateScriptByName(std::string sn)
DefScript *newscript = new DefScript(this); DefScript *newscript = new DefScript(this);
newscript->SetName(sn); // necessary that the script knows its own name newscript->SetName(sn); // necessary that the script knows its own name
Script[sn] = newscript; Script[sn] = newscript;
lists.Assign(SCRIPT_NAMESPACE + sn, &(newscript->Line));
} }
std::string DefScriptPackage::SecureString(std::string s) std::string DefScriptPackage::SecureString(std::string s)

View File

@ -80,6 +80,7 @@ typedef std::deque<std::string> DefList;
typedef std::map<std::string,DefList*> DefListMap; typedef std::map<std::string,DefList*> DefListMap;
class DefScript { class DefScript {
friend class DefScriptPackage;
public: public:
DefScript(DefScriptPackage *p); DefScript(DefScriptPackage *p);
~DefScript(); ~DefScript();
@ -97,7 +98,7 @@ public:
private: private:
std::deque<std::string> Line; DefList Line;
unsigned int lines; unsigned int lines;
std::string scriptname; std::string scriptname;
unsigned char permission; unsigned char permission;
@ -108,6 +109,7 @@ private:
class DefScriptPackage { class DefScriptPackage {
friend class DefScript;
public: public:
DefScriptPackage(); DefScriptPackage();
~DefScriptPackage(); ~DefScriptPackage();
@ -180,6 +182,7 @@ private:
DefReturnResult func_unset(CmdSet&); DefReturnResult func_unset(CmdSet&);
DefReturnResult func_loaddef(CmdSet&); DefReturnResult func_loaddef(CmdSet&);
DefReturnResult func_reloaddef(CmdSet&); DefReturnResult func_reloaddef(CmdSet&);
DefReturnResult func_createdef(CmdSet&);
DefReturnResult func_unloaddef(CmdSet&); DefReturnResult func_unloaddef(CmdSet&);
DefReturnResult func_out(CmdSet&); DefReturnResult func_out(CmdSet&);
DefReturnResult func_eof(CmdSet&); DefReturnResult func_eof(CmdSet&);

View File

@ -73,6 +73,17 @@ DefReturnResult DefScriptPackage::func_unloaddef(CmdSet& Set)
return true; 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 DefScriptPackage::func_unset(CmdSet& Set){
DefReturnResult r; DefReturnResult r;
r.ret=Set.defaultarg; r.ret=Set.defaultarg;
@ -511,7 +522,7 @@ DefReturnResult DefScriptPackage::func_strfind(CmdSet& Set)
DefReturnResult DefScriptPackage::func_scriptexists(CmdSet& Set) DefReturnResult DefScriptPackage::func_scriptexists(CmdSet& Set)
{ {
return ScriptExists(Set.defaultarg); return ScriptExists(stringToLower(Set.defaultarg));
} }
DefReturnResult DefScriptPackage::func_funcexists(CmdSet& Set) DefReturnResult DefScriptPackage::func_funcexists(CmdSet& Set)

View File

@ -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++);
}
}

View File

@ -13,13 +13,14 @@ public:
T *Get(std::string); T *Get(std::string);
T *GetNoCreate(std::string); T *GetNoCreate(std::string);
void Assign(std::string,T*); void Assign(std::string,T*);
void Unlink(std::string);
private: private:
T *_Create(std::string); T *_Create(std::string);
std::map<std::string,T*> _storage; 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) template<class T> bool TypeStorage<T>::Exists(std::string s)
{ {
for(std::map<std::string,T*>::iterator it = _storage.begin(); it != _storage.end(); it++) 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; return false;
} }
// helper to create and assign a new object
template<class T> T *TypeStorage<T>::_Create(std::string s) template<class T> T *TypeStorage<T>::_Create(std::string s)
{ {
T *elem = new T(); T *elem = new T();
@ -37,6 +39,7 @@ template<class T> T *TypeStorage<T>::_Create(std::string s)
return elem; return elem;
} }
// delete object with that name, if present
template<class T> void TypeStorage<T>::Delete(std::string s) template<class T> void TypeStorage<T>::Delete(std::string s)
{ {
for(std::map<std::string,T*>::iterator it = _storage.begin(); it != _storage.end(); it++) 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) template <class T> T *TypeStorage<T>::GetNoCreate(std::string s)
{ {
for(std::map<std::string,T*>::iterator it = _storage.begin(); it != _storage.end(); it++) 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 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) template<class T> T *TypeStorage<T>::Get(std::string s)
{ {
T *elem = GetNoCreate(s); T *elem = GetNoCreate(s);
return elem ? elem : _Create(s); return elem ? elem : _Create(s);
} }
// when destroying the TypeStorage, delete all stored objects
template<class T> TypeStorage<T>::~TypeStorage() template<class T> TypeStorage<T>::~TypeStorage()
{ {
for(std::map<std::string,T*>::iterator it = _storage.begin(); it != _storage.end();) 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) template<class T> void TypeStorage<T>::Assign(std::string s,T *elem)
{ {
if(Exists(s)) if(Exists(s))
Delete(s) Delete(s);
_storage[s] = elem; _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 #endif

View File

@ -783,9 +783,13 @@ DefReturnResult DefScriptPackage::SCGetFileList(CmdSet& Set)
DefReturnResult DefScriptPackage::SCPrintScript(CmdSet &Set) DefReturnResult DefScriptPackage::SCPrintScript(CmdSet &Set)
{ {
DefScript *sc = GetScript(DefScriptTools::stringToLower(Set.defaultarg)); 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 ""; return "";
} }

View File

@ -242,15 +242,6 @@
<File <File
RelativePath=".\Client\DefScript\DynamicEvent.h"> RelativePath=".\Client\DefScript\DynamicEvent.h">
</File> </File>
<File
RelativePath=".\Client\DefScript\TypeStorage.cpp">
<FileConfiguration
Name="Debug|Win32"
ExcludedFromBuild="TRUE">
<Tool
Name="VCCLCompilerTool"/>
</FileConfiguration>
</File>
<File <File
RelativePath=".\Client\DefScript\TypeStorage.h"> RelativePath=".\Client\DefScript\TypeStorage.h">
</File> </File>