Finalize CVarManager (for now)

This commit is contained in:
Phillip Stephens 2015-11-21 22:51:25 -08:00
parent 59708f4478
commit a21aef6fa3
8 changed files with 234 additions and 72 deletions

View File

@ -1,6 +1,7 @@
make_dnalist(liblist
CMDL
CVar)
add_library(DNACommon
DNACommon.hpp DNACommon.cpp
${liblist}

View File

@ -2,21 +2,19 @@
#define _DNACOMMON_CVAR_HPP_
#include <Athena/Global.hpp>
#include <type_traits>
#include "DNACommon.hpp"
namespace Retro
{
namespace DNACVAR
{
enum class EType
enum class EType : atUint8
{
Boolean,
Integer,
Float,
Literal,
Color,
Bind
Color
};
enum EFlags
@ -24,12 +22,13 @@ enum EFlags
All = -1, // NOTE: is this really necessary? It seems rather overkill
System = (1 << 0),
Game = (1 << 1),
Gui = (1 << 2),
Cheat = (1 << 3),
Hidden = (1 << 4),
ReadOnly = (1 << 5),
Archive = (1 << 6),
Modified = (1 << 7)
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)
@ -40,7 +39,14 @@ public:
String<-1> m_name;
String<-1> m_value;
Value<EType> m_type;
Value<EFlags> m_flags;
};
struct CVarContainer : BigYAML
{
DECL_YAML
Value<atUint32> magic = 'CVAR';
Value<atUint32> cvarCount;
Vector<CVar, DNA_COUNT(cvarCount)> cvars;
};
}

View File

