2013-02-24 13:51:56 -08:00
|
|
|
#ifndef OPENMW_COMPONENTS_NIF_RECORDPTR_HPP
|
|
|
|
#define OPENMW_COMPONENTS_NIF_RECORDPTR_HPP
|
2010-01-06 12:28:37 +01:00
|
|
|
|
2013-02-24 13:51:56 -08:00
|
|
|
#include "niffile.hpp"
|
2014-08-24 13:27:09 -04:00
|
|
|
#include "nifstream.hpp"
|
2010-01-06 15:00:08 +01:00
|
|
|
#include <vector>
|
2010-01-06 12:28:37 +01:00
|
|
|
|
|
|
|
namespace Nif
|
|
|
|
{
|
2010-01-06 15:00:08 +01:00
|
|
|
|
|
|
|
/** A reference to another record. It is read as an index from the
|
|
|
|
NIF, and later looked up in the index table to get an actual
|
|
|
|
pointer.
|
|
|
|
*/
|
2010-01-06 12:28:37 +01:00
|
|
|
template <class X>
|
|
|
|
class RecordPtrT
|
|
|
|
{
|
2012-07-02 21:41:21 -07:00
|
|
|
union {
|
|
|
|
intptr_t index;
|
|
|
|
X* ptr;
|
|
|
|
};
|
2010-01-06 12:28:37 +01:00
|
|
|
|
2012-07-02 21:41:21 -07:00
|
|
|
public:
|
|
|
|
RecordPtrT() : index(-2) {}
|
2010-01-06 15:00:08 +01:00
|
|
|
|
2018-07-08 22:22:34 +03:00
|
|
|
RecordPtrT(X* ptr) : ptr(ptr) {}
|
|
|
|
|
2012-07-02 21:41:21 -07:00
|
|
|
/// Read the index from the nif
|
2013-01-05 13:03:05 -08:00
|
|
|
void read(NIFStream *nif)
|
2012-07-02 21:41:21 -07:00
|
|
|
{
|
|
|
|
// Can only read the index once
|
|
|
|
assert(index == -2);
|
2010-01-06 12:28:37 +01:00
|
|
|
|
2012-07-02 21:41:21 -07:00
|
|
|
// Store the index for later
|
|
|
|
index = nif->getInt();
|
2012-07-03 18:37:04 -07:00
|
|
|
assert(index >= -1);
|
2012-07-02 21:41:21 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Resolve index to pointer
|
|
|
|
void post(NIFFile *nif)
|
|
|
|
{
|
|
|
|
if(index < 0)
|
2018-10-09 10:21:12 +04:00
|
|
|
ptr = nullptr;
|
2012-07-02 21:41:21 -07:00
|
|
|
else
|
|
|
|
{
|
|
|
|
Record *r = nif->getRecord(index);
|
|
|
|
// And cast it
|
|
|
|
ptr = dynamic_cast<X*>(r);
|
2018-10-09 10:21:12 +04:00
|
|
|
assert(ptr != nullptr);
|
2012-07-02 21:41:21 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Look up the actual object from the index
|
2013-07-24 23:58:35 -07:00
|
|
|
const X* getPtr() const
|
2012-07-02 21:41:21 -07:00
|
|
|
{
|
2018-10-09 10:21:12 +04:00
|
|
|
assert(ptr != nullptr);
|
2012-07-02 21:41:21 -07:00
|
|
|
return ptr;
|
|
|
|
}
|
2013-07-24 23:58:35 -07:00
|
|
|
X* getPtr()
|
|
|
|
{
|
2018-10-09 10:21:12 +04:00
|
|
|
assert(ptr != nullptr);
|
2013-07-24 23:58:35 -07:00
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
const X& get() const
|
|
|
|
{ return *getPtr(); }
|
|
|
|
X& get()
|
2012-07-11 06:37:17 -07:00
|
|
|
{ return *getPtr(); }
|
2012-07-02 21:41:21 -07:00
|
|
|
|
|
|
|
/// Syntactic sugar
|
2013-07-24 23:58:35 -07:00
|
|
|
const X* operator->() const
|
|
|
|
{ return getPtr(); }
|
|
|
|
X* operator->()
|
2012-07-11 06:37:17 -07:00
|
|
|
{ return getPtr(); }
|
2012-07-02 21:41:21 -07:00
|
|
|
|
|
|
|
/// Pointers are allowed to be empty
|
2012-07-11 06:37:17 -07:00
|
|
|
bool empty() const
|
2018-10-09 10:21:12 +04:00
|
|
|
{ return ptr == nullptr; }
|
2010-01-06 12:28:37 +01:00
|
|
|
};
|
|
|
|
|
2010-01-06 15:00:08 +01:00
|
|
|
/** A list of references to other records. These are read as a list,
|
|
|
|
and later converted to pointers as needed. Not an optimized
|
|
|
|
implementation.
|
|
|
|
*/
|
|
|
|
template <class X>
|
|
|
|
class RecordListT
|
|
|
|
{
|
2012-07-02 21:41:21 -07:00
|
|
|
typedef RecordPtrT<X> Ptr;
|
|
|
|
std::vector<Ptr> list;
|
2010-01-06 15:00:08 +01:00
|
|
|
|
2012-07-02 21:41:21 -07:00
|
|
|
public:
|
2018-07-08 22:22:34 +03:00
|
|
|
RecordListT() = default;
|
|
|
|
|
|
|
|
RecordListT(std::vector<Ptr> list)
|
|
|
|
: list(std::move(list))
|
|
|
|
{}
|
|
|
|
|
2013-01-05 13:03:05 -08:00
|
|
|
void read(NIFStream *nif)
|
2012-07-02 21:41:21 -07:00
|
|
|
{
|
|
|
|
int len = nif->getInt();
|
|
|
|
list.resize(len);
|
2010-01-06 15:00:08 +01:00
|
|
|
|
2012-07-02 21:41:21 -07:00
|
|
|
for(size_t i=0;i < list.size();i++)
|
|
|
|
list[i].read(nif);
|
|
|
|
}
|
2010-01-06 15:00:08 +01:00
|
|
|
|
2012-07-02 21:41:21 -07:00
|
|
|
void post(NIFFile *nif)
|
2010-01-06 15:00:08 +01:00
|
|
|
{
|
2012-07-02 21:41:21 -07:00
|
|
|
for(size_t i=0;i < list.size();i++)
|
|
|
|
list[i].post(nif);
|
2010-01-06 15:00:08 +01:00
|
|
|
}
|
2010-01-06 12:28:37 +01:00
|
|
|
|
2012-07-12 05:37:56 -07:00
|
|
|
const Ptr& operator[](size_t index) const
|
|
|
|
{ return list.at(index); }
|
2013-07-24 23:58:35 -07:00
|
|
|
Ptr& operator[](size_t index)
|
|
|
|
{ return list.at(index); }
|
2010-01-17 13:37:20 +01:00
|
|
|
|
2012-07-11 06:39:03 -07:00
|
|
|
size_t length() const
|
2012-07-02 21:41:21 -07:00
|
|
|
{ return list.size(); }
|
2010-01-06 15:00:08 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2020-12-16 01:06:05 +03:00
|
|
|
struct Node;
|
|
|
|
struct Extra;
|
|
|
|
struct Property;
|
|
|
|
struct NiUVData;
|
|
|
|
struct NiPosData;
|
|
|
|
struct NiVisData;
|
|
|
|
struct Controller;
|
|
|
|
struct Named;
|
|
|
|
struct NiSkinData;
|
|
|
|
struct NiFloatData;
|
2015-03-06 21:36:42 +13:00
|
|
|
struct NiMorphData;
|
2020-12-16 01:06:05 +03:00
|
|
|
struct NiPixelData;
|
|
|
|
struct NiColorData;
|
2015-03-06 21:36:42 +13:00
|
|
|
struct NiKeyframeData;
|
2020-12-16 01:06:05 +03:00
|
|
|
struct NiTriStripsData;
|
|
|
|
struct NiSkinInstance;
|
|
|
|
struct NiSourceTexture;
|
|
|
|
struct NiPalette;
|
2019-09-13 19:54:19 +03:00
|
|
|
struct NiParticleModifier;
|
2020-11-07 03:40:21 +03:00
|
|
|
struct NiBoolData;
|
2020-11-07 04:04:46 +03:00
|
|
|
struct NiSkinPartition;
|
2020-11-10 22:30:44 +03:00
|
|
|
struct NiFloatInterpolator;
|
|
|
|
struct NiPoint3Interpolator;
|
|
|
|
struct NiTransformInterpolator;
|
2020-12-13 02:35:57 +03:00
|
|
|
struct BSShaderTextureSet;
|
2020-12-13 03:16:05 +03:00
|
|
|
struct NiGeometryData;
|
2020-12-13 04:04:14 +03:00
|
|
|
struct BSShaderProperty;
|
2020-12-16 01:06:05 +03:00
|
|
|
struct NiAlphaProperty;
|
2010-01-06 12:28:37 +01:00
|
|
|
|
2019-10-09 17:08:32 +03:00
|
|
|
using NodePtr = RecordPtrT<Node>;
|
|
|
|
using ExtraPtr = RecordPtrT<Extra>;
|
|
|
|
using NiUVDataPtr = RecordPtrT<NiUVData>;
|
|
|
|
using NiPosDataPtr = RecordPtrT<NiPosData>;
|
|
|
|
using NiVisDataPtr = RecordPtrT<NiVisData>;
|
|
|
|
using ControllerPtr = RecordPtrT<Controller>;
|
|
|
|
using NamedPtr = RecordPtrT<Named>;
|
|
|
|
using NiSkinDataPtr = RecordPtrT<NiSkinData>;
|
|
|
|
using NiMorphDataPtr = RecordPtrT<NiMorphData>;
|
|
|
|
using NiPixelDataPtr = RecordPtrT<NiPixelData>;
|
|
|
|
using NiFloatDataPtr = RecordPtrT<NiFloatData>;
|
|
|
|
using NiColorDataPtr = RecordPtrT<NiColorData>;
|
|
|
|
using NiKeyframeDataPtr = RecordPtrT<NiKeyframeData>;
|
|
|
|
using NiSkinInstancePtr = RecordPtrT<NiSkinInstance>;
|
|
|
|
using NiSourceTexturePtr = RecordPtrT<NiSourceTexture>;
|
|
|
|
using NiPalettePtr = RecordPtrT<NiPalette>;
|
|
|
|
using NiParticleModifierPtr = RecordPtrT<NiParticleModifier>;
|
2020-11-07 03:40:21 +03:00
|
|
|
using NiBoolDataPtr = RecordPtrT<NiBoolData>;
|
2020-11-07 04:04:46 +03:00
|
|
|
using NiSkinPartitionPtr = RecordPtrT<NiSkinPartition>;
|
2020-11-10 22:30:44 +03:00
|
|
|
using NiFloatInterpolatorPtr = RecordPtrT<NiFloatInterpolator>;
|
|
|
|
using NiPoint3InterpolatorPtr = RecordPtrT<NiPoint3Interpolator>;
|
|
|
|
using NiTransformInterpolatorPtr = RecordPtrT<NiTransformInterpolator>;
|
2020-12-13 02:35:57 +03:00
|
|
|
using BSShaderTextureSetPtr = RecordPtrT<BSShaderTextureSet>;
|
2020-12-13 03:16:05 +03:00
|
|
|
using NiGeometryDataPtr = RecordPtrT<NiGeometryData>;
|
2020-12-13 04:04:14 +03:00
|
|
|
using BSShaderPropertyPtr = RecordPtrT<BSShaderProperty>;
|
|
|
|
using NiAlphaPropertyPtr = RecordPtrT<NiAlphaProperty>;
|
2019-10-09 17:08:32 +03:00
|
|
|
|
|
|
|
using NodeList = RecordListT<Node>;
|
|
|
|
using PropertyList = RecordListT<Property>;
|
2019-12-29 15:53:44 +03:00
|
|
|
using ExtraList = RecordListT<Extra>;
|
2019-10-09 17:08:32 +03:00
|
|
|
using NiSourceTextureList = RecordListT<NiSourceTexture>;
|
2020-11-10 22:30:44 +03:00
|
|
|
using NiFloatInterpolatorList = RecordListT<NiFloatInterpolator>;
|
2020-12-13 03:16:05 +03:00
|
|
|
using NiTriStripsDataList = RecordListT<NiTriStripsData>;
|
2010-01-06 12:28:37 +01:00
|
|
|
|
|
|
|
} // Namespace
|
|
|
|
#endif
|