2020-11-11 09:18:35 +01:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <hex.hpp>
|
|
|
|
|
2020-11-22 19:43:35 +01:00
|
|
|
#include <cmath>
|
2020-11-27 09:09:48 +01:00
|
|
|
#include <concepts>
|
|
|
|
#include <map>
|
2020-11-23 13:10:14 +01:00
|
|
|
#include <optional>
|
2020-11-15 16:06:10 +01:00
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
|
|
|
|
2020-11-11 09:18:35 +01:00
|
|
|
namespace hex::prv {
|
|
|
|
|
|
|
|
class Provider {
|
|
|
|
public:
|
2020-11-22 19:43:35 +01:00
|
|
|
constexpr static size_t PageSize = 0x1000'0000;
|
|
|
|
|
2020-11-27 09:09:48 +01:00
|
|
|
Provider() {
|
|
|
|
this->m_patches.emplace_back();
|
|
|
|
}
|
|
|
|
|
2020-11-11 09:28:44 +01:00
|
|
|
virtual ~Provider() = default;
|
2020-11-11 09:18:35 +01:00
|
|
|
|
|
|
|
virtual bool isAvailable() = 0;
|
|
|
|
virtual bool isReadable() = 0;
|
|
|
|
virtual bool isWritable() = 0;
|
|
|
|
|
2020-11-27 09:09:48 +01:00
|
|
|
virtual void read(u64 offset, void *buffer, size_t size) { this->readRaw(offset, buffer, size); }
|
2020-11-27 13:44:52 +01:00
|
|
|
virtual void write(u64 offset, const void *buffer, size_t size) { this->writeRaw(offset, buffer, size); }
|
2020-11-27 09:09:48 +01:00
|
|
|
|
|
|
|
virtual void readRaw(u64 offset, void *buffer, size_t size) = 0;
|
2020-11-27 13:44:52 +01:00
|
|
|
virtual void writeRaw(u64 offset, const void *buffer, size_t size) = 0;
|
2020-11-22 19:43:35 +01:00
|
|
|
virtual size_t getActualSize() = 0;
|
|
|
|
|
2020-11-27 09:09:48 +01:00
|
|
|
const std::map<u64, u8>& getPatches() { return this->m_patches.back(); }
|
|
|
|
void applyPatches() {
|
|
|
|
for (auto &[patchAddress, patch] : this->m_patches.back())
|
|
|
|
this->writeRaw(patchAddress, &patch, 1);
|
|
|
|
}
|
|
|
|
|
2020-11-22 19:43:35 +01:00
|
|
|
u32 getPageCount() { return std::ceil(this->getActualSize() / double(PageSize)); }
|
|
|
|
u32 getCurrentPage() const { return this->m_currPage; }
|
|
|
|
void setCurrentPage(u32 page) { if (page < getPageCount()) this->m_currPage = page; }
|
|
|
|
|
|
|
|
virtual size_t getBaseAddress() {
|
|
|
|
return PageSize * this->m_currPage;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual size_t getSize() {
|
|
|
|
return std::min(this->getActualSize() - PageSize * this->m_currPage, PageSize);
|
|
|
|
}
|
2020-11-15 16:06:10 +01:00
|
|
|
|
2020-11-23 13:10:14 +01:00
|
|
|
virtual std::optional<u32> getPageOfAddress(u64 address) {
|
|
|
|
u32 page = std::floor(address / double(PageSize));
|
|
|
|
|
|
|
|
if (page >= this->getPageCount())
|
|
|
|
return { };
|
|
|
|
|
|
|
|
return page;
|
|
|
|
}
|
|
|
|
|
2020-11-15 16:06:10 +01:00
|
|
|
virtual std::vector<std::pair<std::string, std::string>> getDataInformation() = 0;
|
2020-11-22 19:43:35 +01:00
|
|
|
|
|
|
|
protected:
|
|
|
|
u32 m_currPage = 0;
|
2020-11-27 09:09:48 +01:00
|
|
|
|
|
|
|
std::vector<std::map<u64, u8>> m_patches;
|
2020-11-11 09:18:35 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|