* fixed if..else..endif

* small .def update
* misc, minor changes to some functions
* added a few more DefScript functions (and, or, xor, GetObjectType, ObjectKnown)
This commit is contained in:
False.Genesis 2007-03-16 16:14:13 +00:00
parent 6fef92a30c
commit 7de82dd3a9
8 changed files with 118 additions and 38 deletions

View File

@ -4,6 +4,7 @@
// args: none // args: none
// usage: - load the script to register the uptime counter // usage: - load the script to register the uptime counter
// - call the script to say the uptime formatted in hours, mins & secs // - call the script to say the uptime formatted in hours, mins & secs
// returns: uptime formatted as 0h 0m 0s
#onload #onload
set,#uptime 0 set,#uptime 0
@ -15,15 +16,23 @@ set,s ${#uptime}
set,h ${s} set,h ${s}
div,h 3600 div,h 3600
toint,h ${h}
mod,s 3600 mod,s 3600
set,m ${s} set,m ${s}
div,m 60 div,m 60
toint,m ${m}
mod,s 60 mod,s 60
toint,s ${s}
set,str ${h}h ${m}m ${s}s
out UPTIME: ${#uptime} secs = ${str}
out UPTIME: ${#uptime} secs = ${h}h ${m}m ${s}s if ${@inworld}
say UPTIME: ${h} hours, ${m} minutes, ${s} seconds say UPTIME: ${h} hours, ${m} minutes, ${s} seconds
endif
unset h unset h
unset m unset m
unset s unset s
return ${str}

View File

@ -10,6 +10,12 @@
using namespace DefScriptTools; using namespace DefScriptTools;
enum DefScriptBlockType
{
BLOCK_IF,
BLOCK_LOOP
};
// --- SECTION FOR SCRIPT PACKAGES --- // --- SECTION FOR SCRIPT PACKAGES ---
DefScriptPackage::DefScriptPackage() DefScriptPackage::DefScriptPackage()
{ {
@ -72,6 +78,9 @@ void DefScriptPackage::_InitFunctions(void)
AddFunc("smaller",&DefScriptPackage::func_smaller); AddFunc("smaller",&DefScriptPackage::func_smaller);
AddFunc("strlen",&DefScriptPackage::func_strlen); AddFunc("strlen",&DefScriptPackage::func_strlen);
AddFunc("tohex",&DefScriptPackage::func_tohex); AddFunc("tohex",&DefScriptPackage::func_tohex);
AddFunc("and",&DefScriptPackage::func_and);
AddFunc("or",&DefScriptPackage::func_or);
AddFunc("xor",&DefScriptPackage::func_xor);
} }
void DefScriptPackage::AddFunc(std::string n,DefReturnResult (DefScriptPackage::*f)(CmdSet& Set)) void DefScriptPackage::AddFunc(std::string n,DefReturnResult (DefScriptPackage::*f)(CmdSet& Set))
@ -339,6 +348,7 @@ DefReturnResult DefScriptPackage::RunScript(std::string name, CmdSet *pSet)
{ {
if(!Blocks.size()) if(!Blocks.size())
{ {
printf("DEBUG: else-block without any block?! [%s:%u]\n",name.c_str(),i);
r.ok=false; r.ok=false;
break; break;
} }
@ -346,26 +356,30 @@ DefReturnResult DefScriptPackage::RunScript(std::string name, CmdSet *pSet)
if(b.type==BLOCK_IF && b.istrue) if(b.type==BLOCK_IF && b.istrue)
{ {
for(i=b.startline;sc->GetLine(i)!="endif";i++); // skip lines until "endif" for(i=b.startline;sc->GetLine(i)!="endif";i++); // skip lines until "endif"
Blocks.pop_back(); i--; // next line read will be "endif"
}
if(b.type==BLOCK_IF && !b.istrue)
{
printf("DEBUG: else does not have an if-block that was false before");
} }
continue; continue;
} }
else if(line=="endif") else if(line=="endif")
{ {
if(!Blocks.size())
{
printf("DEBUG: endif without any block [%s:%u]\n",name.c_str(),i);
r.ok=false;
break;
}
if(Blocks.back().type!=BLOCK_IF) if(Blocks.back().type!=BLOCK_IF)
{ {
printf("DEBUG: endif: closed block is not an if block! [%s:%u]\n",name.c_str(),i); printf("DEBUG: endif: closed block is not an if block! [%s:%u]\n",name.c_str(),i);
r.ok=false;
break; break;
} }
Blocks.pop_back(); Blocks.pop_back();
continue; continue;
} }
_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: \"%s\"\n",final.str.c_str())); _DEFSC_DEBUG(printf("DefScript parsed: \"%s\"\n",final.str.c_str()));
SplitLine(mySet,final.str); SplitLine(mySet,final.str);
if(mySet.cmd=="if") if(mySet.cmd=="if")
{ {
@ -376,9 +390,9 @@ DefReturnResult DefScriptPackage::RunScript(std::string name, CmdSet *pSet)
Blocks.push_back(b); Blocks.push_back(b);
if(!b.istrue) if(!b.istrue)
{ {
for(i=b.startline;sc->GetLine(i)!="else" && sc->GetLine(i)!="endif";i++); // skip lines until "else" for(i=b.startline;sc->GetLine(i)!="else" && sc->GetLine(i)!="endif";i++);
if(sc->GetLine(i)!="endif") i--; // next line read will be either "else" or "endif", decide then what to do
Blocks.pop_back();
} }
continue; // and read line after "else" continue; // and read line after "else"
} }
@ -669,7 +683,6 @@ DefXChgResult DefScriptPackage::ReplaceVars(std::string str, CmdSet *pSet, unsig
res=RunSingleLine(str); res=RunSingleLine(str);
str=res.ret; // returns empty string on invalid function!! str=res.ret; // returns empty string on invalid function!!
xchg.result.ok=res.ok; xchg.result.ok=res.ok;
if(res.ok)
xchg.changed=true; xchg.changed=true;
//xchg.result.err += res.err; //xchg.result.err += res.err;
} }

