Skip to content

Commit

Permalink
update: 0.5.3
Browse files Browse the repository at this point in the history
  • Loading branch information
KobeBryant114514 authored Jan 21, 2024
2 parents 809e7b6 + 7f309df commit be8c953
Show file tree
Hide file tree
Showing 8 changed files with 60 additions and 67 deletions.
Binary file modified GMLIB/GMLIB.dll
Binary file not shown.
2 changes: 1 addition & 1 deletion GMLIB/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@
"type": "native",
"author": "GroupMountain",
"description": "Group Mountain Library",
"version": "0.5.2"
"version": "0.5.3"
}
59 changes: 53 additions & 6 deletions include/GMLIB/Files/JsonConfig.h
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
#pragma once
#include "GMLIB/GMLIB.h"
#include "nlohmann/json.hpp"

class GMLIB_JsonConfig {
public:
std::string mFilePath;
nlohmann::ordered_json mValue;

public:
GMLIB_JsonConfig(std::string filePath, std::string defaultJson);
GMLIB_JsonConfig(std::string filePath, nlohmann::ordered_json defaultJson);
GMLIB_JsonConfig() = delete;
GMLIB_API GMLIB_JsonConfig(std::string filePath, std::string defaultJson);
GMLIB_API GMLIB_JsonConfig(std::string filePath, nlohmann::ordered_json defaultJson);
GMLIB_API GMLIB_JsonConfig() = delete;

public:
virtual ~GMLIB_JsonConfig();
Expand All @@ -18,13 +19,59 @@ class GMLIB_JsonConfig {
GMLIB_API bool initConfig();

template <typename T>
GMLIB_API std::optional<T> getValue(std::vector<std::string> key);
inline std::optional<T> getValue(std::vector<std::string> path) {
try {
if (path.empty()) {
return {};
}
auto value = mValue;
for (auto& key : path) {
if (value.is_object() && value.contains(key)) {
value = value[key];
} else {
return {};
}
}
return value.get<T>();
} catch (...) {
return {};
}
}

template <typename T>
GMLIB_API T getValue(std::vector<std::string> key, T defaultValue);
inline T getValue(std::vector<std::string> path, T defaultValue) {
try {
std::optional<T> result = getValue<T>(path);
if (result.has_value()) {
return result.value();
}
this->setValue<T>(path, defaultValue);
return defaultValue;
} catch (...) {
this->setValue<T>(path, defaultValue);
return defaultValue;
}
}

template <typename T>
GMLIB_API bool setValue(std::vector<std::string> key, T value);
inline bool setValue(std::vector<std::string> path, T data) {
try {
if (path.empty()) {
return false;
}
nlohmann::ordered_json* jsonObj = &mValue;
for (auto& key : path) {
if (jsonObj->is_null()) {
*jsonObj = nlohmann::ordered_json::object();
}
jsonObj = &(*jsonObj)[key];
}
*jsonObj = data;
return this->writeFile();
} catch (...) {
return false;
}
}

GMLIB_API bool deleteKey(std::vector<std::string> key);

Expand Down
6 changes: 3 additions & 3 deletions include/GMLIB/Files/JsonLanguage.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ class GMLIB_JsonLanguage : public GMLIB_JsonConfig {
public:

public:
GMLIB_JsonLanguage(std::string filePath, std::string defaultJson);
GMLIB_JsonLanguage(std::string filePath, nlohmann::ordered_json defaultJson);
GMLIB_JsonLanguage() = delete;
GMLIB_API GMLIB_JsonLanguage(std::string filePath, std::string defaultJson);
GMLIB_API GMLIB_JsonLanguage(std::string filePath, nlohmann::ordered_json defaultJson);
GMLIB_API GMLIB_JsonLanguage() = delete;

public:
GMLIB_API std::string getValue(std::string key);
Expand Down
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@
"type": "native",
"author": "GroupMountain",
"description": "Group Mountain Library",
"version": "0.5.2"
"version": "0.5.3"
}
54 changes: 0 additions & 54 deletions src/Files/JsonConfig.cc
Original file line number Diff line number Diff line change
Expand Up @@ -46,60 +46,6 @@ bool GMLIB_JsonConfig::writeFile() {

nlohmann::ordered_json GMLIB_JsonConfig::getSelf() { return mValue; }

template <typename T>
std::optional<T> GMLIB_JsonConfig::getValue(std::vector<std::string> path) {
try {
if (path.empty()) {
return {};
}
auto value = mValue;
for (auto& key : path) {
if (value.is_object() && value.contains(key)) {
value = value[key];
} else {
return {};
}
}
return value.get<T>();
} catch (...) {
return {};
}
}

template <typename T>
T GMLIB_JsonConfig::getValue(std::vector<std::string> path, T defaultValue) {
try {
std::optional<T> result = getValue<T>(path);
if (result.has_value) {
return result.value();
}
this->setValue<T>(path, defaultValue);
return defaultValue;
} catch (...) {
return false;
}
}

template <typename T>
bool GMLIB_JsonConfig::setValue(std::vector<std::string> path, T data) {
try {
if (path.empty()) {
return false;
}
nlohmann::ordered_json* jsonObj = &mValue;
for (auto& key : path) {
if (jsonObj->is_null()) {
*jsonObj = nlohmann::ordered_json::object();
}
jsonObj = &(*jsonObj)[key];
}
*jsonObj = data;
return this->writeFile();
} catch (...) {
return false;
}
}

bool GMLIB_JsonConfig::deleteKey(std::vector<std::string> path) {
try {
if (path.empty()) {
Expand Down
2 changes: 1 addition & 1 deletion src/Version.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

#define LIB_VERSION_MAJOR 0
#define LIB_VERSION_MINOR 5
#define LIB_VERSION_REVISION 2
#define LIB_VERSION_REVISION 3
#define LIB_VERSION_PRERELEASE ""
#define LIB_VERSION_BUILD_META ""

Expand Down
2 changes: 1 addition & 1 deletion tooth.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"format_version": 2,
"tooth": "github.com/GroupMountain/GMLIB",
"version": "0.5.2",
"version": "0.5.3",
"info": {
"name": "GMLIB",
"description": "Group Mountain Library",
Expand Down

0 comments on commit be8c953

Please sign in to comment.