changed global variable records to new variant type

This commit is contained in:
Marc Zinnschlag 2013-03-04 14:32:59 +01:00
parent 65081f5520
commit a1ac20c6f3
6 changed files with 22 additions and 62 deletions

View file

@ -713,9 +713,7 @@ void Record<ESM::Faction>::print()
template<> template<>
void Record<ESM::Global>::print() void Record<ESM::Global>::print()
{ {
// nothing to print (well, nothing that's correct anyway) std::cout << " " << mData.mValue << std::endl;
std::cout << " Type: " << mData.mType << std::endl;
std::cout << " Value: " << mData.mValue << std::endl;
} }
template<> template<>

View file

@ -154,8 +154,7 @@ void CSMDoc::Document::addOptionalGlobals()
{ {
ESM::Global global; ESM::Global global;
global.mId = sGlobals[i]; global.mId = sGlobals[i];
global.mType = ESM::VT_Int; global.mValue.setType (ESM::VT_Int);
global.mValue = 0;
addOptionalGlobal (global); addOptionalGlobal (global);
} }
} }
@ -192,9 +191,14 @@ void CSMDoc::Document::createBase()
for (int i=0; sGlobals[i]; ++i) for (int i=0; sGlobals[i]; ++i)
{ {
ESM::Global record; ESM::Global record;
record.mId = sGlobals[i]; record.mId = sGlobals[i];
record.mValue = i==0 ? 1 : 0;
record.mType = ESM::VT_Float; record.mValue.setType (i==2 ? ESM::VT_Float : ESM::VT_Int);
if (i==0)
record.mValue.setInteger (1);
getData().getGlobals().add (record); getData().getGlobals().add (record);
} }
} }

View file

@ -12,13 +12,13 @@ namespace CSMWorld
virtual QVariant get (const Record<ESXRecordT>& record) const virtual QVariant get (const Record<ESXRecordT>& record) const
{ {
return record.get().mValue; return record.get().mValue.getFloat();
} }
virtual void set (Record<ESXRecordT>& record, const QVariant& data) virtual void set (Record<ESXRecordT>& record, const QVariant& data)
{ {
ESXRecordT record2 = record.get(); ESXRecordT record2 = record.get();
record2.mValue = data.toFloat(); record2.mValue.setFloat (data.toFloat());
record.setModified (record2); record.setModified (record2);
} }

View file

@ -47,27 +47,24 @@ namespace MWWorld
char type = ' '; char type = ' ';
Data value; Data value;
switch (iter->mType) switch (iter->mValue.getType())
{ {
case ESM::VT_Short: case ESM::VT_Short:
type = 's'; type = 's';
value.mShort = *reinterpret_cast<const Interpreter::Type_Float *> ( value.mShort = iter->mValue.getInteger();
&iter->mValue);
break; break;
case ESM::VT_Long: case ESM::VT_Long:
type = 'l'; type = 'l';
value.mLong = *reinterpret_cast<const Interpreter::Type_Float *> ( value.mLong = iter->mValue.getInteger();
&iter->mValue);
break; break;
case ESM::VT_Float: case ESM::VT_Float:
type = 'f'; type = 'f';
value.mFloat = *reinterpret_cast<const Interpreter::Type_Float *> ( value.mFloat = iter->mValue.getFloat();
&iter->mValue);
break; break;
default: default:

View file

@ -6,60 +6,23 @@
namespace ESM namespace ESM
{ {
void Global::load(ESMReader &esm) void Global::load(ESMReader &esm)
{
std::string tmp = esm.getHNString("FNAM");
if (tmp == "s")
mType = VT_Short;
else if (tmp == "l")
mType = VT_Long;
else if (tmp == "f")
mType = VT_Float;
else
esm.fail("Illegal global variable type " + tmp);
// Note: Both floats and longs are represented as floats.
esm.getHNT(mValue, "FLTV");
if (mType==VT_Short)
{ {
if (mValue!=mValue) mValue.read (esm, ESM::Variant::Format_Global);
mValue = 0; // nan
else
mValue = static_cast<short> (mValue);
} }
}
void Global::save(ESMWriter &esm) void Global::save(ESMWriter &esm)
{
switch(mType)
{ {
case VT_Short: mValue.write (esm, ESM::Variant::Format_Global);
esm.writeHNString("FNAM", "s");
break;
case VT_Long:
esm.writeHNString("FNAM", "l");
break;
case VT_Float:
esm.writeHNString("FNAM", "f");
break;
default:
return;
} }
esm.writeHNT("FLTV", mValue);
}
void Global::blank() void Global::blank()
{ {
mValue = 0; mValue.setType (ESM::VT_None);
mType = VT_Float;
} }
bool operator== (const Global& left, const Global& right) bool operator== (const Global& left, const Global& right)
{ {
return left.mId==right.mId && left.mValue==right.mValue && left.mType==right.mType; return left.mId==right.mId && left.mValue==right.mValue;
} }
} }

View file

@ -3,7 +3,6 @@
#include <string> #include <string>
#include "defs.hpp"
#include "variant.hpp" #include "variant.hpp"
namespace ESM namespace ESM
@ -19,8 +18,7 @@ class ESMWriter;
struct Global struct Global
{ {
std::string mId; std::string mId;
float mValue; Variant mValue;
VarType mType;
void load(ESMReader &esm); void load(ESMReader &esm);
void save(ESMWriter &esm); void save(ESMWriter &esm);