2021-12-05 17:54:36 +00:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
// C++
|
2024-02-10 11:05:25 +00:00
|
|
|
#include <charconv>
|
|
|
|
#include <cstdint>
|
|
|
|
#include <ctime>
|
2024-04-15 06:14:54 +00:00
|
|
|
#include <functional>
|
|
|
|
#include <optional>
|
|
|
|
#include <random>
|
|
|
|
#include <span>
|
|
|
|
#include <stdexcept>
|
2021-12-05 17:54:36 +00:00
|
|
|
#include <string>
|
2024-02-10 11:05:25 +00:00
|
|
|
#include <string_view>
|
2021-12-05 17:54:36 +00:00
|
|
|
#include <type_traits>
|
2024-04-15 06:14:54 +00:00
|
|
|
|
2024-01-05 12:33:52 +00:00
|
|
|
#include "BitStream.h"
|
2023-03-25 10:26:39 +00:00
|
|
|
#include "NiPoint3.h"
|
2024-02-10 11:05:25 +00:00
|
|
|
#include "dPlatforms.h"
|
2021-12-05 17:54:36 +00:00
|
|
|
#include "Game.h"
|
2023-10-21 23:31:55 +00:00
|
|
|
#include "Logger.h"
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2022-12-18 15:46:04 +00:00
|
|
|
enum eInventoryType : uint32_t;
|
2023-04-25 18:17:40 +00:00
|
|
|
enum class eObjectBits : size_t;
|
2023-03-04 07:16:37 +00:00
|
|
|
enum class eReplicaComponentType : uint32_t;
|
2022-12-18 15:46:04 +00:00
|
|
|
|
2021-12-05 17:54:36 +00:00
|
|
|
/*!
|
|
|
|
\file GeneralUtils.hpp
|
|
|
|
\brief A namespace containing general utility functions
|
|
|
|
*/
|
|
|
|
|
|
|
|
//! The general utils namespace
|
|
|
|
namespace GeneralUtils {
|
|
|
|
//! Converts a plain ASCII string to a UTF-16 string
|
|
|
|
/*!
|
|
|
|
\param string The string to convert
|
2024-04-15 06:14:54 +00:00
|
|
|
\param size A size to trim the string to. Default is SIZE_MAX (No trimming)
|
2021-12-05 17:54:36 +00:00
|
|
|
\return An UTF-16 representation of the string
|
|
|
|
*/
|
2024-04-15 06:14:54 +00:00
|
|
|
std::u16string ASCIIToUTF16(const std::string_view string, const size_t size = SIZE_MAX);
|
2022-07-28 13:39:57 +00:00
|
|
|
|
2022-07-26 04:11:30 +00:00
|
|
|
//! Converts a UTF-8 String to a UTF-16 string
|
|
|
|
/*!
|
|
|
|
\param string The string to convert
|
2024-04-15 06:14:54 +00:00
|
|
|
\param size A size to trim the string to. Default is SIZE_MAX (No trimming)
|
2022-07-26 04:11:30 +00:00
|
|
|
\return An UTF-16 representation of the string
|
|
|
|
*/
|
2024-04-15 06:14:54 +00:00
|
|
|
std::u16string UTF8ToUTF16(const std::string_view string, const size_t size = SIZE_MAX);
|
2022-07-28 13:39:57 +00:00
|
|
|
|
2024-04-15 06:14:54 +00:00
|
|
|
namespace details {
|
|
|
|
//! Internal, do not use
|
|
|
|
bool _NextUTF8Char(std::string_view& slice, uint32_t& out);
|
|
|
|
}
|
2022-07-28 13:39:57 +00:00
|
|
|
|
2021-12-05 17:54:36 +00:00
|
|
|
//! Converts a UTF-16 string to a UTF-8 string
|
|
|
|
/*!
|
|
|
|
\param string The string to convert
|
2024-04-15 06:14:54 +00:00
|
|
|
\param size A size to trim the string to. Default is SIZE_MAX (No trimming)
|
2021-12-05 17:54:36 +00:00
|
|
|
\return An UTF-8 representation of the string
|
|
|
|
*/
|
2024-04-15 06:14:54 +00:00
|
|
|
std::string UTF16ToWTF8(const std::u16string_view string, const size_t size = SIZE_MAX);
|
2022-07-28 13:39:57 +00:00
|
|
|
|
2021-12-05 17:54:36 +00:00
|
|
|
/**
|
|
|
|
* Compares two basic strings but does so ignoring case sensitivity
|
|
|
|
* \param a the first string to compare against the second string
|
|
|
|
* \param b the second string to compare against the first string
|
|
|
|
* @return if the two strings are equal
|
|
|
|
*/
|
2024-04-15 06:14:54 +00:00
|
|
|
bool CaseInsensitiveStringCompare(const std::string_view a, const std::string_view b);
|
2022-07-28 13:39:57 +00:00
|
|
|
|
2021-12-05 17:54:36 +00:00
|
|
|
// MARK: Bits
|
2022-07-28 13:39:57 +00:00
|
|
|
|
2021-12-05 17:54:36 +00:00
|
|
|
// MARK: Bits
|
2022-07-28 13:39:57 +00:00
|
|
|
|
2021-12-05 17:54:36 +00:00
|
|
|
//! Sets a bit on a numerical value
|
|
|
|
template <typename T>
|
2024-04-15 06:14:54 +00:00
|
|
|
inline void SetBit(T& value, const eObjectBits bits) {
|
2021-12-05 17:54:36 +00:00
|
|
|
static_assert(std::is_arithmetic<T>::value, "Not an arithmetic type");
|
2024-04-15 06:14:54 +00:00
|
|
|
const auto index = static_cast<size_t>(bits);
|
2021-12-05 17:54:36 +00:00
|
|
|
if (index > (sizeof(T) * 8) - 1) {
|
|
|
|
return;
|
|
|
|
}
|
2022-07-28 13:39:57 +00:00
|
|
|
|
2021-12-05 17:54:36 +00:00
|
|
|
value |= static_cast<T>(1) << index;
|
|
|
|
}
|
2022-07-28 13:39:57 +00:00
|
|
|
|
2021-12-05 17:54:36 +00:00
|
|
|
//! Clears a bit on a numerical value
|
|
|
|
template <typename T>
|
2024-04-15 06:14:54 +00:00
|
|
|
inline void ClearBit(T& value, const eObjectBits bits) {
|
2021-12-05 17:54:36 +00:00
|
|
|
static_assert(std::is_arithmetic<T>::value, "Not an arithmetic type");
|
2024-04-15 06:14:54 +00:00
|
|
|
const auto index = static_cast<size_t>(bits);
|
2021-12-05 17:54:36 +00:00
|
|
|
if (index > (sizeof(T) * 8 - 1)) {
|
|
|
|
return;
|
|
|
|
}
|
2022-07-28 13:39:57 +00:00
|
|
|
|
2021-12-05 17:54:36 +00:00
|
|
|
value &= ~(static_cast<T>(1) << index);
|
|
|
|
}
|
2022-07-28 13:39:57 +00:00
|
|
|
|
2021-12-05 17:54:36 +00:00
|
|
|
//! Sets a specific bit in a signed 64-bit integer
|
|
|
|
/*!
|
|
|
|
\param value The value to set the bit for
|
|
|
|
\param index The index of the bit
|
|
|
|
*/
|
2024-04-15 06:14:54 +00:00
|
|
|
int64_t SetBit(int64_t value, const uint32_t index);
|
2022-07-28 13:39:57 +00:00
|
|
|
|
2021-12-05 17:54:36 +00:00
|
|
|
//! Clears a specific bit in a signed 64-bit integer
|
|
|
|
/*!
|
|
|
|
\param value The value to clear the bit from
|
|
|
|
\param index The index of the bit
|
|
|
|
*/
|
2024-04-15 06:14:54 +00:00
|
|
|
int64_t ClearBit(int64_t value, const uint32_t index);
|
2022-07-28 13:39:57 +00:00
|
|
|
|
2021-12-05 17:54:36 +00:00
|
|
|
//! Checks a specific bit in a signed 64-bit integer
|
|
|
|
/*!
|
|
|
|
\parma value The value to check the bit in
|
|
|
|
\param index The index of the bit
|
|
|
|
\return Whether or not the bit is set
|
|
|
|
*/
|
2024-04-15 06:14:54 +00:00
|
|
|
bool CheckBit(int64_t value, const uint32_t index);
|
2022-07-28 13:39:57 +00:00
|
|
|
|
2024-04-15 06:14:54 +00:00
|
|
|
bool ReplaceInString(std::string& str, const std::string_view from, const std::string_view to);
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2024-02-26 14:17:22 +00:00
|
|
|
std::u16string ReadWString(RakNet::BitStream& inStream);
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2024-04-15 06:14:54 +00:00
|
|
|
std::vector<std::wstring> SplitString(const std::wstring_view str, const wchar_t delimiter);
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2024-04-15 06:14:54 +00:00
|
|
|
std::vector<std::u16string> SplitString(const std::u16string_view str, const char16_t delimiter);
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2024-04-15 06:14:54 +00:00
|
|
|
std::vector<std::string> SplitString(const std::string_view str, const char delimiter);
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2024-04-15 06:14:54 +00:00
|
|
|
std::vector<std::string> GetSqlFileNamesFromFolder(const std::string_view folder);
|
2022-07-28 13:39:57 +00:00
|
|
|
|
2024-02-10 11:05:25 +00:00
|
|
|
// Concept constraining to enum types
|
2021-12-05 17:54:36 +00:00
|
|
|
template <typename T>
|
2024-02-10 11:05:25 +00:00
|
|
|
concept Enum = std::is_enum_v<T>;
|
2022-07-10 19:40:26 +00:00
|
|
|
|
2024-02-10 11:05:25 +00:00
|
|
|
// Concept constraining to numeric types
|
|
|
|
template <typename T>
|
|
|
|
concept Numeric = std::integral<T> || Enum<T> || std::floating_point<T>;
|
2023-10-09 20:22:40 +00:00
|
|
|
|
2024-02-10 11:05:25 +00:00
|
|
|
// Concept trickery to enable parsing underlying numeric types
|
|
|
|
template <Numeric T>
|
|
|
|
struct numeric_parse { using type = T; };
|
2022-07-28 13:39:57 +00:00
|
|
|
|
2024-02-10 11:05:25 +00:00
|
|
|
// If an enum, present an alias to its underlying type for parsing
|
|
|
|
template <Numeric T> requires Enum<T>
|
|
|
|
struct numeric_parse<T> { using type = std::underlying_type_t<T>; };
|
2022-07-28 13:39:57 +00:00
|
|
|
|
2024-02-10 11:05:25 +00:00
|
|
|
// If a boolean, present an alias to an intermediate integral type for parsing
|
|
|
|
template <Numeric T> requires std::same_as<T, bool>
|
2024-04-15 06:14:54 +00:00
|
|
|
struct numeric_parse<T> { using type = uint8_t; };
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2024-02-10 11:05:25 +00:00
|
|
|
// Shorthand type alias
|
|
|
|
template <Numeric T>
|
|
|
|
using numeric_parse_t = numeric_parse<T>::type;
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2024-02-10 11:05:25 +00:00
|
|
|
/**
|
|
|
|
* For numeric values: Parses a string_view and returns an optional variable depending on the result.
|
|
|
|
* @param str The string_view to be evaluated
|
|
|
|
* @returns An std::optional containing the desired value if it is equivalent to the string
|
|
|
|
*/
|
|
|
|
template <Numeric T>
|
2024-04-08 20:13:19 +00:00
|
|
|
[[nodiscard]] std::optional<T> TryParse(std::string_view str) {
|
2024-02-10 11:05:25 +00:00
|
|
|
numeric_parse_t<T> result;
|
2024-04-09 05:20:25 +00:00
|
|
|
while (!str.empty() && std::isspace(str.front())) str.remove_prefix(1);
|
2023-11-18 09:33:23 +00:00
|
|
|
|
2024-02-10 11:05:25 +00:00
|
|
|
const char* const strEnd = str.data() + str.size();
|
|
|
|
const auto [parseEnd, ec] = std::from_chars(str.data(), strEnd, result);
|
|
|
|
const bool isParsed = parseEnd == strEnd && ec == std::errc{};
|
2022-07-28 13:39:57 +00:00
|
|
|
|
2024-02-10 11:05:25 +00:00
|
|
|
return isParsed ? static_cast<T>(result) : std::optional<T>{};
|
2021-12-05 17:54:36 +00:00
|
|
|
}
|
2022-07-28 13:39:57 +00:00
|
|
|
|
2024-04-01 02:46:51 +00:00
|
|
|
#if !(__GNUC__ >= 11 || _MSC_VER >= 1924)
|
2022-12-18 15:46:04 +00:00
|
|
|
|
2024-02-24 10:30:02 +00:00
|
|
|
// MacOS floating-point parse helper function specializations
|
|
|
|
namespace details {
|
2024-02-10 11:05:25 +00:00
|
|
|
template <std::floating_point T>
|
2024-02-24 10:30:02 +00:00
|
|
|
[[nodiscard]] T _parse(const std::string_view str, size_t& parseNum);
|
2022-07-28 13:39:57 +00:00
|
|
|
}
|
|
|
|
|
2024-02-10 11:05:25 +00:00
|
|
|
/**
|
|
|
|
* For floating-point values: Parses a string_view and returns an optional variable depending on the result.
|
|
|
|
* Note that this function overload is only included for MacOS, as from_chars will fulfill its purpose otherwise.
|
|
|
|
* @param str The string_view to be evaluated
|
|
|
|
* @returns An std::optional containing the desired value if it is equivalent to the string
|
|
|
|
*/
|
|
|
|
template <std::floating_point T>
|
2024-04-08 20:13:19 +00:00
|
|
|
[[nodiscard]] std::optional<T> TryParse(std::string_view str) noexcept
|
2024-02-24 10:30:02 +00:00
|
|
|
try {
|
2024-04-09 05:20:25 +00:00
|
|
|
while (!str.empty() && std::isspace(str.front())) str.remove_prefix(1);
|
2024-04-08 20:13:19 +00:00
|
|
|
|
2024-02-10 11:05:25 +00:00
|
|
|
size_t parseNum;
|
2024-02-24 10:30:02 +00:00
|
|
|
const T result = details::_parse<T>(str, parseNum);
|
2024-02-10 11:05:25 +00:00
|
|
|
const bool isParsed = str.length() == parseNum;
|
|
|
|
|
|
|
|
return isParsed ? result : std::optional<T>{};
|
|
|
|
} catch (...) {
|
|
|
|
return std::nullopt;
|
2021-12-05 17:54:36 +00:00
|
|
|
}
|
2022-07-28 13:39:57 +00:00
|
|
|
|
2024-02-10 11:05:25 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The TryParse overload for handling NiPoint3 by passing 3 seperate string references
|
|
|
|
* @param strX The string representing the X coordinate
|
|
|
|
* @param strY The string representing the Y coordinate
|
|
|
|
* @param strZ The string representing the Z coordinate
|
|
|
|
* @returns An std::optional containing the desired NiPoint3 if it can be constructed from the string parameters
|
|
|
|
*/
|
2021-12-05 17:54:36 +00:00
|
|
|
template <typename T>
|
2024-04-15 06:14:54 +00:00
|
|
|
[[nodiscard]] std::optional<NiPoint3> TryParse(const std::string_view strX, const std::string_view strY, const std::string_view strZ) {
|
2024-02-10 11:05:25 +00:00
|
|
|
const auto x = TryParse<float>(strX);
|
|
|
|
if (!x) return std::nullopt;
|
|
|
|
|
|
|
|
const auto y = TryParse<float>(strY);
|
|
|
|
if (!y) return std::nullopt;
|
|
|
|
|
|
|
|
const auto z = TryParse<float>(strZ);
|
|
|
|
return z ? std::make_optional<NiPoint3>(x.value(), y.value(), z.value()) : std::nullopt;
|
2021-12-05 17:54:36 +00:00
|
|
|
}
|
2022-07-28 13:39:57 +00:00
|
|
|
|
2024-02-10 11:05:25 +00:00
|
|
|
/**
|
2024-04-15 06:14:54 +00:00
|
|
|
* The TryParse overload for handling NiPoint3 by passing a span of three strings
|
|
|
|
* @param str The string vector representing the X, Y, and Z coordinates
|
2024-02-10 11:05:25 +00:00
|
|
|
* @returns An std::optional containing the desired NiPoint3 if it can be constructed from the string parameters
|
|
|
|
*/
|
|
|
|
template <typename T>
|
2024-04-15 06:14:54 +00:00
|
|
|
[[nodiscard]] std::optional<NiPoint3> TryParse(const std::span<const std::string> str) {
|
2024-02-10 11:05:25 +00:00
|
|
|
return (str.size() == 3) ? TryParse<NiPoint3>(str[0], str[1], str[2]) : std::nullopt;
|
|
|
|
}
|
2023-03-25 10:26:39 +00:00
|
|
|
|
2024-02-10 11:05:25 +00:00
|
|
|
template <typename T>
|
2024-04-15 06:14:54 +00:00
|
|
|
std::u16string to_u16string(const T value) {
|
2021-12-05 17:54:36 +00:00
|
|
|
return GeneralUtils::ASCIIToUTF16(std::to_string(value));
|
|
|
|
}
|
2022-07-28 13:39:57 +00:00
|
|
|
|
2021-12-05 17:54:36 +00:00
|
|
|
// From boost::hash_combine
|
|
|
|
template <class T>
|
2024-02-10 11:05:25 +00:00
|
|
|
constexpr void hash_combine(std::size_t& s, const T& v) {
|
2021-12-05 17:54:36 +00:00
|
|
|
std::hash<T> h;
|
|
|
|
s ^= h(v) + 0x9e3779b9 + (s << 6) + (s >> 2);
|
|
|
|
}
|
2023-07-23 21:59:43 +00:00
|
|
|
|
|
|
|
// MARK: Random Number Generation
|
|
|
|
|
|
|
|
//! Generates a random number
|
|
|
|
/*!
|
|
|
|
\param min The minimum the generate from
|
|
|
|
\param max The maximum to generate to
|
|
|
|
*/
|
|
|
|
template <typename T>
|
2024-04-15 06:14:54 +00:00
|
|
|
inline T GenerateRandomNumber(const std::size_t min, const std::size_t max) {
|
2023-07-23 21:59:43 +00:00
|
|
|
// Make sure it is a numeric type
|
|
|
|
static_assert(std::is_arithmetic<T>::value, "Not an arithmetic type");
|
|
|
|
|
|
|
|
if constexpr (std::is_integral_v<T>) { // constexpr only necessary on first statement
|
|
|
|
std::uniform_int_distribution<T> distribution(min, max);
|
|
|
|
return distribution(Game::randomEngine);
|
|
|
|
} else if (std::is_floating_point_v<T>) {
|
|
|
|
std::uniform_real_distribution<T> distribution(min, max);
|
|
|
|
return distribution(Game::randomEngine);
|
|
|
|
}
|
|
|
|
|
|
|
|
return T();
|
|
|
|
}
|
|
|
|
|
2023-12-24 00:11:00 +00:00
|
|
|
/**
|
|
|
|
* Casts the value of an enum entry to its underlying type
|
|
|
|
* @param entry Enum entry to cast
|
|
|
|
* @returns The enum entry's value in its underlying type
|
|
|
|
*/
|
2024-02-10 11:05:25 +00:00
|
|
|
template <Enum eType>
|
2024-03-07 05:45:04 +00:00
|
|
|
constexpr std::underlying_type_t<eType> ToUnderlying(const eType entry) noexcept {
|
|
|
|
return static_cast<std::underlying_type_t<eType>>(entry);
|
2023-12-24 00:11:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// on Windows we need to undef these or else they conflict with our numeric limits calls
|
|
|
|
// DEVELOPERS DEVELOPERS DEVELOPERS DEVELOPERS DEVELOPERS DEVELOPERS DEVELOPERS DEVELOPERS
|
2024-04-15 06:14:54 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
#undef min
|
|
|
|
#undef max
|
|
|
|
#endif
|
2023-07-23 21:59:43 +00:00
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
inline T GenerateRandomNumber() {
|
|
|
|
// Make sure it is a numeric type
|
|
|
|
static_assert(std::is_arithmetic<T>::value, "Not an arithmetic type");
|
2023-12-24 00:11:00 +00:00
|
|
|
|
2023-07-23 21:59:43 +00:00
|
|
|
return GenerateRandomNumber<T>(std::numeric_limits<T>::min(), std::numeric_limits<T>::max());
|
|
|
|
}
|
2021-12-05 17:54:36 +00:00
|
|
|
}
|