[+] i can has registry

This commit is contained in:
Reece Wilson 2021-06-30 17:35:02 +01:00
parent 22c1681132
commit f87c2b47a6
4 changed files with 156 additions and 6 deletions

View File

@ -9,10 +9,8 @@
namespace Aurora::Registry
{
enum ERegistrySource
enum class ERegistrySource
{
eFSGlobal,
eFSProfile,
eGlobal
eFS
};
}

View File

@ -10,7 +10,7 @@
namespace Aurora::Registry
{
using RegistryType = Aurora::Data::DataType;
using RegistryValue = Aurora::Data::Value;
using RegistryValue = Aurora::Data::TypedValue;
class IRegistry
{

View File

@ -5,3 +5,156 @@
Date: 2021-6-13
Author: Reece
***/
#include <RuntimeInternal.hpp>
#include "Registry.hpp"
#include <nlohmann/json.hpp>
using json = nlohmann::json;
namespace Aurora::Registry
{
class FSRegistry : public IRegistry
{
public:
json document;
AuString path;
bool Init(const AuString &path);
bool KeyExists(const AuString &key, RegistryType &type);
bool GetOrCreateKey(const AuString &key, const RegistryValue &ref, RegistryValue &value);
bool GetKey(const AuString &key, RegistryValue &value);
bool SetKey(const AuString &key, const RegistryValue &value);
bool Save();
};
bool FSRegistry::Init(const AuString &path)
{
try
{
this->path = path;
AuString file;
if (!IO::FS::ReadString(path, file))
{
return IO::FS::WriteString(path, "{}");
}
document = json::parse(file);
}
catch (...)
{
return false;
}
return true;
}
bool FSRegistry::KeyExists(const AuString &key, RegistryType &type)
{
auto itrObject = document.find(key);
if (itrObject == document.end()) return false;
auto itrType = itrObject->find("type");
if (itrType == itrObject->end()) return false;
auto itrValue = itrObject->find("value");
if (itrValue == itrObject->end()) return false;
return true;
}
bool FSRegistry::GetOrCreateKey(const AuString &key, const RegistryValue &ref, RegistryValue &value)
{
if (GetKey(key, value))
{
return true;
}
return SetKey(key, ref);
}
bool FSRegistry::GetKey(const AuString &key, RegistryValue &value)
{
auto itrObject = document.find(key);
if (itrObject == document.end())
{
return false;
}
auto itrType = itrObject->find("type");
auto itrValue = itrObject->find("value");
if ((itrType == itrObject->end()) ||
(itrValue == itrObject->end()))
{
return false;
}
if (!itrValue->is_string())
{
return false;
}
if (!itrType->is_number_integer())
{
return false;
}
Parse::ParsableTag type = *itrType;
AuString strValue = *itrValue;
Parse::ParseValueEx parsedValue;
if (!Parse::ConsumeToken(type, strValue , parsedValue))
{
return false;
}
value.type = static_cast<Data::DataType>(type);
value.value = parsedValue;
return true;
}
bool FSRegistry::SetKey(const AuString &key, const RegistryValue &value)
{
json object {};
object["type"] = value.type;
AuString stringified;
Parse::SerializeToken(static_cast<Parse::ParsableTag>(value.type), value.value, stringified);
object["value"] = stringified;
document[key] = object;
return Save();
}
bool FSRegistry::Save()
{
if (!IO::FS::WriteString(path, document.dump(4)))
{
SysPushErrorIO("Couldn't write registry to: {}", path);
return false;
}
return true;
}
AUKN_SYM IRegistry *RegistryNew(ERegistrySource source, const AuString &name)
{
SysAssert(source == ERegistrySource::eFS, "Unknown registry type");
auto registry = _new FSRegistry();
if (!registry)
{
return nullptr;
}
if (!registry->Init(name))
{
delete registry;
return nullptr;
}
return registry;
}
AUKN_SYM void RegistryRelease(IRegistry *registry)
{
SafeDelete<FSRegistry *>(registry);
}
}

View File

@ -6,4 +6,3 @@
Author: Reece
***/
#pragma once