2016-05-02 18:16:26 -07:00
|
|
|
#ifndef __AMUSE_COMMON_HPP__
|
|
|
|
#define __AMUSE_COMMON_HPP__
|
|
|
|
|
2016-05-07 22:19:27 -07:00
|
|
|
#include <algorithm>
|
2017-06-17 19:48:52 -07:00
|
|
|
#include <limits>
|
2017-12-28 23:57:22 -08:00
|
|
|
#include <cstdio>
|
|
|
|
#include <cstdint>
|
|
|
|
#include <cstdarg>
|
2016-06-12 22:47:07 -07:00
|
|
|
#include <string>
|
2017-11-12 22:15:33 -08:00
|
|
|
#include <string_view>
|
2016-06-16 23:10:15 -07:00
|
|
|
#include <cstring>
|
2018-07-28 20:37:06 -07:00
|
|
|
#include <atomic>
|
2018-07-13 23:06:33 -07:00
|
|
|
#include "athena/DNA.hpp"
|
2016-05-07 22:19:27 -07:00
|
|
|
|
2016-09-02 12:52:17 -07:00
|
|
|
#ifndef _WIN32
|
2016-05-29 11:45:31 -07:00
|
|
|
#include <strings.h>
|
2016-06-20 23:40:13 -07:00
|
|
|
#include <sys/stat.h>
|
2016-09-02 12:52:17 -07:00
|
|
|
#include <unistd.h>
|
|
|
|
#else
|
|
|
|
#ifndef WIN32_LEAN_AND_MEAN
|
|
|
|
#define WIN32_LEAN_AND_MEAN 1
|
|
|
|
#endif
|
|
|
|
#include <Windows.h>
|
2016-05-29 11:45:31 -07:00
|
|
|
#endif
|
|
|
|
|
2017-09-30 22:30:34 -07:00
|
|
|
#undef min
|
|
|
|
#undef max
|
|
|
|
|
2017-06-17 19:48:52 -07:00
|
|
|
constexpr float NativeSampleRate = 32000.0f;
|
|
|
|
|
2016-05-02 18:16:26 -07:00
|
|
|
namespace amuse
|
|
|
|
{
|
2018-07-13 23:06:33 -07:00
|
|
|
struct NameDB;
|
|
|
|
|
|
|
|
using BigDNA = athena::io::DNA<athena::Big>;
|
|
|
|
using LittleDNA = athena::io::DNA<athena::Little>;
|
|
|
|
using BigDNAV = athena::io::DNAVYaml<athena::Big>;
|
|
|
|
using LittleDNAV = athena::io::DNAVYaml<athena::Little>;
|
|
|
|
|
|
|
|
/** Common ID structure statically tagging
|
2018-07-14 23:10:50 -07:00
|
|
|
* SoundMacros, Tables, Keymaps, Layers, Samples, SFX, Songs */
|
2018-07-13 23:06:33 -07:00
|
|
|
struct ObjectId
|
|
|
|
{
|
|
|
|
uint16_t id = 0xffff;
|
|
|
|
operator uint16_t() const { return id; }
|
|
|
|
ObjectId() = default;
|
|
|
|
ObjectId(uint16_t idIn) : id(idIn) {}
|
|
|
|
ObjectId& operator=(uint16_t idIn) { id = idIn; return *this; }
|
|
|
|
static thread_local NameDB* CurNameDB;
|
|
|
|
};
|
|
|
|
template <athena::Endian DNAEn>
|
|
|
|
struct AT_SPECIALIZE_PARMS(athena::Endian::Big, athena::Endian::Little)
|
|
|
|
ObjectIdDNA : BigDNA
|
|
|
|
{
|
|
|
|
AT_DECL_EXPLICIT_DNA_YAML
|
|
|
|
void _read(athena::io::YAMLDocReader& r);
|
|
|
|
void _write(athena::io::YAMLDocWriter& w);
|
|
|
|
ObjectId id;
|
2018-07-14 23:10:50 -07:00
|
|
|
ObjectIdDNA() = default;
|
|
|
|
ObjectIdDNA(ObjectId idIn) : id(idIn) {}
|
|
|
|
operator ObjectId() const { return id; }
|
2018-07-13 23:06:33 -07:00
|
|
|
};
|
|
|
|
|
2018-07-14 23:10:50 -07:00
|
|
|
#define DECL_ID_TYPE(type) \
|
|
|
|
struct type : ObjectId \
|
|
|
|
{ \
|
|
|
|
using ObjectId::ObjectId; \
|
2018-08-19 13:05:39 -07:00
|
|
|
type() = default; \
|
2018-07-14 23:10:50 -07:00
|
|
|
type(const ObjectId& id) : ObjectId(id) {} \
|
|
|
|
static thread_local NameDB* CurNameDB; \
|
|
|
|
}; \
|
|
|
|
template <athena::Endian DNAEn> \
|
|
|
|
struct AT_SPECIALIZE_PARMS(athena::Endian::Big, athena::Endian::Little) \
|
|
|
|
type##DNA : BigDNA \
|
|
|
|
{ \
|
|
|
|
AT_DECL_EXPLICIT_DNA_YAML \
|
|
|
|
void _read(athena::io::YAMLDocReader& r); \
|
|
|
|
void _write(athena::io::YAMLDocWriter& w); \
|
|
|
|
type id; \
|
|
|
|
type##DNA() = default; \
|
|
|
|
type##DNA(type idIn) : id(idIn) {} \
|
|
|
|
operator type() const { return id; } \
|
2018-07-13 23:06:33 -07:00
|
|
|
};
|
2018-07-14 23:10:50 -07:00
|
|
|
DECL_ID_TYPE(SoundMacroId)
|
|
|
|
DECL_ID_TYPE(SampleId)
|
|
|
|
DECL_ID_TYPE(TableId)
|
|
|
|
DECL_ID_TYPE(KeymapId)
|
|
|
|
DECL_ID_TYPE(LayersId)
|
|
|
|
DECL_ID_TYPE(SongId)
|
|
|
|
DECL_ID_TYPE(SFXId)
|
2018-07-16 21:48:38 -07:00
|
|
|
DECL_ID_TYPE(GroupId)
|
2018-07-14 23:10:50 -07:00
|
|
|
|
|
|
|
/* MusyX has object polymorphism between Keymaps and Layers when
|
|
|
|
* referenced by a song group's page object. When the upper bit is set,
|
|
|
|
* this indicates a layer type. */
|
2018-07-13 23:06:33 -07:00
|
|
|
template <athena::Endian DNAEn>
|
|
|
|
struct AT_SPECIALIZE_PARMS(athena::Endian::Big, athena::Endian::Little)
|
2018-07-14 23:10:50 -07:00
|
|
|
PageObjectIdDNA : BigDNA
|
2018-07-13 23:06:33 -07:00
|
|
|
{
|
|
|
|
AT_DECL_EXPLICIT_DNA_YAML
|
|
|
|
void _read(athena::io::YAMLDocReader& r);
|
|
|
|
void _write(athena::io::YAMLDocWriter& w);
|
2018-07-14 23:10:50 -07:00
|
|
|
ObjectId id;
|
|
|
|
PageObjectIdDNA() = default;
|
|
|
|
PageObjectIdDNA(ObjectId idIn) : id(idIn) {}
|
|
|
|
operator ObjectId() const { return id; }
|
2018-07-13 23:06:33 -07:00
|
|
|
};
|
|
|
|
|
2018-07-27 21:34:29 -07:00
|
|
|
struct SoundMacroStep
|
|
|
|
{
|
|
|
|
uint16_t step = 0;
|
|
|
|
operator uint16_t() const { return step; }
|
|
|
|
SoundMacroStep() = default;
|
|
|
|
SoundMacroStep(uint16_t idIn) : step(idIn) {}
|
|
|
|
SoundMacroStep& operator=(uint16_t idIn) { step = idIn; return *this; }
|
|
|
|
};
|
|
|
|
|
|
|
|
template <athena::Endian DNAEn>
|
|
|
|
struct AT_SPECIALIZE_PARMS(athena::Endian::Big, athena::Endian::Little)
|
|
|
|
SoundMacroStepDNA : BigDNA
|
|
|
|
{
|
|
|
|
AT_DECL_EXPLICIT_DNA_YAML
|
|
|
|
SoundMacroStep step;
|
|
|
|
SoundMacroStepDNA() = default;
|
|
|
|
SoundMacroStepDNA(SoundMacroStep idIn) : step(idIn) {}
|
|
|
|
operator SoundMacroStep() const { return step; }
|
|
|
|
};
|
|
|
|
|
2018-07-13 23:06:33 -07:00
|
|
|
struct LittleUInt24 : LittleDNA
|
|
|
|
{
|
|
|
|
AT_DECL_EXPLICIT_DNA_YAML
|
|
|
|
atUint32 val;
|
|
|
|
operator uint32_t() const { return val; }
|
|
|
|
LittleUInt24() = default;
|
|
|
|
LittleUInt24(uint32_t valIn) : val(valIn) {}
|
|
|
|
LittleUInt24& operator=(uint32_t valIn) { val = valIn; return *this; }
|
|
|
|
};
|
2016-05-02 18:16:26 -07:00
|
|
|
|
2018-07-28 20:37:06 -07:00
|
|
|
class IObj
|
|
|
|
{
|
|
|
|
std::atomic_int m_refCount = {0};
|
|
|
|
protected:
|
|
|
|
virtual ~IObj() = default;
|
|
|
|
public:
|
|
|
|
void increment() { m_refCount++; }
|
|
|
|
void decrement()
|
|
|
|
{
|
|
|
|
if (m_refCount.fetch_sub(1) == 1)
|
|
|
|
delete this;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template<class SubCls>
|
2018-07-29 23:20:03 -07:00
|
|
|
class ObjWrapper : public IObj
|
2018-07-28 20:37:06 -07:00
|
|
|
{
|
|
|
|
SubCls m_obj;
|
|
|
|
public:
|
|
|
|
template <class... _Args>
|
|
|
|
ObjWrapper(_Args&&... args) : m_obj(std::forward<_Args>(args)...) {}
|
2018-07-29 23:20:03 -07:00
|
|
|
SubCls* get() { return &m_obj; }
|
|
|
|
const SubCls* get() const { return &m_obj; }
|
2018-07-28 20:37:06 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
template<class SubCls>
|
2018-07-29 23:20:03 -07:00
|
|
|
class ObjTokenBase
|
2018-07-28 20:37:06 -07:00
|
|
|
{
|
2018-07-29 23:20:03 -07:00
|
|
|
protected:
|
|
|
|
IObj* m_obj = nullptr;
|
|
|
|
ObjTokenBase(IObj* obj) : m_obj(obj) { if (m_obj) m_obj->increment(); }
|
2018-07-28 20:37:06 -07:00
|
|
|
public:
|
2018-07-29 23:20:03 -07:00
|
|
|
ObjTokenBase() = default;
|
|
|
|
ObjTokenBase(const ObjTokenBase& other) : m_obj(other.m_obj) { if (m_obj) m_obj->increment(); }
|
|
|
|
ObjTokenBase(ObjTokenBase&& other) : m_obj(other.m_obj) { other.m_obj = nullptr; }
|
|
|
|
ObjTokenBase& operator=(const ObjTokenBase& other)
|
2018-07-28 20:37:06 -07:00
|
|
|
{ if (m_obj) m_obj->decrement(); m_obj = other.m_obj; if (m_obj) m_obj->increment(); return *this; }
|
2018-07-29 23:20:03 -07:00
|
|
|
ObjTokenBase& operator=(ObjTokenBase&& other)
|
2018-07-28 20:37:06 -07:00
|
|
|
{ if (m_obj) m_obj->decrement(); m_obj = other.m_obj; other.m_obj = nullptr; return *this; }
|
2018-07-29 23:20:03 -07:00
|
|
|
~ObjTokenBase() { if (m_obj) m_obj->decrement(); }
|
2018-07-28 20:37:06 -07:00
|
|
|
operator bool() const { return m_obj != nullptr; }
|
|
|
|
void reset() { if (m_obj) m_obj->decrement(); m_obj = nullptr; }
|
|
|
|
};
|
|
|
|
|
2018-07-29 23:20:03 -07:00
|
|
|
template<class SubCls, class Enable = void>
|
|
|
|
class ObjToken : public ObjTokenBase<SubCls>
|
|
|
|
{
|
|
|
|
IObj*& _obj() { return ObjTokenBase<SubCls>::m_obj; }
|
|
|
|
IObj*const& _obj() const { return ObjTokenBase<SubCls>::m_obj; }
|
|
|
|
public:
|
|
|
|
using ObjTokenBase<SubCls>::ObjTokenBase;
|
2018-08-19 13:05:39 -07:00
|
|
|
ObjToken() = default;
|
2018-07-29 23:20:03 -07:00
|
|
|
ObjToken(ObjWrapper<SubCls>* obj) : ObjTokenBase<SubCls>(obj) {}
|
|
|
|
ObjToken& operator=(ObjWrapper<SubCls>* obj)
|
|
|
|
{ if (_obj()) _obj()->decrement(); _obj() = obj; if (_obj()) _obj()->increment(); return *this; }
|
|
|
|
SubCls* get() const { return static_cast<ObjWrapper<SubCls>*>(_obj())->get(); }
|
|
|
|
SubCls* operator->() const { return get(); }
|
|
|
|
SubCls& operator*() const { return *get(); }
|
|
|
|
};
|
|
|
|
|
|
|
|
template<class SubCls>
|
|
|
|
class ObjToken<SubCls, typename std::enable_if_t<std::is_base_of_v<IObj, SubCls>>> : public ObjTokenBase<SubCls>
|
|
|
|
{
|
|
|
|
IObj*& _obj() { return ObjTokenBase<SubCls>::m_obj; }
|
|
|
|
IObj*const& _obj() const { return ObjTokenBase<SubCls>::m_obj; }
|
|
|
|
public:
|
|
|
|
using ObjTokenBase<SubCls>::ObjTokenBase;
|
2018-08-19 13:05:39 -07:00
|
|
|
ObjToken() = default;
|
2018-07-29 23:20:03 -07:00
|
|
|
ObjToken(IObj* obj) : ObjTokenBase<SubCls>(obj) {}
|
|
|
|
ObjToken& operator=(IObj* obj)
|
|
|
|
{ if (_obj()) _obj()->decrement(); _obj() = obj; if (_obj()) _obj()->increment(); return *this; }
|
|
|
|
SubCls* get() const { return static_cast<SubCls*>(_obj()); }
|
|
|
|
SubCls* operator->() const { return get(); }
|
|
|
|
SubCls& operator*() const { return *get(); }
|
|
|
|
template <class T>
|
|
|
|
T* cast() const { return static_cast<T*>(_obj()); }
|
|
|
|
};
|
|
|
|
|
|
|
|
/* ONLY USE WITH CLASSES DERIVED FROM IOBJ!
|
|
|
|
* Bypasses type_traits tests for incomplete type definitions. */
|
|
|
|
template<class SubCls>
|
|
|
|
class IObjToken : public ObjTokenBase<SubCls>
|
|
|
|
{
|
|
|
|
IObj*& _obj() { return ObjTokenBase<SubCls>::m_obj; }
|
|
|
|
IObj*const& _obj() const { return ObjTokenBase<SubCls>::m_obj; }
|
|
|
|
public:
|
|
|
|
using ObjTokenBase<SubCls>::ObjTokenBase;
|
2018-08-19 13:05:39 -07:00
|
|
|
IObjToken() = default;
|
2018-07-29 23:20:03 -07:00
|
|
|
IObjToken(IObj* obj) : ObjTokenBase<SubCls>(obj) {}
|
|
|
|
IObjToken& operator=(IObj* obj)
|
|
|
|
{ if (_obj()) _obj()->decrement(); _obj() = obj; if (_obj()) _obj()->increment(); return *this; }
|
|
|
|
SubCls* get() const { return static_cast<SubCls*>(_obj()); }
|
|
|
|
SubCls* operator->() const { return get(); }
|
|
|
|
SubCls& operator*() const { return *get(); }
|
|
|
|
template <class T>
|
|
|
|
T* cast() const { return static_cast<T*>(_obj()); }
|
|
|
|
};
|
|
|
|
|
|
|
|
template <class Tp, class... _Args>
|
|
|
|
inline typename std::enable_if_t<std::is_base_of_v<IObj, Tp>, ObjToken<Tp>> MakeObj(_Args&&... args)
|
|
|
|
{
|
|
|
|
return new Tp(std::forward<_Args>(args)...);
|
|
|
|
}
|
|
|
|
|
2018-07-28 20:37:06 -07:00
|
|
|
template <class Tp, class... _Args>
|
2018-07-29 23:20:03 -07:00
|
|
|
inline typename std::enable_if_t<!std::is_base_of_v<IObj, Tp>, ObjToken<Tp>> MakeObj(_Args&&... args)
|
2018-07-28 20:37:06 -07:00
|
|
|
{
|
|
|
|
return new ObjWrapper<Tp>(std::forward<_Args>(args)...);
|
|
|
|
}
|
|
|
|
|
2016-05-09 22:32:31 -07:00
|
|
|
#ifndef PRISize
|
|
|
|
#ifdef _MSC_VER
|
|
|
|
#define PRISize "Iu"
|
|
|
|
#else
|
|
|
|
#define PRISize "zu"
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
2016-06-12 22:47:07 -07:00
|
|
|
#ifdef _WIN32
|
2016-07-13 21:54:46 -07:00
|
|
|
using SystemString = std::wstring;
|
2017-11-13 19:35:02 -08:00
|
|
|
using SystemStringView = std::wstring_view;
|
2016-07-13 21:54:46 -07:00
|
|
|
using SystemChar = wchar_t;
|
|
|
|
#ifndef _S
|
|
|
|
#define _S(val) L##val
|
|
|
|
#endif
|
|
|
|
typedef struct _stat Sstat;
|
|
|
|
static inline int Mkdir(const wchar_t* path, int) { return _wmkdir(path); }
|
|
|
|
static inline int Stat(const wchar_t* path, Sstat* statout) { return _wstat(path, statout); }
|
2016-06-12 22:47:07 -07:00
|
|
|
#else
|
2016-07-13 21:54:46 -07:00
|
|
|
using SystemString = std::string;
|
2017-11-12 22:15:33 -08:00
|
|
|
using SystemStringView = std::string_view;
|
2016-07-13 21:54:46 -07:00
|
|
|
using SystemChar = char;
|
|
|
|
#ifndef _S
|
|
|
|
#define _S(val) val
|
|
|
|
#endif
|
|
|
|
typedef struct stat Sstat;
|
|
|
|
static inline int Mkdir(const char* path, mode_t mode) { return mkdir(path, mode); }
|
|
|
|
static inline int Stat(const char* path, Sstat* statout) { return stat(path, statout); }
|
2016-06-12 22:47:07 -07:00
|
|
|
#endif
|
|
|
|
|
2016-06-15 20:30:04 -07:00
|
|
|
#if _WIN32
|
2016-07-13 21:54:46 -07:00
|
|
|
static inline int CompareCaseInsensitive(const char* a, const char* b) { return _stricmp(a, b); }
|
2016-06-15 20:30:04 -07:00
|
|
|
#endif
|
2016-05-26 17:56:18 -07:00
|
|
|
|
2016-06-12 22:47:07 -07:00
|
|
|
static inline int CompareCaseInsensitive(const SystemChar* a, const SystemChar* b)
|
|
|
|
{
|
|
|
|
#if _WIN32
|
|
|
|
return _wcsicmp(a, b);
|
|
|
|
#else
|
|
|
|
return strcasecmp(a, b);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2016-05-07 22:19:27 -07:00
|
|
|
template <typename T>
|
2016-07-13 21:54:46 -07:00
|
|
|
static inline T clamp(T a, T val, T b)
|
|
|
|
{
|
|
|
|
return std::max<T>(a, std::min<T>(b, val));
|
|
|
|
}
|
2016-05-07 22:19:27 -07:00
|
|
|
|
2016-05-08 15:38:51 -07:00
|
|
|
template <typename T>
|
2017-06-17 19:48:52 -07:00
|
|
|
inline T ClampFull(float in)
|
|
|
|
{
|
2017-12-02 22:09:34 -08:00
|
|
|
if (std::is_floating_point<T>())
|
2017-06-17 19:48:52 -07:00
|
|
|
{
|
2017-12-02 22:09:34 -08:00
|
|
|
return std::min<T>(std::max<T>(in, -1.f), 1.f);
|
2017-06-17 19:48:52 -07:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
constexpr T MAX = std::numeric_limits<T>::max();
|
|
|
|
constexpr T MIN = std::numeric_limits<T>::min();
|
|
|
|
|
2017-12-02 22:09:34 -08:00
|
|
|
if (in < MIN)
|
2017-06-17 19:48:52 -07:00
|
|
|
return MIN;
|
2017-12-02 22:09:34 -08:00
|
|
|
else if (in > MAX)
|
2017-06-17 19:48:52 -07:00
|
|
|
return MAX;
|
|
|
|
else
|
|
|
|
return in;
|
|
|
|
}
|
2016-05-08 15:38:51 -07:00
|
|
|
}
|
|
|
|
|
2016-05-06 14:22:39 -07:00
|
|
|
#ifndef M_PIF
|
|
|
|
#define M_PIF 3.14159265358979323846f /* pi */
|
|
|
|
#endif
|
|
|
|
|
2016-06-12 22:47:07 -07:00
|
|
|
#if __GNUC__
|
2016-07-13 21:54:46 -07:00
|
|
|
__attribute__((__format__(__printf__, 1, 2)))
|
2016-06-12 22:47:07 -07:00
|
|
|
#endif
|
2016-07-13 21:54:46 -07:00
|
|
|
static inline void
|
|
|
|
Printf(const SystemChar* fmt, ...)
|
2016-06-12 22:47:07 -07:00
|
|
|
{
|
|
|
|
va_list args;
|
|
|
|
va_start(args, fmt);
|
|
|
|
#if _WIN32
|
|
|
|
vwprintf(fmt, args);
|
|
|
|
#else
|
|
|
|
vprintf(fmt, args);
|
|
|
|
#endif
|
|
|
|
va_end(args);
|
|
|
|
}
|
|
|
|
|
|
|
|
#if __GNUC__
|
2016-07-13 21:54:46 -07:00
|
|
|
__attribute__((__format__(__printf__, 3, 4)))
|
2016-06-12 22:47:07 -07:00
|
|
|
#endif
|
2016-07-13 21:54:46 -07:00
|
|
|
static inline void
|
|
|
|
SNPrintf(SystemChar* str, size_t maxlen, const SystemChar* format, ...)
|
2016-06-12 22:47:07 -07:00
|
|
|
{
|
|
|
|
va_list va;
|
|
|
|
va_start(va, format);
|
|
|
|
#if _WIN32
|
|
|
|
_vsnwprintf(str, maxlen, format, va);
|
|
|
|
#else
|
|
|
|
vsnprintf(str, maxlen, format, va);
|
|
|
|
#endif
|
|
|
|
va_end(va);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline const SystemChar* StrRChr(const SystemChar* str, SystemChar ch)
|
|
|
|
{
|
|
|
|
#if _WIN32
|
|
|
|
return wcsrchr(str, ch);
|
|
|
|
#else
|
|
|
|
return strrchr(str, ch);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline SystemChar* StrRChr(SystemChar* str, SystemChar ch)
|
|
|
|
{
|
|
|
|
#if _WIN32
|
|
|
|
return wcsrchr(str, ch);
|
|
|
|
#else
|
|
|
|
return strrchr(str, ch);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2016-05-26 17:56:18 -07:00
|
|
|
static inline int FSeek(FILE* fp, int64_t offset, int whence)
|
|
|
|
{
|
|
|
|
#if _WIN32
|
|
|
|
return _fseeki64(fp, offset, whence);
|
|
|
|
#elif __APPLE__ || __FreeBSD__
|
|
|
|
return fseeko(fp, offset, whence);
|
|
|
|
#else
|
|
|
|
return fseeko64(fp, offset, whence);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline int64_t FTell(FILE* fp)
|
|
|
|
{
|
|
|
|
#if _WIN32
|
|
|
|
return _ftelli64(fp);
|
|
|
|
#elif __APPLE__ || __FreeBSD__
|
|
|
|
return ftello(fp);
|
|
|
|
#else
|
|
|
|
return ftello64(fp);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2016-06-12 22:47:07 -07:00
|
|
|
static inline FILE* FOpen(const SystemChar* path, const SystemChar* mode)
|
|
|
|
{
|
|
|
|
#if _WIN32
|
|
|
|
FILE* fp = _wfopen(path, mode);
|
|
|
|
if (!fp)
|
|
|
|
return nullptr;
|
|
|
|
#else
|
|
|
|
FILE* fp = fopen(path, mode);
|
|
|
|
if (!fp)
|
|
|
|
return nullptr;
|
|
|
|
#endif
|
|
|
|
return fp;
|
|
|
|
}
|
|
|
|
|
2016-09-02 12:52:17 -07:00
|
|
|
static inline void Unlink(const SystemChar* file)
|
|
|
|
{
|
|
|
|
#if _WIN32
|
|
|
|
_wunlink(file);
|
|
|
|
#else
|
|
|
|
unlink(file);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2016-05-02 18:16:26 -07:00
|
|
|
#undef bswap16
|
|
|
|
#undef bswap32
|
|
|
|
#undef bswap64
|
|
|
|
|
|
|
|
/* Type-sensitive byte swappers */
|
|
|
|
template <typename T>
|
|
|
|
static inline T bswap16(T val)
|
|
|
|
{
|
|
|
|
#if __GNUC__
|
|
|
|
return __builtin_bswap16(val);
|
|
|
|
#elif _WIN32
|
|
|
|
return _byteswap_ushort(val);
|
|
|
|
#else
|
|
|
|
return (val = (val << 8) | ((val >> 8) & 0xFF));
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
static inline T bswap32(T val)
|
|
|
|
{
|
|
|
|
#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>
|
|
|
|
static inline T bswap64(T val)
|
|
|
|
{
|
|
|
|
#if __GNUC__
|
|
|
|
return __builtin_bswap64(val);
|
|
|
|
#elif _WIN32
|
|
|
|
return _byteswap_uint64(val);
|
|
|
|
#else
|
2016-07-13 21:54:46 -07:00
|
|
|
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);
|
2016-05-02 18:16:26 -07:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
|
2016-07-13 21:54:46 -07:00
|
|
|
static inline int16_t SBig(int16_t val) { return bswap16(val); }
|
|
|
|
static inline uint16_t SBig(uint16_t val) { return bswap16(val); }
|
|
|
|
static inline int32_t SBig(int32_t val) { return bswap32(val); }
|
|
|
|
static inline uint32_t SBig(uint32_t val) { return bswap32(val); }
|
|
|
|
static inline int64_t SBig(int64_t val) { return bswap64(val); }
|
|
|
|
static inline uint64_t SBig(uint64_t val) { return bswap64(val); }
|
2016-05-02 18:16:26 -07:00
|
|
|
static inline float SBig(float val)
|
|
|
|
{
|
|
|
|
int32_t ival = bswap32(*((int32_t*)(&val)));
|
|
|
|
return *((float*)(&ival));
|
|
|
|
}
|
|
|
|
static inline double SBig(double val)
|
|
|
|
{
|
|
|
|
int64_t ival = bswap64(*((int64_t*)(&val)));
|
|
|
|
return *((double*)(&ival));
|
|
|
|
}
|
|
|
|
#ifndef SBIG
|
2016-07-13 21:54:46 -07:00
|
|
|
#define SBIG(q) (((q)&0x000000FF) << 24 | ((q)&0x0000FF00) << 8 | ((q)&0x00FF0000) >> 8 | ((q)&0xFF000000) >> 24)
|
2016-05-02 18:16:26 -07:00
|
|
|
#endif
|
|
|
|
|
2016-07-13 21:54:46 -07:00
|
|
|
static inline int16_t SLittle(int16_t val) { return val; }
|
|
|
|
static inline uint16_t SLittle(uint16_t val) { return val; }
|
|
|
|
static inline int32_t SLittle(int32_t val) { return val; }
|
|
|
|
static inline uint32_t SLittle(uint32_t val) { return val; }
|
|
|
|
static inline int64_t SLittle(int64_t val) { return val; }
|
|
|
|
static inline uint64_t SLittle(uint64_t val) { return val; }
|
|
|
|
static inline float SLittle(float val) { return val; }
|
|
|
|
static inline double SLittle(double val) { return val; }
|
2016-05-02 18:16:26 -07:00
|
|
|
#ifndef SLITTLE
|
|
|
|
#define SLITTLE(q) (q)
|
|
|
|
#endif
|
|
|
|
#else
|
2016-07-13 21:54:46 -07:00
|
|
|
static inline int16_t SLittle(int16_t val) { return bswap16(val); }
|
|
|
|
static inline uint16_t SLittle(uint16_t val) { return bswap16(val); }
|
|
|
|
static inline int32_t SLittle(int32_t val) { return bswap32(val); }
|
|
|
|
static inline uint32_t SLittle(uint32_t val) { return bswap32(val); }
|
|
|
|
static inline int64_t SLittle(int64_t val) { return bswap64(val); }
|
|
|
|
static inline uint64_t SLittle(uint64_t val) { return bswap64(val); }
|
2016-05-02 18:16:26 -07:00
|
|
|
static inline float SLittle(float val)
|
|
|
|
{
|
|
|
|
int32_t ival = bswap32(*((int32_t*)(&val)));
|
|
|
|
return *((float*)(&ival));
|
|
|
|
}
|
|
|
|
static inline double SLittle(double val)
|
|
|
|
{
|
|
|
|
int64_t ival = bswap64(*((int64_t*)(&val)));
|
|
|
|
return *((double*)(&ival));
|
|
|
|
}
|
|
|
|
#ifndef SLITTLE
|
2016-07-13 21:54:46 -07:00
|
|
|
#define SLITTLE(q) (((q)&0x000000FF) << 24 | ((q)&0x0000FF00) << 8 | ((q)&0x00FF0000) >> 8 | ((q)&0xFF000000) >> 24)
|
2016-05-02 18:16:26 -07:00
|
|
|
#endif
|
|
|
|
|
2016-07-13 21:54:46 -07:00
|
|
|
static inline int16_t SBig(int16_t val) { return val; }
|
|
|
|
static inline uint16_t SBig(uint16_t val) { return val; }
|
|
|
|
static inline int32_t SBig(int32_t val) { return val; }
|
|
|
|
static inline uint32_t SBig(uint32_t val) { return val; }
|
|
|
|
static inline int64_t SBig(int64_t val) { return val; }
|
|
|
|
static inline uint64_t SBig(uint64_t val) { return val; }
|
|
|
|
static inline float SBig(float val) { return val; }
|
|
|
|
static inline double SBig(double val) { return val; }
|
2016-05-02 18:16:26 -07:00
|
|
|
#ifndef SBIG
|
|
|
|
#define SBIG(q) (q)
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
2016-05-27 19:28:59 -07:00
|
|
|
/** Versioned data format to interpret */
|
|
|
|
enum class DataFormat
|
|
|
|
{
|
|
|
|
GCN,
|
|
|
|
N64,
|
|
|
|
PC
|
|
|
|
};
|
|
|
|
|
|
|
|
/** Meta-type for selecting GameCube (MusyX 2.0) data formats */
|
2016-07-13 21:54:46 -07:00
|
|
|
struct GCNDataTag
|
|
|
|
{
|
|
|
|
};
|
2016-05-27 19:28:59 -07:00
|
|
|
|
|
|
|
/** Meta-type for selecting N64 (MusyX 1.0) data formats */
|
2016-07-13 21:54:46 -07:00
|
|
|
struct N64DataTag
|
|
|
|
{
|
|
|
|
};
|
2016-05-27 19:28:59 -07:00
|
|
|
|
|
|
|
/** Meta-type for selecting PC (MusyX 1.0) data formats */
|
2016-07-13 21:54:46 -07:00
|
|
|
struct PCDataTag
|
|
|
|
{
|
|
|
|
};
|
2018-07-14 23:10:50 -07:00
|
|
|
|
2018-07-16 21:48:38 -07:00
|
|
|
template <class T>
|
|
|
|
static std::vector<std::pair<typename T::key_type,
|
|
|
|
std::reference_wrapper<typename T::mapped_type>>> SortUnorderedMap(T& um)
|
|
|
|
{
|
|
|
|
std::vector<std::pair<typename T::key_type, std::reference_wrapper<typename T::mapped_type>>> ret;
|
|
|
|
ret.reserve(um.size());
|
|
|
|
for (auto& p : um)
|
|
|
|
ret.emplace_back(p.first, p.second);
|
|
|
|
std::sort(ret.begin(), ret.end(), [](const auto& a, const auto& b) { return a.first < b.first; });
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2018-07-14 23:10:50 -07:00
|
|
|
template <class T>
|
|
|
|
static std::vector<std::pair<typename T::key_type,
|
|
|
|
std::reference_wrapper<const typename T::mapped_type>>> SortUnorderedMap(const T& um)
|
|
|
|
{
|
2018-07-16 21:48:38 -07:00
|
|
|
std::vector<std::pair<typename T::key_type, std::reference_wrapper<const typename T::mapped_type>>> ret;
|
|
|
|
ret.reserve(um.size());
|
|
|
|
for (const auto& p : um)
|
|
|
|
ret.emplace_back(p.first, p.second);
|
2018-07-14 23:10:50 -07:00
|
|
|
std::sort(ret.begin(), ret.end(), [](const auto& a, const auto& b) { return a.first < b.first; });
|
|
|
|
return ret;
|
|
|
|
}
|
2018-08-18 17:28:52 -07:00
|
|
|
|
|
|
|
template <class T>
|
|
|
|
static std::vector<typename T::key_type> SortUnorderedSet(T& us)
|
|
|
|
{
|
|
|
|
std::vector<typename T::key_type> ret;
|
|
|
|
ret.reserve(us.size());
|
|
|
|
for (auto& p : us)
|
|
|
|
ret.emplace_back(p);
|
|
|
|
std::sort(ret.begin(), ret.end());
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class T>
|
|
|
|
static std::vector<typename T::key_type> SortUnorderedSet(const T& us)
|
|
|
|
{
|
|
|
|
std::vector<typename T::key_type> ret;
|
|
|
|
ret.reserve(us.size());
|
|
|
|
for (const auto& p : us)
|
|
|
|
ret.emplace_back(p);
|
|
|
|
std::sort(ret.begin(), ret.end());
|
|
|
|
return ret;
|
|
|
|
}
|
2016-05-02 18:16:26 -07:00
|
|
|
}
|
|
|
|
|
2018-07-13 23:06:33 -07:00
|
|
|
namespace std
|
|
|
|
{
|
2018-07-14 23:10:50 -07:00
|
|
|
#define DECL_ID_HASH(type) \
|
|
|
|
template<> \
|
|
|
|
struct hash<amuse::type> \
|
|
|
|
{ \
|
|
|
|
size_t operator()(const amuse::type& val) const noexcept { return val.id; } \
|
2018-07-13 23:06:33 -07:00
|
|
|
};
|
2018-07-14 23:10:50 -07:00
|
|
|
DECL_ID_HASH(ObjectId)
|
|
|
|
DECL_ID_HASH(SoundMacroId)
|
|
|
|
DECL_ID_HASH(SampleId)
|
|
|
|
DECL_ID_HASH(TableId)
|
|
|
|
DECL_ID_HASH(KeymapId)
|
|
|
|
DECL_ID_HASH(LayersId)
|
|
|
|
DECL_ID_HASH(SongId)
|
|
|
|
DECL_ID_HASH(SFXId)
|
2018-07-16 21:48:38 -07:00
|
|
|
DECL_ID_HASH(GroupId)
|
2018-07-28 20:37:06 -07:00
|
|
|
|
|
|
|
template<class T>
|
|
|
|
struct hash<amuse::ObjToken<T>>
|
|
|
|
{
|
|
|
|
size_t operator()(const amuse::ObjToken<T>& val) const noexcept { return reinterpret_cast<size_t>(val.get()); }
|
|
|
|
};
|
2018-07-13 23:06:33 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
namespace amuse
|
|
|
|
{
|
|
|
|
struct NameDB
|
|
|
|
{
|
|
|
|
enum class Type
|
|
|
|
{
|
2018-07-14 23:10:50 -07:00
|
|
|
SoundMacro,
|
|
|
|
Table,
|
|
|
|
Keymap,
|
|
|
|
Layer,
|
|
|
|
Song,
|
|
|
|
SFX,
|
2018-07-16 21:48:38 -07:00
|
|
|
Group,
|
2018-07-14 23:10:50 -07:00
|
|
|
Sample
|
2018-07-13 23:06:33 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
std::unordered_map<std::string, ObjectId> m_stringToId;
|
|
|
|
std::unordered_map<ObjectId, std::string> m_idToString;
|
|
|
|
|
2018-07-16 00:41:15 -07:00
|
|
|
ObjectId generateId(Type tp) const;
|
2018-07-14 23:10:50 -07:00
|
|
|
static std::string generateName(ObjectId id, Type tp);
|
2018-08-09 23:19:23 -07:00
|
|
|
std::string generateDefaultName(Type tp) const;
|
2018-07-13 23:06:33 -07:00
|
|
|
std::string_view registerPair(std::string_view str, ObjectId id);
|
|
|
|
std::string_view resolveNameFromId(ObjectId id) const;
|
|
|
|
ObjectId resolveIdFromName(std::string_view str) const;
|
2018-08-09 00:42:17 -07:00
|
|
|
void remove(ObjectId id);
|
|
|
|
void rename(ObjectId id, std::string_view str);
|
2018-07-13 23:06:33 -07:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2016-05-02 18:16:26 -07:00
|
|
|
#endif // __AMUSE_COMMON_HPP__
|