From f87c2b47a60cf4f46c0045b730efdd1812ce7121 Mon Sep 17 00:00:00 2001 From: Reece Date: Wed, 30 Jun 2021 17:35:02 +0100 Subject: [PATCH] [+] i can has registry --- Include/Aurora/Registry/ERegistrySource.hpp | 6 +- Include/Aurora/Registry/IRegistry.hpp | 2 +- Source/Registry/Registry.cpp | 153 ++++++++++++++++++++ Source/Registry/Registry.hpp | 1 - 4 files changed, 156 insertions(+), 6 deletions(-) diff --git a/Include/Aurora/Registry/ERegistrySource.hpp b/Include/Aurora/Registry/ERegistrySource.hpp index 74ab296e..d068606c 100644 --- a/Include/Aurora/Registry/ERegistrySource.hpp +++ b/Include/Aurora/Registry/ERegistrySource.hpp @@ -9,10 +9,8 @@ namespace Aurora::Registry { - enum ERegistrySource + enum class ERegistrySource { - eFSGlobal, - eFSProfile, - eGlobal + eFS }; } \ No newline at end of file diff --git a/Include/Aurora/Registry/IRegistry.hpp b/Include/Aurora/Registry/IRegistry.hpp index cdcb180d..82e575a8 100644 --- a/Include/Aurora/Registry/IRegistry.hpp +++ b/Include/Aurora/Registry/IRegistry.hpp @@ -10,7 +10,7 @@ namespace Aurora::Registry { using RegistryType = Aurora::Data::DataType; - using RegistryValue = Aurora::Data::Value; + using RegistryValue = Aurora::Data::TypedValue; class IRegistry { diff --git a/Source/Registry/Registry.cpp b/Source/Registry/Registry.cpp index aea9203e..5fc1c385 100644 --- a/Source/Registry/Registry.cpp +++ b/Source/Registry/Registry.cpp @@ -5,3 +5,156 @@ Date: 2021-6-13 Author: Reece ***/ +#include +#include "Registry.hpp" + +#include + +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(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(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(registry); + } +} \ No newline at end of file diff --git a/Source/Registry/Registry.hpp b/Source/Registry/Registry.hpp index 84b428ae..ef4f43e6 100644 --- a/Source/Registry/Registry.hpp +++ b/Source/Registry/Registry.hpp @@ -6,4 +6,3 @@ Author: Reece ***/ #pragma once -