2022-02-14 20:26:32 +01:00
|
|
|
#pragma once
|
2020-04-29 18:02:36 +02:00
|
|
|
|
2024-08-04 20:59:07 +02:00
|
|
|
#include "json.hpp"
|
2023-11-04 15:32:45 +01:00
|
|
|
#include <algorithm>
|
2022-10-03 21:51:12 +02:00
|
|
|
#include <coreinit/dynload.h>
|
2022-05-14 14:00:20 +02:00
|
|
|
#include <cstdint>
|
|
|
|
#include <forward_list>
|
|
|
|
#include <memory>
|
|
|
|
#include <mutex>
|
2023-11-04 15:32:45 +01:00
|
|
|
#include <set>
|
2023-12-16 17:36:57 +01:00
|
|
|
#include <vector>
|
2020-04-29 18:02:36 +02:00
|
|
|
|
|
|
|
#ifdef __cplusplus
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
|
|
|
|
2022-02-04 16:25:44 +01:00
|
|
|
#define LIMIT(x, min, max) \
|
|
|
|
({ \
|
|
|
|
typeof(x) _x = x; \
|
|
|
|
typeof(min) _min = min; \
|
|
|
|
typeof(max) _max = max; \
|
|
|
|
(((_x) < (_min)) ? (_min) : ((_x) > (_max)) ? (_max) \
|
|
|
|
: (_x)); \
|
|
|
|
})
|
2020-04-29 18:02:36 +02:00
|
|
|
|
2022-02-04 16:25:44 +01:00
|
|
|
#define DegToRad(a) ((a) *0.01745329252f)
|
|
|
|
#define RadToDeg(a) ((a) *57.29577951f)
|
2020-04-29 18:02:36 +02:00
|
|
|
|
2022-02-04 16:25:44 +01:00
|
|
|
#define ALIGN4(x) (((x) + 3) & ~3)
|
|
|
|
#define ALIGN32(x) (((x) + 31) & ~31)
|
2020-04-29 18:02:36 +02:00
|
|
|
|
2022-02-14 20:23:27 +01:00
|
|
|
#define ALIGN_DATA(align) __attribute__((aligned(align)))
|
|
|
|
#define ALIGN_DATA_0x40 ALIGN_DATA(0x40)
|
|
|
|
|
2020-04-29 18:02:36 +02:00
|
|
|
// those work only in powers of 2
|
2022-02-04 16:25:44 +01:00
|
|
|
#define ROUNDDOWN(val, align) ((val) & ~(align - 1))
|
|
|
|
#define ROUNDUP(val, align) ROUNDDOWN(((val) + (align - 1)), align)
|
2020-04-29 18:02:36 +02:00
|
|
|
|
|
|
|
|
2022-02-04 16:25:44 +01:00
|
|
|
#define le16(i) ((((uint16_t) ((i) &0xFF)) << 8) | ((uint16_t) (((i) &0xFF00) >> 8)))
|
|
|
|
#define le32(i) ((((uint32_t) le16((i) &0xFFFF)) << 16) | ((uint32_t) le16(((i) &0xFFFF0000) >> 16)))
|
|
|
|
#define le64(i) ((((uint64_t) le32((i) &0xFFFFFFFFLL)) << 32) | ((uint64_t) le32(((i) &0xFFFFFFFF00000000LL) >> 32)))
|
2020-04-29 18:02:36 +02:00
|
|
|
|
|
|
|
//Needs to have log_init() called beforehand.
|
2020-05-03 12:30:15 +02:00
|
|
|
void dumpHex(const void *data, size_t size);
|
2020-04-29 18:02:36 +02:00
|
|
|
|
|
|
|
#ifdef __cplusplus
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2022-05-14 14:00:20 +02:00
|
|
|
template<class T, class... Args>
|
|
|
|
std::unique_ptr<T> make_unique_nothrow(Args &&...args) noexcept(noexcept(T(std::forward<Args>(args)...))) {
|
|
|
|
return std::unique_ptr<T>(new (std::nothrow) T(std::forward<Args>(args)...));
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
2022-07-25 22:19:22 +02:00
|
|
|
inline typename std::unique_ptr<T> make_unique_nothrow(size_t num) noexcept {
|
2022-05-14 14:00:20 +02:00
|
|
|
return std::unique_ptr<T>(new (std::nothrow) std::remove_extent_t<T>[num]());
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class T, class... Args>
|
|
|
|
std::shared_ptr<T> make_shared_nothrow(Args &&...args) noexcept(noexcept(T(std::forward<Args>(args)...))) {
|
|
|
|
return std::shared_ptr<T>(new (std::nothrow) T(std::forward<Args>(args)...));
|
|
|
|
}
|
|
|
|
|
2023-11-04 15:32:45 +01:00
|
|
|
template<typename Container, typename Predicate>
|
2024-11-27 20:44:20 +01:00
|
|
|
std::enable_if_t<std::is_same_v<Container, std::forward_list<typename Container::value_type>>, bool>
|
2023-10-07 22:38:27 +02:00
|
|
|
remove_first_if(Container &container, Predicate pred) {
|
|
|
|
auto it = container.before_begin();
|
|
|
|
|
|
|
|
for (auto prev = it, current = ++it; current != container.end(); ++prev, ++current) {
|
|
|
|
if (pred(*current)) {
|
|
|
|
container.erase_after(prev);
|
|
|
|
return true;
|
|
|
|
}
|
2022-05-14 14:00:20 +02:00
|
|
|
}
|
2023-11-04 15:32:45 +01:00
|
|
|
|
2022-05-14 14:00:20 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-10-07 22:38:27 +02:00
|
|
|
template<typename Container, typename Predicate>
|
2024-11-27 20:44:20 +01:00
|
|
|
std::enable_if_t<std::is_same_v<Container, std::set<typename Container::value_type, typename Container::key_compare>>, bool>
|
2023-10-07 22:38:27 +02:00
|
|
|
remove_first_if(Container &container, Predicate pred) {
|
|
|
|
auto it = container.begin();
|
|
|
|
while (it != container.end()) {
|
|
|
|
if (pred(*it)) {
|
|
|
|
container.erase(it);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
++it;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-12-16 17:36:57 +01:00
|
|
|
template<typename Container, typename Predicate>
|
2024-11-27 20:44:20 +01:00
|
|
|
std::enable_if_t<std::is_same_v<Container, std::vector<typename Container::value_type>>, bool>
|
2023-12-16 17:36:57 +01:00
|
|
|
remove_first_if(Container &container, Predicate pred) {
|
|
|
|
auto it = container.begin();
|
|
|
|
while (it != container.end()) {
|
|
|
|
if (pred(*it)) {
|
|
|
|
container.erase(it);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
++it;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-10-07 22:38:27 +02:00
|
|
|
template<typename Container, typename Predicate>
|
|
|
|
bool remove_locked_first_if(std::mutex &mutex, Container &container, Predicate pred) {
|
|
|
|
std::lock_guard<std::mutex> lock(mutex);
|
|
|
|
return remove_first_if(container, pred);
|
|
|
|
}
|
|
|
|
|
2023-12-16 17:36:57 +01:00
|
|
|
template<typename T, typename Predicate>
|
|
|
|
T pop_locked_first_if(std::mutex &mutex, std::vector<T> &container, Predicate pred) {
|
|
|
|
std::lock_guard<std::mutex> lock(mutex);
|
|
|
|
T result;
|
|
|
|
auto it = container.begin();
|
|
|
|
while (it != container.end()) {
|
|
|
|
if (pred(*it)) {
|
|
|
|
result = std::move(*it);
|
|
|
|
container.erase(it);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
++it;
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2024-11-27 20:44:20 +01:00
|
|
|
template<typename Container>
|
|
|
|
void append_move_all_values(Container &dest, Container &src) {
|
|
|
|
dest.insert(dest.end(), std::make_move_iterator(src.begin()), std::make_move_iterator(src.end()));
|
|
|
|
src.clear();
|
|
|
|
}
|
|
|
|
|
2022-10-03 21:51:12 +02:00
|
|
|
std::string getPluginPath();
|
|
|
|
|
2024-08-04 20:59:07 +02:00
|
|
|
std::string getModulePath();
|
|
|
|
|
2022-10-03 21:51:12 +02:00
|
|
|
OSDynLoad_Error CustomDynLoadAlloc(int32_t size, int32_t align, void **outAddr);
|
|
|
|
|
2024-08-04 20:59:07 +02:00
|
|
|
void CustomDynLoadFree(void *addr);
|
|
|
|
|
2024-08-09 16:41:29 +02:00
|
|
|
bool ParseJsonFromFile(const std::string &filePath, nlohmann::json &outJson);
|
|
|
|
|
|
|
|
std::vector<std::string> getPluginFilePaths(std::string_view basePath);
|
|
|
|
|
|
|
|
std::vector<std::string> getNonBaseAromaPluginFilenames(std::string_view basePath);
|