Merge pull request #191 from diamante0018/refactor-smol-components

Refactor some utils
This commit is contained in:
Dss0 2022-02-27 00:11:01 +01:00 committed by GitHub
commit d1da779388
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
14 changed files with 41 additions and 46 deletions

View File

@ -8,7 +8,8 @@ namespace Components
Utils::Cryptography::Token Auth::ComputeToken; Utils::Cryptography::Token Auth::ComputeToken;
Utils::Cryptography::ECC::Key Auth::GuidKey; Utils::Cryptography::ECC::Key Auth::GuidKey;
std::vector<std::uint64_t> Auth::BannedUids = { std::vector<std::uint64_t> Auth::BannedUids =
{
0xf4d2c30b712ac6e3, 0xf4d2c30b712ac6e3,
0xf7e33c4081337fa3, 0xf7e33c4081337fa3,
0x6f5597f103cc50e9 0x6f5597f103cc50e9
@ -179,8 +180,8 @@ namespace Components
Utils::InfoString infostr(params[2]); Utils::InfoString infostr(params[2]);
// Read the required data // Read the required data
std::string steamId = infostr.get("xuid"); const auto& steamId = infostr.get("xuid");
std::string challenge = infostr.get("challenge"); const auto& challenge = infostr.get("challenge");
if (steamId.empty() || challenge.empty()) if (steamId.empty() || challenge.empty())
{ {
@ -189,12 +190,12 @@ namespace Components
} }
// Parse the id // Parse the id
unsigned __int64 xuid = strtoull(steamId.data(), nullptr, 16); const auto xuid = std::strtoull(steamId.data(), nullptr, 16);
SteamID guid; SteamID guid;
guid.bits = xuid; guid.bits = xuid;
if (Bans::IsBanned({ guid, address.getIP() })) if (Bans::IsBanned({guid, address.getIP()}))
{ {
Network::Send(address, "error\nEXE_ERR_BANNED_PERM"); Network::Send(address, "error\nEXE_ERR_BANNED_PERM");
return; return;
@ -223,8 +224,8 @@ namespace Components
} }
// Verify the security level // Verify the security level
uint32_t ourLevel = static_cast<uint32_t>(Dvar::Var("sv_securityLevel").get<int>()); auto ourLevel = Dvar::Var("sv_securityLevel").get<unsigned int>();
uint32_t userLevel = Auth::GetZeroBits(connectData.token(), connectData.publickey()); auto userLevel = Auth::GetZeroBits(connectData.token(), connectData.publickey());
if (userLevel < ourLevel) if (userLevel < ourLevel)
{ {

View File

@ -96,7 +96,7 @@ namespace Components
if (bots.exists()) if (bots.exists())
{ {
std::vector<std::string> names = Utils::String::Explode(bots.getBuffer(), '\n'); std::vector<std::string> names = Utils::String::Split(bots.getBuffer(), '\n');
for (auto name : names) for (auto name : names)
{ {

View File

@ -19,7 +19,7 @@ namespace Components
data = "^1Unable to get changelog."; data = "^1Unable to get changelog.";
} }
Changelog::Lines = Utils::String::Explode(data, '\n'); Changelog::Lines = Utils::String::Split(data, '\n');
for (auto& line : Changelog::Lines) for (auto& line : Changelog::Lines)
{ {

View File

@ -124,7 +124,7 @@ namespace Components
{ {
auto rotation = Dvar::Var("sv_mapRotation").get<std::string>(); auto rotation = Dvar::Var("sv_mapRotation").get<std::string>();
const auto tokens = Utils::String::Explode(rotation, ' '); const auto tokens = Utils::String::Split(rotation, ' ');
std::vector<std::pair<std::string, std::string>> mapRotationPair; std::vector<std::pair<std::string, std::string>> mapRotationPair;
for (auto i = 0u; i < (tokens.size() - 1); i += 2) for (auto i = 0u; i < (tokens.size() - 1); i += 2)
@ -207,7 +207,7 @@ namespace Components
auto rotation = Dvar::Var("sv_mapRotationCurrent").get<std::string>(); auto rotation = Dvar::Var("sv_mapRotationCurrent").get<std::string>();
auto tokens = Utils::String::Explode(rotation, ' '); auto tokens = Utils::String::Split(rotation, ' ');
for (unsigned int i = 0; i < (tokens.size() - 1); i += 2) for (unsigned int i = 0; i < (tokens.size() - 1); i += 2)
{ {

View File

@ -366,7 +366,7 @@ namespace Components
{ {
if (arena->keys[j] == "dependency"s) if (arena->keys[j] == "dependency"s)
{ {
return Utils::String::Explode(arena->values[j], ' '); return Utils::String::Split(arena->values[j], ' ');
} }
} }
} }

View File

@ -60,7 +60,7 @@ namespace Components
std::string nodes = Utils::Cache::GetFile("/iw4/nodes.txt"); std::string nodes = Utils::Cache::GetFile("/iw4/nodes.txt");
if (nodes.empty()) return; if (nodes.empty()) return;
auto nodeList = Utils::String::Explode(nodes, '\n'); auto nodeList = Utils::String::Split(nodes, '\n');
for (auto& node : nodeList) for (auto& node : nodeList)
{ {
Utils::String::Replace(node, "\r", ""); Utils::String::Replace(node, "\r", "");

View File

@ -264,7 +264,7 @@ namespace Components
Dvar::Var("uiSi_ModName").set(info.get("fs_game").data() + 5); Dvar::Var("uiSi_ModName").set(info.get("fs_game").data() + 5);
} }
auto lines = Utils::String::Explode(data, '\n'); auto lines = Utils::String::Split(data, '\n');
if (lines.size() <= 1) return; if (lines.size() <= 1) return;

View File

@ -539,8 +539,8 @@ namespace Components
bool ServerList::CompareVersion(const std::string& version1, const std::string& version2) bool ServerList::CompareVersion(const std::string& version1, const std::string& version2)
{ {
std::vector<std::string> subVersions1 = Utils::String::Explode(version1, '.'); auto subVersions1 = Utils::String::Split(version1, '.');
std::vector<std::string> subVersions2 = Utils::String::Explode(version2, '.'); auto subVersions2 = Utils::String::Split(version2, '.');
while (subVersions1.size() >= 3) subVersions1.pop_back(); while (subVersions1.size() >= 3) subVersions1.pop_back();
while (subVersions2.size() >= 3) subVersions2.pop_back(); while (subVersions2.size() >= 3) subVersions2.pop_back();

View File

@ -286,7 +286,7 @@ namespace Game
typedef dvar_t* (__cdecl * Dvar_FindVar_t)(const char *dvarName); typedef dvar_t* (__cdecl * Dvar_FindVar_t)(const char *dvarName);
extern Dvar_FindVar_t Dvar_FindVar; extern Dvar_FindVar_t Dvar_FindVar;
typedef char* (__cdecl* Dvar_InfoString_Big_t)(int typeMask); typedef char* (__cdecl * Dvar_InfoString_Big_t)(int bit);
extern Dvar_InfoString_Big_t Dvar_InfoString_Big; extern Dvar_InfoString_Big_t Dvar_InfoString_Big;
typedef void(__cdecl * Dvar_SetCommand_t)(const char* dvarName, const char* string); typedef void(__cdecl * Dvar_SetCommand_t)(const char* dvarName, const char* string);

View File

@ -71,7 +71,7 @@ namespace Utils
if (!buffer.empty()) if (!buffer.empty())
{ {
auto rows = Utils::String::Explode(buffer, '\n'); auto rows = Utils::String::Split(buffer, '\n');
for (auto& row : rows) for (auto& row : rows)
{ {

View File

@ -9,9 +9,10 @@ namespace Utils
std::string InfoString::get(const std::string& key) std::string InfoString::get(const std::string& key)
{ {
if (this->keyValuePairs.find(key) != this->keyValuePairs.end()) const auto value = this->keyValuePairs.find(key);
if (value != this->keyValuePairs.end())
{ {
return this->keyValuePairs[key]; return value->second;
} }
return ""; return "";
@ -24,11 +25,13 @@ namespace Utils
buffer = buffer.substr(1); buffer = buffer.substr(1);
} }
std::vector<std::string> KeyValues = Utils::String::Explode(buffer, '\\'); auto KeyValues = Utils::String::Split(buffer, '\\');
for (unsigned int i = 0; i < (KeyValues.size() - 1); i += 2) for (size_t i = 0; !KeyValues.empty() && i < (KeyValues.size() - 1); i += 2)
{ {
this->keyValuePairs[KeyValues[i]] = KeyValues[i + 1]; const auto& key = KeyValues[i];
const auto& value = KeyValues[i + 1];
this->keyValuePairs[key] = value;
} }
} }
@ -36,16 +39,16 @@ namespace Utils
{ {
std::string infoString; std::string infoString;
bool first = true; auto first = true;
for (auto i = this->keyValuePairs.begin(); i != this->keyValuePairs.end(); ++i) for (const auto& [key, value] : this->keyValuePairs)
{ {
if (first) first = false; if (first) first = false;
else infoString.append("\\"); else infoString.append("\\");
infoString.append(i->first); // Key infoString.append(key);
infoString.append("\\"); infoString.append("\\");
infoString.append(i->second); // Value infoString.append(value);
} }
return infoString; return infoString;
@ -53,9 +56,9 @@ namespace Utils
void InfoString::dump() void InfoString::dump()
{ {
for (auto i = this->keyValuePairs.begin(); i != this->keyValuePairs.end(); ++i) for (const auto& [key, value] : this->keyValuePairs)
{ {
OutputDebugStringA(Utils::String::VA("%s: %s", i->first.data(), i->second.data())); OutputDebugStringA(Utils::String::VA("%s: %s\n", key.data(), value.data()));
} }
} }

View File

@ -7,11 +7,9 @@ namespace Utils
public: public:
InfoString() {}; InfoString() {};
InfoString(const std::string& buffer) : InfoString() { this->parse(buffer); }; InfoString(const std::string& buffer) : InfoString() { this->parse(buffer); };
InfoString(const InfoString &obj) : keyValuePairs(obj.keyValuePairs) {};
void set(const std::string& key, const std::string& value); void set(const std::string& key, const std::string& value);
std::string get(const std::string& key); std::string get(const std::string& key);
std::string build(); std::string build();
void dump(); void dump();

View File

@ -62,25 +62,18 @@ namespace Utils
return str; return str;
} }
std::vector<std::string> Explode(const std::string& str, char delim) std::vector<std::string> Split(const std::string& str, const char delim)
{ {
std::vector<std::string> result; std::stringstream ss(str);
std::istringstream iss(str); std::string item;
std::vector<std::string> elems;
for (std::string token; std::getline(iss, token, delim);) while (std::getline(ss, item, delim))
{ {
std::string _entry = std::move(token); elems.push_back(item); // elems.push_back(std::move(item)); // if C++11 (based on comment from S1x)
// Remove trailing 0x0 bytes
while (_entry.size() && !_entry.back())
{
_entry = _entry.substr(0, _entry.size() - 1);
} }
result.push_back(_entry); return elems;
}
return result;
} }
void Replace(std::string &string, const std::string& find, const std::string& replace) void Replace(std::string &string, const std::string& find, const std::string& replace)

View File

@ -78,7 +78,7 @@ namespace Utils
std::string ToLower(std::string input); std::string ToLower(std::string input);
std::string ToUpper(std::string input); std::string ToUpper(std::string input);
bool EndsWith(const std::string& haystack, const std::string& needle); bool EndsWith(const std::string& haystack, const std::string& needle);
std::vector<std::string> Explode(const std::string& str, char delim); std::vector<std::string> Split(const std::string& str, const char delim);
void Replace(std::string &string, const std::string& find, const std::string& replace); void Replace(std::string &string, const std::string& find, const std::string& replace);
bool StartsWith(const std::string& haystack, const std::string& needle); bool StartsWith(const std::string& haystack, const std::string& needle);
std::string &LTrim(std::string &s); std::string &LTrim(std::string &s);