* implemented multiple scripts per .def file. ATTENTION: scripts will no longer be auto-loaded by the engine; they will be loaded via startup.def now. scripts will be compacted in next revs.

* forgot to use repository's openssl in Network system also.
* moved _startup.def to PseuWoW base folder!
This commit is contained in:
False.Genesis 2007-06-19 19:44:14 +00:00
parent d0bf7c3c2b
commit 996256718c
8 changed files with 132 additions and 115 deletions

43
bin/_startup.def Normal file
View File

@ -0,0 +1,43 @@
#permission=255
// PSEUWOW DEF_SCRIPT STARTUP FILE
LOG * DefScript StartUp [${@version_short}]...
// first, load all scripts in patch 'scripts' with extension .def
SET,fcount ?{LGETFILES,scriptlist,def scripts}
LOGDETAIL * Loading ${fcount} scripts.
// iterate over all files and load them; if counter i is equal to the amount of files we are done.
SET,i 0
LOOP
IF ?{EQUAL,${i} ${fcount}}
EXITLOOP
ENDIF
SET,fn ./scripts/?{LINDEX,scriptlist ${i}}
LOGDEBUG * Loading script file [${fn}]
IF ?{NOT ?{LOADDEF ${fn}}}
LOGERROR Can't load script [${fn}]
ENDIF
ADD,i 1
ENDLOOP
UNSET tmp
UNSET fcount
UNSET i
UNSET fn
LDELETE scriptlist
// loads & applies the configuration
CONFIG
// set permissions for internal functions
INTERNAL_PERM
// Load some SCP files
LOADALLSCP
// do more stuff here in future...
LOG * StartUp complete!

View File

@ -1,23 +0,0 @@
#permission=255
// PSEUWOW DEF_SCRIPT STARTUP FILE
LOG * DefScript StartUp [${@version_short}]...
// loads & applies the configuration
CONFIG
// set permissions for internal functions
INTERNAL_PERM
// Load some SCP files
LOADALLSCP
// do more stuff here in future...
// load the uptime counter
LOADDEF uptime
LOG * StartUp complete!

View File

