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>
|
2023-02-08 08:42:34 -05:00
|
|
|
#include <ghc/fs_fwd.hpp>
|
2024-01-25 11:45:12 -05:00
|
|
|
#include <matjson.hpp>
|
2022-07-30 12:24:03 -04:00
|
|
|
|
2022-11-29 17:48:06 -05:00
|
|
|
// for some reason std::filesystem::path doesn't have std::hash defined in C++17
|
|
|
|
// and ghc seems to have inherited this limitation
|
|
|
|
template<>
|
|
|
|
struct std::hash<ghc::filesystem::path> {
|
|
|
|
std::size_t operator()(ghc::filesystem::path const& path) const noexcept {
|
|
|
|
return ghc::filesystem::hash_value(path);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
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
|
|
|
template <class T>
|
|
|
|
struct TypeIdentity {
|
|
|
|
using type = T;
|
|
|
|
};
|
2022-07-30 12:24:03 -04:00
|
|
|
|
2022-11-28 12:09:39 -05:00
|
|
|
template <class T>
|
|
|
|
using TypeIdentityType = typename TypeIdentity<T>::type;
|
|
|
|
|
|
|
|
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];
|
|
|
|
}
|
2022-07-30 12:24:03 -04:00
|
|
|
|
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];
|
|
|
|
}
|
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-07-30 12:24:03 -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>
|
|
|
|
constexpr const T& clamp(const T& value, const T& minValue, const T& maxValue) {
|
|
|
|
return value < minValue ? minValue : maxValue < value ? maxValue : value;
|
|
|
|
}
|
2022-07-30 12:24:03 -04:00
|
|
|
|
2022-11-28 12:09:39 -05:00
|
|
|
// from https://stackoverflow.com/questions/2342162/stdstring-formatting-like-sprintf
|
|
|
|
template <typename... Args>
|
|
|
|
std::string strfmt(std::string const& format, Args... args) {
|
|
|
|
int size_s = std::snprintf(nullptr, 0, format.c_str(), args...) + 1; // Extra space for '\0'
|
|
|
|
if (size_s <= 0) {
|
|
|
|
throw std::runtime_error("Error during formatting.");
|
|
|
|
}
|
|
|
|
auto size = static_cast<size_t>(size_s);
|
|
|
|
auto buf = std::make_unique<char[]>(size);
|
|
|
|
std::snprintf(buf.get(), size, format.c_str(), args...);
|
|
|
|
return std::string(buf.get(), buf.get() + size - 1);
|
2022-09-21 07:50:23 -04:00
|
|
|
}
|
2022-11-28 12:09:39 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Format a string
|
|
|
|
* @returns Pointer to char array. MAKE SURE TO CALL DELETE[]!
|
|
|
|
*/
|
|
|
|
template <typename... Args>
|
|
|
|
char const* cstrfmt(char const* fmt, Args... args) {
|
|
|
|
auto str = strfmt(fmt, args...);
|
|
|
|
char* ptr = new char[str.size() + 1];
|
|
|
|
strcpy_s(ptr, str.c_str());
|
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
|
|
|
GEODE_DLL std::string timePointAsString(std::chrono::system_clock::time_point const& tp);
|
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);
|
|
|
|
}
|