2021-12-07 22:47:41 +01:00
|
|
|
#include <hex/helpers/encoding_file.hpp>
|
2021-02-14 01:11:55 +01:00
|
|
|
|
|
|
|
#include <hex/helpers/utils.hpp>
|
2023-03-13 08:58:08 +01:00
|
|
|
|
2023-03-12 18:27:29 +01:00
|
|
|
#include <wolv/io/file.hpp>
|
2023-03-13 08:58:08 +01:00
|
|
|
#include <wolv/utils/string.hpp>
|
2021-02-14 01:11:55 +01:00
|
|
|
|
|
|
|
namespace hex {
|
|
|
|
|
2023-06-21 23:58:21 +02:00
|
|
|
EncodingFile::EncodingFile() : m_mapping(std::make_unique<std::map<size_t, std::map<std::vector<u8>, std::string>>>()) {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
EncodingFile::EncodingFile(const hex::EncodingFile &other) {
|
2023-12-19 13:10:25 +01:00
|
|
|
m_mapping = std::make_unique<std::map<size_t, std::map<std::vector<u8>, std::string>>>(*other.m_mapping);
|
|
|
|
m_tableContent = other.m_tableContent;
|
|
|
|
m_longestSequence = other.m_longestSequence;
|
|
|
|
m_valid = other.m_valid;
|
|
|
|
m_name = other.m_name;
|
2023-06-21 23:58:21 +02:00
|
|
|
}
|
|
|
|
|
2023-11-10 20:47:08 +01:00
|
|
|
EncodingFile::EncodingFile(EncodingFile &&other) noexcept {
|
2023-12-19 13:10:25 +01:00
|
|
|
m_mapping = std::move(other.m_mapping);
|
|
|
|
m_tableContent = std::move(other.m_tableContent);
|
|
|
|
m_longestSequence = other.m_longestSequence;
|
|
|
|
m_valid = other.m_valid;
|
|
|
|
m_name = std::move(other.m_name);
|
2023-06-21 23:58:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
EncodingFile::EncodingFile(Type type, const std::fs::path &path) : EncodingFile() {
|
2023-03-12 18:27:29 +01:00
|
|
|
auto file = wolv::io::File(path, wolv::io::File::Mode::Read);
|
2021-02-14 01:11:55 +01:00
|
|
|
switch (type) {
|
2022-02-01 22:09:44 +01:00
|
|
|
case Type::Thingy:
|
2023-03-31 11:06:51 +02:00
|
|
|
parse(file.readString());
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-07-11 00:04:26 +02:00
|
|
|
{
|
2023-12-19 13:10:25 +01:00
|
|
|
m_name = path.stem().string();
|
|
|
|
m_name = wolv::util::replaceStrings(m_name, "_", " ");
|
2023-07-11 00:04:26 +02:00
|
|
|
|
2023-12-19 13:10:25 +01:00
|
|
|
if (!m_name.empty())
|
|
|
|
m_name[0] = std::toupper(m_name[0]);
|
2023-07-11 00:04:26 +02:00
|
|
|
}
|
|
|
|
|
2023-12-19 13:10:25 +01:00
|
|
|
m_valid = true;
|
2023-03-31 11:06:51 +02:00
|
|
|
}
|
|
|
|
|
2023-06-21 23:58:21 +02:00
|
|
|
EncodingFile::EncodingFile(Type type, const std::string &content) : EncodingFile() {
|
2023-03-31 11:06:51 +02:00
|
|
|
switch (type) {
|
|
|
|
case Type::Thingy:
|
|
|
|
parse(content);
|
2022-02-01 22:09:44 +01:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return;
|
2021-02-14 01:11:55 +01:00
|
|
|
}
|
2022-01-15 23:44:15 +01:00
|
|
|
|
2023-12-19 13:10:25 +01:00
|
|
|
m_name = "Unknown";
|
|
|
|
m_valid = true;
|
2021-02-14 01:11:55 +01:00
|
|
|
}
|
|
|
|
|
2023-06-21 23:58:21 +02:00
|
|
|
|
|
|
|
EncodingFile &EncodingFile::operator=(const hex::EncodingFile &other) {
|
2023-12-19 13:10:25 +01:00
|
|
|
m_mapping = std::make_unique<std::map<size_t, std::map<std::vector<u8>, std::string>>>(*other.m_mapping);
|
|
|
|
m_tableContent = other.m_tableContent;
|
|
|
|
m_longestSequence = other.m_longestSequence;
|
|
|
|
m_valid = other.m_valid;
|
|
|
|
m_name = other.m_name;
|
2023-06-21 23:58:21 +02:00
|
|
|
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2023-11-10 20:47:08 +01:00
|
|
|
EncodingFile &EncodingFile::operator=(EncodingFile &&other) noexcept {
|
2023-12-19 13:10:25 +01:00
|
|
|
m_mapping = std::move(other.m_mapping);
|
|
|
|
m_tableContent = std::move(other.m_tableContent);
|
|
|
|
m_longestSequence = other.m_longestSequence;
|
|
|
|
m_valid = other.m_valid;
|
|
|
|
m_name = std::move(other.m_name);
|
2023-06-21 23:58:21 +02:00
|
|
|
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2023-01-28 21:12:35 +01:00
|
|
|
std::pair<std::string_view, size_t> EncodingFile::getEncodingFor(std::span<u8> buffer) const {
|
2023-12-19 13:10:25 +01:00
|
|
|
for (auto riter = m_mapping->crbegin(); riter != m_mapping->crend(); ++riter) {
|
2021-10-09 23:07:58 +02:00
|
|
|
const auto &[size, mapping] = *riter;
|
|
|
|
|
2021-02-14 01:11:55 +01:00
|
|
|
if (size > buffer.size()) continue;
|
|
|
|
|
2023-11-10 20:47:08 +01:00
|
|
|
std::vector key(buffer.begin(), buffer.begin() + size);
|
2021-02-14 01:11:55 +01:00
|
|
|
if (mapping.contains(key))
|
|
|
|
return { mapping.at(key), size };
|
|
|
|
}
|
|
|
|
|
|
|
|
return { ".", 1 };
|
|
|
|
}
|
|
|
|
|
2023-03-14 09:35:43 +01:00
|
|
|
size_t EncodingFile::getEncodingLengthFor(std::span<u8> buffer) const {
|
2023-12-19 13:10:25 +01:00
|
|
|
for (auto riter = m_mapping->crbegin(); riter != m_mapping->crend(); ++riter) {
|
2023-03-14 09:35:43 +01:00
|
|
|
const auto &[size, mapping] = *riter;
|
|
|
|
|
|
|
|
if (size > buffer.size()) continue;
|
|
|
|
|
2023-11-10 20:47:08 +01:00
|
|
|
std::vector key(buffer.begin(), buffer.begin() + size);
|
2023-03-14 09:35:43 +01:00
|
|
|
if (mapping.contains(key))
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2023-03-31 11:06:51 +02:00
|
|
|
void EncodingFile::parse(const std::string &content) {
|
2023-12-19 13:10:25 +01:00
|
|
|
m_tableContent = content;
|
|
|
|
for (const auto &line : splitString(m_tableContent, "\n")) {
|
2021-02-14 01:11:55 +01:00
|
|
|
|
2021-02-14 12:05:58 +01:00
|
|
|
std::string from, to;
|
|
|
|
{
|
2022-05-17 20:46:42 +02:00
|
|
|
auto delimiterPos = line.find('=');
|
2021-02-14 01:11:55 +01:00
|
|
|
|
2022-05-27 20:42:07 +02:00
|
|
|
if (delimiterPos >= line.length())
|
2022-05-17 20:46:42 +02:00
|
|
|
continue;
|
2021-02-14 01:11:55 +01:00
|
|
|
|
2021-02-14 12:05:58 +01:00
|
|
|
from = line.substr(0, delimiterPos);
|
2022-02-01 22:09:44 +01:00
|
|
|
to = line.substr(delimiterPos + 1);
|
2021-02-14 12:05:58 +01:00
|
|
|
|
2021-02-14 12:32:48 +01:00
|
|
|
if (from.empty()) continue;
|
2021-02-14 12:05:58 +01:00
|
|
|
}
|
2021-02-14 01:11:55 +01:00
|
|
|
|
|
|
|
auto fromBytes = hex::parseByteString(from);
|
2021-02-14 12:32:48 +01:00
|
|
|
if (fromBytes.empty()) continue;
|
|
|
|
|
2022-07-16 12:14:06 +02:00
|
|
|
if (to.length() > 1)
|
2023-03-13 08:58:08 +01:00
|
|
|
to = wolv::util::trim(to);
|
2022-07-16 12:14:06 +02:00
|
|
|
if (to.empty())
|
|
|
|
to = " ";
|
|
|
|
|
2023-12-19 13:10:25 +01:00
|
|
|
if (!m_mapping->contains(fromBytes.size()))
|
|
|
|
m_mapping->insert({ fromBytes.size(), {} });
|
2021-02-14 01:11:55 +01:00
|
|
|
|
2023-03-14 09:35:43 +01:00
|
|
|
auto keySize = fromBytes.size();
|
2023-12-19 13:10:25 +01:00
|
|
|
(*m_mapping)[keySize].insert({ std::move(fromBytes), to });
|
2023-03-14 09:35:43 +01:00
|
|
|
|
2023-12-19 13:10:25 +01:00
|
|
|
m_longestSequence = std::max(m_longestSequence, keySize);
|
2021-02-14 01:11:55 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|