metaforce/Runtime/RetroTypes.hpp

404 lines
14 KiB
C++
Raw Normal View History

2018-10-07 03:42:33 +00:00
#pragma once
2015-08-17 20:33:58 +00:00
#include <functional>
#include <optional>
#include <string>
#include <utility>
#include <vector>
2022-02-16 05:21:24 +00:00
#include "GCNTypes.hpp"
#include "rstl.hpp"
#include <zeus/CMatrix3f.hpp>
#include <zeus/CMatrix4f.hpp>
#include <zeus/CTransform.hpp>
#include <zeus/CVector2f.hpp>
#include <zeus/CVector3f.hpp>
2015-08-17 20:33:58 +00:00
2017-12-30 01:09:45 +00:00
#undef min
#undef max
2017-11-24 08:23:28 +00:00
using namespace std::literals;
2022-02-16 05:21:24 +00:00
namespace athena::io {
class IStreamReader;
class IStreamWriter;
} // namespace athena::io
2021-04-10 08:42:06 +00:00
namespace metaforce {
2021-06-06 23:53:41 +00:00
using kUniqueIdType = u16;
static constexpr int kMaxEntities = 1024;
constexpr kUniqueIdType kUniqueIdSize = sizeof(u16);
constexpr kUniqueIdType kUniqueIdBits = kUniqueIdSize * 8;
constexpr kUniqueIdType kUniqueIdMax = UINT16_MAX;
constexpr kUniqueIdType kUniqueIdVersionMax = 64;
constexpr kUniqueIdType kUniqueIdVersionMask = kUniqueIdVersionMax - 1;
constexpr kUniqueIdType kUniqueIdValueMask = kMaxEntities - 1;
constexpr kUniqueIdType kUniqueIdValueBits = 10;
constexpr kUniqueIdType kUniqueIdVersionBits = 6;
2022-02-16 05:21:24 +00:00
#undef bswap16
#undef bswap32
#undef bswap64
/* Type-sensitive byte swappers */
template <typename T>
constexpr T bswap16(T val) noexcept {
#if __GNUC__
return __builtin_bswap16(val);
#elif _WIN32
return _byteswap_ushort(val);
#else
return (val = (val << 8) | ((val >> 8) & 0xFF));
#endif
}
template <typename T>
constexpr T bswap32(T val) noexcept {
#if __GNUC__
return __builtin_bswap32(val);
#elif _WIN32
return _byteswap_ulong(val);
#else
val = (val & 0x0000FFFF) << 16 | (val & 0xFFFF0000) >> 16;
val = (val & 0x00FF00FF) << 8 | (val & 0xFF00FF00) >> 8;
return val;
#endif
}
template <typename T>
constexpr T bswap64(T val) noexcept {
#if __GNUC__
return __builtin_bswap64(val);
#elif _WIN32
return _byteswap_uint64(val);
#else
return ((val & 0xFF00000000000000ULL) >> 56) | ((val & 0x00FF000000000000ULL) >> 40) |
((val & 0x0000FF0000000000ULL) >> 24) | ((val & 0x000000FF00000000ULL) >> 8) |
((val & 0x00000000FF000000ULL) << 8) | ((val & 0x0000000000FF0000ULL) << 24) |
((val & 0x000000000000FF00ULL) << 40) | ((val & 0x00000000000000FFULL) << 56);
#endif
}
#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
constexpr int16_t SBig(int16_t val) noexcept { return bswap16(val); }
constexpr uint16_t SBig(uint16_t val) noexcept { return bswap16(val); }
constexpr int32_t SBig(int32_t val) noexcept { return bswap32(val); }
constexpr uint32_t SBig(uint32_t val) noexcept { return bswap32(val); }
constexpr int64_t SBig(int64_t val) noexcept { return bswap64(val); }
constexpr uint64_t SBig(uint64_t val) noexcept { return bswap64(val); }
constexpr float SBig(float val) noexcept {
union {
float f;
atInt32 i;
} uval1 = {val};
union {
atInt32 i;
float f;
} uval2 = {bswap32(uval1.i)};
return uval2.f;
}
constexpr double SBig(double val) noexcept {
union {
double f;
atInt64 i;
} uval1 = {val};
union {
atInt64 i;
double f;
} uval2 = {bswap64(uval1.i)};
return uval2.f;
}
#ifndef SBIG
#define SBIG(q) (((q)&0x000000FF) << 24 | ((q)&0x0000FF00) << 8 | ((q)&0x00FF0000) >> 8 | ((q)&0xFF000000) >> 24)
#endif
constexpr int16_t SLittle(int16_t val) noexcept { return val; }
constexpr uint16_t SLittle(uint16_t val) noexcept { return val; }
constexpr int32_t SLittle(int32_t val) noexcept { return val; }
constexpr uint32_t SLittle(uint32_t val) noexcept { return val; }
constexpr int64_t SLittle(int64_t val) noexcept { return val; }
constexpr uint64_t SLittle(uint64_t val) noexcept { return val; }
constexpr float SLittle(float val) noexcept { return val; }
constexpr double SLittle(double val) noexcept { return val; }
#ifndef SLITTLE
#define SLITTLE(q) (q)
#endif
#else
constexpr int16_t SLittle(int16_t val) noexcept { return bswap16(val); }
constexpr uint16_t SLittle(uint16_t val) noexcept { return bswap16(val); }
constexpr int32_t SLittle(int32_t val) noexcept { return bswap32(val); }
constexpr uint32_t SLittle(uint32_t val) noexcept { return bswap32(val); }
constexpr int64_t SLittle(int64_t val) noexcept { return bswap64(val); }
constexpr uint64_t SLittle(uint64_t val) noexcept { return bswap64(val); }
constexpr float SLittle(float val) noexcept {
int32_t ival = bswap32(*((int32_t*)(&val)));
return *((float*)(&ival));
}
constexpr double SLittle(double val) noexcept {
int64_t ival = bswap64(*((int64_t*)(&val)));
return *((double*)(&ival));
}
#ifndef SLITTLE
#define SLITTLE(q) (((q)&0x000000FF) << 24 | ((q)&0x0000FF00) << 8 | ((q)&0x00FF0000) >> 8 | ((q)&0xFF000000) >> 24)
#endif
constexpr int16_t SBig(int16_t val) noexcept { return val; }
constexpr uint16_t SBig(uint16_t val) noexcept { return val; }
constexpr int32_t SBig(int32_t val) noexcept { return val; }
constexpr uint32_t SBig(uint32_t val) noexcept { return val; }
constexpr int64_t SBig(int64_t val) noexcept { return val; }
constexpr uint64_t SBig(uint64_t val) noexcept { return val; }
constexpr float SBig(float val) noexcept { return val; }
constexpr double SBig(double val) noexcept { return val; }
#ifndef SBIG
#define SBIG(q) (q)
#endif
#endif
class FourCC {
protected:
union {
char fcc[4];
uint32_t num = 0;
};
public:
// Sentinel FourCC
constexpr FourCC() noexcept = default;
constexpr FourCC(const FourCC& other) noexcept = default;
constexpr FourCC(FourCC&& other) noexcept = default;
constexpr FourCC(const char* name) noexcept : fcc{name[0], name[1], name[2], name[3]} {}
constexpr FourCC(uint32_t n) noexcept : num(n) {}
constexpr FourCC& operator=(const FourCC&) noexcept = default;
constexpr FourCC& operator=(FourCC&&) noexcept = default;
constexpr bool operator==(const FourCC& other) const noexcept { return num == other.num; }
constexpr bool operator!=(const FourCC& other) const noexcept { return !operator==(other); }
constexpr bool operator==(const char* other) const noexcept {
return other[0] == fcc[0] && other[1] == fcc[1] && other[2] == fcc[2] && other[3] == fcc[3];
}
constexpr bool operator!=(const char* other) const noexcept { return !operator==(other); }
constexpr bool operator==(int32_t other) const noexcept { return num == uint32_t(other); }
constexpr bool operator!=(int32_t other) const noexcept { return !operator==(other); }
constexpr bool operator==(uint32_t other) const noexcept { return num == other; }
constexpr bool operator!=(uint32_t other) const noexcept { return !operator==(other); }
std::string toString() const { return std::string(std::begin(fcc), std::end(fcc)); }
constexpr std::string_view toStringView() const { return std::string_view(fcc, std::size(fcc)); }
constexpr uint32_t toUint32() const noexcept { return num; }
constexpr const char* getChars() const noexcept { return fcc; }
constexpr char* getChars() noexcept { return fcc; }
constexpr bool IsValid() const noexcept { return num != 0; }
};
#define FOURCC(chars) FourCC(SBIG(chars))
using CInputStream = athena::io::IStreamReader;
using COutputStream = athena::io::IStreamWriter;
2017-08-13 05:26:14 +00:00
2018-12-08 05:30:43 +00:00
class CAssetId {
u64 id = UINT64_MAX;
2017-08-13 05:26:14 +00:00
public:
constexpr CAssetId() noexcept = default;
constexpr CAssetId(u64 v) noexcept { Assign(v); }
2018-12-08 05:30:43 +00:00
explicit CAssetId(CInputStream& in);
[[nodiscard]] constexpr bool IsValid() const noexcept { return id != UINT64_MAX; }
[[nodiscard]] constexpr u64 Value() const noexcept { return id; }
constexpr void Assign(u64 v) noexcept { id = (v == UINT32_MAX ? UINT64_MAX : (v == 0 ? UINT64_MAX : v)); }
constexpr void Reset() noexcept { id = UINT64_MAX; }
2018-12-08 05:30:43 +00:00
void PutTo(COutputStream& out);
[[nodiscard]] constexpr bool operator==(CAssetId other) const noexcept { return id == other.id; }
[[nodiscard]] constexpr bool operator!=(CAssetId other) const noexcept { return !operator==(other); }
[[nodiscard]] constexpr bool operator<(CAssetId other) const noexcept { return id < other.id; }
2017-08-13 05:26:14 +00:00
};
//#define kInvalidAssetId CAssetId()
2015-08-23 06:42:29 +00:00
2018-12-08 05:30:43 +00:00
struct SObjectTag {
FourCC type;
CAssetId id;
constexpr explicit operator bool() const noexcept { return id.IsValid(); }
[[nodiscard]] constexpr bool operator==(const SObjectTag& other) const noexcept { return id == other.id; }
[[nodiscard]] constexpr bool operator!=(const SObjectTag& other) const noexcept { return !operator==(other); }
[[nodiscard]] constexpr bool operator<(const SObjectTag& other) const noexcept { return id < other.id; }
constexpr SObjectTag() noexcept = default;
constexpr SObjectTag(FourCC tp, CAssetId rid) noexcept : type(tp), id(rid) {}
explicit SObjectTag(CInputStream& in) {
2018-12-08 05:30:43 +00:00
in.readBytesToBuf(&type, 4);
id = CAssetId(in);
}
void readMLVL(CInputStream& in) {
id = CAssetId(in);
in.readBytesToBuf(&type, 4);
}
2015-08-22 01:58:41 +00:00
};
2018-12-08 05:30:43 +00:00
struct TEditorId {
u32 id = UINT32_MAX;
constexpr TEditorId() noexcept = default;
constexpr TEditorId(u32 idin) noexcept : id(idin) {}
[[nodiscard]] constexpr u8 LayerNum() const noexcept { return u8((id >> 26) & 0x3f); }
[[nodiscard]] constexpr u16 AreaNum() const noexcept { return u16((id >> 16) & 0x3ff); }
[[nodiscard]] constexpr u16 Id() const noexcept { return u16(id & 0xffff); }
2021-06-06 23:53:41 +00:00
[[nodiscard]] constexpr bool operator<(TEditorId other) const noexcept {
return (id & 0x3ffffff) < (other.id & 0x3ffffff);
}
[[nodiscard]] constexpr bool operator==(TEditorId other) const noexcept {
return (id & 0x3ffffff) == (other.id & 0x3ffffff);
}
[[nodiscard]] constexpr bool operator!=(TEditorId other) const noexcept { return !operator==(other); }
};
2017-08-13 05:26:14 +00:00
#define kInvalidEditorId TEditorId()
2018-12-08 05:30:43 +00:00
struct TUniqueId {
2021-06-06 23:53:41 +00:00
kUniqueIdType id = kUniqueIdMax;
constexpr TUniqueId() noexcept = default;
2021-06-06 23:53:41 +00:00
constexpr TUniqueId(kUniqueIdType value, kUniqueIdType version) noexcept
: id(value | (version << kUniqueIdValueBits)) {}
[[nodiscard]] constexpr kUniqueIdType Version() const noexcept {
return kUniqueIdType((id >> kUniqueIdValueBits) & kUniqueIdVersionMask);
}
[[nodiscard]] constexpr kUniqueIdType Value() const noexcept { return kUniqueIdType(id & kUniqueIdValueMask); }
[[nodiscard]] constexpr bool operator<(TUniqueId other) const noexcept { return id < other.id; }
[[nodiscard]] constexpr bool operator==(TUniqueId other) const noexcept { return id == other.id; }
[[nodiscard]] constexpr bool operator!=(TUniqueId other) const noexcept { return !operator==(other); }
};
2017-08-13 05:26:14 +00:00
#define kInvalidUniqueId TUniqueId()
using EntityList = rstl::reserved_vector<TUniqueId, kMaxEntities>;
2017-08-13 05:26:14 +00:00
2017-01-15 03:59:37 +00:00
using TAreaId = s32;
2015-08-20 02:52:07 +00:00
#define kInvalidAreaId TAreaId(-1)
2016-09-10 04:50:00 +00:00
#if 0
template <class T, size_t N>
class TRoundRobin
{
rstl::reserved_vector<T, N> vals;
public:
TRoundRobin(const T& val) : vals(N, val) {}
void PushBack(const T& val) { vals.push_back(val); }
size_t Size() const { return vals.size(); }
const T& GetLastValue() const { return vals.back(); }
void Clear() { vals.clear(); }
const T& GetValue(s32) const {}
};
#endif
2018-12-08 05:30:43 +00:00
template <class T>
[[nodiscard]] T GetAverage(const T* v, s32 count) noexcept {
2018-12-08 05:30:43 +00:00
T r = v[0];
for (s32 i = 1; i < count; ++i)
r += v[i];
2018-12-08 05:30:43 +00:00
return r / count;
}
2018-12-08 05:30:43 +00:00
template <class T, size_t N>
class TReservedAverage : rstl::reserved_vector<T, N> {
2016-09-16 22:21:19 +00:00
public:
2018-12-08 05:30:43 +00:00
TReservedAverage() = default;
2016-09-16 22:21:19 +00:00
2018-12-08 05:30:43 +00:00
TReservedAverage(const T& t) { rstl::reserved_vector<T, N>::resize(N, t); }
2018-12-08 05:30:43 +00:00
void AddValue(const T& t) {
if (this->size() < N) {
this->insert(this->begin(), t);
} else {
this->pop_back();
this->insert(this->begin(), t);
2016-09-16 22:21:19 +00:00
}
2021-06-06 23:53:41 +00:00
}
2016-09-16 22:21:19 +00:00
[[nodiscard]] std::optional<T> GetAverage() const {
if (this->empty()) {
return std::nullopt;
}
2018-12-08 05:30:43 +00:00
2021-04-10 08:42:06 +00:00
return {metaforce::GetAverage<T>(this->data(), this->size())};
2018-12-08 05:30:43 +00:00
}
2017-07-31 05:19:05 +00:00
[[nodiscard]] std::optional<T> GetEntry(int i) const {
if (i >= this->size()) {
return std::nullopt;
}
2018-12-08 05:30:43 +00:00
return this->operator[](i);
}
2017-09-30 03:45:57 +00:00
2018-12-08 05:30:43 +00:00
void Clear() { this->clear(); }
[[nodiscard]] size_t Size() const { return this->size(); }
};
2016-09-16 22:21:19 +00:00
2021-04-10 08:42:06 +00:00
} // namespace metaforce
2018-12-08 05:30:43 +00:00
namespace std {
2022-02-16 05:21:24 +00:00
template <>
struct hash<metaforce::FourCC> {
size_t operator()(const metaforce::FourCC& val) const noexcept { return val.toUint32(); }
};
2016-09-10 04:50:00 +00:00
template <>
2021-04-10 08:42:06 +00:00
struct hash<metaforce::SObjectTag> {
size_t operator()(const metaforce::SObjectTag& tag) const noexcept { return tag.id.Value(); }
2017-08-13 05:26:14 +00:00
};
template <>
2021-04-10 08:42:06 +00:00
struct hash<metaforce::CAssetId> {
size_t operator()(const metaforce::CAssetId& id) const noexcept { return id.Value(); }
2015-08-23 06:42:29 +00:00
};
2018-12-08 05:30:43 +00:00
} // namespace std
2019-04-07 05:14:48 +00:00
2021-04-10 08:42:06 +00:00
FMT_CUSTOM_FORMATTER(metaforce::CAssetId, "{:08X}", obj.Value())
FMT_CUSTOM_FORMATTER(metaforce::TEditorId, "{:08X}", obj.id)
2021-06-06 23:53:41 +00:00
static_assert(sizeof(metaforce::kUniqueIdType) == sizeof(u16),
"TUniqueId size does not match expected size! Update TUniqueId format string!");
2021-04-10 08:42:06 +00:00
FMT_CUSTOM_FORMATTER(metaforce::TUniqueId, "{:04X}", obj.id)
2022-02-16 05:21:24 +00:00
FMT_CUSTOM_FORMATTER(metaforce::FourCC, "{:c}{:c}{:c}{:c}", obj.getChars()[0], obj.getChars()[1], obj.getChars()[2],
obj.getChars()[3])
2021-04-10 08:42:06 +00:00
FMT_CUSTOM_FORMATTER(metaforce::SObjectTag, "{} {}", obj.type, obj.id)
2019-07-28 01:21:31 +00:00
FMT_CUSTOM_FORMATTER(zeus::CVector3f, "({} {} {})", float(obj.x()), float(obj.y()), float(obj.z()))
FMT_CUSTOM_FORMATTER(zeus::CVector2f, "({} {})", float(obj.x()), float(obj.y()))
2021-06-06 23:53:41 +00:00
FMT_CUSTOM_FORMATTER(zeus::CMatrix3f,
"\n({} {} {})"
"\n({} {} {})"
"\n({} {} {})",
float(obj[0][0]), float(obj[1][0]), float(obj[2][0]), float(obj[0][1]), float(obj[1][1]),
float(obj[2][1]), float(obj[0][2]), float(obj[1][2]), float(obj[2][2]))
FMT_CUSTOM_FORMATTER(zeus::CMatrix4f,
"\n({} {} {} {})"
"\n({} {} {} {})"
"\n({} {} {} {})"
"\n({} {} {} {})",
float(obj[0][0]), float(obj[1][0]), float(obj[2][0]), float(obj[3][0]), float(obj[0][1]),
float(obj[1][1]), float(obj[2][1]), float(obj[3][1]), float(obj[0][2]), float(obj[1][2]),
float(obj[2][2]), float(obj[3][2]), float(obj[0][3]), float(obj[1][3]), float(obj[2][3]),
float(obj[3][3]))
FMT_CUSTOM_FORMATTER(zeus::CTransform,
"\n({} {} {} {})"
"\n({} {} {} {})"
"\n({} {} {} {})",
2019-07-20 04:27:21 +00:00
float(obj.basis[0][0]), float(obj.basis[1][0]), float(obj.basis[2][0]), float(obj.origin[0]),
float(obj.basis[0][1]), float(obj.basis[1][1]), float(obj.basis[2][1]), float(obj.origin[1]),
float(obj.basis[0][2]), float(obj.basis[1][2]), float(obj.basis[2][2]), float(obj.origin[2]))
2019-04-07 05:14:48 +00:00
#if defined(__has_feature)
#if __has_feature(memory_sanitizer)
#define URDE_MSAN 1
#endif
#endif