CheatMenuSA/src/datastore.cpp

83 lines
1.6 KiB
C++
Raw Normal View History

2022-06-15 06:45:43 -04:00
#include "datastore.h"
#include "pch.h"
DataStore::DataStore(const char* fileName, bool isPathPredefined) noexcept
2022-06-15 06:45:43 -04:00
{
if (isPathPredefined)
{
path = std::string(fileName) + fileExt;
}
else
{
2022-06-16 14:44:33 -04:00
path = PLUGIN_PATH((char*)FILE_NAME "/data/") + std::string(fileName) + fileExt;
}
2022-06-15 06:45:43 -04:00
if (std::filesystem::exists(path))
{
toml::parse_result result = toml::parse_file(path);
if (result)
{
pTable = std::make_unique<toml::table>(std::move(result));
return;
}
}
if (pTable == nullptr)
{
pTable = std::make_unique<toml::table>();
if (fileName == FILE_NAME)
2022-06-15 06:45:43 -04:00
{
Log::Print<eLogLevel::Info>("Creating {}{}", fileName, fileExt);
}
else
{
Log::Print<eLogLevel::Warn>("Error parsing {}{}", fileName, fileExt);
}
}
}
bool DataStore::Contains(const char* ele) noexcept
{
if (pTable)
{
return pTable->contains(ele);
}
return false;
}
DataStore::Table& DataStore::Items() noexcept
{
return pTable->as_table()->ref<DataStore::Table>();
}
void DataStore::RemoveTable(const char* key) noexcept
{
if (pTable)
{
pTable->erase(key);
}
}
void DataStore::RemoveKey(const char* key, const char* entry) noexcept
{
if (pTable)
{
(*pTable).at_path(key).as_table()->erase(entry);
2022-06-15 06:45:43 -04:00
}
}
void DataStore::Save() noexcept
{
if (pTable)
{
std::ofstream file(path);
if (file.good())
{
file << *pTable << std::endl;
file.close();
}
}
}