Pol  Revision:cb584c9
polsystemmod.cpp
Go to the documentation of this file.
1 
14 #include "polsystemmod.h"
15 
16 #include <ctime>
17 #include <fstream>
18 #include <string>
19 
20 #include "../../bscript/berror.h"
21 #include "../../bscript/bobject.h"
22 #include "../../bscript/dict.h"
23 #include "../../bscript/impstr.h"
24 #include "../../clib/clib.h"
25 #include "../../clib/clib_MD5.h"
26 #include "../../clib/compilerspecifics.h"
27 #include "../../clib/fileutil.h"
28 #include "../../clib/rawtypes.h"
29 #include "../../clib/strutil.h"
30 #include "../../clib/threadhelp.h"
31 #include "../../plib/pkg.h"
32 #include "../../plib/systemstate.h"
33 #include "../cmdlevel.h"
34 #include "../core.h"
35 #include "../globals/settings.h"
36 #include "../globals/uvars.h"
37 #include "../item/item.h"
38 #include "../item/itemdesc.h"
39 #include "../listenpt.h"
40 #include "../packetscrobj.h"
41 #include "../polcfg.h"
42 #include "../proplist.h"
43 #include "../realms.h"
44 #include "../realms/realm.h"
45 #include "../tooltips.h"
46 #include "../uobject.h"
47 #include "../uoexhelp.h"
48 
49 namespace Pol
50 {
51 namespace Core
52 {
53 void decay_thread_shadow( void* );
55 }
56 namespace Module
57 {
59 {
60 public:
61  explicit PackagePtrHolder( Plib::Package* pkg ) : m_pPkg( pkg ) {}
62  Plib::Package* operator->() { return m_pPkg; }
63  const Plib::Package* operator->() const { return m_pPkg; }
64  Plib::Package* Ptr() { return m_pPkg; }
65  const Plib::Package* Ptr() const { return m_pPkg; }
66 
67 private:
69 };
70 
72 // typedef BApplicObj< ref_ptr<Package> > PackageObjImpBase;
74 class PackageObjImp : public PackageObjImpBase
75 {
76  typedef PackageObjImpBase base;
77 
78 public:
79  explicit PackageObjImp( const PackagePtrHolder& other );
80  virtual const char* typeOf() const POL_OVERRIDE;
81  virtual u8 typeOfInt() const POL_OVERRIDE;
82  virtual Bscript::BObjectImp* copy() const POL_OVERRIDE;
83  virtual Bscript::BObjectImp* call_method( const char* methodname,
84  Bscript::Executor& ex ) POL_OVERRIDE;
85  virtual Bscript::BObjectRef get_member( const char* membername ) POL_OVERRIDE;
86 };
88  : PackageObjImpBase( &packageobjimp_type, other )
89 {
90 }
91 }
92 namespace Bscript
93 {
94 using namespace Module;
95 template <>
99  {"GetCmdLevelName", &PolSystemExecutorModule::mf_GetCmdLevelName},
100  {"GetCmdLevelNumber", &PolSystemExecutorModule::mf_GetCmdLevelNumber},
102  {"GetPackageByName", &PolSystemExecutorModule::mf_GetPackageByName},
103  {"ListTextCommands", &PolSystemExecutorModule::mf_ListTextCommands},
105  {"ReloadConfiguration", &PolSystemExecutorModule::mf_ReloadConfiguration},
106  {"ReadMillisecondClock", &PolSystemExecutorModule::mf_ReadMillisecondClock},
107  {"ListenPoints", &PolSystemExecutorModule::mf_ListenPoints},
108  {"SetSysTrayPopupText", &PolSystemExecutorModule::mf_SetSysTrayPopupText},
109  {"GetItemDescriptor", &PolSystemExecutorModule::mf_GetItemDescriptor},
110  {"FormatItemDescription", &PolSystemExecutorModule::mf_FormatItemDescription},
111  {"CreatePacket", &PolSystemExecutorModule::mf_CreatePacket},
113  {"DeleteRealm", &PolSystemExecutorModule::mf_DeleteRealm},
115  {"LogCPropProfile", &PolSystemExecutorModule::mf_LogCPropProfile},
116 };
117 }
118 namespace Module
119 {
120 using namespace Bscript;
121 
122 const char* PackageObjImp::typeOf() const
123 {
124  return "Package";
125 }
127 {
128  return OTPackage;
129 }
131 {
132  return new PackageObjImp( obj_ );
133 }
134 BObjectImp* PackageObjImp::call_method( const char* /*methodname*/, Executor& /*ex*/ )
135 {
136  return new BError( "undefined method" );
137 }
138 BObjectRef PackageObjImp::get_member( const char* membername )
139 {
140  if ( stricmp( membername, "name" ) == 0 )
141  {
142  return BObjectRef( new String( value()->name() ) );
143  }
144  else if ( stricmp( membername, "version" ) == 0 )
145  {
146  return BObjectRef( new String( value()->version() ) );
147  }
148  else if ( stricmp( membername, "supports_http" ) == 0 )
149  {
150  const Plib::Package* pkg = value().Ptr();
151  return BObjectRef( new BLong( Clib::FileExists( pkg->dir() + "www" ) ) );
152  }
153  else if ( stricmp( membername, "npcdesc" ) == 0 )
154  {
155  Plib::Package* pkg = value().Ptr();
156  std::string filepath = Plib::GetPackageCfgPath( pkg, "npcdesc.cfg" );
157  return BObjectRef( new BLong( Clib::FileExists( filepath ) ) );
158  }
159  else
160  {
161  return BObjectRef( new BError( "Undefined member" ) );
162  }
163 }
164 
166  : Bscript::TmplExecutorModule<PolSystemExecutorModule>( "polsys", exec )
167 {
168 }
169 
171 {
172  Core::UObject* uobj;
173  if ( getUObjectParam( exec, 0, uobj ) )
174  {
175  uobj->increv();
177  return new BLong( 1 );
178  }
179  else
180  {
181  return new BError( "Invalid parameter type" );
182  }
183 }
184 
186 {
187  int cmdlevel_num;
188  const String* cmdlevel_alias;
189 
190  if ( exec.numParams() != 1 )
191  return new BError( "Expected 1 parameter." );
192  else if ( getParam( 0, cmdlevel_num ) )
193  {
194  if ( cmdlevel_num >= static_cast<int>( Core::gamestate.cmdlevels.size() ) )
195  cmdlevel_num = static_cast<int>( Core::gamestate.cmdlevels.size() - 1 );
196 
197  return new String( Core::gamestate.cmdlevels[cmdlevel_num].name );
198  }
199  else if ( getStringParam( 0, cmdlevel_alias ) )
200  {
201  Core::CmdLevel* cmdlevel = Core::FindCmdLevelByAlias( cmdlevel_alias->data() );
202  if ( cmdlevel == nullptr )
203  return new BError( "Could not find a command level with that alias." );
204  else
205  return new String( cmdlevel->name );
206  }
207  else
208  return new BError( "Invalid parameter type." );
209 }
210 
212 {
213  const String* cmdlvlname;
214  if ( !getStringParam( 0, cmdlvlname ) )
215  return new BError( "Invalid parameter type." );
216 
217  Core::CmdLevel* cmdlevel_search = Core::find_cmdlevel( cmdlvlname->data() );
218  if ( cmdlevel_search == nullptr )
219  return new BError( "Could not find a command level with that name." );
220 
221  return new BLong( cmdlevel_search->cmdlevel );
222 }
223 
225 {
226  std::unique_ptr<ObjArray> arr( new ObjArray );
227  for ( unsigned i = 0; i < Plib::systemstate.packages.size(); ++i )
228  {
229  PackageObjImp* imp = new PackageObjImp( PackagePtrHolder( Plib::systemstate.packages[i] ) );
230  arr->addElement( imp );
231  }
232  return arr.release();
233 }
234 
236 {
237  const String* pkgname;
238  if ( !getStringParam( 0, pkgname ) )
239  return new BError( "Invalid parameter type." );
240 
241  // pkgname->toLower();
242  Plib::Package* pkg = Plib::find_package( pkgname->value() );
243  if ( !pkg )
244  return new BError( "No package found by that name." );
245  else
246  return new PackageObjImp( PackagePtrHolder( pkg ) );
247 }
248 
250 {
251  std::unique_ptr<BDictionary> pkg_list( new BDictionary );
252  // Sets up text commands not in a package.
253  {
254  std::unique_ptr<BDictionary> cmd_lvl_list( new BDictionary );
255  for ( unsigned num = 0; num < Core::gamestate.cmdlevels.size(); ++num )
256  {
257  ObjArray* script_list = Core::GetCommandsInPackage( nullptr, num );
258  if ( script_list == nullptr )
259  continue;
260  else if ( !script_list->ref_arr.empty() )
261  cmd_lvl_list->addMember( new BLong( num ), script_list );
262  }
263  if ( cmd_lvl_list->contents().size() > 0 )
264  pkg_list->addMember( new String( "" ), cmd_lvl_list.release() );
265  }
266  //
267  // Sets up packaged text commands.
268  for ( Plib::Packages::iterator itr = Plib::systemstate.packages.begin();
269  itr != Plib::systemstate.packages.end(); ++itr )
270  {
271  Plib::Package* pkg = ( *itr );
272  std::unique_ptr<BDictionary> cmd_lvl_list( new BDictionary );
273  for ( unsigned num = 0; num < Core::gamestate.cmdlevels.size(); ++num )
274  {
275  ObjArray* script_list = Core::GetCommandsInPackage( pkg, num );
276  if ( script_list == nullptr )
277  continue;
278  else if ( !script_list->ref_arr.empty() )
279  cmd_lvl_list->addMember( new BLong( num ), script_list );
280  }
281  if ( cmd_lvl_list->contents().size() > 0 )
282  pkg_list->addMember( new String( pkg->name().c_str() ), cmd_lvl_list.release() );
283  }
284  return pkg_list.release();
285 }
286 
288 {
290 
291  return new BLong( 1 );
292 }
293 
295 {
296  return new BLong( Core::polticks_t_to_ms( Core::polclock() ) );
297 }
298 
300 {
301  return Core::GetListenPoints();
302 }
303 
305 {
306  std::unique_ptr<BStruct> details( new BStruct() );
307  details->addMember( "width", new BLong( realm->width() ) );
308  details->addMember( "height", new BLong( realm->height() ) );
309  details->addMember( "season", new BLong( realm->season() ) );
310  details->addMember( "mapid", new BLong( realm->getUOMapID() ) );
311  details->addMember( "toplevel_item_count", new BLong( realm->toplevel_item_count() ) );
312  details->addMember( "mobile_count", new BLong( realm->mobile_count() ) );
313  details->addMember( "offline_mobs_count", new BLong( realm->offline_mobile_count() ) );
314  details->addMember( "multi_count", new BLong( realm->multi_count() ) );
315 
316  return details.release();
317 }
318 
320 {
321  const String* realm_name;
322  // getStringParam(0, realm_name);
323  BObjectImp* imp = getParamImp( 0 );
324  if ( imp->isa( BObjectImp::OTString ) )
325  {
326  realm_name = static_cast<const String*>( imp );
327  }
328  else
329  {
330  return new BError( std::string( "Parameter must be a String or empty, got " ) +
331  BObjectImp::typestr( imp->type() ) );
332  }
333 
334  if ( realm_name->length() > 0 )
335  {
336  Realms::Realm* realm = Core::find_realm( realm_name->value() );
337  if ( !realm )
338  return new BError( "Realm not found." );
339  else
340  return SetupRealmDetails( realm );
341  }
342  else
343  {
344  BDictionary* dict = new BDictionary;
345  std::vector<Realms::Realm*>::iterator itr;
346  for ( itr = Core::gamestate.Realms.begin(); itr != Core::gamestate.Realms.end(); ++itr )
347  {
348  dict->addMember( ( *itr )->name().c_str(), SetupRealmDetails( *itr ) );
349  }
350 
351  return dict;
352  }
353 }
354 
356 {
357 #ifdef _WIN32
358  const char* text = exec.paramAsString( 0 );
359 
361 #endif
362  return new BLong( 1 );
363 }
364 
366 {
367  unsigned int objtype;
368  if ( Core::getObjtypeParam( exec, 0, objtype ) )
369  {
370  const Items::ItemDesc& id = Items::find_itemdesc( objtype );
371  if ( id.objtype == 0 && id.graphic == 0 )
372  return new BError( "Itemdesc.cfg entry for objtype " + Clib::hexint( objtype ) +
373  " not found." );
374 
375  std::unique_ptr<BStruct> descriptor( new BStruct() );
376 
377  id.PopulateStruct( descriptor.get() );
378 
379  return descriptor.release();
380  }
381  else
382  {
383  return new BError( "Invalid parameter type" );
384  }
385 }
386 
388 {
389  const String* desc;
390  unsigned short amount;
391  const String* suffix;
392 
393  if ( getStringParam( 0, desc ) && getParam( 1, amount ) && getStringParam( 2, suffix ) )
394  {
395  return new String( Core::format_description( 0, desc->value(), amount, suffix->value() ) );
396  }
397  else
398  {
399  return new BError( "Invalid parameter type" );
400  }
401 }
402 
404 {
405  int size;
406  unsigned short type;
407  if ( exec.getParam( 0, type ) && exec.getParam( 1, size ) )
408  {
409  if ( type > 0xFF )
410  return new BError( "Packet type too high" );
411 
412  return new Core::BPacket( static_cast<u8>( type ), static_cast<signed short>( size ) );
413  }
414  else
415  {
416  return new BError( "Invalid parameter type" );
417  }
418 }
419 
421 {
422  const String* realm_name;
423  const String* base;
424  if ( !( getStringParam( 0, realm_name ) && getStringParam( 1, base ) ) )
425  {
426  return new BError( "Invalid parameter" );
427  }
428  Realms::Realm* baserealm = Core::find_realm( base->value() );
429  if ( !baserealm )
430  return new BError( "BaseRealm not found." );
431  if ( baserealm->is_shadowrealm )
432  return new BError( "BaseRealm is a ShadowRealm." );
433  if ( Core::defined_realm( realm_name->value() ) )
434  return new BError( "Realmname already defined." );
435  Core::add_realm( realm_name->value(), baserealm );
436  if ( Core::settingsManager.ssopt.decay_items )
437  {
438  if ( !Plib::systemstate.config.single_thread_decay )
439  {
440  std::ostringstream thname;
441  thname << "Decay_" << realm_name->value();
443  (void*)Core::find_realm( realm_name->value() ) );
444  }
445  }
446  return new BLong( 1 );
447 }
448 
450 {
451  const String* realm_name;
452  if ( !( getStringParam( 0, realm_name ) ) )
453  return new BError( "Invalid parameter" );
454 
455  Realms::Realm* realm = Core::find_realm( realm_name->value() );
456 
457  if ( !realm )
458  return new BError( "Realm not found." );
459  if ( !realm->is_shadowrealm )
460  return new BError( "Realm is not a ShadowRealm." );
461  if ( realm->mobile_count() > 0 )
462  return new BError( "Mobiles in Realm." );
463  if ( realm->offline_mobile_count() > 0 )
464  return new BError( "Offline characters in Realm" );
465  if ( realm->toplevel_item_count() > 0 )
466  return new BError( "Items in Realm." );
467  if ( realm->multi_count() > 0 )
468  return new BError( "Multis in Realm." );
469 
470  Core::remove_realm( realm_name->value() );
471  return new BLong( 1 );
472 }
473 
475 {
476  const String* string;
477  if ( !( getStringParam( 0, string ) ) )
478  return new BError( "Invalid parameter" );
479  if ( string->length() < 1 )
480  return new BError( "String is empty" );
481  std::string temp;
482  if ( !Clib::MD5_Encrypt( string->value(), temp ) )
483  return new BError( "Failed to encrypt" );
484  return new String( temp );
485 }
486 
493 {
494  const std::string filepath = "log/cpprofile.log";
495  std::ofstream ofs( filepath.c_str(), std::ios::out | std::ios::app );
496 
497  if ( !ofs.is_open() )
498  return new BError( "Unable to open file: " + filepath );
499 
500  // Write the header
501  auto t = std::time( nullptr );
502  auto tm = Clib::localtime( t );
503  ofs << std::string( 80, '=' ) << std::endl;
504  ofs << "CProp profiling information dumped on " << std::asctime( &tm ) << std::endl;
505  ofs << "the profiler is using an estimated amount of "
506  << Core::CPropProfiler::instance().estimateSize() << " Bytes of memory." << std::endl;
507  ofs << "the profiler is currently "
508  << ( Plib::systemstate.config.profile_cprops ? "enabled" : "disabled" ) << "." << std::endl;
509  ofs << std::endl;
510 
511  // Write the body
513 
514  // Write the footer
515  ofs << std::string( 80, '=' ) << std::endl;
516 
517  if ( ofs.fail() )
518  return new BError( "Error during write." );
519 
520  ofs.close();
521  return new BLong( 1 );
522 }
523 }
524 }
void decay_thread_shadow(void *arg)
Definition: decay.cpp:188
unsigned char u8
Definition: rawtypes.h:25
std::string format_description(unsigned int polflags, const std::string &descdef, unsigned short amount, const std::string suffix)
Definition: ufunc.cpp:1786
Bscript::ObjArray * GetCommandsInPackage(Plib::Package *m_pkg, int cmdlvl_num)
Definition: cmdlevel.cpp:104
bool MD5_Encrypt(const std::string &in, std::string &out)
Definition: clib_MD5.cpp:97
bool defined_realm(const std::string &name)
Definition: realms.cpp:74
#define POL_OVERRIDE
const std::string & value() const
Definition: impstr.h:67
Bscript::BObjectImp * mf_SetSysTrayPopupText()
Bscript::BObjectImp * mf_GetCmdLevelNumber()
BObjectType type() const
Definition: bobject.h:358
SystemState systemstate
Definition: systemstate.cpp:12
Bscript::BObjectImp * mf_IncRevision()
bool isa(BObjectType type) const
Definition: bobject.h:353
polclock_t polclock()
Definition: polclock.cpp:72
Core::PolConfig config
Definition: systemstate.h:43
void remove_realm(const std::string &name)
Definition: realms.cpp:92
static CPropProfiler & instance()
Definition: proplist.cpp:71
Bscript::BApplicObjType packageobjimp_type
std::tm localtime(const std::time_t &t)
threadsafe version of localtime
Definition: clib.h:143
bool getParam(unsigned param, int &value)
Definition: execmodl.cpp:62
unsigned getUOMapID() const
Definition: realm.h:224
polticks_t polticks_t_to_ms(polticks_t ticks)
Definition: polclock.h:55
Bscript::BObjectImp * mf_CreatePacket()
const Plib::Package * operator->() const
void reload_configuration()
Definition: loadunld.cpp:263
void start_thread(void(*entry)(void *), const char *thread_name, void *arg)
Definition: threadhelp.cpp:233
bool is_shadowrealm
Definition: realm.h:64
BObjectImp * getParamImp(unsigned param)
Definition: execmodl.cpp:22
virtual Bscript::BObjectImp * call_method(const char *methodname, Bscript::Executor &ex) POL_OVERRIDE
CmdLevels cmdlevels
Definition: uvars.h:137
const std::string & name() const
Definition: pkg.h:83
unsigned int mobile_count() const
Definition: realm.h:189
void CoreSetSysTrayToolTip(const std::string &text, Priority priority)
Definition: core.cpp:36
const ItemDesc & find_itemdesc(unsigned int objtype)
Definition: itemdesc.cpp:933
PolSystemExecutorModule(Bscript::Executor &exec)
Package * find_package(const std::string &pkgname)
Definition: pkg.cpp:33
Bscript::BObjectImp * mf_ReadMillisecondClock()
const char * paramAsString(unsigned param)
Definition: executor.cpp:209
Bscript::BObjectImp * mf_GetCmdLevelName()
unsigned int offline_mobile_count() const
Definition: realm.h:193
std::string hexint(unsigned short v)
Definition: strutil.cpp:23
Bscript::BObjectImp * mf_ListTextCommands()
PackagePtrHolder(Plib::Package *pkg)
Bscript::BObjectImp * mf_MD5Encrypt()
unsigned season() const
Definition: realm.cpp:114
Bscript::BObjectImp * mf_AddRealm()
std::vector< Realms::Realm * > Realms
Definition: uvars.h:163
Bscript::BObjectImp * mf_Realms()
unsigned short height() const
Definition: realm.h:245
unsigned int multi_count() const
Definition: realm.h:201
unsigned char cmdlevel
Definition: cmdlevel.h:39
Bscript::BObjectImp * mf_ReloadConfiguration()
std::string name
Definition: cmdlevel.h:38
size_t numParams() const
Definition: executor.h:145
Bscript::BObjectImp * mf_FormatItemDescription()
Bscript::BObjectImp * mf_LogCPropProfile()
void addMember(const char *name, BObjectRef val)
Definition: dict.cpp:232
Bscript::BObjectImp * mf_GetItemDescriptor()
GameState gamestate
Definition: uvars.cpp:74
size_t estimateSize() const
Definition: proplist.cpp:303
const String * getStringParam(unsigned param)
Definition: execmodl.cpp:36
SettingsManager settingsManager
Definition: settings.cpp:14
virtual Bscript::BObjectRef get_member(const char *membername) POL_OVERRIDE
bool getObjtypeParam(Executor &exec, unsigned param, unsigned int &objtype)
Definition: uoexhelp.cpp:400
const Plib::Package * Ptr() const
Bscript::BObjectImp * mf_GetPackageByName()
bool getUObjectParam(Executor &exec, unsigned param, UObject *&objptr)
Definition: uoexhelp.cpp:369
BStruct * SetupRealmDetails(Realms::Realm *realm)
unsigned int toplevel_item_count() const
Definition: realm.h:197
CmdLevel * find_cmdlevel(const char *name)
Definition: cmdlevel.cpp:84
unsigned short width() const
Definition: realm.h:241
Realms::Realm * find_realm(const std::string &name)
Definition: realms.cpp:64
std::string name
Definition: osmod.cpp:943
Bscript::BObjectImp * mf_DeleteRealm()
Bscript::BObjectImp * GetListenPoints()
Definition: listenpt.cpp:119
std::string GetPackageCfgPath(const Package *pkg, const std::string &filename)
Definition: pkg.cpp:491
bool FileExists(const char *filename)
Definition: fileutil.cpp:118
virtual u8 typeOfInt() const POL_OVERRIDE
void send_object_cache_to_inrange(const UObject *obj)
Definition: tooltips.cpp:77
Bscript::BApplicObj< PackagePtrHolder > PackageObjImpBase
void add_realm(const std::string &name, Realms::Realm *base)
Definition: realms.cpp:84
void dumpProfile(std::ostream &os) const
Definition: proplist.cpp:214
Bscript::BObjectImp * mf_ListenPoints()
virtual Bscript::BObjectImp * copy() const POL_OVERRIDE
const char * data() const
Definition: impstr.h:66
Definition: berror.cpp:12
Bscript::BObjectImp * mf_Packages()
size_t length() const
Definition: impstr.h:68
static const char * typestr(BObjectType typ)
Definition: object.cpp:218
Plib::Package * operator->()
const std::string & dir() const
Definition: pkg.h:79
PackageObjImpBase base
bool getParam(unsigned param, int &value)
Definition: executor.cpp:363
CmdLevel * FindCmdLevelByAlias(const std::string &str)
Definition: cmdlevel.cpp:94
virtual const char * typeOf() const POL_OVERRIDE