[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);
Game::SV_Cmd_TokenizeString(connectString.data());
Command::Params params(true);
Command::IServerParams params;
if (params.length() < 3)
{
@ -141,7 +141,7 @@ namespace Components
Game::SV_Cmd_TokenizeString(connectData.infostring().data());
// Access the params
Command::Params params(true);
Command::IServerParams params;
// Ensure there are enough params
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::FunctionMapSV;
char* Command::Params::operator[](size_t index)
{
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 Command::IParams::join(size_t startIndex)
{
std::string result;
@ -32,6 +19,28 @@ namespace Components
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)
{
std::string command = Utils::String::ToLower(name);
@ -121,25 +130,25 @@ namespace Components
void Command::MainCallback()
{
Command::Params params(false, *Game::cmd_id);
Command::IClientParams params(*Game::cmd_id);
std::string command = Utils::String::ToLower(params[0]);
if (Command::FunctionMap.find(command) != Command::FunctionMap.end())
{
Command::FunctionMap[command](params);
Command::FunctionMap[command](&params);
}
}
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]);
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
{
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
{
public:
Params(bool sv, DWORD id) : commandId(id), isSV(sv) {};
Params(bool sv) : Params(sv, (sv ? *Game::cmd_id_sv : *Game::cmd_id)) {};
Params(const Params &obj) : commandId(obj.commandId), isSV(obj.isSV) {};
Params() : Params(false, *Game::cmd_id) {};
Params(IParams* _paramInterface) : paramInterface(_paramInterface)
{
if (!paramInterface)
{
throw new std::invalid_argument("Invalid command parameter interface!");
}
};
char* operator[](size_t index);
size_t length();
Params(const Params &obj) : paramInterface(obj.paramInterface) {};
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:
bool isSV;
DWORD commandId;
IParams* paramInterface;
};
typedef void(Callback)(Command::Params params);

View File

@ -114,7 +114,7 @@ namespace Components
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);
}
@ -122,7 +122,7 @@ namespace Components
void QuickPatch::SelectStringTableEntryInDvarStub()
{
Command::Params args;
Command::IClientParams args;
if (args.length() >= 4)
{