openmw-tes3coop/components/esm/loadgmst.cpp

86 lines
1.6 KiB
C++
Raw Normal View History

#include "loadgmst.hpp"
#include <stdexcept>
#include <boost/algorithm/string.hpp>
#include "esmreader.hpp"
#include "esmwriter.hpp"
namespace ESM
{
void GameSetting::load(ESMReader &esm)
{
2012-09-17 07:37:50 +00:00
assert(mId != "");
// We are apparently allowed to be empty
if (!esm.hasMoreSubs())
{
2012-09-17 07:37:50 +00:00
mType = VT_None;
return;
}
// Load some data
esm.getSubName();
NAME n = esm.retSubName();
if (n == "STRV")
{
2012-09-17 07:37:50 +00:00
mStr = esm.getHString();
mType = VT_String;
}
else if (n == "INTV")
{
2012-09-17 07:37:50 +00:00
esm.getHT(mI);
mType = VT_Int;
}
else if (n == "FLTV")
{
2012-09-17 07:37:50 +00:00
esm.getHT(mF);
mType = VT_Float;
}
else
esm.fail("Unwanted subrecord type");
}
void GameSetting::save(ESMWriter &esm)
{
2012-09-17 07:37:50 +00:00
switch(mType)
{
2012-09-17 07:37:50 +00:00
case VT_String: esm.writeHNString("STRV", mStr); break;
case VT_Int: esm.writeHNT("INTV", mI); break;
case VT_Float: esm.writeHNT("FLTV", mF); break;
default: break;
}
}
int GameSetting::getInt() const
{
2012-09-17 07:37:50 +00:00
switch (mType)
{
2012-09-17 07:37:50 +00:00
case VT_Float: return static_cast<int> (mF);
case VT_Int: return mI;
default: throw std::runtime_error ("GMST " + mId + " is not of a numeric type");
}
}
float GameSetting::getFloat() const
{
2012-09-17 07:37:50 +00:00
switch (mType)
{
2012-09-17 07:37:50 +00:00
case VT_Float: return mF;
case VT_Int: return mI;
default: throw std::runtime_error ("GMST " + mId + " is not of a numeric type");
}
}
std::string GameSetting::getString() const
{
2012-09-17 07:37:50 +00:00
if (mType==VT_String)
return mStr;
2012-09-17 07:37:50 +00:00
throw std::runtime_error ("GMST " + mId + " is not a string");
}
}