2010-07-18 16:48:01 +02:00
|
|
|
#include "globals.hpp"
|
|
|
|
|
|
|
|
#include <stdexcept>
|
|
|
|
|
2022-01-22 15:58:41 +01:00
|
|
|
#include <components/esm3/esmreader.hpp>
|
|
|
|
#include <components/esm3/esmwriter.hpp>
|
2013-12-12 12:19:25 +01:00
|
|
|
|
2012-10-01 19:17:04 +04:00
|
|
|
#include "esmstore.hpp"
|
2010-07-18 16:48:01 +02:00
|
|
|
|
|
|
|
namespace MWWorld
|
|
|
|
{
|
2022-05-21 01:21:55 +02:00
|
|
|
Globals::Collection::const_iterator Globals::find(std::string_view name) const
|
2010-07-18 16:48:01 +02:00
|
|
|
{
|
2023-02-01 18:15:49 +01:00
|
|
|
Collection::const_iterator iter = mVariables.find(name);
|
2013-02-26 14:33:05 +01:00
|
|
|
|
2010-07-18 16:48:01 +02:00
|
|
|
if (iter == mVariables.end())
|
2022-05-21 01:21:55 +02:00
|
|
|
throw std::runtime_error("unknown global variable: " + std::string{ name });
|
2013-02-26 14:33:05 +01:00
|
|
|
|
|
|
|
return iter;
|
2010-07-18 16:48:01 +02:00
|
|
|
}
|
|
|
|
|
2022-05-21 01:21:55 +02:00
|
|
|
Globals::Collection::iterator Globals::find(std::string_view name)
|
2010-07-18 16:48:01 +02:00
|
|
|
{
|
2023-02-01 18:15:49 +01:00
|
|
|
Collection::iterator iter = mVariables.find(name);
|
2013-02-26 14:33:05 +01:00
|
|
|
|
2010-07-18 16:48:01 +02:00
|
|
|
if (iter == mVariables.end())
|
2022-05-21 01:21:55 +02:00
|
|
|
throw std::runtime_error("unknown global variable: " + std::string{ name });
|
2013-02-26 14:33:05 +01:00
|
|
|
|
|
|
|
return iter;
|
2010-07-18 16:48:01 +02:00
|
|
|
}
|
|
|
|
|
2013-12-10 15:09:58 +01:00
|
|
|
void Globals::fill(const MWWorld::ESMStore& store)
|
2010-07-18 16:48:01 +02:00
|
|
|
{
|
2013-12-10 15:09:58 +01:00
|
|
|
mVariables.clear();
|
2013-02-26 14:33:05 +01:00
|
|
|
|
2013-12-10 15:09:58 +01:00
|
|
|
const MWWorld::Store<ESM::Global>& globals = store.get<ESM::Global>();
|
2010-07-18 16:48:01 +02:00
|
|
|
|
2019-03-07 12:38:55 +04:00
|
|
|
for (const ESM::Global& esmGlobal : globals)
|
2010-07-18 16:48:01 +02:00
|
|
|
{
|
2023-02-01 18:15:49 +01:00
|
|
|
mVariables.emplace(esmGlobal.mId.getRefIdString(), esmGlobal);
|
2010-07-18 16:48:01 +02:00
|
|
|
}
|
|
|
|
}
|
2013-02-26 14:33:05 +01:00
|
|
|
|
2023-02-07 00:37:55 +01:00
|
|
|
const ESM::Variant& Globals::operator[](GlobalVariableName name) const
|
2010-07-18 16:48:01 +02:00
|
|
|
{
|
2023-02-07 00:37:55 +01:00
|
|
|
return find(name.getValue())->second.mValue;
|
2010-07-18 16:48:01 +02:00
|
|
|
}
|
2013-02-26 14:33:05 +01:00
|
|
|
|
2023-02-07 00:37:55 +01:00
|
|
|
ESM::Variant& Globals::operator[](GlobalVariableName name)
|
2010-07-18 16:48:01 +02:00
|
|
|
{
|
2023-02-07 00:37:55 +01:00
|
|
|
return find(name.getValue())->second.mValue;
|
2010-07-18 16:48:01 +02:00
|
|
|
}
|
2013-02-26 14:33:05 +01:00
|
|
|
|
2023-02-07 00:37:55 +01:00
|
|
|
char Globals::getType(GlobalVariableName name) const
|
2010-07-21 15:01:35 +02:00
|
|
|
{
|
2023-02-07 00:37:55 +01:00
|
|
|
Collection::const_iterator iter = mVariables.find(name.getValue());
|
2013-02-26 14:33:05 +01:00
|
|
|
|
2010-07-21 15:01:35 +02:00
|
|
|
if (iter == mVariables.end())
|
|
|
|
return ' ';
|
2013-02-26 14:33:05 +01:00
|
|
|
|
2015-07-08 21:40:01 +03:00
|
|
|
switch (iter->second.mValue.getType())
|
2013-12-10 15:09:58 +01:00
|
|
|
{
|
|
|
|
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
|
|
|
|
2013-12-10 15:09:58 +01:00
|
|
|
default:
|
|
|
|
return ' ';
|
|
|
|
}
|
2010-07-21 15:01:35 +02:00
|
|
|
}
|
2013-12-12 12:19:25 +01:00
|
|
|
|
|
|
|
int Globals::countSavedGameRecords() const
|
|
|
|
{
|
|
|
|
return mVariables.size();
|
|
|
|
}
|
|
|
|
|
2014-04-28 11:29:57 +02:00
|
|
|
void Globals::write(ESM::ESMWriter& writer, Loading::Listener& progress) const
|
2013-12-12 12:19:25 +01:00
|
|
|
{
|
2023-02-01 18:15:49 +01:00
|
|
|
for (const auto& variable : mVariables)
|
2013-12-12 12:19:25 +01:00
|
|
|
{
|
|
|
|
writer.startRecord(ESM::REC_GLOB);
|
2023-02-01 18:15:49 +01:00
|
|
|
variable.second.save(writer);
|
2013-12-12 12:19:25 +01:00
|
|
|
writer.endRecord(ESM::REC_GLOB);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-22 19:04:59 +01:00
|
|
|
bool Globals::readRecord(ESM::ESMReader& reader, uint32_t type)
|
2013-12-12 12:19:25 +01:00
|
|
|
{
|
|
|
|
if (type == ESM::REC_GLOB)
|
|
|
|
{
|
2015-07-08 21:40:01 +03:00
|
|
|
ESM::Global global;
|
2015-07-21 13:17:03 +03:00
|
|
|
bool isDeleted = false;
|
2013-12-12 12:19:25 +01:00
|
|
|
|
2015-07-21 13:17:03 +03:00
|
|
|
// 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);
|
2013-12-12 12:19:25 +01:00
|
|
|
|
2023-02-01 18:15:49 +01:00
|
|
|
Collection::iterator iter = mVariables.find(global.mId.getRefIdString());
|
2013-12-12 12:19:25 +01:00
|
|
|
if (iter != mVariables.end())
|
2015-07-08 21:40:01 +03:00
|
|
|
iter->second = global;
|
2013-12-12 12:19:25 +01:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2010-07-18 16:48:01 +02:00
|
|
|
}
|