metaforce/DataSpec/DNACommon/DNACommon.hpp

468 lines
14 KiB
C++
Raw Normal View History

2018-10-07 03:42:33 +00:00
#pragma once
2015-06-24 20:56:23 +00:00
2017-12-29 08:08:12 +00:00
#include <cstdio>
#include "logvisor/logvisor.hpp"
#include "athena/DNAYaml.hpp"
2016-03-04 23:04:53 +00:00
#include "hecl/Database.hpp"
2015-08-05 21:46:07 +00:00
#include "../SpecBase.hpp"
2016-04-10 04:49:02 +00:00
#include "boo/ThreadLocalPtr.hpp"
2016-09-16 20:18:03 +00:00
#include "zeus/CColor.hpp"
2015-06-24 20:56:23 +00:00
2016-02-13 09:02:47 +00:00
namespace DataSpec
2015-07-01 23:50:39 +00:00
{
2017-12-29 08:08:12 +00:00
struct SpecBase;
2015-07-01 23:50:39 +00:00
2016-03-04 23:04:53 +00:00
extern logvisor::Module LogDNACommon;
2016-04-10 04:49:02 +00:00
extern ThreadLocalPtr<SpecBase> g_curSpec;
extern ThreadLocalPtr<class PAKRouterBase> g_PakRouter;
2017-12-29 08:08:12 +00:00
extern ThreadLocalPtr<hecl::blender::Token> g_ThreadBlenderToken;
2015-07-16 01:57:34 +00:00
2015-06-24 20:56:23 +00:00
/* This comes up a great deal */
2018-02-22 07:24:51 +00:00
typedef athena::io::DNA<athena::Big> BigDNA;
typedef athena::io::DNAV<athena::Big> BigDNAV;
typedef athena::io::DNAVYaml<athena::Big> BigDNAVYaml;
2015-06-24 20:56:23 +00:00
2015-11-10 02:07:15 +00:00
/** FourCC with DNA read/write */
using DNAFourCC = hecl::DNAFourCC;
2015-07-11 22:41:10 +00:00
2018-02-22 07:24:51 +00:00
class DNAColor final : public BigDNA, public zeus::CColor
2016-09-16 20:18:03 +00:00
{
public:
DNAColor() = default;
DNAColor(const zeus::CColor& color) : zeus::CColor(color) {}
2018-02-22 07:24:51 +00:00
AT_DECL_EXPLICIT_DNA_YAML
};
template <> inline void DNAColor::Enumerate<BigDNA::Read>(typename Read::StreamT& _r)
{ zeus::CColor::readRGBABig(_r); }
template <> inline void DNAColor::Enumerate<BigDNA::Write>(typename Write::StreamT& _w)
{ zeus::CColor::writeRGBABig(_w); }
template <> inline void DNAColor::Enumerate<BigDNA::ReadYaml>(typename ReadYaml::StreamT& _r)
{
size_t count;
if (auto v = _r.enterSubVector(nullptr, count))
2016-09-16 20:18:03 +00:00
{
2018-02-22 07:24:51 +00:00
r = (count >= 1) ? _r.readFloat(nullptr) : 0.f;
g = (count >= 2) ? _r.readFloat(nullptr) : 0.f;
b = (count >= 3) ? _r.readFloat(nullptr) : 0.f;
a = (count >= 4) ? _r.readFloat(nullptr) : 0.f;
2016-09-16 20:18:03 +00:00
}
2018-02-22 07:24:51 +00:00
}
template <> inline void DNAColor::Enumerate<BigDNA::WriteYaml>(typename WriteYaml::StreamT& _w)
{
if (auto v = _w.enterSubVector(nullptr))
2016-09-16 20:18:03 +00:00
{
2018-02-22 07:24:51 +00:00
_w.writeFloat(nullptr, r);
_w.writeFloat(nullptr, g);
_w.writeFloat(nullptr, b);
_w.writeFloat(nullptr, a);
2016-09-16 20:18:03 +00:00
}
2018-02-22 07:24:51 +00:00
}
template <> inline void DNAColor::Enumerate<BigDNA::BinarySize>(typename BinarySize::StreamT& _s)
{ _s += 16; }
2016-09-16 20:18:03 +00:00
2016-03-04 23:04:53 +00:00
using FourCC = hecl::FourCC;
class UniqueID32;
class UniqueID64;
class UniqueID128;
/** Common virtual interface for runtime ambiguity resolution */
class PAKRouterBase
{
protected:
const SpecBase& m_dataSpec;
public:
PAKRouterBase(const SpecBase& dataSpec) : m_dataSpec(dataSpec) {}
2016-03-05 00:03:41 +00:00
hecl::Database::Project& getProject() const {return m_dataSpec.getProject();}
virtual hecl::ProjectPath getWorking(const UniqueID32&, bool silenceWarnings=false) const
{
2016-03-05 00:03:41 +00:00
LogDNACommon.report(logvisor::Fatal,
"PAKRouter IDType mismatch; expected UniqueID32 specialization");
2016-03-05 00:03:41 +00:00
return hecl::ProjectPath();
}
virtual hecl::ProjectPath getWorking(const UniqueID64&, bool silenceWarnings=false) const
{
2016-03-05 00:03:41 +00:00
LogDNACommon.report(logvisor::Fatal,
"PAKRouter IDType mismatch; expected UniqueID64 specialization");
2016-03-05 00:03:41 +00:00
return hecl::ProjectPath();
}
virtual hecl::ProjectPath getWorking(const UniqueID128&, bool silenceWarnings=false) const
{
2016-03-05 00:03:41 +00:00
LogDNACommon.report(logvisor::Fatal,
"PAKRouter IDType mismatch; expected UniqueID128 specialization");
2016-03-05 00:03:41 +00:00
return hecl::ProjectPath();
}
};
/** Globally-accessed manager allowing UniqueID* classes to directly
* lookup destination paths of resources */
class UniqueIDBridge
{
friend class UniqueID32;
friend class UniqueID64;
2016-04-10 04:49:02 +00:00
static ThreadLocalPtr<hecl::Database::Project> s_Project;
static ThreadLocalPtr<IDRestorer<UniqueID32>> s_restorer32;
static ThreadLocalPtr<IDRestorer<UniqueID64>> s_restorer64;
static ThreadLocalPtr<IDRestorer<UniqueID128>> s_restorer128;
public:
template <class IDType>
static hecl::ProjectPath TranslatePakIdToPath(const IDType& id, bool silenceWarnings=false);
2016-10-02 22:41:36 +00:00
template <class IDType>
2017-11-13 06:19:18 +00:00
static hecl::ProjectPath MakePathFromString(std::string_view str);
template <class IDType>
static void TransformOldHashToNewHash(IDType& id);
static void SetThreadProject(hecl::Database::Project& project);
template <class IDType>
static IDRestorer<IDType>* GetIDRestorer();
template <class IDType>
static void SetIDRestorer(IDRestorer<IDType>* restorer);
};
2015-08-23 06:42:29 +00:00
template <>
inline IDRestorer<UniqueID32>* UniqueIDBridge::GetIDRestorer<UniqueID32>()
{
return s_restorer32.get();
}
template <>
inline void UniqueIDBridge::SetIDRestorer<UniqueID32>(IDRestorer<UniqueID32>* restorer)
{
s_restorer32.reset(restorer);
}
template <>
inline IDRestorer<UniqueID64>* UniqueIDBridge::GetIDRestorer<UniqueID64>()
{
return s_restorer64.get();
}
template <>
inline void UniqueIDBridge::SetIDRestorer<UniqueID64>(IDRestorer<UniqueID64>* restorer)
{
s_restorer64.reset(restorer);
}
template <>
inline IDRestorer<UniqueID128>* UniqueIDBridge::GetIDRestorer<UniqueID128>()
{
return s_restorer128.get();
}
template <>
inline void UniqueIDBridge::SetIDRestorer<UniqueID128>(IDRestorer<UniqueID128>* restorer)
{
s_restorer128.reset(restorer);
}
2015-11-10 02:07:15 +00:00
/** PAK 32-bit Unique ID */
2018-02-22 07:24:51 +00:00
class UniqueID32 : public BigDNA
2015-06-24 20:56:23 +00:00
{
protected:
2015-08-11 23:32:02 +00:00
uint32_t m_id = 0xffffffff;
2015-06-24 20:56:23 +00:00
public:
using value_type = uint32_t;
static UniqueID32 kInvalidId;
2018-02-22 07:24:51 +00:00
AT_DECL_EXPLICIT_DNA_YAML
operator bool() const {return m_id != 0xffffffff && m_id != 0;}
void assign(uint32_t id, bool noOriginal = false);
2015-07-06 01:33:06 +00:00
2016-03-04 23:04:53 +00:00
UniqueID32& operator=(const hecl::ProjectPath& path)
2017-03-20 05:09:53 +00:00
{assign(path.hash().val32()); return *this;}
2015-11-10 02:07:15 +00:00
2015-09-22 01:58:26 +00:00
bool operator!=(const UniqueID32& other) const {return m_id != other.m_id;}
bool operator==(const UniqueID32& other) const {return m_id == other.m_id;}
bool operator<(const UniqueID32& other) const {return m_id < other.m_id;}
2015-09-22 01:58:26 +00:00
uint32_t toUint32() const {return m_id;}
uint64_t toUint64() const {return m_id;}
std::string toString() const;
2015-11-10 02:07:15 +00:00
void clear() {m_id = 0xffffffff;}
2015-10-19 03:28:47 +00:00
UniqueID32() = default;
UniqueID32(uint32_t idin, bool noOriginal = false) {assign(idin, noOriginal);}
2016-03-04 23:04:53 +00:00
UniqueID32(athena::io::IStreamReader& reader) {read(reader);}
UniqueID32(const hecl::ProjectPath& path) {*this = path;}
2015-10-19 03:28:47 +00:00
UniqueID32(const char* hexStr)
{
char copy[9];
strncpy(copy, hexStr, 8);
copy[8] = '\0';
2017-03-20 05:09:53 +00:00
assign(strtoul(copy, nullptr, 16));
2015-10-19 03:28:47 +00:00
}
2015-11-10 20:12:43 +00:00
UniqueID32(const wchar_t* hexStr)
{
wchar_t copy[9];
wcsncpy(copy, hexStr, 8);
copy[8] = L'\0';
2017-03-20 05:09:53 +00:00
assign(wcstoul(copy, nullptr, 16));
2015-11-10 20:12:43 +00:00
}
2015-11-10 02:07:15 +00:00
static constexpr size_t BinarySize() {return 4;}
2015-06-24 20:56:23 +00:00
};
2018-04-02 04:27:24 +00:00
/** PAK 32-bit Unique ID - writes zero when invalid */
class UniqueID32Zero : public UniqueID32
{
public:
AT_DECL_DNA_YAML
Delete __d2;
using UniqueID32::UniqueID32;
};
class AuxiliaryID32 : public UniqueID32
{
const hecl::SystemChar* m_auxStr;
const hecl::SystemChar* m_addExtension;
UniqueID32 m_baseId;
public:
2018-02-22 07:24:51 +00:00
AT_DECL_DNA
Delete __d2;
AuxiliaryID32(const hecl::SystemChar* auxStr,
const hecl::SystemChar* addExtension=nullptr)
: m_auxStr(auxStr), m_addExtension(addExtension) {}
AuxiliaryID32& operator=(const hecl::ProjectPath& path);
AuxiliaryID32& operator=(const UniqueID32& id);
const UniqueID32& getBaseId() const {return m_baseId;}
};
2015-11-10 02:07:15 +00:00
/** PAK 64-bit Unique ID */
2018-02-22 07:24:51 +00:00
class UniqueID64 : public BigDNA
2015-06-24 20:56:23 +00:00
{
2015-08-11 23:32:02 +00:00
uint64_t m_id = 0xffffffffffffffff;
2015-06-24 20:56:23 +00:00
public:
using value_type = uint64_t;
2018-02-22 07:24:51 +00:00
AT_DECL_EXPLICIT_DNA_YAML
operator bool() const {return m_id != 0xffffffffffffffff && m_id != 0;}
void assign(uint64_t id, bool noOriginal = false);
2015-07-06 01:33:06 +00:00
2016-03-04 23:04:53 +00:00
UniqueID64& operator=(const hecl::ProjectPath& path)
2017-03-20 05:09:53 +00:00
{assign(path.hash().val64()); return *this;}
2015-11-10 02:07:15 +00:00
2015-09-22 01:58:26 +00:00
bool operator!=(const UniqueID64& other) const {return m_id != other.m_id;}
bool operator==(const UniqueID64& other) const {return m_id == other.m_id;}
bool operator<(const UniqueID64& other) const {return m_id < other.m_id;}
2015-09-22 01:58:26 +00:00
uint64_t toUint64() const {return m_id;}
std::string toString() const;
2015-11-10 02:07:15 +00:00
void clear() {m_id = 0xffffffffffffffff;}
2015-10-19 03:28:47 +00:00
UniqueID64() = default;
UniqueID64(uint64_t idin, bool noOriginal = false) {assign(idin, noOriginal);}
2016-03-04 23:04:53 +00:00
UniqueID64(athena::io::IStreamReader& reader) {read(reader);}
UniqueID64(const hecl::ProjectPath& path) {*this = path;}
2015-10-19 03:28:47 +00:00
UniqueID64(const char* hexStr)
{
char copy[17];
strncpy(copy, hexStr, 16);
copy[16] = '\0';
2015-11-10 20:12:43 +00:00
#if _WIN32
2017-03-20 05:09:53 +00:00
assign(_strtoui64(copy, nullptr, 16));
2015-11-10 20:12:43 +00:00
#else
2017-03-20 05:09:53 +00:00
assign(strtouq(copy, nullptr, 16));
2015-11-10 20:12:43 +00:00
#endif
}
UniqueID64(const wchar_t* hexStr)
{
wchar_t copy[17];
wcsncpy(copy, hexStr, 16);
copy[16] = L'\0';
#if _WIN32
2017-03-20 05:09:53 +00:00
assign(_wcstoui64(copy, nullptr, 16));
2015-11-10 20:12:43 +00:00
#else
2017-03-20 05:09:53 +00:00
assign(wcstoull(copy, nullptr, 16));
2015-11-10 20:12:43 +00:00
#endif
2015-10-19 03:28:47 +00:00
}
2015-11-10 02:07:15 +00:00
static constexpr size_t BinarySize() {return 8;}
2015-06-24 20:56:23 +00:00
};
2015-11-10 02:07:15 +00:00
/** PAK 128-bit Unique ID */
2018-02-22 07:24:51 +00:00
class UniqueID128 : public BigDNA
2015-06-24 20:56:23 +00:00
{
public:
union Value
2015-07-06 01:33:06 +00:00
{
uint64_t id[2];
2015-07-06 01:33:06 +00:00
#if __SSE__
__m128i id128;
2015-07-06 01:33:06 +00:00
#endif
};
private:
Value m_id;
2015-06-24 20:56:23 +00:00
public:
using value_type = uint64_t;
2018-02-22 07:24:51 +00:00
AT_DECL_EXPLICIT_DNA_YAML
UniqueID128() {m_id.id[0]=0xffffffffffffffff; m_id.id[1]=0xffffffffffffffff;}
UniqueID128(uint64_t idin, bool noOriginal = false)
{
m_id.id[0] = idin;
m_id.id[1] = 0;
}
2015-09-22 01:58:26 +00:00
operator bool() const
{return m_id.id[0] != 0xffffffffffffffff && m_id.id[0] != 0 && m_id.id[1] != 0xffffffffffffffff && m_id.id[1] != 0;}
2015-07-06 01:33:06 +00:00
2016-03-05 00:03:41 +00:00
UniqueID128& operator=(const hecl::ProjectPath& path)
{
m_id.id[0] = path.hash().val64();
m_id.id[1] = 0;
return *this;
}
2016-10-02 22:41:36 +00:00
UniqueID128(const hecl::ProjectPath& path) {*this = path;}
2015-09-22 01:58:26 +00:00
bool operator!=(const UniqueID128& other) const
2015-07-06 01:33:06 +00:00
{
#if __SSE__
__m128i vcmp = _mm_cmpeq_epi32(m_id.id128, other.m_id.id128);
2015-07-06 02:07:57 +00:00
int vmask = _mm_movemask_epi8(vcmp);
return vmask != 0xffff;
2015-07-06 01:33:06 +00:00
#else
return (m_id.id[0] != other.m_id.id[0]) || (m_id.id[1] != other.m_id.id[1]);
2015-07-06 01:33:06 +00:00
#endif
}
2015-09-22 01:58:26 +00:00
bool operator==(const UniqueID128& other) const
2015-07-06 01:33:06 +00:00
{
#if __SSE__
__m128i vcmp = _mm_cmpeq_epi32(m_id.id128, other.m_id.id128);
2015-07-06 02:07:57 +00:00
int vmask = _mm_movemask_epi8(vcmp);
return vmask == 0xffff;
2015-07-06 01:33:06 +00:00
#else
return (m_id.id[0] == other.m_id.id[0]) && (m_id.id[1] == other.m_id.id[1]);
2015-07-06 01:33:06 +00:00
#endif
}
void clear() {m_id.id[0] = 0xffffffffffffffff; m_id.id[1] = 0xffffffffffffffff;}
uint64_t toUint64() const {return m_id.id[0];}
uint64_t toHighUint64() const {return m_id.id[0];}
uint64_t toLowUint64() const {return m_id.id[1];}
std::string toString() const;
2015-11-10 02:07:15 +00:00
static constexpr size_t BinarySize() {return 16;}
2015-06-24 20:56:23 +00:00
};
2018-04-02 04:27:24 +00:00
/** Casts ID type to its null-zero equivalent */
template <class T>
using CastIDToZero = typename std::conditional_t<std::is_same_v<T, UniqueID32>, UniqueID32Zero, T>;
2015-11-10 02:07:15 +00:00
/** Word Bitmap reader/writer */
2015-08-31 03:44:42 +00:00
class WordBitmap
2015-08-11 23:32:02 +00:00
{
std::vector<atUint32> m_words;
size_t m_bitCount = 0;
2015-08-31 03:44:42 +00:00
public:
void read(athena::io::IStreamReader& reader, size_t bitCount);
void write(athena::io::IStreamWriter& writer) const;
2016-10-09 07:45:04 +00:00
void reserve(size_t bitCount) { m_words.reserve((bitCount + 31) / 32); }
2018-03-04 06:15:40 +00:00
void binarySize(size_t& __isz) const;
2015-08-11 23:32:02 +00:00
size_t getBitCount() const {return m_bitCount;}
bool getBit(size_t idx) const
{
size_t wordIdx = idx / 32;
if (wordIdx >= m_words.size())
return false;
size_t wordCur = idx % 32;
return (m_words[wordIdx] >> wordCur) & 0x1;
}
void setBit(size_t idx)
{
size_t wordIdx = idx / 32;
while (wordIdx >= m_words.size())
m_words.push_back(0);
size_t wordCur = idx % 32;
m_words[wordIdx] |= (1 << wordCur);
2016-10-09 07:45:04 +00:00
m_bitCount = std::max(m_bitCount, idx + 1);
2015-08-11 23:32:02 +00:00
}
void unsetBit(size_t idx)
{
size_t wordIdx = idx / 32;
while (wordIdx >= m_words.size())
m_words.push_back(0);
size_t wordCur = idx % 32;
m_words[wordIdx] &= ~(1 << wordCur);
2016-10-09 07:45:04 +00:00
m_bitCount = std::max(m_bitCount, idx + 1);
2015-08-11 23:32:02 +00:00
}
2016-10-09 07:45:04 +00:00
void clear() { m_words.clear(); m_bitCount = 0; }
2015-08-11 23:32:02 +00:00
2017-12-07 04:13:12 +00:00
class Iterator
2015-08-11 23:32:02 +00:00
{
friend class WordBitmap;
const WordBitmap& m_bmp;
size_t m_idx = 0;
Iterator(const WordBitmap& bmp, size_t idx) : m_bmp(bmp), m_idx(idx) {}
public:
2017-12-07 04:13:12 +00:00
using iterator_category = std::forward_iterator_tag;
using value_type = bool;
using difference_type = std::ptrdiff_t;
using pointer = bool*;
using reference = bool&;
2015-08-11 23:32:02 +00:00
Iterator& operator++() {++m_idx; return *this;}
bool operator*() {return m_bmp.getBit(m_idx);}
bool operator!=(const Iterator& other) const {return m_idx != other.m_idx;}
};
Iterator begin() const {return Iterator(*this, 0);}
Iterator end() const {return Iterator(*this, m_bitCount);}
};
2015-11-10 02:07:15 +00:00
/** Resource cooker function */
2016-03-04 23:04:53 +00:00
typedef std::function<bool(const hecl::ProjectPath&, const hecl::ProjectPath&)> ResCooker;
2015-07-10 05:28:08 +00:00
/** Mappings of resources involved in extracting characters */
template <class IDType>
struct CharacterAssociations
{
using RigPair = std::pair<IDType, IDType>;
/* CMDL -> (CSKR, CINF) */
std::unordered_map<IDType, RigPair> m_cmdlRigs;
/* (CSKR, CINF) -> ANCS */
std::unordered_map<IDType, std::pair<IDType, std::string>> m_cskrCinfToCharacter;
/* ANCS -> (CINF, CMDL) */
std::unordered_multimap<IDType, std::pair<RigPair, std::string>> m_characterToAttachmentRigs;
using MultimapIteratorPair = std::pair<
typename std::unordered_multimap<IDType, std::pair<RigPair, std::string>>::const_iterator,
typename std::unordered_multimap<IDType, std::pair<RigPair, std::string>>::const_iterator>;
void addAttachmentRig(IDType character, IDType cinf, IDType cmdl, const char* name)
{
auto range = m_characterToAttachmentRigs.equal_range(character);
for (auto it = range.first; it != range.second; ++it)
if (it->second.second == name)
return;
m_characterToAttachmentRigs.insert(
std::make_pair(character, std::make_pair(std::make_pair(cinf, cmdl), name)));
}
};
2015-07-06 01:33:06 +00:00
}
/* Hash template-specializations for UniqueID types */
namespace std
2015-06-24 20:56:23 +00:00
{
template<>
2016-02-13 09:02:47 +00:00
struct hash<DataSpec::DNAFourCC>
{
2016-02-13 09:02:47 +00:00
size_t operator()(const DataSpec::DNAFourCC& fcc) const
{return fcc.toUint32();}
};
2015-07-06 01:33:06 +00:00
template<>
2016-02-13 09:02:47 +00:00
struct hash<DataSpec::UniqueID32>
2015-07-06 01:33:06 +00:00
{
2016-02-13 09:02:47 +00:00
size_t operator()(const DataSpec::UniqueID32& id) const
2015-07-06 02:07:57 +00:00
{return id.toUint32();}
2015-07-06 01:33:06 +00:00
};
template<>
2016-02-13 09:02:47 +00:00
struct hash<DataSpec::UniqueID64>
2015-07-06 01:33:06 +00:00
{
2016-02-13 09:02:47 +00:00
size_t operator()(const DataSpec::UniqueID64& id) const
2015-07-06 02:07:57 +00:00
{return id.toUint64();}
2015-06-24 20:56:23 +00:00
};
2015-07-06 01:33:06 +00:00
template<>
2016-02-13 09:02:47 +00:00
struct hash<DataSpec::UniqueID128>
2015-07-06 01:33:06 +00:00
{
2016-02-13 09:02:47 +00:00
size_t operator()(const DataSpec::UniqueID128& id) const
2015-07-06 02:07:57 +00:00
{return id.toHighUint64() ^ id.toLowUint64();}
2015-07-06 01:33:06 +00:00
};
2015-07-01 23:50:39 +00:00
}