metaforce/hecl/lib/CVar.cpp

557 lines
13 KiB
C++
Raw Normal View History

#include "hecl/CVar.hpp"
#include <sstream>
2018-01-22 02:40:11 +00:00
#include "hecl/CVarManager.hpp"
#include "hecl/hecl.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
2018-12-08 05:18:42 +00:00
namespace hecl {
2018-01-22 02:40:11 +00:00
extern CVar* com_developer;
extern CVar* com_enableCheats;
2017-11-13 06:13:53 +00:00
using namespace std::literals;
CVar::CVar(std::string_view name, std::string_view value, std::string_view help, CVar::EFlags flags)
: CVar(name, help, EType::Literal) {
fromLiteral(value);
init(flags);
2015-12-02 21:11:10 +00:00
}
CVar::CVar(std::string_view name, const atVec2f& value, std::string_view help, EFlags flags)
: CVar(name, help, EType::Vec2f) {
fromVec2f(value);
init(flags);
}
2016-01-04 05:25:00 +00:00
CVar::CVar(std::string_view name, const atVec2d& value, std::string_view help, EFlags flags)
: CVar(name, help, EType::Vec2d) {
fromVec2d(value);
2015-12-02 21:11:10 +00:00
init(flags);
2015-12-02 21:11:10 +00:00
}
CVar::CVar(std::string_view name, const atVec3f& value, std::string_view help, EFlags flags)
: CVar(name, help, EType::Vec3f) {
fromVec3f(value);
init(flags, false);
}
2018-12-08 05:18:42 +00:00
CVar::CVar(std::string_view name, const atVec3d& value, std::string_view help, EFlags flags)
: CVar(name, help, EType::Vec3d) {
fromVec3d(value);
init(flags, false);
}
2018-12-08 05:18:42 +00:00
CVar::CVar(std::string_view name, const atVec4f& value, std::string_view help, EFlags flags)
: CVar(name, help, EType::Vec4f) {
2018-12-08 05:18:42 +00:00
fromVec4f(value);
init(flags, false);
}
2018-12-08 05:18:42 +00:00
CVar::CVar(std::string_view name, const atVec4d& value, std::string_view help, EFlags flags)
: CVar(name, help, EType::Vec4d) {
fromVec4d(value);
init(flags, false);
2018-01-22 02:40:11 +00:00
}
2015-12-02 21:11:10 +00:00
CVar::CVar(std::string_view name, double value, std::string_view help, EFlags flags) : CVar(name, help, EType::Real) {
fromReal(value);
init(flags);
}
2018-12-08 05:18:42 +00:00
CVar::CVar(std::string_view name, bool value, std::string_view help, CVar::EFlags flags)
: CVar(name, help, EType::Boolean) {
fromBoolean(value);
init(flags);
}
2018-12-08 05:18:42 +00:00
CVar::CVar(std::string_view name, int32_t value, std::string_view help, CVar::EFlags flags)
: CVar(name, help, EType::Signed) {
fromInteger(value);
init(flags);
}
2018-12-08 05:18:42 +00:00
CVar::CVar(std::string_view name, uint32_t value, std::string_view help, CVar::EFlags flags)
: CVar(name, help, EType::Unsigned) {
fromInteger(value);
init(flags);
2015-12-02 21:11:10 +00:00
}
std::string CVar::help() const {
return m_help + (m_defaultValue.empty() ? "" : "\ndefault: " + m_defaultValue) + (isReadOnly() ? " [ReadOnly]" : "");
}
2018-12-08 05:18:42 +00:00
atVec2f CVar::toVec2f(bool* isValid) const {
if (m_type != EType::Vec2f) {
if (isValid != nullptr)
*isValid = false;
2018-12-08 05:18:42 +00:00
return atVec2f{};
}
2018-12-08 05:18:42 +00:00
if (isValid != nullptr)
*isValid = true;
atVec2f vec{};
athena::simd_floats f;
std::sscanf(m_value.c_str(), "%g %g", &f[0], &f[1]);
vec.simd.copy_from(f);
return vec;
2018-01-15 12:34:02 +00:00
}
2015-12-02 21:11:10 +00:00
atVec2d CVar::toVec2d(bool* isValid) const {
if (m_type != EType::Vec2d) {
if (isValid != nullptr)
*isValid = false;
2018-12-08 05:18:42 +00:00
return atVec2d{};
}
2018-12-08 05:18:42 +00:00
if (isValid != nullptr)
*isValid = true;
2018-12-08 05:18:42 +00:00
atVec2d vec{};
athena::simd_doubles f;
std::sscanf(m_value.c_str(), "%lg %lg", &f[0], &f[1]);
vec.simd.copy_from(f);
return vec;
2018-01-15 12:34:02 +00:00
}
2015-12-02 21:11:10 +00:00
atVec3f CVar::toVec3f(bool* isValid) const {
if (m_type != EType::Vec3f) {
if (isValid != nullptr)
*isValid = false;
return atVec3f{};
}
if (isValid != nullptr)
*isValid = true;
atVec3f vec{};
athena::simd_floats f;
std::sscanf(m_value.c_str(), "%g %g %g", &f[0], &f[1], &f[2]);
vec.simd.copy_from(f);
return vec;
}
atVec3d CVar::toVec3d(bool* isValid) const {
if (m_type != EType::Vec3d) {
if (isValid != nullptr)
*isValid = false;
return atVec3d{};
}
if (isValid != nullptr)
*isValid = true;
atVec3d vec{};
athena::simd_doubles f;
std::sscanf(m_value.c_str(), "%lg %lg %lg", &f[0], &f[1], &f[2]);
vec.simd.copy_from(f);
return vec;
2015-12-02 21:11:10 +00:00
}
2018-12-08 05:18:42 +00:00
atVec4f CVar::toVec4f(bool* isValid) const {
if (m_type != EType::Vec4f) {
if (isValid != nullptr)
*isValid = false;
2018-01-15 12:34:02 +00:00
2018-12-08 05:18:42 +00:00
return atVec4f{};
}
2015-12-02 21:11:10 +00:00
2018-12-08 05:18:42 +00:00
if (isValid != nullptr)
*isValid = true;
2018-01-22 02:40:11 +00:00
atVec4f vec{};
2018-12-08 05:18:42 +00:00
athena::simd_floats f;
std::sscanf(m_value.c_str(), "%g %g %g %g", &f[0], &f[1], &f[2], &f[3]);
2018-12-08 05:18:42 +00:00
vec.simd.copy_from(f);
2018-01-22 02:40:11 +00:00
2018-12-08 05:18:42 +00:00
return vec;
2015-12-02 21:11:10 +00:00
}
atVec4d CVar::toVec4d(bool* isValid) const {
if (m_type != EType::Vec4d) {
if (isValid != nullptr)
*isValid = false;
return atVec4d{};
}
if (isValid != nullptr)
*isValid = true;
atVec4d vec{};
athena::simd_doubles f;
std::sscanf(m_value.c_str(), "%lg %lg %lg %lg", &f[0], &f[1], &f[2], &f[3]);
vec.simd.copy_from(f);
return vec;
}
double CVar::toReal(bool* isValid) const {
if (m_type != EType::Real) {
2018-12-08 05:18:42 +00:00
if (isValid)
*isValid = false;
return 0.0f;
}
2018-01-22 02:40:11 +00:00
if (isValid != nullptr)
*isValid = true;
return strtod(m_value.c_str(), nullptr);
2015-12-02 21:11:10 +00:00
}
2018-12-08 05:18:42 +00:00
bool CVar::toBoolean(bool* isValid) const {
if (m_type != EType::Boolean) {
2018-01-22 02:40:11 +00:00
if (isValid)
2018-12-08 05:18:42 +00:00
*isValid = false;
2015-12-02 21:11:10 +00:00
2018-01-22 02:40:11 +00:00
return false;
2018-12-08 05:18:42 +00:00
}
if (isValid != nullptr)
*isValid = true;
2018-12-08 05:18:42 +00:00
return athena::utility::parseBool(m_value);
}
int32_t CVar::toSigned(bool* isValid) const {
if (m_type != EType::Signed && m_type != EType::Unsigned) {
2018-12-08 05:18:42 +00:00
if (isValid)
*isValid = false;
return 0;
2018-12-08 05:18:42 +00:00
}
if (isValid != nullptr)
*isValid = true;
2018-12-08 05:18:42 +00:00
return strtol(m_value.c_str(), nullptr, 0);
2018-01-15 12:34:02 +00:00
}
2015-12-02 21:11:10 +00:00
uint32_t CVar::toUnsigned(bool* isValid) const {
if (m_type != EType::Signed && m_type != EType::Unsigned) {
2018-12-08 05:18:42 +00:00
if (isValid)
*isValid = false;
return 0;
}
2018-01-22 02:40:11 +00:00
if (isValid != nullptr)
*isValid = true;
return strtoul(m_value.c_str(), nullptr, 0);
2015-12-02 21:11:10 +00:00
}
std::string CVar::toLiteral(bool* isValid) const {
2018-12-08 05:18:42 +00:00
if (m_type != EType::Literal && (com_developer && com_developer->toBoolean())) {
if (isValid != nullptr)
*isValid = false;
} else if (isValid != nullptr) {
2018-12-08 05:18:42 +00:00
*isValid = true;
}
2018-12-08 05:18:42 +00:00
// Even if it's not a literal, it's still safe to return
return m_value;
2018-01-22 02:40:11 +00:00
}
2015-12-02 21:11:10 +00:00
std::wstring CVar::toWideLiteral(bool* isValid) const {
2018-12-08 05:18:42 +00:00
if (m_type != EType::Literal && (com_developer && com_developer->toBoolean())) {
if (isValid != nullptr)
*isValid = false;
} else if (isValid != nullptr) {
2018-12-08 05:18:42 +00:00
*isValid = true;
}
2018-12-08 05:18:42 +00:00
// Even if it's not a literal, it's still safe to return
return hecl::UTF8ToWide(m_value);
2018-01-15 12:34:02 +00:00
}
2015-12-02 21:11:10 +00:00
bool CVar::fromVec2f(const atVec2f& val) {
if (!safeToModify(EType::Vec2f))
2018-12-08 05:18:42 +00:00
return false;
2015-12-02 21:11:10 +00:00
athena::simd_floats f(val.simd);
2020-04-11 22:48:11 +00:00
m_value.assign(fmt::format(FMT_STRING("{} {}"), f[0], f[1]));
m_flags |= EFlags::Modified;
return true;
}
bool CVar::fromVec2d(const atVec2d& val) {
if (!safeToModify(EType::Vec2d))
2018-12-08 05:18:42 +00:00
return false;
2015-12-02 21:11:10 +00:00
athena::simd_doubles f(val.simd);
2020-04-11 22:48:11 +00:00
m_value.assign(fmt::format(FMT_STRING("{} {}"), f[0], f[1]));
m_flags |= EFlags::Modified;
return true;
}
bool CVar::fromVec3f(const atVec3f& val) {
if (!safeToModify(EType::Vec3f))
2018-12-08 05:18:42 +00:00
return false;
2015-12-02 21:11:10 +00:00
2018-12-08 05:18:42 +00:00
athena::simd_floats f(val.simd);
2020-04-11 22:48:11 +00:00
m_value.assign(fmt::format(FMT_STRING("{} {} {}"), f[0], f[1], f[2]));
2018-12-08 05:18:42 +00:00
m_flags |= EFlags::Modified;
return true;
2018-01-15 12:34:02 +00:00
}
2015-12-02 21:11:10 +00:00
bool CVar::fromVec3d(const atVec3d& val) {
if (!safeToModify(EType::Vec3d))
2018-12-08 05:18:42 +00:00
return false;
athena::simd_doubles f(val.simd);
2020-04-11 22:48:11 +00:00
m_value.assign(fmt::format(FMT_STRING("{} {} {}"), f[0], f[1], f[2]));
m_flags |= EFlags::Modified;
return true;
}
bool CVar::fromVec4f(const atVec4f& val) {
if (!safeToModify(EType::Vec4f))
2018-12-08 05:18:42 +00:00
return false;
2015-12-02 21:11:10 +00:00
athena::simd_floats f(val.simd);
2020-04-11 22:48:11 +00:00
m_value.assign(fmt::format(FMT_STRING("{} {} {} {}"), f[0], f[1], f[2], f[3]));
m_flags |= EFlags::Modified;
return true;
}
bool CVar::fromVec4d(const atVec4d& val) {
if (!safeToModify(EType::Vec4d))
2018-12-08 05:18:42 +00:00
return false;
2018-01-15 12:34:02 +00:00
athena::simd_doubles f(val.simd);
2020-04-11 22:48:11 +00:00
m_value.assign(fmt::format(FMT_STRING("{} {} {} {}"), f[0], f[1], f[2], f[3]));
m_flags |= EFlags::Modified;
return true;
}
bool CVar::fromReal(double val) {
if (!safeToModify(EType::Real))
2018-12-08 05:18:42 +00:00
return false;
2018-01-22 02:40:11 +00:00
2020-04-11 22:48:11 +00:00
m_value.assign(fmt::format(FMT_STRING("{}"), val));
2018-12-08 05:18:42 +00:00
setModified();
return true;
2018-01-15 12:34:02 +00:00
}
2015-12-02 21:11:10 +00:00
2018-12-08 05:18:42 +00:00
bool CVar::fromBoolean(bool val) {
if (!safeToModify(EType::Boolean))
2018-12-08 05:18:42 +00:00
return false;
2018-01-22 02:40:11 +00:00
2018-12-08 05:18:42 +00:00
if (val)
m_value = "true"sv;
else
m_value = "false"sv;
2018-01-22 02:40:11 +00:00
2018-12-08 05:18:42 +00:00
setModified();
return true;
2018-01-15 12:34:02 +00:00
}
2015-12-02 21:11:10 +00:00
bool CVar::fromInteger(int32_t val) {
if ((com_developer && com_enableCheats) && (!com_developer->toBoolean() || !com_enableCheats->toBoolean()) &&
isCheat())
2018-12-08 05:18:42 +00:00
return false;
2015-12-02 21:11:10 +00:00
// We'll accept both signed an unsigned input
if (m_type != EType::Signed && m_type != EType::Unsigned)
2018-12-08 05:18:42 +00:00
return false;
2015-12-02 21:11:10 +00:00
2018-12-08 05:18:42 +00:00
if (isReadOnly() && (com_developer && !com_developer->toBoolean()))
return false;
2018-01-22 02:40:11 +00:00
// Properly format based on signedness
2020-04-11 22:48:11 +00:00
m_value = fmt::format(FMT_STRING("{}"), (m_type == EType::Signed ? val : static_cast<uint32_t>(val)));
2018-12-08 05:18:42 +00:00
setModified();
return true;
}
bool CVar::fromInteger(uint32_t val) {
if ((com_developer && com_enableCheats) && (!com_developer->toBoolean() || !com_enableCheats->toBoolean()) &&
isCheat())
2018-12-08 05:18:42 +00:00
return false;
// We'll accept both signed an unsigned input
if (m_type != EType::Signed && m_type != EType::Unsigned)
2018-12-08 05:18:42 +00:00
return false;
2015-12-02 21:11:10 +00:00
2018-12-08 05:18:42 +00:00
if (isReadOnly() && (com_developer && !com_developer->toBoolean()))
return false;
2018-01-22 02:40:11 +00:00
// Properly format based on signedness
2020-04-11 22:48:11 +00:00
m_value = fmt::format(FMT_STRING("{}"), (m_type == EType::Unsigned ? val : static_cast<int32_t>(val)));
2018-12-08 05:18:42 +00:00
setModified();
return true;
2018-01-15 12:34:02 +00:00
}
2015-12-02 21:11:10 +00:00
bool CVar::fromLiteral(std::string_view val) {
if (!safeToModify(EType::Literal))
2018-12-08 05:18:42 +00:00
return false;
2018-01-22 02:40:11 +00:00
m_value.assign(val);
setModified();
return true;
}
2018-01-22 02:40:11 +00:00
bool CVar::fromLiteral(std::wstring_view val) {
if (!safeToModify(EType::Literal))
2018-12-08 05:18:42 +00:00
return false;
2018-01-22 02:40:11 +00:00
2018-12-08 05:18:42 +00:00
m_value.assign(hecl::WideToUTF8(val));
setModified();
return true;
2018-01-15 12:34:02 +00:00
}
2015-12-02 21:11:10 +00:00
bool CVar::fromLiteralToType(std::string_view val) {
if (!safeToModify(m_type) || !isValidInput(val))
return false;
m_value = val;
2019-09-29 15:17:01 +00:00
setModified();
return true;
2018-01-15 12:34:02 +00:00
}
2015-12-02 21:11:10 +00:00
bool CVar::fromLiteralToType(std::wstring_view val) {
return fromLiteralToType(hecl::WideToUTF8(val));
2018-01-15 12:34:02 +00:00
}
2015-12-02 21:11:10 +00:00
bool CVar::isModified() const { return True(m_flags & EFlags::Modified); }
bool CVar::modificationRequiresRestart() const { return True(m_flags & EFlags::ModifyRestart); }
2018-01-14 07:37:00 +00:00
bool CVar::isReadOnly() const { return True(m_flags & EFlags::ReadOnly); }
2018-01-14 07:37:00 +00:00
bool CVar::isCheat() const { return True(m_flags & EFlags::Cheat); }
2018-01-14 07:37:00 +00:00
bool CVar::isHidden() const { return True(m_flags & EFlags::Hidden); }
2018-01-22 02:40:11 +00:00
bool CVar::isArchive() const { return True(m_flags & EFlags::Archive); }
bool CVar::isInternalArchivable() const { return True(m_flags & EFlags::InternalArchivable); }
bool CVar::isColor() const {
return True(m_flags & EFlags::Color) &&
(m_type == EType::Vec3f || m_type == EType::Vec3d || m_type == EType::Vec3f || m_type == EType::Vec4d);
}
2018-01-22 02:40:11 +00:00
bool CVar::isNoDeveloper() const { return True(m_flags & EFlags::NoDeveloper); }
2015-12-02 21:11:10 +00:00
2018-01-22 02:40:11 +00:00
bool CVar::wasDeserialized() const { return m_wasDeserialized; }
bool CVar::hasDefaultValue() const { return m_defaultValue == m_value; }
2018-12-08 05:18:42 +00:00
void CVar::clearModified() {
if (!modificationRequiresRestart())
m_flags &= ~EFlags::Modified;
2018-01-15 12:34:02 +00:00
}
2015-12-02 21:11:10 +00:00
2018-01-22 02:40:11 +00:00
void CVar::setModified() { m_flags |= EFlags::Modified; }
2018-12-08 05:18:42 +00:00
void CVar::unlock() {
if (isReadOnly() && !m_unlocked) {
m_oldFlags = m_flags;
m_flags &= ~EFlags::ReadOnly;
m_unlocked = true;
}
2015-12-02 21:11:10 +00:00
}
2018-12-08 05:18:42 +00:00
void CVar::lock() {
if (!isReadOnly() && m_unlocked) {
m_flags = m_oldFlags;
m_unlocked = false;
}
2015-12-02 21:11:10 +00:00
}
2018-12-08 05:18:42 +00:00
void CVar::dispatch() {
for (const ListenerFunc& listen : m_listeners)
listen(this);
2015-12-02 21:11:10 +00:00
}
bool isReal(std::string_view v) {
char* p;
std::strtod(v.data(), &p);
return *p == 0;
}
bool isReal(const std::vector<std::string>& v) {
for (auto& s : v) {
if (!isReal(s))
return false;
}
return true;
}
bool CVar::isValidInput(std::string_view input) const {
std::vector<std::string> parts = athena::utility::split(input, ' ');
char* p;
switch(m_type) {
case EType::Boolean: {
bool valid = false;
athena::utility::parseBool(input, &valid);
return valid;
}
case EType::Signed:
std::strtol(input.data(), &p, 0);
return p == nullptr;
case EType::Unsigned:
std::strtoul(input.data(), &p, 0);
return p == nullptr;
case EType::Real: {
bool size = parts.size() == 1;
bool ret = isReal(input);
return ret && size;
}
case EType::Literal:
return true;
case EType::Vec2f:
case EType::Vec2d:
return parts.size() == 2 && isReal(parts);
case EType::Vec3f:
case EType::Vec3d:
return parts.size() == 3 && isReal(parts);
case EType::Vec4f:
case EType::Vec4d:
return parts.size() == 4 && isReal(parts);
}
return false;
}
bool CVar::isValidInput(std::wstring_view input) const {
return isValidInput(hecl::WideToUTF8(input));
}
bool CVar::safeToModify(EType type) const {
// Are we NoDevelper?
if (isNoDeveloper())
return false;
// Are we a cheat?
if (isCheat() && (com_developer && com_enableCheats) &&
(!com_developer->toBoolean() || !com_enableCheats->toBoolean()))
return false;
// Are we read only?
if (isReadOnly() && (com_developer && !com_developer->toBoolean()))
return false;
return m_type == type;
}
void CVar::init(EFlags flags, bool removeColor) {
m_defaultValue = m_value;
m_flags = flags;
if (removeColor) {
// If the user specifies color, we don't want it
m_flags &= ~EFlags::Color;
}
}
2018-12-08 05:18:42 +00:00
} // namespace hecl