CheatMenuSA/CheatMenu/Json.cpp

47 lines
914 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
{
2021-06-17 09:00:32 -04:00
if (name == "")
return;
file_path = PLUGIN_PATH((char*)"/CheatMenu/json/") + std::string(name) + ".json";
2021-02-24 16:54:45 -05:00
if (fs::exists(file_path))
2020-12-02 16:19:16 -05:00
{
try
2020-12-02 16:19:16 -05:00
{
std::ifstream file(file_path);
file >> data;
file.close();
}
catch (...)
{
2021-02-25 17:45:41 -05:00
flog << "Error trying to read " << file_path << std::endl;
2020-12-02 16:19:16 -05:00
data = "{}"_json;
}
}
else
{
data = "{}"_json;
2021-02-27 15:40:51 -05:00
if (file_path.find("config"))
flog << "Creating config.json file" << std::endl;
else
flog << "Failed to locate file " << file_path << std::endl;
2020-12-02 16:19:16 -05:00
}
}
void CJson::WriteToDisk()
{
std::ofstream file(file_path);
2021-02-24 16:54:45 -05:00
file << 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
{
for (auto element : data.items())
vec.push_back(element.key());
2021-02-25 17:45:41 -05:00
}