View File

@ -12,12 +12,6 @@
class DefScriptPackage; class DefScriptPackage;
class DefScript; class DefScript;
enum DefScriptBlockType
{
BLOCK_IF,
BLOCK_LOOP
};
// general struct for if..else..endif / loop..endloop blocks // general struct for if..else..endif / loop..endloop blocks
struct Def_Block struct Def_Block
{ {
@ -175,6 +169,9 @@ private:
DefReturnResult func_smaller(CmdSet&); DefReturnResult func_smaller(CmdSet&);
DefReturnResult func_strlen(CmdSet&); DefReturnResult func_strlen(CmdSet&);
DefReturnResult func_tohex(CmdSet&); DefReturnResult func_tohex(CmdSet&);
DefReturnResult func_and(CmdSet&);
DefReturnResult func_or(CmdSet&);
DefReturnResult func_xor(CmdSet&);
// setup own function declarations here // setup own function declarations here
# include "DefScriptInterfaceInclude.h" # include "DefScriptInterfaceInclude.h"

View File

@ -241,8 +241,8 @@ DefReturnResult DefScriptPackage::func_mod(CmdSet& Set)
} }
std::string vname=_NormalizeVarName(Set.arg[0], Set.myname); std::string vname=_NormalizeVarName(Set.arg[0], Set.myname);
uint64 a=(uint64)toNumber(variables.Get(vname)); uint64 a=toUint64(variables.Get(vname));
uint64 b=(uint64)toNumber(Set.defaultarg); uint64 b=toUint64(Set.defaultarg);
if(b==0) if(b==0)
a=0; a=0;
else else
@ -350,12 +350,12 @@ DefReturnResult DefScriptPackage::func_equal(CmdSet& Set)
DefReturnResult DefScriptPackage::func_smaller(CmdSet& Set) DefReturnResult DefScriptPackage::func_smaller(CmdSet& Set)
{ {
return toNumber(Set.defaultarg) < toNumber(Set.arg[0]); return toNumber(Set.arg[0]) < toNumber(Set.defaultarg);
} }
DefReturnResult DefScriptPackage::func_bigger(CmdSet& Set) DefReturnResult DefScriptPackage::func_bigger(CmdSet& Set)
{ {
return toNumber(Set.defaultarg) > toNumber(Set.arg[0]); return toNumber(Set.arg[0]) > toNumber(Set.defaultarg);
} }
DefReturnResult DefScriptPackage::func_not(CmdSet& Set) DefReturnResult DefScriptPackage::func_not(CmdSet& Set)
@ -403,3 +403,19 @@ DefReturnResult DefScriptPackage::func_abs(CmdSet& Set)
r.ret=toString(fabs(toNumber(Set.defaultarg))); r.ret=toString(fabs(toNumber(Set.defaultarg)));
return r; return r;
} }
DefReturnResult DefScriptPackage::func_and(CmdSet& Set)
{
return isTrue(Set.defaultarg) && isTrue(Set.arg[0]);
}
DefReturnResult DefScriptPackage::func_or(CmdSet& Set)
{
return isTrue(Set.defaultarg) || isTrue(Set.arg[0]);
}
DefReturnResult DefScriptPackage::func_xor(CmdSet& Set)
{
return (isTrue(Set.defaultarg) && isTrue(Set.arg[0])) || ( (!isTrue(Set.defaultarg)) && (!isTrue(Set.arg[0]) ) );
}

