[Command] Implement abstract parameter interface

This commit is contained in:
momo5502 2016-12-07 18:18:11 +01:00
parent 107076c26a
commit d6a2dbdd2b
4 changed files with 94 additions and 31 deletions

View File

@ -67,7 +67,7 @@ namespace Components
std::string connectString(format, len); std::string connectString(format, len);
Game::SV_Cmd_TokenizeString(connectString.data()); Game::SV_Cmd_TokenizeString(connectString.data());
Command::Params params(true); Command::IServerParams params;
if (params.length() < 3) if (params.length() < 3)
{ {
@ -141,7 +141,7 @@ namespace Components
Game::SV_Cmd_TokenizeString(connectData.infostring().data()); Game::SV_Cmd_TokenizeString(connectData.infostring().data());
// Access the params // Access the params
Command::Params params(true); Command::IServerParams params;
// Ensure there are enough params // Ensure there are enough params
if (params.length() < 3) if (params.length() < 3)

View File

@ -6,20 +6,7 @@ namespace Components
std::map<std::string, wink::slot<Command::Callback>> Command::FunctionMap; std::map<std::string, wink::slot<Command::Callback>> Command::FunctionMap;
std::map<std::string, wink::slot<Command::Callback>> Command::FunctionMapSV; std::map<std::string, wink::slot<Command::Callback>> Command::FunctionMapSV;
char* Command::Params::operator[](size_t index) std::string Command::IParams::join(size_t startIndex)
{
if (index >= this->length()) return "";
if (this->isSV) return Game::cmd_argv_sv[this->commandId][index];
else return Game::cmd_argv[this->commandId][index];
}
size_t Command::Params::length()
{
if (this->isSV) return Game::cmd_argc_sv[this->commandId];
else return Game::cmd_argc[this->commandId];
}
std::string Command::Params::join(size_t startIndex)
{ {
std::string result; std::string result;
@ -32,6 +19,28 @@ namespace Components
return result; return result;
} }
char* Command::IClientParams::operator[](size_t index)
{
if (index >= this->length()) return "";
return Game::cmd_argv[this->commandId][index];
}
size_t Command::IClientParams::length()
{
return Game::cmd_argc[this->commandId];
}
char* Command::IServerParams::operator[](size_t index)
{
if (index >= this->length()) return "";
return Game::cmd_argv_sv[this->commandId][index];
}
size_t Command::IServerParams::length()
{
return Game::cmd_argc_sv[this->commandId];
}
void Command::Add(const char* name, Command::Callback* callback) void Command::Add(const char* name, Command::Callback* callback)
{ {
std::string command = Utils::String::ToLower(name); std::string command = Utils::String::ToLower(name);
@ -121,25 +130,25 @@ namespace Components
void Command::MainCallback() void Command::MainCallback()
{ {
Command::Params params(false, *Game::cmd_id); Command::IClientParams params(*Game::cmd_id);
std::string command = Utils::String::ToLower(params[0]); std::string command = Utils::String::ToLower(params[0]);
if (Command::FunctionMap.find(command) != Command::FunctionMap.end()) if (Command::FunctionMap.find(command) != Command::FunctionMap.end())
{ {
Command::FunctionMap[command](params); Command::FunctionMap[command](&params);
} }
} }
void Command::MainCallbackSV() void Command::MainCallbackSV()
{ {
Command::Params params(true, *Game::cmd_id_sv); Command::IServerParams params(*Game::cmd_id_sv);
std::string command = Utils::String::ToLower(params[0]); std::string command = Utils::String::ToLower(params[0]);
if (Command::FunctionMapSV.find(command) != Command::FunctionMapSV.end()) if (Command::FunctionMapSV.find(command) != Command::FunctionMapSV.end())
{ {
Command::FunctionMapSV[command](params); Command::FunctionMapSV[command](&params);
} }
} }

View File

@ -3,22 +3,76 @@ namespace Components
class Command : public Component class Command : public Component
{ {
public: public:
class IParams
{
public:
IParams() {};
virtual ~IParams() {};
virtual char* operator[](size_t index) = 0;
virtual size_t length() = 0;
virtual std::string join(size_t startIndex);
};
class IClientParams : public IParams
{
public:
IClientParams(unsigned int id) : commandId(id) {};
IClientParams(const IClientParams &obj) : commandId(obj.commandId) {};
IClientParams() : IClientParams(*Game::cmd_id) {};
~IClientParams() {};
char* operator[](size_t index) override;
size_t length() override;
private:
unsigned int commandId;
};
class IServerParams : public IParams
{
public:
IServerParams(unsigned int id) : commandId(id) {};
IServerParams(const IServerParams &obj) : commandId(obj.commandId) {};
IServerParams() : IServerParams(*Game::cmd_id_sv) {};
~IServerParams() {};
char* operator[](size_t index) override;
size_t length() override;
private:
unsigned int commandId;
};
class Params class Params
{ {
public: public:
Params(bool sv, DWORD id) : commandId(id), isSV(sv) {}; Params(IParams* _paramInterface) : paramInterface(_paramInterface)
Params(bool sv) : Params(sv, (sv ? *Game::cmd_id_sv : *Game::cmd_id)) {}; {
Params(const Params &obj) : commandId(obj.commandId), isSV(obj.isSV) {}; if (!paramInterface)
Params() : Params(false, *Game::cmd_id) {}; {
throw new std::invalid_argument("Invalid command parameter interface!");
}
};
char* operator[](size_t index); Params(const Params &obj) : paramInterface(obj.paramInterface) {};
size_t length();
std::string join(size_t startIndex); char* operator[](size_t index)
{
return paramInterface->operator[](index);
}
size_t length()
{
return paramInterface->length();
}
std::string join(size_t startIndex)
{
return paramInterface->join(startIndex);
}
private: private:
bool isSV; IParams* paramInterface;
DWORD commandId;
}; };
typedef void(Callback)(Command::Params params); typedef void(Callback)(Command::Params params);

View File

@ -114,7 +114,7 @@ namespace Components
void QuickPatch::CL_HandleRelayPacketCheck(Game::msg_t* msg, int client) void QuickPatch::CL_HandleRelayPacketCheck(Game::msg_t* msg, int client)
{ {
if (Command::Params().length() >= 3) if (Command::IClientParams().length() >= 3)
{ {
Game::CL_HandleRelayPacket(msg, client); Game::CL_HandleRelayPacket(msg, client);
} }
@ -122,7 +122,7 @@ namespace Components
void QuickPatch::SelectStringTableEntryInDvarStub() void QuickPatch::SelectStringTableEntryInDvarStub()
{ {
Command::Params args; Command::IClientParams args;
if (args.length() >= 4) if (args.length() >= 4)
{ {