metaforce/DataSpec/DNACommon/DNACommon.hpp

261 lines
6.9 KiB
C++
Raw Normal View History

2015-06-24 20:56:23 +00:00
#ifndef __DNA_COMMON_HPP__
#define __DNA_COMMON_HPP__
#include <Athena/DNA.hpp>
2015-07-06 01:33:06 +00:00
#include "HECL/HECL.hpp"
2015-06-24 20:56:23 +00:00
2015-07-01 23:50:39 +00:00
namespace Retro
{
2015-07-16 01:57:34 +00:00
extern LogVisor::LogModule LogDNACommon;
2015-06-24 20:56:23 +00:00
/* This comes up a great deal */
typedef Athena::io::DNA<Athena::BigEndian> BigDNA;
/* FourCC with DNA read/write */
2015-07-11 22:41:10 +00:00
class FourCC final : public BigDNA, public HECL::FourCC
{
public:
FourCC() : HECL::FourCC() {}
FourCC(const HECL::FourCC& other)
: HECL::FourCC() {num = other.toUint32();}
2015-07-11 22:41:10 +00:00
FourCC(const char* name)
: HECL::FourCC(name) {}
Delete expl;
inline void read(Athena::io::IStreamReader& reader)
{reader.readUBytesToBuf(fcc, 4);}
inline void write(Athena::io::IStreamWriter& writer) const
{writer.writeUBytes((atUint8*)fcc, 4);}
};
2015-07-06 01:33:06 +00:00
/* PAK 32-bit Unique ID */
class UniqueID32 : public BigDNA
2015-06-24 20:56:23 +00:00
{
2015-07-06 01:33:06 +00:00
uint32_t m_id;
2015-06-24 20:56:23 +00:00
public:
Delete expl;
2015-07-06 01:33:06 +00:00
inline void read(Athena::io::IStreamReader& reader)
2015-07-12 04:26:26 +00:00
{m_id = reader.readUint32();}
2015-07-06 01:33:06 +00:00
inline void write(Athena::io::IStreamWriter& writer) const
{writer.writeUint32(m_id);}
inline bool operator!=(const UniqueID32& other) const {return m_id != other.m_id;}
inline bool operator==(const UniqueID32& other) const {return m_id == other.m_id;}
2015-07-06 02:07:57 +00:00
inline uint32_t toUint32() const {return m_id;}
2015-07-06 01:33:06 +00:00
inline std::string toString() const
{
char buf[9];
snprintf(buf, 9, "%08X", m_id);
return std::string(buf);
}
2015-06-24 20:56:23 +00:00
};
2015-07-06 01:33:06 +00:00
/* PAK 64-bit Unique ID */
class UniqueID64 : public BigDNA
2015-06-24 20:56:23 +00:00
{
2015-07-06 01:33:06 +00:00
uint64_t m_id;
2015-06-24 20:56:23 +00:00
public:
Delete expl;
2015-07-06 01:33:06 +00:00
inline void read(Athena::io::IStreamReader& reader)
2015-07-12 04:26:26 +00:00
{m_id = reader.readUint64();}
2015-07-06 01:33:06 +00:00
inline void write(Athena::io::IStreamWriter& writer) const
{writer.writeUint64(m_id);}
inline bool operator!=(const UniqueID64& other) const {return m_id != other.m_id;}
inline bool operator==(const UniqueID64& other) const {return m_id == other.m_id;}
2015-07-06 02:07:57 +00:00
inline uint64_t toUint64() const {return m_id;}
2015-07-06 01:33:06 +00:00
inline std::string toString() const
{
char buf[17];
2015-07-06 02:07:57 +00:00
snprintf(buf, 17, "%16lX", m_id);
2015-07-06 01:33:06 +00:00
return std::string(buf);
}
2015-06-24 20:56:23 +00:00
};
2015-07-06 01:33:06 +00:00
/* PAK 128-bit Unique ID */
class UniqueID128 : public BigDNA
2015-06-24 20:56:23 +00:00
{
2015-07-06 01:33:06 +00:00
union
{
uint64_t m_id[2];
#if __SSE__
__m128i m_id128;
#endif
};
2015-06-24 20:56:23 +00:00
public:
Delete expl;
2015-07-06 01:33:06 +00:00
inline void read(Athena::io::IStreamReader& reader)
{
m_id[0] = reader.readUint64();
m_id[1] = reader.readUint64();
}
inline void write(Athena::io::IStreamWriter& writer) const
{
writer.writeUint64(m_id[0]);
writer.writeUint64(m_id[1]);
}
inline bool operator!=(const UniqueID128& other) const
{
#if __SSE__
2015-07-06 02:07:57 +00:00
__m128i vcmp = _mm_cmpeq_epi32(m_id128, other.m_id128);
int vmask = _mm_movemask_epi8(vcmp);
return vmask != 0xffff;
2015-07-06 01:33:06 +00:00
#else
return (m_id[0] != other.m_id[0]) || (m_id[1] != other.m_id[1]);
#endif
}
inline bool operator==(const UniqueID128& other) const
{
#if __SSE__
2015-07-06 02:07:57 +00:00
__m128i vcmp = _mm_cmpeq_epi32(m_id128, other.m_id128);
int vmask = _mm_movemask_epi8(vcmp);
return vmask == 0xffff;
2015-07-06 01:33:06 +00:00
#else
return (m_id[0] == other.m_id[0]) && (m_id[1] == other.m_id[1]);
#endif
}
2015-07-06 02:07:57 +00:00
inline uint64_t toHighUint64() const {return m_id[0];}
inline uint64_t toLowUint64() const {return m_id[1];}
2015-07-06 01:33:06 +00:00
inline std::string toString() const
{
char buf[33];
2015-07-06 02:07:57 +00:00
snprintf(buf, 33, "%16lX%16lX", m_id[0], m_id[1]);
2015-07-06 01:33:06 +00:00
return std::string(buf);
}
2015-06-24 20:56:23 +00:00
};
2015-07-14 00:38:48 +00:00
/* Case-insensitive comparator for std::map sorting */
2015-07-10 05:28:08 +00:00
struct CaseInsensitiveCompare
{
inline bool operator()(const std::string& lhs, const std::string& rhs) const
{
2015-07-14 00:38:48 +00:00
#if _WIN32
if (stricmp(lhs.c_str(), rhs.c_str()) < 0)
#else
if (strcasecmp(lhs.c_str(), rhs.c_str()) < 0)
#endif
2015-07-10 05:28:08 +00:00
return true;
return false;
}
};
2015-07-14 00:38:48 +00:00
/* PAK entry stream reader */
class PAKEntryReadStream : public Athena::io::IStreamReader
{
std::unique_ptr<atUint8[]> m_buf;
atUint64 m_sz;
atUint64 m_pos;
public:
PAKEntryReadStream(std::unique_ptr<atUint8[]>&& buf, atUint64 sz, atUint64 pos)
: m_buf(std::move(buf)), m_sz(sz), m_pos(pos)
{
if (m_pos >= m_sz)
2015-07-16 01:57:34 +00:00
LogDNACommon.report(LogVisor::FatalError, "PAK stream cursor overrun");
2015-07-14 00:38:48 +00:00
}
inline void seek(atInt64 pos, Athena::SeekOrigin origin)
{
if (origin == Athena::Begin)
m_pos = pos;
else if (origin == Athena::Current)
m_pos += pos;
else if (origin == Athena::End)
m_pos = m_sz + pos;
if (m_pos >= m_sz)
2015-07-16 01:57:34 +00:00
LogDNACommon.report(LogVisor::FatalError, "PAK stream cursor overrun");
2015-07-14 00:38:48 +00:00
}
inline atUint64 position() const {return m_pos;}
inline atUint64 length() const {return m_sz;}
inline atUint64 readUBytesToBuf(void* buf, atUint64 len)
{
atUint64 bufEnd = m_pos + len;
if (bufEnd > m_sz)
len -= bufEnd - m_sz;
memcpy(buf, m_buf.get() + m_pos, len);
m_pos += len;
return len;
}
};
2015-07-10 05:28:08 +00:00
/* Language-identifiers */
extern const HECL::FourCC ENGL;
extern const HECL::FourCC FREN;
extern const HECL::FourCC GERM;
extern const HECL::FourCC SPAN;
extern const HECL::FourCC ITAL;
extern const HECL::FourCC JAPN;
/* Resource types */
extern const HECL::FourCC AFSM;
extern const HECL::FourCC AGSC;
extern const HECL::FourCC ANCS;
extern const HECL::FourCC ANIM;
extern const HECL::FourCC ATBL;
extern const HECL::FourCC CINF;
extern const HECL::FourCC CMDL;
extern const HECL::FourCC CRSC;
extern const HECL::FourCC CSKR;
extern const HECL::FourCC CSMP;
extern const HECL::FourCC CSNG;
extern const HECL::FourCC CTWK;
extern const HECL::FourCC DGRP;
extern const HECL::FourCC DPSC;
extern const HECL::FourCC DUMB;
extern const HECL::FourCC ELSC;
extern const HECL::FourCC EVNT;
extern const HECL::FourCC FONT;
extern const HECL::FourCC FRME;
extern const HECL::FourCC HINT;
extern const HECL::FourCC MAPA;
extern const HECL::FourCC MAPU;
extern const HECL::FourCC MAPW;
extern const HECL::FourCC MLVL;
extern const HECL::FourCC MREA;
extern const HECL::FourCC PART;
extern const HECL::FourCC PATH;
extern const HECL::FourCC RFRM;
extern const HECL::FourCC ROOM;
extern const HECL::FourCC SAVW;
extern const HECL::FourCC SCAN;
extern const HECL::FourCC STRG;
extern const HECL::FourCC SWHC;
extern const HECL::FourCC TXTR;
extern const HECL::FourCC WPSC;
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<>
struct hash<Retro::FourCC>
{
inline size_t operator()(const Retro::FourCC& fcc) const
{return fcc.toUint32();}
};
2015-07-06 01:33:06 +00:00
template<>
struct hash<Retro::UniqueID32>
{
inline size_t operator()(const Retro::UniqueID32& id) const
2015-07-06 02:07:57 +00:00
{return id.toUint32();}
2015-07-06 01:33:06 +00:00
};
template<>
struct hash<Retro::UniqueID64>
{
inline size_t operator()(const Retro::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<>
struct hash<Retro::UniqueID128>
{
inline size_t operator()(const Retro::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
}
2015-06-24 20:56:23 +00:00
#endif // __DNA_COMMON_HPP__