From afc72a01625832111de8d7b477f9e86417b58924 Mon Sep 17 00:00:00 2001 From: Sandu Liviu Catalin Date: Thu, 17 Mar 2016 22:06:14 +0200 Subject: [PATCH] Fixed a bug in the command system that did not take into account the null terminator when finding the command separator. Added the option to retrieve the listener instance of the currently executed command. Moved several helper functions to where they belong. --- source/Command.cpp | 30 ++++++++++++++++-------------- source/Command.hpp | 36 ++++++++++++++++++++++-------------- 2 files changed, 38 insertions(+), 28 deletions(-) diff --git a/source/Command.cpp b/source/Command.cpp index 1825d128..9d52adb9 100644 --- a/source/Command.cpp +++ b/source/Command.cpp @@ -281,7 +281,7 @@ Int32 CmdManager::Run(Int32 invoker, CCStr command) // Where the name ends and argument begins CCStr split = command; // Find where the command name ends - while (!isspace(*split)) ++split; + while (!isspace(*split) && *split != '\0') ++split; // Are there any arguments specified? if (split != nullptr) { @@ -1602,12 +1602,6 @@ static const Object & Cmd_FindByName(CSStr name) return _Cmd->FindByName(name); } -// ------------------------------------------------------------------------------------------------ -static const Object & Cmd_Current() -{ - return _Cmd->Current(); -} - // ------------------------------------------------------------------------------------------------ static Function & Cmd_GetOnError() { @@ -1645,13 +1639,19 @@ static Int32 Cmd_GetInvokerID() } // ------------------------------------------------------------------------------------------------ -static CSStr Cmd_GetCommand() +static const Object & Cmd_GetObject() +{ + return _Cmd->GetObject(); +} + +// ------------------------------------------------------------------------------------------------ +static const String & Cmd_GetCommand() { return _Cmd->GetCommand(); } // ------------------------------------------------------------------------------------------------ -static CSStr Cmd_GetArgument() +static const String & Cmd_GetArgument() { return _Cmd->GetArgument(); } @@ -1727,7 +1727,6 @@ void Register_Command(HSQUIRRELVM vm) cmdns.Func(_SC("Sort"), &Cmd_Sort); cmdns.Func(_SC("Count"), &Cmd_Count); - cmdns.Func(_SC("Current"), &Cmd_Current); cmdns.Func(_SC("FindByName"), &Cmd_FindByName); cmdns.Func(_SC("GetOnError"), &Cmd_GetOnError); cmdns.Func(_SC("SetOnError"), &Cmd_SetOnError); @@ -1735,10 +1734,13 @@ void Register_Command(HSQUIRRELVM vm) cmdns.Func(_SC("GetOnAuth"), &Cmd_GetOnAuth); cmdns.Func(_SC("SetOnAuth"), &Cmd_SetOnAuth); cmdns.Func(_SC("BindAuth"), &Cmd_SetOnAuth); - cmdns.Func(_SC("GetInvoker"), &Cmd_GetInvoker); - cmdns.Func(_SC("GetInvokerID"), &Cmd_GetInvokerID); - cmdns.Func(_SC("GetName"), &Cmd_GetCommand); - cmdns.Func(_SC("GetText"), &Cmd_GetArgument); + cmdns.Func(_SC("Invoker"), &Cmd_GetInvoker); + cmdns.Func(_SC("InvokerID"), &Cmd_GetInvokerID); + cmdns.Func(_SC("Instance"), &Cmd_GetObject); + cmdns.Func(_SC("Name"), &Cmd_GetCommand); + cmdns.Func(_SC("Command"), &Cmd_GetCommand); + cmdns.Func(_SC("Text"), &Cmd_GetArgument); + cmdns.Func(_SC("Argument"), &Cmd_GetArgument); cmdns.Overload< Object & (CSStr) >(_SC("Create"), &Cmd_Create); cmdns.Overload< Object & (CSStr, CSStr) >(_SC("Create"), &Cmd_Create); cmdns.Overload< Object & (CSStr, CSStr, Array &) >(_SC("Create"), &Cmd_Create); diff --git a/source/Command.hpp b/source/Command.hpp index 85b74680..0eb3d804 100644 --- a/source/Command.hpp +++ b/source/Command.hpp @@ -212,14 +212,6 @@ public: */ const Object & FindByName(const String & name); - /* -------------------------------------------------------------------------------------------- - * Retrieve the currently active command object. - */ - const Object & Current() const - { - return m_Object; - } - /* -------------------------------------------------------------------------------------------- * Terminate current session. */ @@ -266,19 +258,35 @@ public: } /* -------------------------------------------------------------------------------------------- - * Retrieve the last executed command. + * Retrieve the currently active command instance. */ - CSStr GetCommand() const + const CmdListener * GetInstance() const { - return m_Command.c_str(); + return m_Instance; } /* -------------------------------------------------------------------------------------------- - * Retrieve the argument of the last executed command. + * Retrieve the currently active command object. */ - CSStr GetArgument() const + const Object & GetObject() const { - return m_Argument.c_str(); + return m_Object; + } + + /* -------------------------------------------------------------------------------------------- + * Retrieve the currently active command name. + */ + const String & GetCommand() const + { + return m_Command; + } + + /* -------------------------------------------------------------------------------------------- + * Retrieve the currently active command argument. + */ + const String & GetArgument() const + { + return m_Argument; } /* --------------------------------------------------------------------------------------------