1
0
mirror of https://gitlab.com/OpenMW/openmw.git synced 2025-01-26 18:35:20 +00:00
OpenMW/apps/openmw/mwworld/globals.cpp

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

111 lines
2.8 KiB
C++
Raw Normal View History

#include "globals.hpp"
#include <stdexcept>
#include <components/esm3/esmreader.hpp>
#include <components/esm3/esmwriter.hpp>
2012-10-01 19:17:04 +04:00
#include "esmstore.hpp"
namespace MWWorld
{
Globals::Collection::const_iterator Globals::find(std::string_view name) const
{
Collection::const_iterator iter = mVariables.find(name);
2013-02-26 14:33:05 +01:00
if (iter == mVariables.end())
throw std::runtime_error("unknown global variable: " + std::string{ name });
2013-02-26 14:33:05 +01:00
return iter;
}
Globals::Collection::iterator Globals::find(std::string_view name)
{
Collection::iterator iter = mVariables.find(name);
2013-02-26 14:33:05 +01:00
if (iter == mVariables.end())
throw std::runtime_error("unknown global variable: " + std::string{ name });
2013-02-26 14:33:05 +01:00
return iter;
}
void Globals::fill(const MWWorld::ESMStore& store)
{
mVariables.clear();
2013-02-26 14:33:05 +01:00
const MWWorld::Store<ESM::Global>& globals = store.get<ESM::Global>();
for (const ESM::Global& esmGlobal : globals)
{
mVariables.emplace(esmGlobal.mId.getRefIdString(), esmGlobal);
}
}
2013-02-26 14:33:05 +01:00
const ESM::Variant& Globals::operator[](GlobalVariableName name) const
{
return find(name.getValue())->second.mValue;
}
2013-02-26 14:33:05 +01:00
ESM::Variant& Globals::operator[](GlobalVariableName name)
{
return find(name.getValue())->second.mValue;
}
2013-02-26 14:33:05 +01:00
char Globals::getType(GlobalVariableName name) const
{
Collection::const_iterator iter = mVariables.find(name.getValue());
2013-02-26 14:33:05 +01:00
if (iter == mVariables.end())
return ' ';
2013-02-26 14:33:05 +01:00
switch (iter->second.mValue.getType())
{
case ESM::VT_Short:
return 's';
case ESM::VT_Long:
return 'l';
case ESM::VT_Float:
return 'f';
2022-09-22 21:26:05 +03:00
default:
return ' ';
}
}
int Globals::countSavedGameRecords() const
{
return mVariables.size();
}
void Globals::write(ESM::ESMWriter& writer, Loading::Listener& progress) const
{
for (const auto& variable : mVariables)
{
writer.startRecord(ESM::REC_GLOB);
variable.second.save(writer);
writer.endRecord(ESM::REC_GLOB);
}
}
2015-01-22 19:04:59 +01:00
bool Globals::readRecord(ESM::ESMReader& reader, uint32_t type)
{
if (type == ESM::REC_GLOB)
{
ESM::Global global;
bool isDeleted = false;
// This readRecord() method is used when reading a saved game.
// Deleted globals can't appear there, so isDeleted will be ignored here.
global.load(reader, isDeleted);
Collection::iterator iter = mVariables.find(global.mId.getRefIdString());
if (iter != mVariables.end())
iter->second = global;
return true;
}
return false;
}
}