2022-07-30 12:24:03 -04:00
|
|
|
#pragma once
|
|
|
|
|
2022-10-30 14:59:20 -04:00
|
|
|
#include "Result.hpp"
|
2022-07-30 12:24:03 -04:00
|
|
|
|
2022-12-01 15:42:49 -05:00
|
|
|
#include "../DefaultInclude.hpp"
|
2022-10-30 14:59:20 -04:00
|
|
|
#include <chrono>
|
|
|
|
#include <iomanip>
|
|
|
|
#include <sstream>
|
|
|
|
#include <string>
|
2022-11-28 12:09:39 -05:00
|
|
|
#include <vector>
|
2024-06-02 21:35:05 -04:00
|
|
|
#include <filesystem>
|
2024-01-25 11:45:12 -05:00
|
|
|
#include <matjson.hpp>
|
2024-02-13 16:17:22 -05:00
|
|
|
#include <charconv>
|
2024-02-18 09:04:19 -05:00
|
|
|
#include <clocale>
|
2024-06-07 02:38:06 -04:00
|
|
|
#include <type_traits>
|
2022-07-30 12:24:03 -04:00
|
|
|
|
2024-06-03 23:41:21 -04:00
|
|
|
// only windows seems to properly implement std::hash on std::filesystem::path
|
2024-06-04 07:20:35 -04:00
|
|
|
#ifdef GEODE_IS_ANDROID
|
2024-06-03 23:41:21 -04:00
|
|
|
template <>
|
|
|
|
struct std::hash<std::filesystem::path> {
|
|
|
|
std::size_t operator()(std::filesystem::path const& path) const noexcept {
|
|
|
|
return std::filesystem::hash_value(path);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
#endif
|
|
|
|
|
2022-11-28 12:09:39 -05:00
|
|
|
namespace geode {
|
2022-12-14 06:11:19 -05:00
|
|
|
using ByteVector = std::vector<uint8_t>;
|
2022-10-30 14:59:20 -04:00
|
|
|
|
2022-11-28 12:09:39 -05:00
|
|
|
template <typename T>
|
2022-12-14 09:17:52 -05:00
|
|
|
ByteVector toByteArray(T const& a) {
|
2022-12-14 06:11:19 -05:00
|
|
|
ByteVector out;
|
2022-11-28 12:09:39 -05:00
|
|
|
out.resize(sizeof(T));
|
|
|
|
std::memcpy(out.data(), &a, sizeof(T));
|
|
|
|
return out;
|
2022-07-30 12:24:03 -04:00
|
|
|
}
|
|
|
|
|
2022-11-28 12:09:39 -05:00
|
|
|
namespace utils {
|
2022-12-06 14:22:03 -05:00
|
|
|
// helper for std::visit
|
|
|
|
template<class... Ts> struct makeVisitor : Ts... { using Ts::operator()...; };
|
|
|
|
template<class... Ts> makeVisitor(Ts...) -> makeVisitor<Ts...>;
|
|
|
|
|
|
|
|
template<class T, class ... Args>
|
|
|
|
constexpr T getOr(std::variant<Args...> const& variant, T const& defValue) {
|
|
|
|
return std::holds_alternative<T>(variant) ?
|
|
|
|
std::get<T>(variant) : defValue;
|
|
|
|
}
|
|
|
|
|
2022-11-28 12:09:39 -05:00
|
|
|
constexpr unsigned int hash(char const* str, int h = 0) {
|
|
|
|
return !str[h] ? 5381 : (hash(str, h + 1) * 33) ^ str[h];
|
|
|
|
}
|
2024-02-23 06:28:22 -05:00
|
|
|
constexpr unsigned int hash(std::string_view str, int h = 0) {
|
|
|
|
return h >= str.size() ? 5381 : (hash(str, h + 1) * 33) ^ str[h];
|
|
|
|
}
|
2022-11-28 12:09:39 -05:00
|
|
|
constexpr unsigned int hash(wchar_t const* str, int h = 0) {
|
|
|
|
return !str[h] ? 5381 : (hash(str, h + 1) * 33) ^ str[h];
|
|
|
|
}
|
2024-02-23 06:28:22 -05:00
|
|
|
constexpr unsigned int hash(std::wstring_view str, int h = 0) {
|
|
|
|
return h >= str.size() ? 5381 : (hash(str, h + 1) * 33) ^ str[h];
|
|
|
|
}
|
2022-10-30 14:59:20 -04:00
|
|
|
|
2022-11-28 12:09:39 -05:00
|
|
|
constexpr size_t operator"" _h(char const* txt, size_t) {
|
|
|
|
return geode::utils::hash(txt);
|
2022-10-30 14:59:20 -04:00
|
|
|
}
|
2022-11-28 12:09:39 -05:00
|
|
|
constexpr size_t operator"" _h(wchar_t const* txt, size_t) {
|
|
|
|
return geode::utils::hash(txt);
|
|
|
|
}
|
2022-07-30 12:24:03 -04:00
|
|
|
|
2022-11-28 12:09:39 -05:00
|
|
|
template <typename T>
|
2024-06-07 02:38:06 -04:00
|
|
|
constexpr const T& clamp(const T& value, const std::type_identity_t<T>& minValue, const std::type_identity_t<T>& maxValue) {
|
2022-11-28 12:09:39 -05:00
|
|
|
return value < minValue ? minValue : maxValue < value ? maxValue : value;
|
|
|
|
}
|
2022-07-30 12:24:03 -04:00
|
|
|
|
2022-11-28 12:09:39 -05:00
|
|
|
template <typename T>
|
|
|
|
std::string intToHex(T i) {
|
|
|
|
std::stringstream stream;
|
|
|
|
stream << std::showbase << std::setbase(16) << (uint64_t)i;
|
|
|
|
return stream.str();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Turn a number into a string, with support for specifying precision
|
|
|
|
* (unlike std::to_string).
|
|
|
|
* @param num Number to convert to string
|
|
|
|
* @param precision Precision of the converted number
|
|
|
|
* @returns Number as string
|
|
|
|
*/
|
|
|
|
template <class Num>
|
|
|
|
std::string numToString(Num num, size_t precision = 0) {
|
|
|
|
std::stringstream ss;
|
|
|
|
if (precision) {
|
|
|
|
ss << std::fixed << std::setprecision(precision);
|
|
|
|
}
|
|
|
|
ss << num;
|
|
|
|
return ss.str();
|
|
|
|
}
|
|
|
|
|
2024-05-04 16:01:07 -04:00
|
|
|
/**
|
|
|
|
* Turn a number into an abbreviated string, like `1253` to `1.25K`
|
|
|
|
*/
|
|
|
|
template <std::integral Num>
|
|
|
|
std::string numToAbbreviatedString(Num num) {
|
|
|
|
if (num >= 1'000'000'000) return fmt::format("{:0.3}B", num / 1'000'000'000.f);
|
|
|
|
if (num >= 1'000'000) return fmt::format("{:0.3}M", num / 1'000'000.f);
|
|
|
|
if (num >= 1'000) return fmt::format("{:0.3}K", num / 1'000.f);
|
|
|
|
return numToString(num);
|
|
|
|
}
|
|
|
|
|
2024-02-13 16:14:49 -05:00
|
|
|
/**
|
|
|
|
* Parse a number from a string
|
|
|
|
* @param str The string to parse
|
|
|
|
* @param base The base to use
|
|
|
|
* @returns String as number, or Err if the string couldn't be converted
|
|
|
|
*/
|
|
|
|
template <class Num>
|
|
|
|
Result<Num> numFromString(std::string_view const str, int base = 10) {
|
2024-02-18 09:19:25 -05:00
|
|
|
if constexpr (std::is_floating_point_v<Num>
|
|
|
|
#if defined(__cpp_lib_to_chars)
|
|
|
|
&& false
|
|
|
|
#endif
|
|
|
|
) {
|
|
|
|
Num val;
|
2024-02-18 09:04:19 -05:00
|
|
|
char* strEnd;
|
|
|
|
errno = 0;
|
2024-02-26 05:50:43 -05:00
|
|
|
if (std::setlocale(LC_NUMERIC, "C")) {
|
2024-02-18 09:04:19 -05:00
|
|
|
if constexpr (std::is_same_v<Num, float>) val = std::strtof(str.data(), &strEnd);
|
|
|
|
else if constexpr (std::is_same_v<Num, double>) val = std::strtod(str.data(), &strEnd);
|
|
|
|
else if constexpr (std::is_same_v<Num, long double>) val = std::strtold(str.data(), &strEnd);
|
|
|
|
if (errno == ERANGE) return Err("Number is too large to fit");
|
|
|
|
else if (strEnd == str.data()) return Err("String is not a number");
|
|
|
|
else return Ok(val);
|
|
|
|
}
|
|
|
|
else return Err("Failed to set locale");
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
Num result;
|
2024-02-18 09:19:25 -05:00
|
|
|
std::from_chars_result res;
|
|
|
|
if constexpr (std::is_floating_point_v<Num>) res = std::from_chars(str.data(), str.data() + str.size(), result);
|
|
|
|
else res = std::from_chars(str.data(), str.data() + str.size(), result, base);
|
|
|
|
|
|
|
|
auto [_, ec] = res;
|
|
|
|
if (ec == std::errc()) return Ok(result);
|
|
|
|
else if (ec == std::errc::invalid_argument) return Err("String is not a number");
|
|
|
|
else if (ec == std::errc::result_out_of_range) return Err("Number is too large to fit");
|
|
|
|
else return Err("Unknown error");
|
2024-02-13 16:14:49 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-28 12:09:39 -05:00
|
|
|
GEODE_DLL std::string timePointAsString(std::chrono::system_clock::time_point const& tp);
|
2024-05-12 09:25:36 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the display pixel factor for the current screen,
|
2024-05-12 12:01:26 -04:00
|
|
|
* i.e. the ratio between physical pixels and logical pixels on one axis.
|
|
|
|
* On most platforms this is 1.0, but on retina displays for example this returns 2.0.
|
2024-05-12 09:25:36 -04:00
|
|
|
*/
|
|
|
|
GEODE_DLL float getDisplayFactor();
|
2022-09-21 07:50:23 -04:00
|
|
|
}
|
2022-11-28 12:09:39 -05:00
|
|
|
}
|
2022-09-21 07:50:23 -04:00
|
|
|
|
2024-01-14 16:42:04 -05:00
|
|
|
template<>
|
|
|
|
struct matjson::Serialize<geode::ByteVector> {
|
|
|
|
static matjson::Value to_json(geode::ByteVector const& bytes) {
|
|
|
|
return matjson::Array(bytes.begin(), bytes.end());
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-11-28 12:09:39 -05:00
|
|
|
namespace geode::utils::clipboard {
|
|
|
|
GEODE_DLL bool write(std::string const& data);
|
|
|
|
GEODE_DLL std::string read();
|
2022-07-30 12:24:03 -04:00
|
|
|
}
|
2023-08-08 14:59:13 -04:00
|
|
|
|
|
|
|
namespace geode::utils::game {
|
2023-09-11 09:36:35 -04:00
|
|
|
GEODE_DLL void exit();
|
2023-08-08 14:59:13 -04:00
|
|
|
GEODE_DLL void restart();
|
2023-09-11 09:36:35 -04:00
|
|
|
GEODE_DLL void launchLoaderUninstaller(bool deleteSaveData);
|
2023-08-08 14:59:13 -04:00
|
|
|
}
|
2024-01-28 07:33:33 -05:00
|
|
|
|
|
|
|
namespace geode::utils::thread {
|
|
|
|
GEODE_DLL std::string getName();
|
2024-01-28 09:41:13 -05:00
|
|
|
GEODE_DLL std::string getDefaultName();
|
2024-01-28 07:33:33 -05:00
|
|
|
GEODE_DLL void setName(std::string const& name);
|
|
|
|
}
|