00001 #include "MercuryCommands.h"
00002 #include "MercuryLog.h"
00003 #include "MercuryObject.h"
00004 #include "MercuryUtil.h"
00005 #include "PStack.h"
00006 #include "MercuryObjectFactory.h"
00007
00008 using namespace std;
00009
00010 bool MercuryCommandHandler::Command( PStack & ret, const char * command, PStack & args )
00011 {
00012 ret.PushItem( MercuryObjectFactory::GetInstance().GetCallback( GetType(), command )( this, args) );
00013 return true;
00014 }
00015
00016 void MercuryCommandHandler::EnumerateCommands( MVector< MString > & toAdd )
00017 {
00018 MercuryObjectFactory::GetInstance().GetAllFunctions( GetType(), toAdd );
00019 }
00020
00021 void MercuryCommandRegisteringSystem::CheckInit()
00022 {
00023 if ( !bInit )
00024 {
00025 mCommands = new std::map< MString, CMDCallback >;
00026 bInit = true;
00027 }
00028 }
00029
00030 int MercuryCommandRegisteringSystem::Register( const MString & command, CMDCallback reg )
00031 {
00032 CheckInit();
00033 if ( mCommands->find( command ) == mCommands->end() )
00034 (*mCommands)[command] = reg;
00035 else
00036 {
00037 LOG.Warn("Double registeration of command \""+command+"\"!");
00038 return 0;
00039 }
00040 return 1;
00041 }
00042
00043 bool MercuryCommandRegisteringSystem::ExecuteCommand( PStack & ret, const MString & command, PStack & args, void * source )
00044 {
00045 CheckInit();
00046 if ( mCommands->find( command ) == mCommands->end() )
00047 {
00048 LOG.Warn( "There is no command by the name: "+command );
00049 return false;
00050 }
00051 return (*(*mCommands)[command])( ret, command, args, source );
00052 }
00053
00054 bool MercuryCommandRegisteringSystem::ExecuteObjectCommand( PStack & ret, const MString & object, const MString & command, PStack & args )
00055 {
00056 CheckInit();
00057 return OBJECTREGISTER.GetObjectFromName( object )->Command( ret, command.c_str(), args );
00058 }
00059
00060 bool MercuryCommandRegisteringSystem::ExecuteCommandString( const MString & sCommand )
00061 {
00062 return false;
00063 }
00064
00065 MercuryCommandRegisteringSystem CMDMAN;
00066
00067
00068
00069 bool ToMessage( PStack & ret, const char * cmd, PStack & args, void * info )
00070 {
00071 PSElement g = args.PopItem();
00072 if ( g.GetType() == PSElement::STRING )
00073 {
00074 MESSAGEMAN->BroadcastMessage( g.GetValueS(), args );
00075 return true;
00076 }
00077 else
00078 {
00079 LOG.Warn( "Message command was not a string!" );
00080 return false;
00081 }
00082 }
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092 REGISTER_FUNCTION_TO_COMMAND( Message, ToMessage );
00093
00094 bool HGEXPORT RunCommandOnObject( const char * sObjectName, const char * sCommandName, int iMPStackArgs, int iMPStackRet )
00095 {
00096 PStack * args = GetMPStack( iMPStackArgs );
00097 PStack * ret = GetMPStack( iMPStackRet );
00098 if ( !args || !ret )
00099 return false;
00100 MercuryObject * obj = OBJECTREGISTER.GetObjectFromName( MString( sObjectName ) );
00101 if ( obj )
00102 return obj->Command( *ret, sCommandName, *args );
00103 else
00104 return false;
00105 }
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134