@ -1,24 +1,30 @@
#include <LogVisor/LogVisor.hpp>
#include <boo/boo.hpp>
#include <Specter/Specter.hpp>
#include <Runtime/CVarManager.hpp>
#include <Runtime/CGameAllocator.hpp>
namespace RUDE
{
struct Application : boo::IApplicationCallback
{
Retro::CGameAllocator m_allocator;
HECL::Runtime::FileStoreManager m_fileMgr;
Specter::FontCache m_fontCache;
Specter::RootView m_rootView;
Retro::CVarManager m_cvarManager;
boo::IWindow* m_mainWindow;
bool m_running = true;
Application() : m_fileMgr(_S("rude")), m_fontCache(m_fileMgr), m_rootView(m_fontCache) {}
Application() : m_fileMgr(_S("rude")), m_fontCache(m_fileMgr), m_rootView(m_fontCache), m_cvarManager(m_fileMgr, true){}
int appMain(boo::IApplication* app)
{
m_allocator.Initialize();
m_mainWindow = app->newWindow(_S("RUDE"));
m_rootView.setWindow(m_mainWindow, 1.0f);
m_cvarManager.serialize();
while (m_running)
{
@ -26,6 +32,7 @@ struct Application : boo::IApplicationCallback
}
return 0;
}
void appQuitting(boo::IApplication*)

View File

@ -15,6 +15,8 @@ else()
list(APPEND PLAT_SRCS CMemoryCardSysNix.cpp CCallStackNix.cpp)
endif()
make_dnalist(CVarManager.hpp)
add_library(RuntimeCommon
CMemory.hpp CMemory.cpp
CMemoryCardSys.hpp
@ -68,7 +70,7 @@ add_library(RuntimeCommon
rstl.hpp rstl.cpp
GameGlobalObjects.hpp
CVar.hpp CVar.cpp
CVarManager.hpp CVarManager.cpp
CVarManager.cpp
RetroTypes.hpp
GCNTypes.hpp
${PLAT_SRCS})

View File

@ -7,7 +7,11 @@
namespace Retro
{
CVar::CVar(const std::string& name, const std::string &value, const std::string &help, EType type, EFlags flags, CVarManager* parent)
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;
@ -16,10 +20,10 @@ CVar::CVar(const std::string& name, const std::string &value, const std::string
m_type = type;
m_flags = flags;
m_allowedWrite = false;
m_mgr = parent;
}
CVar::CVar(const std::string& name, const std::string& value, const std::string& help, CVar::EFlags flags, CVarManager* parent)
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();
@ -29,7 +33,6 @@ CVar::CVar(const std::string& name, const std::string& value, const std::string&
m_type = EType::Literal;
m_flags = flags;
m_allowedWrite = false;
m_mgr = parent;
fromLiteral(value);
m_defaultValue = m_value;
@ -40,7 +43,8 @@ CVar::CVar(const std::string& name, const std::string& value, const std::string&
m_flags = flags;
}
CVar::CVar(const std::string& name, const Zeus::CColor& value, const std::string& help, EFlags flags, CVarManager* parent)
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();
@ -50,7 +54,6 @@ CVar::CVar(const std::string& name, const Zeus::CColor& value, const std::string
m_type = EType::Color;
m_flags = flags;
m_allowedWrite = false;
m_mgr = parent;
fromColor(value);
m_defaultValue = m_value;
@ -61,7 +64,8 @@ CVar::CVar(const std::string& name, const Zeus::CColor& value, const std::string
m_flags = flags;
}
CVar::CVar(const std::string& name, float value, const std::string& help, EFlags flags, CVarManager* parent)
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();
@ -71,7 +75,6 @@ CVar::CVar(const std::string& name, float value, const std::string& help, EFlags
m_type = EType::Float;
m_flags = flags;
m_allowedWrite = false;
m_mgr = parent;
fromFloat(value);
m_defaultValue = m_value;
@ -82,7 +85,8 @@ CVar::CVar(const std::string& name, float value, const std::string& help, EFlags
m_flags = flags;
}
CVar::CVar(const std::string& name, bool value, const std::string& help, CVar::EFlags flags, CVarManager* parent)
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();
@ -92,7 +96,6 @@ CVar::CVar(const std::string& name, bool value, const std::string& help, CVar::E
m_type = EType::Boolean;
m_flags = flags;
m_allowedWrite = false;
m_mgr = parent;
fromBoolean(value);
m_defaultValue = m_value;
@ -103,7 +106,8 @@ CVar::CVar(const std::string& name, bool value, const std::string& help, CVar::E
m_flags = flags;
}
CVar::CVar(const std::string& name, int value, const std::string& help, CVar::EFlags flags, CVarManager* parent)
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();
@ -113,7 +117,6 @@ CVar::CVar(const std::string& name, int value, const std::string& help, CVar::EF
m_type = EType::Integer;
m_flags = flags;
m_allowedWrite = false;
m_mgr = parent;
fromInteger(value);
m_defaultValue = m_value;
@ -211,8 +214,7 @@ int CVar::toInteger(bool* isValid) const
return strtol(m_value.c_str(), nullptr, 0);
}
const std::string& CVar::toLiteral(bool* isValid) const
const std::string CVar::toLiteral(bool* isValid) const
{
if (m_type != EType::Literal /*&& (com_developer && com_developer->toBoolean())*/)
{
@ -226,6 +228,20 @@ const std::string& CVar::toLiteral(bool* isValid) const
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())
@ -310,6 +326,22 @@ bool CVar::fromLiteral(const std::string& val)
return true;
}
bool CVar::fromLiteral(const std::wstring& val)
{
if (isCheat() /*&& (!com_developer->toBoolean() && !com_enableCheats->toBoolean())*/)
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; }

View File

@ -11,16 +11,18 @@ namespace Retro
class CVarManager;
class CVar : protected DNACVAR::CVar
{
friend class CVarManager;
public:
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);
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;
@ -30,13 +32,15 @@ public:
float toFloat(bool* isValid = nullptr) const;
bool toBoolean(bool* isValid = nullptr) const;
int toInteger(bool* isValid = nullptr) const;
const std::string& toLiteral(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; }
@ -72,9 +76,10 @@ public:
private:
std::string m_help;
std::string m_defaultValue;
EFlags m_flags;
bool m_allowedWrite;
const CVarManager* m_mgr;
CVarManager& m_mgr;
};

View File

@ -1,23 +1,34 @@
#include "CVarManager.hpp"
#include "CVar.hpp"
#include <Athena/FileWriter.hpp>
#include <Athena/Utility.hpp>
#include <HECL/Runtime.hpp>
#include <memory>
namespace Retro
{
CVarManager::CVarManager()
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::Archive | 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::initialize()
{
}
bool CVarManager::registerCVar(std::shared_ptr<CVar> cvar)
bool CVarManager::registerCVar(CVar* cvar)
{
std::string tmp = cvar->name();
Athena::utility::tolower(tmp);
@ -28,7 +39,7 @@ bool CVarManager::registerCVar(std::shared_ptr<CVar> cvar)
return true;
}
std::shared_ptr<CVar> CVarManager::findCVar(const std::string &name)
CVar* CVarManager::findCVar(const std::string &name)
{
std::string tmp = std::string(name);
Athena::utility::tolower(tmp);
@ -38,40 +49,124 @@ std::shared_ptr<CVar> CVarManager::findCVar(const std::string &name)
return m_cvars[tmp];
}
std::vector<std::shared_ptr<CVar>> CVarManager::archivedCVars() const
std::vector<CVar*> CVarManager::archivedCVars() const
{
std::vector<std::shared_ptr<CVar>> ret;
for (std::pair<std::string, std::shared_ptr<CVar>> pair : m_cvars)
{
if (int(pair.second->flags() & CVar::EFlags::Archive) != 0)
std::vector<CVar*> ret;
for (std::pair<std::string, CVar*> pair : m_cvars)
if (pair.second->isArchive())
ret.push_back(pair.second);
}
return ret;
}
std::vector<std::shared_ptr<CVar>> CVarManager::cvars() const
std::vector<CVar*> CVarManager::cvars() const
{
std::vector<std::shared_ptr<CVar>> ret;
for (std::pair<std::string, std::shared_ptr<CVar>> pair : m_cvars)
std::vector<CVar*> ret;
for (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 (HECL::Stat(filename.c_str(), &st) || !S_ISREG(st.st_mode))
return;
if (m_useBinary)
{
filename += _S(".bin");
Athena::io::FileReader reader(filename);
if (reader.isOpen())
container.read(reader);
}
else
{
filename += _S(".yaml");
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();
bool oldDeveloper = com_developer->toBoolean();
CVarUnlocker unlock(com_developer);
com_developer->fromBoolean(false);*/
com_developer->fromBoolean(false);
return false; //oldDeveloper;
return oldDeveloper;
}
void CVarManager::restoreDeveloper(bool oldDeveloper)
{
// CVarUnlocker unlock(com_developer);
// com_developer->fromBoolean(oldDeveloper);
CVarUnlocker unlock(com_developer);
com_developer->fromBoolean(oldDeveloper);
}
}

View File

@ -3,51 +3,65 @@
#include <unordered_map>
#include <vector>
#include <fstream>
#include "CVar.hpp"
namespace HECL
{
namespace Runtime
{
class FileStoreManager;
}
}
namespace Retro
{
class CVarManager
{
using CVarContainer = DNACVAR::CVarContainer;
template <typename T>
std::shared_ptr<CVar> _newCVar(const std::string& name, const std::string& help, const T& value, CVar::EFlags flags)
CVar* _newCVar(const std::string& name, const std::string& help, const T& value, CVar::EFlags flags)
{
std::shared_ptr<CVar> ret(new CVar(name, value, help, flags, this));
CVar* ret(new CVar(name, value, help, flags, *this));
if (registerCVar(ret))
{
deserialize(ret);
return ret;
}
delete ret;
return nullptr;
}
HECL::Runtime::FileStoreManager& m_store;
bool m_useBinary;
public:
CVarManager();
CVarManager(HECL::Runtime::FileStoreManager& store, bool useBinary = false);
~CVarManager();
void initialize();
std::shared_ptr<CVar> newCVar(const std::string& name, const std::string& help, const Zeus::CColor& value, CVar::EFlags flags)
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); }
std::shared_ptr<CVar> newCVar(const std::string& name, const std::string& help, const std::string& value, CVar::EFlags 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); }
std::shared_ptr<CVar> newCVar(const std::string& name, const std::string& help, bool value, CVar::EFlags flags)
CVar* newCVar(const std::string& name, const std::string& help, bool value, CVar::EFlags flags)
{ return _newCVar<bool>(name, help, value, flags); }
std::shared_ptr<CVar> newCVar(const std::string& name, const std::string& help, float value, CVar::EFlags flags)
CVar* newCVar(const std::string& name, const std::string& help, float value, CVar::EFlags flags)
{ return _newCVar<float>(name, help, value, flags); }
std::shared_ptr<CVar> newCVar(const std::string& name, const std::string& help, int value, CVar::EFlags 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(std::shared_ptr<CVar> cvar);
bool registerCVar(CVar* cvar);
std::shared_ptr<CVar>findCVar(const std::string& name);
CVar*findCVar(const std::string& name);
std::vector<std::shared_ptr<CVar>> archivedCVars() const;
std::vector<std::shared_ptr<CVar>> cvars() const;
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, std::shared_ptr<CVar>> m_cvars;
std::unordered_map<std::string, CVar*> m_cvars;
};
}