[General] Upgrade to C++17 features

This commit is contained in:
momo5502 2017-06-13 15:35:12 +02:00
parent a1c14cd03f
commit 110aeb02f7
10 changed files with 50 additions and 56 deletions

View File

@ -232,7 +232,7 @@ namespace Components
void AntiCheat::QuickCodeScanner_1()
{
static Utils::Time::Interval interval;
static Utils::Value<std::string> hashVal;
static std::optional<std::string> hashVal;
if (!interval.elapsed(11s)) return;
interval.update();
@ -243,30 +243,30 @@ namespace Components
uint8_t* textBase = reinterpret_cast<uint8_t*>(0x400FFF);
std::string hash = Utils::Cryptography::SHA256::Compute(textBase + 1, textSize + 1, false);
if (hashVal.isValid() && hash != hashVal.get())
if (hashVal.has_value() && hash != hashVal.value())
{
Utils::Hook::Set<BYTE>(0x42A667, 0x90); // Crash
}
hashVal.set(hash);
hashVal.emplace(hash);
}
void AntiCheat::QuickCodeScanner_2()
{
static Utils::Time::Interval interval;
static Utils::Value<std::string> hashVal;
static std::optional<std::string> hashVal;
if (!interval.elapsed(12s)) return;
interval.update();
// Hash .text segment
std::string hash = Utils::Cryptography::SHA1::Compute(reinterpret_cast<uint8_t*>(0x401000), 0x2D6000, false);
if (hashVal.isValid() && hash != hashVal.get())
if (hashVal.has_value() && hash != hashVal.value())
{
Utils::Hook::Set<BYTE>(0x40797C, 0x90); // Crash
}
hashVal.set(hash);
hashVal.emplace(hash);
}
#ifdef DEBUG_LOAD_LIBRARY

View File

@ -8,14 +8,14 @@ namespace Components
bool Dedicated::IsEnabled()
{
static Utils::Value<bool> flag;
static std::optional<bool> flag;
if (!flag.isValid())
if (!flag.has_value())
{
flag.set(Flags::HasFlag("dedicated"));
flag.emplace(Flags::HasFlag("dedicated"));
}
return flag.get();
return flag.value();
}
void Dedicated::InitDedicatedServer()

View File

@ -283,15 +283,15 @@ namespace Components
void Friends::UpdateRank()
{
static Utils::Value<int> levelVal;
static std::optional<int> levelVal;
int experience = Game::Live_GetXp(0);
int prestige = Game::Live_GetPrestige(0);
int level = (experience & 0xFFFFFF) | ((prestige & 0xFF) << 24);
if(!levelVal.isValid() || levelVal.get() != level)
if(!levelVal.has_value() || levelVal.value() != level)
{
levelVal.set(level);
levelVal.emplace(level);
Friends::SetPresence("iw4x_experience", Utils::String::VA("%d", experience));
Friends::SetPresence("iw4x_prestige", Utils::String::VA("%d", prestige));

View File

@ -7,14 +7,14 @@ namespace Components
{
bool Monitor::IsEnabled()
{
static Utils::Value<bool> flag;
static std::optional<bool> flag;
if (!flag.isValid())
if (!flag.has_value())
{
flag.set(Flags::HasFlag("monitor"));
flag.emplace(Flags::HasFlag("monitor"));
}
return flag.get();
return flag.value();
}
int __stdcall Monitor::EntryPoint(HINSTANCE /*hInstance*/, HINSTANCE /*hPrevInstance*/, LPSTR /*lpCmdLine*/, int /*nShowCmd*/)

View File

@ -735,6 +735,21 @@ namespace Components
}
}
printf("Success\n");
printf("Testing trimming...");
std::string trim1 = " 1 ";
std::string trim2 = " 1";
std::string trim3 = "1 ";
Utils::String::Trim(trim1);
Utils::String::LTrim(trim2);
Utils::String::RTrim(trim3);
if (trim1 != "1") return false;
if (trim2 != "1") return false;
if (trim3 != "1") return false;
printf("Success\n");
return true;
}

View File

@ -664,14 +664,14 @@ namespace Components
bool ZoneBuilder::IsEnabled()
{
static Utils::Value<bool> flag;
static std::optional<bool> flag;
if (!flag.isValid())
if (!flag.has_value())
{
flag.set(Flags::HasFlag("zonebuilder"));
flag.emplace(Flags::HasFlag("zonebuilder"));
}
return (flag.get() && !Dedicated::IsEnabled());
return (flag.value() && !Dedicated::IsEnabled());
}
void ZoneBuilder::BeginAssetTrace(std::string zone)

View File

@ -5,6 +5,7 @@
#ifndef RC_INVOKED
#define _HAS_CXX17 1
#define VC_EXTRALEAN
#define WIN32_LEAN_AND_MEAN
@ -39,6 +40,7 @@
// Experimental C++17 features
#include <filesystem>
#include <optional>
#pragma warning(pop)

View File

@ -21,9 +21,9 @@ namespace Steam
unsigned int Utils::GetServerRealTime()
{
static ::Utils::Value<unsigned int> timeDelta;
static ::std::optional<unsigned int> timeDelta;
if(!timeDelta.isValid())
if(!timeDelta.has_value())
{
unsigned int steamTime = static_cast<unsigned int>(time(nullptr));
@ -32,10 +32,10 @@ namespace Steam
steamTime = Steam::Proxy::SteamUtils->GetServerRealTime();
}
timeDelta.set(steamTime - (Game::Sys_Milliseconds() / 1000));
timeDelta.emplace(steamTime - (Game::Sys_Milliseconds() / 1000));
}
return timeDelta.get() + (Game::Sys_Milliseconds() / 1000);
return timeDelta.value() + (Game::Sys_Milliseconds() / 1000);
}
const char* Utils::GetIPCountry()

View File

@ -113,14 +113,20 @@ namespace Utils
// trim from start
std::string &LTrim(std::string &s)
{
s.erase(s.begin(), std::find_if(s.begin(), s.end(), std::not1(std::ptr_fun<int, int>(IsSpace))));
s.erase(s.begin(), std::find_if(s.begin(), s.end(), [](int val)
{
return !IsSpace(val);
}));
return s;
}
// trim from end
std::string &RTrim(std::string &s)
{
s.erase(std::find_if(s.rbegin(), s.rend(), std::not1(std::ptr_fun<int, int>(IsSpace))).base(), s.end());
s.erase(std::find_if(s.rbegin(), s.rend(), [](int val)
{
return !IsSpace(val);
}).base(), s.end());
return s;
}

View File

@ -112,33 +112,4 @@ namespace Utils
private:
std::vector<Slot<T>> slots;
};
// TODO: Replace with std::optional, once C++17 is fully available!
template <typename T>
class Value
{
public:
Value() : hasValue(false) {}
Value(T _value) { this->set(_value); }
void set(T _value)
{
this->value = _value;
this->hasValue = true;
}
bool isValid()
{
return this->hasValue;
}
T get()
{
return this->value;
}
private:
bool hasValue;
T value;
};
}