From cb15d250f98fe8dc0dfbea533a07fb036e167eb3 Mon Sep 17 00:00:00 2001 From: momo5502 Date: Thu, 24 Dec 2015 17:30:36 +0100 Subject: [PATCH] InfoString class. --- iw4/Components/Network.cpp | 35 ++++++++++-------- iw4/Utils/Utils.cpp | 74 ++++++++++++++++++++++++++++++++++++++ iw4/Utils/Utils.hpp | 18 ++++++++++ 3 files changed, 113 insertions(+), 14 deletions(-) diff --git a/iw4/Components/Network.cpp b/iw4/Components/Network.cpp index abcfeb1a..059c1890 100644 --- a/iw4/Components/Network.cpp +++ b/iw4/Components/Network.cpp @@ -110,20 +110,27 @@ namespace Components // Install packet deploy hook Utils::Hook::Set(0x5AA715, (DWORD)Network::DeployPacketStub - 0x5AA713 - 6); -// Network::Handle("infoResponse", [] (Address address, Game::msg_t* message) -// { -// OutputDebugStringA(Utils::VA("Inforesponse received: %s!", address.GetString())); -// }); -// -// Network::Handle("getInfo", [] (Address address, Game::msg_t* message) -// { -// OutputDebugStringA(Utils::VA("getinfo received: %s!", address.GetString())); -// }); -// -// Command::Add("zob", [] (Command::Params params) -// { -// Network::Send(Game::NS_CLIENT, Network::Address("localhost:28960"), "getinfo xxx\n"); -// }); + Network::Handle("infoResponse", [] (Address address, Game::msg_t* message) + { + OutputDebugStringA(Utils::VA("Inforesponse received: %s %s!", address.GetString(), message->data)); + }); + + Network::Handle("getInfo", [] (Address address, Game::msg_t* message) + { + OutputDebugStringA(Utils::VA("getinfo received: %s!", address.GetString())); + + Utils::InfoString info; + info.Set("mapname", "mp_rust"); + info.Set("gamename", "tdm"); + 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() diff --git a/iw4/Utils/Utils.cpp b/iw4/Utils/Utils.cpp index d7f6c535..c8de385b 100644 --- a/iw4/Utils/Utils.cpp +++ b/iw4/Utils/Utils.cpp @@ -29,4 +29,78 @@ namespace Utils { return (strstr(heystack, needle) == (heystack + strlen(heystack) - strlen(needle))); } + + std::vector Explode(const std::string& str, const std::string& delimiters) + { + std::vector 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 KeyValues = Utils::Explode(buffer, "\\"); + + for (unsigned int i = 0; i < (KeyValues.size() - 1); i+=2) + { + this->KeyValuePairs[KeyValues[i]] = KeyValues[i + 1]; + } + } } \ No newline at end of file diff --git a/iw4/Utils/Utils.hpp b/iw4/Utils/Utils.hpp index de95b9db..4ba36bdd 100644 --- a/iw4/Utils/Utils.hpp +++ b/iw4/Utils/Utils.hpp @@ -3,4 +3,22 @@ namespace Utils const char *VA(const char *fmt, ...); std::string StrToLower(std::string input); bool EndsWith(const char* heystack, const char* needle); + std::vector 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 KeyValuePairs; + void Parse(std::string buffer); + }; }