CheatMenuSA/CheatMenu/Json.cpp

48 lines
986 B
C++
Raw Normal View History

2020-12-02 16:19:16 -05:00
#include "pch.h"
#include "Json.h"
CJson::CJson(const char* name)
2020-12-02 16:19:16 -05:00
{
if (name == "" || !fs::is_directory(PLUGIN_PATH((char*)"CheatMenu")))
2021-06-17 09:00:32 -04:00
return;
2021-06-18 12:49:11 -04:00
m_FilePath = PLUGIN_PATH((char*)"/CheatMenu/json/") + std::string(name) + ".json";
2021-02-24 16:54:45 -05:00
2021-06-18 12:49:11 -04:00
if (fs::exists(m_FilePath))
2020-12-02 16:19:16 -05:00
{
try
2020-12-02 16:19:16 -05:00
{
2021-06-18 12:49:11 -04:00
std::ifstream file(m_FilePath);
file >> m_Data;
file.close();
}
catch (...)
{
2021-06-18 12:49:11 -04:00
flog << "Error trying to read " << m_FilePath << std::endl;
m_Data = "{}"_json;
2020-12-02 16:19:16 -05:00
}
}
else
{
2021-06-18 12:49:11 -04:00
m_Data = "{}"_json;
2021-02-27 15:40:51 -05:00
2021-06-18 12:49:11 -04:00
if (m_FilePath.find("config"))
2021-02-27 15:40:51 -05:00
flog << "Creating config.json file" << std::endl;
else
2021-06-18 12:49:11 -04:00
flog << "Failed to locate file " << m_FilePath << std::endl;
2020-12-02 16:19:16 -05:00
}
}
void CJson::WriteToDisk()
{
2021-06-18 12:49:11 -04:00
std::ofstream file(m_FilePath);
file << m_Data.dump(4, ' ', false, nlohmann::json::error_handler_t::replace) << std::endl;
file.close();
}
void CJson::LoadData(std::vector<std::string>& vec, std::string& selected) // Test
2020-12-02 16:19:16 -05:00
{
2021-06-18 12:49:11 -04:00
for (auto element : m_Data.items())
2020-12-02 16:19:16 -05:00
vec.push_back(element.key());
2021-06-18 12:49:11 -04:00
}