22 #include "../bscript/berror.h" 23 #include "../bscript/bobject.h" 24 #include "../bscript/executor.h" 25 #include "../bscript/impstr.h" 26 #include "../bscript/objmembers.h" 27 #include "../bscript/objmethods.h" 28 #include "../clib/clib_endian.h" 29 #include "../clib/stlutil.h" 44 using namespace Bscript;
65 buffer.resize( length, 0 );
84 if ( objmember !=
nullptr )
97 return new BError(
"SendPacket requires 1 parameter." );
103 if ( chr !=
nullptr )
106 return new BLong( 0 );
111 if ( client !=
nullptr )
116 static_cast<int>(
buffer.size() ) );
117 return new BLong( 1 );
120 return new BLong( 0 );
129 return new BError(
"SendAreaPacket requires 4 parameters." );
130 unsigned short x, y, range;
137 return new BError(
"Realm not found" );
138 if ( !realm->
valid( x, y, 0 ) )
139 return new BError(
"Invalid Coordinates for realm" );
141 unsigned short num_sent_to = 0;
145 static_cast<int>(
buffer.size() ) );
148 return new BLong( num_sent_to );
156 return new BError(
"GetInt8 requires 1 parameter." );
157 unsigned short offset;
160 if ( offset >=
buffer.size() )
161 return new BError(
"Offset too high" );
163 return new BLong( *data );
171 return new BError(
"GetInt16 requires 1 parameter." );
172 unsigned short offset;
175 if ( offset >
buffer.size() -
sizeof(
u16 ) )
176 return new BError(
"Offset too high" );
186 return new BError(
"GetInt32 requires 1 parameter." );
187 unsigned short offset;
190 if ( offset >
buffer.size() -
sizeof(
u32 ) )
191 return new BError(
"Offset too high" );
201 return new BError(
"GetInt16Flipped requires 1 parameter." );
202 unsigned short offset;
205 if ( offset >
buffer.size() -
sizeof(
u16 ) )
206 return new BError(
"Offset too high" );
216 return new BError(
"GetInt32Flipped requires 1 parameter." );
217 unsigned short offset;
220 if ( offset >
buffer.size() -
sizeof(
u32 ) )
221 return new BError(
"Offset too high" );
231 return new BError(
"GetString requires 2 parameter." );
232 unsigned short offset, len;
235 if ( ( offset >=
buffer.size() ) ||
236 ( static_cast<u16>( offset + len ) >
238 return new BError(
"Offset too high" );
240 const char* str_offset =
reinterpret_cast<const char*
>( &
buffer[offset] );
244 while ( real_len < len && *( str_offset + real_len ) )
247 return new String( str_offset, real_len );
255 return new BError(
"GetUnicodeString requires 2 parameter." );
256 unsigned short offset, len;
260 if ( ( offset >=
buffer.size() ) ||
261 ( static_cast<u16>( offset + len * 2 ) >
263 return new BError(
"Offset too high" );
275 return new BError(
"GetUnicodeStringFlipped requires 2 parameter." );
276 unsigned short offset, len;
280 if ( ( offset >=
buffer.size() ) ||
281 ( static_cast<u16>( offset + len * 2 ) >
283 return new BError(
"Offset too high" );
293 return new BLong( static_cast<int>(
buffer.size() ) );
298 return new BError(
"SetSize requires 1 parameter." );
299 unsigned short newsize;
302 return SetSize( newsize,
true );
310 return new BError(
"SetInt8 requires 2 parameters." );
311 unsigned short offset, value;
315 if ( offset >=
buffer.size() )
317 if ( !
SetSize( ( offset +
sizeof(
u8 ) ) ) )
319 return new BError(
"Offset value out of range on a fixed length packet" );
323 buffer[offset] =
static_cast<u8>( value );
324 return new BLong( 1 );
332 return new BError(
"SetInt16 requires 2 parameters." );
333 unsigned short offset, value;
336 if ( static_cast<u16>( offset +
sizeof(
u16 ) ) >
buffer.size() )
340 return new BError(
"Offset value out of range on a fixed length packet" );
345 u16* bufptr =
reinterpret_cast<u16*
>( &
buffer[offset] );
346 *bufptr =
ctBEu16( static_cast<u16>( value ) );
347 return new BLong( 1 );
355 return new BError(
"SetInt32 requires 2 parameters." );
356 unsigned short offset;
360 if ( static_cast<u32>( offset +
sizeof(
u32 ) ) >
buffer.size() )
364 return new BError(
"Offset value out of range on a fixed length packet" );
369 u32* bufptr =
reinterpret_cast<u32*
>( &
buffer[offset] );
370 *bufptr =
ctBEu32( static_cast<u32>( lvalue ) );
371 return new BLong( 1 );
379 return new BError(
"SetInt16Flipped requires 2 parameters." );
380 unsigned short offset, value;
383 if ( static_cast<u16>( offset +
sizeof(
u16 ) ) >
buffer.size() )
387 return new BError(
"Offset value out of range on a fixed length packet" );
392 u16* bufptr =
reinterpret_cast<u16*
>( &
buffer[offset] );
393 *bufptr =
ctLEu16( static_cast<u16>( value ) );
394 return new BLong( 1 );
402 return new BError(
"SetInt32Flipped requires 2 parameters." );
403 unsigned short offset;
407 if ( static_cast<u32>( offset +
sizeof(
u32 ) ) >
buffer.size() )
411 return new BError(
"Offset value out of range on a fixed length packet" );
415 u32* bufptr =
reinterpret_cast<u32*
>( &
buffer[offset] );
416 *bufptr =
ctLEu32( static_cast<u32>( lvalue ) );
417 return new BLong( 1 );
425 return new BError(
"SetString requires 3 parameters." );
426 unsigned short offset, nullterm;
431 if ( static_cast<u16>( offset + textlen + nullterm ) >
buffer.size() )
433 if ( !
SetSize( ( offset + textlen + nullterm ) ) )
435 return new BError(
"Offset value out of range on a fixed length packet" );
440 u8* bufptr =
reinterpret_cast<u8*
>( &
buffer[offset] );
441 const char* textptr = text->
value().c_str();
442 for (
u16 i = 0; i < textlen; i++ )
443 bufptr[i] = textptr[i];
448 return new BLong( 1 );
455 return new BError(
"SetUnicodeString requires 3 parameters." );
456 unsigned short offset, nullterm;
463 u16 nulltermlen = nullterm ? 2 : 0;
464 if ( static_cast<u16>( offset + ( textlen * 2 ) + nulltermlen ) >
buffer.size() )
466 if ( !
SetSize( ( offset + ( textlen * 2 ) + nulltermlen ) ) )
468 return new BError(
"Offset value out of range on a fixed length packet" );
473 true, nullterm ?
true :
false ) )
474 return new BError(
"Invalid value in Unicode array." );
476 return new BLong( 1 );
483 return new BError(
"SetUnicodeStringFlipped requires 3 parameters." );
484 unsigned short offset, nullterm;
491 u16 nulltermlen = nullterm ? 2 : 0;
492 if ( static_cast<u16>( offset + ( textlen * 2 ) + nulltermlen ) >
buffer.size() )
494 if ( !
SetSize( ( offset + ( textlen * 2 ) + nulltermlen ) ) )
496 return new BError(
"Offset value out of range on a fixed length packet" );
501 false, nullterm ?
true :
false ) )
502 return new BError(
"Invalid value in Unicode array." );
503 return new BLong( 1 );
510 return new BError(
"Invalid parameter" );
517 if ( objmethod !=
nullptr )
529 for (
auto itr =
buffer.begin(); itr !=
buffer.end(); ++itr )
530 os << std::setfill(
'0' ) << std::setw( 2 ) << std::hex << static_cast<u16>( *itr );
549 return new BError(
"Attempted to resize a fixed length packet" );
550 unsigned short oldsize =
static_cast<unsigned short>(
buffer.size() );
554 return new BLong( oldsize );
std::vector< unsigned char > buffer
const std::string & value() const
ObjMember * getKnownObjMember(const char *token)
virtual Bscript::BObjectImp * call_method(const char *methodname, Bscript::Executor &ex) POL_OVERRIDE
bool has_active_client() const
#define OSTRINGSTREAM_STR(x)
static void InRange(u16 x, u16 y, const Realms::Realm *realm, unsigned range, F &&f)
ObjMethod * getKnownObjMethod(const char *token)
virtual std::string getStringRep() const POL_OVERRIDE
std::unique_ptr< Network::ClientTransmit > clientTransmit
virtual Bscript::BObjectRef get_member(const char *membername) POL_OVERRIDE
bool getCharacterOrClientParam(Executor &exec, unsigned param, Mobile::Character *&chrptr, Network::Client *&clientptr)
NetworkManager networkManager
bool convertArrayToUC(Bscript::ObjArray *&in_text, u16 *out_wtext, size_t textlen, bool ConvToBE, bool nullterm)
static UninitObject * create()
bool convertUCtoArray(const u16 *in_wtext, Bscript::ObjArray *&out_text, size_t textlen, bool ConvFromBE)
std::unordered_map< u64, ScriptDiffData > data
const String * getStringParam(unsigned param)
Realms::Realm * find_realm(const std::string &name)
bool valid(unsigned short x, unsigned short y, short z) const
virtual Bscript::BObjectRef get_member_id(const int id) POL_OVERRIDE
bool getObjArrayParam(unsigned param, ObjArray *&pobjarr)
bool SetSize(u16 newsize)
virtual Bscript::BObjectImp * call_method_id(const int id, Bscript::Executor &ex, bool forcebuiltin=false) POL_OVERRIDE
virtual Bscript::BObjectImp * copy() const POL_OVERRIDE
bool getParam(unsigned param, int &value)