mojo_client/bin/scripts/__core_func.def
2008-03-24 16:43:51 +00:00

177 lines
5.0 KiB
Modula-2

// core functions. required by other scripts to run!
#onload
log * Loading core scripts...
#/onload
//--------------------------------------------------------
#script=append
//--------------------------------------------------------
// usage: append text to a string.
// args:
// @def: text to append
// @0: variable to which the text should be appended
// get the var name if the original variable
SET,v #${@caller}::${@0}
// if it has not been set before, init it now
DEFAULT,${v}
// append to the original var. inner ${..} gets the var name, outer ${..} get the value of the var name we just got.
SET,${v} ${${v}}${@def}
// remove the name placeholder
UNSET v
//--------------------------------------------------------
#script=exloadscp
#permission=255
//--------------------------------------------------------
if ?{fileexists ${@def}}
loadscp,{${@0}} ${@def}
else
logdebug skipped loading of non-existent file '${@def}'
endif
//--------------------------------------------------------
#script=loadallscp
#permission=255
//--------------------------------------------------------
LOG * Loading SCP data storages...
// example:
// LOADSCP,test data/test.scp
// load default databases
LOADSCP,class data/scp/class.scp
LOADSCP,gender data/scp/gender.scp
LOADSCP,language data/scp/language.scp
LOADSCP,map data/scp/map.scp
LOADSCP,race data/scp/race.scp
// load extended databases if present.
EXLOADSCP,sound data/scp/sound.scp
EXLOADSCP,emote data/scp/emote.scp
EXLOADSCP,zone data/scp/zone.scp
LOG * SCP loaded.
//-------------------------------------------------
#script=string_is_command
//-------------------------------------------------
// purpose: detect if @def might be a server command.
// split all chars we have set in the config files to a list.
lsplit,#CMDCHARLIST ${#OTHER_CMD_CHARS}
// find out the first char of the string
SET,c ?{SUBSTR,1 ${@def}}
// does the char list contain our first char? if yes it can trigger a server command.
if ?{lcontains,#CMDCHARLIST ${c}}
unset c
return true
endif
unset c
return false
//--------------------------------------------------------
#script=normalize_name
//--------------------------------------------------------
// uppercases first char, lowercases rest
set,name ?{uppercase ?{substr,1 ${@def}}}
set,len ?{strlen ${@def}}
sub,len 1
append,name ?{lowercase ?{substr,${len},1 ${@def}}}
return ${name}
//---------------------------------------------------------
#script=globname
//---------------------------------------------------------
// purpose: returns the global name of a variable
// args: @def: var name, @0 (optional): name of the intended parent func
set,c ?{substr,1 ${@def}}
// it is a global var if the varname starts with # or there is no caller script
if ?{or,?{equal,# ${c}} ?{not ${@caller}}}
return ${@def}
endif
set,top ${@0}
default,top ${@caller}
return #${top}::${@def}
//---------------------------------------------------------
#script=sclistname
//---------------------------------------------------------
// purpose: returns the global name of a script list
// args: @def: script name
set,c ?{substr,1 ${@def}}
// it is a global name already if the script name starts with # or there is no caller script
if ?{or,?{equal,# ${c}} ?{not ${@caller}}}
return ${@def}
endif
return #DEFSCRIPT::SCRIPT::${@def}
//---------------------------------------------------------
#script=getvar
//---------------------------------------------------------
// purpose: returns the value of a variable. if the variable hasnt been set return empty string.
set,top ${@caller}
default,top #
set,v ?{globname,${top} ${@def}}
unset top
//out getvar: v=${v} -> ${${v}}
if ?{isset ${v}}
return ${${v}}
endif
return
//---------------------------------------------------------
#script=reverse
//---------------------------------------------------------
// purpose: reverse a srtring
// args: @def: string to reverse
// returns: reversed string
set,outstr
set,elems ?{lsplit,mylist ${@def}}
set,i ${elems}
loop
sub,i 1
if ?{equal,${i} -1}
exitloop
endif
append,outstr ?{lindex,mylist ${i}}
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\}"
set,sc ?{lowercase ${@0}}
if ?{and,{?{strlen ${@def}}} ?{strlen ${sc}}}
if ?{not ?{ScriptExists ${sc}}}
createdef ${sc}
endif
lpushback,{?{sclistname ${sc}}} ${@def}
unset cmd
endif
//---------------------------------------------------------
#script=scripthasline
//---------------------------------------------------------
// purpose: check if a script has a certain line of code (exact match)
// args:
// @0: script name
// @def: line of text to look for
return ?{lcontains,{?{sclistname ?{lowercase ${@0}}}} ${@def}}