Pol  Revision:cb584c9
getmsg.cpp
Go to the documentation of this file.
1 
7 #include <ctype.h>
8 #include <stddef.h>
9 
10 #include "../bscript/berror.h"
11 #include "../bscript/impstr.h"
12 #include "../clib/clib_endian.h"
13 #include "../clib/logfacility.h"
14 #include "../clib/rawtypes.h"
15 #include "item/item.h"
16 #include "mobile/charactr.h"
17 #include "module/osmod.h"
18 #include "module/uomod.h"
19 #include "network/cgdata.h"
20 #include "network/client.h"
21 #include "network/packethelper.h"
22 #include "network/packets.h"
23 #include "pktboth.h"
24 #include "ufunc.h"
25 #include "uoexec.h"
26 
27 namespace Pol
28 {
29 namespace Core
30 {
31 void send_prompt( Network::Client* client, u32 serial )
32 {
34  msg->WriteFlipped<u16>( sizeof msg->buffer );
35  msg->Write<u32>( serial );
36  msg->WriteFlipped<u32>( 0x15u );
37  msg->offset += 5; // u32 type u8 text[0]
38  msg.Send( client );
39 }
40 
41 void handle_prompt( Network::Client* client, PKTBI_9A* msg )
42 {
43  Module::UOExecutorModule* uoemod = client->gd->prompt_uoemod;
44  if ( uoemod == nullptr )
45  return;
46  int textlen = cfBEu16( msg->msglen ) - offsetof( PKTBI_9A, text );
47  if ( msg->type )
48  {
49  if ( textlen <= 120 && msg->text[textlen - 1] == '\0' )
50  {
51  bool ok = true;
52  --textlen; // don't include null terminator (already checked)
53  for ( int i = 0; i < textlen; ++i )
54  {
55  if ( !isprint( msg->text[i] ) )
56  {
57  ok = false;
58  break;
59  }
60  }
61  if ( ok )
62  {
63  Bscript::String* str = new Bscript::String( msg->text, textlen );
64  uoemod->uoexec.ValueStack.back().set( new Bscript::BObject( str ) );
65  }
66  }
67  }
68  uoemod->uoexec.os_module->revive();
69  uoemod->prompt_chr = nullptr;
70  client->gd->prompt_uoemod = nullptr;
71 }
72 }
73 namespace Module
74 {
76 {
77  Mobile::Character* chr;
78  Items::Item* item;
79  const Bscript::String* prompt;
80  if ( !getCharacterParam( exec, 0, chr ) || !getItemParam( exec, 1, item ) ||
81  !exec.getStringParam( 2, prompt ) )
82  {
83  return new Bscript::BError( "Invalid parameter" );
84  }
85 
86  if ( chr->client == nullptr )
87  {
88  return new Bscript::BError( "No client attached" );
89  }
90 
91  if ( chr->has_active_prompt() != false )
92  {
93  return new Bscript::BError( "Another script has an active prompt" );
94  }
95 
96  if ( !uoexec.suspend() )
97  {
98  DEBUGLOG << "Script Error in '" << scriptname() << "' PC=" << exec.PC << ": \n"
99  << "\tCall to function UO::RequestInput():\n"
100  << "\tThe execution of this script can't be blocked!\n";
101  return new Bscript::BError( "Script can't be blocked" );
102  }
103 
104  Core::send_sysmessage( chr->client, prompt->data() );
105 
106  chr->client->gd->prompt_uoemod = this;
107  prompt_chr = chr;
108 
109  Core::send_prompt( chr->client, ctBEu32( item->serial ) );
110 
111  return new Bscript::BLong( 0 );
112 }
113 }
114 }
bool has_active_prompt() const
Definition: charactr.cpp:453
ValueStackCont ValueStack
Definition: executor.h:120
Network::Client * client
Definition: charactr.h:871
Mobile::Character * prompt_chr
Definition: uomod.h:303
void send_prompt(Network::Client *client, u32 serial)
Definition: getmsg.cpp:31
Core::UOExecutor & uoexec
Definition: uomod.h:291
Bscript::BObjectImp * mf_PromptInput()
Definition: getmsg.cpp:75
void handle_prompt(Network::Client *client, PKTBI_9A *msg)
Definition: getmsg.cpp:41
unsigned short u16
Definition: rawtypes.h:26
unsigned int u32
Definition: rawtypes.h:27
bool getCharacterParam(Bscript::Executor &exec, unsigned param, Mobile::Character *&chrptr)
Definition: uoexhelp.cpp:140
#define DEBUGLOG
Definition: logfacility.h:237
Module::UOExecutorModule * prompt_uoemod
Definition: cgdata.h:64
void Send(Client *client, int len=-1) const
Definition: packethelper.h:69
#define cfBEu16(x)
Definition: clib_endian.h:44
bool getItemParam(Executor &exec, unsigned param, Items::Item *&itemptr)
Definition: uoexhelp.cpp:219
ClientGameData * gd
Definition: client.h:252
#define ctBEu32(x)
Definition: clib_endian.h:45
const char * data() const
Definition: impstr.h:66
Definition: berror.cpp:12
void send_sysmessage(Network::Client *client, const char *text, unsigned short font, unsigned short color)
Definition: ufunc.cpp:1147
Module::OSExecutorModule * os_module
Definition: uoexec.h:37