Index: shared/Network/TcpSocket.cpp =================================================================== --- shared/Network/TcpSocket.cpp (revision 255) +++ shared/Network/TcpSocket.cpp (working copy) @@ -746,7 +746,7 @@ va_list ap; va_start(ap, format); char slask[5000]; - vsnprintf(slask, 5000, format, ap); + vsprintf(slask, format, ap); va_end(ap); Send( slask ); } Index: shared/SysDefs.h =================================================================== --- shared/SysDefs.h (revision 255) +++ shared/SysDefs.h (working copy) @@ -72,7 +72,7 @@ #define I64FMTD "%llu" #define SI64FMTD "%lld" # if PLATFORM == PLATFORM_UNIX - typedef __int64_t int64; +/* typedef __int64_t int64; typedef __int32_t int32; typedef __int16_t int16; typedef __int8_t int8; @@ -81,7 +81,17 @@ typedef __uint16_t uint16; typedef __uint8_t uint8; typedef uint16 WORD; - typedef uint32 DWORD; + typedef uint32 DWORD;*/ + typedef long long int64; + typedef long int32; + typedef short int16; + typedef char int8; + typedef unsigned long long uint64; + typedef unsigned long uint32; + typedef unsigned short uint16; + typedef unsigned char uint8; + typedef unsigned short WORD; + typedef uint32 DWORD; # else typedef long long int64; typedef long int32; Index: Client/DefScript/DefScript.cpp =================================================================== --- Client/DefScript/DefScript.cpp (revision 255) +++ Client/DefScript/DefScript.cpp (working copy) @@ -1,3 +1,4 @@ +/*<<<<<<< .mine #include #include #include @@ -21,6 +22,8 @@ // --- SECTION FOR SCRIPT PACKAGES --- DefScriptPackage::DefScriptPackage() { +char printf[]="printf"; + SetLog(printf); SetDebugLog(printf); SetErrorLog(printf); @@ -305,7 +308,7 @@ line.erase(0,1); if(line.empty()) continue; - if(line.at(0)=='/' && line.at(1)=='/') + if(line.at(0)=='/' && line.at(1)=='/') continue; // line is comment, proceed with next line if(line_strip) @@ -340,7 +343,7 @@ commented = false; continue; } - + // commented lines will not be loaded nor have any #-tags evaluated (except the one above!) if(commented) continue; @@ -489,12 +492,12 @@ DeleteScript(sn); return false; } - + // ... return true; } - + // --- SECTION FOR THE INDIVIDUAL SCRIPTS IN A PACKAGE --- DefScript::DefScript(DefScriptPackage *p) @@ -607,7 +610,7 @@ std::string line; for(unsigned int i=0;iGetLines();i++) - { + { line=sc->GetLine(i); if(line.empty() || line[0] == '#') // skip markers and preload statements if not removed before continue; @@ -766,8 +769,8 @@ } void DefScriptPackage::SplitLine(CmdSet& Set,std::string line) -{ - +{ + unsigned int i=0; unsigned int bracketsOpen=0,curParam=0; bool cmdDefined=false; @@ -790,8 +793,8 @@ curParam++; } tempLine.clear(); - - } + + } else if( line[i]==' ' && !bracketsOpen) { if(!cmdDefined){ @@ -802,8 +805,8 @@ } Set.defaultarg=line.substr(i+1,line.length()-i); - break; - + break; + } else if(line[i]=='\\') { @@ -861,7 +864,7 @@ t.erase(ob,blen); t.insert(ob,retStr); i=ob-1; - + } isVar=false; } @@ -907,7 +910,7 @@ bracketsOpen=0, // amount of brackets opened bLen=0; // the lenth of the string in brackets, e.g. ${abc} == 3 - unsigned char + unsigned char nextVar=DEFSCRIPT_NONE; // '$' or '?' bool hasChanged=false, // additional helper. once true, xchg.result will be true later also @@ -924,7 +927,7 @@ escaped=false; continue; } - else if(str[i]=='{') + else if(str[i]=='{') { if(!bracketsOpen) openingBracket=i; // var starts with $, normal bracket with { @@ -1096,7 +1099,7 @@ break; } - if( (!global) && (vn.at(0)!='@') ) + if( (!global) && (vn.at(0)!='@') ) vn=sn+"::"+vn; return vn; @@ -1124,7 +1127,7 @@ { if(_functable[i].escape) // if we are going to use a C++ function, unescape the whole set, if supposed to do so. UnescapeSet(Set); // it will not have any bad side effects, we leave the func within this block! - + result=(this->*(_functable[i].func))(Set); if(_functable[i].escape) result.ret = EscapeString(result.ret); // and since we are returning a string into the engine, escape it again, if set. @@ -1141,10 +1144,10 @@ // if nothing has been found its maybe an external script file to run result=RunScript(Set.cmd, &Set); - if((!result.ok) /*&& Script[Set.cmd]->GetDebug()*/) + if((!result.ok) )///*&& Script[Set.cmd]->GetDebug() std::cout << "Could not execute script command '" << Set.cmd << "'\n"; - + return result; } @@ -1173,7 +1176,7 @@ return out; } -// escapes whole string, which can no longer be parsed & interpreted +// escapes whole string, which can no longer be parsed & interpreted std::string DefScriptPackage::EscapeString(std::string s) { std::string out; @@ -1263,3 +1266,1270 @@ } return false; } +/*======= +#include +#include +#include +#include +#include +#include +#include +#include "VarSet.h" +#include "DefScript.h" + +using namespace DefScriptTools; + +#define SN_ONLOAD "?onload?" + +enum DefScriptBlockType +{ + BLOCK_IF, + BLOCK_LOOP +}; + +// --- SECTION FOR SCRIPT PACKAGES --- +DefScriptPackage::DefScriptPackage() +{ + SetLog(printf); + SetDebugLog(printf); + SetErrorLog(printf); + _DEFSC_DEBUG + ( + hLogfile.open("DefScriptLog.txt",std::ios_base::out); + hLogfile << "DefScript engine execution log, compilation date: " __DATE__ " " __TIME__ "\n\n" ; + ) + _eventmgr=new DefScript_DynamicEventMgr(this); + _InitFunctions(); +# ifdef USING_DEFSCRIPT_EXTENSIONS + _InitDefScriptInterface(); +# endif +} + +DefScriptPackage::~DefScriptPackage() +{ + if(_eventmgr) + delete _eventmgr; + Clear(); + _DEFSC_DEBUG(hLogfile.close()); +} + +void DefScriptPackage::Log(const char* fmt,...) +{ + va_list ap; + va_start(ap, fmt); + (*_slog)(fmt,ap); + va_end(ap); +} + +void DefScriptPackage::DebugLog(const char* fmt,...) +{ + va_list ap; + va_start(ap, fmt); + (*_sdebuglog)(fmt,ap); + va_end(ap); +} + +void DefScriptPackage::ErrorLog(const char* fmt,...) +{ + va_list ap; + va_start(ap, fmt); + (*_serrorlog)(fmt,ap); + va_end(ap); +} + +void DefScriptPackage::SetParentMethod(void *p) +{ + parentMethod = p; +} + +void DefScriptPackage::Clear(void) +{ + for(std::map::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 + } + + Script.empty(); +} + +void DefScriptPackage::_InitFunctions(void) +{ + AddFunc("out",&DefScriptPackage::func_out); + AddFunc("set",&DefScriptPackage::func_set,false); + AddFunc("default",&DefScriptPackage::func_default,false); + AddFunc("unset",&DefScriptPackage::func_unset); + AddFunc("shdn",&DefScriptPackage::func_shdn,false); + AddFunc("loaddef",&DefScriptPackage::func_loaddef); + AddFunc("reloaddef",&DefScriptPackage::func_reloaddef); + AddFunc("createdef",&DefScriptPackage::func_createdef); + AddFunc("unloaddef",&DefScriptPackage::func_unloaddef); + AddFunc("setscriptpermission",&DefScriptPackage::func_setscriptpermission); + AddFunc("toint",&DefScriptPackage::func_toint); + AddFunc("add",&DefScriptPackage::func_add); + AddFunc("sub",&DefScriptPackage::func_sub); + AddFunc("mul",&DefScriptPackage::func_mul); + AddFunc("div",&DefScriptPackage::func_div); + AddFunc("mod",&DefScriptPackage::func_mod); + AddFunc("pow",&DefScriptPackage::func_pow); + AddFunc("bitor",&DefScriptPackage::func_bitor); + AddFunc("bitand",&DefScriptPackage::func_bitand); + AddFunc("bitxor",&DefScriptPackage::func_bitxor); + AddFunc("addevent",&DefScriptPackage::func_addevent); + AddFunc("removeevent",&DefScriptPackage::func_removeevent); + AddFunc("abs",&DefScriptPackage::func_abs); + AddFunc("greater",&DefScriptPackage::func_bigger); + AddFunc("greater_eq",&DefScriptPackage::func_bigger_eq); + AddFunc("bigger",&DefScriptPackage::func_bigger); + AddFunc("bigger_eq",&DefScriptPackage::func_bigger_eq); + AddFunc("equal",&DefScriptPackage::func_equal); + AddFunc("isset",&DefScriptPackage::func_isset); + AddFunc("not",&DefScriptPackage::func_not); + AddFunc("smaller",&DefScriptPackage::func_smaller); + AddFunc("smaller_eq",&DefScriptPackage::func_smaller_eq); + AddFunc("strlen",&DefScriptPackage::func_strlen); + AddFunc("tohex",&DefScriptPackage::func_tohex); + AddFunc("and",&DefScriptPackage::func_and); + 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); + AddFunc("fileexists",&DefScriptPackage::func_fileexists); + AddFunc("strfind",&DefScriptPackage::func_strfind); + AddFunc("funcexists",&DefScriptPackage::func_funcexists); + AddFunc("scriptexists",&DefScriptPackage::func_scriptexists); + + // list functions + AddFunc("lpushback",&DefScriptPackage::func_lpushback); + AddFunc("lappend",&DefScriptPackage::func_lpushback); // alias for lpushback + AddFunc("lpushfront",&DefScriptPackage::func_lpushfront); + AddFunc("lpopback",&DefScriptPackage::func_lpopback); + AddFunc("lpopfront",&DefScriptPackage::func_lpopfront); + AddFunc("ldelete",&DefScriptPackage::func_ldelete); + AddFunc("lexists",&DefScriptPackage::func_lexists); + AddFunc("llen",&DefScriptPackage::func_llen); + AddFunc("linsert",&DefScriptPackage::func_linsert); + AddFunc("lsplit",&DefScriptPackage::func_lsplit); + AddFunc("lcsplit",&DefScriptPackage::func_lcsplit); + AddFunc("ljoin",&DefScriptPackage::func_ljoin); + AddFunc("lindex",&DefScriptPackage::func_lindex); + AddFunc("lclean",&DefScriptPackage::func_lclean); + AddFunc("lmclean",&DefScriptPackage::func_lmclean); + AddFunc("lerase",&DefScriptPackage::func_lerase); + AddFunc("lsort",&DefScriptPackage::func_lsort); + + // ByteBuffer functions + AddFunc("bbinit",&DefScriptPackage::func_bbinit); + AddFunc("bbdelete",&DefScriptPackage::func_bbdelete); + AddFunc("bbappend",&DefScriptPackage::func_bbappend); + AddFunc("bbread",&DefScriptPackage::func_bbread); + AddFunc("bbhexlike",&DefScriptPackage::func_bbhexlike); + AddFunc("bbtextlike",&DefScriptPackage::func_bbtextlike); + AddFunc("bbsetrpos",&DefScriptPackage::func_bbsetrpos); + AddFunc("bbsetwpos",&DefScriptPackage::func_bbsetwpos); + AddFunc("bbsize",&DefScriptPackage::func_bbsize); + + // file functions + AddFunc("fopen",&DefScriptPackage::func_fopen); + AddFunc("fclose",&DefScriptPackage::func_fclose); + AddFunc("fisopen",&DefScriptPackage::func_fisopen); + AddFunc("feof",&DefScriptPackage::func_feof); + AddFunc("frpos",&DefScriptPackage::func_frpos); + AddFunc("fwpos",&DefScriptPackage::func_fwpos); + AddFunc("fdel",&DefScriptPackage::func_fdel); + AddFunc("fflush",&DefScriptPackage::func_fflush); + AddFunc("fwrite",&DefScriptPackage::func_fwrite); + AddFunc("fread",&DefScriptPackage::func_fread); + AddFunc("freadb",&DefScriptPackage::func_freadb); + AddFunc("fwriteb",&DefScriptPackage::func_fwriteb); + AddFunc("freadbb",&DefScriptPackage::func_freadbb); + AddFunc("fwritebb",&DefScriptPackage::func_fwritebb); + AddFunc("fsize",&DefScriptPackage::func_fsize); + AddFunc("fexists",&DefScriptPackage::func_fileexists); // name alternative for "fileexists" + AddFunc("freadline",&DefScriptPackage::func_freadline); + AddFunc("fseekw",&DefScriptPackage::func_fseekw); + AddFunc("fseekr",&DefScriptPackage::func_fseekr); +} + +void DefScriptPackage::AddFunc(std::string n,DefReturnResult (DefScriptPackage::*f)(CmdSet& Set), bool esc) +{ + DefScriptFunctionEntry e(n,f,esc); + AddFunc(e); +} + +void DefScriptPackage::AddFunc(DefScriptFunctionEntry e) +{ + if( (!e.name.empty()) && (!HasFunc(e.name)) ) + _functable.push_back(e); +} + +bool DefScriptPackage::HasFunc(std::string n) +{ + for(DefScriptFunctionTable::iterator i=_functable.begin();i!=_functable.end();i++) + if(i->name==n) + return true; + return false; +} + +void DefScriptPackage::DelFunc(std::string n) +{ + for(DefScriptFunctionTable::iterator i=_functable.begin();i!=_functable.end();i++) + if(i->name==n) + { + _functable.erase(i); + break; + } +} + +void DefScriptPackage::SetPath(std::string p){ + scPath=p; +} + +DefScript *DefScriptPackage::GetScript(std::string scname){ + return ScriptExists(scname) ? Script[scname] : NULL; +} + +unsigned int DefScriptPackage::GetScripts(void){ + return Script.size(); +} + +DefScript_DynamicEventMgr *DefScriptPackage::GetEventMgr(void) +{ + return _eventmgr; +} + +bool DefScriptPackage::ScriptExists(std::string name) +{ + for (std::map::iterator i = Script.begin();i != Script.end();i++) + if(i->first == name && i->second != NULL) + return true; + return false; +} + +void DefScriptPackage::DeleteScript(std::string sn) +{ + lists.Unlink(SCRIPT_NAMESPACE + sn); // remove name from the list storage + if(ScriptExists(sn)) + { + + delete GetScript(sn); // delete the script itself + Script.erase(sn); // remove reference + } +} + +bool DefScriptPackage::LoadByName(std::string name){ + return LoadScriptFromFile((scPath+name).append(".def")); +} + +bool DefScriptPackage::LoadScriptFromFile(std::string fn){ + if(fn.empty()) + return false; + + std::string label, value, line, sn; + std::fstream f; + bool load_debug=false,load_notify=false,cantload=false; + bool line_strip = true; // true by default + bool commented = false; + bool escape_all = false; + char z; + unsigned int absline=0; + DefScript *curScript = NULL; + + f.open(fn.c_str(),std::ios_base::in); + if(!f.is_open()) + return false; + + std::deque Blocks; + + // 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 = stringToLower(fn.substr(slashpos+1,(ppos-slashpos-1))); + _UpdateOrCreateScriptByName(sn); + curScript=Script[sn]; + + DeleteScript(SN_ONLOAD); + + while(!f.eof()) + { + line.clear(); + while (true) + { + f.get(z); + if(z=='\n' || f.eof()) + break; + line+=z; + } + absline++; + if(line.empty()) + continue; // line is empty, proceed with next line + + while( !line.empty() && (line.at(0)==' ' || line.at(0)=='\t') ) + line.erase(0,1); + if(line.empty()) + continue; + if(line.at(0)=='/' && line.at(1)=='/') + continue; // line is comment, proceed with next line + + if(line_strip) + { + // strip comments at the end of the line + unsigned int cmpos = 0; + while(true) + { + // note: this must also cover lines like: "out blah \// no-comment // comment" + cmpos = line.find("//",cmpos+1); // start searching at one position later then previous + if(cmpos != std::string::npos) + { + if(line[cmpos-1] == '\\') + continue; + line = line.substr(0,cmpos); + break; + } + else + break; + } + + // strip spaces at the end of the line + while(line.at(line.length()-1) == ' ' || line.at(line.length()-1) == '\t') + { + line.erase(line.length()-1,1); + } + } + + // need to check comments-end seperatedly + if(line=="#ce" || line=="#comments-end") + { + commented = false; + continue; + } + + // commented lines will not be loaded nor have any #-tags evaluated (except the one above!) + if(commented) + continue; + + if(line.at(0)=='#') + { + line.erase(0,1); // remove # + label=line.substr(0,line.find('=',0)); + value=line.substr(line.find('=',0)+1,line.length()); + if(label=="permission") + { + scriptPermissionMap[sn] = atoi(value.c_str()); + } + else if(label=="script") + { + if(!curScript->GetLines()) // delete script if unused + DeleteScript(curScript->GetName()); + sn = stringToLower(value); + _UpdateOrCreateScriptByName(sn); + _DEFSC_DEBUG(printf("DefScript: now loading '%s'\n",sn.c_str())); + curScript=Script[sn]; + } + else if(label=="linestrip") + { + line_strip = isTrue(value); + } + else if(label=="escape-all") + { + escape_all = isTrue(value); + } + else if(line=="debug") + { + if(curScript) + curScript->SetDebug(true); + } + else if(line=="onload") + { + _UpdateOrCreateScriptByName(SN_ONLOAD); + curScript=Script[SN_ONLOAD]; + } + else if(line=="endonload" || line=="/onload") + { + RunScript(SN_ONLOAD,NULL,sn); + DeleteScript(SN_ONLOAD); + curScript=Script[sn]; + } + else if(line=="cs" || line=="comments-start") + { + commented = true; + } + else if(strncmp(line.c_str(),"tag",3)==0 || strncmp(line.c_str(),"mark",4)==0) + { + curScript->AddLine("#" + line); + } + + //... + continue; // line was an option, not script content + } + + if(escape_all) + { + line = EscapeString(line); + } + + // 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"; + else if(memcmp(tline.c_str(),"endif ",6)==0 || memcmp(tline.c_str(),"endif\t",5)==0) tline="endif"; + else if(memcmp(tline.c_str(),"loop ",5)==0 || memcmp(tline.c_str(),"loop\t",5)==0) tline="loop"; + else if(memcmp(tline.c_str(),"endloop ",8)==0 || memcmp(tline.c_str(),"endloop\t",8)==0) tline="endloop"; + if(tline=="else" || tline=="endif" || tline=="loop" || tline=="endloop") + line=tline; + + // check for correct block match + if(memcmp(tline.c_str(),"if ",3)==0) // need to check lowercased line! (IF may be uppercased) + Blocks.push_back(BLOCK_IF); + else if(line=="else") + { + if(Blocks.empty() || Blocks.back()!=BLOCK_IF) + { + cantload=true; + break; + } + } + else if(line=="endif") + { + if(Blocks.empty() || Blocks.back()!=BLOCK_IF) + { + cantload=true; + break; + } + Blocks.pop_back(); + } + else if(line=="loop") + Blocks.push_back(BLOCK_LOOP); + else if(line=="exitloop" && !Blocks.size()) // TODO: check: at least one BLOCK_LOOP must be opened + { + cantload=true; + break; + } + else if(line=="endloop") + { + if(Blocks.empty() || Blocks.back()!=BLOCK_LOOP) + { + cantload=true; + break; + } + Blocks.pop_back(); + } + + // check for correct bracket match + unsigned int bopen=0; + bool mismatch=false; + for(unsigned int bpos=0;bposAddLine(line); + } + f.close(); + if(cantload || Blocks.size()) + { + printf("DefScript: Error loading file '%s'. block mismatch?\n",fn.c_str()); + DeleteScript(sn); + return false; + } + + // ... + return true; +} + + +// --- SECTION FOR THE INDIVIDUAL SCRIPTS IN A PACKAGE --- + +DefScript::DefScript(DefScriptPackage *p) +{ + _parent=p; + scriptname="{NONAME}"; + debugmode=false; +} + +DefScript::~DefScript() +{ + Clear(); +} + +void DefScript::Clear(void) +{ + Line.clear(); +} + +void DefScript::SetDebug(bool d) +{ + debugmode=d; +} + +bool DefScript::GetDebug(void) +{ + return debugmode; +} + +void DefScript::SetName(std::string name) +{ + scriptname=name; +} + +std::string DefScript::GetName(void) +{ + return scriptname; +} + +bool DefScript::AddLine(std::string l){ + if(l.empty()) + return false; + Line.push_back(l); + return true; +} + + +// --- SECTION FOR COMMAND SETS --- + +CmdSet::CmdSet() +{ +} + +CmdSet::~CmdSet() +{ +} + +void CmdSet::Clear() +{ + arg.clear(); + cmd=""; + defaultarg=""; + caller=""; + myname=""; +} + + +// --- FUNCTIONS TO PARSE AND EXECUTE A SCRIPT --- PARENT: DefScriptPackage! + + +// the referred pSet is the parent from which RunScript() has been called +bool DefScriptPackage::BoolRunScript(std::string name, CmdSet *pSet) +{ + DefReturnResult r = RunScript(name,pSet); + return r.ok; +} + +// the referred pSet is the parent from which RunScript() has been called +DefReturnResult DefScriptPackage::RunScript(std::string name, CmdSet *pSet,std::string override_name) +{ + DefReturnResult r; + if( (!ScriptExists(name)) && (!HasFunc(name)) ) + { + r.ok=false; // doesnt exist + r.ret=""; + return r; + } + + DefScript *sc = GetScript(name); + if(!sc) + { + r.ok=false; + r.ret=""; + return r; + } + + if(!override_name.empty()) + name=override_name; + + CmdSet temp; + if(!pSet) + { + pSet = &temp; + } + pSet->caller=pSet->myname; + pSet->myname=name; + + std::deque Blocks; + CmdSet mySet; + std::string line; + + for(unsigned int i=0;iGetLines();i++) + { + line=sc->GetLine(i); + if(line.empty() || line[0] == '#') // skip markers and preload statements if not removed before + continue; + if(line=="else") + { + if(!Blocks.size()) + { + printf("DEBUG: else-block without any block?! [%s:%u]\n",name.c_str(),i); + r.ok=false; + break; + } + Def_Block b=Blocks.back(); + if(b.type==BLOCK_IF && b.istrue) + { + unsigned int other_ifs=0; + for(i=b.startline+1;;i++) + { + if(memcmp(sc->GetLine(i).c_str(),"if ",3)==0) + other_ifs++; + else if(sc->GetLine(i)=="endif") + { + if(!other_ifs) + break; + other_ifs--; + } + } + i--; // next line read will be "endif", decide then what to do + } + continue; + } + 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) + { + printf("DEBUG: endif: closed block is not an if block! [%s:%u]\n",name.c_str(),i); + r.ok=false; + break; + } + Blocks.pop_back(); + continue; + } + else if(line=="loop") + { + Def_Block b; + b.startline=i; + b.type=BLOCK_LOOP; + b.istrue=true; + Blocks.push_back(b); + continue; + } + else if(line=="endloop") + { + if(!Blocks.size()) + { + printf("DEBUG: endloop without any block [%s:%u]\n",name.c_str(),i); + r.ok=false; + break; + } + if(Blocks.back().type!=BLOCK_LOOP) + { + printf("DEBUG: endloop: closed block is not a loop block! [%s:%u]\n",name.c_str(),i); + r.ok=false; + break; + } + i=Blocks.back().startline; // next line executed will be the line after "loop" + continue; + } + //_DEFSC_DEBUG(printf("DefScript before: \"%s\"\n",line.c_str())); + DefXChgResult final=ReplaceVars(line,pSet,0,true); + //_DEFSC_DEBUG(printf("DefScript parsed: \"%s\"\n",final.str.c_str())); + mySet.Clear(); + SplitLine(mySet,final.str); + if(mySet.cmd=="if") + { + Def_Block b; + b.startline=i; + b.type=BLOCK_IF; + b.istrue=isTrue(mySet.defaultarg); + Blocks.push_back(b); + if(!b.istrue) + { + unsigned int other_ifs=0; + for(i=b.startline+1; i < sc->GetLines() ;i++) + { + if(!memcmp(sc->GetLine(i).c_str(),"if ",3)) + other_ifs++; + if(sc->GetLine(i)=="else" || sc->GetLine(i)=="endif") + { + if(!other_ifs) + break; + if(sc->GetLine(i)=="endif") + other_ifs--; + } + } + i--; // next line read will be either "else" or "endif", decide then what to do + } + continue; // and read line after "else" + } + else if(mySet.cmd=="exitloop") + { + // skip some ifs if they are present + while(Blocks.back().type!=BLOCK_LOOP) + Blocks.pop_back(); + Blocks.pop_back(); + unsigned int other_loops=0; + + while(true) + { + if(sc->GetLine(i)=="loop") + other_loops++; + if(sc->GetLine(i)=="endloop") + { + if(!other_loops) + break; + other_loops--; + } + i++; // go until next endloop + } + // next line read will be the line after "endloop" + continue; + } + + mySet.myname=name; + mySet.caller=pSet?pSet->myname:""; + r=Interpret(mySet); + if(r.mustreturn) + { + r.mustreturn=false; + break; + } + } + return r; +} + +DefReturnResult DefScriptPackage::RunSingleLine(std::string line) +{ + DefXChgResult final=ReplaceVars(line,NULL,0,true); + CmdSet Set; + SplitLine(Set,final.str); + return Interpret(Set); +} + +DefReturnResult DefScriptPackage::RunSingleLineFromScript(std::string line, DefScript *pScript) +{ + CmdSet Set; + Set.myname=pScript->GetName(); + DefXChgResult final=ReplaceVars(line,&Set,0,true); + SplitLine(Set,final.str); + return Interpret(Set); +} + +void DefScriptPackage::SplitLine(CmdSet& Set,std::string line) +{ + + unsigned int i=0; + unsigned int bracketsOpen=0,curParam=0; + bool cmdDefined=false; + std::string tempLine; + +// extract cmd+params and txt + for(i=0;i0 && (t[i-1]=='$' || t[i-1]=='?') ) + isVar=true; + if(!bo) + ob=i; + bo++; + } + else if(t[i]=='}') + { + bo--; + if(!bo) + { + if(!isVar) + { + unsigned int blen=i-ob+1; + std::string subStr=t.substr(ob,blen); + std::string retStr=RemoveBracketsFromString(subStr); + t.erase(ob,blen); + t.insert(ob,retStr); + i=ob-1; + + } + isVar=false; + } + } + else if(t[i]=='\\') + { + i++; + continue; + } + + } + + return t; +} + +void DefScriptPackage::RemoveBrackets(CmdSet& Set) +{ + Set.defaultarg=RemoveBracketsFromString(Set.defaultarg); + Set.cmd=RemoveBracketsFromString(Set.cmd); + for(_CmdSetArgMap::iterator i=Set.arg.begin(); i!=Set.arg.end(); i++) + { + i->second=RemoveBracketsFromString(i->second); + } +} + +void DefScriptPackage::UnescapeSet(CmdSet& Set) +{ + Set.defaultarg=UnescapeString(Set.defaultarg); + //Set.cmd=UnescapeString(Set.cmd); // this is done already in SplitLine() ! + for(_CmdSetArgMap::iterator i=Set.arg.begin(); i!=Set.arg.end(); i++) + { + i->second=UnescapeString(i->second); + } +} + + +DefXChgResult DefScriptPackage::ReplaceVars(std::string str, CmdSet *pSet, unsigned char VarType, bool run_embedded) +{ + + unsigned int + openingBracket=0, // defines the position from where the recursive call is started + closingBracket=0, // the closing bracket + bracketsOpen=0, // amount of brackets opened + bLen=0; // the lenth of the string in brackets, e.g. ${abc} == 3 + + unsigned char + nextVar=DEFSCRIPT_NONE; // '$' or '?' + bool + hasChanged=false, // additional helper. once true, xchg.result will be true later also + hasVar=false, // true if openingBracket (= the first bracket) was preceded by '$' or '?' + escaped=false; + + std::string subStr; + DefXChgResult xchg; + + for(unsigned int i=0;i0 && str[i-1]=='$') + { + hasVar=true; + if(!bracketsOpen) + nextVar=DEFSCRIPT_VAR; + } + if(i>0 && str[i-1]=='?') + { + hasVar=true; + if(!bracketsOpen) + nextVar=DEFSCRIPT_FUNC; + } + bracketsOpen++; + } + else if(str[i]=='}') + { + if(bracketsOpen) + bracketsOpen--; + if(!bracketsOpen) + { + closingBracket=i; + if(nextVar==DEFSCRIPT_NONE && VarType==DEFSCRIPT_VAR && !hasVar) // remove brackets in var names, like ${var{ia}ble} + { + str.erase(closingBracket,1); + str.erase(openingBracket,1); + i=openingBracket; // jump to the pos where the opening bracket was + continue; + } + else + { + bLen=closingBracket-openingBracket-1; + subStr=str.substr(openingBracket+1,bLen); + //printf("SUBSTR: \"%s\"\n",subStr.c_str()); + xchg=ReplaceVars(subStr,pSet,nextVar,run_embedded); + if( nextVar==DEFSCRIPT_NONE && hasVar && xchg.changed ) + { + str.erase(openingBracket+1,subStr.length()); + str.insert(openingBracket+1,xchg.str); + hasVar=false; + i-=(subStr.length()+1); + hasChanged=true; + } + else if( nextVar!=DEFSCRIPT_NONE && hasVar && xchg.changed ) + { + str.erase(openingBracket-1,bLen+3); // remove ${...} (+3 because of '${}') + i-=(bLen+2); // adjust position + str.insert(i,xchg.str); + if(str[i]=='\\') // workaround, since i will be increased 1 too high + escaped=true; + hasVar=false; + nextVar=DEFSCRIPT_NONE; + } + } + } + } // end if '}' + else if(str[i]=='\\') // if escape char \ found, skip parsing next char + { + i++; + continue; + } + } // end for + if(!bracketsOpen && VarType!=DEFSCRIPT_NONE) + { + // fix for empty var: ${} + if(str.empty()) + { + xchg.str=""; + xchg.changed=true; + return xchg; + } + if(VarType==DEFSCRIPT_VAR) + { + std::string vname=_NormalizeVarName(str, (pSet==NULL) ? "" : pSet->myname); + if(vname[0]=='@') + { + std::stringstream vns; + std::string subs=vname.substr(1,str.length()-1); + unsigned int vn=atoi( subs.c_str() ); + vns << vn; + if(pSet && vns.str()==subs) // resolve arg macros @0 - @4294967295 + str=pSet->arg[vn]; + else if(pSet && subs=="def") + str=pSet->defaultarg; + else if(pSet && subs=="myname") + str=pSet->myname; + else if(pSet && subs=="cmd") + str=pSet->cmd; + else if(pSet && subs=="caller") + str=pSet->caller; + else if(subs=="n") + str="\n"; + else if(subs=="clock") + { + std::stringstream clock_s; + clock_s << clock(); + str = clock_s.str(); + } + else if(subs=="time") + { + std::stringstream time_s; + time_s << time(NULL); + str = time_s.str(); + } + else if(variables.Exists(vname)) + str=variables.Get(vname); + else + { + // TODO: call custom macro table + //... + str.clear(); + } + xchg.changed=true; + } + else + { + if(variables.Exists(vname)) + { + str=variables.Get(vname); + xchg.changed=true; + } + } + } + else if(VarType==DEFSCRIPT_FUNC) + { + if(run_embedded) + { + DefReturnResult res; + if(pSet) + res=RunSingleLineFromScript(str,GetScript(pSet->myname)); + else + res=RunSingleLine(str); + str=res.ret; // returns empty string on invalid function!! + xchg.result.ok=res.ok; + xchg.changed=true; + //xchg.result.err += res.err; + } + else // if not allowed to run scripts via ?{...} + { + str=""; // just replace with 0 + xchg.changed=true; // yes we have changed something + xchg.result.ok=true; // change ok, insert our (empty) return value + } + } + } + + xchg.str = str; + if(hasChanged) + xchg.changed=true; + return xchg; +} + +std::string DefScriptPackage::_NormalizeVarName(std::string vn_in, std::string sn){ + if(vn_in.empty()) + return vn_in; + std::string vn=vn_in; + bool global=false; + if(sn.empty()) + global = true; + while(true) + { + if(vn.at(0)=='#') + global = true; + if(vn.at(0)=='#' || vn.at(0)==':') + vn.erase(0,1); + else + break; + } + + if( (!global) && (vn.at(0)!='@') ) + vn=sn+"::"+vn; + + return vn; +} + +DefReturnResult DefScriptPackage::Interpret(CmdSet& Set) +{ + // TODO: remove this debug block again as soon as the interpreter bugs are fixed. + _DEFSC_DEBUG + ( + hLogfile << "PARENT: '" << Set.caller << "'\n"; + hLogfile << "cmd = '" << Set.cmd << "'\n"; + unsigned int ctr=0; + for(_CmdSetArgMap::iterator i=Set.arg.begin(); i!=Set.arg.end(); i++) + hLogfile << "arg[" << ctr++ << "] = '" << i->second << "'\n"; + hLogfile << "defaultarg = '" << Set.defaultarg << "'\n\n"; + ); + + DefReturnResult result; + + // first search if the script is defined in the internal functions + for(unsigned int i=0;i<_functable.size();i++) + { + if(Set.cmd==_functable[i].name) + { + if(_functable[i].escape) // if we are going to use a C++ function, unescape the whole set, if supposed to do so. + UnescapeSet(Set); // it will not have any bad side effects, we leave the func within this block! + + result=(this->*(_functable[i].func))(Set); + if(_functable[i].escape) + result.ret = EscapeString(result.ret); // and since we are returning a string into the engine, escape it again, if set. + return result; + } + } + + if(Set.cmd=="return") + { + result.mustreturn=true; + result.ret=Set.defaultarg; + return result; + } + + // if nothing has been found its maybe an external script file to run + result=RunScript(Set.cmd, &Set); + if((!result.ok) )///*&& Script[Set.cmd]->GetDebug() + std::cout << "Could not execute script command '" << Set.cmd << "'\n"; + + + 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; + lists.Assign(SCRIPT_NAMESPACE + sn, &(newscript->Line)); +} + +std::string DefScriptPackage::SecureString(std::string s) +{ + std::string out; + for(unsigned int i = 0; i < s.length(); i++) + { + if(s[i] == '{') + out += '['; + else if(s[i] == '}') + out += ']'; + else + out += s[i]; + } + return out; +} + +// escapes whole string, which can no longer be parsed & interpreted +std::string DefScriptPackage::EscapeString(std::string s) +{ + std::string out; + out.reserve(s.length()+8); + for(unsigned int i = 0; i < s.length(); i++) + { + switch(s[i]) + { + case '{': + case '}': + case '\\': + out += '\\'; + out += s[i]; + break; + + case '\n': + out += "\\n"; + break; + + case '\t': + out += "\\t"; + break; + + default: + out += s[i]; + } + + } + return out; +} + +// converts a string into a printable form, with all escape sequences resolved +std::string DefScriptPackage::UnescapeString(std::string s) +{ + std::string out; + out.reserve(s.length()); + for(unsigned int i = 0; i < s.length(); i++) + { + if(s[i] == '\\' && i+1 < s.length()) + { + switch(s[i+1]) + { // if any of these are found, append to output string and skip this byte + case '{': + case '}': + case '\\': + out += s[++i]; + break; + + case 'n': + out += '\n'; + i++; + break; + + case 't': + out += '\t'; + i++; + break; + + case 'x': + if(i+4 <= s.length()) + { + std::string xd = s.substr(i+2,2); + char c = (char)strtoul(s.substr(i+2,2).c_str(),NULL,16); + out += c; + i += 3; + } + } + } + else + { + out += s[i]; + } + } + return out; +} + +std::string DefScriptPackage::GetUnescapedVar(std::string n) +{ + return UnescapeString(variables.Get(n)); +} + +bool DefScriptPackage::RunScriptIfExists(std::string name,CmdSet *pSet) +{ + if(ScriptExists(name)) + { + return BoolRunScript(name,pSet); + } + return false; +} +>>>>>>> .r255*/ Index: Client/DefScript/DefScriptFileFunctions.cpp =================================================================== --- Client/DefScript/DefScriptFileFunctions.cpp (revision 255) +++ Client/DefScript/DefScriptFileFunctions.cpp (working copy) @@ -1,3 +1,4 @@ +//<<<<<<< .mine #include #include #include @@ -17,6 +18,367 @@ if(fh->is_open()) return ""; std::string mode = stringToLower(Set.arg[1]); + std::ios::openmode m ; + if(mode.find('b') != std::string::npos) + m |= std::ios_base::binary; + if(mode.find('a') != std::string::npos) + m |= std::ios_base::app; + if(mode.find('r') != std::string::npos) + m |= std::ios_base::in; + if(mode.find('w') != std::string::npos) + m |= std::ios_base::out; + + // sort out some possible errors with bad open modes + if(m & std::ios_base::app) + m |= std::ios_base::out; // always open for writing when appending + if(m == 0 || m == std::ios_base::binary) + m |= std::ios_base::out | std::ios_base::in; // if no openmode or binary only specified, use both r+w additionally + + fh->open(Set.defaultarg.c_str(), m); + + // if the file didnt open properly, like if a not existing file opened in read+write mode, + // try fallback mode with write mode only + if(!fh->is_open() && (m & std::ios_base::in)) + { + m &= (~std::ios_base::in); + fh->open(Set.defaultarg.c_str(), m); + } + _DEFSC_DEBUG( if(!fh->is_open()) printf("DefScript: Can't open file '%s'\n",Set.defaultarg.c_str())); + return fh->is_open(); +} + +DefReturnResult DefScriptPackage::func_fclose(CmdSet& Set) +{ + std::fstream *fh = files.GetNoCreate(_NormalizeVarName(Set.defaultarg,Set.myname)); + if(fh) + { + fh->flush(); + fh->close(); + files.DeleteByPtr(fh); + return true; + } + return false; +} + +DefReturnResult DefScriptPackage::func_fisopen(CmdSet& Set) +{ + std::fstream *fh = files.GetNoCreate(_NormalizeVarName(Set.defaultarg,Set.myname)); + return fh && fh->good() && fh->is_open(); +} + +DefReturnResult DefScriptPackage::func_feof(CmdSet& Set) +{ + std::fstream *fh = files.GetNoCreate(_NormalizeVarName(Set.defaultarg,Set.myname)); + return (!fh) || fh->eof(); +} + +DefReturnResult DefScriptPackage::func_frpos(CmdSet& Set) +{ + std::fstream *fh = files.GetNoCreate(_NormalizeVarName(Set.defaultarg,Set.myname)); + if(!fh) + return ""; + return toString((uint64)fh->tellg()); +} + +DefReturnResult DefScriptPackage::func_fwpos(CmdSet& Set) +{ + std::fstream *fh = files.GetNoCreate(_NormalizeVarName(Set.defaultarg,Set.myname)); + if(!fh) + return ""; + return toString((uint64)fh->tellp()); +} + +DefReturnResult DefScriptPackage::func_fdel(CmdSet& Set) +{ + return (remove(Set.defaultarg.c_str()) == 0); +} + +DefReturnResult DefScriptPackage::func_fflush(CmdSet& Set) +{ + std::fstream *fh = files.GetNoCreate(_NormalizeVarName(Set.defaultarg,Set.myname)); + if(fh && fh->is_open()) + fh->flush(); + return ""; +} + +DefReturnResult DefScriptPackage::func_fwrite(CmdSet& Set) +{ + std::fstream *fh = files.GetNoCreate(_NormalizeVarName(Set.arg[0],Set.myname)); + if(fh && fh->is_open()) + { + *fh << Set.defaultarg; + return true; + } + return false; +} + +DefReturnResult DefScriptPackage::func_fread(CmdSet& Set) +{ + std::string fn = _NormalizeVarName(Set.arg[0],Set.myname); + std::fstream *fh = files.GetNoCreate(fn); + if(fh && fh->is_open()) + { + uint64 bytes; + uint64 read = 0; + if(stringToLower(Set.defaultarg) == "all") + { + std::fstream::pos_type begin_pos, end_pos, old_pos = fh->tellg(); + fh->seekg(0, std::ios_base::beg); + begin_pos=fh->tellg(); + fh->seekg(0, std::ios_base::end); + end_pos = fh->tellg(); + fh->seekg(old_pos, std::ios_base::beg); + bytes = (end_pos - begin_pos); + } + else + bytes = toUint64(Set.defaultarg); + std::string ret; + ret.reserve(bytes); + for(uint64 i = 0; i < bytes && !fh->eof(); i++) + { + ret += fh->get(); + read++; + } + return toString(read); + } + return ""; +} + +DefReturnResult DefScriptPackage::func_freadb(CmdSet& Set) +{ + std::fstream *fh = files.GetNoCreate(_NormalizeVarName(Set.arg[0],Set.myname)); + if(fh && fh->is_open()) + { + std::string t = stringToLower(Set.defaultarg); + if(t == "string") + { + std::string ret; + while(char c = fh->get() && !fh->eof()) + ret += c; + return ret; + } + else if(t == "strnz") + { + uint32 bytes = (uint32)toUint64(Set.arg[1]); + if(bytes) + { + std::string g; + for(uint32 i = 0; i < bytes; i++) + { + g += fh->get(); + } + return g; + } + return ""; + } + FH_MACRO_READ_I(fh,t,uint8); + FH_MACRO_READ_I(fh,t,uint16); + FH_MACRO_READ_I(fh,t,uint32); + FH_MACRO_READ_I(fh,t,uint64); + FH_MACRO_READ_F(fh,t,float); + FH_MACRO_READ_F(fh,t,double); + } + return ""; +} + +DefReturnResult DefScriptPackage::func_fwriteb(CmdSet& Set) +{ + std::fstream *fh = files.GetNoCreate(_NormalizeVarName(Set.arg[0],Set.myname)); + if(fh && fh->is_open()) + { + std::string t = stringToLower(Set.arg[1]); + if(t == "string") + { + fh->write(Set.defaultarg.c_str(), Set.defaultarg.size() + 1); // also put \0 + return true; + } + else if(t == "strnz") + { + fh->write(Set.defaultarg.c_str(), Set.defaultarg.size()); // skip \0 + return true; + } + else if(t == "uint8") + { + uint8 o = (uint8)toUint64(Set.defaultarg); + fh->write((char*)&o,sizeof(uint8)); + return true; + } + else if(t == "uint16") + { + uint16 o = (uint16)toUint64(Set.defaultarg); + fh->write((char*)&o,sizeof(uint16)); + return true; + } + else if(t == "uint32") + { + uint32 o = (uint32)toUint64(Set.defaultarg); + fh->write((char*)&o,sizeof(uint32)); + return true; + } + else if(t == "uint64") + { + uint64 o = toUint64(Set.defaultarg); + fh->write((char*)&o,sizeof(uint64)); + return true; + } + else if(t == "float") + { + float o = (float)toNumber(Set.defaultarg); + fh->write((char*)&o,sizeof(float)); + return true; + } + else if(t == "double") + { + double o = (double)toNumber(Set.defaultarg); + fh->write((char*)&o,sizeof(double)); + return true; + } + + } + return false; +} + +DefReturnResult DefScriptPackage::func_fwritebb(CmdSet& Set) +{ + std::fstream *fh = files.GetNoCreate(_NormalizeVarName(Set.arg[0],Set.myname)); + if(fh && fh->is_open()) + { + ByteBuffer *bb = bytebuffers.GetNoCreate(_NormalizeVarName(Set.defaultarg,Set.myname)); + if(bb) + { + uint32 bytes = (uint32)toUint64(Set.arg[1]); + if(!bytes) + { + bytes = bb->size(); + } + if(bytes) + { + fh->write((char*)bb->contents(), bytes); + } + return toString((uint64)bytes); + } + } + return ""; +} + +DefReturnResult DefScriptPackage::func_freadbb(CmdSet& Set) +{ + std::fstream *fh = files.GetNoCreate(_NormalizeVarName(Set.arg[0],Set.myname)); + if(fh && fh->is_open()) + { + ByteBuffer *bb = bytebuffers.Get(_NormalizeVarName(Set.defaultarg,Set.myname)); + uint32 bytes = (uint32)toUint64(Set.arg[1]); + if(!bytes) + { + // get amount of remaining bytes + std::ios::pos_type oldpos = fh->tellg(); + fh->seekg(0,std::ios_base::end); + bytes = uint32(fh->tellg() - oldpos); + fh->seekg(oldpos,std::ios_base::beg); + } + if(bytes) + { + bb->resize(bb->size() + bytes); + fh->read((char*)bb->contents(), bytes); + } + return toString((uint64)bytes); + } + return ""; +} + +DefReturnResult DefScriptPackage::func_fsize(CmdSet& Set) +{ + std::ifstream f; + f.open(Set.defaultarg.c_str(), std::ios_base::binary | std::ios_base::in); + if (!f.good() || f.eof() || !f.is_open()) { return ""; } + f.seekg(0, std::ios_base::beg); + std::ifstream::pos_type begin_pos = f.tellg(), end_pos; + f.seekg(0, std::ios_base::end); + end_pos = f.tellg(); + f.close(); + return toString((uint64)(end_pos - begin_pos)); +} + +DefReturnResult DefScriptPackage::func_freadline(CmdSet& Set) +{ + std::fstream *fh = files.GetNoCreate(_NormalizeVarName(Set.defaultarg,Set.myname)); + if(fh && fh->is_open()) + { + char c; + std::string line; + line.reserve(30); // rough guess to speedup appending + while(!fh->eof()) + { + c = fh->get(); + if(c == '\n' || fh->eof()) + return line; + else + line += c; + } + } + return ""; +} + +DefReturnResult DefScriptPackage::func_fseekw(CmdSet& Set) +{ + std::fstream *fh = files.GetNoCreate(_NormalizeVarName(Set.arg[0],Set.myname)); + if(fh && fh->is_open()) + { + std::string w = stringToLower(Set.arg[1]); + uint32 pos = (uint32)toUint64(Set.defaultarg); + if(w.empty() || w == "begin") + { + fh->seekp(pos, std::ios_base::beg); + return true; + } + else if(w == "end") + { + fh->seekp(pos, std::ios_base::end); + return true; + } + } + return false; +} + +DefReturnResult DefScriptPackage::func_fseekr(CmdSet& Set) +{ + std::fstream *fh = files.GetNoCreate(_NormalizeVarName(Set.arg[0],Set.myname)); + if(fh && fh->is_open()) + { + std::string w = stringToLower(Set.arg[1]); + uint32 pos = (uint32)toUint64(Set.defaultarg); + if(w.empty() || w == "begin") + { + fh->seekg(pos, std::ios_base::beg); + return true; + } + else if(w == "end") + { + fh->seekg(pos, std::ios_base::end); + return true; + } + } + return false; +/*=======/* +#include +#include +#include +#include +#include "DefScript.h" +#include "DefScriptTools.h" +#include "SysDefs.h" + +using namespace DefScriptTools; + +#define FH_MACRO_READ_I(_f,_sty,_ty) if(_sty==(#_ty)) { _ty _in; (_f)->read((char*)&_in,sizeof(_ty)); return toString((uint64)_in); } +#define FH_MACRO_READ_F(_f,_sty,_ty) if(_sty==(#_ty)) { _ty _in; (_f)->read((char*)&_in,sizeof(_ty)); return toString((ldbl)_in); } + +DefReturnResult DefScriptPackage::func_fopen(CmdSet& Set) +{ + std::fstream *fh = files.Get(_NormalizeVarName(Set.arg[0],Set.myname)); + if(fh->is_open()) + return ""; + std::string mode = stringToLower(Set.arg[1]); std::ios::openmode m = 0; if(mode.find('b') != std::string::npos) m |= std::ios_base::binary; @@ -232,7 +594,7 @@ fh->write((char*)&o,sizeof(double)); return true; } - + } return false; } @@ -358,4 +720,5 @@ } } return false; -} \ No newline at end of file +>>>>>>> .r255*/ +} Index: Client/DefScript/DefScriptFunctions.cpp =================================================================== --- Client/DefScript/DefScriptFunctions.cpp (revision 255) +++ Client/DefScript/DefScriptFunctions.cpp (working copy) @@ -53,7 +53,7 @@ } 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; @@ -127,7 +127,7 @@ variables.Set(vname,vval); r.ret=vval; - + DefScript *sc = GetScript(Set.myname); if(sc && sc->GetDebug()) printf("VAR: %s = '%s'\n",vname.c_str(),vval.c_str()); @@ -199,7 +199,8 @@ } std::string vname=_NormalizeVarName(Set.arg[0], Set.myname); - ldbl a=toNumber(variables.Get(vname)); + std::string temp = variables.Get(vname); + ldbl a=toNumber(temp); ldbl b=toNumber(Set.defaultarg); a+=b; variables.Set(vname,toString(a)); @@ -216,7 +217,8 @@ } std::string vname=_NormalizeVarName(Set.arg[0], Set.myname); - ldbl a=toNumber(variables.Get(vname)); + std::string temp = variables.Get(vname); + ldbl a=toNumber(temp); ldbl b=toNumber(Set.defaultarg); a-=b; variables.Set(vname,toString(a)); @@ -233,7 +235,8 @@ } std::string vname=_NormalizeVarName(Set.arg[0], Set.myname); - ldbl a=toNumber(variables.Get(vname)); + std::string temp = variables.Get(vname); + ldbl a=toNumber(temp); ldbl b=toNumber(Set.defaultarg); a*=b; variables.Set(vname,toString(a)); @@ -250,7 +253,8 @@ } std::string vname=_NormalizeVarName(Set.arg[0], Set.myname); - ldbl a=toNumber(variables.Get(vname)); + std::string temp = variables.Get(vname); + ldbl a=toNumber(temp); ldbl b=toNumber(Set.defaultarg); if(b==0) a=0; @@ -270,7 +274,9 @@ } std::string vname=_NormalizeVarName(Set.arg[0], Set.myname); - uint64 a=toUint64(variables.Get(vname)); + std::string temp = variables.Get(vname); + + uint64 a=toUint64(temp); uint64 b=toUint64(Set.defaultarg); if(b==0) a=0; @@ -290,7 +296,8 @@ } std::string vname=_NormalizeVarName(Set.arg[0], Set.myname); - ldbl a=toNumber(variables.Get(vname)); + std::string temp = variables.Get(vname); + ldbl a=toNumber(temp); ldbl b=toNumber(Set.defaultarg); a=pow(a,b); variables.Set(vname,toString(a)); @@ -307,7 +314,8 @@ } std::string vname=_NormalizeVarName(Set.arg[0], Set.myname); - uint64 a=toUint64(variables.Get(vname)); + std::string temp = variables.Get(vname); + uint64 a=toUint64(temp); uint64 b=toUint64(Set.defaultarg); a|=b; variables.Set(vname,toString(a)); @@ -324,7 +332,8 @@ } std::string vname=_NormalizeVarName(Set.arg[0], Set.myname); - uint64 a=toUint64(variables.Get(vname)); + std::string temp = variables.Get(vname); + uint64 a=toUint64(temp); uint64 b=toUint64(Set.defaultarg); a&=b; variables.Set(vname,toString(a)); @@ -341,7 +350,8 @@ } std::string vname=_NormalizeVarName(Set.arg[0], Set.myname); - uint64 a=toUint64(variables.Get(vname)); + std::string temp = variables.Get(vname); + uint64 a=toUint64(temp); uint64 b=toUint64(Set.defaultarg); a^=b; variables.Set(vname,toString(a)); @@ -523,7 +533,7 @@ unsigned int pos = Set.defaultarg.find(Set.arg[0],(unsigned int)toNumber(Set.arg[1])); if(pos == std::string::npos) return ""; - return toString((uint64)pos); + return toString((uint64)pos); } DefReturnResult DefScriptPackage::func_scriptexists(CmdSet& Set) Index: Client/GUI/SceneWorld.cpp =================================================================== --- Client/GUI/SceneWorld.cpp (revision 255) +++ Client/GUI/SceneWorld.cpp (working copy) @@ -13,7 +13,7 @@ #include "World.h" #define MOUSE_SENSIVITY 0.5f -#define ANGLE_STEP (M_PI/180) +#define ANGLE_STEP (M_PI/180) #define DEG_TO_RAD(x) ((x)*ANGLE_STEP) #define RAD_TO_DEG(x) ((x)/ANGLE_STEP) #define RAD_FIX(x) ( (x)>(2*M_PI) ? ((x)-(2*M_PI)) : (x) ) @@ -54,7 +54,7 @@ debugText = guienv->addStaticText(L"< debug text >",rect(0,0,driver->getScreenSize().Width,30),true,true,0,-1,true); - smgr->addSkyDomeSceneNode(driver->getTexture("data/misc/sky.jpg"),64,64,1.0f,2.0f); + smgr->addSkyDomeSceneNode(driver->getTexture("data/misc/sky.jpg"),64,64,1.0f,2.0f); driver->setFog(video::SColor(0,100,101,190), true, fogdist, fogdist + 30, 0.02f); @@ -101,7 +101,7 @@ else camera->turnLeft(timediff_f * M_PI * 25); } - + if(eventrecv->key.pressed_once(KEY_BACK)) { debugmode = !debugmode; @@ -160,7 +160,7 @@ //device->getCursorControl()->setVisible(true); mouse_pos = device->getCursorControl()->getPosition(); } - + // camera height control if (eventrecv->mouse.wheel < 10) eventrecv->mouse.wheel = 10; camera->setHeight( eventrecv->mouse.wheel + terrain->getHeight(camera->getPosition()) ); @@ -177,7 +177,7 @@ str += " ## HEAD: "; str += DEG_TO_RAD(camera->getHeading()); str += L" Pos: "; - str = ((((((str + wp.x) + L" | ") + wp.y) + L" | ") + wp.z) + L" | OR:") + wp.o; + str = ((((((str + wp.x) + L" | ") + wp.y) + L" | ") + wp.z) + L" | OR:") + wp.o; str += L" -- Terrain: Sectors: "; str += (int)terrain->getSectorsRendered(); str += L" / "; @@ -228,7 +228,7 @@ { // check if we changed the maptile if(map_gridX == mapmgr->GetGridX() && map_gridY == mapmgr->GetGridY()) - return; // grid not changed, not necessary to update tile data + return; // grid not changed, not necessary to update tile data // ... if changed, do necessary stuff... map_gridX = mapmgr->GetGridX(); @@ -297,8 +297,8 @@ for(s32 i=0; igetSize().Width+1; i++) { curheight = terrain->getHeight(i,j); - highest = max(highest,curheight); - lowest = min(lowest,curheight); + highest = std::max(highest,curheight); + lowest = std::min(lowest,curheight); } // f32 heightdiff = highest - lowest; @@ -359,10 +359,10 @@ cam.X = tile->GetBaseX() - wp.x + (tsize.Width * UNITSIZE); cam.Z = tile->GetBaseX() - wp.y + (tsize.Height * UNITSIZE); float heading = RAD_TO_DEG(((M_PI*3.0f)/2.0f) - wp.o); - float heading_diff = camera->getHeading() - heading; + float heading_diff = camera->getHeading() - heading; logdebug("Setting camera to x: %3f y: %3f z:%3f head: %3f", cam.X, cam.Y, cam.Z, heading); camera->turnLeft(heading_diff); - + // TODO: // - correct the above formulas // - find out terrain height where the camera should be set Index: Client/GUI/MInput.h =================================================================== --- Client/GUI/MInput.h (revision 255) +++ Client/GUI/MInput.h (working copy) @@ -124,7 +124,7 @@ mouse.right = false; } - virtual bool OnEvent(SEvent event) + virtual bool OnEvent(const SEvent& event) { bool value = false; Index: Client/GUI/ShTlTerrainSceneNode.cpp =================================================================== --- Client/GUI/ShTlTerrainSceneNode.cpp (revision 255) +++ Client/GUI/ShTlTerrainSceneNode.cpp (working copy) @@ -188,8 +188,8 @@ Material[0].TextureLayer[1].TextureWrap = video::ETC_CLAMP_TO_EDGE; */ // irrlicht 1.3: - Material[0].Textures[1] = CTexture; - Material[0].TextureWrap[1] = video::ETC_CLAMP_TO_EDGE; + Material[0].TextureLayer[1].Texture = CTexture; + Material[0].TextureLayer[1].TextureWrap = video::ETC_CLAMP_TO_EDGE; // setup UV coordinates of vertices on 2nd texture layer f32 ax = (f32)MeshSize.Width / CTexture->getSize().Width / MeshSize.Width; Index: Client/World/MovementMgr.cpp =================================================================== --- Client/World/MovementMgr.cpp (revision 255) +++ Client/World/MovementMgr.cpp (working copy) @@ -29,12 +29,13 @@ wp << _moveFlags; wp << (uint8)0; // unk wp << getMSTime(); - wp << _mychar->GetPosition(); + WorldPosition temp = _mychar->GetPosition(); + wp << temp; // TODO: transport not yet handled/done if(_moveFlags & MOVEMENTFLAG_ONTRANSPORT) { wp << (uint64)0; // transport guid - wp << WorldPosition(); // transport position +// wp << WorldPosition(); // transport position wp << getMSTime(); // transport time (??) } // TODO: swimming not yet done @@ -59,7 +60,8 @@ if(_movemode == MOVEMODE_AUTO) { - WorldPosition& pos = _mychar->GetPosition(); + + WorldPosition pos = _mychar->GetPosition(); float turnspeed = _mychar->GetSpeed(MOVE_TURN) / 1000.0f * timediff; float runspeed = _mychar->GetSpeed(MOVE_RUN) / 1000.0f * timediff; float movespeed = runspeed; // or use walkspeed, depending on setting. for now use only runspeed