This commit is contained in:
2025-08-25 17:13:47 +06:00
parent c06683cd57
commit 36346c9798
5 changed files with 249 additions and 338 deletions

View File

@@ -3,11 +3,8 @@
#include "Common/Abstract.hpp"
#include "TOSLib.hpp"
#include "Common/Net.hpp"
#include "assets.hpp"
#include "sha2.hpp"
#include <bitset>
#include <boost/interprocess/file_mapping.hpp>
#include <boost/interprocess/mapped_region.hpp>
#include <filesystem>
#include <optional>
#include <unordered_map>
@@ -18,60 +15,6 @@ namespace LV::Server {
namespace fs = std::filesystem;
struct Resource {
private:
struct InlineMMap {
boost::interprocess::file_mapping MMap;
boost::interprocess::mapped_region Region;
Hash_t Hash;
InlineMMap(fs::path path)
: MMap(path.c_str(), boost::interprocess::read_only),
Region(MMap, boost::interprocess::read_only)
{
Hash = sha2::sha256((const uint8_t*) Region.get_address(), Region.get_size());
}
const std::byte* data() const { return (const std::byte*) Region.get_address(); }
size_t size() const { return Region.get_size(); }
};
struct InlinePtr {
std::vector<uint8_t> Data;
Hash_t Hash;
InlinePtr(const uint8_t* data, size_t size) {
Data.resize(size);
std::copy(data, data+size, Data.data());
Hash = sha2::sha256(data, size);
}
const std::byte* data() const { return (const std::byte*) Data.data(); }
size_t size() const { return Data.size(); }
};
std::shared_ptr<std::variant<InlineMMap, InlinePtr>> In;
public:
Resource(fs::path path)
: In(std::make_shared<std::variant<InlineMMap, InlinePtr>>(InlineMMap(path)))
{}
Resource(const uint8_t* data, size_t size)
: In(std::make_shared<std::variant<InlineMMap, InlinePtr>>(InlinePtr(data, size)))
{}
Resource(const Resource&) = default;
Resource(Resource&&) = default;
Resource& operator=(const Resource&) = default;
Resource& operator=(Resource&&) = default;
bool operator<=>(const Resource&) const = default;
const std::byte* data() const { return std::visit<const std::byte*>([](auto& obj){ return obj.data(); }, *In); }
size_t size() const { return std::visit<size_t>([](auto& obj){ return obj.size(); }, *In); }
Hash_t hash() const { return std::visit<Hash_t>([](auto& obj){ return obj.Hash; }, *In); }
};
/*
Используется для расчёта коллизии,
если это необходимо.