Moved CVars to HECL

This commit is contained in:
Jack Andersen 2015-12-02 11:14:31 -10:00
parent 28a2778919
commit 04fa61e0c2
10 changed files with 16 additions and 836 deletions

View File

@ -1,6 +1,5 @@
make_dnalist(liblist
CMDL
CVar)
CMDL)
add_library(DNACommon
DNACommon.hpp DNACommon.cpp

View File

@ -1,55 +0,0 @@
#ifndef _DNACOMMON_CVAR_HPP_
#define _DNACOMMON_CVAR_HPP_
#include <Athena/Global.hpp>
#include "DNACommon.hpp"
namespace Retro
{
namespace DNACVAR
{
enum class EType : atUint8
{
Boolean,
Integer,
Float,
Literal,
Color
};
enum EFlags
{
All = -1, // NOTE: is this really necessary? It seems rather overkill
System = (1 << 0),
Game = (1 << 1),
Editor = (1 << 2),
Gui = (1 << 3),
Cheat = (1 << 4),
Hidden = (1 << 5),
ReadOnly = (1 << 6),
Archive = (1 << 7),
Modified = (1 << 8)
};
ENABLE_BITWISE_ENUM(EFlags)
class CVar : BigYAML
{
public:
DECL_YAML
String<-1> m_name;
String<-1> m_value;
Value<EType> m_type;
};
struct CVarContainer : BigYAML
{
DECL_YAML
Value<atUint32> magic = 'CVAR';
Value<atUint32> cvarCount;
Vector<CVar, DNA_COUNT(cvarCount)> cvars;
};
}
}
#endif

View File

@ -1,7 +1,7 @@
#include <LogVisor/LogVisor.hpp>
#include <boo/boo.hpp>
#include <Specter/Specter.hpp>
#include <Runtime/CVarManager.hpp>
#include <HECL/CVarManager.hpp>
#include <Runtime/CGameAllocator.hpp>
#include <functional>
@ -14,16 +14,9 @@ struct Application : boo::IApplicationCallback
boo::IWindow* m_mainWindow;
Specter::ViewResources m_viewResources;
std::unique_ptr<Specter::RootView> m_rootView;
Retro::CVarManager m_cvarManager;
Zeus::CColor m_clearColor;
HECL::CVarManager m_cvarManager;
bool m_running = true;
void onCVarModified(Retro::CVar* cvar)
{
if (cvar == m_cvarManager.findCVar("r_clearColor"))
m_clearColor = cvar->toColor();
}
Application() :
m_fileMgr(_S("rude")),
m_fontCache(m_fileMgr),
@ -35,13 +28,13 @@ struct Application : boo::IApplicationCallback
m_mainWindow->showWindow();
m_mainWindow->setWaitCursor(true);
m_cvarManager.serialize();
Retro::CVar* tmp = m_cvarManager.findCVar("r_clearcolor");
Retro::CVar::ListenerFunc listen = std::bind(&Application::onCVarModified, this, std::placeholders::_1);
if (tmp)
tmp->addListener(listen);
unsigned dpi = m_mainWindow->getVirtualPixelFactor() * 72;
HECL::CVar* cvDPI = m_cvarManager.newCVar("ed_dpi", "User-selected UI DPI",
int(dpi), HECL::CVar::EFlags::Editor);
boo::IGraphicsDataFactory* gf = m_mainWindow->getMainContextDataFactory();
m_viewResources.init(gf, &m_fontCache, m_mainWindow->getVirtualPixelFactor());
m_viewResources.init(gf, &m_fontCache, dpi);
m_rootView.reset(new Specter::RootView(m_viewResources, m_mainWindow));
m_mainWindow->setWaitCursor(false);
@ -50,7 +43,12 @@ struct Application : boo::IApplicationCallback
{
if (m_rootView->isDestroyed())
break;
m_cvarManager.update();
if (cvDPI->isModified())
{
dpi = cvDPI->toInteger();
m_viewResources.init(gf, &m_fontCache, dpi);
cvDPI->clearModified();
}
m_rootView->dispatchEvents();
m_rootView->draw(gfxQ);
gfxQ->execute();

View File

@ -67,8 +67,6 @@ add_library(RuntimeCommon
CGameDebug.hpp CGameDebug.cpp
rstl.hpp rstl.cpp
GameGlobalObjects.hpp
CVar.hpp CVar.cpp
CVarManager.hpp CVarManager.cpp
RetroTypes.hpp
GCNTypes.hpp
${PLAT_SRCS})

View File

@ -1,402 +0,0 @@
#include "CVar.hpp"
#include "CVarManager.hpp"
#include "CVarManager.hpp"
#include "CBasics.hpp"
#include <Athena/Utility.hpp>
namespace Retro
{
extern CVar* com_developer;
extern CVar* com_enableCheats;
CVar::CVar(const std::string& name, const std::string &value, const std::string &help, EType type, EFlags flags, CVarManager& parent)
: m_mgr(parent)
{
m_name= name;
m_value = value;
m_defaultValue = value;
m_help = help;
m_type = type;
m_flags = flags;
m_allowedWrite = false;
}
CVar::CVar(const std::string& name, const std::string& value, const std::string& help, CVar::EFlags flags, CVarManager& parent)
: m_mgr(parent)
{
// Unlock the cvar for writing if readonly
unlock();
m_name= name;
m_help = help;
m_type = EType::Literal;
m_flags = flags;
m_allowedWrite = false;
fromLiteral(value);
m_defaultValue = m_value;
// Lock the cvar
lock();
// Clear the modified flag, just incase lock didn't do it.
m_flags = flags;
}
CVar::CVar(const std::string& name, const Zeus::CColor& value, const std::string& help, EFlags flags, CVarManager& parent)
: m_mgr(parent)
{
// Unlock the cvar for writing if readonly
unlock();
m_name= name;
m_help = help;
m_type = EType::Color;
m_flags = flags;
m_allowedWrite = false;
fromColor(value);
m_defaultValue = m_value;
// Lock the cvar
lock();
// Clear the modified flag, just incase lock didn't do it.
m_flags = flags;
}
CVar::CVar(const std::string& name, float value, const std::string& help, EFlags flags, CVarManager& parent)
: m_mgr(parent)
{
// Unlock the cvar for writing if readonly
unlock();
m_name= name;
m_help = help;
m_type = EType::Float;
m_flags = flags;
m_allowedWrite = false;
fromFloat(value);
m_defaultValue = m_value;
// Lock the cvar
lock();
// Clear the modified flag, just incase lock didn't do it.
m_flags = flags;
}
CVar::CVar(const std::string& name, bool value, const std::string& help, CVar::EFlags flags, CVarManager& parent)
: m_mgr(parent)
{
// Unlock the cvar for writing if readonly
unlock();
m_name= name;
m_help = help;
m_type = EType::Boolean;
m_flags = flags;
m_allowedWrite = false;
fromBoolean(value);
m_defaultValue = m_value;
// Lock the cvar
lock();
// Clear the modified flag, just incase lock didn't do it.
m_flags = flags;
}
CVar::CVar(const std::string& name, int value, const std::string& help, CVar::EFlags flags, CVarManager& parent)
: m_mgr(parent)
{
// Unlock the cvar for writing if readonly
unlock();
m_name= name;
m_help = help;
m_type = EType::Integer;
m_flags = flags;
m_allowedWrite = false;
fromInteger(value);
m_defaultValue = m_value;
// Lock the cvar
lock();
// Clear the modified flag, just incase lock didn't do it.
m_flags = flags;
}
std::string CVar::name() const
{
return m_name;
}
std::string CVar::help() const
{
return std::string(m_help + (m_defaultValue != std::string() ? "\ndefault: " + m_defaultValue : "") +
(isReadOnly() ? "[ReadOnly]" : ""));
}
Zeus::CColor CVar::toColor(bool* isValid) const
{
if (m_type != EType::Color)
{
if (isValid != nullptr)
*isValid = false;
return Zeus::CColor();
}
if (isValid != NULL)
*isValid = true;
int r, g, b, a;
std::sscanf(m_value.c_str(), "%i %i %i %i", &r, &g, &b, &a);
return Zeus::CColor(Zeus::Comp8(r), Zeus::Comp8(g), Zeus::Comp8(b), Zeus::Comp8(a));
}
float CVar::toFloat(bool* isValid) const
{
if (m_type != EType::Float)
{
if (isValid)
*isValid = false;
return 0.0f;
}
return strtof(m_value.c_str(), nullptr);
}
bool CVar::toBoolean(bool* isValid) const
{
if (m_type != EType::Boolean)
{
if (isValid)
*isValid = false;
return false;
}
// We don't want to modify the original value;
std::string tmp = m_value;
std::transform(tmp.begin(), tmp.end(), tmp.begin(), ::tolower);
if (!tmp.compare("yes") || !tmp.compare("true") || !tmp.compare("1"))
{
if (isValid)
*isValid = true;
return true;
}
else if (!tmp.compare("no") || !tmp.compare("false") || !tmp.compare("0"))
{
if (isValid)
*isValid = true;
return false;
}
if (isValid)
*isValid = false;
return false;
}
int CVar::toInteger(bool* isValid) const
{
if (m_type != EType::Integer)
{
if (isValid)
*isValid = false;
return 0;
}
return strtol(m_value.c_str(), nullptr, 0);
}
const std::string CVar::toLiteral(bool* isValid) const
{
if (m_type != EType::Literal && (com_developer && com_developer->toBoolean()))
{
if (isValid != nullptr)
*isValid = false;
}
else if (isValid != nullptr)
*isValid = true;
// Even if it's not a literal, it's still safe to return
return m_value;
}
const std::wstring CVar::toWideLiteral(bool* isValid) const
{
if (m_type != EType::Literal && (com_developer && com_developer->toBoolean()))
{
if (isValid != nullptr)
*isValid = false;
}
else if (isValid != nullptr)
*isValid = true;
// Even if it's not a literal, it's still safe to return
return HECL::UTF8ToWide(m_value);
}
bool CVar::fromColor(const Zeus::CColor& val)
{
if (isCheat() && (com_developer && !com_developer->toBoolean() && !com_enableCheats->toBoolean()))
return false;
else if (isCheat())
return false;
if (m_type != EType::Color)
return false;
if (isReadOnly() && (com_developer && !com_developer->toBoolean()))
return false;
m_value.assign(CBasics::Stringize("%i %i %i %i", unsigned(val.r), unsigned(val.g), unsigned(val.b), unsigned(val.a)));
m_flags |= EFlags::Modified;
return true;
}
bool CVar::fromFloat(float val)
{
if (isCheat() && (com_developer && !com_developer->toBoolean() && !com_enableCheats->toBoolean()))
return false;
else if (isCheat())
return false;
if (m_type != EType::Float)
return false;
if (isReadOnly() && (com_developer && !com_developer->toBoolean()))
return false;
m_value.assign(CBasics::Stringize("%f", val));
setModified();
return true;
}
bool CVar::fromBoolean(bool val)
{
if (isCheat() && (com_developer && !com_developer->toBoolean() && !com_enableCheats->toBoolean()))
return false;
else if (isCheat())
return false;
if (m_type != EType::Boolean)
return false;
if (isReadOnly() && (com_developer && !com_developer->toBoolean()))
return false;
if (val)
m_value = "true";
else
m_value = "false";
setModified();
return true;
}
bool CVar::fromInteger(int val)
{
if (isCheat() && (com_developer && !com_developer->toBoolean() && !com_enableCheats->toBoolean()))
return false;
else if (isCheat())
return false;
if (m_type != EType::Integer)
return false;
if (isReadOnly() && (com_developer && !com_developer->toBoolean()))
return false;
m_value = CBasics::Stringize("%i", val);
setModified();
return true;
}
bool CVar::fromLiteral(const std::string& val)
{
if (isCheat() && (com_developer && !com_developer->toBoolean() && !com_enableCheats->toBoolean()))
return false;
else if (isCheat())
return false;
if (m_type != EType::Literal)
return false;
if (isReadOnly() && (com_developer && !com_developer->toBoolean()))
return false;
m_value.assign(val);
setModified();
return true;
}
bool CVar::fromLiteral(const std::wstring& val)
{
if (isCheat() && (com_developer && !com_developer->toBoolean() && !com_enableCheats->toBoolean()))
return false;
else if (isCheat())
return false;
if (m_type != EType::Literal)
return false;
if (isReadOnly() && (com_developer && !com_developer->toBoolean()))
return false;
m_value.assign(HECL::WideToUTF8(val));
setModified();
return true;
}
bool CVar::isModified() const { return int(m_flags & EFlags::Modified) != 0;}
bool CVar::isReadOnly() const { return int(m_flags & EFlags::ReadOnly) != 0; }
bool CVar::isCheat() const { return int(m_flags & EFlags::Cheat) != 0; }
bool CVar::isHidden() const { return int(m_flags & EFlags::Hidden) != 0; }
bool CVar::isArchive() const { return int(m_flags & EFlags::Archive) != 0; }
void CVar::clearModified() { m_flags &= ~EFlags::Modified; }
void CVar::setModified() { m_flags |= EFlags::Modified; }
void CVar::unlock()
{
if (!isReadOnly())
return;
if (!m_allowedWrite)
{
m_allowedWrite = true;
m_flags &= ~EFlags::ReadOnly;
}
}
void CVar::lock()
{
if (!isReadOnly())
return;
if (m_allowedWrite)
{
m_flags |= EFlags::ReadOnly;
m_allowedWrite = false;
clearModified();
}
}
void CVar::dispatch()
{
for (const ListenerFunc& listen : m_listeners)
listen(this);
}
}

View File

@ -1,105 +0,0 @@
#ifndef CVAR_HPP
#define CVAR_HPP
#include <string>
#include <functional>
#include "CColor.hpp"
#include "DataSpec/DNACommon/CVar.hpp"
namespace Retro
{
class CVarManager;
class CVar : protected DNACVAR::CVar
{
friend class CVarManager;
public:
typedef std::function<void(CVar*)> ListenerFunc;
using EType = DNACVAR::EType;
using EFlags = DNACVAR::EFlags;
CVar(const std::string& name, const std::string& value, const std::string& help, EType type, EFlags flags, CVarManager& parent);
CVar(const std::string& name, const std::string& value, const std::string& help, EFlags flags, CVarManager& parent);
CVar(const std::string& name, const Zeus::CColor& value, const std::string& help, EFlags flags, CVarManager& parent);
CVar(const std::string& name, float value, const std::string& help, EFlags flags, CVarManager& parent);
CVar(const std::string& name, bool value, const std::string& help, EFlags flags, CVarManager& parent);
CVar(const std::string& name, int value, const std::string& help, EFlags flags, CVarManager& parent);
std::string name() const;
std::string help() const;
Zeus::CColor toColor(bool* isValid = nullptr) const;
float toFloat(bool* isValid = nullptr) const;
bool toBoolean(bool* isValid = nullptr) const;
int toInteger(bool* isValid = nullptr) const;
const std::wstring toWideLiteral(bool* isValid = nullptr) const;
const std::string toLiteral(bool* isValid = nullptr) const;
bool fromColor(const Zeus::CColor& val);
bool fromFloat(float val);
bool fromBoolean(bool val);
bool fromInteger(int val);
bool fromLiteral(const std::string& val);
bool fromLiteral(const std::wstring& val);
bool isFloat() const { return m_type == EType::Float; }
bool isBoolean() const { return m_type == EType::Boolean; }
bool isInteger() const { return m_type == EType::Integer; }
bool isLiteral() const { return m_type == EType::Literal; }
bool isColor() const { return m_type == EType::Color; }
bool isModified() const;
bool isReadOnly() const;
bool isCheat() const;
bool isHidden() const;
bool isArchive() const;
void clearModified();
void setModified();
EType type() const { return m_type; }
EFlags flags() const { return m_flags; }
/*!
* \brief Unlocks the CVar for writing if it is ReadOnly.
* <b>Handle with care!!!</b> if you use unlock(), make sure
* you lock the cvar using lock()
* \see lock
*/
void unlock();
/*!
* \brief Locks the CVar to prevent writing if it is ReadOnly.
* Unlike it's partner function unlock, lock is harmless
* \see unlock
*/
void lock();
void addListener(ListenerFunc func) { m_listeners.push_back(func); }
private:
void dispatch();
std::string m_help;
std::string m_defaultValue;
EFlags m_flags;
bool m_allowedWrite;
CVarManager& m_mgr;
std::vector<ListenerFunc> m_listeners;
};
class CVarUnlocker
{
CVar* m_cvar;
public:
CVarUnlocker(CVar* cvar) : m_cvar(cvar) { if (m_cvar) m_cvar->unlock(); }
~CVarUnlocker() { if (m_cvar) m_cvar->lock(); }
};
}
#endif // CVAR_HPP

View File

@ -1,183 +0,0 @@
#include "CVarManager.hpp"
#include "CVar.hpp"
#include <Athena/FileWriter.hpp>
#include <Athena/Utility.hpp>
#include <HECL/Runtime.hpp>
#include <memory>
namespace Retro
{
CVar* com_developer = nullptr;
CVar* com_configfile = nullptr;
CVar* com_enableCheats = nullptr;
CVar* r_clearColor = nullptr;
LogVisor::LogModule CVarLog("CVarManager");
CVarManager::CVarManager(HECL::Runtime::FileStoreManager& store, bool useBinary)
: m_store(store),
m_useBinary(useBinary)
{
com_configfile = newCVar("config", "File to store configuration", std::string("config"), CVar::EFlags::System);
com_developer = newCVar("developer", "Enables developer mode", false, (CVar::EFlags::System | CVar::EFlags::Cheat | CVar::EFlags::ReadOnly));
com_enableCheats = newCVar("iamaweiner", "Enable cheats", false, (CVar::EFlags::System | CVar::EFlags::ReadOnly | CVar::EFlags::Hidden));
r_clearColor = newCVar("r_clearcolor", "Sets the clear color for the frame buffer", Zeus::CColor{Zeus::Comp8(255), 255, 255}, (CVar::EFlags::System | CVar::EFlags::Archive));
}
CVarManager::~CVarManager()
{
}
void CVarManager::update()
{
for (const std::pair<std::string, CVar*>& pair : m_cvars)
if (pair.second->isModified())
{
pair.second->dispatch();
pair.second->clearModified();
}
}
bool CVarManager::registerCVar(CVar* cvar)
{
std::string tmp = cvar->name();
Athena::utility::tolower(tmp);
if (m_cvars.find(tmp) != m_cvars.end())
return false;
m_cvars[tmp] = cvar;
return true;
}
CVar* CVarManager::findCVar(std::string name)
{
Athena::utility::tolower(name);
if (m_cvars.find(name) == m_cvars.end())
return nullptr;
return m_cvars[name];
}
std::vector<CVar*> CVarManager::archivedCVars() const
{
std::vector<CVar*> ret;
for (const std::pair<std::string, CVar*>& pair : m_cvars)
if (pair.second->isArchive())
ret.push_back(pair.second);
return ret;
}
std::vector<CVar*> CVarManager::cvars() const
{
std::vector<CVar*> ret;
for (const std::pair<std::string, CVar*>& pair : m_cvars)
ret.push_back(pair.second);
return ret;
}
void CVarManager::deserialize(CVar* cvar)
{
if (!cvar || !cvar->isArchive())
return;
CVarContainer container;
#if _WIN32
HECL::SystemString filename = m_store.getStoreRoot() + _S('/') + com_configfile->toWideLiteral();
#else
HECL::SystemString filename = m_store.getStoreRoot() + _S('/') + com_configfile->toLiteral();
#endif
HECL::Sstat st;
if (m_useBinary)
{
filename += _S(".bin");
if (HECL::Stat(filename.c_str(), &st) || !S_ISREG(st.st_mode))
return;
Athena::io::FileReader reader(filename);
if (reader.isOpen())
container.read(reader);
}
else
{
filename += _S(".yaml");
if (HECL::Stat(filename.c_str(), &st) || !S_ISREG(st.st_mode))
return;
FILE* f = HECL::Fopen(filename.c_str(), _S("rb"));
if (f)
container.fromYAMLFile(f);
fclose(f);
}
if (container.cvars.size() > 0)
{
auto serialized = std::find_if(container.cvars.begin(), container.cvars.end(), [&cvar](const DNACVAR::CVar& c) -> bool
{ return c.m_name == cvar->name(); });
if (serialized != container.cvars.end())
{
DNACVAR::CVar& tmp = *serialized;
if (tmp.m_type != cvar->type())
{
CVarLog.report(LogVisor::Error, _S("Stored type for %s does not match actual type!"), tmp.m_name.c_str());
return;
}
cvar->m_value = tmp.m_value;
}
}
}
void CVarManager::serialize()
{
CVarContainer container;
for (const std::pair<std::string, CVar*>& pair : m_cvars)
if (pair.second->isArchive())
{
CVar tmp = *pair.second;
container.cvars.push_back(tmp);
}
container.cvarCount = container.cvars.size();
#if _WIN32
HECL::SystemString filename = m_store.getStoreRoot() + _S('/') + com_configfile->toWideLiteral();
#else
HECL::SystemString filename = m_store.getStoreRoot() + _S('/') + com_configfile->toLiteral();
#endif
if (m_useBinary)
{
filename += _S(".bin");
Athena::io::FileWriter writer(filename);
if (writer.isOpen())
container.write(writer);
}
else
{
filename += _S(".yaml");
FILE* f = HECL::Fopen(filename.c_str(), _S("wb"));
if (f)
container.toYAMLFile(f);
fclose(f);
}
}
bool CVarManager::suppressDeveloper()
{
bool oldDeveloper = com_developer->toBoolean();
CVarUnlocker unlock(com_developer);
com_developer->fromBoolean(false);
return oldDeveloper;
}
void CVarManager::restoreDeveloper(bool oldDeveloper)
{
CVarUnlocker unlock(com_developer);
com_developer->fromBoolean(oldDeveloper);
}
}

View File

@ -1,70 +0,0 @@
#ifndef CVARMANAGER_HPP
#define CVARMANAGER_HPP
#include <unordered_map>
#include <vector>
#include "CVar.hpp"
namespace HECL
{
namespace Runtime
{
class FileStoreManager;
}
}
namespace Retro
{
class CVarManager
{
using CVarContainer = DNACVAR::CVarContainer;
template <typename T>
CVar* _newCVar(const std::string& name, const std::string& help, const T& value, CVar::EFlags flags)
{
CVar* ret(new CVar(name, value, help, flags, *this));
if (registerCVar(ret))
{
if (ret->isArchive())
deserialize(ret);
return ret;
}
delete ret;
return nullptr;
}
HECL::Runtime::FileStoreManager& m_store;
bool m_useBinary;
public:
CVarManager(HECL::Runtime::FileStoreManager& store, bool useBinary = false);
~CVarManager();
void update();
CVar* newCVar(const std::string& name, const std::string& help, const Zeus::CColor& value, CVar::EFlags flags)
{ return _newCVar<Zeus::CColor>(name, help, value, flags); }
CVar* newCVar(const std::string& name, const std::string& help, const std::string& value, CVar::EFlags flags)
{ return _newCVar<std::string>(name, help, value, flags); }
CVar* newCVar(const std::string& name, const std::string& help, bool value, CVar::EFlags flags)
{ return _newCVar<bool>(name, help, value, flags); }
CVar* newCVar(const std::string& name, const std::string& help, float value, CVar::EFlags flags)
{ return _newCVar<float>(name, help, value, flags); }
CVar* newCVar(const std::string& name, const std::string& help, int value, CVar::EFlags flags)
{ return _newCVar<int>(name, help, value, flags); }
bool registerCVar(CVar* cvar);
CVar* findCVar(std::string name);
std::vector<CVar*> archivedCVars() const;
std::vector<CVar*> cvars() const;
void deserialize(CVar* cvar);
void serialize();
private:
bool suppressDeveloper();
void restoreDeveloper(bool oldDeveloper);
std::unordered_map<std::string, CVar*> m_cvars;
};
}
#endif // CVARMANAGER_HPP

2
hecl

@ -1 +1 @@
Subproject commit 5193310ce43c87a14b8521191eb43d60aecf32f8
Subproject commit e98d0f25d94acca5675f96fe03cbb536d126faec

@ -1 +1 @@
Subproject commit 4ce426b4b275e9a0db10aab93375c01e85103437
Subproject commit 9734f2ceddd387273c41421769e134d927dd1a7e