1
0
Fork 0
mirror of https://github.com/OpenMW/openmw.git synced 2025-01-16 17:59:56 +00:00
openmw/components/esm3/variantimp.cpp

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

169 lines
5.3 KiB
C++
Raw Normal View History

2013-03-04 12:59:06 +00:00
#include "variantimp.hpp"
2021-04-18 11:26:26 +00:00
#include <cmath>
#include <sstream>
2013-03-04 12:59:06 +00:00
#include <stdexcept>
#include "esmreader.hpp"
#include "esmwriter.hpp"
namespace ESM
{
void 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");
if (format == Variant::Format_Info)
esm.fail("info variables of type string not supported");
if (format == Variant::Format_Local)
esm.fail("local variables of type string not supported");
2013-03-04 12:59:06 +00:00
// GMST
2013-03-04 12:59:06 +00:00
out = esm.getHString();
2022-09-22 18:26:05 +00:00
}
2013-03-04 12:59:06 +00:00
void writeESMVariantValue(ESMWriter& esm, Variant::Format format, VarType type, const std::string& in)
2022-09-22 18:26:05 +00:00
{
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");
if (format == Variant::Format_Info)
throw std::runtime_error("info variables of type string not supported");
2013-03-04 12:59:06 +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
esm.writeHNString("STRV", in);
2022-09-22 18:26:05 +00:00
}
2013-03-04 12:59:06 +00:00
2023-10-24 17:25:52 +00:00
void readESMVariantValue(ESMReader& esm, Variant::Format format, VarType type, int32_t& 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");
2022-09-22 18:26:05 +00:00
if (type == VT_Short)
if (std::isnan(value))
2022-09-22 18:26:05 +00:00
out = 0;
else
2023-10-24 17:25:52 +00:00
out = static_cast<int16_t>(value);
2013-03-04 12:59:06 +00:00
else if (type == VT_Long)
2023-10-24 17:25:52 +00:00
out = static_cast<int32_t>(value);
2022-09-22 18:26:05 +00:00
else
esm.fail("unsupported global variable integer type");
}
else if (format == Variant::Format_Gmst || format == Variant::Format_Info)
{
if (type != VT_Int)
2022-09-22 18:26:05 +00:00
{
std::ostringstream stream;
stream << "unsupported " << (format == Variant::Format_Gmst ? "gmst" : "info")
<< " variable integer type";
esm.fail(stream.str());
2022-09-22 18:26:05 +00:00
}
esm.getHT(out);
}
else if (format == Variant::Format_Local)
{
if (type == VT_Short)
2022-09-22 18:26:05 +00:00
{
2023-10-24 17:25:52 +00:00
int16_t value;
esm.getHT(value);
out = value;
2022-09-22 18:26:05 +00:00
}
else if (type == VT_Int)
2022-09-22 18:26:05 +00:00
{
esm.getHT(out);
2022-09-22 18:26:05 +00:00
}
else
esm.fail("unsupported local variable integer type");
}
}
2013-03-04 12:59:06 +00:00
2023-10-24 17:25:52 +00:00
void writeESMVariantValue(ESMWriter& esm, Variant::Format format, VarType type, int32_t 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");
2022-09-22 18:26:05 +00:00
2013-03-04 12:59:06 +00:00
if (format == Variant::Format_Global)
{
if (type == VT_Short || type == VT_Long)
2022-09-22 18:26:05 +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);
2022-09-22 18:26:05 +00:00
}
else
2013-03-04 12:59:06 +00:00
throw std::runtime_error("unsupported global variable integer type");
}
else if (format == Variant::Format_Gmst || format == Variant::Format_Info)
{
if (type != VT_Int)
2022-09-22 18:26:05 +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
esm.writeHNT("INTV", in);
2022-09-22 18:26:05 +00:00
}
else if (format == Variant::Format_Local)
2022-09-22 18:26:05 +00:00
{
if (type == VT_Short)
2023-10-24 17:25:52 +00:00
esm.writeHNT("STTV", static_cast<int16_t>(in));
else if (type == VT_Int)
esm.writeHNT("INTV", in);
2022-09-22 18:26:05 +00:00
else
throw std::runtime_error("unsupported local variable integer type");
2022-09-22 18:26:05 +00:00
}
}
2013-03-04 12:59:06 +00:00
void 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");
2013-03-04 12:59:06 +00:00
if (format == Variant::Format_Global)
2022-09-22 18:26:05 +00:00
{
esm.getHNT(out, "FLTV");
2022-09-22 18:26:05 +00:00
}
else if (format == Variant::Format_Gmst || format == Variant::Format_Info || format == Variant::Format_Local)
2013-03-04 12:59:06 +00:00
{
esm.getHT(out);
2022-09-22 18:26:05 +00:00
}
2013-03-04 12:59:06 +00:00
}
2022-09-22 18:26:05 +00:00
void 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");
2022-09-22 18:26:05 +00:00
2013-03-04 12:59:06 +00:00
if (format == Variant::Format_Global)
2022-09-22 18:26:05 +00:00
{
2013-03-04 12:59:06 +00:00
esm.writeHNString("FNAM", "f");
esm.writeHNT("FLTV", in);
2022-09-22 18:26:05 +00:00
}
else if (format == Variant::Format_Gmst || format == Variant::Format_Info || format == Variant::Format_Local)
2022-09-22 18:26:05 +00:00
{
esm.writeHNT("FLTV", in);
2022-09-22 18:26:05 +00:00
}
2013-03-04 12:59:06 +00:00
}
}