2013-03-04 12:59:06 +00:00
|
|
|
#include "variantimp.hpp"
|
|
|
|
|
|
|
|
#include <stdexcept>
|
2021-04-18 11:26:26 +00:00
|
|
|
#include <cmath>
|
2013-03-04 12:59:06 +00:00
|
|
|
|
|
|
|
#include "esmreader.hpp"
|
|
|
|
#include "esmwriter.hpp"
|
|
|
|
|
2021-04-04 17:17:10 +00:00
|
|
|
void ESM::readESMVariantValue(ESMReader& esm, Variant::Format format, VarType type, std::string& out)
|
2013-03-04 12:59:06 +00:00
|
|
|
{
|
|
|
|
if (type!=VT_String)
|
|
|
|
throw std::logic_error ("not a string type");
|
|
|
|
|
|
|
|
if (format==Variant::Format_Global)
|
|
|
|
esm.fail ("global variables of type string not supported");
|
|
|
|
|
2013-03-05 08:40:41 +00:00
|
|
|
if (format==Variant::Format_Info)
|
|
|
|
esm.fail ("info variables of type string not supported");
|
|
|
|
|
2015-01-24 15:15:52 +00:00
|
|
|
if (format==Variant::Format_Local)
|
|
|
|
esm.fail ("local variables of type string not supported");
|
|
|
|
|
2013-03-04 12:59:06 +00:00
|
|
|
// GMST
|
2021-04-04 17:17:10 +00:00
|
|
|
out = esm.getHString();
|
2013-03-04 12:59:06 +00:00
|
|
|
}
|
|
|
|
|
2021-04-04 17:17:10 +00:00
|
|
|
void ESM::writeESMVariantValue(ESMWriter& esm, Variant::Format format, VarType type, const std::string& in)
|
2013-03-04 12:59:06 +00:00
|
|
|
{
|
|
|
|
if (type!=VT_String)
|
|
|
|
throw std::logic_error ("not a string type");
|
|
|
|
|
|
|
|
if (format==Variant::Format_Global)
|
|
|
|
throw std::runtime_error ("global variables of type string not supported");
|
|
|
|
|
2013-03-05 08:40:41 +00:00
|
|
|
if (format==Variant::Format_Info)
|
|
|
|
throw std::runtime_error ("info variables of type string not supported");
|
|
|
|
|
2021-04-04 20:51:21 +00:00
|
|
|
if (format==Variant::Format_Local)
|
|
|
|
throw std::runtime_error ("local variables of type string not supported");
|
|
|
|
|
2013-03-04 12:59:06 +00:00
|
|
|
// GMST
|
2021-04-04 17:17:10 +00:00
|
|
|
esm.writeHNString("STRV", in);
|
2013-03-04 12:59:06 +00:00
|
|
|
}
|
|
|
|
|
2021-04-04 17:17:10 +00:00
|
|
|
void ESM::readESMVariantValue(ESMReader& esm, Variant::Format format, VarType type, int& out)
|
2013-03-04 12:59:06 +00:00
|
|
|
{
|
|
|
|
if (type!=VT_Short && type!=VT_Long && type!=VT_Int)
|
|
|
|
throw std::logic_error ("not an integer type");
|
|
|
|
|
|
|
|
if (format==Variant::Format_Global)
|
|
|
|
{
|
|
|
|
float value;
|
|
|
|
esm.getHNT (value, "FLTV");
|
|
|
|
|
|
|
|
if (type==VT_Short)
|
2021-04-18 11:26:26 +00:00
|
|
|
if (std::isnan(value))
|
2021-04-17 18:58:09 +00:00
|
|
|
out = 0;
|
|
|
|
else
|
|
|
|
out = static_cast<short> (value);
|
2013-03-04 12:59:06 +00:00
|
|
|
else if (type==VT_Long)
|
2021-04-04 17:17:10 +00:00
|
|
|
out = static_cast<int> (value);
|
2013-03-04 12:59:06 +00:00
|
|
|
else
|
|
|
|
esm.fail ("unsupported global variable integer type");
|
|
|
|
}
|
2013-03-05 08:40:41 +00:00
|
|
|
else if (format==Variant::Format_Gmst || format==Variant::Format_Info)
|
2013-03-04 12:59:06 +00:00
|
|
|
{
|
2013-03-05 07:15:03 +00:00
|
|
|
if (type!=VT_Int)
|
2013-03-05 08:40:41 +00:00
|
|
|
{
|
|
|
|
std::ostringstream stream;
|
|
|
|
stream
|
|
|
|
<< "unsupported " <<(format==Variant::Format_Gmst ? "gmst" : "info")
|
|
|
|
<< " variable integer type";
|
|
|
|
esm.fail (stream.str());
|
|
|
|
}
|
2013-03-04 12:59:06 +00:00
|
|
|
|
2021-04-04 17:17:10 +00:00
|
|
|
esm.getHT(out);
|
2013-03-04 12:59:06 +00:00
|
|
|
}
|
2015-01-24 15:15:52 +00:00
|
|
|
else if (format==Variant::Format_Local)
|
|
|
|
{
|
|
|
|
if (type==VT_Short)
|
|
|
|
{
|
|
|
|
short value;
|
|
|
|
esm.getHT(value);
|
2021-04-04 17:17:10 +00:00
|
|
|
out = value;
|
2015-01-24 15:15:52 +00:00
|
|
|
}
|
|
|
|
else if (type==VT_Int)
|
|
|
|
{
|
2021-04-04 17:17:10 +00:00
|
|
|
esm.getHT(out);
|
2015-01-24 15:15:52 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
esm.fail("unsupported local variable integer type");
|
|
|
|
}
|
2013-03-04 12:59:06 +00:00
|
|
|
}
|
|
|
|
|
2021-04-04 17:17:10 +00:00
|
|
|
void ESM::writeESMVariantValue(ESMWriter& esm, Variant::Format format, VarType type, int in)
|
2013-03-04 12:59:06 +00:00
|
|
|
{
|
|
|
|
if (type!=VT_Short && type!=VT_Long && type!=VT_Int)
|
|
|
|
throw std::logic_error ("not an integer type");
|
|
|
|
|
|
|
|
if (format==Variant::Format_Global)
|
|
|
|
{
|
|
|
|
if (type==VT_Short || type==VT_Long)
|
|
|
|
{
|
2021-04-04 17:17:10 +00:00
|
|
|
float value = static_cast<float>(in);
|
2013-03-04 12:59:06 +00:00
|
|
|
esm.writeHNString ("FNAM", type==VT_Short ? "s" : "l");
|
|
|
|
esm.writeHNT ("FLTV", value);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
throw std::runtime_error ("unsupported global variable integer type");
|
|
|
|
}
|
2013-03-05 08:40:41 +00:00
|
|
|
else if (format==Variant::Format_Gmst || format==Variant::Format_Info)
|
2013-03-04 12:59:06 +00:00
|
|
|
{
|
2013-09-27 11:17:04 +00:00
|
|
|
if (type!=VT_Int)
|
2013-03-05 08:40:41 +00:00
|
|
|
{
|
|
|
|
std::ostringstream stream;
|
|
|
|
stream
|
|
|
|
<< "unsupported " <<(format==Variant::Format_Gmst ? "gmst" : "info")
|
|
|
|
<< " variable integer type";
|
|
|
|
throw std::runtime_error (stream.str());
|
|
|
|
}
|
2013-03-04 12:59:06 +00:00
|
|
|
|
2021-04-04 17:17:10 +00:00
|
|
|
esm.writeHNT("INTV", in);
|
2013-03-04 12:59:06 +00:00
|
|
|
}
|
2015-01-24 15:15:52 +00:00
|
|
|
else if (format==Variant::Format_Local)
|
|
|
|
{
|
|
|
|
if (type==VT_Short)
|
2021-04-04 17:17:10 +00:00
|
|
|
esm.writeHNT("STTV", static_cast<short>(in));
|
2015-01-24 15:15:52 +00:00
|
|
|
else if (type == VT_Int)
|
2021-04-04 17:17:10 +00:00
|
|
|
esm.writeHNT("INTV", in);
|
2015-01-24 15:15:52 +00:00
|
|
|
else
|
|
|
|
throw std::runtime_error("unsupported local variable integer type");
|
|
|
|
}
|
2013-03-04 12:59:06 +00:00
|
|
|
}
|
|
|
|
|
2021-04-04 17:17:10 +00:00
|
|
|
void ESM::readESMVariantValue(ESMReader& esm, Variant::Format format, VarType type, float& out)
|
2013-03-04 12:59:06 +00:00
|
|
|
{
|
|
|
|
if (type!=VT_Float)
|
|
|
|
throw std::logic_error ("not a float type");
|
|
|
|
|
|
|
|
if (format==Variant::Format_Global)
|
|
|
|
{
|
2021-04-04 17:17:10 +00:00
|
|
|
esm.getHNT(out, "FLTV");
|
2013-03-04 12:59:06 +00:00
|
|
|
}
|
2015-01-24 15:15:52 +00:00
|
|
|
else if (format==Variant::Format_Gmst || format==Variant::Format_Info || format==Variant::Format_Local)
|
2013-03-04 12:59:06 +00:00
|
|
|
{
|
2021-04-04 17:17:10 +00:00
|
|
|
esm.getHT(out);
|
2013-03-04 12:59:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-04 17:17:10 +00:00
|
|
|
void ESM::writeESMVariantValue(ESMWriter& esm, Variant::Format format, VarType type, float in)
|
2013-03-04 12:59:06 +00:00
|
|
|
{
|
|
|
|
if (type!=VT_Float)
|
|
|
|
throw std::logic_error ("not a float type");
|
|
|
|
|
|
|
|
if (format==Variant::Format_Global)
|
|
|
|
{
|
|
|
|
esm.writeHNString ("FNAM", "f");
|
2021-04-04 17:17:10 +00:00
|
|
|
esm.writeHNT("FLTV", in);
|
2013-03-04 12:59:06 +00:00
|
|
|
}
|
2015-01-24 15:15:52 +00:00
|
|
|
else if (format==Variant::Format_Gmst || format==Variant::Format_Info || format==Variant::Format_Local)
|
2013-03-04 12:59:06 +00:00
|
|
|
{
|
2021-04-04 17:17:10 +00:00
|
|
|
esm.writeHNT("FLTV", in);
|
2013-03-04 12:59:06 +00:00
|
|
|
}
|
|
|
|
}
|