metaforce/hecl/lib/CVar.cpp

375 lines
9.2 KiB
C++
Raw Normal View History

2016-03-04 23:02:44 +00:00
#include "hecl/hecl.hpp"
#include "hecl/CVar.hpp"
2015-12-02 21:11:10 +00:00
2016-03-04 23:02:44 +00:00
#include <athena/Utility.hpp>
2015-12-02 21:11:10 +00:00
#include <algorithm>
2018-01-14 06:40:42 +00:00
#include <sstream>
2015-12-02 21:11:10 +00:00
2016-03-04 23:02:44 +00:00
namespace hecl
2015-12-02 21:11:10 +00:00
{
2017-11-13 06:13:53 +00:00
using namespace std::literals;
2018-01-15 12:34:02 +00:00
extern BoolCVar* com_developer;
extern BoolCVar* com_enableCheats;
2017-11-13 06:13:53 +00:00
2018-01-15 12:34:02 +00:00
CVar::CVar(std::string_view name, std::string_view help, CVar::EFlags flags)
: m_name(name)
, m_help(help)
, m_flags(flags)
2015-12-02 21:11:10 +00:00
{
}
2018-01-15 12:34:02 +00:00
std::string CVar::help() const
2015-12-02 21:11:10 +00:00
{
2018-01-15 12:34:02 +00:00
return std::string(m_help + (hasDefaultValue() ? " (default: " + defaultValueString() : "") +
(isReadOnly() ? " [ReadOnly]" : "")) + ")";
2015-12-02 21:11:10 +00:00
}
2018-01-15 12:34:02 +00:00
bool CVar::fromString(std::string_view v)
2015-12-02 21:11:10 +00:00
{
2018-01-15 12:34:02 +00:00
if (isCheat() && (com_developer && !com_developer->value() && !com_enableCheats->value()))
return false;
else if (isCheat())
return false;
2016-01-04 05:25:00 +00:00
2018-01-15 12:34:02 +00:00
if (isReadOnly() && (com_developer && !com_developer->value()))
return false;
2015-12-02 21:11:10 +00:00
2018-01-15 12:34:02 +00:00
bool ret = _fromString(v);
if (ret)
m_flags |= EFlags::Modified;
return ret;
2015-12-02 21:11:10 +00:00
}
2018-01-15 12:34:02 +00:00
bool CVar::isModified() const { return int(m_flags & EFlags::Modified) != 0; }
bool CVar::modificationRequiresRestart() const { return int(m_flags & EFlags::ModifyRestart) != 0; }
2016-01-04 05:25:00 +00:00
2018-01-15 12:34:02 +00:00
bool CVar::isReadOnly() const { return int(m_flags & EFlags::ReadOnly) != 0; }
2015-12-02 21:11:10 +00:00
2018-01-15 12:34:02 +00:00
bool CVar::isCheat() const { return int(m_flags & EFlags::Cheat) != 0; }
2015-12-02 21:11:10 +00:00
2018-01-15 12:34:02 +00:00
bool CVar::isHidden() const { return int(m_flags & EFlags::Hidden) != 0; }
2015-12-02 21:11:10 +00:00
2018-01-15 12:34:02 +00:00
bool CVar::isArchive() const { return int(m_flags & EFlags::Archive) != 0; }
2016-01-04 05:25:00 +00:00
2018-01-15 12:34:02 +00:00
bool CVar::isInternalArchivable() const { return int(m_flags & EFlags::InternalArchivable) != 0; }
2015-12-02 21:11:10 +00:00
2018-01-15 12:34:02 +00:00
bool CVar::wasDeserialized() const { return m_wasDeserialized; }
2015-12-02 21:11:10 +00:00
2018-01-15 12:34:02 +00:00
void CVar::clearModified()
2015-12-02 21:11:10 +00:00
{
2018-01-15 12:34:02 +00:00
if (!modificationRequiresRestart())
m_flags &= ~EFlags::Modified;
2015-12-02 21:11:10 +00:00
}
2018-01-15 12:34:02 +00:00
void CVar::setModified() { m_flags |= EFlags::Modified; }
2015-12-02 21:11:10 +00:00
2018-01-15 12:34:02 +00:00
void CVar::unlock()
2015-12-02 21:11:10 +00:00
{
2018-01-15 12:34:02 +00:00
if (isReadOnly() && !m_unlocked)
2015-12-02 21:11:10 +00:00
{
2018-01-15 12:34:02 +00:00
m_oldFlags = m_flags;
m_flags &= ~EFlags::ReadOnly;
m_unlocked = true;
2015-12-02 21:11:10 +00:00
}
}
2018-01-15 12:34:02 +00:00
void CVar::lock()
2015-12-02 21:11:10 +00:00
{
2018-01-15 12:34:02 +00:00
if (!isReadOnly() && m_unlocked)
2015-12-02 21:11:10 +00:00
{
2018-01-15 12:34:02 +00:00
m_flags = m_oldFlags;
m_unlocked = false;
2015-12-02 21:11:10 +00:00
}
}
2018-01-15 12:34:02 +00:00
template <typename T>
TCVar<T>::TCVar(T& value, std::string_view name, std::string_view description, EFlags flags)
: CVar(name, description, flags)
, m_value(value)
, m_defaultValue(value)
2015-12-02 21:11:10 +00:00
{
2018-01-15 12:34:02 +00:00
}
2015-12-02 21:11:10 +00:00
2018-01-15 12:34:02 +00:00
template<typename T>
std::string TCVar<T>::toString() const
{
std::stringstream ss;
ss << std::boolalpha << m_value;
return ss.str();
}
2015-12-02 21:11:10 +00:00
2018-01-15 12:34:02 +00:00
template<typename T>
std::wstring TCVar<T>::toWideString() const
{
return hecl::UTF8ToWide(toString());
}
2015-12-02 21:11:10 +00:00
2018-01-15 12:34:02 +00:00
template<typename T>
bool TCVar<T>::_fromString(std::string_view v)
{
T tmp;
std::stringstream ss;
ss << std::boolalpha << v;
ss >> tmp;
if (ss.good())
m_value = tmp;
return ss.good();
2015-12-02 21:11:10 +00:00
}
2018-01-15 12:34:02 +00:00
template<typename T>
void TCVar<T>::deserialize(athena::io::YAMLDocReader &reader)
2015-12-02 21:11:10 +00:00
{
2018-01-15 12:34:02 +00:00
m_value = reader.readVal<T>(m_name.c_str());
}
2015-12-02 21:11:10 +00:00
2018-01-15 12:34:02 +00:00
template<typename T>
void TCVar<T>::serialize(athena::io::YAMLDocWriter& writer) const
{
writer.writeVal(m_name.c_str(), m_value);
2015-12-02 21:11:10 +00:00
}
2018-01-15 12:34:02 +00:00
template<typename T>
bool TCVar<T>::hasDefaultValue() const
2015-12-02 21:11:10 +00:00
{
2018-01-15 12:34:02 +00:00
return m_value == m_defaultValue;
}
2015-12-02 21:11:10 +00:00
2018-01-15 12:34:02 +00:00
template<typename T>
T TCVar<T>::value() const
{
2015-12-02 21:11:10 +00:00
return m_value;
}
2018-01-15 12:34:02 +00:00
template<typename T>
T TCVar<T>::defaultValue() const
2015-12-02 21:11:10 +00:00
{
2018-01-15 12:34:02 +00:00
return m_defaultValue;
2015-12-02 21:11:10 +00:00
}
2018-01-15 12:34:02 +00:00
template<typename T>
std::string TCVar<T>::defaultValueString() const
2015-12-02 21:11:10 +00:00
{
2018-01-15 12:34:02 +00:00
std::stringstream ss;
ss << std::boolalpha << m_defaultValue;
return ss.str();
}
2015-12-02 21:11:10 +00:00
2018-01-15 12:34:02 +00:00
Vec3fCVar::Vec3fCVar(atVec3f &value, std::string_view name, std::string_view description, CVar::EFlags flags)
: CVar(name, description, flags)
, m_value(value)
, m_defaultValue(value)
{}
2015-12-02 21:11:10 +00:00
2018-01-15 12:34:02 +00:00
std::string Vec3fCVar::toString() const
{
return athena::utility::sprintf("%f %f %f", double(m_value.vec[0]), double(m_value.vec[1]), double(m_value.vec[2]));
2015-12-02 21:11:10 +00:00
}
2018-01-15 12:34:02 +00:00
bool Vec3fCVar::_fromString(std::string_view v)
2015-12-02 21:11:10 +00:00
{
2018-01-15 12:34:02 +00:00
float x, y, z;
if (std::sscanf(v.data(), "%f %f %f", &x, &y, &z) != 3)
2015-12-02 21:11:10 +00:00
return false;
2018-01-15 12:34:02 +00:00
m_value.vec[0] = x;
m_value.vec[1] = y;
m_value.vec[2] = y;
2015-12-02 21:11:10 +00:00
return true;
}
2018-01-15 12:34:02 +00:00
bool Vec3fCVar::hasDefaultValue() const
2015-12-02 21:11:10 +00:00
{
2018-01-15 12:34:02 +00:00
return !memcmp(&m_value.vec, &m_defaultValue.vec[0], sizeof(atVec3f));
}
2015-12-02 21:11:10 +00:00
2018-01-15 12:34:02 +00:00
void Vec3fCVar::deserialize(athena::io::YAMLDocReader& reader)
{
m_value = reader.readVec3f(m_name.c_str());
}
2015-12-02 21:11:10 +00:00
2018-01-15 12:34:02 +00:00
void Vec3fCVar::serialize(athena::io::YAMLDocWriter& writer) const
{
writer.writeVec3f(m_name.c_str(), m_value);
2015-12-02 21:11:10 +00:00
}
2018-01-15 12:34:02 +00:00
std::string Vec3fCVar::defaultValueString() const
2015-12-02 21:11:10 +00:00
{
2018-01-15 12:34:02 +00:00
return athena::utility::sprintf("%f %f %f", double(m_defaultValue.vec[0]), double(m_defaultValue.vec[1]), double(m_defaultValue.vec[2]));
}
2015-12-02 21:11:10 +00:00
2018-01-15 12:34:02 +00:00
Vec3dCVar::Vec3dCVar(atVec3d &value, std::string_view name, std::string_view description, CVar::EFlags flags)
: CVar(name, description, flags)
, m_value(value)
, m_defaultValue(value)
{}
2015-12-02 21:11:10 +00:00
2018-01-15 12:34:02 +00:00
std::string Vec3dCVar::toString() const
{
return athena::utility::sprintf("%lf %lf %lf", m_value.vec[0], m_value.vec[1], m_value.vec[2]);
}
2015-12-02 21:11:10 +00:00
2018-01-15 12:34:02 +00:00
bool Vec3dCVar::_fromString(std::string_view v)
{
double x, y, z;
if (std::sscanf(v.data(), "%lf %lf %lf", &x, &y, &z) != 3)
return false;
m_value.vec[0] = x;
m_value.vec[1] = y;
m_value.vec[2] = y;
2015-12-02 21:11:10 +00:00
return true;
}
2018-01-15 12:34:02 +00:00
bool Vec3dCVar::hasDefaultValue() const
2015-12-02 21:11:10 +00:00
{
2018-01-15 12:34:02 +00:00
return !memcmp(&m_value.vec, &m_defaultValue.vec[0], sizeof(atVec3d));
}
2015-12-02 21:11:10 +00:00
2018-01-15 12:34:02 +00:00
void Vec3dCVar::deserialize(athena::io::YAMLDocReader& reader)
{
m_value = reader.readVec3d(m_name.c_str());
}
2015-12-02 21:11:10 +00:00
2018-01-15 12:34:02 +00:00
void Vec3dCVar::serialize(athena::io::YAMLDocWriter& writer) const
{
writer.writeVec3d(m_name.c_str(), m_value);
}
2015-12-02 21:11:10 +00:00
2018-01-15 12:34:02 +00:00
std::string Vec3dCVar::defaultValueString() const
{
return athena::utility::sprintf("%g %g %g", m_defaultValue.vec[0], m_defaultValue.vec[1], m_defaultValue.vec[2]);
2015-12-02 21:11:10 +00:00
}
2018-01-15 12:34:02 +00:00
Vec4fCVar::Vec4fCVar(atVec4f &value, std::string_view name, std::string_view description, CVar::EFlags flags)
: CVar(name, description, flags)
, m_value(value)
, m_defaultValue(value)
{}
std::string Vec4fCVar::toString() const
2015-12-02 21:11:10 +00:00
{
2018-01-15 12:34:02 +00:00
return athena::utility::sprintf("%g %g %g %g", &m_value.vec[0], &m_value.vec[1], &m_value.vec[2], &m_value.vec[3]);
}
2015-12-02 21:11:10 +00:00
2018-01-15 12:34:02 +00:00
bool Vec4fCVar::_fromString(std::string_view v)
{
float x, y, z, w;
if (std::sscanf(v.data(), "%f %f %f %f", &x, &y, &z, &w) != 4)
2015-12-02 21:11:10 +00:00
return false;
2018-01-15 12:34:02 +00:00
m_value.vec[0] = x;
m_value.vec[1] = y;
m_value.vec[2] = y;
m_value.vec[3] = w;
return true;
}
2015-12-02 21:11:10 +00:00
2018-01-15 12:34:02 +00:00
bool Vec4fCVar::hasDefaultValue() const
{
return !memcmp(&m_value.vec, &m_defaultValue.vec[0], sizeof(atVec4f));
}
2015-12-02 21:11:10 +00:00
2018-01-15 12:34:02 +00:00
void Vec4fCVar::deserialize(athena::io::YAMLDocReader& reader)
{
m_value = reader.readVec4f(m_name.c_str());
2015-12-02 21:11:10 +00:00
}
2018-01-15 12:34:02 +00:00
void Vec4fCVar::serialize(athena::io::YAMLDocWriter& writer) const
{
2018-01-15 12:34:02 +00:00
writer.writeVec4f(m_name.c_str(), m_value);
}
2018-01-15 12:34:02 +00:00
std::string Vec4fCVar::defaultValueString() const
{
2018-01-15 12:34:02 +00:00
return athena::utility::sprintf("%f %f %f", &m_defaultValue.vec[0], &m_defaultValue.vec[1], &m_defaultValue.vec[2]);
}
2018-01-15 12:34:02 +00:00
Vec4dCVar::Vec4dCVar(atVec4d &value, std::string_view name, std::string_view description, CVar::EFlags flags)
: CVar(name, description, flags)
, m_value(value)
, m_defaultValue(value)
{}
2015-12-02 21:11:10 +00:00
2018-01-15 12:34:02 +00:00
std::string Vec4dCVar::toString() const
{
return athena::utility::sprintf("%f %f %f %f", m_value.vec[0], m_value.vec[1], m_value.vec[2], m_value.vec[3]);
}
2015-12-02 21:11:10 +00:00
2018-01-15 12:34:02 +00:00
bool Vec4dCVar::_fromString(std::string_view v)
{
double x, y, z, w;
if (std::sscanf(v.data(), "%lf %lf %lf %lf", &x, &y, &z, &w) != 4)
return false;
m_value.vec[0] = x;
m_value.vec[1] = y;
m_value.vec[2] = y;
m_value.vec[3] = w;
return true;
}
2015-12-02 21:11:10 +00:00
2018-01-15 12:34:02 +00:00
bool Vec4dCVar::hasDefaultValue() const
{
return !memcmp(&m_value.vec, &m_defaultValue.vec[0], sizeof(atVec4d));
}
2015-12-02 21:11:10 +00:00
2018-01-15 12:34:02 +00:00
void Vec4dCVar::deserialize(athena::io::YAMLDocReader& reader)
{
m_value = reader.readVec4d(m_name.c_str());
}
2015-12-02 21:11:10 +00:00
2018-01-15 12:34:02 +00:00
void Vec4dCVar::serialize(athena::io::YAMLDocWriter& writer) const
{
writer.writeVec4d(m_name.c_str(), m_value);
}
2018-01-14 07:37:00 +00:00
2018-01-15 12:34:02 +00:00
std::string Vec4dCVar::defaultValueString() const
{
return athena::utility::sprintf("%g %g %g", m_defaultValue.vec[0], m_defaultValue.vec[1], m_defaultValue.vec[2]);
}
2018-01-14 07:37:00 +00:00
2018-01-15 12:34:02 +00:00
StringCVar::StringCVar(std::string& value, std::string_view name, std::string_view help, CVar::EFlags flags)
: CVar(name, help, flags)
, m_value(value)
, m_defaultValue(value)
{
}
2018-01-14 07:37:00 +00:00
2018-01-15 12:34:02 +00:00
std::string StringCVar::toString() const
{
2018-01-15 12:34:02 +00:00
return m_value;
}
2015-12-02 21:11:10 +00:00
2018-01-15 12:34:02 +00:00
bool StringCVar::_fromString(std::string_view v)
{
m_value = v;
return true;
}
2015-12-02 21:11:10 +00:00
2018-01-15 12:34:02 +00:00
bool StringCVar::hasDefaultValue() const
2015-12-02 21:11:10 +00:00
{
2018-01-15 12:34:02 +00:00
return m_value == m_defaultValue;
2015-12-02 21:11:10 +00:00
}
2018-01-15 12:34:02 +00:00
void StringCVar::deserialize(athena::io::YAMLDocReader& reader)
2015-12-02 21:11:10 +00:00
{
2018-01-15 12:34:02 +00:00
m_value = reader.readString(m_name.c_str());
2015-12-02 21:11:10 +00:00
}
2018-01-15 12:34:02 +00:00
void StringCVar::serialize(athena::io::YAMLDocWriter& writer) const
2015-12-02 21:11:10 +00:00
{
2018-01-15 12:34:02 +00:00
writer.writeString(m_name.c_str(), m_value.c_str());
2015-12-02 21:11:10 +00:00
}
2018-01-15 12:34:02 +00:00
template class TCVar<bool>;
template class TCVar<int16_t>;
template class TCVar<uint16_t>;
template class TCVar<int32_t>;
template class TCVar<uint32_t>;
template class TCVar<int64_t>;
template class TCVar<uint64_t>;
template class TCVar<float>;
template class TCVar<double>;
2015-12-02 21:11:10 +00:00
}