InfoString class.

This commit is contained in:
momo5502 2015-12-24 17:30:36 +01:00
parent 5f6a06fe5b
commit cb15d250f9
3 changed files with 113 additions and 14 deletions

View File

@ -110,20 +110,27 @@ namespace Components
// Install packet deploy hook // Install packet deploy hook
Utils::Hook::Set<int>(0x5AA715, (DWORD)Network::DeployPacketStub - 0x5AA713 - 6); Utils::Hook::Set<int>(0x5AA715, (DWORD)Network::DeployPacketStub - 0x5AA713 - 6);
// Network::Handle("infoResponse", [] (Address address, Game::msg_t* message) Network::Handle("infoResponse", [] (Address address, Game::msg_t* message)
// { {
// OutputDebugStringA(Utils::VA("Inforesponse received: %s!", address.GetString())); OutputDebugStringA(Utils::VA("Inforesponse received: %s %s!", address.GetString(), message->data));
// }); });
//
// Network::Handle("getInfo", [] (Address address, Game::msg_t* message) Network::Handle("getInfo", [] (Address address, Game::msg_t* message)
// { {
// OutputDebugStringA(Utils::VA("getinfo received: %s!", address.GetString())); OutputDebugStringA(Utils::VA("getinfo received: %s!", address.GetString()));
// });
// Utils::InfoString info;
// Command::Add("zob", [] (Command::Params params) info.Set("mapname", "mp_rust");
// { info.Set("gamename", "tdm");
// Network::Send(Game::NS_CLIENT, Network::Address("localhost:28960"), "getinfo xxx\n"); info.Set("testKey", "testVal");
// });
Network::Send(Game::NS_CLIENT, address, Utils::VA("infoResponse\n%s\n", info.Build().data()));
});
Command::Add("zob", [] (Command::Params params)
{
Network::Send(Game::NS_CLIENT, Network::Address("localhost:28960"), "getinfo xxx\n");
});
} }
Network::~Network() Network::~Network()

View File

@ -29,4 +29,78 @@ namespace Utils
{ {
return (strstr(heystack, needle) == (heystack + strlen(heystack) - strlen(needle))); return (strstr(heystack, needle) == (heystack + strlen(heystack) - strlen(needle)));
} }
std::vector<std::string> Explode(const std::string& str, const std::string& delimiters)
{
std::vector<std::string> tokens;
auto subStrBeginPos = str.find_first_not_of(delimiters, 0);
auto subStrEndPos = str.find_first_of(delimiters, subStrBeginPos);
while (std::string::npos != subStrBeginPos || std::string::npos != subStrEndPos)
{
tokens.push_back(str.substr(subStrBeginPos, subStrEndPos - subStrBeginPos));
subStrBeginPos = str.find_first_not_of(delimiters, subStrEndPos);
subStrEndPos = str.find_first_of(delimiters, subStrBeginPos);
}
return tokens;
}
void Replace(std::string &string, std::string find, std::string replace)
{
size_t nPos = 0;
while ((nPos = string.find(find, nPos)) != std::string::npos)
{
string = string.replace(nPos, find.length(), replace);
nPos += replace.length();
}
}
// Infostring class
void InfoString::Set(std::string key, std::string value)
{
this->KeyValuePairs[key] = value;
}
std::string InfoString::Get(std::string key)
{
if (this->KeyValuePairs.find(key) != this->KeyValuePairs.end())
{
return this->KeyValuePairs[key];
}
return "";
}
std::string InfoString::Build()
{
std::string infoString;
bool first = true;
for (auto i = this->KeyValuePairs.begin(); i != this->KeyValuePairs.end(); i++)
{
if (first) first = false;
else infoString.append("\\");
infoString.append(i->first); // Key
infoString.append("\\");
infoString.append(i->second); // Value
}
return infoString;
}
void InfoString::Parse(std::string buffer)
{
std::vector<std::string> KeyValues = Utils::Explode(buffer, "\\");
for (unsigned int i = 0; i < (KeyValues.size() - 1); i+=2)
{
this->KeyValuePairs[KeyValues[i]] = KeyValues[i + 1];
}
}
} }

View File

@ -3,4 +3,22 @@ namespace Utils
const char *VA(const char *fmt, ...); const char *VA(const char *fmt, ...);
std::string StrToLower(std::string input); std::string StrToLower(std::string input);
bool EndsWith(const char* heystack, const char* needle); bool EndsWith(const char* heystack, const char* needle);
std::vector<std::string> Explode(const std::string& str, const std::string& delimiters);
void Replace(std::string &string, std::string find, std::string replace);
class InfoString
{
public:
InfoString() {};
InfoString(std::string buffer) :InfoString() { this->Parse(buffer); };
void Set(std::string key, std::string value);
std::string Get(std::string key);
std::string Build();
private:
std::map<std::string, std::string> KeyValuePairs;
void Parse(std::string buffer);
};
} }