store global variables in saved game files

actorid
Marc Zinnschlag 11 years ago
parent b38bfe1f21
commit fc37c77a91

@ -161,6 +161,7 @@ void MWState::StateManager::loadGame (const Character *character, const Slot *sl
case ESM::REC_NPC_: case ESM::REC_NPC_:
case ESM::REC_SPEL: case ESM::REC_SPEL:
case ESM::REC_WEAP: case ESM::REC_WEAP:
case ESM::REC_GLOB:
MWBase::Environment::get().getWorld()->readRecord (reader, n.val); MWBase::Environment::get().getWorld()->readRecord (reader, n.val);
break; break;

@ -3,6 +3,11 @@
#include <stdexcept> #include <stdexcept>
#include <components/misc/stringops.hpp>
#include <components/esm/esmwriter.hpp>
#include <components/esm/esmreader.hpp>
#include "esmstore.hpp" #include "esmstore.hpp"
namespace MWWorld namespace MWWorld
@ -66,4 +71,39 @@ namespace MWWorld
default: return ' '; default: return ' ';
} }
} }
int Globals::countSavedGameRecords() const
{
return mVariables.size();
}
void Globals::write (ESM::ESMWriter& writer) const
{
for (Collection::const_iterator iter (mVariables.begin()); iter!=mVariables.end(); ++iter)
{
writer.startRecord (ESM::REC_GLOB);
writer.writeHNString ("NAME", iter->first);
iter->second.write (writer, ESM::Variant::Format_Global);
writer.endRecord (ESM::REC_GLOB);
}
}
bool Globals::readRecord (ESM::ESMReader& reader, int32_t type)
{
if (type==ESM::REC_GLOB)
{
std::string id = reader.getHNString ("NAME");
Collection::iterator iter = mVariables.find (Misc::StringUtils::lowerCase (id));
if (iter!=mVariables.end())
iter->second.read (reader, ESM::Variant::Format_Global);
else
reader.skipHRecord();
return true;
}
return false;
}
} }

@ -5,9 +5,17 @@
#include <string> #include <string>
#include <map> #include <map>
#include <libs/platform/stdint.h>
#include <components/interpreter/types.hpp> #include <components/interpreter/types.hpp>
#include <components/esm/variant.hpp> #include <components/esm/variant.hpp>
namespace ESM
{
class ESMWriter;
class ESMReader;
}
namespace MWWorld namespace MWWorld
{ {
class ESMStore; class ESMStore;
@ -35,6 +43,16 @@ namespace MWWorld
void fill (const MWWorld::ESMStore& store); void fill (const MWWorld::ESMStore& store);
///< Replace variables with variables from \a store with default values. ///< Replace variables with variables from \a store with default values.
int countSavedGameRecords() const;
void write (ESM::ESMWriter& writer) const;
bool readRecord (ESM::ESMReader& reader, int32_t type);
///< Records for variables that do not exist are dropped silently.
///
/// \return Known type?
}; };
} }

@ -301,20 +301,23 @@ namespace MWWorld
int World::countSavedGameRecords() const int World::countSavedGameRecords() const
{ {
return mStore.countSavedGameRecords(); return
mStore.countSavedGameRecords()
+mGlobalVariables.countSavedGameRecords();
} }
void World::write (ESM::ESMWriter& writer) const void World::write (ESM::ESMWriter& writer) const
{ {
mStore.write (writer); mStore.write (writer);
mGlobalVariables.write (writer);
} }
void World::readRecord (ESM::ESMReader& reader, int32_t type) void World::readRecord (ESM::ESMReader& reader, int32_t type)
{ {
if (!mStore.readRecord (reader, type)) if (!mStore.readRecord (reader, type) &&
!mGlobalVariables.readRecord (reader, type))
{ {
/// \todo handle other world state records throw std::runtime_error ("unknown record in saved game");
} }
} }

Loading…
Cancel
Save