@ -174,18 +174,19 @@ void DefScriptPackage::DeleteScript(std::string sn)
}
bool DefScriptPackage::LoadByName(std::string name){
return LoadScriptFromFile((scPath+name).append(".def"),name);
return LoadScriptFromFile((scPath+name).append(".def"));
}
bool DefScriptPackage::LoadScriptFromFile(std::string fn, std::string sn){
if(fn.empty() || sn.empty())
bool DefScriptPackage::LoadScriptFromFile(std::string fn){
if(fn.empty())
return false;
std::string label, value, line;
std::string label, value, line, sn;
std::fstream f;
bool load_debug=false,load_notify=false, exec=false, cantload=false;
bool load_debug=false,load_notify=false,cantload=false;
char z;
unsigned int absline=0;
DefScript *curScript = NULL;
f.open(fn.c_str(),std::ios_base::in);
if(!f.is_open())
@ -193,15 +194,24 @@ bool DefScriptPackage::LoadScriptFromFile(std::string fn, std::string sn){
std::deque<unsigned int> Blocks;
if(GetScript(sn))
delete GetScript(sn);
DefScript *newscript = new DefScript(this);
Script[sn] = newscript;
Script[sn]->SetName(sn); // necessary that the script knows its own name
// auto-assign the scriptname as the plain filename without extension. can be changed while loading
unsigned int slashpos = fn.find_last_of("\\/");
if(slashpos == std::string::npos)
slashpos = 0;
unsigned int ppos = fn.find_last_of(".");
if(ppos == std::string::npos)
ppos = fn.length();
sn = fn.substr(slashpos+1,(ppos-slashpos-1));
_UpdateOrCreateScriptByName(sn);
curScript=Script[sn];
DeleteScript(SN_ONLOAD);
while(!f.eof()){
while(!f.eof())
{
line.clear();
while (true) {
while (true)
{
f.get(z);
if(z=='\n' || f.eof())
break;
@ -226,19 +236,32 @@ bool DefScriptPackage::LoadScriptFromFile(std::string fn, std::string sn){
{
scriptPermissionMap[sn] = atoi(value.c_str());
}
if(line=="load_debug")
load_debug=true;
if(line=="load_notify")
load_notify=true;
if(line=="debug")
Script[sn]->SetDebug(true);
if(line=="onload")
exec=true;
if(line=="endonload" || line=="/onload")
exec=false;
else if(label=="script")
{
if(!curScript->GetLines()) // delete script if unused
DeleteScript(curScript->GetName());
sn = value;
_UpdateOrCreateScriptByName(sn);
curScript=Script[sn];
}
else if(line=="debug")
{
if(curScript)
curScript->SetDebug(true);
}
else if(line=="onload")
Script[SN_ONLOAD] = curScript = new DefScript(this);
else if(line=="endonload" || line=="/onload")
{
RunScript(SN_ONLOAD,NULL,sn);
DeleteScript(SN_ONLOAD);
curScript=Script[sn];
}
//...
continue; // line was an option, not script content
}
// help with loading lines where a space or tab have accidently been put after the cmd
std::string tline=stringToLower(line);
if(memcmp(tline.c_str(),"else ",5)==0 || memcmp(tline.c_str(),"else\t",5)==0) tline="else";
@ -304,31 +327,15 @@ bool DefScriptPackage::LoadScriptFromFile(std::string fn, std::string sn){
continue; // continue with next line without adding the current line to script
}
if(load_debug)
std::cout<<"~LOAD: "<<line<<"\n";
if(!exec)
Script[sn]->AddLine(line);
else
{
if(!ScriptExists(SN_ONLOAD))
Script[SN_ONLOAD] = new DefScript(this);
Script[SN_ONLOAD]->AddLine(line);
}
curScript->AddLine(line);
}
f.close();
if(cantload || Blocks.size())
{
printf("DefScript: Error loading script '%s'. block mismatch?\n",sn.c_str());
printf("DefScript: Error loading file '%s'. block mismatch?\n",fn.c_str());
DeleteScript(sn);
return false;
}
if(ScriptExists(SN_ONLOAD))
{
RunScript(SN_ONLOAD,NULL,sn);
DeleteScript(SN_ONLOAD);
}
if(load_notify)
std::cout << "+> Script '" << sn << "' [" << fn << "] successfully loaded.\n";
// ...
return true;
@ -387,7 +394,7 @@ std::string DefScript::GetLine(unsigned int id)
bool DefScript::AddLine(std::string l){
if(l.empty())
return false;
Line.insert(Line.end(),l);
Line.push_back(l);
return true;
}
@ -427,9 +434,8 @@ DefReturnResult DefScriptPackage::RunScript(std::string name, CmdSet *pSet,std::
{
DefReturnResult r;
if( (!ScriptExists(name)) && (!HasFunc(name)) )
if(!LoadByName(name))
{
r.ok=false; // doesnt exist & cant be loaded
r.ok=false; // doesnt exist
r.ret="";
return r;
}
@ -933,4 +939,11 @@ DefReturnResult DefScriptPackage::Interpret(CmdSet& Set)
return result;
}
void DefScriptPackage::_UpdateOrCreateScriptByName(std::string sn)
{
if(GetScript(sn))
DeleteScript(sn);
DefScript *newscript = new DefScript(this);
newscript->SetName(sn); // necessary that the script knows its own name
Script[sn] = newscript;
}

View File

@ -106,7 +106,7 @@ public:
void Clear(void);
DefScript *GetScript(std::string);
unsigned int GetScripts(void);
bool LoadScriptFromFile(std::string,std::string);
bool LoadScriptFromFile(std::string);
DefReturnResult RunScript(std::string name,CmdSet* pSet,std::string override_name="");
bool BoolRunScript(std::string,CmdSet*);
unsigned int GetScriptID(std::string);
@ -133,6 +133,7 @@ public:
void My_Run(std::string line,std::string username);
private:
void _UpdateOrCreateScriptByName(std::string);
void _InitFunctions(void);
DefXChgResult ReplaceVars(std::string str, CmdSet* pSet, unsigned char VarType, bool run_embedded);
void SplitLine(CmdSet&,std::string);

View File

@ -27,37 +27,40 @@ DefReturnResult DefScriptPackage::func_out(CmdSet& Set){
}
DefReturnResult DefScriptPackage::func_loaddef(CmdSet& Set){
DefReturnResult r;
if( ScriptExists(Set.defaultarg) )
{
r.ret="exists";
return r;
return "exists";
}
return func_reloaddef(Set);
}
// supply either the filename in default scripts directory, without extension,
// OR provide the full file path WITH extension
DefReturnResult DefScriptPackage::func_reloaddef(CmdSet& Set){
DefReturnResult r;
bool result=false;
std::string fn;
if(Set.arg[0].empty())
// if the filename does NOT contain any path, load from default script dir
std::string::size_type slashpos = Set.defaultarg.find_last_of("\\/");
std::string::size_type ppos = Set.defaultarg.find_last_of(".");
if(slashpos == std::string::npos)
{
result=LoadByName(Set.defaultarg);
fn=(scPath + Set.defaultarg).append(".def");
fn=scPath+Set.defaultarg;
}
else
{
std::string::size_type pos = Set.arg[0].find('/');
if(pos == std::string::npos)
fn=scPath+Set.arg[0];
else
fn=Set.arg[0];
result=LoadScriptFromFile(fn,Set.defaultarg);
fn=Set.defaultarg;
}
if(ppos==std::string::npos || ppos < slashpos) // even if there was neither / nor . they will be equal
fn+=".def";
result=LoadScriptFromFile(fn);
r.ret=fn;
if(!result)
{
std::cout << "Could not load script '" << Set.defaultarg << "' [" << fn << "]\n";
//std::cout << "Could not load script '" << Set.defaultarg << "' [" << fn << "]\n";
r.ret="";
}
return r;

View File

@ -108,37 +108,17 @@ bool PseuInstance::Init(void) {
_scp->variables.Set("@version",_ver);
_scp->variables.Set("@inworld","false");
if(!_scp->BoolRunScript("_startup",NULL))
if(!_scp->LoadScriptFromFile("./_startup.def"))
{
logerror("Error loading '_startup.def'");
SetError();
}
else 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());
}
}
}
// TODO: find a better loaction where to place this block!
if(GetConf()->enablegui)
{

View File

@ -35,7 +35,7 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#include <assert.h>
#include <stdarg.h>
#ifdef HAVE_OPENSSL
#include <openssl/rand.h>
#include "openssl/rand.h"
#endif
#include "SocketHandler.h"

View File

@ -26,7 +26,7 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#include "Socket.h"
#include "CircularBuffer.h"
#ifdef HAVE_OPENSSL
#include <openssl/ssl.h>
#include "openssl/ssl.h"
#ifdef _WIN32
// TODO: systray.exe??
#define RANDOM "systray.exe"