Files
PDModdingCommunity_PD-Loader/source-code/source/plugins/Patches/databank.cpp
T
2025-03-05 21:49:41 +01:00

133 lines
3.7 KiB
C++

#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
#include "databank.h"
#include "urlutil.h"
namespace Databank
{
bool PvList::readFromStr(std::string& str)
{
std::string decoded = urlDecode(urlDecode(str));
std::vector<std::string> tokens;
std::stringstream ss(decoded);
std::string token;
while (std::getline(ss, token, ','))
{
if (!token.empty())
{
tokens.push_back(token);
}
}
if (tokens.size() % 7 != 0)
{
std::cerr << "[Patches] Databank error: Token count (" << tokens.size() << ") is not a multiple of 7." << std::endl;
return false;
}
for (size_t i = 0; i < tokens.size(); i += 7)
{
try
{
PvEntry entry;
int id = std::stoi(tokens[i]);
entry.unk1 = std::stoi(tokens[i + 1]);
entry.unk2 = std::stoi(tokens[i + 2]);
entry.advStart = tokens[i + 3];
entry.advEnd = tokens[i + 4];
entry.start = tokens[i + 5];
entry.end = tokens[i + 6];
entries.insert({ id, entry });
}
catch (const std::exception& e)
{
std::cerr << "[Patches] Databank parse error: " << e.what() << std::endl;
return false;
}
}
return true;
}
bool PvList::readFromFile(const std::string& filename)
{
try
{
std::ifstream file(filename, std::ios::in | std::ios::binary);
if (!file)
{
std::cerr << "[Patches] Failed to open databank " + filename << std::endl;
return false;
}
std::string contents = std::string((std::istreambuf_iterator<char>(file)), std::istreambuf_iterator<char>());
return readFromStr(contents);
}
catch (const std::exception& e)
{
std::cerr << "[Patches] Databank file read error: " << e.what() << std::endl;
return false;
}
}
void PvList::addEntry(int id, PvEntry& entry)
{
entries.insert({ id, entry });
}
void PvList::generateMissingEntries()
{
PvEntry entry = PvEntry();
for (int i = 0; i <= 999; i++)
{
entry.advStart = "2000-01-01";
entry.advEnd = "2029-01-01";
entry.start = "2000-01-01";
entry.end = "2029-01-01";
entries.insert({ i, entry });
}
}
std::string PvList::finalize()
{
std::ostringstream csvStream;
if (entries.empty())
{
return "%2A%2A%2A"; // ***
//checksum = "5341";
}
else
{
bool first = true;
for (const auto& entryMap : entries)
{
std::ostringstream entryStream;
if (!first)
{
csvStream << ",";
}
first = false;
const PvEntry& entry = entryMap.second;
entryStream << entryMap.first << ","
<< entry.unk1 << ","
<< entry.unk2 << ","
<< entry.advStart << ","
<< entry.advEnd << ","
<< entry.start << ","
<< entry.end;
csvStream << urlEncode(entryStream.str());
}
return urlEncode(csvStream.str());
//checksum = std::to_string(crc16_ccitt(finalString));
}
}
}