mirror of https://github.com/OpenMW/openmw.git
moved global variable handling into a separate class
parent
608ddd0a58
commit
083b11c740
@ -0,0 +1,149 @@
|
||||
|
||||
#include "globals.hpp"
|
||||
|
||||
#include <stdexcept>
|
||||
|
||||
#include <components/esm_store/store.hpp>
|
||||
|
||||
namespace MWWorld
|
||||
{
|
||||
Globals::Collection::const_iterator Globals::find (const std::string& name) const
|
||||
{
|
||||
Collection::const_iterator iter = mVariables.find (name);
|
||||
|
||||
if (iter==mVariables.end())
|
||||
throw std::runtime_error ("unknown global variable: " + name);
|
||||
|
||||
return iter;
|
||||
}
|
||||
|
||||
Globals::Collection::iterator Globals::find (const std::string& name)
|
||||
{
|
||||
Collection::iterator iter = mVariables.find (name);
|
||||
|
||||
if (iter==mVariables.end())
|
||||
throw std::runtime_error ("unknown global variable: " + name);
|
||||
|
||||
return iter;
|
||||
}
|
||||
|
||||
Globals::Globals (const ESMS::ESMStore& store)
|
||||
{
|
||||
for (ESMS::RecListT<ESM::Global>::MapType::const_iterator iter
|
||||
(store.globals.list.begin()); iter != store.globals.list.end(); ++iter)
|
||||
{
|
||||
char type = ' ';
|
||||
Data value;
|
||||
|
||||
switch (iter->second.type)
|
||||
{
|
||||
case ESM::VT_Short:
|
||||
|
||||
type = 's';
|
||||
value.mShort = *reinterpret_cast<const Interpreter::Type_Integer *> (
|
||||
&iter->second.value);
|
||||
break;
|
||||
|
||||
case ESM::VT_Int:
|
||||
|
||||
type = 'l';
|
||||
value.mLong = *reinterpret_cast<const Interpreter::Type_Float *> (
|
||||
&iter->second.value);
|
||||
break;
|
||||
|
||||
case ESM::VT_Float:
|
||||
|
||||
type = 'f';
|
||||
value.mFloat = *reinterpret_cast<const Interpreter::Type_Float *> (
|
||||
&iter->second.value);
|
||||
break;
|
||||
|
||||
default:
|
||||
|
||||
throw std::runtime_error ("unsupported global variable type");
|
||||
}
|
||||
|
||||
mVariables.insert (std::make_pair (iter->first, std::make_pair (type, value)));
|
||||
}
|
||||
|
||||
if (mVariables.find ("dayspassed")==mVariables.end())
|
||||
{
|
||||
// vanilla Morrowind does not define dayspassed.
|
||||
Data value;
|
||||
value.mLong = 0;
|
||||
|
||||
mVariables.insert (std::make_pair ("dayspassed", std::make_pair ('l', value)));
|
||||
}
|
||||
}
|
||||
|
||||
const Globals::Data& Globals::operator[] (const std::string& name) const
|
||||
{
|
||||
Collection::const_iterator iter = find (name);
|
||||
|
||||
return iter->second.second;
|
||||
}
|
||||
|
||||
Globals::Data& Globals::operator[] (const std::string& name)
|
||||
{
|
||||
Collection::iterator iter = find (name);
|
||||
|
||||
return iter->second.second;
|
||||
}
|
||||
|
||||
void Globals::setInt (const std::string& name, int value)
|
||||
{
|
||||
Collection::iterator iter = find (name);
|
||||
|
||||
switch (iter->second.first)
|
||||
{
|
||||
case 's': iter->second.second.mShort = value; break;
|
||||
case 'l': iter->second.second.mLong = value; break;
|
||||
case 'f': iter->second.second.mFloat = value; break;
|
||||
|
||||
default: throw std::runtime_error ("unsupported global variable type");
|
||||
}
|
||||
}
|
||||
|
||||
void Globals::setFloat (const std::string& name, float value)
|
||||
{
|
||||
Collection::iterator iter = find (name);
|
||||
|
||||
switch (iter->second.first)
|
||||
{
|
||||
case 's': iter->second.second.mShort = value; break;
|
||||
case 'l': iter->second.second.mLong = value; break;
|
||||
case 'f': iter->second.second.mFloat = value; break;
|
||||
|
||||
default: throw std::runtime_error ("unsupported global variable type");
|
||||
}
|
||||
}
|
||||
|
||||
int Globals::getInt (const std::string& name) const
|
||||
{
|
||||
Collection::const_iterator iter = find (name);
|
||||
|
||||
switch (iter->second.first)
|
||||
{
|
||||
case 's': return iter->second.second.mShort;
|
||||
case 'l': return iter->second.second.mLong;
|
||||
case 'f': return iter->second.second.mFloat;
|
||||
|
||||
default: throw std::runtime_error ("unsupported global variable type");
|
||||
}
|
||||
}
|
||||
|
||||
float Globals::getFloat (const std::string& name) const
|
||||
{
|
||||
Collection::const_iterator iter = find (name);
|
||||
|
||||
switch (iter->second.first)
|
||||
{
|
||||
case 's': return iter->second.second.mShort;
|
||||
case 'l': return iter->second.second.mLong;
|
||||
case 'f': return iter->second.second.mFloat;
|
||||
|
||||
default: throw std::runtime_error ("unsupported global variable type");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,59 @@
|
||||
#ifndef GAME_MWWORLD_GLOBALS_H
|
||||
#define GAME_MWWORLD_GLOBALS_H
|
||||
|
||||
#include <string>
|
||||
#include <map>
|
||||
|
||||
#include <components/interpreter/types.hpp>
|
||||
|
||||
namespace ESMS
|
||||
{
|
||||
struct ESMStore;
|
||||
}
|
||||
|
||||
namespace MWWorld
|
||||
{
|
||||
class Globals
|
||||
{
|
||||
public:
|
||||
|
||||
union Data
|
||||
{
|
||||
Interpreter::Type_Float mFloat;
|
||||
Interpreter::Type_Float mLong; // Why Morrowind, why? :(
|
||||
Interpreter::Type_Integer mShort;
|
||||
};
|
||||
|
||||
typedef std::map<std::string, std::pair<char, Data> > Collection;
|
||||
|
||||
private:
|
||||
|
||||
Collection mVariables; // type, value
|
||||
|
||||
Collection::const_iterator find (const std::string& name) const;
|
||||
|
||||
Collection::iterator find (const std::string& name);
|
||||
|
||||
public:
|
||||
|
||||
Globals (const ESMS::ESMStore& store);
|
||||
|
||||
const Data& operator[] (const std::string& name) const;
|
||||
|
||||
Data& operator[] (const std::string& name);
|
||||
|
||||
void setInt (const std::string& name, int value);
|
||||
///< Set value independently from real type.
|
||||
|
||||
void setFloat (const std::string& name, float value);
|
||||
///< Set value independently from real type.
|
||||
|
||||
int getInt (const std::string& name) const;
|
||||
///< Get value independently from real type.
|
||||
|
||||
float getFloat (const std::string& name) const;
|
||||
///< Get value independently from real type.
|
||||
};
|
||||
}
|
||||
|
||||
#endif
|
Loading…
Reference in New Issue