* auto-load all .def files in ./scripts/ directory after executing _startup.def

* fixed exception at DefScript:substr when called with empty string
* added 3 new functions: uppercase, lowercase, random[,<min>] <max>.
* added some better debug output when an incorrect script is loaded
This commit is contained in:
False.Genesis 2007-04-02 22:41:33 +00:00
parent 7961aa9046
commit a3ae8273ba
13 changed files with 263 additions and 18 deletions

View File

@ -7,13 +7,6 @@ LOG * DefScript StartUp [${@version_short}]...
// loads & applies the configuration
CONFIG
// preload the scripts, however its not important to load them now.
//they will get loaded automatically if needed
LOADALL
// RELOADDEF myscript
// ...
// set permissions for internal functions
INTERNAL_PERM

3
bin/scripts/gcl.def Normal file
View File

@ -0,0 +1,3 @@
// write leetspeak to channel generalchat (see gc.def)
#permission=0
gc ?{toleet ${@def}}

3
bin/scripts/sl.def Normal file
View File

@ -0,0 +1,3 @@
// say leetspeak text. gmcommands can't be executed this way
#permission=0
s,{${@0}} ?{toleet ${@def}}

35
bin/scripts/slap.def Normal file
View File

@ -0,0 +1,35 @@
// purpose: let a player fly. (at least works on MaNGOS)
// args: @def: player name
// returns: guid of the targeted player, else false. empty string if we are not in the world.
#permission=100
// we can use this script only if we are in the world
if ?{not ${@inworld}}
logerror Can't slap anything if i am not in the world!
return
endif
// normalize player name. first char uppercased, rest lowercased.
set,name ?{uppercase ?{substr,1 ${@def}}}
out name1: ${name}
set,len ?{strlen ${@def}}
out strlen: ${len}
sub,len 1
out len: ${len}
append,name ?{lowercase ?{substr,${len},1 ${@def}}}
out name2: ${name}
// target the player. if targeting was successful, cast spell "Knockback 500".
set,t ?{target ${name}}
if ${t}
logdebug slapping player '${name}'
castspell 11027
target 0
else
logerror Can't target player '${name}'
endif
unset name
return ${t}

95
bin/scripts/toleet.def Normal file
View File

@ -0,0 +1,95 @@
// toleet.def
// ==========
// purpose: convert any text into leetspeak [1337sp34k]
// args:
// @def: the text to convert
// returns: leetspeak^^
// call it from script only
#permission=255
#onload
// this can be used by other scripts to check if we have loaded this script
set,#HAVE_LEET true
#endonload
// --- Begin of main function ---
// empty the string where the leet will be stored
set,str
// total length of the string to convert
set,l ?{strlen ${@def}}
// position counter
set,x 0
loop
// check if we have reached the end of the string
if ?{bigger,${x} ${l}}
exitloop
endif
// c stores current char (@def at position x); c2 is the same char but always lowercased
set,c ?{substr,1,${x} ${@def}}
set,c2 ?{lowercase ${c}}
// conversion functions:
// note that "+" is a variable name here!
// it will store the "new" char if c/c2 could be converted
if ?{equal,${c2} a}
set,+ 4
endif
if ?{equal,${c2} e}
set,+ 3
endif
if ?{equal,${c2} i}
set,+ !
endif
if ?{equal,${c2} l}
set,+ 1
endif
if ?{equal,${c2} t}
set,+ 7
endif
if ?{equal,${c2} s}
set,+ 5
endif
if ?{equal,${c2} o}
set,+ 0
endif
if ?{equal,${c2} f}
set,+ ph
endif
if ?{equal,${c2} h}
set,+ #
endif
if ?{equal,${c} Z}
set,+ 7
endif
if ?{equal,${c} R}
set,+ 2
endif
if ?{equal,${c} B}
set,+ <3
endif
// if var "+" is still empty, default it to our current char
default,+ ${c}
// and append it to the final output
append,str ${+}
// finally delete it again
unset +
// and increase the counter by 1
add,x 1
endloop
unset l
unset x
unset c
unset c2
return ${str}

View File