View File

@ -39,6 +39,8 @@ void DefScriptPackage::_InitDefScriptInterface(void)
AddFunc("getname",&DefScriptPackage::SCGetName); AddFunc("getname",&DefScriptPackage::SCGetName);
AddFunc("getentry",&DefScriptPackage::SCGetName); AddFunc("getentry",&DefScriptPackage::SCGetName);
AddFunc("getitemprotovalue",&DefScriptPackage::SCGetName); AddFunc("getitemprotovalue",&DefScriptPackage::SCGetName);
AddFunc("getobjecttype",&DefScriptPackage::SCGetObjectType);
AddFunc("objectknown",&DefScriptPackage::SCObjectKnown);
} }
DefReturnResult DefScriptPackage::SCshdn(CmdSet& Set) DefReturnResult DefScriptPackage::SCshdn(CmdSet& Set)
@ -215,8 +217,7 @@ DefReturnResult DefScriptPackage::SCcastspell(CmdSet& Set)
if (spellId <= 0) if (spellId <= 0)
{ {
logerror("Invalid Script call: SCcastspell: SpellId not valid"); return false;
DEF_RETURN_ERROR;
} }
((PseuInstance*)parentMethod)->GetWSession()->SendCastSpell(spellId); ((PseuInstance*)parentMethod)->GetWSession()->SendCastSpell(spellId);
@ -240,6 +241,7 @@ DefReturnResult DefScriptPackage::SCqueryitem(CmdSet& Set){
DefReturnResult DefScriptPackage::SCtarget(CmdSet& Set) DefReturnResult DefScriptPackage::SCtarget(CmdSet& Set)
{ {
// TODO: special targets: _self _pet _nearest ... // TODO: special targets: _self _pet _nearest ...
DefReturnResult r;
if(!(((PseuInstance*)parentMethod)->GetWSession() && ((PseuInstance*)parentMethod)->GetWSession()->IsValid())) if(!(((PseuInstance*)parentMethod)->GetWSession() && ((PseuInstance*)parentMethod)->GetWSession()->IsValid()))
{ {
@ -257,11 +259,17 @@ DefReturnResult DefScriptPackage::SCtarget(CmdSet& Set)
uint64 guid = (((PseuInstance*)parentMethod)->GetWSession()->plrNameCache.GetGuid(Set.defaultarg)); uint64 guid = (((PseuInstance*)parentMethod)->GetWSession()->plrNameCache.GetGuid(Set.defaultarg));
if( guid && ((PseuInstance*)parentMethod)->GetWSession()->objmgr.GetObj(guid) ) // object must be near if( guid && ((PseuInstance*)parentMethod)->GetWSession()->objmgr.GetObj(guid) ) // object must be near
((PseuInstance*)parentMethod)->GetWSession()->SendSetSelection(guid); {
((PseuInstance*)parentMethod)->GetWSession()->SendSetSelection(guid); // will also set the target for myCharacter
r.ret=toString(guid);
}
else else
{
logdetail("Target '%s' not found!",Set.defaultarg.c_str()); logdetail("Target '%s' not found!",Set.defaultarg.c_str());
return false;
}
return true; return r;
} }
DefReturnResult DefScriptPackage::SCloadscp(CmdSet& Set) DefReturnResult DefScriptPackage::SCloadscp(CmdSet& Set)
@ -416,6 +424,40 @@ DefReturnResult DefScriptPackage::SCGetEntry(CmdSet& Set)
return r; return r;
} }
DefReturnResult DefScriptPackage::SCGetObjectType(CmdSet& Set)
{
if(!(((PseuInstance*)parentMethod)->GetWSession() && ((PseuInstance*)parentMethod)->GetWSession()->IsValid()))
{
logerror("Invalid Script call: SCGetObjectType: WorldSession not valid");
DEF_RETURN_ERROR;
}
DefReturnResult r;
uint64 guid=DefScriptTools::toNumber(Set.defaultarg);
r.ret="0";
Object *o=((PseuInstance*)parentMethod)->GetWSession()->objmgr.GetObj(guid);
if(o)
{
r.ret=DefScriptTools::toString((uint64)o->GetTypeId());
}
else
{
logerror("SCGetObjectType: Object "I64FMT" not known",guid);
}
return r;
}
DefReturnResult DefScriptPackage::SCObjectKnown(CmdSet& Set)
{
if(!(((PseuInstance*)parentMethod)->GetWSession() && ((PseuInstance*)parentMethod)->GetWSession()->IsValid()))
{
logerror("Invalid Script call: SCObjectIsKnown: WorldSession not valid");
DEF_RETURN_ERROR;
}
uint64 guid=DefScriptTools::toNumber(Set.defaultarg);
Object *o=((PseuInstance*)parentMethod)->GetWSession()->objmgr.GetObj(guid);
return o!=NULL;
}
DefReturnResult DefScriptPackage::SCGetItemProtoValue(CmdSet& Set) DefReturnResult DefScriptPackage::SCGetItemProtoValue(CmdSet& Set)
{ {
if(!(((PseuInstance*)parentMethod)->GetWSession() && ((PseuInstance*)parentMethod)->GetWSession()->IsValid())) if(!(((PseuInstance*)parentMethod)->GetWSession() && ((PseuInstance*)parentMethod)->GetWSession()->IsValid()))
@ -663,18 +705,17 @@ void DefScriptPackage::My_Run(std::string line, std::string username)
} }
} }
// temp fix to prevent users from executing scripts via return values exploit. example: DefXChgResult final;
// -out ?{say .shutdown} if(usrperm < 255)
// note that the following code can still be executed:
// -out ${q}{say .shutdown}
// where var q = "?"
if(usrperm < 255 && line.find("?{")!=std::string::npos)
{ {
if(line.find("?{")!=std::string::npos)
logerror("WARNING: %s wanted to exec \"%s\"",username.c_str(),line.c_str()); logerror("WARNING: %s wanted to exec \"%s\"",username.c_str(),line.c_str());
return; final=ReplaceVars(line,NULL,0,false); // prevent execution of embedded scripts (= using return values) that could trigger dangerous stuff.
} }
else
final=ReplaceVars(line,NULL,0,true); // exec as usual
DefXChgResult final=ReplaceVars(line,NULL,0,false);
CmdSet curSet; CmdSet curSet;
SplitLine(curSet,final.str); SplitLine(curSet,final.str);

