CheatMenuSA/src/json.h

124 lines
2.4 KiB
C
Raw Normal View History

2020-12-02 16:19:16 -05:00
#pragma once
2021-09-20 08:41:40 -04:00
#include "../depend/json.hpp"
2020-12-02 16:19:16 -05:00
/*
2021-10-25 10:03:27 -04:00
Wrapper class for nlohmann::json
2022-01-07 03:18:00 -05:00
Contains helper methods
*/
2020-12-02 16:19:16 -05:00
class CJson
{
private:
2022-01-07 03:18:00 -05:00
std::string m_FilePath;
2020-12-02 16:19:16 -05:00
public:
2022-01-07 03:18:00 -05:00
nlohmann::json m_Data;
/*
Returns a value from json structure hierarchy using '.'
Example: "Menu.Window.X"
*/
// specialize since typeid(std::string) doesn't work
template <typename T>
T GetValue(std::string&& key, T&& defaultVal)
{
try
{
std::stringstream ss(key);
std::string line;
nlohmann::json* json = &m_Data;
while (getline(ss, line, '.'))
{
json = &((*json)[line]);
}
// json library bugs with bool, using int instead
if (typeid(T) == typeid(bool))
{
return ((json->get<int>() == 1) ? true : false);
}
return json->get<T>();
}
catch (...)
{
return defaultVal;
}
}
template <>
std::string GetValue(std::string&& key, std::string&& defaultVal)
{
try
{
std::stringstream ss(key);
std::string line;
nlohmann::json* json = &m_Data;
while (getline(ss, line, '.'))
{
json = &((*json)[line]);
}
return json->get<std::string>();
}
catch (...)
{
return defaultVal;
}
}
/*
Allows to save values in json hierarchy using '.'
Example: "Menu.Window.X"
*/
template <typename T>
2022-01-20 02:04:45 -05:00
void SetValue(std::string&& key, const T& val)
2022-01-07 03:18:00 -05:00
{
std::stringstream ss(key);
std::string line;
nlohmann::json* json = &m_Data;
while (getline(ss, line, '.'))
{
json = &((*json)[line]);
}
// json library bugs with bool, using int instead
if (typeid(T) == typeid(bool))
{
*json = (val ? 1 : 0);
}
else
{
*json = val;
}
}
template <>
2022-01-20 02:04:45 -05:00
void SetValue(std::string&& key, const std::string& val)
2022-01-07 03:18:00 -05:00
{
std::stringstream ss(key);
std::string line;
nlohmann::json* json = &m_Data;
while (getline(ss, line, '.'))
{
json = &((*json)[line]);
}
*json = val;
}
/*
Saves json data to disk
*/
void WriteToDisk();
CJson(const char* text = "");
2020-12-02 16:19:16 -05:00
};