Pol  Revision:cb584c9
exscrobj.cpp
Go to the documentation of this file.
1 
15 #include "exscrobj.h"
16 
17 #include <stddef.h>
18 #include <string>
19 
20 #include "../bscript/berror.h"
21 #include "../bscript/bobject.h"
22 #include "../bscript/dict.h"
23 #include "../bscript/executor.h"
24 #include "../bscript/impstr.h"
25 #include "../bscript/objmembers.h"
26 #include "../bscript/objmethods.h"
27 #include "../clib/strutil.h"
28 #include "../plib/systemstate.h"
29 #include "module/osmod.h"
30 #include "module/uomod.h"
31 #include "polcfg.h"
32 #include "uoexec.h"
33 #include "uoscrobj.h"
34 
35 namespace Pol
36 {
37 namespace Core
38 {
39 using namespace Bscript;
40 
42 
44  : BApplicObj<ScriptExPtr>( &scriptexobjimp_type, uoexec->weakptr )
45 {
46 }
47 
48 const char* ScriptExObjImp::typeOf() const
49 {
50  return "ScriptExRef";
51 }
53 {
54  return OTScriptExRef;
55 }
56 
58 {
59  if ( value().exists() )
60  return new ScriptExObjImp( value().get_weakptr() );
61  else
62  return new BError( "Script has been destroyed" );
63 }
64 
65 
66 BObjectImp* ScriptExObjImp::call_method_id( const int id, Executor& ex, bool /*forcebuiltin*/ )
67 {
68  if ( !value().exists() )
69  return new BError( "Script has been destroyed" );
70 
71  Core::UOExecutor* uoexec = value().get_weakptr();
72  Module::OSExecutorModule* osemod = uoexec->os_module;
73 
74  switch ( id )
75  {
76  case MTH_GET_MEMBER:
77  {
78  if ( !ex.hasParams( 1 ) )
79  return new BError( "Not enough parameters" );
80 
81  const String* mname;
82  if ( ex.getStringParam( 0, mname ) )
83  {
84  BObjectRef ref_temp = get_member( mname->value().c_str() );
85  BObjectRef& ref = ref_temp;
86  BObject* bo = ref.get();
87  BObjectImp* ret = bo->impptr();
88  ret = ret->copy();
89  if ( ret->isa( OTUninit ) )
90  {
91  std::string message = std::string( "Member " ) + std::string( mname->value() ) +
92  std::string( " not found on that object" );
93  return new BError( message );
94  }
95  else
96  {
97  return ret;
98  }
99  }
100  else
101  return new BError( "Invalid parameter type" );
102  break;
103  }
104  case MTH_SENDEVENT:
105  {
106  if ( !ex.hasParams( 1 ) )
107  return new BError( "Not enough parameters" );
108  BObjectImp* param0 = ex.getParamImp( 0 );
109  if ( osemod->signal_event( param0->copy() ) )
110  return new BLong( 1 );
111  else
112  return new BError( "Event queue is full, discarding event" );
113  }
114 
115  case MTH_KILL:
116  uoexec->seterror( true );
117 
118  // A Sleeping script would otherwise sit and wait until it wakes up to be killed.
119  osemod->revive();
120  if ( osemod->in_debugger_holdlist() )
121  osemod->revive_debugged();
122 
123  return new BLong( 1 );
124 
125  case MTH_LOADSYMBOLS:
126  {
127  int res = const_cast<EScriptProgram*>( uoexec->prog() )->read_dbg_file();
128  return new BLong( !res );
129  }
130 
131  case MTH_CLEAR_EVENT_QUEUE: // DAVE added this 11/20
132  return ( osemod->clear_event_queue() );
133 
134  default:
135  return new BError( "undefined" );
136  }
137 }
138 
139 BObjectImp* ScriptExObjImp::call_method( const char* methodname, Executor& ex )
140 {
141  ObjMethod* objmethod = getKnownObjMethod( methodname );
142  if ( objmethod != nullptr )
143  return this->call_method_id( objmethod->id, ex );
144  else
145  return new BError( "undefined" );
146 }
147 
149 {
150  BDictionary* dict = new BDictionary;
151 
152  BObjectRefVec::const_iterator itr = uoexec->Globals2.begin(), end = uoexec->Globals2.end();
153 
154  for ( unsigned idx = 0; itr != end; ++itr, ++idx )
155  {
156  const BObjectRef obref( ( *itr )->impref().copy() );
157 
158  if ( uoexec->prog()->globalvarnames.size() > idx )
159  dict->addMember( uoexec->prog()->globalvarnames[idx].c_str(), obref );
160  else
161  dict->addMember( Clib::decint( idx ).c_str(), obref );
162  }
163  return dict;
164 }
165 
167 {
168  if ( !value().exists() )
169  return BObjectRef( new BError( "Script has been destroyed" ) );
170 
171  UOExecutor* uoexec = value().get_weakptr();
172  Module::OSExecutorModule* osemod = uoexec->os_module;
173  Module::UOExecutorModule* uoemod =
174  static_cast<Module::UOExecutorModule*>( uoexec->findModule( "UO" ) );
175 
176  switch ( id )
177  {
178  case MBR_PID:
179  return BObjectRef( new BLong( osemod->pid() ) );
180  case MBR_NAME:
181  return BObjectRef( new String( uoexec->scriptname() ) );
182  case MBR_STATE:
183  return BObjectRef( new String( uoexec->state() ) );
184  case MBR_INSTR_CYCLES:
185  return BObjectRef( new Double( static_cast<double>( uoexec->instr_cycles ) ) );
186  case MBR_SLEEP_CYCLES:
187  return BObjectRef( new Double( static_cast<double>( uoexec->sleep_cycles ) ) );
188  case MBR_CONSEC_CYCLES:
189  {
190  u64 consec_cycles =
191  uoexec->instr_cycles -
193  uoexec->runaway_cycles;
194  return BObjectRef( new Double( static_cast<double>( consec_cycles ) ) );
195  }
196  case MBR_PC:
197  return BObjectRef( new BLong( uoexec->PC ) );
198  case MBR_CALL_DEPTH:
199  return BObjectRef( new BLong( static_cast<int>( uoexec->ControlStack.size() ) ) );
200  case MBR_NUM_GLOBALS:
201  return BObjectRef( new BLong( static_cast<int>( uoexec->Globals2.size() ) ) );
202  case MBR_VAR_SIZE:
203  return BObjectRef( new BLong( static_cast<int>( uoexec->sizeEstimate() ) ) );
204  case MBR_GLOBALS:
205  return BObjectRef( GetGlobals( uoexec ) );
206  case MBR_ATTACHED_TO:
207  if ( uoemod->attached_chr_ != nullptr )
208  return BObjectRef( new Module::ECharacterRefObjImp( uoemod->attached_chr_ ) );
209  else if ( uoemod->attached_npc_ != nullptr )
210  return BObjectRef( new Module::ECharacterRefObjImp( uoemod->attached_npc_ ) );
211  else if ( uoemod->attached_item_ )
212  return BObjectRef( new Module::EItemRefObjImp( uoemod->attached_item_ ) );
213  else
214  return BObjectRef( new BLong( 0 ) );
215  case MBR_CONTROLLER:
216  if ( uoemod->controller_.get() )
217  return BObjectRef( new Module::ECharacterRefObjImp( uoemod->controller_.get() ) );
218  else
219  return BObjectRef( new BLong( 0 ) );
220 
221  default:
222  return BObjectRef( UninitObject::create() );
223  }
224 }
225 
226 BObjectRef ScriptExObjImp::get_member( const char* membername )
227 {
228  ObjMember* objmember = getKnownObjMember( membername );
229  if ( objmember != nullptr )
230  return this->get_member_id( objmember->id );
231  else
232  return BObjectRef( UninitObject::create() );
233 }
234 }
235 }
unsigned char u8
Definition: rawtypes.h:25
const EScriptProgram * prog() const
Definition: executor.h:423
const std::string & value() const
Definition: impstr.h:67
bool in_debugger_holdlist() const
Definition: osmod.cpp:869
virtual u8 typeOfInt() const POL_OVERRIDE
Definition: exscrobj.cpp:52
SystemState systemstate
Definition: systemstate.cpp:12
bool isa(BObjectType type) const
Definition: bobject.h:353
Core::PolConfig config
Definition: systemstate.h:43
void seterror(bool err)
Definition: executor.h:437
virtual Bscript::BObjectRef get_member_id(const int id) POL_OVERRIDE
Definition: exscrobj.cpp:166
unsigned int runaway_script_threshold
Definition: polcfg.h:55
ObjMember * getKnownObjMember(const char *token)
Definition: parser.cpp:483
std::string decint(unsigned short v)
Definition: strutil.cpp:64
bool signal_event(Bscript::BObjectImp *eventimp)
Definition: osmod.cpp:769
virtual Bscript::BObjectImp * call_method_id(const int id, Bscript::Executor &ex, bool forcebuiltin=false) POL_OVERRIDE
Definition: exscrobj.cpp:66
virtual Bscript::BObjectImp * call_method(const char *methodname, Bscript::Executor &ex) POL_OVERRIDE
Definition: exscrobj.cpp:139
virtual const char * typeOf() const POL_OVERRIDE
Definition: exscrobj.cpp:48
virtual BObjectImp * copy() const =0
T * get_weakptr() const
Definition: weakptr.h:110
unsigned int pid() const
Definition: osmod.cpp:129
Core::CharacterRef controller_
Definition: uomod.h:314
ObjMethod * getKnownObjMethod(const char *token)
Definition: parser.cpp:666
BObjectImp * GetGlobals(const UOExecutor *uoexec)
Definition: exscrobj.cpp:148
unsigned long long u64
Definition: rawtypes.h:38
BObjectRefVec Globals2
Definition: executor.h:109
std::vector< std::string > globalvarnames
Definition: eprog.h:133
std::vector< ReturnContext > ControlStack
Definition: executor.h:113
Mobile::Character * attached_chr_
Definition: uomod.h:311
ExecutorModule * findModule(const std::string &name)
Definition: executor.cpp:3038
virtual size_t sizeEstimate() const POL_OVERRIDE
Definition: uoexec.cpp:80
u64 warn_runaway_on_cycle
Definition: uoexec.h:53
virtual Bscript::BObjectRef get_member(const char *membername) POL_OVERRIDE
Definition: exscrobj.cpp:226
static UninitObject * create()
Definition: bobject.h:482
Items::Item * attached_item_
Definition: uomod.h:313
void addMember(const char *name, BObjectRef val)
Definition: dict.cpp:232
std::string state()
Definition: uoexec.cpp:70
const std::string & scriptname() const
Definition: executor.h:413
const String * getStringParam(unsigned param)
Definition: executor.cpp:347
bool hasParams(unsigned howmany) const
Definition: executor.h:144
BApplicObjType scriptexobjimp_type
Definition: exscrobj.cpp:41
Bscript::BObjectImp * clear_event_queue()
Definition: osmod.cpp:527
Definition: berror.cpp:12
virtual Bscript::BObjectImp * copy() const POL_OVERRIDE
Definition: exscrobj.cpp:57
Mobile::Character * attached_npc_
Definition: uomod.h:312
BObjectImp * getParamImp(unsigned param)
Definition: executor.cpp:266
Module::OSExecutorModule * os_module
Definition: uoexec.h:37
ScriptExObjImp(UOExecutor *uoexec)
Definition: exscrobj.cpp:43