@ -84,6 +84,9 @@ void DefScriptPackage::_InitFunctions(void)
AddFunc("or",&DefScriptPackage::func_or);
AddFunc("xor",&DefScriptPackage::func_xor);
AddFunc("substr",&DefScriptPackage::func_substr);
AddFunc("uppercase",&DefScriptPackage::func_uppercase);
AddFunc("lowercase",&DefScriptPackage::func_lowercase);
AddFunc("random",&DefScriptPackage::func_random);
}
void DefScriptPackage::AddFunc(std::string n,DefReturnResult (DefScriptPackage::*f)(CmdSet& Set))
@ -155,12 +158,14 @@ bool DefScriptPackage::LoadByName(std::string name){
}
bool DefScriptPackage::LoadScriptFromFile(std::string fn, std::string sn){
if(fn.empty() || sn.empty()) return false;
if(fn.empty() || sn.empty())
return false;
std::string label, value, line;
std::fstream f;
bool load_debug=false,load_notify=false, exec=false, cantload=false;
char z;
unsigned int absline=0;
f.open(fn.c_str(),std::ios_base::in);
if(!f.is_open())
@ -182,6 +187,7 @@ bool DefScriptPackage::LoadScriptFromFile(std::string fn, std::string sn){
break;
line+=z;
}
absline++;
if(line.empty())
continue; // line is empty, proceed with next line
@ -274,7 +280,7 @@ bool DefScriptPackage::LoadScriptFromFile(std::string fn, std::string sn){
}
if(mismatch || bopen) // no bracket must be left open now
{
printf("DefScript: Bracket mismatch at line '%s'\n",line.c_str());
printf("DefScript [%s:%u]: Bracket mismatch at line '%s'\n",sn.c_str(),absline,line.c_str());
continue; // continue with next line without adding the current line to script
}
@ -292,7 +298,7 @@ bool DefScriptPackage::LoadScriptFromFile(std::string fn, std::string sn){
f.close();
if(cantload || Blocks.size())
{
printf("DefScript: Error loading script '%s'\n",sn.c_str());
printf("DefScript: Error loading script '%s'. block mismatch?\n",sn.c_str());
DeleteScript(sn);
return false;
}

View File

@ -174,6 +174,9 @@ private:
DefReturnResult func_or(CmdSet&);
DefReturnResult func_xor(CmdSet&);
DefReturnResult func_substr(CmdSet&);
DefReturnResult func_uppercase(CmdSet&);
DefReturnResult func_lowercase(CmdSet&);
DefReturnResult func_random(CmdSet&);
// setup own function declarations here
# include "DefScriptInterfaceInclude.h"

View File

@ -413,12 +413,45 @@ DefReturnResult DefScriptPackage::func_xor(CmdSet& Set)
DefReturnResult DefScriptPackage::func_substr(CmdSet& Set)
{
DefReturnResult r;
unsigned int start,len;
len=(unsigned int)toNumber(Set.arg[0]);
start=(unsigned int)toNumber(Set.arg[1]);
if(start+len>Set.defaultarg.length())
len=Set.defaultarg.length()-start;
r.ret=Set.defaultarg.substr(start,len);
if(Set.defaultarg.empty())
{
r.ret="";
}
else
{
unsigned int start,len;
len=(unsigned int)toNumber(Set.arg[0]);
start=(unsigned int)toNumber(Set.arg[1]);
if(start+len>Set.defaultarg.length())
len=Set.defaultarg.length()-start;
r.ret=Set.defaultarg.substr(start,len);
}
return r;
}
DefReturnResult DefScriptPackage::func_uppercase(CmdSet& Set)
{
DefReturnResult r;
r.ret=stringToUpper(Set.defaultarg);
return r;
}
DefReturnResult DefScriptPackage::func_lowercase(CmdSet& Set)
{
DefReturnResult r;
r.ret=stringToLower(Set.defaultarg);
return r;
}
DefReturnResult DefScriptPackage::func_random(CmdSet& Set)
{
DefReturnResult r;
int min,max;
min=(int)toUint64(Set.arg[0]);
max=(int)toUint64(Set.defaultarg);
r.ret=toString(ldbl( min + ( rand() % (max - min + 1)) ));
return r;
}

View File

@ -100,14 +100,37 @@ bool PseuInstance::Init(void) {
_scp->variables.Set("@version",_ver);
_scp->variables.Set("@inworld","false");
log("Loading DefScripts from folder '%s'",_scpdir.c_str());
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<std::string> 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());
}
}
}
if(GetConf()->enablecli)
{
log("Starting CLI...");

View File

@ -5,6 +5,13 @@
#include <time.h>
#include "tools.h"
#ifndef _WIN32
# include <sys/dir.h>
# include <errno.h>
#else
# include <windows.h>
#endif
void printchex(std::string in, bool spaces=true){
unsigned int len=0,i;
len=in.length();
@ -84,3 +91,46 @@ std::string toHexDump(uint8* array,uint32 size,bool spaces)
return ss.str();
}
std::deque<std::string> GetFileList(std::string path)
{
std::deque<std::string> files;
# ifndef _WIN32 // TODO: fix this function for linux if needed
const char *p = path.c_str();
DIR * dirp;
struct dirent * dp;
dirp = opendir(p);
while (dirp)
{
errno = 0;
if ((dp = readdir(dirp)) != NULL)
files.push_back(std::string(dp->d_name));
else
break;
}
if(dirp)
closedir(dirp);
# else
if(path.at(path.length()-1)!='/')
path += "/";
path += "*.*";
const char *p = path.c_str();
WIN32_FIND_DATA fil;
HANDLE hFil=FindFirstFile(p,&fil);
if(hFil!=INVALID_HANDLE_VALUE)
{
files.push_back(std::string(fil.cFileName));
while(FindNextFile(hFil,&fil))
files.push_back(std::string(fil.cFileName));
}
# endif
while(files.front()=="." || files.front()=="..")
files.pop_front();
return files;
}

View File

@ -15,5 +15,6 @@ std::string toString(uint64);
std::string getDateString(void);
uint64 toInt(std::string);
std::string toHexDump(uint8* array,uint32 size,bool spaces=true);
std::deque<std::string> GetFileList(std::string);
#endif

Binary file not shown.