* DefScript: added ByteBuffer functionality. big thx to bLuma for patch!
This commit is contained in:
parent
83780495a8
commit
e7c3f27f54
29
bin/scripts/bbExample.def
Normal file
29
bin/scripts/bbExample.def
Normal file
@ -0,0 +1,29 @@
|
||||
#script=bbexample
|
||||
bbinit,50 mybb
|
||||
bbappend,mybb,uint32 123456
|
||||
bbappend,mybb,uint8 255
|
||||
bbappend,mybb,float 3.141592
|
||||
bbappend,mybb,string Hello World!
|
||||
bbappend,mybb,uint32 0xDEADBABE
|
||||
bbappend,mybb,uint16 1024
|
||||
bbappend,mybb,uint64 0
|
||||
bbappend,mybb,uint64 0
|
||||
|
||||
out ByteBuffer Example:
|
||||
bbhexlike mybb
|
||||
bbtextlike mybb
|
||||
|
||||
bbrpos,mybb 0
|
||||
out Reading uint32 - ?{bbread,mybb uint32}
|
||||
out Reading uint8 - ?{bbread,mybb uint8}
|
||||
out Reading float - ?{bbread,mybb float}
|
||||
out Reading string - ?{bbread,mybb string}
|
||||
set,t ?{bbread,mybb uint32}
|
||||
out Reading uint32 - ${t} hex: ?{tohex ${t}}
|
||||
out Reading uint16 - ?{bbread,mybb uint16}
|
||||
out skipping 16 bytes
|
||||
bbread,mybb uint64
|
||||
bbread,mybb uint64
|
||||
out read from invalid position: result='?{bbread,mybb uint8}'
|
||||
|
||||
bbdelete mybb
|
||||
@ -44,24 +44,24 @@ DefScriptPackage::~DefScriptPackage()
|
||||
void DefScriptPackage::Log(const char* fmt,...)
|
||||
{
|
||||
va_list ap;
|
||||
va_start(ap, fmt);
|
||||
(*_slog)(fmt,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_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_start(ap, fmt);
|
||||
(*_serrorlog)(fmt,ap);
|
||||
va_end(ap);
|
||||
}
|
||||
|
||||
@ -145,6 +145,18 @@ void DefScriptPackage::_InitFunctions(void)
|
||||
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);
|
||||
|
||||
}
|
||||
|
||||
void DefScriptPackage::AddFunc(std::string n,DefReturnResult (DefScriptPackage::*f)(CmdSet& Set), bool esc)
|
||||
|
||||
@ -7,6 +7,7 @@
|
||||
#include <deque>
|
||||
#include <fstream>
|
||||
#include "VarSet.h"
|
||||
#include "ByteBuffer.h"
|
||||
#include "DynamicEvent.h"
|
||||
#include "TypeStorage.h"
|
||||
#include "DefScriptTools.h"
|
||||
@ -132,6 +133,7 @@ public:
|
||||
bool HasFunc(std::string);
|
||||
void DelFunc(std::string);
|
||||
TypeStorage<DefList> lists;
|
||||
TypeStorage<ByteBuffer> bytebuffers;
|
||||
std::string SecureString(std::string);
|
||||
std::string EscapeString(std::string);
|
||||
std::string UnescapeString(std::string);
|
||||
@ -236,6 +238,17 @@ private:
|
||||
DefReturnResult func_lerase(CmdSet&);
|
||||
DefReturnResult func_lsort(CmdSet&);
|
||||
|
||||
// ByteBuffer functions
|
||||
DefReturnResult func_bbinit(CmdSet&);
|
||||
DefReturnResult func_bbdelete(CmdSet&);
|
||||
DefReturnResult func_bbappend(CmdSet&);
|
||||
DefReturnResult func_bbread(CmdSet&);
|
||||
DefReturnResult func_bbsetrpos(CmdSet&);
|
||||
DefReturnResult func_bbsetwpos(CmdSet&);
|
||||
DefReturnResult func_bbhexlike(CmdSet&);
|
||||
DefReturnResult func_bbtextlike(CmdSet&);
|
||||
DefReturnResult func_bbsize(CmdSet&);
|
||||
|
||||
// setup own function declarations here
|
||||
# include "DefScriptInterfaceInclude.h"
|
||||
|
||||
@ -246,4 +259,4 @@ private:
|
||||
|
||||
|
||||
|
||||
#endif
|
||||
#endif
|
||||
|
||||
157
src/Client/DefScript/DefScriptBBFunctions.cpp
Normal file
157
src/Client/DefScript/DefScriptBBFunctions.cpp
Normal file
@ -0,0 +1,157 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string>
|
||||
#include <algorithm>
|
||||
#include "DefScript.h"
|
||||
#include "DefScriptTools.h"
|
||||
#include "SysDefs.h"
|
||||
#include "ByteBuffer.h"
|
||||
|
||||
using namespace DefScriptTools;
|
||||
|
||||
// Helper macros for insert/extract various datatypes that need type casting
|
||||
#define BB_CAN_READ(bytebuffer, _ty) ((*(bytebuffer)).size() - (*(bytebuffer)).rpos() >= sizeof(_ty))
|
||||
#define BB_MACRO_INSERT(bytebuffer, _sty, _ty) if( (_sty)==(#_ty) ) { *(bytebuffer) << (_ty)toNumber(Set.defaultarg); return true; }
|
||||
#define BB_MACRO_EXTRACT_I(bytebuffer, _sty, _ty) if( (_sty)==(#_ty) && BB_CAN_READ(bytebuffer,_ty) ) {_ty _var; *(bytebuffer) >> _var; return DefScriptTools::toString((uint64)_var); }
|
||||
#define BB_MACRO_EXTRACT_F(bytebuffer, _sty, _ty) if( (_sty)==(#_ty) && BB_CAN_READ(bytebuffer,_ty) ) {_ty _var; *(bytebuffer) >> _var; return DefScriptTools::toString((ldbl)_var); }
|
||||
|
||||
|
||||
// Initializes bytebuffer with initial capacity (reserved, not resized)
|
||||
// @def - bytebuffer identifier
|
||||
// @0 - if set, initial capacity
|
||||
DefReturnResult DefScriptPackage::func_bbinit(CmdSet& Set)
|
||||
{
|
||||
ByteBuffer *bb = bytebuffers.Get(_NormalizeVarName(Set.defaultarg,Set.myname));
|
||||
bb->clear(); // also resets wpos & rpos
|
||||
if (!Set.arg[0].empty())
|
||||
bb->reserve((size_t)DefScriptTools::toUint64(Set.arg[0]));
|
||||
return true;
|
||||
}
|
||||
|
||||
// Cleans memory and deletes ByteBuffer from storage
|
||||
DefReturnResult DefScriptPackage::func_bbdelete(CmdSet& Set)
|
||||
{
|
||||
bytebuffers.Delete(_NormalizeVarName(Set.defaultarg,Set.myname));
|
||||
return true;
|
||||
}
|
||||
|
||||
// Appends data to ByteBuffer
|
||||
// @def - data
|
||||
// @0 - bytebuffer identifier
|
||||
// @1 - datatype of added data (uint8,uint16,uint32,uint64,float,double,string)
|
||||
DefReturnResult DefScriptPackage::func_bbappend(CmdSet& Set)
|
||||
{
|
||||
ByteBuffer *bb = bytebuffers.Get(_NormalizeVarName(Set.arg[0],Set.myname));
|
||||
|
||||
std::string dtype = DefScriptTools::stringToLower(Set.arg[1]);
|
||||
|
||||
if (dtype == "string")
|
||||
{
|
||||
*bb << Set.defaultarg;
|
||||
return true;
|
||||
}
|
||||
|
||||
BB_MACRO_INSERT(bb, dtype, uint8);
|
||||
BB_MACRO_INSERT(bb, dtype, uint16);
|
||||
BB_MACRO_INSERT(bb, dtype, uint32);
|
||||
BB_MACRO_INSERT(bb, dtype, uint64);
|
||||
BB_MACRO_INSERT(bb, dtype, float);
|
||||
BB_MACRO_INSERT(bb, dtype, double);
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
// Extracts data from bytebuffer
|
||||
// @def - datatype to extract (uint8,uint16,uint32,uint64,float,double,string)
|
||||
// @0 - bytebuffer identifier
|
||||
DefReturnResult DefScriptPackage::func_bbread(CmdSet& Set)
|
||||
{
|
||||
ByteBuffer *bb = bytebuffers.GetNoCreate(_NormalizeVarName(Set.arg[0],Set.myname));
|
||||
if (!bb)
|
||||
return false;
|
||||
|
||||
std::string dtype = DefScriptTools::stringToLower(Set.defaultarg);
|
||||
|
||||
if (dtype == "string") {
|
||||
std::string g;
|
||||
*bb >> g;
|
||||
return g;
|
||||
}
|
||||
|
||||
BB_MACRO_EXTRACT_I(bb, dtype, uint8);
|
||||
BB_MACRO_EXTRACT_I(bb, dtype, uint16);
|
||||
BB_MACRO_EXTRACT_I(bb, dtype, uint32);
|
||||
BB_MACRO_EXTRACT_I(bb, dtype, uint64);
|
||||
BB_MACRO_EXTRACT_F(bb, dtype, float);
|
||||
BB_MACRO_EXTRACT_F(bb, dtype, double);
|
||||
|
||||
return ""; // if extraction unsuccessful, return empty string
|
||||
}
|
||||
|
||||
// Sets read position in bytebuffer
|
||||
// @0 - bytebuffer identifier
|
||||
// @def - setted rpos (if "end", automatically set at end)
|
||||
DefReturnResult DefScriptPackage::func_bbsetrpos(CmdSet& Set)
|
||||
{
|
||||
ByteBuffer *bb = bytebuffers.GetNoCreate(_NormalizeVarName(Set.arg[0],Set.myname));
|
||||
if (!bb)
|
||||
return false;
|
||||
|
||||
if (Set.defaultarg == "end")
|
||||
bb->rpos( bb->size() );
|
||||
else
|
||||
bb->rpos( (unsigned int)toNumber(Set.defaultarg) );
|
||||
return true;
|
||||
}
|
||||
|
||||
// Sets write position in bytebuffer
|
||||
// @0 - bytebuffer identifier
|
||||
// @def - setted wpos (if "end", automatically set at end)
|
||||
DefReturnResult DefScriptPackage::func_bbsetwpos(CmdSet& Set)
|
||||
{
|
||||
ByteBuffer *bb = bytebuffers.GetNoCreate(_NormalizeVarName(Set.arg[0],Set.myname));
|
||||
if (!bb)
|
||||
return false;
|
||||
|
||||
if (Set.defaultarg == "end")
|
||||
bb->wpos( bb->size() );
|
||||
else
|
||||
bb->wpos( (unsigned int)toNumber(Set.defaultarg) );
|
||||
return true;
|
||||
}
|
||||
|
||||
// Hexlike output of bytebuffer to console
|
||||
// @def - bytebuffer identifier
|
||||
DefReturnResult DefScriptPackage::func_bbhexlike(CmdSet& Set)
|
||||
{
|
||||
ByteBuffer *bb = bytebuffers.GetNoCreate(_NormalizeVarName(Set.defaultarg,Set.myname));
|
||||
if (!bb)
|
||||
return false;
|
||||
|
||||
bb->hexlike();
|
||||
return true;
|
||||
}
|
||||
|
||||
// Textlike output of bytebuffer to console
|
||||
// @def - bytebuffer identifier
|
||||
DefReturnResult DefScriptPackage::func_bbtextlike(CmdSet& Set)
|
||||
{
|
||||
ByteBuffer *bb = bytebuffers.GetNoCreate(_NormalizeVarName(Set.defaultarg,Set.myname));
|
||||
if (!bb)
|
||||
return false;
|
||||
|
||||
bb->textlike();
|
||||
return true;
|
||||
}
|
||||
|
||||
// Returns size of bytebuffer
|
||||
// @def - bytebuffer identifier
|
||||
DefReturnResult DefScriptPackage::func_bbsize(CmdSet& Set)
|
||||
{
|
||||
ByteBuffer *bb = bytebuffers.GetNoCreate(_NormalizeVarName(Set.defaultarg,Set.myname));
|
||||
if (!bb)
|
||||
return false;
|
||||
|
||||
return DefScriptTools::toString((uint64)bb->size());
|
||||
}
|
||||
|
||||
@ -1,6 +1,8 @@
|
||||
#ifndef DEFSCRIPTDEFINES_H
|
||||
#define DEFSCRIPTDEFINES_H
|
||||
|
||||
#include "SysDefs.h"
|
||||
|
||||
// use this directive to check if the parser is working correctly
|
||||
#define DEF_DEBUG_SCRIPT_CALLS
|
||||
|
||||
@ -12,6 +14,8 @@
|
||||
# define _DEFSC_DEBUG_LOG
|
||||
#endif
|
||||
|
||||
// now included in SysDefs.h
|
||||
/*
|
||||
#if COMPILER == COMPILER_MICROSOFT
|
||||
typedef __int64 int64;
|
||||
typedef unsigned __int64 uint64;
|
||||
@ -20,6 +24,7 @@ typedef __int64_t int64;
|
||||
typedef __uint64_t uint64;
|
||||
// TODO: correct ATOI64 for linux if necessary
|
||||
#endif
|
||||
*/
|
||||
|
||||
enum VariableType
|
||||
{
|
||||
|
||||
@ -218,6 +218,9 @@
|
||||
Name="VCCustomBuildTool"/>
|
||||
</FileConfiguration>
|
||||
</File>
|
||||
<File
|
||||
RelativePath=".\Client\DefScript\DefScriptBBFunctions.cpp">
|
||||
</File>
|
||||
<File
|
||||
RelativePath=".\Client\DefScript\DefScriptDefines.h">
|
||||
</File>
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user