2015-11-07 11:17:39 +01:00
|
|
|
#ifndef _COMMAND_HPP_
|
|
|
|
#define _COMMAND_HPP_
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2016-02-20 23:25:00 +01:00
|
|
|
#include "Base/Shared.hpp"
|
|
|
|
#include "Base/Buffer.hpp"
|
2015-11-07 11:17:39 +01:00
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2016-02-20 23:25:00 +01:00
|
|
|
#include <map>
|
|
|
|
#include <vector>
|
2015-11-07 11:17:39 +01:00
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
namespace SqMod {
|
|
|
|
|
2016-02-20 23:25:00 +01:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
class CmdListener;
|
2015-11-07 11:17:39 +01:00
|
|
|
|
|
|
|
/* ------------------------------------------------------------------------------------------------
|
2016-02-20 23:25:00 +01:00
|
|
|
* Manages command instances and processes executed commands.
|
2015-11-07 11:17:39 +01:00
|
|
|
*/
|
|
|
|
class CmdManager
|
|
|
|
{
|
2016-02-20 23:25:00 +01:00
|
|
|
// --------------------------------------------------------------------------------------------
|
|
|
|
friend class CmdListener;
|
2015-11-07 11:17:39 +01:00
|
|
|
|
2016-05-22 05:20:38 +02:00
|
|
|
private:
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------------------------------
|
|
|
|
static CmdManager s_Inst; // Command manager instance.
|
|
|
|
|
2016-02-20 23:25:00 +01:00
|
|
|
private:
|
2015-11-07 11:17:39 +01:00
|
|
|
|
2016-06-08 15:26:55 +02:00
|
|
|
// --------------------------------------------------------------------------------------------
|
|
|
|
typedef std::pair< Uint8, Object > CmdArg; // Can hold the argument value and type.
|
|
|
|
typedef std::vector< CmdArg > CmdArgs; // A list of extracted arguments.
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------
|
|
|
|
* Holds the execution context of a command.
|
|
|
|
*/
|
|
|
|
struct Context
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------------------
|
|
|
|
Buffer mBuffer; // Shared buffer used to extract arguments.
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------------------
|
|
|
|
const Int32 mInvoker; // The identifier of the last player that invoked a command.
|
|
|
|
String mCommand; // The extracted command name.
|
|
|
|
String mArgument; // The extracted command argument.
|
|
|
|
CmdListener* mInstance; // Pointer to the currently executed command.
|
|
|
|
Object mObject; // Script object of the currently exectued command.
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------------------
|
|
|
|
CmdArgs mArgv; // Extracted command arguments.
|
|
|
|
Uint32 mArgc; // Extracted arguments count.
|
|
|
|
|
|
|
|
/* ----------------------------------------------------------------------------------------
|
|
|
|
* Default constructor.
|
|
|
|
*/
|
|
|
|
Context(Int32 invoker);
|
|
|
|
|
|
|
|
/* ----------------------------------------------------------------------------------------
|
|
|
|
* Copy constructor. (disabled)
|
|
|
|
*/
|
|
|
|
Context(const Context & o) = delete;
|
|
|
|
|
|
|
|
/* ----------------------------------------------------------------------------------------
|
|
|
|
* Move constructor. (disabled)
|
|
|
|
*/
|
|
|
|
Context(Context && o) = delete;
|
|
|
|
|
|
|
|
/* ----------------------------------------------------------------------------------------
|
|
|
|
* Copy assignment operator. (disabled)
|
|
|
|
*/
|
|
|
|
Context & operator = (const Context & o) = delete;
|
|
|
|
|
|
|
|
/* ----------------------------------------------------------------------------------------
|
|
|
|
* Move assignment operator. (disabled)
|
|
|
|
*/
|
|
|
|
Context & operator = (Context && o) = delete;
|
|
|
|
};
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------------------------------
|
|
|
|
typedef SharedPtr< Context > CtxRef; // Shared reference to an execution context.
|
|
|
|
|
2016-05-22 05:20:38 +02:00
|
|
|
/* --------------------------------------------------------------------------------------------
|
2016-06-08 15:26:55 +02:00
|
|
|
* Helper class implementing RAII to release the command context.
|
2016-05-22 05:20:38 +02:00
|
|
|
*/
|
|
|
|
struct Guard
|
|
|
|
{
|
2016-06-08 15:26:55 +02:00
|
|
|
public:
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------------------
|
|
|
|
CtxRef mPrevious; // Previous context when this guard was created.
|
|
|
|
CtxRef mCurrent; // The context managed by this guard.
|
|
|
|
|
2016-05-22 05:20:38 +02:00
|
|
|
/* ----------------------------------------------------------------------------------------
|
|
|
|
* Default constructor.
|
|
|
|
*/
|
2016-06-08 15:26:55 +02:00
|
|
|
Guard(const CtxRef & ctx);
|
2016-05-22 05:20:38 +02:00
|
|
|
|
|
|
|
/* ----------------------------------------------------------------------------------------
|
|
|
|
* Copy constructor.
|
|
|
|
*/
|
|
|
|
Guard(const Guard & o) = delete;
|
|
|
|
|
|
|
|
/* ----------------------------------------------------------------------------------------
|
|
|
|
* Move constructor.
|
|
|
|
*/
|
|
|
|
Guard(Guard && o) = delete;
|
|
|
|
|
|
|
|
/* ----------------------------------------------------------------------------------------
|
|
|
|
* Destructor.
|
|
|
|
*/
|
|
|
|
~Guard();
|
|
|
|
|
|
|
|
/* ----------------------------------------------------------------------------------------
|
|
|
|
* Copy assignment operator.
|
|
|
|
*/
|
|
|
|
Guard & operator = (const Guard & o) = delete;
|
|
|
|
|
|
|
|
/* ----------------------------------------------------------------------------------------
|
|
|
|
* Move assignment operator.
|
|
|
|
*/
|
|
|
|
Guard & operator = (Guard && o) = delete;
|
|
|
|
};
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------------------------------
|
|
|
|
friend class Guard; // Allow the guard to access the member it's supposed to release.
|
|
|
|
|
2016-03-15 09:56:00 +01:00
|
|
|
/* --------------------------------------------------------------------------------------------
|
|
|
|
* Structure that represents a unique command in the pool.
|
|
|
|
*/
|
|
|
|
struct Command
|
|
|
|
{
|
|
|
|
// ----------------------------------------------------------------------------------------
|
|
|
|
std::size_t mHash; // The unique hash that identifies this command.
|
|
|
|
String mName; // The unique name that identifies this command.
|
|
|
|
CmdListener* mPtr; // The listener that reacts to this command.
|
|
|
|
Object mObj; // A strong reference to the script object.
|
|
|
|
|
|
|
|
/* ----------------------------------------------------------------------------------------
|
|
|
|
* Construct a command and the also create a script object from the specified listener.
|
|
|
|
*/
|
|
|
|
Command(std::size_t hash, const String & name, CmdListener * ptr)
|
|
|
|
: mHash(hash), mName(name), mPtr(ptr), mObj(ptr)
|
|
|
|
{
|
|
|
|
/* ... */
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ----------------------------------------------------------------------------------------
|
|
|
|
* Construct a command and extract the listener from the specified script object.
|
|
|
|
*/
|
|
|
|
Command(std::size_t hash, const String & name, const Object & obj)
|
|
|
|
: mHash(hash), mName(name), mPtr(obj.Cast< CmdListener * >()), mObj(obj)
|
|
|
|
{
|
|
|
|
/* ... */
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ----------------------------------------------------------------------------------------
|
|
|
|
* Copy constructor.
|
|
|
|
*/
|
|
|
|
Command(const Command & o) = default;
|
|
|
|
|
|
|
|
/* ----------------------------------------------------------------------------------------
|
|
|
|
* Move constructor.
|
|
|
|
*/
|
|
|
|
Command(Command && o) = default;
|
|
|
|
|
|
|
|
/* ----------------------------------------------------------------------------------------
|
|
|
|
* Destructor.
|
|
|
|
*/
|
|
|
|
~Command() = default;
|
|
|
|
|
|
|
|
/* ----------------------------------------------------------------------------------------
|
|
|
|
* Copy assignment operator.
|
|
|
|
*/
|
|
|
|
Command & operator = (const Command & o) = default;
|
|
|
|
|
|
|
|
/* ----------------------------------------------------------------------------------------
|
|
|
|
* Move assignment operator.
|
|
|
|
*/
|
|
|
|
Command & operator = (Command && o) = default;
|
|
|
|
};
|
|
|
|
|
2016-02-20 23:25:00 +01:00
|
|
|
// --------------------------------------------------------------------------------------------
|
2016-03-15 09:56:00 +01:00
|
|
|
typedef std::vector< Command > CmdList;
|
2015-11-07 11:17:39 +01:00
|
|
|
|
2016-05-22 05:20:38 +02:00
|
|
|
private:
|
2016-03-16 18:11:42 +01:00
|
|
|
|
2016-05-22 05:20:38 +02:00
|
|
|
// --------------------------------------------------------------------------------------------
|
|
|
|
CmdList m_Commands; // List of available command instances.
|
2016-06-08 15:26:55 +02:00
|
|
|
CtxRef m_Context; // The context of the currently executed command.
|
2015-11-07 11:17:39 +01:00
|
|
|
|
2016-05-22 05:20:38 +02:00
|
|
|
// --------------------------------------------------------------------------------------------
|
|
|
|
Function m_OnFail; // Callback when something failed while running a command.
|
|
|
|
Function m_OnAuth; // Callback if an invoker failed to authenticate properly.
|
2015-11-07 11:17:39 +01:00
|
|
|
|
2016-05-22 05:20:38 +02:00
|
|
|
protected:
|
2015-11-07 11:17:39 +01:00
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------
|
2016-02-20 23:25:00 +01:00
|
|
|
* Attach a command listener to a certain name.
|
2015-11-07 11:17:39 +01:00
|
|
|
*/
|
2016-03-15 09:56:00 +01:00
|
|
|
Object & Attach(const String & name, CmdListener * ptr, bool autorel);
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------
|
|
|
|
* Detach a command listener from a certain name.
|
|
|
|
*/
|
|
|
|
void Detach(const String & name);
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------
|
|
|
|
* Detach a command listener from a certain name.
|
|
|
|
*/
|
|
|
|
void Detach(CmdListener * ptr);
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------
|
|
|
|
* See whether a certain name exist in the command list.
|
|
|
|
*/
|
|
|
|
bool Attached(const String & name) const;
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------
|
|
|
|
* See whether a certain instance exist in the command list.
|
|
|
|
*/
|
|
|
|
bool Attached(const CmdListener * ptr) const;
|
2015-11-07 11:17:39 +01:00
|
|
|
|
2016-05-22 05:20:38 +02:00
|
|
|
/* --------------------------------------------------------------------------------------------
|
|
|
|
* Forward error message to the error callback.
|
|
|
|
*/
|
|
|
|
template < typename T > void SqError(Int32 type, CSStr msg, T data)
|
|
|
|
{
|
|
|
|
// Is there a callback that deals with errors?
|
|
|
|
if (m_OnFail.IsNull())
|
|
|
|
return;
|
|
|
|
// Attempt to forward the error to that callback
|
|
|
|
try
|
|
|
|
{
|
|
|
|
m_OnFail.Execute< Int32, CSStr, T >(type, msg, data);
|
|
|
|
}
|
|
|
|
catch (const Sqrat::Exception & e)
|
|
|
|
{
|
|
|
|
// We can only log this incident and in the future maybe also include the location
|
|
|
|
LogErr("Command error callback failed [%s]", e.Message().c_str());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------
|
|
|
|
* Default constructor.
|
|
|
|
*/
|
|
|
|
CmdManager();
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------
|
|
|
|
* Copy constructor. (disabled)
|
|
|
|
*/
|
|
|
|
CmdManager(const CmdManager & o) = delete;
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------
|
|
|
|
* Move constructor. (disabled)
|
|
|
|
*/
|
|
|
|
CmdManager(CmdManager && o) = delete;
|
2015-11-07 11:17:39 +01:00
|
|
|
|
2015-11-08 11:36:23 +01:00
|
|
|
/* --------------------------------------------------------------------------------------------
|
2016-02-20 23:25:00 +01:00
|
|
|
* Destructor.
|
2015-11-08 11:36:23 +01:00
|
|
|
*/
|
2016-02-20 23:25:00 +01:00
|
|
|
~CmdManager();
|
2015-11-08 11:36:23 +01:00
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------
|
2016-05-22 05:20:38 +02:00
|
|
|
* Copy assignment operator. (disabled)
|
2015-11-08 11:36:23 +01:00
|
|
|
*/
|
2016-05-22 05:20:38 +02:00
|
|
|
CmdManager & operator = (const CmdManager & o) = delete;
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------
|
|
|
|
* Move assignment operator. (disabled)
|
|
|
|
*/
|
|
|
|
CmdManager & operator = (CmdManager && o) = delete;
|
|
|
|
|
|
|
|
public:
|
2015-11-08 11:36:23 +01:00
|
|
|
|
2016-05-22 05:20:38 +02:00
|
|
|
/* --------------------------------------------------------------------------------------------
|
|
|
|
* Retrieve the command manager instance.
|
|
|
|
*/
|
|
|
|
static CmdManager & Get()
|
|
|
|
{
|
|
|
|
return s_Inst;
|
2016-02-20 23:25:00 +01:00
|
|
|
}
|
2015-11-08 11:36:23 +01:00
|
|
|
|
2016-05-22 05:20:38 +02:00
|
|
|
/* --------------------------------------------------------------------------------------------
|
|
|
|
* Initialize the command manager instance.
|
|
|
|
*/
|
|
|
|
void Initialize();
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------
|
|
|
|
* Terminate the command manager instance.
|
|
|
|
*/
|
|
|
|
void Deinitialize();
|
|
|
|
|
2015-11-08 11:36:23 +01:00
|
|
|
/* --------------------------------------------------------------------------------------------
|
2016-03-15 09:56:00 +01:00
|
|
|
* Sort the command list in an ascending order.
|
2015-11-08 11:36:23 +01:00
|
|
|
*/
|
2016-03-15 09:56:00 +01:00
|
|
|
void Sort();
|
2015-11-08 11:36:23 +01:00
|
|
|
|
2016-03-15 13:12:14 +01:00
|
|
|
/* --------------------------------------------------------------------------------------------
|
|
|
|
* Retrieve the number of commands.
|
|
|
|
*/
|
|
|
|
Uint32 Count() const
|
|
|
|
{
|
|
|
|
return static_cast< Uint32 >(m_Commands.size());
|
|
|
|
}
|
|
|
|
|
2015-11-08 11:36:23 +01:00
|
|
|
/* --------------------------------------------------------------------------------------------
|
2016-03-15 09:56:00 +01:00
|
|
|
* Sort the command list in an ascending order.
|
2015-11-07 11:17:39 +01:00
|
|
|
*/
|
2016-03-15 09:56:00 +01:00
|
|
|
const Object & FindByName(const String & name);
|
|
|
|
|
2015-11-07 11:17:39 +01:00
|
|
|
/* --------------------------------------------------------------------------------------------
|
2016-02-20 23:25:00 +01:00
|
|
|
* Retrieve the error callback.
|
2015-11-07 11:17:39 +01:00
|
|
|
*/
|
2016-05-22 05:20:38 +02:00
|
|
|
Function & GetOnFail()
|
2016-02-20 23:25:00 +01:00
|
|
|
{
|
2016-05-22 05:20:38 +02:00
|
|
|
return m_OnFail;
|
2016-02-20 23:25:00 +01:00
|
|
|
}
|
2015-11-07 11:17:39 +01:00
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------
|
2016-02-20 23:25:00 +01:00
|
|
|
* Modify the error callback.
|
2015-11-07 11:17:39 +01:00
|
|
|
*/
|
2016-05-22 05:20:38 +02:00
|
|
|
void SetOnFail(Object & env, Function & func)
|
2016-02-20 23:25:00 +01:00
|
|
|
{
|
2016-05-22 05:20:38 +02:00
|
|
|
m_OnFail = Function(env.GetVM(), env, func.GetFunc());
|
2016-02-20 23:25:00 +01:00
|
|
|
}
|
2015-11-07 11:17:39 +01:00
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------
|
2016-02-20 23:25:00 +01:00
|
|
|
* Retrieve the authentication callback.
|
2015-11-07 11:17:39 +01:00
|
|
|
*/
|
2016-02-20 23:25:00 +01:00
|
|
|
Function & GetOnAuth()
|
|
|
|
{
|
|
|
|
return m_OnAuth;
|
|
|
|
}
|
2015-11-07 11:17:39 +01:00
|
|
|
|
2015-11-08 11:36:23 +01:00
|
|
|
/* --------------------------------------------------------------------------------------------
|
2016-02-20 23:25:00 +01:00
|
|
|
* Modify the authentication callback.
|
2015-11-08 11:36:23 +01:00
|
|
|
*/
|
2016-02-20 23:25:00 +01:00
|
|
|
void SetOnAuth(Object & env, Function & func)
|
|
|
|
{
|
|
|
|
m_OnAuth = Function(env.GetVM(), env, func.GetFunc());
|
|
|
|
}
|
2015-11-08 22:52:56 +01:00
|
|
|
|
2016-06-08 15:26:55 +02:00
|
|
|
/* --------------------------------------------------------------------------------------------
|
|
|
|
* Retrieve the identifier of the last invoker.
|
|
|
|
*/
|
|
|
|
bool IsContext() const
|
|
|
|
{
|
|
|
|
return !!m_Context;
|
|
|
|
}
|
|
|
|
|
2015-11-08 22:52:56 +01:00
|
|
|
/* --------------------------------------------------------------------------------------------
|
2016-02-20 23:25:00 +01:00
|
|
|
* Retrieve the identifier of the last invoker.
|
2015-11-08 22:52:56 +01:00
|
|
|
*/
|
2016-02-20 23:25:00 +01:00
|
|
|
Int32 GetInvoker() const
|
|
|
|
{
|
2016-06-08 15:26:55 +02:00
|
|
|
// See if there's an execution context available
|
|
|
|
if (!m_Context)
|
|
|
|
{
|
|
|
|
STHROWF("No active execution context");
|
|
|
|
}
|
|
|
|
// Return the requested information
|
|
|
|
return m_Context->mInvoker;
|
2016-02-20 23:25:00 +01:00
|
|
|
}
|
2015-11-08 11:36:23 +01:00
|
|
|
|
2015-11-07 11:17:39 +01:00
|
|
|
/* --------------------------------------------------------------------------------------------
|
2016-03-17 21:06:14 +01:00
|
|
|
* Retrieve the currently active command instance.
|
|
|
|
*/
|
|
|
|
const CmdListener * GetInstance() const
|
|
|
|
{
|
2016-06-08 15:26:55 +02:00
|
|
|
// See if there's an execution context available
|
|
|
|
if (!m_Context)
|
|
|
|
{
|
|
|
|
STHROWF("No active execution context");
|
|
|
|
}
|
|
|
|
// Return the requested information
|
|
|
|
return m_Context->mInstance;
|
2016-03-17 21:06:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------
|
|
|
|
* Retrieve the currently active command object.
|
|
|
|
*/
|
|
|
|
const Object & GetObject() const
|
|
|
|
{
|
2016-06-08 15:26:55 +02:00
|
|
|
// See if there's an execution context available
|
|
|
|
if (!m_Context)
|
|
|
|
{
|
|
|
|
STHROWF("No active execution context");
|
|
|
|
}
|
|
|
|
// Return the requested information
|
|
|
|
return m_Context->mObject;
|
2016-03-17 21:06:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------
|
|
|
|
* Retrieve the currently active command name.
|
2015-11-07 11:17:39 +01:00
|
|
|
*/
|
2016-03-17 21:06:14 +01:00
|
|
|
const String & GetCommand() const
|
2016-02-20 23:25:00 +01:00
|
|
|
{
|
2016-06-08 15:26:55 +02:00
|
|
|
// See if there's an execution context available
|
|
|
|
if (!m_Context)
|
|
|
|
{
|
|
|
|
STHROWF("No active execution context");
|
|
|
|
}
|
|
|
|
// Return the requested information
|
|
|
|
return m_Context->mCommand;
|
2016-02-20 23:25:00 +01:00
|
|
|
}
|
2015-11-07 11:17:39 +01:00
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------
|
2016-03-17 21:06:14 +01:00
|
|
|
* Retrieve the currently active command argument.
|
2015-11-07 11:17:39 +01:00
|
|
|
*/
|
2016-03-17 21:06:14 +01:00
|
|
|
const String & GetArgument() const
|
2016-02-20 23:25:00 +01:00
|
|
|
{
|
2016-06-08 15:26:55 +02:00
|
|
|
// See if there's an execution context available
|
|
|
|
if (!m_Context)
|
|
|
|
{
|
|
|
|
STHROWF("No active execution context");
|
|
|
|
}
|
|
|
|
// Return the requested information
|
|
|
|
return m_Context->mArgument;
|
2016-02-20 23:25:00 +01:00
|
|
|
}
|
2015-11-07 11:17:39 +01:00
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------
|
2016-03-15 09:56:00 +01:00
|
|
|
* Run a command under a specific player.
|
2015-11-07 11:17:39 +01:00
|
|
|
*/
|
2016-02-20 23:25:00 +01:00
|
|
|
Int32 Run(Int32 invoker, CSStr command);
|
2015-11-07 11:17:39 +01:00
|
|
|
|
|
|
|
protected:
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------
|
2016-02-20 23:25:00 +01:00
|
|
|
* Attempt to execute the specified command.
|
2015-11-07 11:17:39 +01:00
|
|
|
*/
|
2016-06-08 15:26:55 +02:00
|
|
|
Int32 Exec(Context & ctx);
|
2015-11-07 11:17:39 +01:00
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------
|
2016-02-20 23:25:00 +01:00
|
|
|
* Attempt to parse the specified argument.
|
2015-11-07 11:17:39 +01:00
|
|
|
*/
|
2016-06-08 15:26:55 +02:00
|
|
|
bool Parse(Context & ctx);
|
2015-11-07 11:17:39 +01:00
|
|
|
|
2016-03-15 09:56:00 +01:00
|
|
|
public:
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------
|
|
|
|
* Create command instances and obtain the associated object.
|
|
|
|
*/
|
|
|
|
Object & Create(CSStr name);
|
|
|
|
Object & Create(CSStr name, CSStr spec);
|
|
|
|
Object & Create(CSStr name, CSStr spec, Array & tags);
|
|
|
|
Object & Create(CSStr name, CSStr spec, Uint8 min, Uint8 max);
|
|
|
|
Object & Create(CSStr name, CSStr spec, Array & tags, Uint8 min, Uint8 max);
|
2015-11-07 11:17:39 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
/* ------------------------------------------------------------------------------------------------
|
2016-02-20 23:25:00 +01:00
|
|
|
* Attaches to a command name and listens for invocations.
|
2015-11-07 11:17:39 +01:00
|
|
|
*/
|
|
|
|
class CmdListener
|
|
|
|
{
|
|
|
|
// --------------------------------------------------------------------------------------------
|
|
|
|
friend class CmdManager;
|
|
|
|
|
2016-03-15 09:56:00 +01:00
|
|
|
/* --------------------------------------------------------------------------------------------
|
|
|
|
* Base constructors.
|
|
|
|
*/
|
|
|
|
CmdListener(CSStr name);
|
|
|
|
CmdListener(CSStr name, CSStr spec);
|
|
|
|
CmdListener(CSStr name, CSStr spec, Array & tags);
|
|
|
|
CmdListener(CSStr name, CSStr spec, Uint8 min, Uint8 max);
|
|
|
|
CmdListener(CSStr name, CSStr spec, Array & tags, Uint8 min, Uint8 max);
|
|
|
|
|
2015-11-07 11:17:39 +01:00
|
|
|
/* --------------------------------------------------------------------------------------------
|
2016-02-20 23:25:00 +01:00
|
|
|
* Copy constructor. (disabled)
|
2015-11-07 11:17:39 +01:00
|
|
|
*/
|
2016-02-20 23:25:00 +01:00
|
|
|
CmdListener(const CmdListener &);
|
2015-11-07 11:17:39 +01:00
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------
|
2016-02-20 23:25:00 +01:00
|
|
|
* Copy assignment operator. (disabled)
|
2015-11-07 11:17:39 +01:00
|
|
|
*/
|
2016-02-20 23:25:00 +01:00
|
|
|
CmdListener & operator = (const CmdListener &);
|
2015-11-07 11:17:39 +01:00
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------
|
2016-02-20 23:25:00 +01:00
|
|
|
* Initialize the instance for the first time.
|
2015-11-07 11:17:39 +01:00
|
|
|
*/
|
2016-02-20 23:25:00 +01:00
|
|
|
void Init(CSStr name, CSStr spec, Array & tags, Uint8 min, Uint8 max);
|
2015-11-07 11:17:39 +01:00
|
|
|
|
2016-02-20 23:25:00 +01:00
|
|
|
public:
|
2015-11-07 11:17:39 +01:00
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------
|
|
|
|
* Destructor.
|
|
|
|
*/
|
|
|
|
~CmdListener();
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------
|
2016-02-20 23:25:00 +01:00
|
|
|
* Used by the script engine to compare two instances of this type.
|
2015-11-07 11:17:39 +01:00
|
|
|
*/
|
2016-02-20 23:25:00 +01:00
|
|
|
Int32 Cmp(const CmdListener & o) const;
|
2015-11-07 11:17:39 +01:00
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------
|
2016-02-20 23:25:00 +01:00
|
|
|
* Used by the script engine to convert this instance to a string.
|
2015-11-07 11:17:39 +01:00
|
|
|
*/
|
2016-02-20 23:25:00 +01:00
|
|
|
CSStr ToString() const;
|
2015-11-07 11:17:39 +01:00
|
|
|
|
2016-03-15 09:56:00 +01:00
|
|
|
/* --------------------------------------------------------------------------------------------
|
|
|
|
* Used by the script engine to retrieve the name from instances of this type.
|
|
|
|
*/
|
|
|
|
static SQInteger Typename(HSQUIRRELVM vm);
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------
|
|
|
|
* Attach the listener instance to the associated command name.
|
|
|
|
*/
|
|
|
|
void Attach();
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------
|
|
|
|
* Detach the listener instance from the associated command name.
|
|
|
|
*/
|
|
|
|
void Detach();
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------
|
|
|
|
* See whether the listener instance is attached to the associated command name.
|
|
|
|
*/
|
|
|
|
bool Attached() const;
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------
|
|
|
|
* Retrieve the flags of the specified argument.
|
|
|
|
*/
|
2016-02-20 23:25:00 +01:00
|
|
|
Uint8 GetArgFlags(Uint32 idx) const;
|
2015-11-07 11:17:39 +01:00
|
|
|
|
2016-03-15 09:56:00 +01:00
|
|
|
/* --------------------------------------------------------------------------------------------
|
|
|
|
* Retrieve the name that triggers this command listener instance.
|
|
|
|
*/
|
2016-02-20 23:25:00 +01:00
|
|
|
CSStr GetName() const;
|
2016-03-15 09:56:00 +01:00
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------
|
|
|
|
* Retrieve the name that triggers this command listener instance.
|
|
|
|
*/
|
2016-02-20 23:25:00 +01:00
|
|
|
void SetName(CSStr name);
|
2015-11-08 21:31:03 +01:00
|
|
|
|
2016-03-15 09:56:00 +01:00
|
|
|
/* --------------------------------------------------------------------------------------------
|
|
|
|
* Retrieve the argument specification string.
|
|
|
|
*/
|
2016-02-20 23:25:00 +01:00
|
|
|
CSStr GetSpec() const;
|
2016-03-15 09:56:00 +01:00
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------
|
|
|
|
* Modify the argument specification string.
|
|
|
|
*/
|
2016-02-20 23:25:00 +01:00
|
|
|
void SetSpec(CSStr spec);
|
2015-11-08 21:31:03 +01:00
|
|
|
|
2016-03-15 09:56:00 +01:00
|
|
|
/* --------------------------------------------------------------------------------------------
|
|
|
|
* Retrieve the argument tags array.
|
|
|
|
*/
|
2015-11-08 21:31:03 +01:00
|
|
|
Array GetArgTags() const;
|
2016-03-15 09:56:00 +01:00
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------
|
|
|
|
* Modify the argument tags array.
|
|
|
|
*/
|
2015-11-08 21:31:03 +01:00
|
|
|
void SetArgTags(Array & tags);
|
|
|
|
|
2016-03-15 09:56:00 +01:00
|
|
|
/* --------------------------------------------------------------------------------------------
|
|
|
|
* Retrieve the help message associated with this command listener instance.
|
|
|
|
*/
|
2016-02-20 23:25:00 +01:00
|
|
|
CSStr GetHelp() const;
|
2016-03-15 09:56:00 +01:00
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------
|
|
|
|
* Modify the help message associated with this command listener instance.
|
|
|
|
*/
|
2016-02-20 23:25:00 +01:00
|
|
|
void SetHelp(CSStr help);
|
2015-11-07 11:17:39 +01:00
|
|
|
|
2016-03-15 09:56:00 +01:00
|
|
|
/* --------------------------------------------------------------------------------------------
|
|
|
|
* Retrieve the informational message associated with this command listener instance.
|
|
|
|
*/
|
2016-02-20 23:25:00 +01:00
|
|
|
CSStr GetInfo() const;
|
2016-03-15 09:56:00 +01:00
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------
|
|
|
|
* Modify the informational message associated with this command listener instance.
|
|
|
|
*/
|
2016-02-20 23:25:00 +01:00
|
|
|
void SetInfo(CSStr info);
|
2015-11-07 11:17:39 +01:00
|
|
|
|
2016-03-15 09:56:00 +01:00
|
|
|
/* --------------------------------------------------------------------------------------------
|
|
|
|
* Retrieve the authority level required to execute this command listener instance.
|
|
|
|
*/
|
2016-02-20 23:25:00 +01:00
|
|
|
Int32 GetAuthority() const;
|
2016-03-15 09:56:00 +01:00
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------
|
|
|
|
* Modify the authority level required to execute this command listener instance.
|
|
|
|
*/
|
2016-02-20 23:25:00 +01:00
|
|
|
void SetAuthority(Int32 level);
|
2015-11-07 11:17:39 +01:00
|
|
|
|
2016-03-15 09:56:00 +01:00
|
|
|
/* --------------------------------------------------------------------------------------------
|
|
|
|
* See whether this command listener instance requires explicit authority inspection.
|
|
|
|
*/
|
2016-02-20 23:25:00 +01:00
|
|
|
bool GetProtected() const;
|
2016-03-15 09:56:00 +01:00
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------
|
|
|
|
* Set whether this command listener instance requires explicit authority inspection.
|
|
|
|
*/
|
2016-02-20 23:25:00 +01:00
|
|
|
void SetProtected(bool toggle);
|
2015-11-07 11:17:39 +01:00
|
|
|
|
2016-03-15 09:56:00 +01:00
|
|
|
/* --------------------------------------------------------------------------------------------
|
|
|
|
* See whether this command listener instance is currently ignoring calls.
|
|
|
|
*/
|
2015-11-08 19:53:50 +01:00
|
|
|
bool GetSuspended() const;
|
2016-03-15 09:56:00 +01:00
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------
|
|
|
|
* Set whether this command listener instance should start ignoring calls.
|
|
|
|
*/
|
2015-11-08 19:53:50 +01:00
|
|
|
void SetSuspended(bool toggle);
|
2015-11-07 11:17:39 +01:00
|
|
|
|
2016-03-15 09:56:00 +01:00
|
|
|
/* --------------------------------------------------------------------------------------------
|
|
|
|
* See whether this command listener instance receives arguments in an associative container.
|
|
|
|
*/
|
2016-02-20 23:25:00 +01:00
|
|
|
bool GetAssociate() const;
|
2016-03-15 09:56:00 +01:00
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------
|
|
|
|
* Set whether this command listener instance receives arguments in an associative container.
|
|
|
|
*/
|
2016-02-20 23:25:00 +01:00
|
|
|
void SetAssociate(bool toggle);
|
2015-11-07 11:17:39 +01:00
|
|
|
|
2016-03-15 09:56:00 +01:00
|
|
|
/* --------------------------------------------------------------------------------------------
|
|
|
|
* Retrieve the maximum arguments supported by this command listener.
|
|
|
|
*/
|
2016-02-20 23:25:00 +01:00
|
|
|
Uint8 GetMinArgC() const;
|
2016-03-15 09:56:00 +01:00
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------
|
|
|
|
* Modify the minimum arguments supported by this command listener.
|
|
|
|
*/
|
2015-11-08 22:25:40 +01:00
|
|
|
void SetMinArgC(Uint8 val);
|
2015-11-07 11:17:39 +01:00
|
|
|
|
2016-03-15 09:56:00 +01:00
|
|
|
/* --------------------------------------------------------------------------------------------
|
|
|
|
* Retrieve the maximum arguments supported by this command listener.
|
|
|
|
*/
|
2015-11-08 22:25:40 +01:00
|
|
|
Uint8 GetMaxArgC() const;
|
|
|
|
|
2016-03-15 09:56:00 +01:00
|
|
|
/* --------------------------------------------------------------------------------------------
|
|
|
|
* Modify the maximum arguments supported by this command listener.
|
|
|
|
*/
|
|
|
|
void SetMaxArgC(Uint8 val);
|
2015-11-07 11:17:39 +01:00
|
|
|
|
2016-03-15 09:56:00 +01:00
|
|
|
/* --------------------------------------------------------------------------------------------
|
|
|
|
* Retrieve the function that must be called when this command listener is executed.
|
|
|
|
*/
|
2016-02-20 23:25:00 +01:00
|
|
|
Function & GetOnExec();
|
2016-03-15 09:56:00 +01:00
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------
|
|
|
|
* Modify the function that must be called when this command listener is executed.
|
|
|
|
*/
|
2016-02-20 23:25:00 +01:00
|
|
|
void SetOnExec(Object & env, Function & func);
|
2015-11-07 11:17:39 +01:00
|
|
|
|
2016-03-15 09:56:00 +01:00
|
|
|
/* --------------------------------------------------------------------------------------------
|
|
|
|
* Retrieve the function that must be called when this command listener needs to authenticate.
|
|
|
|
*/
|
2016-02-20 23:25:00 +01:00
|
|
|
Function & GetOnAuth();
|
2016-03-15 09:56:00 +01:00
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------
|
|
|
|
* Modify the function that must be called when this command listener needs to authenticate.
|
|
|
|
*/
|
2016-02-20 23:25:00 +01:00
|
|
|
void SetOnAuth(Object & env, Function & func);
|
2015-11-08 22:52:56 +01:00
|
|
|
|
2016-03-15 09:56:00 +01:00
|
|
|
/* --------------------------------------------------------------------------------------------
|
|
|
|
* Retrieve the function that must be called when this command listener finished execution.
|
|
|
|
*/
|
2016-02-20 23:25:00 +01:00
|
|
|
Function & GetOnPost();
|
2016-03-15 09:56:00 +01:00
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------
|
|
|
|
* Modify the function that must be called when this command listener finished execution.
|
|
|
|
*/
|
2016-02-20 23:25:00 +01:00
|
|
|
void SetOnPost(Object & env, Function & func);
|
2015-11-07 11:17:39 +01:00
|
|
|
|
2016-02-20 23:25:00 +01:00
|
|
|
// --------------------------------------------------------------------------------------------
|
|
|
|
Function & GetOnFail();
|
|
|
|
void SetOnFail(Object & env, Function & func);
|
2015-11-07 11:17:39 +01:00
|
|
|
|
2016-03-15 09:56:00 +01:00
|
|
|
/* --------------------------------------------------------------------------------------------
|
|
|
|
* Retrieve the function that must be called when this command listener failed to execute.
|
|
|
|
*/
|
2016-02-20 23:25:00 +01:00
|
|
|
CSStr GetArgTag(Uint32 arg) const;
|
2016-03-15 09:56:00 +01:00
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------
|
|
|
|
* Modify the function that must be called when this command listener failed to execute.
|
|
|
|
*/
|
2016-02-20 23:25:00 +01:00
|
|
|
void SetArgTag(Uint32 arg, CSStr name);
|
2015-11-07 11:17:39 +01:00
|
|
|
|
2016-03-15 09:56:00 +01:00
|
|
|
/* --------------------------------------------------------------------------------------------
|
|
|
|
* Use the command listener argument properties to generate an informational message.
|
|
|
|
*/
|
2016-02-20 23:25:00 +01:00
|
|
|
void GenerateInfo(bool full);
|
2015-11-07 11:17:39 +01:00
|
|
|
|
2016-03-15 09:56:00 +01:00
|
|
|
/* --------------------------------------------------------------------------------------------
|
|
|
|
* See whether whether the specified argument can be used on this command listener instance.
|
|
|
|
*/
|
2016-02-20 23:25:00 +01:00
|
|
|
bool ArgCheck(Uint32 arg, Uint8 flag) const;
|
2015-11-08 21:31:03 +01:00
|
|
|
|
2016-03-15 09:56:00 +01:00
|
|
|
/* --------------------------------------------------------------------------------------------
|
|
|
|
* See whether the specified player entity has the proper authority to run this command.
|
|
|
|
*/
|
2016-02-20 23:25:00 +01:00
|
|
|
bool AuthCheck(CPlayer & player);
|
2016-03-15 09:56:00 +01:00
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------
|
|
|
|
* See whether the specified player entity has the proper authority to run this command.
|
|
|
|
*/
|
2016-02-20 23:25:00 +01:00
|
|
|
bool AuthCheckID(Int32 id);
|
2015-11-07 11:17:39 +01:00
|
|
|
|
2016-02-20 23:25:00 +01:00
|
|
|
protected:
|
2015-11-07 11:17:39 +01:00
|
|
|
|
2016-02-20 23:25:00 +01:00
|
|
|
// --------------------------------------------------------------------------------------------
|
|
|
|
typedef Uint8 ArgSpec[SQMOD_MAX_CMD_ARGS];
|
|
|
|
typedef String ArgTags[SQMOD_MAX_CMD_ARGS];
|
2015-11-07 11:17:39 +01:00
|
|
|
|
2015-11-08 21:31:03 +01:00
|
|
|
/* --------------------------------------------------------------------------------------------
|
2016-03-15 09:56:00 +01:00
|
|
|
* Execute the designated callback by passing the arguments in their specified order.
|
2015-11-08 21:31:03 +01:00
|
|
|
*/
|
2016-02-20 23:25:00 +01:00
|
|
|
SQInteger Execute(Object & invoker, Array & args);
|
2015-11-08 21:31:03 +01:00
|
|
|
|
2015-11-07 11:17:39 +01:00
|
|
|
/* --------------------------------------------------------------------------------------------
|
2016-03-15 09:56:00 +01:00
|
|
|
* Execute the designated callback by passing the arguments using an associative container.
|
2015-11-07 11:17:39 +01:00
|
|
|
*/
|
2016-02-20 23:25:00 +01:00
|
|
|
SQInteger Execute(Object & invoker, Table & args);
|
2015-11-07 11:17:39 +01:00
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------
|
2016-03-15 09:56:00 +01:00
|
|
|
* Process the specified string and extract the argument properties in it.
|
2015-11-07 11:17:39 +01:00
|
|
|
*/
|
2016-03-10 04:57:13 +01:00
|
|
|
void ProcSpec(CSStr spec);
|
2015-11-07 11:17:39 +01:00
|
|
|
|
2016-02-20 23:25:00 +01:00
|
|
|
private:
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------------------------------
|
2015-11-07 11:17:39 +01:00
|
|
|
String m_Name;
|
|
|
|
|
2016-02-20 23:25:00 +01:00
|
|
|
// --------------------------------------------------------------------------------------------
|
|
|
|
ArgSpec m_ArgSpec;
|
|
|
|
ArgTags m_ArgTags;
|
2015-11-07 11:17:39 +01:00
|
|
|
|
2016-02-20 23:25:00 +01:00
|
|
|
// --------------------------------------------------------------------------------------------
|
|
|
|
Uint8 m_MinArgc;
|
|
|
|
Uint8 m_MaxArgc;
|
2015-11-07 11:17:39 +01:00
|
|
|
|
2016-02-20 23:25:00 +01:00
|
|
|
// --------------------------------------------------------------------------------------------
|
|
|
|
String m_Spec;
|
|
|
|
String m_Help;
|
2015-11-07 11:17:39 +01:00
|
|
|
String m_Info;
|
|
|
|
|
2016-02-20 23:25:00 +01:00
|
|
|
// --------------------------------------------------------------------------------------------
|
2015-11-08 19:53:50 +01:00
|
|
|
Function m_OnExec;
|
|
|
|
Function m_OnAuth;
|
2016-02-20 23:25:00 +01:00
|
|
|
Function m_OnPost;
|
|
|
|
Function m_OnFail;
|
2015-11-07 11:17:39 +01:00
|
|
|
|
2016-02-20 23:25:00 +01:00
|
|
|
// --------------------------------------------------------------------------------------------
|
|
|
|
Int32 m_Authority;
|
2015-11-07 11:17:39 +01:00
|
|
|
|
2016-02-20 23:25:00 +01:00
|
|
|
// --------------------------------------------------------------------------------------------
|
|
|
|
bool m_Protected;
|
2015-11-08 19:53:50 +01:00
|
|
|
bool m_Suspended;
|
2016-02-20 23:25:00 +01:00
|
|
|
bool m_Associate;
|
2015-11-07 11:17:39 +01:00
|
|
|
};
|
|
|
|
|
2016-05-22 05:20:38 +02:00
|
|
|
/* ------------------------------------------------------------------------------------------------
|
|
|
|
* Converts a command specifier to a string.
|
|
|
|
*/
|
|
|
|
CSStr CmdArgSpecToStr(Uint8 spec);
|
|
|
|
|
2015-11-07 11:17:39 +01:00
|
|
|
} // Namespace:: SqMod
|
|
|
|
|
|
|
|
#endif // _COMMAND_HPP_
|