Clear banlist if valid if we attempt to load it

When we 'reload' the banlist, and have a valid file, none of the current bans in the vector will get overwritten. We clear the entire vector if valid, and rebuild it if the file is valid. if no file is found, or if access to file is denied, we assume the server operator wants all bans dropped.
This commit is contained in:
Kawe Mazidjatari 2022-08-29 17:03:14 +02:00
parent 23e85cae37
commit 71c5ff2416

View File

@ -33,12 +33,16 @@ void CBanSystem::operator[](std::pair<const string&, const uint64_t> pair)
void CBanSystem::Load(void) void CBanSystem::Load(void)
{ {
fs::path path = std::filesystem::current_path() /= "platform\\banlist.json"; // !TODO: Use FS "PLATFORM" fs::path path = std::filesystem::current_path() /= "platform\\banlist.json"; // !TODO: Use FS "PLATFORM"
nlohmann::json jsIn; nlohmann::json jsIn;
ifstream banFile(path, std::ios::in);
ifstream banFile(path, std::ios::in);
int nTotalBans = 0; int nTotalBans = 0;
if (IsBanListValid())
{
m_vBanList.clear();
}
if (banFile.good() && banFile) if (banFile.good() && banFile)
{ {
banFile >> jsIn; // Into json. banFile >> jsIn; // Into json.
@ -60,17 +64,12 @@ void CBanSystem::Load(void)
continue; continue;
} }
uint64_t nNucleusID = jsEntry["nucleusID"].get<uint64_t>(); uint64_t nNucleusID = jsEntry["nucleusId"].get<uint64_t>();
string svIpAddress = jsEntry["ipAddress"].get<string>(); string svIpAddress = jsEntry["ipAddress"].get<string>();
m_vBanList.push_back(std::make_pair(svIpAddress, nNucleusID)); m_vBanList.push_back(std::make_pair(svIpAddress, nNucleusID));
} }
} }
else
{
// File no longer accessible, assume they want all bans dropped.
m_vBanList.clear();
}
} }
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
@ -82,10 +81,10 @@ void CBanSystem::Save(void) const
for (size_t i = 0; i < m_vBanList.size(); i++) for (size_t i = 0; i < m_vBanList.size(); i++)
{ {
jsOut["totalBans"] = m_vBanList.size();
jsOut[std::to_string(i)]["ipAddress"] = m_vBanList[i].first; jsOut[std::to_string(i)]["ipAddress"] = m_vBanList[i].first;
jsOut[std::to_string(i)]["nucleusID"] = m_vBanList[i].second; jsOut[std::to_string(i)]["nucleusId"] = m_vBanList[i].second;
} }
jsOut["totalBans"] = m_vBanList.size();
fs::path path = std::filesystem::current_path() /= "platform\\banlist.json"; // !TODO: Use FS "PLATFORM". fs::path path = std::filesystem::current_path() /= "platform\\banlist.json"; // !TODO: Use FS "PLATFORM".
ofstream outFile(path, std::ios::out | std::ios::trunc); // Write config file.. ofstream outFile(path, std::ios::out | std::ios::trunc); // Write config file..