View File

@ -31,5 +31,7 @@ DefReturnResult SCGetName(CmdSet&);
DefReturnResult SCGetPlayerGuid(CmdSet&); DefReturnResult SCGetPlayerGuid(CmdSet&);
DefReturnResult SCGetEntry(CmdSet&); DefReturnResult SCGetEntry(CmdSet&);
DefReturnResult SCGetItemProtoValue(CmdSet&); DefReturnResult SCGetItemProtoValue(CmdSet&);
DefReturnResult SCGetObjectType(CmdSet&);
DefReturnResult SCObjectKnown(CmdSet&);
#endif #endif

View File

@ -98,6 +98,7 @@ bool PseuInstance::Init(void) {
_scp->variables.Set("@version_short",_ver_short); _scp->variables.Set("@version_short",_ver_short);
_scp->variables.Set("@version",_ver); _scp->variables.Set("@version",_ver);
_scp->variables.Set("@inworld","false");
log("Loading DefScripts from folder '%s'",_scpdir.c_str()); log("Loading DefScripts from folder '%s'",_scpdir.c_str());

View File

@ -208,6 +208,7 @@ void WorldSession::_OnEnterWorld(void)
if(!_logged) if(!_logged)
{ {
_logged=true; _logged=true;
GetInstance()->GetScripts()->variables.Set("@inworld","true");
GetInstance()->GetScripts()->RunScript("_enterworld",NULL); GetInstance()->GetScripts()->RunScript("_enterworld",NULL);
} }
@ -219,7 +220,7 @@ void WorldSession::_OnLeaveWorld(void)
{ {
_logged=false; _logged=false;
GetInstance()->GetScripts()->RunScript("_leaveworld",NULL); GetInstance()->GetScripts()->RunScript("_leaveworld",NULL);
GetInstance()->GetScripts()->variables.Set("@inworld","false");
} }
} }