Compare commits
14 Commits
ad4b0d593a
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
| 0b8326e278 | |||
| 07ccd4dd68 | |||
| da673b0965 | |||
| 3fb06080db | |||
| affdc75ebd | |||
| 49c4d77c59 | |||
| 16a0fa5f7a | |||
| a29e772f35 | |||
| 5135aa30a7 | |||
| 523f9725c0 | |||
| c13ad06ba9 | |||
| 83530a6c15 | |||
| b61cc9fb03 | |||
| 7224499d14 |
@@ -84,6 +84,16 @@ FetchContent_Declare(
|
||||
FetchContent_MakeAvailable(Boost)
|
||||
target_link_libraries(luavox_common INTERFACE Boost::asio Boost::thread Boost::json Boost::iostreams Boost::interprocess Boost::timer Boost::circular_buffer Boost::lockfree Boost::stacktrace Boost::uuid Boost::serialization Boost::nowide)
|
||||
|
||||
# unordered_dense
|
||||
FetchContent_Declare(
|
||||
unordered_dense
|
||||
GIT_REPOSITORY https://github.com/martinus/unordered_dense.git
|
||||
GIT_TAG v4.8.1
|
||||
)
|
||||
FetchContent_MakeAvailable(unordered_dense)
|
||||
|
||||
target_link_libraries(luavox_common INTERFACE unordered_dense::unordered_dense)
|
||||
|
||||
# glm
|
||||
# find_package(glm REQUIRED)
|
||||
# target_include_directories(${PROJECT_NAME} PUBLIC ${GLM_INCLUDE_DIR})
|
||||
|
||||
@@ -1,9 +1,13 @@
|
||||
#pragma once
|
||||
|
||||
#include "Common/Net.hpp"
|
||||
#include <cstdint>
|
||||
#include <functional>
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
#include <unordered_map>
|
||||
#include <unordered_set>
|
||||
#include <variant>
|
||||
#include <vector>
|
||||
#include <Common/Abstract.hpp>
|
||||
|
||||
@@ -60,15 +64,40 @@ public:
|
||||
// states
|
||||
};
|
||||
|
||||
struct AssetsModelUpdate {
|
||||
ResourceId Id = 0;
|
||||
HeadlessModel Model;
|
||||
HeadlessModel::Header Header;
|
||||
};
|
||||
|
||||
struct AssetsNodestateUpdate {
|
||||
ResourceId Id = 0;
|
||||
HeadlessNodeState Nodestate;
|
||||
HeadlessNodeState::Header Header;
|
||||
};
|
||||
|
||||
struct AssetsTextureUpdate {
|
||||
ResourceId Id = 0;
|
||||
uint16_t Width = 0;
|
||||
uint16_t Height = 0;
|
||||
std::vector<uint32_t> Pixels;
|
||||
ResourceHeader Header;
|
||||
};
|
||||
|
||||
struct AssetsBinaryUpdate {
|
||||
ResourceId Id = 0;
|
||||
std::u8string Data;
|
||||
};
|
||||
|
||||
/* Интерфейс рендера текущего подключения к серверу */
|
||||
class IRenderSession {
|
||||
public:
|
||||
// Объект уведомления об изменениях
|
||||
struct TickSyncData {
|
||||
// Новые или изменённые используемые теперь двоичные ресурсы
|
||||
std::unordered_map<EnumAssets, std::vector<ResourceId>> Assets_ChangeOrAdd;
|
||||
// Более не используемые ресурсы
|
||||
std::unordered_map<EnumAssets, std::vector<ResourceId>> Assets_Lost;
|
||||
// Изменения в ассетах.
|
||||
std::vector<AssetsModelUpdate> AssetsModels;
|
||||
std::vector<AssetsNodestateUpdate> AssetsNodestates;
|
||||
std::vector<AssetsTextureUpdate> AssetsTextures;
|
||||
|
||||
// Новые или изменённые профили контента
|
||||
std::unordered_map<EnumDefContent, std::vector<ResourceId>> Profiles_ChangeOrAdd;
|
||||
@@ -87,7 +116,7 @@ public:
|
||||
// Началась стадия изменения данных IServerSession, все должны приостановить работу
|
||||
virtual void pushStageTickSync() = 0;
|
||||
// После изменения внутренних данных IServerSession, IRenderSession уведомляется об изменениях
|
||||
virtual void tickSync(const TickSyncData& data) = 0;
|
||||
virtual void tickSync(TickSyncData& data) = 0;
|
||||
|
||||
// Установить позицию для камеры
|
||||
virtual void setCameraPos(WorldId_t worldId, Pos::Object pos, glm::quat quat) = 0;
|
||||
@@ -124,14 +153,6 @@ struct WorldInfo {
|
||||
std::unordered_map<Pos::GlobalRegion, Region> Regions;
|
||||
};
|
||||
|
||||
struct VoxelInfo {
|
||||
|
||||
};
|
||||
|
||||
struct NodeInfo {
|
||||
|
||||
};
|
||||
|
||||
struct PortalInfo {
|
||||
|
||||
};
|
||||
@@ -143,28 +164,96 @@ struct EntityInfo {
|
||||
glm::quat Quat = glm::quat(1.f, 0.f, 0.f, 0.f);
|
||||
};
|
||||
|
||||
struct FuncEntityInfo {
|
||||
/*
|
||||
Конструируются с серверными идентификаторами
|
||||
*/
|
||||
|
||||
struct DefVoxel {
|
||||
DefVoxel() = default;
|
||||
DefVoxel(const std::u8string_view view) {
|
||||
|
||||
}
|
||||
|
||||
void reBind(const std::function<ResourceId(EnumAssets, ResourceId)>& am) {
|
||||
|
||||
}
|
||||
};
|
||||
|
||||
struct DefItemInfo {
|
||||
struct DefNode {
|
||||
std::variant<AssetsNodestate> RenderStates;
|
||||
|
||||
DefNode() = default;
|
||||
DefNode(const std::u8string_view view) {
|
||||
Net::LinearReader lr(view);
|
||||
RenderStates = lr.read<uint32_t>();
|
||||
}
|
||||
|
||||
void reBind(const std::function<ResourceId(EnumAssets, ResourceId)>& am) {
|
||||
RenderStates = am(EnumAssets::Nodestate, std::get<AssetsNodestate>(RenderStates));
|
||||
}
|
||||
};
|
||||
|
||||
struct DefVoxel_t {};
|
||||
struct DefNode_t {
|
||||
AssetsNodestate NodestateId = 0;
|
||||
AssetsTexture TexId = 0;
|
||||
struct DefWorld {
|
||||
DefWorld() = default;
|
||||
DefWorld(const std::u8string_view view) {
|
||||
|
||||
}
|
||||
|
||||
void reBind(const std::function<ResourceId(EnumAssets, ResourceId)>& am) {
|
||||
|
||||
}
|
||||
};
|
||||
|
||||
struct DefPortal {
|
||||
DefPortal() = default;
|
||||
DefPortal(const std::u8string_view view) {
|
||||
|
||||
}
|
||||
|
||||
void reBind(const std::function<ResourceId(EnumAssets, ResourceId)>& am) {
|
||||
|
||||
}
|
||||
};
|
||||
|
||||
struct DefEntity {
|
||||
DefEntity() = default;
|
||||
DefEntity(const std::u8string_view view) {
|
||||
|
||||
}
|
||||
|
||||
void reBind(const std::function<ResourceId(EnumAssets, ResourceId)>& am) {
|
||||
|
||||
}
|
||||
};
|
||||
|
||||
struct DefItem {
|
||||
DefItem() = default;
|
||||
DefItem(const std::u8string_view view) {
|
||||
|
||||
}
|
||||
|
||||
void reBind(const std::function<ResourceId(EnumAssets, ResourceId)>& am) {
|
||||
|
||||
}
|
||||
};
|
||||
|
||||
struct AssetEntry {
|
||||
EnumAssets Type;
|
||||
ResourceId Id;
|
||||
std::string Domain, Key;
|
||||
Resource Res;
|
||||
Hash_t Hash = {};
|
||||
std::vector<uint8_t> Dependencies;
|
||||
ResourceId Id = 0;
|
||||
std::string Domain;
|
||||
std::string Key;
|
||||
|
||||
HeadlessModel Model;
|
||||
HeadlessModel::Header ModelHeader;
|
||||
|
||||
HeadlessNodeState Nodestate;
|
||||
HeadlessNodeState::Header NodestateHeader;
|
||||
|
||||
uint16_t Width = 0;
|
||||
uint16_t Height = 0;
|
||||
std::vector<uint32_t> Pixels;
|
||||
ResourceHeader Header;
|
||||
|
||||
std::u8string Data;
|
||||
};
|
||||
|
||||
/*
|
||||
@@ -180,17 +269,14 @@ public:
|
||||
// Включить логирование входящих сетевых пакетов на клиенте.
|
||||
bool DebugLogPackets = false;
|
||||
|
||||
// Используемые двоичные ресурсы
|
||||
std::unordered_map<EnumAssets, std::unordered_map<ResourceId, AssetEntry>> Assets;
|
||||
|
||||
// Используемые профили контента
|
||||
struct {
|
||||
std::unordered_map<DefVoxelId, DefVoxel_t> DefVoxel;
|
||||
std::unordered_map<DefNodeId, DefNode_t> DefNode;
|
||||
std::unordered_map<DefWorldId, DefWorldInfo> DefWorld;
|
||||
std::unordered_map<DefPortalId, DefPortalInfo> DefPortal;
|
||||
std::unordered_map<DefEntityId, DefEntityInfo> DefEntity;
|
||||
std::unordered_map<DefItemId, DefItemInfo> DefItem;
|
||||
std::unordered_map<DefVoxelId, DefVoxel> DefVoxels;
|
||||
std::unordered_map<DefNodeId, DefNode> DefNodes;
|
||||
std::unordered_map<DefWorldId, DefWorld> DefWorlds;
|
||||
std::unordered_map<DefPortalId, DefPortal> DefPortals;
|
||||
std::unordered_map<DefEntityId, DefEntity> DefEntitys;
|
||||
std::unordered_map<DefItemId, DefItem> DefItems;
|
||||
} Profiles;
|
||||
|
||||
// Видимый контент
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
#pragma once
|
||||
|
||||
#include <algorithm>
|
||||
#include <array>
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
@@ -9,285 +10,629 @@
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
#include <unordered_map>
|
||||
#include <unordered_set>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
#include <cstring>
|
||||
#include "Client/AssetsCacheManager.hpp"
|
||||
#include "Client/AssetsHeaderCodec.hpp"
|
||||
#include "Common/Abstract.hpp"
|
||||
#include "Common/IdProvider.hpp"
|
||||
#include "Common/AssetsPreloader.hpp"
|
||||
#include "Common/TexturePipelineProgram.hpp"
|
||||
#include "TOSLib.hpp"
|
||||
#include "assets.hpp"
|
||||
#include "boost/asio/io_context.hpp"
|
||||
#include "png++/image.hpp"
|
||||
#include <fstream>
|
||||
#include "Abstract.hpp"
|
||||
|
||||
namespace LV::Client {
|
||||
|
||||
namespace fs = std::filesystem;
|
||||
|
||||
class AssetsManager {
|
||||
class AssetsManager : public IdProvider<EnumAssets> {
|
||||
public:
|
||||
using Ptr = std::shared_ptr<AssetsManager>;
|
||||
using AssetType = EnumAssets;
|
||||
using AssetId = ResourceId;
|
||||
struct ResourceUpdates {
|
||||
|
||||
// Ключ запроса ресурса (идентификация + хеш для поиска источника).
|
||||
struct ResourceKey {
|
||||
// Хеш ресурса, используемый для поиска в источниках и кэше.
|
||||
Hash_t Hash{};
|
||||
// Тип ресурса (модель, текстура и т.д.).
|
||||
AssetType Type{};
|
||||
// Домен ресурса.
|
||||
std::string Domain;
|
||||
// Ключ ресурса внутри домена.
|
||||
std::string Key;
|
||||
// Идентификатор ресурса на стороне клиента/локальный.
|
||||
AssetId Id = 0;
|
||||
std::vector<AssetsModelUpdate> Models;
|
||||
std::vector<AssetsNodestateUpdate> Nodestates;
|
||||
std::vector<AssetsTextureUpdate> Textures;
|
||||
std::vector<AssetsBinaryUpdate> Particles;
|
||||
std::vector<AssetsBinaryUpdate> Animations;
|
||||
std::vector<AssetsBinaryUpdate> Sounds;
|
||||
std::vector<AssetsBinaryUpdate> Fonts;
|
||||
};
|
||||
|
||||
// Информация о биндинге серверного ресурса на локальный id.
|
||||
struct BindInfo {
|
||||
// Тип ресурса.
|
||||
AssetType Type{};
|
||||
// Локальный идентификатор.
|
||||
AssetId LocalId = 0;
|
||||
// Домен ресурса.
|
||||
std::string Domain;
|
||||
// Ключ ресурса.
|
||||
std::string Key;
|
||||
// Хеш ресурса.
|
||||
Hash_t Hash{};
|
||||
// Бинарный заголовок с зависимостями.
|
||||
std::vector<uint8_t> Header;
|
||||
};
|
||||
|
||||
// Результат биндинга ресурса сервера.
|
||||
struct BindResult {
|
||||
// Итоговый локальный идентификатор.
|
||||
AssetId LocalId = 0;
|
||||
// Признак изменения бинда (хеш/заголовок).
|
||||
bool Changed = false;
|
||||
// Признак новой привязки.
|
||||
bool NewBinding = false;
|
||||
// Идентификатор, от которого произошёл ребинд (если был).
|
||||
std::optional<AssetId> ReboundFrom;
|
||||
};
|
||||
|
||||
// Регистрация набора ресурспаков.
|
||||
struct PackRegister {
|
||||
// Пути до паков (директории/архивы).
|
||||
std::vector<fs::path> Packs;
|
||||
};
|
||||
|
||||
// Ресурс, собранный из пака.
|
||||
struct PackResource {
|
||||
// Тип ресурса.
|
||||
AssetType Type{};
|
||||
// Локальный идентификатор.
|
||||
AssetId LocalId = 0;
|
||||
// Домен ресурса.
|
||||
std::string Domain;
|
||||
// Ключ ресурса.
|
||||
std::string Key;
|
||||
// Тело ресурса.
|
||||
Resource Res;
|
||||
// Хеш ресурса.
|
||||
Hash_t Hash{};
|
||||
// Заголовок ресурса (например, зависимости).
|
||||
std::u8string Header;
|
||||
};
|
||||
|
||||
// Результат пересканирования паков.
|
||||
struct PackReloadResult {
|
||||
// Добавленные/изменённые ресурсы по типам.
|
||||
std::array<std::vector<AssetId>, static_cast<size_t>(AssetType::MAX_ENUM)> ChangeOrAdd;
|
||||
// Потерянные ресурсы по типам.
|
||||
std::array<std::vector<AssetId>, static_cast<size_t>(AssetType::MAX_ENUM)> Lost;
|
||||
};
|
||||
|
||||
using ParsedHeader = AssetsHeaderCodec::ParsedHeader;
|
||||
|
||||
// Фабрика с настройкой лимитов кэша.
|
||||
static Ptr Create(asio::io_context& ioc, const fs::path& cachePath,
|
||||
size_t maxCacheDirectorySize = 8 * 1024 * 1024 * 1024ULL,
|
||||
size_t maxLifeTime = 7 * 24 * 60 * 60) {
|
||||
return Ptr(new AssetsManager(ioc, cachePath, maxCacheDirectorySize, maxLifeTime));
|
||||
public:
|
||||
AssetsManager(asio::io_context& ioc, fs::path cachePath)
|
||||
: Cache(AssetsCacheManager::Create(ioc, cachePath))
|
||||
{
|
||||
for(size_t type = 0; type < static_cast<size_t>(EnumAssets::MAX_ENUM); type++) {
|
||||
ServerToClientMap[type].push_back(0);
|
||||
}
|
||||
}
|
||||
|
||||
// Пересканировать ресурспаки и вернуть изменившиеся/утраченные ресурсы.
|
||||
PackReloadResult reloadPacks(const PackRegister& reg);
|
||||
// Ручные обновления
|
||||
struct Out_checkAndPrepareResourcesUpdate {
|
||||
AssetsPreloader::Out_checkAndPrepareResourcesUpdate RP, ES;
|
||||
|
||||
// Связать серверный ресурс с локальным id и записать метаданные.
|
||||
BindResult bindServerResource(AssetType type, AssetId serverId, std::string domain, std::string key,
|
||||
const Hash_t& hash, std::vector<uint8_t> header);
|
||||
// Отвязать серверный id и вернуть актуальный локальный id (если был).
|
||||
std::optional<AssetId> unbindServerResource(AssetType type, AssetId serverId);
|
||||
// Сбросить все серверные бинды.
|
||||
void clearServerBindings();
|
||||
std::unordered_map<ResourceFile::Hash_t, std::u8string> Files;
|
||||
};
|
||||
|
||||
// Получить данные бинда по локальному id.
|
||||
const BindInfo* getBind(AssetType type, AssetId localId) const;
|
||||
Out_checkAndPrepareResourcesUpdate checkAndPrepareResourcesUpdate(
|
||||
const std::vector<fs::path>& resourcePacks,
|
||||
const std::vector<fs::path>& extraSources
|
||||
) {
|
||||
Out_checkAndPrepareResourcesUpdate result;
|
||||
|
||||
// Перебиндить хедер, заменив id зависимостей.
|
||||
std::vector<uint8_t> rebindHeader(AssetType type, const std::vector<uint8_t>& header, bool serverIds = true);
|
||||
// Распарсить хедер ресурса.
|
||||
static std::optional<ParsedHeader> parseHeader(AssetType type, const std::vector<uint8_t>& header);
|
||||
result.RP = ResourcePacks.checkAndPrepareResourcesUpdate(
|
||||
AssetsPreloader::AssetsRegister{resourcePacks},
|
||||
[&](EnumAssets type, std::string_view domain, std::string_view key) -> ResourceId {
|
||||
return getId(type, domain, key);
|
||||
},
|
||||
[&](std::u8string&& data, ResourceFile::Hash_t hash, fs::path path) {
|
||||
result.Files.emplace(hash, std::move(data));
|
||||
}
|
||||
);
|
||||
|
||||
// Протолкнуть новые ресурсы в память и кэш.
|
||||
void pushResources(std::vector<Resource> resources);
|
||||
result.ES = ExtraSource.checkAndPrepareResourcesUpdate(
|
||||
AssetsPreloader::AssetsRegister{resourcePacks},
|
||||
[&](EnumAssets type, std::string_view domain, std::string_view key) -> ResourceId {
|
||||
return getId(type, domain, key);
|
||||
}
|
||||
);
|
||||
|
||||
// Поставить запросы чтения ресурсов.
|
||||
void pushReads(std::vector<ResourceKey> reads);
|
||||
// Получить готовые результаты чтения.
|
||||
std::vector<std::pair<ResourceKey, std::optional<Resource>>> pullReads();
|
||||
// Продвинуть асинхронные источники (кэш).
|
||||
void tickSources();
|
||||
return result;
|
||||
}
|
||||
|
||||
// Получить или создать локальный id по домену/ключу.
|
||||
AssetId getOrCreateLocalId(AssetType type, std::string_view domain, std::string_view key);
|
||||
// Получить локальный id по серверному id (если есть).
|
||||
std::optional<AssetId> getLocalIdFromServer(AssetType type, AssetId serverId) const;
|
||||
struct Out_applyResourcesUpdate {
|
||||
|
||||
};
|
||||
|
||||
Out_applyResourcesUpdate applyResourcesUpdate(const Out_checkAndPrepareResourcesUpdate& orr) {
|
||||
Out_applyResourcesUpdate result;
|
||||
|
||||
for(size_t type = 0; type < static_cast<size_t>(EnumAssets::MAX_ENUM); ++type) {
|
||||
for(ResourceId id : orr.RP.LostLinks[type]) {
|
||||
std::optional<AssetsPreloader::Out_Resource> res = ResourcePacks.getResource((EnumAssets) type, id);
|
||||
assert(res);
|
||||
|
||||
auto hashIter = HashToPath.find(res->Hash);
|
||||
assert(hashIter != HashToPath.end());
|
||||
auto& entry = hashIter->second;
|
||||
auto iter = std::find(entry.begin(), entry.end(), res->Path);
|
||||
assert(iter != entry.end());
|
||||
entry.erase(iter);
|
||||
|
||||
if(entry.empty())
|
||||
HashToPath.erase(hashIter);
|
||||
}
|
||||
}
|
||||
|
||||
ResourcePacks.applyResourcesUpdate(orr.RP);
|
||||
ExtraSource.applyResourcesUpdate(orr.ES);
|
||||
|
||||
std::unordered_set<ResourceFile::Hash_t> needHashes;
|
||||
|
||||
for(size_t type = 0; type < static_cast<size_t>(EnumAssets::MAX_ENUM); ++type) {
|
||||
for(const auto& res : orr.RP.ResourceUpdates[type]) {
|
||||
// Помечаем ресурс для обновления
|
||||
PendingUpdateFromAsync[type].push_back(std::get<ResourceId>(res));
|
||||
HashToPath[std::get<ResourceFile::Hash_t>(res)].push_back(std::get<fs::path>(res));
|
||||
}
|
||||
|
||||
for(ResourceId id : orr.RP.LostLinks[type]) {
|
||||
// Помечаем ресурс для обновления
|
||||
PendingUpdateFromAsync[type].push_back(id);
|
||||
|
||||
auto& hh = ServerIdToHH[type];
|
||||
if(id < hh.size())
|
||||
needHashes.insert(std::get<ResourceFile::Hash_t>(hh[id]));
|
||||
}
|
||||
}
|
||||
|
||||
{
|
||||
for(const auto& [hash, data] : orr.Files) {
|
||||
WaitingHashes.insert(hash);
|
||||
}
|
||||
|
||||
for(const auto& hash : WaitingHashes)
|
||||
needHashes.erase(hash);
|
||||
|
||||
std::vector<std::tuple<ResourceFile::Hash_t, fs::path>> toDisk;
|
||||
std::vector<ResourceFile::Hash_t> toCache;
|
||||
|
||||
// Теперь раскидаем хеши по доступным источникам.
|
||||
for(const auto& hash : needHashes) {
|
||||
auto iter = HashToPath.find(hash);
|
||||
if(iter != HashToPath.end()) {
|
||||
// Ставим задачу загрузить с диска.
|
||||
toDisk.emplace_back(hash, iter->second.front());
|
||||
} else {
|
||||
// Сделаем запрос в кеш.
|
||||
toCache.push_back(hash);
|
||||
}
|
||||
}
|
||||
|
||||
// Запоминаем, что эти ресурсы уже ожидаются.
|
||||
WaitingHashes.insert_range(needHashes);
|
||||
|
||||
// Запрос в кеш (если там не найдётся, то запрос уйдёт на сервер).
|
||||
if(!toCache.empty())
|
||||
Cache->pushReads(std::move(toCache));
|
||||
|
||||
// Запрос к диску.
|
||||
if(!toDisk.empty())
|
||||
NeedToReadFromDisk.append_range(std::move(toDisk));
|
||||
|
||||
_onHashLoad(orr.Files);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
// ServerSession
|
||||
// Новые привязки ассетов к Домен+Ключ.
|
||||
void pushAssetsBindDK(
|
||||
const std::vector<std::string>& domains,
|
||||
const std::array<
|
||||
std::vector<std::vector<std::string>>,
|
||||
static_cast<size_t>(EnumAssets::MAX_ENUM)
|
||||
>& keys
|
||||
) {
|
||||
LOG.debug() << "BindDK domains=" << domains.size();
|
||||
for(size_t type = 0; type < static_cast<size_t>(EnumAssets::MAX_ENUM); ++type) {
|
||||
LOG.info() << type;
|
||||
for(size_t forDomainIter = 0; forDomainIter < keys[type].size(); ++forDomainIter) {
|
||||
LOG.info() << "\t" << domains[forDomainIter];
|
||||
for(const std::string& key : keys[type][forDomainIter]) {
|
||||
uint32_t id = getId((EnumAssets) type, domains[forDomainIter], key);
|
||||
LOG.info() << "\t\t" << key << " -> " << id;
|
||||
ServerToClientMap[type].push_back(id);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Новые привязки ассетов к Hash+Header.
|
||||
void pushAssetsBindHH(
|
||||
std::array<
|
||||
std::vector<std::tuple<ResourceId, ResourceFile::Hash_t, ResourceHeader>>,
|
||||
static_cast<size_t>(EnumAssets::MAX_ENUM)
|
||||
>&& hash_and_headers
|
||||
) {
|
||||
std::unordered_set<ResourceFile::Hash_t> needHashes;
|
||||
|
||||
size_t totalBinds = 0;
|
||||
for(size_t type = 0; type < static_cast<size_t>(EnumAssets::MAX_ENUM); ++type) {
|
||||
size_t maxSize = 0;
|
||||
|
||||
for(auto& [id, hash, header] : hash_and_headers[type]) {
|
||||
totalBinds++;
|
||||
assert(id < ServerToClientMap[type].size());
|
||||
id = ServerToClientMap[type][id];
|
||||
|
||||
if(id >= maxSize)
|
||||
maxSize = id+1;
|
||||
|
||||
// Добавляем идентификатор в таблицу ожидающих обновлений.
|
||||
PendingUpdateFromAsync[type].push_back(id);
|
||||
|
||||
// Поискать есть ли ресурс в ресурспаках.
|
||||
std::optional<AssetsPreloader::Out_Resource> res = ResourcePacks.getResource((EnumAssets) type, id);
|
||||
if(res) {
|
||||
needHashes.insert(res->Hash);
|
||||
} else {
|
||||
needHashes.insert(hash);
|
||||
}
|
||||
}
|
||||
|
||||
{
|
||||
// Уберём повторения в идентификаторах.
|
||||
auto& vec = PendingUpdateFromAsync[type];
|
||||
std::sort(vec.begin(), vec.end());
|
||||
vec.erase(std::unique(vec.begin(), vec.end()), vec.end());
|
||||
}
|
||||
|
||||
if(ServerIdToHH[type].size() < maxSize)
|
||||
ServerIdToHH[type].resize(maxSize);
|
||||
|
||||
for(auto& [id, hash, header] : hash_and_headers[type]) {
|
||||
ServerIdToHH[type][id] = {hash, std::move(header)};
|
||||
}
|
||||
}
|
||||
|
||||
if(totalBinds)
|
||||
LOG.debug() << "BindHH total=" << totalBinds << " wait=" << WaitingHashes.size();
|
||||
|
||||
// Нужно убрать хеши, которые уже запрошены
|
||||
// needHashes ^ WaitingHashes.
|
||||
|
||||
for(const auto& hash : WaitingHashes)
|
||||
needHashes.erase(hash);
|
||||
|
||||
std::vector<std::tuple<ResourceFile::Hash_t, fs::path>> toDisk;
|
||||
std::vector<ResourceFile::Hash_t> toCache;
|
||||
|
||||
// Теперь раскидаем хеши по доступным источникам.
|
||||
for(const auto& hash : needHashes) {
|
||||
auto iter = HashToPath.find(hash);
|
||||
if(iter != HashToPath.end()) {
|
||||
// Ставим задачу загрузить с диска.
|
||||
toDisk.emplace_back(hash, iter->second.front());
|
||||
} else {
|
||||
// Сделаем запрос в кеш.
|
||||
toCache.push_back(hash);
|
||||
}
|
||||
}
|
||||
|
||||
// Запоминаем, что эти ресурсы уже ожидаются.
|
||||
WaitingHashes.insert_range(needHashes);
|
||||
|
||||
// Запрос к диску.
|
||||
if(!toDisk.empty())
|
||||
NeedToReadFromDisk.append_range(std::move(toDisk));
|
||||
|
||||
// Запрос в кеш (если там не найдётся, то запрос уйдёт на сервер).
|
||||
if(!toCache.empty())
|
||||
Cache->pushReads(std::move(toCache));
|
||||
}
|
||||
|
||||
// Новые ресурсы, полученные с сервера.
|
||||
void pushNewResources(
|
||||
std::vector<std::tuple<ResourceFile::Hash_t, std::u8string>> &&resources
|
||||
) {
|
||||
std::unordered_map<ResourceFile::Hash_t, std::u8string> files;
|
||||
std::vector<Resource> vec;
|
||||
files.reserve(resources.size());
|
||||
vec.reserve(resources.size());
|
||||
|
||||
for(auto& [hash, res] : resources) {
|
||||
vec.emplace_back(res);
|
||||
files.emplace(hash, std::move(res));
|
||||
}
|
||||
|
||||
_onHashLoad(files);
|
||||
Cache->pushResources(std::move(vec));
|
||||
}
|
||||
|
||||
// Для запроса отсутствующих ресурсов с сервера на клиент.
|
||||
std::vector<ResourceFile::Hash_t> pullNeededResources() {
|
||||
return std::move(NeedToRequestFromServer);
|
||||
}
|
||||
|
||||
// Получить изменённые ресурсы (для передачи другим модулям).
|
||||
ResourceUpdates pullResourceUpdates() {
|
||||
return std::move(RU);
|
||||
}
|
||||
|
||||
ResourceId reBind(EnumAssets type, ResourceId server) {
|
||||
return ServerToClientMap[static_cast<size_t>(type)].at(server);
|
||||
}
|
||||
|
||||
void tick() {
|
||||
// Проверим кеш
|
||||
std::vector<std::pair<Hash_t, std::optional<Resource>>> resources = Cache->pullReads();
|
||||
if(!resources.empty()) {
|
||||
std::unordered_map<ResourceFile::Hash_t, std::u8string> needToProceed;
|
||||
needToProceed.reserve(resources.size());
|
||||
|
||||
for(auto& [hash, res] : resources) {
|
||||
if(!res)
|
||||
NeedToRequestFromServer.push_back(hash);
|
||||
else
|
||||
needToProceed.emplace(hash, std::u8string{(const char8_t*) res->data(), res->size()});
|
||||
}
|
||||
|
||||
if(!NeedToRequestFromServer.empty())
|
||||
LOG.debug() << "CacheMiss count=" << NeedToRequestFromServer.size();
|
||||
|
||||
if(!needToProceed.empty())
|
||||
_onHashLoad(needToProceed);
|
||||
}
|
||||
|
||||
/// Читаем с диска TODO: получилась хрень с определением типа, чтобы получать headless ресурс
|
||||
if(!NeedToReadFromDisk.empty()) {
|
||||
std::unordered_map<ResourceFile::Hash_t, std::u8string> files;
|
||||
files.reserve(NeedToReadFromDisk.size());
|
||||
|
||||
auto detectTypeDomainKey = [&](const fs::path& path, EnumAssets& typeOut, std::string& domainOut, std::string& keyOut) -> bool {
|
||||
fs::path cur = path.parent_path();
|
||||
for(; !cur.empty(); cur = cur.parent_path()) {
|
||||
std::string name = cur.filename().string();
|
||||
for(size_t typeIndex = 0; typeIndex < static_cast<size_t>(EnumAssets::MAX_ENUM); ++typeIndex) {
|
||||
EnumAssets type = static_cast<EnumAssets>(typeIndex);
|
||||
if(name == ::EnumAssetsToDirectory(type)) {
|
||||
typeOut = type;
|
||||
domainOut = cur.parent_path().filename().string();
|
||||
keyOut = fs::relative(path, cur).generic_string();
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
};
|
||||
|
||||
for(const auto& [hash, path] : NeedToReadFromDisk) {
|
||||
std::u8string data;
|
||||
std::ifstream file(path, std::ios::binary);
|
||||
if(file) {
|
||||
file.seekg(0, std::ios::end);
|
||||
std::streamoff size = file.tellg();
|
||||
if(size < 0)
|
||||
size = 0;
|
||||
file.seekg(0, std::ios::beg);
|
||||
data.resize(static_cast<size_t>(size));
|
||||
if(size > 0) {
|
||||
file.read(reinterpret_cast<char*>(data.data()), size);
|
||||
if(!file)
|
||||
data.clear();
|
||||
}
|
||||
} else {
|
||||
LOG.warn() << "DiskReadFail " << path.string();
|
||||
}
|
||||
|
||||
if(!data.empty()) {
|
||||
EnumAssets type{};
|
||||
std::string domain;
|
||||
std::string key;
|
||||
if(detectTypeDomainKey(path, type, domain, key)) {
|
||||
if(type == EnumAssets::Nodestate) {
|
||||
std::string_view view(reinterpret_cast<const char*>(data.data()), data.size());
|
||||
js::object obj = js::parse(view).as_object();
|
||||
HeadlessNodeState hns;
|
||||
auto modelResolver = [&](const std::string_view model) -> AssetsModel {
|
||||
auto [mDomain, mKey] = parseDomainKey(model, domain);
|
||||
return getId(EnumAssets::Model, mDomain, mKey);
|
||||
};
|
||||
hns.parse(obj, modelResolver);
|
||||
data = hns.dump();
|
||||
} else if(type == EnumAssets::Model) {
|
||||
std::string_view view(reinterpret_cast<const char*>(data.data()), data.size());
|
||||
js::object obj = js::parse(view).as_object();
|
||||
HeadlessModel hm;
|
||||
auto modelResolver = [&](const std::string_view model) -> AssetsModel {
|
||||
auto [mDomain, mKey] = parseDomainKey(model, domain);
|
||||
return getId(EnumAssets::Model, mDomain, mKey);
|
||||
};
|
||||
auto textureIdResolver = [&](const std::string_view texture) -> std::optional<uint32_t> {
|
||||
auto [tDomain, tKey] = parseDomainKey(texture, domain);
|
||||
return getId(EnumAssets::Texture, tDomain, tKey);
|
||||
};
|
||||
auto textureResolver = [&](const std::string_view texturePipelineSrc) -> std::vector<uint8_t> {
|
||||
TexturePipelineProgram tpp;
|
||||
if(!tpp.compile(texturePipelineSrc))
|
||||
return {};
|
||||
tpp.link(textureIdResolver);
|
||||
return tpp.toBytes();
|
||||
};
|
||||
hm.parse(obj, modelResolver, textureResolver);
|
||||
data = hm.dump();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
files.emplace(hash, std::move(data));
|
||||
}
|
||||
|
||||
NeedToReadFromDisk.clear();
|
||||
_onHashLoad(files);
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
// Связка домен/ключ для локального id.
|
||||
struct DomainKey {
|
||||
// Домен ресурса.
|
||||
std::string Domain;
|
||||
// Ключ ресурса.
|
||||
std::string Key;
|
||||
// Признак валидности записи.
|
||||
bool Known = false;
|
||||
};
|
||||
|
||||
using IdTable = std::unordered_map<
|
||||
std::string,
|
||||
std::unordered_map<std::string, AssetId, detail::TSVHash, detail::TSVEq>,
|
||||
detail::TSVHash,
|
||||
detail::TSVEq>;
|
||||
|
||||
using PackTable = std::unordered_map<
|
||||
std::string,
|
||||
std::unordered_map<std::string, PackResource, detail::TSVHash, detail::TSVEq>,
|
||||
detail::TSVHash,
|
||||
detail::TSVEq>;
|
||||
|
||||
struct PerType {
|
||||
// Таблица домен/ключ -> локальный id.
|
||||
IdTable DKToLocal;
|
||||
// Таблица локальный id -> домен/ключ.
|
||||
std::vector<DomainKey> LocalToDK;
|
||||
// Union-Find родительские ссылки для ребиндов.
|
||||
std::vector<AssetId> LocalParent;
|
||||
// Таблица серверный id -> локальный id.
|
||||
std::vector<AssetId> ServerToLocal;
|
||||
// Бинды с сервером по локальному id.
|
||||
std::vector<std::optional<BindInfo>> BindInfos;
|
||||
// Ресурсы, собранные из паков.
|
||||
PackTable PackResources;
|
||||
// Следующий локальный id.
|
||||
AssetId NextLocalId = 1;
|
||||
};
|
||||
|
||||
enum class SourceStatus {
|
||||
Hit,
|
||||
Miss,
|
||||
Pending
|
||||
};
|
||||
|
||||
struct SourceResult {
|
||||
// Статус ответа источника.
|
||||
SourceStatus Status = SourceStatus::Miss;
|
||||
// Значение ресурса, если найден.
|
||||
std::optional<Resource> Value;
|
||||
// Индекс источника.
|
||||
size_t SourceIndex = 0;
|
||||
};
|
||||
|
||||
struct SourceReady {
|
||||
// Хеш готового ресурса.
|
||||
Hash_t Hash{};
|
||||
// Значение ресурса, если найден.
|
||||
std::optional<Resource> Value;
|
||||
// Индекс источника.
|
||||
size_t SourceIndex = 0;
|
||||
};
|
||||
|
||||
class IResourceSource {
|
||||
public:
|
||||
virtual ~IResourceSource() = default;
|
||||
// Попытка получить ресурс синхронно.
|
||||
virtual SourceResult tryGet(const ResourceKey& key) = 0;
|
||||
// Забрать готовые результаты асинхронных запросов.
|
||||
virtual void collectReady(std::vector<SourceReady>& out) = 0;
|
||||
// Признак асинхронности источника.
|
||||
virtual bool isAsync() const = 0;
|
||||
// Запустить асинхронные запросы по хешам.
|
||||
virtual void startPending(std::vector<Hash_t> hashes) = 0;
|
||||
};
|
||||
|
||||
struct SourceEntry {
|
||||
// Экземпляр источника.
|
||||
std::unique_ptr<IResourceSource> Source;
|
||||
// Поколение для инвалидирования кэша.
|
||||
size_t Generation = 0;
|
||||
};
|
||||
|
||||
struct SourceCacheEntry {
|
||||
// Индекс источника, где был найден хеш.
|
||||
size_t SourceIndex = 0;
|
||||
// Поколение источника на момент кэширования.
|
||||
size_t Generation = 0;
|
||||
};
|
||||
|
||||
// Конструктор с зависимостью от io_context и кэш-пути.
|
||||
AssetsManager(asio::io_context& ioc, const fs::path& cachePath,
|
||||
size_t maxCacheDirectorySize, size_t maxLifeTime);
|
||||
|
||||
// Инициализация списка источников.
|
||||
void initSources();
|
||||
// Забрать готовые результаты из источников.
|
||||
void collectReadyFromSources();
|
||||
// Запросить ресурс в источниках, с учётом кэша.
|
||||
SourceResult querySources(const ResourceKey& key);
|
||||
// Запомнить успешный источник для хеша.
|
||||
void registerSourceHit(const Hash_t& hash, size_t sourceIndex);
|
||||
// Инвалидировать кэш по конкретному источнику.
|
||||
void invalidateSourceCache(size_t sourceIndex);
|
||||
// Инвалидировать весь кэш источников.
|
||||
void invalidateAllSourceCache();
|
||||
|
||||
// Выделить новый локальный id.
|
||||
AssetId allocateLocalId(AssetType type);
|
||||
// Получить корневой локальный id с компрессией пути.
|
||||
AssetId resolveLocalIdMutable(AssetType type, AssetId localId);
|
||||
// Получить корневой локальный id без мутаций.
|
||||
AssetId resolveLocalId(AssetType type, AssetId localId) const;
|
||||
// Объединить два локальных id в один.
|
||||
void unionLocalIds(AssetType type, AssetId fromId, AssetId toId, std::optional<AssetId>* reboundFrom);
|
||||
|
||||
// Найти ресурс в паке по домену/ключу.
|
||||
std::optional<PackResource> findPackResource(AssetType type, std::string_view domain, std::string_view key) const;
|
||||
|
||||
// Логгер подсистемы.
|
||||
Logger LOG = "Client>AssetsManager";
|
||||
|
||||
// Менеджеры учёта дисковых ресурсов
|
||||
AssetsPreloader
|
||||
// В приоритете ищутся ресурсы из ресурспаков по Domain+Key.
|
||||
ResourcePacks,
|
||||
/*
|
||||
Дополнительные источники ресурсов.
|
||||
Используется для поиска ресурса по хешу от сервера (может стоит тот же мод с совпадающими ресурсами),
|
||||
или для временной подгрузки ресурса по Domain+Key пока ресурс не был получен с сервера.
|
||||
*/
|
||||
ExtraSource;
|
||||
|
||||
// Менеджер файлового кэша.
|
||||
AssetsCacheManager::Ptr Cache;
|
||||
|
||||
// Таблицы данных по каждому типу ресурсов.
|
||||
std::array<PerType, static_cast<size_t>(AssetType::MAX_ENUM)> Types;
|
||||
// Указатели на доступные ресурсы
|
||||
std::unordered_map<ResourceFile::Hash_t, std::vector<fs::path>> HashToPath;
|
||||
|
||||
// Список источников ресурсов.
|
||||
std::vector<SourceEntry> Sources;
|
||||
// Кэш попаданий по хешу.
|
||||
std::unordered_map<Hash_t, SourceCacheEntry> SourceCacheByHash;
|
||||
// Индекс источника паков.
|
||||
size_t PackSourceIndex = 0;
|
||||
// Индекс памяти (RAM) как источника.
|
||||
size_t MemorySourceIndex = 0;
|
||||
// Индекс файлового кэша.
|
||||
size_t CacheSourceIndex = 0;
|
||||
// Таблица релинковки ассетов с идентификаторов сервера на клиентские.
|
||||
std::array<
|
||||
std::vector<ResourceId>,
|
||||
static_cast<size_t>(EnumAssets::MAX_ENUM)
|
||||
> ServerToClientMap;
|
||||
|
||||
// Ресурсы в памяти по хешу.
|
||||
std::unordered_map<Hash_t, Resource> MemoryResourcesByHash;
|
||||
// Ожидающие запросы, сгруппированные по хешу.
|
||||
std::unordered_map<Hash_t, std::vector<ResourceKey>> PendingReadsByHash;
|
||||
// Готовые ответы на чтение.
|
||||
std::vector<std::pair<ResourceKey, std::optional<Resource>>> ReadyReads;
|
||||
// Таблица серверных привязок HH (id клиентские)
|
||||
std::array<
|
||||
std::vector<std::tuple<ResourceFile::Hash_t, ResourceHeader>>,
|
||||
static_cast<size_t>(EnumAssets::MAX_ENUM)
|
||||
> ServerIdToHH;
|
||||
|
||||
// Ресурсы в ожидании данных по хешу для обновления (с диска, кеша, сервера).
|
||||
std::array<
|
||||
std::vector<ResourceId>,
|
||||
static_cast<size_t>(EnumAssets::MAX_ENUM)
|
||||
> PendingUpdateFromAsync;
|
||||
|
||||
// Хеши, для которых где-то висит задача на загрузку.
|
||||
std::unordered_set<ResourceFile::Hash_t> WaitingHashes;
|
||||
|
||||
// Хеши, которые необходимо запросить с сервера.
|
||||
std::vector<ResourceFile::Hash_t> NeedToRequestFromServer;
|
||||
|
||||
// Ресурсы, которые нужно считать с диска
|
||||
std::vector<std::tuple<ResourceFile::Hash_t, fs::path>> NeedToReadFromDisk;
|
||||
|
||||
// Обновлённые ресурсы
|
||||
ResourceUpdates RU;
|
||||
|
||||
// Когда данные были получены с диска, кеша или сервера
|
||||
void _onHashLoad(const std::unordered_map<ResourceFile::Hash_t, std::u8string>& files) {
|
||||
const auto& rpLinks = ResourcePacks.getResourceLinks();
|
||||
const auto& esLinks = ExtraSource.getResourceLinks();
|
||||
|
||||
auto mapModelId = [&](ResourceId id) -> ResourceId {
|
||||
const auto& map = ServerToClientMap[static_cast<size_t>(EnumAssets::Model)];
|
||||
if(id >= map.size())
|
||||
return 0;
|
||||
|
||||
return map[id];
|
||||
};
|
||||
auto mapTextureId = [&](ResourceId id) -> ResourceId {
|
||||
const auto& map = ServerToClientMap[static_cast<size_t>(EnumAssets::Texture)];
|
||||
if(id >= map.size())
|
||||
return 0;
|
||||
|
||||
return map[id];
|
||||
};
|
||||
auto rebindHeader = [&](EnumAssets type, const ResourceHeader& header) -> ResourceHeader {
|
||||
if(header.empty())
|
||||
return {};
|
||||
|
||||
std::vector<uint8_t> bytes;
|
||||
bytes.resize(header.size());
|
||||
std::memcpy(bytes.data(), header.data(), header.size());
|
||||
std::vector<uint8_t> rebound = AssetsHeaderCodec::rebindHeader(
|
||||
type,
|
||||
bytes,
|
||||
mapModelId,
|
||||
mapTextureId,
|
||||
[](const std::string&) {}
|
||||
);
|
||||
|
||||
return ResourceHeader(reinterpret_cast<const char8_t*>(rebound.data()), rebound.size());
|
||||
};
|
||||
|
||||
for(size_t typeIndex = 0; typeIndex < static_cast<size_t>(EnumAssets::MAX_ENUM); ++typeIndex) {
|
||||
auto& pending = PendingUpdateFromAsync[typeIndex];
|
||||
if(pending.empty())
|
||||
continue;
|
||||
|
||||
std::vector<ResourceId> stillPending;
|
||||
stillPending.reserve(pending.size());
|
||||
size_t updated = 0;
|
||||
size_t missingSource = 0;
|
||||
size_t missingData = 0;
|
||||
|
||||
for(ResourceId id : pending) {
|
||||
ResourceFile::Hash_t hash{};
|
||||
ResourceHeader header;
|
||||
bool hasSource = false;
|
||||
bool localHeader = false;
|
||||
|
||||
if(id < rpLinks[typeIndex].size() && rpLinks[typeIndex][id].IsExist) {
|
||||
hash = rpLinks[typeIndex][id].Hash;
|
||||
header = rpLinks[typeIndex][id].Header;
|
||||
hasSource = true;
|
||||
localHeader = true;
|
||||
} else if(id < ServerIdToHH[typeIndex].size()) {
|
||||
std::tie(hash, header) = ServerIdToHH[typeIndex][id];
|
||||
hasSource = true;
|
||||
}
|
||||
|
||||
if(!hasSource) {
|
||||
missingSource++;
|
||||
stillPending.push_back(id);
|
||||
continue;
|
||||
}
|
||||
|
||||
auto dataIter = files.find(hash);
|
||||
if(dataIter == files.end()) {
|
||||
missingData++;
|
||||
stillPending.push_back(id);
|
||||
continue;
|
||||
}
|
||||
|
||||
std::string domain = "core";
|
||||
std::string key;
|
||||
{
|
||||
auto d = getDK((EnumAssets) typeIndex, id);
|
||||
if(d) {
|
||||
domain = d->Domain;
|
||||
key = d->Key;
|
||||
}
|
||||
}
|
||||
|
||||
std::u8string data = dataIter->second;
|
||||
EnumAssets type = static_cast<EnumAssets>(typeIndex);
|
||||
ResourceHeader finalHeader = localHeader ? header : rebindHeader(type, header);
|
||||
|
||||
if(id == 0)
|
||||
continue;
|
||||
|
||||
if(type == EnumAssets::Nodestate) {
|
||||
HeadlessNodeState ns;
|
||||
ns.load(data);
|
||||
HeadlessNodeState::Header headerParsed;
|
||||
headerParsed.load(finalHeader);
|
||||
RU.Nodestates.push_back({id, std::move(ns), std::move(headerParsed)});
|
||||
updated++;
|
||||
} else if(type == EnumAssets::Model) {
|
||||
HeadlessModel hm;
|
||||
hm.load(data);
|
||||
HeadlessModel::Header headerParsed;
|
||||
headerParsed.load(finalHeader);
|
||||
RU.Models.push_back({id, std::move(hm), std::move(headerParsed)});
|
||||
updated++;
|
||||
} else if(type == EnumAssets::Texture) {
|
||||
AssetsTextureUpdate entry;
|
||||
entry.Id = id;
|
||||
entry.Header = std::move(finalHeader);
|
||||
if(!data.empty()) {
|
||||
iResource sres(reinterpret_cast<const uint8_t*>(data.data()), data.size());
|
||||
iBinaryStream stream = sres.makeStream();
|
||||
png::image<png::rgba_pixel> img(stream.Stream);
|
||||
entry.Width = static_cast<uint16_t>(img.get_width());
|
||||
entry.Height = static_cast<uint16_t>(img.get_height());
|
||||
entry.Pixels.resize(static_cast<size_t>(entry.Width) * entry.Height);
|
||||
for(uint32_t y = 0; y < entry.Height; ++y) {
|
||||
const auto& row = img.get_pixbuf().operator[](y);
|
||||
for(uint32_t x = 0; x < entry.Width; ++x) {
|
||||
const auto& px = row[x];
|
||||
uint32_t rgba = (uint32_t(px.alpha) << 24)
|
||||
| (uint32_t(px.red) << 16)
|
||||
| (uint32_t(px.green) << 8)
|
||||
| uint32_t(px.blue);
|
||||
entry.Pixels[x + y * entry.Width] = rgba;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
RU.Textures.push_back(std::move(entry));
|
||||
updated++;
|
||||
} else if(type == EnumAssets::Particle) {
|
||||
RU.Particles.push_back({id, std::move(data)});
|
||||
updated++;
|
||||
} else if(type == EnumAssets::Animation) {
|
||||
RU.Animations.push_back({id, std::move(data)});
|
||||
updated++;
|
||||
} else if(type == EnumAssets::Sound) {
|
||||
RU.Sounds.push_back({id, std::move(data)});
|
||||
updated++;
|
||||
} else if(type == EnumAssets::Font) {
|
||||
RU.Fonts.push_back({id, std::move(data)});
|
||||
updated++;
|
||||
}
|
||||
}
|
||||
|
||||
if(updated || missingSource || missingData) {
|
||||
LOG.debug() << "HashLoad type=" << int(typeIndex)
|
||||
<< " updated=" << updated
|
||||
<< " missingSource=" << missingSource
|
||||
<< " missingData=" << missingData;
|
||||
}
|
||||
|
||||
pending = std::move(stillPending);
|
||||
}
|
||||
|
||||
for(const auto& [hash, res] : files)
|
||||
WaitingHashes.erase(hash);
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace LV::Client
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -69,14 +69,14 @@ private:
|
||||
IRenderSession *RS = nullptr;
|
||||
|
||||
// Обработчик кеша ресурсов сервера
|
||||
AssetsManager::Ptr AM;
|
||||
AssetsManager AM;
|
||||
|
||||
static constexpr uint64_t TIME_BEFORE_UNLOAD_RESOURCE = 180;
|
||||
struct {
|
||||
// Существующие привязки ресурсов
|
||||
std::unordered_set<ResourceId> ExistBinds[(int) EnumAssets::MAX_ENUM];
|
||||
// std::unordered_set<ResourceId> ExistBinds[(int) EnumAssets::MAX_ENUM];
|
||||
// Недавно использованные ресурсы, пока хранятся здесь в течении TIME_BEFORE_UNLOAD_RESOURCE секунд
|
||||
std::unordered_map<std::string, std::pair<AssetEntry, uint64_t>> NotInUse[(int) EnumAssets::MAX_ENUM];
|
||||
// std::unordered_map<std::string, std::pair<AssetEntry, uint64_t>> NotInUse[(int) EnumAssets::MAX_ENUM];
|
||||
} MyAssets;
|
||||
|
||||
struct AssetLoadingEntry {
|
||||
@@ -87,7 +87,6 @@ private:
|
||||
};
|
||||
|
||||
struct AssetLoading {
|
||||
std::vector<AssetLoadingEntry> Entries;
|
||||
std::u8string Data;
|
||||
size_t Offset = 0;
|
||||
};
|
||||
@@ -100,21 +99,41 @@ private:
|
||||
std::vector<uint8_t> Header;
|
||||
};
|
||||
|
||||
std::array<std::vector<std::pair<std::string, std::string>>, (int) EnumAssets::MAX_ENUM> ServerIdToDK;
|
||||
std::array<ResourceId, (int) EnumAssets::MAX_ENUM> NextServerId = {};
|
||||
struct UpdateAssetsBindsDK {
|
||||
std::vector<std::string> Domains;
|
||||
std::array<
|
||||
std::vector<std::vector<std::string>>,
|
||||
static_cast<size_t>(EnumAssets::MAX_ENUM)
|
||||
> Keys;
|
||||
};
|
||||
|
||||
struct UpdateAssetsBindsHH {
|
||||
std::array<
|
||||
std::vector<std::tuple<ResourceId, ResourceFile::Hash_t, ResourceHeader>>,
|
||||
static_cast<size_t>(EnumAssets::MAX_ENUM)
|
||||
> HashAndHeaders;
|
||||
};
|
||||
|
||||
struct TickData {
|
||||
std::vector<std::pair<DefVoxelId, void*>> Profile_Voxel_AddOrChange;
|
||||
// Полученные изменения привязок Domain+Key
|
||||
std::vector<UpdateAssetsBindsDK> BindsDK;
|
||||
// Полученные изменения привязок Hash+Header
|
||||
std::vector<UpdateAssetsBindsHH> BindsHH;
|
||||
// Потерянные привязываются к hash_t(0)
|
||||
// Полученные с сервера ресурсы
|
||||
std::vector<std::tuple<ResourceFile::Hash_t, std::u8string>> ReceivedAssets;
|
||||
|
||||
std::vector<std::pair<DefVoxelId, DefVoxel>> Profile_Voxel_AddOrChange;
|
||||
std::vector<DefVoxelId> Profile_Voxel_Lost;
|
||||
std::vector<std::pair<DefNodeId, DefNode_t>> Profile_Node_AddOrChange;
|
||||
std::vector<std::pair<DefNodeId, DefNode>> Profile_Node_AddOrChange;
|
||||
std::vector<DefNodeId> Profile_Node_Lost;
|
||||
std::vector<std::pair<DefWorldId, void*>> Profile_World_AddOrChange;
|
||||
std::vector<std::pair<DefWorldId, DefWorld>> Profile_World_AddOrChange;
|
||||
std::vector<DefWorldId> Profile_World_Lost;
|
||||
std::vector<std::pair<DefPortalId, void*>> Profile_Portal_AddOrChange;
|
||||
std::vector<std::pair<DefPortalId, DefPortal>> Profile_Portal_AddOrChange;
|
||||
std::vector<DefPortalId> Profile_Portal_Lost;
|
||||
std::vector<std::pair<DefEntityId, DefEntityInfo>> Profile_Entity_AddOrChange;
|
||||
std::vector<std::pair<DefEntityId, DefEntity>> Profile_Entity_AddOrChange;
|
||||
std::vector<DefEntityId> Profile_Entity_Lost;
|
||||
std::vector<std::pair<DefItemId, void*>> Profile_Item_AddOrChange;
|
||||
std::vector<std::pair<DefItemId, DefItem>> Profile_Item_AddOrChange;
|
||||
std::vector<DefItemId> Profile_Item_Lost;
|
||||
|
||||
std::vector<std::pair<WorldId_t, void*>> Worlds_AddOrChange;
|
||||
@@ -132,13 +151,6 @@ private:
|
||||
uint32_t Nodes = 0;
|
||||
};
|
||||
|
||||
struct AssetsBindsChange {
|
||||
// Новые привязки ресурсов
|
||||
std::vector<AssetBindEntry> Binds;
|
||||
// Потерянные из видимости ресурсы
|
||||
std::vector<ResourceId> Lost[(int) EnumAssets::MAX_ENUM];
|
||||
};
|
||||
|
||||
struct {
|
||||
// Сюда обращается ветка, обрабатывающая сокет; run()
|
||||
// Получение ресурсов с сервера
|
||||
@@ -146,22 +158,7 @@ private:
|
||||
// Накопление данных за такт сервера
|
||||
TickData ThisTickEntry;
|
||||
|
||||
// Сюда обращается ветка обновления IServerSession, накапливая данные до SyncTick
|
||||
// Ресурсы, ожидающие либо кеш, либо сервер; используются для сопоставления hash->domain/key
|
||||
std::unordered_map<std::string, std::vector<std::pair<std::string, Hash_t>>> ResourceWait[(int) EnumAssets::MAX_ENUM];
|
||||
// Полученные изменения связок в ожидании стадии синхронизации такта
|
||||
std::vector<AssetsBindsChange> Binds;
|
||||
// Подгруженные или принятые меж тактами ресурсы
|
||||
std::vector<AssetEntry> LoadedResources;
|
||||
// Список ресурсов на которые уже был отправлен запрос на загрузку ресурса
|
||||
std::vector<Hash_t> AlreadyLoading;
|
||||
|
||||
|
||||
// Обменный пункт
|
||||
// Полученные ресурсы с сервера
|
||||
TOS::SpinlockObject<std::vector<AssetEntry>> LoadedAssets;
|
||||
// Изменения в наблюдаемых ресурсах
|
||||
TOS::SpinlockObject<std::vector<AssetsBindsChange>> AssetsBinds;
|
||||
// Пакеты обновлений игрового мира
|
||||
TOS::SpinlockObject<std::vector<TickData>> TickSequence;
|
||||
} AsyncContext;
|
||||
@@ -201,6 +198,7 @@ private:
|
||||
coro<> rP_AssetsBindHH(Net::AsyncSocket &sock);
|
||||
coro<> rP_AssetsInitSend(Net::AsyncSocket &sock);
|
||||
coro<> rP_AssetsNextSend(Net::AsyncSocket &sock);
|
||||
coro<> rP_DefinitionsFull(Net::AsyncSocket &sock);
|
||||
coro<> rP_DefinitionsUpdate(Net::AsyncSocket &sock);
|
||||
coro<> rP_ChunkVoxels(Net::AsyncSocket &sock);
|
||||
coro<> rP_ChunkNodes(Net::AsyncSocket &sock);
|
||||
|
||||
@@ -16,7 +16,7 @@
|
||||
|
||||
class SharedStagingBuffer {
|
||||
public:
|
||||
static constexpr VkDeviceSize kDefaultSize = 64ull * 1024ull * 1024ull;
|
||||
static constexpr VkDeviceSize kDefaultSize = 18ull * 1024ull * 1024ull;
|
||||
|
||||
SharedStagingBuffer(VkDevice device,
|
||||
VkPhysicalDevice physicalDevice,
|
||||
|
||||
@@ -29,6 +29,7 @@
|
||||
#include <png++/png.hpp>
|
||||
#include "VulkanRenderSession.hpp"
|
||||
#include <Server/GameServer.hpp>
|
||||
#include <malloc.h>
|
||||
|
||||
extern void LoadSymbolsVulkan(TOS::DynamicLibrary &library);
|
||||
|
||||
@@ -222,6 +223,8 @@ void Vulkan::run()
|
||||
} catch(const std::exception &exc) {
|
||||
LOG.error() << "Game.Session->shutdown: " << exc.what();
|
||||
}
|
||||
|
||||
Game.Session = nullptr;
|
||||
}
|
||||
|
||||
if(!NeedShutdown && glfwWindowShouldClose(Graphics.Window)) {
|
||||
@@ -240,11 +243,12 @@ void Vulkan::run()
|
||||
try {
|
||||
if(Game.Session)
|
||||
Game.Session->shutdown(EnumDisconnect::ByInterface);
|
||||
Game.Session = nullptr;
|
||||
} catch(const std::exception &exc) {
|
||||
LOG.error() << "Game.Session->shutdown: " << exc.what();
|
||||
}
|
||||
|
||||
Game.Session = nullptr;
|
||||
|
||||
try {
|
||||
if(Game.Server)
|
||||
Game.Server->GS.shutdown("Завершение работы из-за остановки клиента");
|
||||
@@ -2254,6 +2258,10 @@ void Vulkan::gui_MainMenu() {
|
||||
}
|
||||
}
|
||||
|
||||
if(ImGui::Button("Memory trim")) {
|
||||
malloc_trim(0);
|
||||
}
|
||||
|
||||
if(ConnectionProgress.InProgress) {
|
||||
if(ImGui::Button("Отмена"))
|
||||
ConnectionProgress.Cancel = true;
|
||||
@@ -2305,6 +2313,10 @@ void Vulkan::gui_ConnectedToServer() {
|
||||
Game.ImGuiInterfaces.pop_back();
|
||||
}
|
||||
|
||||
if(ImGui::Button("Memory trim")) {
|
||||
malloc_trim(0);
|
||||
}
|
||||
|
||||
ImGui::End();
|
||||
|
||||
if(Game.Выйти)
|
||||
|
||||
@@ -48,7 +48,7 @@ struct DeviceId {
|
||||
struct Settings {
|
||||
DeviceId DeviceMain;
|
||||
uint32_t QueueGraphics = -1, QueueSurface = -1;
|
||||
bool Debug = true;
|
||||
bool Debug = false;
|
||||
|
||||
bool isValid()
|
||||
{
|
||||
|
||||
@@ -121,15 +121,15 @@ void ChunkMeshGenerator::run(uint8_t id) {
|
||||
uint8_t fullNodes[18][18][18];
|
||||
|
||||
// Профиль, который используется если на стороне клиента отсутствует нужных профиль
|
||||
DefNode_t defaultProfileNode;
|
||||
DefNode defaultProfileNode;
|
||||
// Кеш запросов профилей нод
|
||||
std::unordered_map<DefNodeId, const DefNode_t*> profilesNodeCache;
|
||||
auto getNodeProfile = [&](DefNodeId id) -> const DefNode_t* {
|
||||
std::unordered_map<DefNodeId, const DefNode*> profilesNodeCache;
|
||||
auto getNodeProfile = [&](DefNodeId id) -> const DefNode* {
|
||||
auto iterCache = profilesNodeCache.find(id);
|
||||
if(iterCache == profilesNodeCache.end()) {
|
||||
// Промах кеша
|
||||
auto iterSS = SS->Profiles.DefNode.find(id);
|
||||
if(iterSS != SS->Profiles.DefNode.end()) {
|
||||
auto iterSS = SS->Profiles.DefNodes.find(id);
|
||||
if(iterSS != SS->Profiles.DefNodes.end()) {
|
||||
return (profilesNodeCache[id] = &iterSS->second);
|
||||
} else {
|
||||
// Профиль отсутствует на клиенте
|
||||
@@ -189,46 +189,50 @@ void ChunkMeshGenerator::run(uint8_t id) {
|
||||
|
||||
std::unordered_map<DefNodeId, bool> nodeFullCuboidCache;
|
||||
auto nodeIsFull = [&](Node node) -> bool {
|
||||
if(node.NodeId == 0)
|
||||
return false;
|
||||
|
||||
auto iterCache = nodeFullCuboidCache.find(node.Data);
|
||||
if(iterCache == nodeFullCuboidCache.end()) {
|
||||
const DefNode_t* profile = getNodeProfile(node.NodeId);
|
||||
if(profile->TexId != 0) {
|
||||
return (nodeFullCuboidCache[node.Data] = true);
|
||||
}
|
||||
|
||||
if(NSP && profile->NodestateId != 0 && NSP->hasNodestate(profile->NodestateId)) {
|
||||
std::unordered_map<std::string, int32_t> states;
|
||||
int32_t meta = node.Meta;
|
||||
states.emplace("meta", meta);
|
||||
const auto routes = NSP->getModelsForNode(profile->NodestateId, metaStatesInfo, states);
|
||||
bool isFull = !routes.empty();
|
||||
if(isFull) {
|
||||
for(const auto& variants : routes) {
|
||||
for(const auto& [weight, faces] : variants) {
|
||||
(void)weight;
|
||||
auto hasFace = [&](EnumFace face) -> bool {
|
||||
auto iterFace = faces.find(face);
|
||||
return iterFace != faces.end() && !iterFace->second.empty();
|
||||
};
|
||||
if(!hasFace(EnumFace::Up)
|
||||
|| !hasFace(EnumFace::Down)
|
||||
|| !hasFace(EnumFace::East)
|
||||
|| !hasFace(EnumFace::West)
|
||||
|| !hasFace(EnumFace::South)
|
||||
|| !hasFace(EnumFace::North))
|
||||
{
|
||||
isFull = false;
|
||||
break;
|
||||
const DefNode* profile = getNodeProfile(node.NodeId);
|
||||
if(NSP) {
|
||||
if(const AssetsNodestate* ptr = std::get_if<AssetsNodestate>(&profile->RenderStates)) {
|
||||
if(NSP->hasNodestate(*ptr)) {
|
||||
std::unordered_map<std::string, int32_t> states;
|
||||
int32_t meta = node.Meta;
|
||||
states.emplace("meta", meta);
|
||||
const auto routes = NSP->getModelsForNode(*ptr, metaStatesInfo, states);
|
||||
bool isFull = !routes.empty();
|
||||
if(isFull) {
|
||||
for(const auto& variants : routes) {
|
||||
for(const auto& [weight, faces] : variants) {
|
||||
(void)weight;
|
||||
auto hasFace = [&](EnumFace face) -> bool {
|
||||
auto iterFace = faces.find(face);
|
||||
return iterFace != faces.end() && !iterFace->second.empty();
|
||||
};
|
||||
if(!hasFace(EnumFace::Up)
|
||||
|| !hasFace(EnumFace::Down)
|
||||
|| !hasFace(EnumFace::East)
|
||||
|| !hasFace(EnumFace::West)
|
||||
|| !hasFace(EnumFace::South)
|
||||
|| !hasFace(EnumFace::North))
|
||||
{
|
||||
isFull = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if(!isFull)
|
||||
break;
|
||||
}
|
||||
}
|
||||
if(!isFull)
|
||||
break;
|
||||
|
||||
return (nodeFullCuboidCache[node.Data] = isFull);
|
||||
}
|
||||
}
|
||||
return (nodeFullCuboidCache[node.Data] = isFull);
|
||||
}
|
||||
|
||||
return (nodeFullCuboidCache[node.Data] = false);
|
||||
return (nodeFullCuboidCache[node.Data] = true);
|
||||
} else {
|
||||
return iterCache->second;
|
||||
}
|
||||
@@ -313,7 +317,7 @@ void ChunkMeshGenerator::run(uint8_t id) {
|
||||
} else {
|
||||
for(int y = 0; y < 16; y++)
|
||||
for(int x = 0; x < 16; x++)
|
||||
fullNodes[x+0][y+1][0] = 0;
|
||||
fullNodes[x+1][y+1][0] = 0;
|
||||
}
|
||||
} else
|
||||
goto end;
|
||||
@@ -420,6 +424,10 @@ void ChunkMeshGenerator::run(uint8_t id) {
|
||||
for(int z = 0; z < 16; z++)
|
||||
for(int y = 0; y < 16; y++)
|
||||
for(int x = 0; x < 16; x++) {
|
||||
const Node& nodeData = (*chunk)[x+y*16+z*16*16];
|
||||
if(nodeData.NodeId == 0)
|
||||
continue;
|
||||
|
||||
const size_t vertexStart = result.NodeVertexs.size();
|
||||
int fullCovered = 0;
|
||||
|
||||
@@ -433,66 +441,46 @@ void ChunkMeshGenerator::run(uint8_t id) {
|
||||
if(fullCovered == 0b111111)
|
||||
continue;
|
||||
|
||||
const Node& nodeData = (*chunk)[x+y*16+z*16*16];
|
||||
const DefNode_t* node = getNodeProfile(nodeData.NodeId);
|
||||
|
||||
if(debugMeshEnabled) {
|
||||
const bool hasRenderable = (node->NodestateId != 0) || (node->TexId != 0);
|
||||
if(hasRenderable && fullCovered != 0b111111) {
|
||||
expectedColumnX[x] = 1;
|
||||
expectedColumnY[y] = 1;
|
||||
expectedColumnZ[z] = 1;
|
||||
}
|
||||
}
|
||||
const DefNode* node = getNodeProfile(nodeData.NodeId);
|
||||
|
||||
bool usedModel = false;
|
||||
|
||||
if(NSP && (node->NodestateId != 0 || NSP->hasNodestate(node->NodestateId))) {
|
||||
auto iterCache = modelCache.find(nodeData.Data);
|
||||
if(iterCache == modelCache.end()) {
|
||||
std::unordered_map<std::string, int32_t> states;
|
||||
states.emplace("meta", nodeData.Meta);
|
||||
if(NSP) {
|
||||
if(const AssetsNodestate* ptr = std::get_if<AssetsNodestate>(&node->RenderStates)) {
|
||||
if(NSP->hasNodestate(*ptr)) {
|
||||
auto iterCache = modelCache.find(nodeData.Data);
|
||||
if(iterCache == modelCache.end()) {
|
||||
std::unordered_map<std::string, int32_t> states;
|
||||
states.emplace("meta", nodeData.Meta);
|
||||
|
||||
ModelCacheEntry entry;
|
||||
entry.Routes = NSP->getModelsForNode(node->NodestateId, metaStatesInfo, states);
|
||||
iterCache = modelCache.emplace(nodeData.Data, std::move(entry)).first;
|
||||
}
|
||||
ModelCacheEntry entry;
|
||||
entry.Routes = NSP->getModelsForNode(*ptr, metaStatesInfo, states);
|
||||
iterCache = modelCache.emplace(nodeData.Data, std::move(entry)).first;
|
||||
}
|
||||
|
||||
if(!iterCache->second.Routes.empty()) {
|
||||
uint32_t seed = uint32_t(nodeData.Data) * 2654435761u;
|
||||
seed ^= uint32_t(x) * 73856093u;
|
||||
seed ^= uint32_t(y) * 19349663u;
|
||||
seed ^= uint32_t(z) * 83492791u;
|
||||
if(!iterCache->second.Routes.empty()) {
|
||||
uint32_t seed = uint32_t(nodeData.Data) * 2654435761u;
|
||||
seed ^= uint32_t(x) * 73856093u;
|
||||
seed ^= uint32_t(y) * 19349663u;
|
||||
seed ^= uint32_t(z) * 83492791u;
|
||||
|
||||
for(size_t routeIndex = 0; routeIndex < iterCache->second.Routes.size(); routeIndex++) {
|
||||
const auto& variants = iterCache->second.Routes[routeIndex];
|
||||
const auto* faces = pickVariant(variants, seed + uint32_t(routeIndex) * 374761393u);
|
||||
if(faces)
|
||||
appendModel(*faces, fullCovered, x, y, z);
|
||||
for(size_t routeIndex = 0; routeIndex < iterCache->second.Routes.size(); routeIndex++) {
|
||||
const auto& variants = iterCache->second.Routes[routeIndex];
|
||||
const auto* faces = pickVariant(variants, seed + uint32_t(routeIndex) * 374761393u);
|
||||
if(faces)
|
||||
appendModel(*faces, fullCovered, x, y, z);
|
||||
}
|
||||
|
||||
usedModel = true;
|
||||
}
|
||||
}
|
||||
|
||||
usedModel = true;
|
||||
}
|
||||
}
|
||||
|
||||
if(usedModel)
|
||||
goto node_done;
|
||||
|
||||
if(NSP && node->TexId != 0) {
|
||||
auto iterTex = baseTextureCache.find(node->TexId);
|
||||
if(iterTex != baseTextureCache.end()) {
|
||||
v.Tex = iterTex->second;
|
||||
} else {
|
||||
uint32_t resolvedTex = NSP->getTextureId(node->TexId);
|
||||
v.Tex = resolvedTex;
|
||||
baseTextureCache.emplace(node->TexId, resolvedTex);
|
||||
}
|
||||
} else {
|
||||
v.Tex = node->TexId;
|
||||
}
|
||||
|
||||
if(v.Tex == 0)
|
||||
goto node_done;
|
||||
v.Tex = 0;
|
||||
|
||||
// Рендерим обычный кубоид
|
||||
// XZ+Y
|
||||
@@ -700,58 +688,6 @@ void ChunkMeshGenerator::run(uint8_t id) {
|
||||
}
|
||||
|
||||
node_done:
|
||||
if(debugMeshEnabled) {
|
||||
const bool emitted = result.NodeVertexs.size() > vertexStart;
|
||||
if(emitted) {
|
||||
generatedColumnX[x] = 1;
|
||||
generatedColumnY[y] = 1;
|
||||
generatedColumnZ[z] = 1;
|
||||
} else {
|
||||
const bool hasRenderable = (node->NodestateId != 0) || (node->TexId != 0);
|
||||
if(hasRenderable && fullCovered != 0b111111) {
|
||||
uint32_t warnIndex = debugMeshWarnCount.fetch_add(1);
|
||||
if(warnIndex < 16) {
|
||||
LOG.warn() << "Missing node geometry at chunk " << int(pos[0]) << ','
|
||||
<< int(pos[1]) << ',' << int(pos[2])
|
||||
<< " local " << x << ',' << y << ',' << z
|
||||
<< " nodeId " << nodeData.NodeId
|
||||
<< " meta " << int(nodeData.Meta)
|
||||
<< " covered " << fullCovered
|
||||
<< " tex " << node->TexId
|
||||
<< " nodestate " << node->NodestateId;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if(debugMeshEnabled) {
|
||||
auto collectMissing = [](const std::array<uint8_t, 16>& expected,
|
||||
const std::array<uint8_t, 16>& generated) {
|
||||
std::string res;
|
||||
for(int i = 0; i < 16; i++) {
|
||||
if(expected[i] && !generated[i]) {
|
||||
if(!res.empty())
|
||||
res += ',';
|
||||
res += std::to_string(i);
|
||||
}
|
||||
}
|
||||
return res;
|
||||
};
|
||||
|
||||
std::string missingX = collectMissing(expectedColumnX, generatedColumnX);
|
||||
std::string missingY = collectMissing(expectedColumnY, generatedColumnY);
|
||||
std::string missingZ = collectMissing(expectedColumnZ, generatedColumnZ);
|
||||
if(!missingX.empty() || !missingY.empty() || !missingZ.empty()) {
|
||||
uint32_t warnIndex = debugMeshWarnCount.fetch_add(1);
|
||||
if(warnIndex < 16) {
|
||||
LOG.warn() << "Missing mesh columns at chunk " << int(pos[0]) << ','
|
||||
<< int(pos[1]) << ',' << int(pos[2])
|
||||
<< " missingX[" << missingX << "]"
|
||||
<< " missingY[" << missingY << "]"
|
||||
<< " missingZ[" << missingZ << "]";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Вычислить индексы и сократить вершины
|
||||
@@ -939,6 +875,7 @@ void ChunkPreparator::tickSync(const TickSyncData& data) {
|
||||
// Получаем готовые чанки
|
||||
{
|
||||
std::vector<ChunkMeshGenerator::ChunkObj_t> chunks = std::move(*CMG.Output.lock());
|
||||
uint8_t frameRetirement = (FrameRoulette+FRAME_COUNT_RESOURCE_LATENCY) % FRAME_COUNT_RESOURCE_LATENCY;
|
||||
for(auto& chunk : chunks) {
|
||||
auto iterWorld = Requests.find(chunk.WId);
|
||||
if(iterWorld == Requests.end())
|
||||
@@ -953,6 +890,14 @@ void ChunkPreparator::tickSync(const TickSyncData& data) {
|
||||
|
||||
// Чанк ожидаем
|
||||
auto& rChunk = ChunksMesh[chunk.WId][chunk.Pos >> 2][Pos::bvec4u(chunk.Pos & 0x3).pack()];
|
||||
if(rChunk.VoxelPointer)
|
||||
VPV_ToFree[frameRetirement].emplace_back(std::move(rChunk.VoxelPointer));
|
||||
if(rChunk.NodePointer) {
|
||||
VPN_ToFree[frameRetirement].emplace_back(std::move(rChunk.NodePointer), std::move(rChunk.NodeIndexes));
|
||||
}
|
||||
rChunk.VoxelPointer = {};
|
||||
rChunk.NodePointer = {};
|
||||
rChunk.NodeIndexes = {};
|
||||
rChunk.Voxels = std::move(chunk.VoxelDefines);
|
||||
if(!chunk.VoxelVertexs.empty())
|
||||
rChunk.VoxelPointer = VertexPool_Voxels.pushVertexs(std::move(chunk.VoxelVertexs));
|
||||
@@ -1635,6 +1580,8 @@ VulkanRenderSession::VulkanRenderSession(Vulkan *vkInst, IServerSession *serverS
|
||||
}
|
||||
|
||||
VulkanRenderSession::~VulkanRenderSession() {
|
||||
|
||||
|
||||
if(VoxelOpaquePipeline)
|
||||
vkDestroyPipeline(VkInst->Graphics.Device, VoxelOpaquePipeline, nullptr);
|
||||
if(VoxelTransparentPipeline)
|
||||
@@ -1662,7 +1609,7 @@ void VulkanRenderSession::pushStageTickSync() {
|
||||
CP.pushStageTickSync();
|
||||
}
|
||||
|
||||
void VulkanRenderSession::tickSync(const TickSyncData& data) {
|
||||
void VulkanRenderSession::tickSync(TickSyncData& data) {
|
||||
// Изменение ассетов
|
||||
// Профили
|
||||
// Чанки
|
||||
@@ -1680,75 +1627,16 @@ void VulkanRenderSession::tickSync(const TickSyncData& data) {
|
||||
if(auto iter = data.Profiles_Lost.find(EnumDefContent::Voxel); iter != data.Profiles_Lost.end())
|
||||
mcpData.ChangedVoxels.insert(mcpData.ChangedVoxels.end(), iter->second.begin(), iter->second.end());
|
||||
|
||||
std::vector<std::tuple<AssetsModel, Resource, const std::vector<uint8_t>*>> modelResources;
|
||||
std::vector<AssetsModel> modelLost;
|
||||
if(auto iter = data.Assets_ChangeOrAdd.find(EnumAssets::Model); iter != data.Assets_ChangeOrAdd.end()) {
|
||||
const auto& list = ServerSession->Assets[EnumAssets::Model];
|
||||
for(ResourceId id : iter->second) {
|
||||
auto entryIter = list.find(id);
|
||||
if(entryIter == list.end())
|
||||
continue;
|
||||
|
||||
modelResources.emplace_back(id, entryIter->second.Res, &entryIter->second.Dependencies);
|
||||
}
|
||||
}
|
||||
if(auto iter = data.Assets_Lost.find(EnumAssets::Model); iter != data.Assets_Lost.end())
|
||||
modelLost.insert(modelLost.end(), iter->second.begin(), iter->second.end());
|
||||
|
||||
std::vector<AssetsModel> changedModels;
|
||||
if(!modelResources.empty() || !modelLost.empty()) {
|
||||
const auto& modelAssets = ServerSession->Assets[EnumAssets::Model];
|
||||
changedModels = MP.onModelChanges(std::move(modelResources), std::move(modelLost), &modelAssets);
|
||||
}
|
||||
if(!data.AssetsModels.empty())
|
||||
changedModels = MP.onModelChanges(std::move(data.AssetsModels));
|
||||
|
||||
if(TP) {
|
||||
std::vector<TextureProvider::TextureUpdate> textureResources;
|
||||
std::vector<AssetsTexture> textureLost;
|
||||
|
||||
if(auto iter = data.Assets_ChangeOrAdd.find(EnumAssets::Texture); iter != data.Assets_ChangeOrAdd.end()) {
|
||||
const auto& list = ServerSession->Assets[EnumAssets::Texture];
|
||||
for(ResourceId id : iter->second) {
|
||||
auto entryIter = list.find(id);
|
||||
if(entryIter == list.end())
|
||||
continue;
|
||||
|
||||
textureResources.push_back({
|
||||
.Id = id,
|
||||
.Res = entryIter->second.Res,
|
||||
.Domain = entryIter->second.Domain,
|
||||
.Key = entryIter->second.Key
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
if(auto iter = data.Assets_Lost.find(EnumAssets::Texture); iter != data.Assets_Lost.end())
|
||||
textureLost.insert(textureLost.end(), iter->second.begin(), iter->second.end());
|
||||
|
||||
if(!textureResources.empty() || !textureLost.empty())
|
||||
TP->onTexturesChanges(std::move(textureResources), std::move(textureLost));
|
||||
}
|
||||
if(TP && !data.AssetsTextures.empty())
|
||||
TP->onTexturesChanges(std::move(data.AssetsTextures));
|
||||
|
||||
std::vector<AssetsNodestate> changedNodestates;
|
||||
if(NSP) {
|
||||
std::vector<std::tuple<AssetsNodestate, Resource, const std::vector<uint8_t>*>> nodestateResources;
|
||||
std::vector<AssetsNodestate> nodestateLost;
|
||||
|
||||
if(auto iter = data.Assets_ChangeOrAdd.find(EnumAssets::Nodestate); iter != data.Assets_ChangeOrAdd.end()) {
|
||||
const auto& list = ServerSession->Assets[EnumAssets::Nodestate];
|
||||
for(ResourceId id : iter->second) {
|
||||
auto entryIter = list.find(id);
|
||||
if(entryIter == list.end())
|
||||
continue;
|
||||
|
||||
nodestateResources.emplace_back(id, entryIter->second.Res, &entryIter->second.Dependencies);
|
||||
}
|
||||
}
|
||||
|
||||
if(auto iter = data.Assets_Lost.find(EnumAssets::Nodestate); iter != data.Assets_Lost.end())
|
||||
nodestateLost.insert(nodestateLost.end(), iter->second.begin(), iter->second.end());
|
||||
|
||||
if(!nodestateResources.empty() || !nodestateLost.empty() || !changedModels.empty())
|
||||
changedNodestates = NSP->onNodestateChanges(std::move(nodestateResources), std::move(nodestateLost), changedModels);
|
||||
if(NSP && (!data.AssetsNodestates.empty() || !changedModels.empty())) {
|
||||
changedNodestates = NSP->onNodestateChanges(std::move(data.AssetsNodestates), std::move(changedModels));
|
||||
}
|
||||
|
||||
if(!changedNodestates.empty()) {
|
||||
@@ -1757,9 +1645,10 @@ void VulkanRenderSession::tickSync(const TickSyncData& data) {
|
||||
for(AssetsNodestate id : changedNodestates)
|
||||
changed.insert(id);
|
||||
|
||||
for(const auto& [nodeId, def] : ServerSession->Profiles.DefNode) {
|
||||
if(changed.contains(def.NodestateId))
|
||||
mcpData.ChangedNodes.push_back(nodeId);
|
||||
for(const auto& [nodeId, def] : ServerSession->Profiles.DefNodes) {
|
||||
if(const AssetsNodestate* ptr = std::get_if<AssetsNodestate>(&def.RenderStates))
|
||||
if(changed.contains(*ptr))
|
||||
mcpData.ChangedNodes.push_back(nodeId);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2048,29 +1937,7 @@ void VulkanRenderSession::ensureEntityTexture() {
|
||||
if(EntityTextureReady || !TP || !NSP)
|
||||
return;
|
||||
|
||||
auto iter = ServerSession->Assets.find(EnumAssets::Texture);
|
||||
if(iter == ServerSession->Assets.end() || iter->second.empty())
|
||||
return;
|
||||
|
||||
const AssetEntry* picked = nullptr;
|
||||
for(const auto& [id, entry] : iter->second) {
|
||||
if(entry.Key == "default.png") {
|
||||
picked = &entry;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if(!picked) {
|
||||
for(const auto& [id, entry] : iter->second) {
|
||||
if(entry.Key == "grass.png") {
|
||||
picked = &entry;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
if(!picked)
|
||||
picked = &iter->second.begin()->second;
|
||||
|
||||
updateTestQuadTexture(NSP->getTextureId(picked->Id));
|
||||
return;
|
||||
}
|
||||
|
||||
void VulkanRenderSession::ensureAtlasLayerPreview() {
|
||||
|
||||
@@ -89,9 +89,7 @@ public:
|
||||
}
|
||||
|
||||
// Применяет изменения, возвращая все затронутые модели
|
||||
std::vector<AssetsModel> onModelChanges(std::vector<std::tuple<AssetsModel, Resource, const std::vector<uint8_t>*>> newOrChanged,
|
||||
std::vector<AssetsModel> lost,
|
||||
const std::unordered_map<ResourceId, AssetEntry>* modelAssets) {
|
||||
std::vector<AssetsModel> onModelChanges(std::vector<AssetsModelUpdate> entries) {
|
||||
std::vector<AssetsModel> result;
|
||||
|
||||
std::move_only_function<void(ResourceId)> makeUnready;
|
||||
@@ -124,99 +122,50 @@ public:
|
||||
iterModel->second.Ready = false;
|
||||
};
|
||||
|
||||
for(ResourceId lostId : lost) {
|
||||
makeUnready(lostId);
|
||||
}
|
||||
|
||||
for(ResourceId lostId : lost) {
|
||||
auto iterModel = Models.find(lostId);
|
||||
if(iterModel == Models.end())
|
||||
continue;
|
||||
|
||||
Models.erase(iterModel);
|
||||
}
|
||||
|
||||
for(const auto& [key, resource, deps] : newOrChanged) {
|
||||
for(const AssetsModelUpdate& entry : entries) {
|
||||
const AssetsModel key = entry.Id;
|
||||
result.push_back(key);
|
||||
|
||||
makeUnready(key);
|
||||
ModelObject model;
|
||||
std::string type = "unknown";
|
||||
std::optional<AssetsManager::ParsedHeader> header;
|
||||
if(deps && !deps->empty())
|
||||
header = AssetsManager::parseHeader(EnumAssets::Model, *deps);
|
||||
const std::vector<uint32_t>* textureDeps = header ? &header->TextureDeps : nullptr;
|
||||
auto remapTextureId = [&](uint32_t placeholder) -> uint32_t {
|
||||
if(!textureDeps || placeholder >= textureDeps->size())
|
||||
return 0;
|
||||
return (*textureDeps)[placeholder];
|
||||
};
|
||||
auto remapPipeline = [&](TexturePipeline pipe) {
|
||||
if(textureDeps) {
|
||||
for(auto& texId : pipe.BinTextures)
|
||||
texId = remapTextureId(texId);
|
||||
if(!pipe.Pipeline.empty()) {
|
||||
std::vector<uint8_t> code;
|
||||
code.resize(pipe.Pipeline.size());
|
||||
std::memcpy(code.data(), pipe.Pipeline.data(), code.size());
|
||||
TexturePipelineProgram::remapTexIds(code, *textureDeps, nullptr);
|
||||
pipe.Pipeline.resize(code.size());
|
||||
std::memcpy(pipe.Pipeline.data(), code.data(), code.size());
|
||||
}
|
||||
}
|
||||
return pipe;
|
||||
};
|
||||
const HeadlessModel& hm = entry.Model;
|
||||
const HeadlessModel::Header& header = entry.Header;
|
||||
|
||||
size_t dataSize = 0;
|
||||
std::array<uint8_t, 4> prefix = {};
|
||||
try {
|
||||
std::u8string_view data((const char8_t*) resource.data(), resource.size());
|
||||
dataSize = data.size();
|
||||
if(!data.empty()) {
|
||||
const size_t prefixLen = std::min<size_t>(prefix.size(), data.size());
|
||||
for(size_t i = 0; i < prefixLen; ++i)
|
||||
prefix[i] = static_cast<uint8_t>(data[i]);
|
||||
}
|
||||
if(data.starts_with((const char8_t*) "bm")) {
|
||||
type = "InternalBinary";
|
||||
// Компилированная модель внутреннего формата
|
||||
HeadlessModel hm;
|
||||
hm.load(data);
|
||||
model.TextureMap.clear();
|
||||
model.TextureMap.reserve(hm.Textures.size());
|
||||
for(const auto& [tkey, id] : hm.Textures) {
|
||||
TexturePipeline pipe;
|
||||
if(header && id < header->TexturePipelines.size()) {
|
||||
pipe.Pipeline = header->TexturePipelines[id];
|
||||
} else {
|
||||
LOG.warn() << "Model texture pipeline id out of range: model=" << key
|
||||
<< " local=" << id
|
||||
<< " pipelines=" << (header ? header->TexturePipelines.size() : 0);
|
||||
pipe.BinTextures.push_back(id);
|
||||
pipe = remapPipeline(std::move(pipe));
|
||||
}
|
||||
model.TextureMap.emplace(tkey, std::move(pipe));
|
||||
model.TextureMap.clear();
|
||||
model.TextureMap.reserve(hm.Textures.size());
|
||||
for(const auto& [tkey, id] : hm.Textures) {
|
||||
TexturePipeline pipe;
|
||||
if(id < header.TexturePipelines.size()) {
|
||||
pipe.Pipeline = header.TexturePipelines[id];
|
||||
} else {
|
||||
LOG.warn() << "Model texture pipeline id out of range: model=" << key
|
||||
<< " local=" << id
|
||||
<< " pipelines=" << header.TexturePipelines.size();
|
||||
pipe.BinTextures.push_back(id);
|
||||
}
|
||||
model.TextureKeys = {};
|
||||
model.TextureMap.emplace(tkey, std::move(pipe));
|
||||
}
|
||||
model.TextureKeys = {};
|
||||
|
||||
for(const HeadlessModel::Cuboid& cb : hm.Cuboids) {
|
||||
glm::vec3 min = glm::min(cb.From, cb.To), max = glm::max(cb.From, cb.To);
|
||||
for(const HeadlessModel::Cuboid& cb : hm.Cuboids) {
|
||||
glm::vec3 min = glm::min(cb.From, cb.To), max = glm::max(cb.From, cb.To);
|
||||
|
||||
for(const auto& [face, params] : cb.Faces) {
|
||||
glm::vec2 from_uv = {params.UV[0], params.UV[1]}, to_uv = {params.UV[2], params.UV[3]};
|
||||
for(const auto& [face, params] : cb.Faces) {
|
||||
glm::vec2 from_uv = {params.UV[0], params.UV[1]}, to_uv = {params.UV[2], params.UV[3]};
|
||||
|
||||
uint32_t texId;
|
||||
{
|
||||
auto iter = std::find(model.TextureKeys.begin(), model.TextureKeys.end(), params.Texture);
|
||||
if(iter == model.TextureKeys.end()) {
|
||||
texId = model.TextureKeys.size();
|
||||
model.TextureKeys.push_back(params.Texture);
|
||||
} else {
|
||||
texId = iter-model.TextureKeys.begin();
|
||||
}
|
||||
uint32_t texId;
|
||||
{
|
||||
auto iter = std::find(model.TextureKeys.begin(), model.TextureKeys.end(), params.Texture);
|
||||
if(iter == model.TextureKeys.end()) {
|
||||
texId = model.TextureKeys.size();
|
||||
model.TextureKeys.push_back(params.Texture);
|
||||
} else {
|
||||
texId = iter-model.TextureKeys.begin();
|
||||
}
|
||||
}
|
||||
|
||||
std::vector<Vertex> v;
|
||||
std::vector<Vertex> v;
|
||||
|
||||
auto addQuad = [&](const glm::vec3& p0,
|
||||
const glm::vec3& p1,
|
||||
@@ -275,22 +224,23 @@ public:
|
||||
}
|
||||
|
||||
cb.Trs.apply(v);
|
||||
model.Vertecies[params.Cullface].append_range(v);
|
||||
const EnumFace cullKey = (params.Cullface == EnumFace::None) ? face : params.Cullface;
|
||||
model.Vertecies[cullKey].append_range(v);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if(!hm.SubModels.empty()) {
|
||||
model.Depends.reserve(hm.SubModels.size());
|
||||
for(const auto& sub : hm.SubModels) {
|
||||
if(!header || sub.Id >= header->ModelDeps.size()) {
|
||||
LOG.warn() << "Model sub-model id out of range: model=" << key
|
||||
<< " local=" << sub.Id
|
||||
<< " deps=" << (header ? header->ModelDeps.size() : 0);
|
||||
continue;
|
||||
}
|
||||
model.Depends.emplace_back(header->ModelDeps[sub.Id], Transformations{});
|
||||
if(!hm.SubModels.empty()) {
|
||||
model.Depends.reserve(hm.SubModels.size());
|
||||
for(const auto& sub : hm.SubModels) {
|
||||
if(sub.Id >= header.Models.size()) {
|
||||
LOG.warn() << "Model sub-model id out of range: model=" << key
|
||||
<< " local=" << sub.Id
|
||||
<< " deps=" << header.Models.size();
|
||||
continue;
|
||||
}
|
||||
model.Depends.emplace_back(header.Models[sub.Id], Transformations{});
|
||||
}
|
||||
}
|
||||
|
||||
// struct Face {
|
||||
// int TintIndex = -1;
|
||||
@@ -299,48 +249,19 @@ public:
|
||||
|
||||
// std::vector<Transformation> Transformations;
|
||||
|
||||
} else if(data.starts_with((const char8_t*) "glTF")) {
|
||||
type = "glb";
|
||||
|
||||
} else if(data.starts_with((const char8_t*) "bgl")) {
|
||||
type = "InternalGLTF";
|
||||
|
||||
} else if(data.starts_with((const char8_t*) "{")) {
|
||||
type = "InternalJson или glTF";
|
||||
// Модель внутреннего формата или glTF
|
||||
}
|
||||
} catch(const std::exception& exc) {
|
||||
LOG.warn() << "Не удалось распарсить модель " << type << ":\n\t" << exc.what();
|
||||
LOG.warn() << "Не удалось собрать модель:\n\t" << exc.what();
|
||||
continue;
|
||||
}
|
||||
|
||||
{
|
||||
static std::atomic<uint32_t> debugModelLogCount = 0;
|
||||
uint32_t idx = debugModelLogCount.fetch_add(1);
|
||||
if(idx < 128) {
|
||||
size_t vertexCount = 0;
|
||||
for(const auto& [_, verts] : model.Vertecies)
|
||||
vertexCount += verts.size();
|
||||
size_t texDepsCount = textureDeps ? textureDeps->size() : 0;
|
||||
LOG.debug() << "Model loaded id=" << key
|
||||
<< " verts=" << vertexCount
|
||||
<< " texKeys=" << model.TextureKeys.size()
|
||||
<< " texDeps=" << texDepsCount;
|
||||
}
|
||||
}
|
||||
|
||||
if(model.Vertecies.empty()) {
|
||||
static std::atomic<uint32_t> debugEmptyModelLogCount = 0;
|
||||
uint32_t idx = debugEmptyModelLogCount.fetch_add(1);
|
||||
if(idx < 128) {
|
||||
LOG.warn() << "Model has empty geometry id=" << key
|
||||
<< " type=" << type
|
||||
<< " size=" << dataSize
|
||||
<< " prefix=" << int(prefix[0]) << '.'
|
||||
<< int(prefix[1]) << '.'
|
||||
<< int(prefix[2]) << '.'
|
||||
<< int(prefix[3]);
|
||||
}
|
||||
size_t vertexCount = 0;
|
||||
for(const auto& [_, verts] : model.Vertecies)
|
||||
vertexCount += verts.size();
|
||||
LOG.debug() << "Model loaded id=" << key
|
||||
<< " verts=" << vertexCount
|
||||
<< " texKeys=" << model.TextureKeys.size()
|
||||
<< " pipelines=" << header.TexturePipelines.size();
|
||||
}
|
||||
|
||||
Models.insert_or_assign(key, std::move(model));
|
||||
@@ -495,7 +416,9 @@ class TextureProvider {
|
||||
public:
|
||||
struct TextureUpdate {
|
||||
AssetsTexture Id = 0;
|
||||
Resource Res;
|
||||
uint16_t Width = 0;
|
||||
uint16_t Height = 0;
|
||||
std::vector<uint32_t> Pixels;
|
||||
std::string Domain;
|
||||
std::string Key;
|
||||
};
|
||||
@@ -635,49 +558,25 @@ public:
|
||||
}
|
||||
|
||||
// Применяет изменения, возвращая все затронутые модели
|
||||
std::vector<AssetsTexture> onTexturesChanges(std::vector<TextureUpdate> newOrChanged, std::vector<AssetsTexture> lost) {
|
||||
std::vector<AssetsTexture> onTexturesChanges(std::vector<AssetsTextureUpdate> entries) {
|
||||
std::lock_guard lock(Mutex);
|
||||
std::vector<AssetsTexture> result;
|
||||
|
||||
for(const auto& update : newOrChanged) {
|
||||
const AssetsTexture key = update.Id;
|
||||
const Resource& res = update.Res;
|
||||
for(auto& entry : entries) {
|
||||
const AssetsTexture key = entry.Id;
|
||||
result.push_back(key);
|
||||
|
||||
iResource sres((const uint8_t*) res.data(), res.size());
|
||||
iBinaryStream stream = sres.makeStream();
|
||||
png::image<png::rgba_pixel> img(stream.Stream);
|
||||
uint32_t width = img.get_width();
|
||||
uint32_t height = img.get_height();
|
||||
|
||||
std::vector<uint32_t> pixels;
|
||||
pixels.resize(width*height);
|
||||
|
||||
for(uint32_t y = 0; y < height; y++) {
|
||||
const auto& row = img.get_pixbuf().operator [](y);
|
||||
for(uint32_t x = 0; x < width; x++) {
|
||||
const auto& px = row[x];
|
||||
uint32_t rgba = (uint32_t(px.alpha) << 24)
|
||||
| (uint32_t(px.red) << 16)
|
||||
| (uint32_t(px.green) << 8)
|
||||
| uint32_t(px.blue);
|
||||
pixels[x + y * width] = rgba;
|
||||
}
|
||||
}
|
||||
if(entry.Width == 0 || entry.Height == 0 || entry.Pixels.empty())
|
||||
continue;
|
||||
|
||||
Atlas->updateTexture(key, StoredTexture(
|
||||
static_cast<uint16_t>(width),
|
||||
static_cast<uint16_t>(height),
|
||||
std::move(pixels)
|
||||
entry.Width,
|
||||
entry.Height,
|
||||
std::move(entry.Pixels)
|
||||
));
|
||||
|
||||
bool animated = false;
|
||||
if(auto anim = getDefaultAnimation(update.Key, width, height)) {
|
||||
AnimatedSources[key] = *anim;
|
||||
animated = true;
|
||||
} else {
|
||||
AnimatedSources.erase(key);
|
||||
}
|
||||
AnimatedSources.erase(key);
|
||||
|
||||
NeedsUpload = true;
|
||||
|
||||
@@ -685,19 +584,11 @@ public:
|
||||
uint32_t idx = debugTextureLogCount.fetch_add(1);
|
||||
if(idx < 128) {
|
||||
LOG.debug() << "Texture loaded id=" << key
|
||||
<< " key=" << update.Domain << ':' << update.Key
|
||||
<< " size=" << width << 'x' << height
|
||||
<< " size=" << entry.Width << 'x' << entry.Height
|
||||
<< " animated=" << (animated ? 1 : 0);
|
||||
}
|
||||
}
|
||||
|
||||
for(AssetsTexture key : lost) {
|
||||
result.push_back(key);
|
||||
Atlas->freeTexture(key);
|
||||
AnimatedSources.erase(key);
|
||||
NeedsUpload = true;
|
||||
}
|
||||
|
||||
std::sort(result.begin(), result.end());
|
||||
auto eraseIter = std::unique(result.begin(), result.end());
|
||||
result.erase(eraseIter, result.end());
|
||||
@@ -911,54 +802,16 @@ public:
|
||||
{}
|
||||
|
||||
// Применяет изменения, возвращает изменённые описания состояний
|
||||
std::vector<AssetsNodestate> onNodestateChanges(std::vector<std::tuple<AssetsNodestate, Resource, const std::vector<uint8_t>*>> newOrChanged, std::vector<AssetsNodestate> lost, std::vector<AssetsModel> changedModels) {
|
||||
std::vector<AssetsNodestate> onNodestateChanges(std::vector<AssetsNodestateUpdate> newOrChanged, std::vector<AssetsModel> changedModels) {
|
||||
std::vector<AssetsNodestate> result;
|
||||
|
||||
for(ResourceId lostId : lost) {
|
||||
auto iterNodestate = Nodestates.find(lostId);
|
||||
if(iterNodestate == Nodestates.end())
|
||||
continue;
|
||||
|
||||
result.push_back(lostId);
|
||||
Nodestates.erase(iterNodestate);
|
||||
}
|
||||
|
||||
for(const auto& [key, resource, deps] : newOrChanged) {
|
||||
for(const AssetsNodestateUpdate& entry : newOrChanged) {
|
||||
const AssetsNodestate key = entry.Id;
|
||||
result.push_back(key);
|
||||
|
||||
PreparedNodeState nodestate;
|
||||
std::string type = "unknown";
|
||||
|
||||
try {
|
||||
std::u8string_view data((const char8_t*) resource.data(), resource.size());
|
||||
if(data.starts_with((const char8_t*) "bn")) {
|
||||
type = "InternalBinary";
|
||||
// Компилированный nodestate внутреннего формата
|
||||
nodestate = PreparedNodeState(data);
|
||||
} else if(data.starts_with((const char8_t*) "{")) {
|
||||
type = "InternalJson";
|
||||
// nodestate в json формате
|
||||
} else {
|
||||
type = "InternalBinaryLegacy";
|
||||
// Старый двоичный формат без заголовка "bn"
|
||||
std::u8string patched;
|
||||
patched.reserve(data.size() + 2);
|
||||
patched.push_back(u8'b');
|
||||
patched.push_back(u8'n');
|
||||
patched.append(data);
|
||||
nodestate = PreparedNodeState(patched);
|
||||
}
|
||||
} catch(const std::exception& exc) {
|
||||
LOG.warn() << "Не удалось распарсить nodestate " << type << ":\n\t" << exc.what();
|
||||
continue;
|
||||
}
|
||||
|
||||
if(deps && !deps->empty()) {
|
||||
auto header = AssetsManager::parseHeader(EnumAssets::Nodestate, *deps);
|
||||
if(header && header->Type == EnumAssets::Nodestate) {
|
||||
nodestate.LocalToModel.assign(header->ModelDeps.begin(), header->ModelDeps.end());
|
||||
}
|
||||
}
|
||||
static_cast<HeadlessNodeState&>(nodestate) = entry.Nodestate;
|
||||
nodestate.LocalToModel.assign(entry.Header.Models.begin(), entry.Header.Models.end());
|
||||
|
||||
Nodestates.insert_or_assign(key, std::move(nodestate));
|
||||
if(key < 64) {
|
||||
@@ -1358,10 +1211,10 @@ class VulkanRenderSession : public IRenderSession {
|
||||
glm::vec3 X64Offset_f, X64Delta; // Смещение мира относительно игрока в матрице вида (0 -> 64)
|
||||
glm::quat Quat;
|
||||
|
||||
ChunkPreparator CP;
|
||||
ModelProvider MP;
|
||||
std::unique_ptr<TextureProvider> TP;
|
||||
ModelProvider MP;
|
||||
std::unique_ptr<NodestateProvider> NSP;
|
||||
ChunkPreparator CP;
|
||||
|
||||
AtlasImage LightDummy;
|
||||
Buffer TestQuad;
|
||||
@@ -1415,7 +1268,7 @@ public:
|
||||
|
||||
virtual void prepareTickSync() override;
|
||||
virtual void pushStageTickSync() override;
|
||||
virtual void tickSync(const TickSyncData& data) override;
|
||||
virtual void tickSync(TickSyncData& data) override;
|
||||
|
||||
virtual void setCameraPos(WorldId_t worldId, Pos::Object pos, glm::quat quat) override;
|
||||
|
||||
|
||||
@@ -824,19 +824,121 @@ std::u8string unCompressLinear(std::u8string_view data) {
|
||||
return *(std::u8string*) &outString;
|
||||
}
|
||||
|
||||
Hash_t ResourceFile::calcHash(const char8_t* data, size_t size) {
|
||||
return sha2::sha256((const uint8_t*) data, size);
|
||||
}
|
||||
|
||||
uint16_t HeadlessNodeState::Header::addModel(AssetsModel id) {
|
||||
auto iter = std::find(Models.begin(), Models.end(), id);
|
||||
if(iter == Models.end()) {
|
||||
Models.push_back(id);
|
||||
return Models.size() - 1;
|
||||
}
|
||||
|
||||
return iter - Models.begin();
|
||||
}
|
||||
|
||||
void HeadlessNodeState::Header::load(std::u8string_view data) {
|
||||
Models.clear();
|
||||
if(data.empty())
|
||||
return;
|
||||
if(data.size() % sizeof(AssetsModel) != 0)
|
||||
MAKE_ERROR("Invalid nodestate header size");
|
||||
|
||||
const size_t count = data.size() / sizeof(AssetsModel);
|
||||
Models.resize(count);
|
||||
std::memcpy(Models.data(), data.data(), data.size());
|
||||
}
|
||||
|
||||
ResourceHeader HeadlessNodeState::Header::dump() const {
|
||||
ResourceHeader rh;
|
||||
rh.reserve(Models.size() * sizeof(AssetsModel));
|
||||
|
||||
for(AssetsModel id : Models) {
|
||||
rh += std::u8string_view((const char8_t*) &id, sizeof(AssetsModel));
|
||||
}
|
||||
|
||||
return rh;
|
||||
}
|
||||
|
||||
uint16_t HeadlessModel::Header::addModel(AssetsModel id) {
|
||||
auto iter = std::find(Models.begin(), Models.end(), id);
|
||||
if(iter == Models.end()) {
|
||||
Models.push_back(id);
|
||||
return Models.size() - 1;
|
||||
}
|
||||
|
||||
return iter - Models.begin();
|
||||
}
|
||||
|
||||
uint16_t HeadlessModel::Header::addTexturePipeline(std::vector<uint8_t> pipeline) {
|
||||
TexturePipelines.push_back(std::move(pipeline));
|
||||
return TexturePipelines.size() - 1;
|
||||
}
|
||||
|
||||
void HeadlessModel::Header::load(std::u8string_view data) {
|
||||
Models.clear();
|
||||
TexturePipelines.clear();
|
||||
if(data.empty())
|
||||
return;
|
||||
|
||||
try {
|
||||
TOS::ByteBuffer buffer(data.size(), reinterpret_cast<const uint8_t*>(data.data()));
|
||||
auto reader = buffer.reader();
|
||||
|
||||
uint16_t modelCount = reader.readUInt16();
|
||||
Models.reserve(modelCount);
|
||||
for(uint16_t i = 0; i < modelCount; ++i)
|
||||
Models.push_back(reader.readUInt32());
|
||||
|
||||
uint16_t texCount = reader.readUInt16();
|
||||
TexturePipelines.reserve(texCount);
|
||||
for(uint16_t i = 0; i < texCount; ++i) {
|
||||
uint32_t size32 = reader.readUInt32();
|
||||
TOS::ByteBuffer pipe;
|
||||
reader.readBuffer(pipe);
|
||||
if(pipe.size() != size32)
|
||||
MAKE_ERROR("Invalid model header size");
|
||||
TexturePipelines.emplace_back(pipe.begin(), pipe.end());
|
||||
}
|
||||
} catch(const std::exception&) {
|
||||
MAKE_ERROR("Invalid model header");
|
||||
}
|
||||
}
|
||||
|
||||
ResourceHeader HeadlessModel::Header::dump() const {
|
||||
TOS::ByteBuffer rh;
|
||||
|
||||
{
|
||||
uint32_t fullSize = 0;
|
||||
for(const auto& vector : TexturePipelines)
|
||||
fullSize += vector.size();
|
||||
rh.reserve(2 + Models.size() * sizeof(AssetsModel) + 2 + 4 * TexturePipelines.size() + fullSize);
|
||||
}
|
||||
|
||||
TOS::ByteBuffer::Writer wr;
|
||||
wr << uint16_t(Models.size());
|
||||
for(AssetsModel id : Models)
|
||||
wr << id;
|
||||
|
||||
wr << uint16_t(TexturePipelines.size());
|
||||
for(const auto& pipe : TexturePipelines) {
|
||||
wr << uint32_t(pipe.size());
|
||||
wr << pipe;
|
||||
}
|
||||
|
||||
TOS::ByteBuffer buff = wr.complite();
|
||||
|
||||
return std::u8string((const char8_t*) buff.data(), buff.size());
|
||||
}
|
||||
|
||||
ResourceHeader HeadlessNodeState::parse(const js::object& profile, const std::function<AssetsModel(const std::string_view model)>& modelResolver) {
|
||||
std::vector<AssetsModel> headerIds;
|
||||
Header header;
|
||||
|
||||
std::function<uint16_t(const std::string_view model)> headerResolver =
|
||||
[&](const std::string_view model) -> uint16_t {
|
||||
AssetsModel id = modelResolver(model);
|
||||
auto iter = std::find(headerIds.begin(), headerIds.end(), id);
|
||||
if(iter == headerIds.end()) {
|
||||
headerIds.push_back(id);
|
||||
return headerIds.size()-1;
|
||||
}
|
||||
|
||||
return iter-headerIds.begin();
|
||||
return header.addModel(id);
|
||||
};
|
||||
|
||||
for(auto& [condition, variability] : profile) {
|
||||
@@ -865,14 +967,7 @@ ResourceHeader HeadlessNodeState::parse(const js::object& profile, const std::fu
|
||||
Routes.emplace_back(node, std::move(models));
|
||||
}
|
||||
|
||||
ResourceHeader rh;
|
||||
rh.reserve(headerIds.size()*sizeof(AssetsModel));
|
||||
|
||||
for(AssetsModel id : headerIds) {
|
||||
rh += std::u8string_view((const char8_t*) &id, sizeof(AssetsModel));
|
||||
}
|
||||
|
||||
return rh;
|
||||
return header.dump();
|
||||
}
|
||||
|
||||
ResourceHeader HeadlessNodeState::parse(const sol::table& profile, const std::function<AssetsModel(const std::string_view model)>& modelResolver) {
|
||||
@@ -1490,21 +1585,14 @@ ResourceHeader HeadlessModel::parse(
|
||||
const std::function<AssetsModel(const std::string_view model)>& modelResolver,
|
||||
const std::function<std::vector<uint8_t>(const std::string_view texturePipelineSrc)>& textureResolver
|
||||
) {
|
||||
std::vector<AssetsModel> headerIdsModels;
|
||||
Header header;
|
||||
|
||||
std::function<uint16_t(const std::string_view model)> headerResolverModel =
|
||||
[&](const std::string_view model) -> uint16_t {
|
||||
AssetsModel id = modelResolver(model);
|
||||
auto iter = std::find(headerIdsModels.begin(), headerIdsModels.end(), id);
|
||||
if(iter == headerIdsModels.end()) {
|
||||
headerIdsModels.push_back(id);
|
||||
return headerIdsModels.size()-1;
|
||||
}
|
||||
|
||||
return iter-headerIdsModels.begin();
|
||||
return header.addModel(id);
|
||||
};
|
||||
|
||||
std::vector<std::vector<uint8_t>> headerIdsTextures;
|
||||
std::unordered_map<std::string, uint32_t, detail::TSVHash, detail::TSVEq> textureToLocal;
|
||||
|
||||
std::function<uint16_t(const std::string_view texturePipelineSrc)> headerResolverTexture =
|
||||
@@ -1514,9 +1602,8 @@ ResourceHeader HeadlessModel::parse(
|
||||
return iter->second;
|
||||
}
|
||||
|
||||
std::vector<uint8_t> program = textureResolver(texturePipelineSrc);
|
||||
headerIdsTextures.push_back(program);
|
||||
uint16_t id = textureToLocal[(std::string) texturePipelineSrc] = headerIdsTextures.size()-1;
|
||||
uint16_t id = header.addTexturePipeline(textureResolver(texturePipelineSrc));
|
||||
textureToLocal[(std::string) texturePipelineSrc] = id;
|
||||
return id;
|
||||
};
|
||||
|
||||
@@ -1744,27 +1831,7 @@ ResourceHeader HeadlessModel::parse(
|
||||
// Заголовок
|
||||
TOS::ByteBuffer rh;
|
||||
|
||||
{
|
||||
uint32_t fullSize = 0;
|
||||
for(const auto& vector : headerIdsTextures)
|
||||
fullSize += vector.size();
|
||||
rh.reserve(2+headerIdsModels.size()*sizeof(AssetsModel)+2+(4)*headerIdsTextures.size()+fullSize);
|
||||
}
|
||||
|
||||
TOS::ByteBuffer::Writer wr;
|
||||
wr << uint16_t(headerIdsModels.size());
|
||||
for(AssetsModel id : headerIdsModels)
|
||||
wr << id;
|
||||
|
||||
wr << uint16_t(headerIdsTextures.size());
|
||||
for(const auto& pipe : headerIdsTextures) {
|
||||
wr << uint32_t(pipe.size());
|
||||
wr << pipe;
|
||||
}
|
||||
|
||||
TOS::ByteBuffer buff = wr.complite();
|
||||
|
||||
return std::u8string((const char8_t*) buff.data(), buff.size());
|
||||
return header.dump();
|
||||
}
|
||||
|
||||
ResourceHeader HeadlessModel::parse(
|
||||
|
||||
@@ -553,6 +553,19 @@ inline std::pair<std::string, std::string> parseDomainKey(const std::string& val
|
||||
}
|
||||
}
|
||||
|
||||
struct ResourceFile {
|
||||
using Hash_t = std::array<uint8_t, 32>;
|
||||
|
||||
Hash_t Hash;
|
||||
std::u8string Data;
|
||||
|
||||
static Hash_t calcHash(const char8_t* data, size_t size);
|
||||
|
||||
void calcHash() {
|
||||
Hash = calcHash(Data.data(), Data.size());
|
||||
}
|
||||
};
|
||||
|
||||
struct NodestateEntry {
|
||||
std::string Name;
|
||||
int Variability = 0; // Количество возможный значений состояния
|
||||
@@ -662,6 +675,14 @@ struct HeadlessNodeState {
|
||||
std::vector<Transformation> Transforms;
|
||||
};
|
||||
|
||||
struct Header {
|
||||
std::vector<AssetsModel> Models;
|
||||
|
||||
uint16_t addModel(AssetsModel id);
|
||||
void load(std::u8string_view data);
|
||||
ResourceHeader dump() const;
|
||||
};
|
||||
|
||||
// Ноды выражений
|
||||
std::vector<Node> Nodes;
|
||||
// Условия -> вариации модели + веса
|
||||
@@ -899,6 +920,16 @@ struct HeadlessModel {
|
||||
std::optional<EnumGuiLight> GuiLight = EnumGuiLight::Default;
|
||||
std::optional<bool> AmbientOcclusion = false;
|
||||
|
||||
struct Header {
|
||||
std::vector<AssetsModel> Models;
|
||||
std::vector<std::vector<uint8_t>> TexturePipelines;
|
||||
|
||||
uint16_t addModel(AssetsModel id);
|
||||
uint16_t addTexturePipeline(std::vector<uint8_t> pipeline);
|
||||
void load(std::u8string_view data);
|
||||
ResourceHeader dump() const;
|
||||
};
|
||||
|
||||
struct FullTransformation {
|
||||
glm::vec3
|
||||
Rotation = glm::vec3(0),
|
||||
|
||||
@@ -1,5 +1,9 @@
|
||||
#include "AssetsPreloader.hpp"
|
||||
#include "Common/Abstract.hpp"
|
||||
#include "Common/TexturePipelineProgram.hpp"
|
||||
#include "sha2.hpp"
|
||||
#include <atomic>
|
||||
#include <filesystem>
|
||||
#include <fstream>
|
||||
#include <unordered_set>
|
||||
#include <utility>
|
||||
@@ -42,21 +46,37 @@ static std::u8string readOptionalMeta(const fs::path& path) {
|
||||
}
|
||||
|
||||
AssetsPreloader::AssetsPreloader() {
|
||||
std::fill(NextId.begin(), NextId.end(), 1);
|
||||
std::fill(LastSendId.begin(), LastSendId.end(), 1);
|
||||
for(size_t type = 0; type < static_cast<size_t>(EnumAssets::MAX_ENUM); type++) {
|
||||
ResourceLinks[type].emplace_back(
|
||||
ResourceFile::Hash_t{0},
|
||||
ResourceHeader(),
|
||||
fs::file_time_type(),
|
||||
fs::path{""},
|
||||
false
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
AssetsPreloader::Out_reloadResources AssetsPreloader::reloadResources(const AssetsRegister& instances, ReloadStatus* status) {
|
||||
AssetsPreloader::Out_checkAndPrepareResourcesUpdate AssetsPreloader::checkAndPrepareResourcesUpdate(
|
||||
const AssetsRegister& instances,
|
||||
const std::function<ResourceId(EnumAssets type, std::string_view domain, std::string_view key)>& idResolver,
|
||||
const std::function<void(std::u8string&& resource, ResourceFile::Hash_t hash, fs::path resPath)>& onNewResourceParsed,
|
||||
ReloadStatus* status
|
||||
) {
|
||||
assert(idResolver);
|
||||
|
||||
#ifndef NDEBUG
|
||||
bool expected = false;
|
||||
assert(_Reloading.compare_exchange_strong(expected, true) && "Двойной вызов reloadResources");
|
||||
struct ReloadGuard {
|
||||
std::atomic<bool>& Flag;
|
||||
~ReloadGuard() { Flag.exchange(false); }
|
||||
} guard{_Reloading};
|
||||
#endif
|
||||
|
||||
try {
|
||||
ReloadStatus secondStatus;
|
||||
return _reloadResources(instances, status ? *status : secondStatus);
|
||||
return _checkAndPrepareResourcesUpdate(instances, idResolver, onNewResourceParsed, status ? *status : secondStatus);
|
||||
} catch(const std::exception& exc) {
|
||||
LOG.error() << exc.what();
|
||||
assert(!"reloadResources: здесь не должно быть ошибок");
|
||||
@@ -67,9 +87,12 @@ AssetsPreloader::Out_reloadResources AssetsPreloader::reloadResources(const Asse
|
||||
}
|
||||
}
|
||||
|
||||
AssetsPreloader::Out_reloadResources AssetsPreloader::_reloadResources(const AssetsRegister& instances, ReloadStatus& status) {
|
||||
Out_reloadResources result;
|
||||
|
||||
AssetsPreloader::Out_checkAndPrepareResourcesUpdate AssetsPreloader::_checkAndPrepareResourcesUpdate(
|
||||
const AssetsRegister& instances,
|
||||
const std::function<ResourceId(EnumAssets type, std::string_view domain, std::string_view key)>& idResolver,
|
||||
const std::function<void(std::u8string&& resource, ResourceFile::Hash_t hash, fs::path resPath)>& onNewResourceParsed,
|
||||
ReloadStatus& status
|
||||
) {
|
||||
// 1) Поиск всех ресурсов и построение конечной карты ресурсов (timestamps, path, name, size)
|
||||
// Карта найденных ресурсов
|
||||
std::array<
|
||||
@@ -87,12 +110,12 @@ AssetsPreloader::Out_reloadResources AssetsPreloader::_reloadResources(const Ass
|
||||
static_cast<size_t>(AssetType::MAX_ENUM)
|
||||
> resourcesFirstStage;
|
||||
|
||||
for (const fs::path& instance : instances.Assets) {
|
||||
for(const fs::path& instance : instances.Assets) {
|
||||
try {
|
||||
if (fs::is_regular_file(instance)) {
|
||||
if(fs::is_regular_file(instance)) {
|
||||
// Может архив
|
||||
/// TODO: пока не поддерживается
|
||||
} else if (fs::is_directory(instance)) {
|
||||
} else if(fs::is_directory(instance)) {
|
||||
// Директория
|
||||
fs::path assetsRoot = instance;
|
||||
fs::path assetsCandidate = instance / "assets";
|
||||
@@ -122,20 +145,20 @@ AssetsPreloader::Out_reloadResources AssetsPreloader::_reloadResources(const Ass
|
||||
>& firstStage = resourcesFirstStage[static_cast<size_t>(assetType)][domain];
|
||||
|
||||
// Исследуем все ресурсы одного типа
|
||||
for (auto begin = fs::recursive_directory_iterator(assetPath), end = fs::recursive_directory_iterator(); begin != end; begin++) {
|
||||
if (begin->is_directory())
|
||||
for(auto begin = fs::recursive_directory_iterator(assetPath), end = fs::recursive_directory_iterator(); begin != end; begin++) {
|
||||
if(begin->is_directory())
|
||||
continue;
|
||||
|
||||
fs::path file = begin->path();
|
||||
if (assetType == AssetType::Texture && file.extension() == ".meta")
|
||||
if(assetType == AssetType::Texture && file.extension() == ".meta")
|
||||
continue;
|
||||
|
||||
std::string key = fs::relative(file, assetPath).generic_string();
|
||||
if (firstStage.contains(key))
|
||||
if(firstStage.contains(key))
|
||||
continue;
|
||||
|
||||
fs::file_time_type timestamp = fs::last_write_time(file);
|
||||
if (assetType == AssetType::Texture) {
|
||||
if(assetType == AssetType::Texture) {
|
||||
fs::path metaPath = file;
|
||||
metaPath += ".meta";
|
||||
if (fs::exists(metaPath) && fs::is_regular_file(metaPath)) {
|
||||
@@ -148,7 +171,8 @@ AssetsPreloader::Out_reloadResources AssetsPreloader::_reloadResources(const Ass
|
||||
// Работаем с ресурсом
|
||||
firstStage[key] = ResourceFindInfo{
|
||||
.Path = file,
|
||||
.Timestamp = timestamp
|
||||
.Timestamp = timestamp,
|
||||
.Id = idResolver(assetType, domain, key)
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -158,7 +182,6 @@ AssetsPreloader::Out_reloadResources AssetsPreloader::_reloadResources(const Ass
|
||||
}
|
||||
} catch (const std::exception& exc) {
|
||||
/// TODO: Логгировать в статусе
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -172,14 +195,14 @@ AssetsPreloader::Out_reloadResources AssetsPreloader::_reloadResources(const Ass
|
||||
= [&](const std::string_view model) -> uint32_t
|
||||
{
|
||||
auto [mDomain, mKey] = parseDomainKey(model, domain);
|
||||
return getId(AssetType::Model, mDomain, mKey);
|
||||
return idResolver(AssetType::Model, mDomain, mKey);
|
||||
};
|
||||
|
||||
std::function<std::optional<uint32_t>(std::string_view)> textureIdResolver
|
||||
= [&](std::string_view texture) -> std::optional<uint32_t>
|
||||
{
|
||||
auto [mDomain, mKey] = parseDomainKey(texture, domain);
|
||||
return getId(AssetType::Texture, mDomain, mKey);
|
||||
return idResolver(AssetType::Texture, mDomain, mKey);
|
||||
};
|
||||
|
||||
std::function<std::vector<uint8_t>(const std::string_view)> textureResolver
|
||||
@@ -202,8 +225,8 @@ AssetsPreloader::Out_reloadResources AssetsPreloader::_reloadResources(const Ass
|
||||
|
||||
HeadlessNodeState hns;
|
||||
out.Header = hns.parse(obj, modelResolver);
|
||||
out.Resource = std::make_shared<std::u8string>(hns.dump());
|
||||
out.Hash = sha2::sha256((const uint8_t*) out.Resource->data(), out.Resource->size());
|
||||
out.Resource = hns.dump();
|
||||
out.Hash = sha2::sha256((const uint8_t*) out.Resource.data(), out.Resource.size());
|
||||
} else if (type == AssetType::Model) {
|
||||
const std::string ext = info.Path.extension().string();
|
||||
if (ext == ".json") {
|
||||
@@ -213,19 +236,8 @@ AssetsPreloader::Out_reloadResources AssetsPreloader::_reloadResources(const Ass
|
||||
|
||||
HeadlessModel hm;
|
||||
out.Header = hm.parse(obj, modelResolver, textureResolver);
|
||||
std::u8string compiled = hm.dump();
|
||||
if(hm.Cuboids.empty()) {
|
||||
static std::atomic<uint32_t> debugEmptyModelLogCount = 0;
|
||||
uint32_t idx = debugEmptyModelLogCount.fetch_add(1);
|
||||
if(idx < 128) {
|
||||
LOG.warn() << "Model compiled with empty cuboids: "
|
||||
<< domain << ':' << key
|
||||
<< " file=" << info.Path.string()
|
||||
<< " size=" << compiled.size();
|
||||
}
|
||||
}
|
||||
out.Resource = std::make_shared<std::u8string>(std::move(compiled));
|
||||
out.Hash = sha2::sha256((const uint8_t*) out.Resource->data(), out.Resource->size());
|
||||
out.Resource = hm.dump();
|
||||
out.Hash = sha2::sha256((const uint8_t*) out.Resource.data(), out.Resource.size());
|
||||
// } else if (ext == ".gltf" || ext == ".glb") {
|
||||
// /// TODO: добавить поддержку gltf
|
||||
// ResourceFile file = readFileBytes(info.Path);
|
||||
@@ -236,239 +248,152 @@ AssetsPreloader::Out_reloadResources AssetsPreloader::_reloadResources(const Ass
|
||||
}
|
||||
} else if (type == AssetType::Texture) {
|
||||
ResourceFile file = readFileBytes(info.Path);
|
||||
out.Resource = std::make_shared<std::u8string>(std::move(file.Data));
|
||||
out.Resource = std::move(file.Data);
|
||||
out.Hash = file.Hash;
|
||||
out.Header = readOptionalMeta(info.Path);
|
||||
} else {
|
||||
ResourceFile file = readFileBytes(info.Path);
|
||||
out.Resource = std::make_shared<std::u8string>(std::move(file.Data));
|
||||
out.Resource = std::move(file.Data);
|
||||
out.Hash = file.Hash;
|
||||
}
|
||||
|
||||
out.Id = getId(type, domain, key);
|
||||
out.Id = idResolver(type, domain, key);
|
||||
|
||||
return out;
|
||||
};
|
||||
|
||||
// 2) Обрабатываться будут только изменённые (новый timestamp) или новые ресурсы
|
||||
// Определяем каких ресурсов не стало
|
||||
// 2) Определяем какие ресурсы изменились (новый timestamp) или новые ресурсы
|
||||
Out_checkAndPrepareResourcesUpdate result;
|
||||
|
||||
// Собираем идентификаторы, чтобы потом определить какие ресурсы пропали
|
||||
std::array<
|
||||
std::unordered_set<ResourceId>,
|
||||
static_cast<size_t>(EnumAssets::MAX_ENUM)
|
||||
> uniqueExists;
|
||||
|
||||
for(size_t type = 0; type < static_cast<size_t>(AssetType::MAX_ENUM); ++type) {
|
||||
auto& tableResourcesFirstStage = resourcesFirstStage[type];
|
||||
for(const auto& [id, resource] : MediaResources[type]) {
|
||||
if(tableResourcesFirstStage.empty()) {
|
||||
result.Lost[type][resource.Domain].push_back(resource.Key);
|
||||
continue;
|
||||
}
|
||||
auto& uniqueExistsTypes = uniqueExists[type];
|
||||
const auto& resourceLinksTyped = ResourceLinks[type];
|
||||
result.MaxNewSize[type] = resourceLinksTyped.size();
|
||||
|
||||
auto iterDomain = tableResourcesFirstStage.find(resource.Domain);
|
||||
if(iterDomain == tableResourcesFirstStage.end()) {
|
||||
result.Lost[type][resource.Domain].push_back(resource.Key);
|
||||
continue;
|
||||
}
|
||||
{
|
||||
size_t allIds = 0;
|
||||
for(const auto& [domain, keys] : resourcesFirstStage[type])
|
||||
allIds += keys.size();
|
||||
|
||||
if(!iterDomain->second.contains(resource.Key)) {
|
||||
result.Lost[type][resource.Domain].push_back(resource.Key);
|
||||
}
|
||||
uniqueExistsTypes.reserve(allIds);
|
||||
}
|
||||
}
|
||||
|
||||
// Определение новых или изменённых ресурсов
|
||||
for(size_t type = 0; type < static_cast<size_t>(AssetType::MAX_ENUM); ++type) {
|
||||
for(const auto& [domain, table] : resourcesFirstStage[type]) {
|
||||
auto iterTableDomain = DKToId[type].find(domain);
|
||||
if(iterTableDomain == DKToId[type].end()) {
|
||||
// Домен неизвестен движку, все ресурсы в нём новые
|
||||
for(const auto& [key, info] : table) {
|
||||
PendingResource resource = buildResource(static_cast<AssetType>(type), domain, key, info);
|
||||
result.NewOrChange[type][domain].push_back(std::move(resource));
|
||||
}
|
||||
} else {
|
||||
for(const auto& [key, info] : table) {
|
||||
bool needsUpdate = true;
|
||||
if(auto iterKey = iterTableDomain->second.find(key); iterKey != iterTableDomain->second.end()) {
|
||||
// Идентификатор найден
|
||||
auto iterRes = MediaResources[type].find(iterKey->second);
|
||||
// Если нашли ресурс по идентификатору и время изменения не поменялось, то он не новый и не изменился
|
||||
if(iterRes != MediaResources[type].end() && iterRes->second.Timestamp == info.Timestamp)
|
||||
needsUpdate = false;
|
||||
for(const auto& [domain, keys] : resourcesFirstStage[type]) {
|
||||
for(const auto& [key, res] : keys) {
|
||||
uniqueExistsTypes.insert(res.Id);
|
||||
|
||||
if(res.Id >= resourceLinksTyped.size() || !resourceLinksTyped[res.Id].IsExist)
|
||||
{ // Если идентификатора нет в таблице или ресурс не привязан
|
||||
PendingResource resource = buildResource(static_cast<AssetType>(type), domain, key, res);
|
||||
if(onNewResourceParsed)
|
||||
onNewResourceParsed(std::move(resource.Resource), resource.Hash, res.Path);
|
||||
result.HashToPathNew[resource.Hash].push_back(res.Path);
|
||||
|
||||
if(res.Id >= result.MaxNewSize[type])
|
||||
result.MaxNewSize[type] = res.Id+1;
|
||||
|
||||
result.ResourceUpdates[type].emplace_back(res.Id, resource.Hash, std::move(resource.Header), resource.Timestamp, res.Path);
|
||||
} else if(resourceLinksTyped[res.Id].Path != res.Path
|
||||
|| resourceLinksTyped[res.Id].LastWrite != res.Timestamp
|
||||
) { // Если ресурс теперь берётся с другого места или изменилось время изменения файла
|
||||
const auto& lastResource = resourceLinksTyped[res.Id];
|
||||
PendingResource resource = buildResource(static_cast<AssetType>(type), domain, key, res);
|
||||
|
||||
if(lastResource.Hash != resource.Hash) {
|
||||
// Хэш изменился
|
||||
// Сообщаем о новом ресурсе
|
||||
if(onNewResourceParsed)
|
||||
onNewResourceParsed(std::move(resource.Resource), resource.Hash, res.Path);
|
||||
// Старый хэш более не доступен по этому расположению.
|
||||
result.HashToPathLost[lastResource.Hash].push_back(resourceLinksTyped[res.Id].Path);
|
||||
// Новый хеш стал доступен по этому расположению.
|
||||
result.HashToPathNew[resource.Hash].push_back(res.Path);
|
||||
} else if(resourceLinksTyped[res.Id].Path != res.Path) {
|
||||
// Изменился конечный путь.
|
||||
// Хэш более не доступен по этому расположению.
|
||||
result.HashToPathLost[resource.Hash].push_back(resourceLinksTyped[res.Id].Path);
|
||||
// Хеш теперь доступен по этому расположению.
|
||||
result.HashToPathNew[resource.Hash].push_back(res.Path);
|
||||
} else {
|
||||
// Ресурс без заголовка никак не изменился.
|
||||
}
|
||||
|
||||
if(!needsUpdate)
|
||||
continue;
|
||||
|
||||
PendingResource resource = buildResource(static_cast<AssetType>(type), domain, key, info);
|
||||
result.NewOrChange[(int) type][domain].push_back(std::move(resource));
|
||||
// Чтобы там не поменялось, мог поменятся заголовок. Уведомляем о новой привязке.
|
||||
result.ResourceUpdates[type].emplace_back(res.Id, resource.Hash, std::move(resource.Header), resource.Timestamp, res.Path);
|
||||
} else {
|
||||
// Ресурс не изменился
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
AssetsPreloader::Out_applyResourceChange AssetsPreloader::applyResourceChange(const Out_reloadResources& orr) {
|
||||
Out_applyResourceChange result;
|
||||
|
||||
// Удаляем ресурсы
|
||||
/*
|
||||
Удаляются только ресурсы, при этом за ними остаётся бронь на идентификатор
|
||||
Уже скомпилированные зависимости к ресурсам не будут
|
||||
перекомпилироваться для смены идентификатора.
|
||||
Если нужный ресурс появится, то привязка останется.
|
||||
Новые клиенты не получат ресурс которого нет,
|
||||
но он может использоваться
|
||||
*/
|
||||
for(size_t type = 0; type < static_cast<size_t>(AssetType::MAX_ENUM); type++) {
|
||||
for(const auto& [domain, keys] : orr.Lost[type]) {
|
||||
auto iterDomain = DKToId[type].find(domain);
|
||||
|
||||
// Если уже было решено, что ресурсы были, и стали потерянными, то так и должно быть
|
||||
assert(iterDomain != DKToId[type].end());
|
||||
|
||||
for(const auto& key : keys) {
|
||||
auto iterKey = iterDomain->second.find(key);
|
||||
|
||||
// Ресурс был и должен быть
|
||||
assert(iterKey != iterDomain->second.end());
|
||||
|
||||
uint32_t id = iterKey->second;
|
||||
auto& resType = MediaResources[type];
|
||||
auto iterRes = resType.find(id);
|
||||
if(iterRes == resType.end())
|
||||
continue;
|
||||
|
||||
// Ресурс был потерян
|
||||
result.Lost[type].push_back(id);
|
||||
// Hash более нам неизвестен
|
||||
HashToId.erase(iterRes->second.Hash);
|
||||
// Затираем ресурс
|
||||
resType.erase(iterRes);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Добавляем
|
||||
for(int type = 0; type < (int) AssetType::MAX_ENUM; type++) {
|
||||
auto& typeTable = DKToId[type];
|
||||
for(const auto& [domain, resources] : orr.NewOrChange[type]) {
|
||||
auto& domainTable = typeTable[domain];
|
||||
for(const PendingResource& pending : resources) {
|
||||
MediaResource resource {
|
||||
.Domain = domain,
|
||||
.Key = std::move(pending.Key),
|
||||
.Timestamp = pending.Timestamp,
|
||||
.Resource = std::move(pending.Resource),
|
||||
.Hash = pending.Hash,
|
||||
.Header = std::move(pending.Header)
|
||||
};
|
||||
|
||||
auto& table = MediaResources[type];
|
||||
// Нужно затереть старую ссылку хеша на данный ресурс
|
||||
if(auto iter = table.find(pending.Id); iter != table.end())
|
||||
HashToId.erase(iter->second.Hash);
|
||||
|
||||
// Добавили ресурс
|
||||
table[pending.Id] = resource;
|
||||
// Связали с хешем
|
||||
HashToId[resource.Hash] = {static_cast<AssetType>(type), pending.Id};
|
||||
// Осведомили о новом/изменённом ресурсе
|
||||
result.NewOrChange[type].emplace_back(pending.Id, resource.Hash, std::move(resource.Header));
|
||||
}
|
||||
}
|
||||
|
||||
// Не должно быть ресурсов, которые были помечены как потерянные
|
||||
#ifndef NDEBUG
|
||||
std::unordered_set<uint32_t> changed;
|
||||
for(const auto& [id, _, _2] : result.NewOrChange[type])
|
||||
changed.insert(id);
|
||||
|
||||
auto& lost = result.Lost[type];
|
||||
for(auto iter : lost)
|
||||
assert(!changed.contains(iter));
|
||||
#endif
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
AssetsPreloader::Out_bakeId AssetsPreloader::bakeIdTables() {
|
||||
#ifndef NDEBUG
|
||||
|
||||
assert(!DKToIdInBakingMode);
|
||||
DKToIdInBakingMode = true;
|
||||
struct _tempStruct {
|
||||
AssetsPreloader* handler;
|
||||
~_tempStruct() { handler->DKToIdInBakingMode = false; }
|
||||
} _lock{this};
|
||||
|
||||
#endif
|
||||
|
||||
Out_bakeId result;
|
||||
|
||||
// 3) Определяем какие ресурсы пропали
|
||||
for(size_t type = 0; type < static_cast<size_t>(AssetType::MAX_ENUM); ++type) {
|
||||
// домен+ключ -> id
|
||||
{
|
||||
auto lock = NewDKToId[type].lock();
|
||||
auto& dkToId = DKToId[type];
|
||||
for(auto& [domain, keys] : *lock) {
|
||||
// Если домен не существует, просто воткнёт новые ключи
|
||||
auto [iterDomain, inserted] = dkToId.try_emplace(domain, std::move(keys));
|
||||
if(!inserted) {
|
||||
// Домен уже существует, сливаем новые ключи
|
||||
iterDomain->second.merge(keys);
|
||||
}
|
||||
}
|
||||
const auto& resourceLinksTyped = ResourceLinks[type];
|
||||
|
||||
lock->clear();
|
||||
}
|
||||
size_t counter = 0;
|
||||
for(const auto& [hash, header, timestamp, path, isExist] : resourceLinksTyped) {
|
||||
size_t id = counter++;
|
||||
if(!isExist)
|
||||
continue;
|
||||
|
||||
// id -> домен+ключ
|
||||
{
|
||||
auto lock = NewIdToDK[type].lock();
|
||||
if(uniqueExists[type].contains(id))
|
||||
continue;
|
||||
|
||||
auto& idToDK = IdToDK[type];
|
||||
result.IdToDK[type] = std::move(*lock);
|
||||
lock->clear();
|
||||
idToDK.append_range(result.IdToDK[type]);
|
||||
|
||||
// result.LastSendId[type] = LastSendId[type];
|
||||
LastSendId[type] = NextId[type];
|
||||
// Ресурс потерян
|
||||
// Хэш более не доступен по этому расположению.
|
||||
result.HashToPathLost[hash].push_back(path);
|
||||
result.LostLinks[type].push_back(id);
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
AssetsPreloader::Out_fullSync AssetsPreloader::collectFullSync() const {
|
||||
Out_fullSync out;
|
||||
AssetsPreloader::Out_applyResourcesUpdate AssetsPreloader::applyResourcesUpdate(const Out_checkAndPrepareResourcesUpdate& orr) {
|
||||
Out_applyResourcesUpdate result;
|
||||
|
||||
for(size_t type = 0; type < static_cast<size_t>(AssetType::MAX_ENUM); ++type) {
|
||||
out.IdToDK[type] = IdToDK[type];
|
||||
}
|
||||
for(size_t type = 0; type < static_cast<size_t>(EnumAssets::MAX_ENUM); ++type) {
|
||||
// Затираем потерянные
|
||||
for(ResourceId id : orr.LostLinks[type]) {
|
||||
assert(id < ResourceLinks[type].size());
|
||||
auto& [hash, header, timestamp, path, isExist] = ResourceLinks[type][id];
|
||||
hash = {0};
|
||||
header = {};
|
||||
timestamp = fs::file_time_type();
|
||||
path.clear();
|
||||
isExist = false;
|
||||
|
||||
for(size_t type = 0; type < static_cast<size_t>(AssetType::MAX_ENUM); ++type) {
|
||||
for(const auto& [id, resource] : MediaResources[type]) {
|
||||
out.HashHeaders[type].push_back(BindHashHeaderInfo{
|
||||
.Id = id,
|
||||
.Hash = resource.Hash,
|
||||
.Header = resource.Header
|
||||
});
|
||||
out.Resources.emplace_back(
|
||||
static_cast<AssetType>(type),
|
||||
id,
|
||||
&resource
|
||||
);
|
||||
result.NewOrUpdates[type].emplace_back(id, hash, header);
|
||||
}
|
||||
|
||||
// Увеличиваем размер, если необходимо
|
||||
if(orr.MaxNewSize[type] > ResourceLinks[type].size()) {
|
||||
ResourceLink def{
|
||||
ResourceFile::Hash_t{0},
|
||||
ResourceHeader(),
|
||||
fs::file_time_type(),
|
||||
fs::path{""},
|
||||
false
|
||||
};
|
||||
|
||||
ResourceLinks[type].resize(orr.MaxNewSize[type], def);
|
||||
}
|
||||
|
||||
// Обновляем / добавляем
|
||||
for(auto& [id, hash, header, timestamp, path] : orr.ResourceUpdates[type]) {
|
||||
ResourceLinks[type][id] = {hash, std::move(header), timestamp, std::move(path), true};
|
||||
result.NewOrUpdates[type].emplace_back(id, hash, header);
|
||||
}
|
||||
}
|
||||
|
||||
return out;
|
||||
}
|
||||
|
||||
std::tuple<AssetsNodestate, std::vector<AssetsModel>, std::vector<AssetsTexture>>
|
||||
AssetsPreloader::getNodeDependency(const std::string& domain, const std::string& key) {
|
||||
(void)domain;
|
||||
(void)key;
|
||||
return {0, {}, {}};
|
||||
return result;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
#pragma once
|
||||
|
||||
#include <algorithm>
|
||||
#include <array>
|
||||
#include <cstdint>
|
||||
#include <cstring>
|
||||
@@ -12,19 +11,13 @@
|
||||
#include <string_view>
|
||||
#include <tuple>
|
||||
#include <unordered_map>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
#include "Common/TexturePipelineProgram.hpp"
|
||||
#include "Abstract.hpp"
|
||||
#include "Common/Abstract.hpp"
|
||||
#include "Common/Async.hpp"
|
||||
#include "TOSAsync.hpp"
|
||||
#include "TOSLib.hpp"
|
||||
#include "sha2.hpp"
|
||||
|
||||
/*
|
||||
Класс отвечает за отслеживание изменений и подгрузки медиаресурсов в указанных директориях.
|
||||
Медиаресурсы, собранные из папки assets или зарегистрированные модами.
|
||||
Хранит все данные в оперативной памяти.
|
||||
*/
|
||||
|
||||
static constexpr const char* EnumAssetsToDirectory(LV::EnumAssets value) {
|
||||
@@ -49,34 +42,10 @@ namespace LV {
|
||||
namespace fs = std::filesystem;
|
||||
using AssetType = EnumAssets;
|
||||
|
||||
struct ResourceFile {
|
||||
using Hash_t = sha2::sha256_hash; // boost::uuids::detail::sha1::digest_type;
|
||||
|
||||
Hash_t Hash;
|
||||
std::u8string Data;
|
||||
|
||||
void calcHash() {
|
||||
Hash = sha2::sha256((const uint8_t*) Data.data(), Data.size());
|
||||
}
|
||||
};
|
||||
|
||||
class AssetsPreloader {
|
||||
public:
|
||||
using Ptr = std::shared_ptr<AssetsPreloader>;
|
||||
using IdTable =
|
||||
std::unordered_map<
|
||||
std::string, // Domain
|
||||
std::unordered_map<
|
||||
std::string, // Key
|
||||
uint32_t, // ResourceId
|
||||
detail::TSVHash,
|
||||
detail::TSVEq
|
||||
>,
|
||||
detail::TSVHash,
|
||||
detail::TSVEq
|
||||
>;
|
||||
|
||||
//
|
||||
/*
|
||||
Ресурс имеет бинарную часть, из который вырезаны все зависимости.
|
||||
Вторая часть это заголовок, которые всегда динамично передаётся с сервера.
|
||||
@@ -100,63 +69,13 @@ public:
|
||||
std::string Key;
|
||||
fs::file_time_type Timestamp;
|
||||
// Обезличенный ресурс
|
||||
std::shared_ptr<std::u8string> Resource;
|
||||
std::u8string Resource;
|
||||
// Его хеш
|
||||
ResourceFile::Hash_t Hash;
|
||||
// Заголовок
|
||||
std::u8string Header;
|
||||
};
|
||||
|
||||
struct BindDomainKeyInfo {
|
||||
std::string Domain;
|
||||
std::string Key;
|
||||
};
|
||||
|
||||
struct BindHashHeaderInfo {
|
||||
ResourceId Id;
|
||||
Hash_t Hash;
|
||||
std::u8string Header;
|
||||
};
|
||||
|
||||
struct Out_reloadResources {
|
||||
std::unordered_map<std::string, std::vector<PendingResource>> NewOrChange[(int) AssetType::MAX_ENUM];
|
||||
std::unordered_map<std::string, std::vector<std::string>> Lost[(int) AssetType::MAX_ENUM];
|
||||
};
|
||||
|
||||
struct Out_applyResourceChange {
|
||||
std::array<
|
||||
std::vector<AssetsPreloader::BindHashHeaderInfo>,
|
||||
static_cast<size_t>(AssetType::MAX_ENUM)
|
||||
> NewOrChange;
|
||||
|
||||
std::array<
|
||||
std::vector<ResourceId>,
|
||||
static_cast<size_t>(AssetType::MAX_ENUM)
|
||||
> Lost;
|
||||
};
|
||||
|
||||
struct Out_bakeId {
|
||||
// Новые привязки
|
||||
std::array<
|
||||
std::vector<BindDomainKeyInfo>,
|
||||
static_cast<size_t>(AssetType::MAX_ENUM)
|
||||
> IdToDK;
|
||||
};
|
||||
|
||||
struct Out_fullSync {
|
||||
std::array<
|
||||
std::vector<BindDomainKeyInfo>,
|
||||
static_cast<size_t>(AssetType::MAX_ENUM)
|
||||
> IdToDK;
|
||||
|
||||
std::array<
|
||||
std::vector<BindHashHeaderInfo>,
|
||||
static_cast<size_t>(AssetType::MAX_ENUM)
|
||||
> HashHeaders;
|
||||
|
||||
std::vector<std::tuple<AssetType, ResourceId, const MediaResource*>> Resources;
|
||||
};
|
||||
|
||||
struct ReloadStatus {
|
||||
/// TODO: callback'и для обновления статусов
|
||||
/// TODO: многоуровневый статус std::vector<std::string>. Этапы/Шаги/Объекты
|
||||
@@ -202,59 +121,125 @@ public:
|
||||
! Бронирует идентификаторы используя getId();
|
||||
|
||||
instances -> пути к директории с assets или архивы с assets внутри. От низшего приоритета к высшему.
|
||||
idResolver -> функция получения идентификатора по Тип+Домен+Ключ
|
||||
onNewResourceParsed -> Callback на обработку распаршенных ресурсов без заголовков
|
||||
(на стороне сервера хранится в другой сущности, на стороне клиента игнорируется).
|
||||
status -> обратный отклик о процессе обновления ресурсов.
|
||||
ReloadStatus <- новые и потерянные ресурсы.
|
||||
*/
|
||||
Out_reloadResources reloadResources(const AssetsRegister& instances, ReloadStatus* status = nullptr);
|
||||
struct Out_checkAndPrepareResourcesUpdate {
|
||||
// Новые связки Id -> Hash + Header + Timestamp + Path (ресурс новый или изменён)
|
||||
std::array<
|
||||
std::vector<
|
||||
std::tuple<
|
||||
ResourceId, // Ресурс
|
||||
ResourceFile::Hash_t, // Хэш ресурса на диске
|
||||
ResourceHeader, // Хедер ресурса (со всеми зависимостями)
|
||||
fs::file_time_type, // Время изменения ресурса на диске
|
||||
fs::path // Путь до ресурса
|
||||
>
|
||||
>,
|
||||
static_cast<size_t>(AssetType::MAX_ENUM)
|
||||
> ResourceUpdates;
|
||||
|
||||
// Используется чтобы эффективно увеличить размер таблиц
|
||||
std::array<
|
||||
ResourceId,
|
||||
static_cast<size_t>(AssetType::MAX_ENUM)
|
||||
> MaxNewSize;
|
||||
|
||||
// Потерянные связки Id (ресурс физически потерян)
|
||||
std::array<
|
||||
std::vector<ResourceId>,
|
||||
static_cast<size_t>(AssetType::MAX_ENUM)
|
||||
> LostLinks;
|
||||
|
||||
/*
|
||||
Новые пути предоставляющие хеш
|
||||
(по каким путям можно получить ресурс определённого хеша).
|
||||
*/
|
||||
std::unordered_map<
|
||||
ResourceFile::Hash_t,
|
||||
std::vector<fs::path>
|
||||
> HashToPathNew;
|
||||
|
||||
/*
|
||||
Потерянные пути, предоставлявшые ресурсы с данным хешем
|
||||
(пути по которым уже нельзя получить заданных хеш).
|
||||
*/
|
||||
std::unordered_map<
|
||||
ResourceFile::Hash_t,
|
||||
std::vector<fs::path>
|
||||
> HashToPathLost;
|
||||
};
|
||||
|
||||
Out_checkAndPrepareResourcesUpdate checkAndPrepareResourcesUpdate(
|
||||
const AssetsRegister& instances,
|
||||
const std::function<ResourceId(EnumAssets type, std::string_view domain, std::string_view key)>& idResolver,
|
||||
const std::function<void(std::u8string&& resource, ResourceFile::Hash_t hash, fs::path resPath)>& onNewResourceParsed = nullptr,
|
||||
ReloadStatus* status = nullptr
|
||||
);
|
||||
|
||||
/*
|
||||
Применяет расчитанные изменения.
|
||||
|
||||
Out_applyResourceChange <- Нужно отправить клиентам новые привязки ресурсов
|
||||
Out_applyResourceUpdate <- Нужно отправить клиентам новые привязки ресурсов
|
||||
id -> hash+header
|
||||
*/
|
||||
Out_applyResourceChange applyResourceChange(const Out_reloadResources& orr);
|
||||
struct BindHashHeaderInfo {
|
||||
ResourceId Id;
|
||||
ResourceFile::Hash_t Hash;
|
||||
ResourceHeader Header;
|
||||
};
|
||||
|
||||
/*
|
||||
Выдаёт идентификатор ресурса.
|
||||
Многопоточно.
|
||||
Иногда нужно вызывать bakeIdTables чтобы оптимизировать таблицы
|
||||
идентификаторов. При этом никто не должен использовать getId
|
||||
*/
|
||||
ResourceId getId(AssetType type, std::string_view domain, std::string_view key);
|
||||
struct Out_applyResourcesUpdate {
|
||||
std::array<
|
||||
std::vector<BindHashHeaderInfo>,
|
||||
static_cast<size_t>(EnumAssets::MAX_ENUM)
|
||||
> NewOrUpdates;
|
||||
};
|
||||
|
||||
/*
|
||||
Оптимизирует таблицы идентификаторов.
|
||||
Нельзя использовать пока есть вероятность что кто-то использует getId().
|
||||
Такжке нельзя при выполнении reloadResources().
|
||||
Out_applyResourcesUpdate applyResourcesUpdate(const Out_checkAndPrepareResourcesUpdate& orr);
|
||||
|
||||
Out_bakeId <- Нужно отправить подключенным клиентам новые привязки id -> домен+ключ
|
||||
*/
|
||||
Out_bakeId bakeIdTables();
|
||||
std::array<
|
||||
std::vector<BindHashHeaderInfo>,
|
||||
static_cast<size_t>(EnumAssets::MAX_ENUM)
|
||||
> collectHashBindings() const
|
||||
{
|
||||
std::array<
|
||||
std::vector<BindHashHeaderInfo>,
|
||||
static_cast<size_t>(EnumAssets::MAX_ENUM)
|
||||
> result;
|
||||
|
||||
// Выдаёт полный список привязок и ресурсов для новых клиентов.
|
||||
Out_fullSync collectFullSync() const;
|
||||
for(size_t type = 0; type < static_cast<size_t>(EnumAssets::MAX_ENUM); ++type) {
|
||||
result[type].reserve(ResourceLinks[type].size());
|
||||
|
||||
/*
|
||||
Выдаёт пакет со всеми текущими привязками id -> домен+ключ.
|
||||
Используется при подключении новых клиентов.
|
||||
*/
|
||||
void makeGlobalLinkagePacket() {
|
||||
/// TODO: Собрать пакет с IdToDK и сжать его домены и ключи и id -> hash+header
|
||||
ResourceId counter = 0;
|
||||
for(const auto& [hash, header, _1, _2, _3] : ResourceLinks[type]) {
|
||||
ResourceId id = counter++;
|
||||
result[type].emplace_back(id, hash, header);
|
||||
}
|
||||
}
|
||||
|
||||
// Тот же пакет для обновления идентификаторов
|
||||
std::unreachable();
|
||||
return result;
|
||||
}
|
||||
|
||||
// Выдаёт ресурс по идентификатору
|
||||
const MediaResource* getResource(AssetType type, uint32_t id) const;
|
||||
const auto& getResourceLinks() const {
|
||||
return ResourceLinks;
|
||||
}
|
||||
|
||||
// Выдаёт ресурс по хешу
|
||||
std::optional<std::tuple<AssetType, uint32_t, const MediaResource*>> getResource(const ResourceFile::Hash_t& hash);
|
||||
struct Out_Resource {
|
||||
ResourceFile::Hash_t Hash;
|
||||
fs::path Path;
|
||||
};
|
||||
|
||||
// Выдаёт зависимости к ресурсам профиля ноды
|
||||
std::tuple<AssetsNodestate, std::vector<AssetsModel>, std::vector<AssetsTexture>>
|
||||
getNodeDependency(const std::string& domain, const std::string& key);
|
||||
std::optional<Out_Resource> getResource(EnumAssets type, ResourceId id) const {
|
||||
const auto& rl = ResourceLinks[static_cast<size_t>(type)];
|
||||
if(id >= rl.size() || !rl[id].IsExist)
|
||||
return std::nullopt;
|
||||
|
||||
return Out_Resource{rl[id].Hash, rl[id].Path};
|
||||
}
|
||||
|
||||
private:
|
||||
struct ResourceFindInfo {
|
||||
@@ -262,6 +247,8 @@ private:
|
||||
fs::path ArchivePath, Path;
|
||||
// Время изменения файла
|
||||
fs::file_time_type Timestamp;
|
||||
// Идентификатор ресурса
|
||||
ResourceId Id;
|
||||
};
|
||||
|
||||
struct HashHasher {
|
||||
@@ -275,136 +262,32 @@ private:
|
||||
}
|
||||
};
|
||||
|
||||
#ifndef NDEBUG
|
||||
// Текущее состояние reloadResources
|
||||
std::atomic<bool> _Reloading = false;
|
||||
|
||||
// Если идентификатор не найден в асинхронной таблице, переходим к работе с синхронной
|
||||
ResourceId _getIdNew(AssetType type, std::string_view domain, std::string_view key);
|
||||
|
||||
Out_reloadResources _reloadResources(const AssetsRegister& instances, ReloadStatus& status);
|
||||
|
||||
#ifndef NDEBUG
|
||||
// Для контроля за режимом слияния ключей
|
||||
bool DKToIdInBakingMode = false;
|
||||
#endif
|
||||
|
||||
/*
|
||||
Многопоточная таблица идентификаторов. Новые идентификаторы выделяются в NewDKToId,
|
||||
и далее вливаются в основную таблицу при вызове bakeIdTables()
|
||||
*/
|
||||
std::array<IdTable, static_cast<size_t>(AssetType::MAX_ENUM)> DKToId;
|
||||
/*
|
||||
Многопоточная таблица обратного резолва.
|
||||
Идентификатор -> домен+ключ
|
||||
*/
|
||||
std::array<std::vector<BindDomainKeyInfo>, static_cast<size_t>(AssetType::MAX_ENUM)> IdToDK;
|
||||
struct ResourceLink {
|
||||
ResourceFile::Hash_t Hash; // Хэш ресурса на диске
|
||||
/// TODO: клиенту не нужны хедеры
|
||||
ResourceHeader Header; // Хедер ресурса (со всеми зависимостями)
|
||||
fs::file_time_type LastWrite; // Время изменения ресурса на диске
|
||||
fs::path Path; // Путь до ресурса
|
||||
bool IsExist;
|
||||
};
|
||||
|
||||
/*
|
||||
Таблица в которой выделяются новые идентификаторы, которых не нашлось в DKToId.
|
||||
Данный объект одновременно может работать только с одним потоком.
|
||||
*/
|
||||
std::array<TOS::SpinlockObject<IdTable>, static_cast<size_t>(AssetType::MAX_ENUM)> NewDKToId;
|
||||
/*
|
||||
Конец поля идентификаторов, известный клиентам.
|
||||
Если NextId продвинулся дальше, нужно уведомить клиентов о новых привязках.
|
||||
*/
|
||||
std::array<ResourceId, static_cast<size_t>(AssetType::MAX_ENUM)> LastSendId;
|
||||
/*
|
||||
Списки в которых пишутся новые привязки. Начала спиской исходят из LastSendId.
|
||||
Id + LastSendId -> домен+ключ
|
||||
*/
|
||||
std::array<TOS::SpinlockObject<std::vector<BindDomainKeyInfo>>, static_cast<size_t>(AssetType::MAX_ENUM)> NewIdToDK;
|
||||
Out_checkAndPrepareResourcesUpdate _checkAndPrepareResourcesUpdate(
|
||||
const AssetsRegister& instances,
|
||||
const std::function<ResourceId(EnumAssets type, std::string_view domain, std::string_view key)>& idResolver,
|
||||
const std::function<void(std::u8string&& resource, ResourceFile::Hash_t hash, fs::path resPath)>& onNewResourceParsed,
|
||||
ReloadStatus& status
|
||||
);
|
||||
|
||||
// Загруженные ресурсы
|
||||
std::array<std::unordered_map<ResourceId, MediaResource>, static_cast<size_t>(AssetType::MAX_ENUM)> MediaResources;
|
||||
// Hash -> ресурс
|
||||
std::unordered_map<ResourceFile::Hash_t, std::pair<AssetType, ResourceId>, HashHasher> HashToId;
|
||||
// Для последовательного выделения идентификаторов
|
||||
std::array<ResourceId, static_cast<size_t>(AssetType::MAX_ENUM)> NextId;
|
||||
// Привязка Id -> Hash + Header + Timestamp + Path
|
||||
std::array<
|
||||
std::vector<ResourceLink>,
|
||||
static_cast<size_t>(AssetType::MAX_ENUM)
|
||||
> ResourceLinks;
|
||||
};
|
||||
|
||||
inline ResourceId AssetsPreloader::getId(AssetType type, std::string_view domain, std::string_view key) {
|
||||
#ifndef NDEBUG
|
||||
assert(!DKToIdInBakingMode);
|
||||
#endif
|
||||
|
||||
const auto& typeTable = DKToId[static_cast<size_t>(type)];
|
||||
auto domainTable = typeTable.find(domain);
|
||||
|
||||
#ifndef NDEBUG
|
||||
assert(!DKToIdInBakingMode);
|
||||
#endif
|
||||
|
||||
if(domainTable == typeTable.end())
|
||||
return _getIdNew(type, domain, key);
|
||||
|
||||
auto keyTable = domainTable->second.find(key);
|
||||
|
||||
if (keyTable == domainTable->second.end())
|
||||
return _getIdNew(type, domain, key);
|
||||
|
||||
return keyTable->second;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
inline ResourceId AssetsPreloader::_getIdNew(AssetType type, std::string_view domain, std::string_view key) {
|
||||
auto lock = NewDKToId[static_cast<size_t>(type)].lock();
|
||||
|
||||
auto iterDomainNewTable = lock->find(domain);
|
||||
if(iterDomainNewTable == lock->end()) {
|
||||
iterDomainNewTable = lock->emplace_hint(
|
||||
iterDomainNewTable,
|
||||
(std::string) domain,
|
||||
std::unordered_map<std::string, uint32_t, detail::TSVHash, detail::TSVEq>{}
|
||||
);
|
||||
}
|
||||
|
||||
auto& domainNewTable = iterDomainNewTable->second;
|
||||
|
||||
if(auto iter = domainNewTable.find(key); iter != domainNewTable.end())
|
||||
return iter->second;
|
||||
|
||||
uint32_t id = domainNewTable[(std::string) key] = NextId[static_cast<size_t>(type)]++;
|
||||
|
||||
auto lock2 = NewIdToDK[static_cast<size_t>(type)].lock();
|
||||
lock.unlock();
|
||||
|
||||
lock2->emplace_back((std::string) domain, (std::string) key);
|
||||
|
||||
return id;
|
||||
}
|
||||
|
||||
inline const AssetsPreloader::MediaResource* AssetsPreloader::getResource(AssetType type, uint32_t id) const {
|
||||
auto& iterType = MediaResources[static_cast<size_t>(type)];
|
||||
|
||||
auto iterRes = iterType.find(id);
|
||||
if(iterRes == iterType.end())
|
||||
return nullptr;
|
||||
|
||||
return &iterRes->second;
|
||||
}
|
||||
|
||||
inline std::optional<std::tuple<AssetType, uint32_t, const AssetsPreloader::MediaResource*>>
|
||||
AssetsPreloader::getResource(const ResourceFile::Hash_t& hash)
|
||||
{
|
||||
auto iter = HashToId.find(hash);
|
||||
if(iter == HashToId.end())
|
||||
return std::nullopt;
|
||||
|
||||
auto [type, id] = iter->second;
|
||||
const MediaResource* res = getResource(type, id);
|
||||
if(!res) {
|
||||
HashToId.erase(iter);
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
if(res->Hash != hash) {
|
||||
HashToId.erase(iter);
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
return std::tuple<AssetType, uint32_t, const MediaResource*>{type, id, res};
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
272
Src/Common/IdProvider.hpp
Normal file
272
Src/Common/IdProvider.hpp
Normal file
@@ -0,0 +1,272 @@
|
||||
#pragma once
|
||||
|
||||
#include "Common/Abstract.hpp"
|
||||
|
||||
#include <ankerl/unordered_dense.h>
|
||||
|
||||
#include <array>
|
||||
#include <atomic>
|
||||
#include <cassert>
|
||||
#include <optional>
|
||||
#include <shared_mutex>
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
#include <algorithm>
|
||||
|
||||
namespace LV {
|
||||
|
||||
template<class Enum = EnumAssets, size_t ShardCount = 64>
|
||||
class IdProvider {
|
||||
public:
|
||||
static constexpr size_t MAX_ENUM = static_cast<size_t>(Enum::MAX_ENUM);
|
||||
|
||||
struct BindDomainKeyInfo {
|
||||
std::string Domain, Key;
|
||||
};
|
||||
|
||||
struct BindDomainKeyViewInfo {
|
||||
std::string_view Domain, Key;
|
||||
};
|
||||
|
||||
struct KeyHash {
|
||||
using is_transparent = void;
|
||||
|
||||
static inline std::size_t h(std::string_view sv) noexcept {
|
||||
return std::hash<std::string_view>{}(sv);
|
||||
}
|
||||
|
||||
static inline std::size_t mix(std::size_t a, std::size_t b) noexcept {
|
||||
a ^= b + 0x9e3779b97f4a7c15ULL + (a << 6) + (a >> 2);
|
||||
return a;
|
||||
}
|
||||
|
||||
std::size_t operator()(const BindDomainKeyInfo& k) const noexcept {
|
||||
return mix(h(k.Domain), h(k.Key));
|
||||
}
|
||||
|
||||
std::size_t operator()(const BindDomainKeyViewInfo& kv) const noexcept {
|
||||
return mix(h(kv.Domain), h(kv.Key));
|
||||
}
|
||||
};
|
||||
|
||||
struct KeyEq {
|
||||
using is_transparent = void;
|
||||
|
||||
bool operator()(const BindDomainKeyInfo& a, const BindDomainKeyInfo& b) const noexcept {
|
||||
return a.Domain == b.Domain && a.Key == b.Key;
|
||||
}
|
||||
|
||||
bool operator()(const BindDomainKeyInfo& a, const BindDomainKeyViewInfo& b) const noexcept {
|
||||
return a.Domain == b.Domain && a.Key == b.Key;
|
||||
}
|
||||
|
||||
bool operator()(const BindDomainKeyViewInfo& a, const BindDomainKeyInfo& b) const noexcept {
|
||||
return a.Domain == b.Domain && a.Key == b.Key;
|
||||
}
|
||||
};
|
||||
|
||||
public:
|
||||
explicit IdProvider() {
|
||||
for(size_t type = 0; type < MAX_ENUM; ++type) {
|
||||
_NextId[type].store(1, std::memory_order_relaxed);
|
||||
_Reverse[type].reserve(1024);
|
||||
|
||||
IdToDK[type].push_back({"core", "none"});
|
||||
|
||||
auto& sh = _shardFor(static_cast<Enum>(type), "core", "none");
|
||||
std::unique_lock lk(sh.mutex);
|
||||
sh.map.emplace(BindDomainKeyInfo{"core", "none"}, 0);
|
||||
|
||||
// ensure id 0 has a reverse mapping too
|
||||
_storeReverse(static_cast<Enum>(type), 0, std::string("core"), std::string("none"));
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
Находит или выдаёт идентификатор на запрошенный ресурс.
|
||||
Функция не требует внешней синхронизации.
|
||||
*/
|
||||
inline ResourceId getId(Enum type, std::string_view domain, std::string_view key) {
|
||||
#ifndef NDEBUG
|
||||
assert(!DKToIdInBakingMode);
|
||||
#endif
|
||||
auto& sh = _shardFor(type, domain, key);
|
||||
|
||||
// 1) Поиск в режиме для чтения
|
||||
{
|
||||
std::shared_lock lk(sh.mutex);
|
||||
if(auto it = sh.map.find(BindDomainKeyViewInfo{domain, key}); it != sh.map.end()) {
|
||||
return it->second;
|
||||
}
|
||||
}
|
||||
|
||||
// 2) Блокируем и повторно ищем запись (может кто уже успел её добавить)
|
||||
std::unique_lock lk(sh.mutex);
|
||||
if (auto it = sh.map.find(BindDomainKeyViewInfo{domain, key}); it != sh.map.end()) {
|
||||
return it->second;
|
||||
}
|
||||
|
||||
// Выделяем идентификатор
|
||||
ResourceId id = _NextId[static_cast<size_t>(type)].fetch_add(1, std::memory_order_relaxed);
|
||||
|
||||
std::string d(domain);
|
||||
std::string k(key);
|
||||
|
||||
sh.map.emplace(BindDomainKeyInfo{d, k}, id);
|
||||
sh.newlyInserted.push_back(id);
|
||||
|
||||
_storeReverse(type, id, std::move(d), std::move(k));
|
||||
|
||||
return id;
|
||||
}
|
||||
|
||||
/*
|
||||
Переносит все новые идентификаторы в основную таблицу.
|
||||
|
||||
В этой реализации "основная таблица" уже основная (forward map обновляется сразу),
|
||||
а bake() собирает только новые привязки (domain,key) по логам вставок и дополняет IdToDK.
|
||||
|
||||
Нельзя использовать пока есть вероятность что кто-то использует getId(), если ты хочешь
|
||||
строгий debug-контроль как раньше. В релизе это не требуется: bake читает только reverse,
|
||||
а forward не трогает.
|
||||
*/
|
||||
std::array<std::vector<BindDomainKeyInfo>, MAX_ENUM> bake() {
|
||||
#ifndef NDEBUG
|
||||
assert(!DKToIdInBakingMode);
|
||||
DKToIdInBakingMode = true;
|
||||
struct _tempStruct {
|
||||
IdProvider* handler;
|
||||
~_tempStruct() { handler->DKToIdInBakingMode = false; }
|
||||
} _lock{this};
|
||||
#endif
|
||||
|
||||
std::array<std::vector<BindDomainKeyInfo>, MAX_ENUM> result;
|
||||
|
||||
for(size_t t = 0; t < MAX_ENUM; ++t) {
|
||||
auto type = static_cast<Enum>(t);
|
||||
|
||||
// 1) собрать новые id из всех шардов
|
||||
std::vector<ResourceId> new_ids;
|
||||
_drainNew(type, new_ids);
|
||||
|
||||
if(new_ids.empty())
|
||||
continue;
|
||||
|
||||
// 2) превратить id -> (domain,key) через reverse и вернуть наружу
|
||||
// + дописать в IdToDK[type] в порядке id (по желанию)
|
||||
std::sort(new_ids.begin(), new_ids.end());
|
||||
new_ids.erase(std::unique(new_ids.begin(), new_ids.end()), new_ids.end());
|
||||
|
||||
result[t].reserve(new_ids.size());
|
||||
|
||||
// reverse читаем под shared lock
|
||||
std::shared_lock rlk(_ReverseMutex[t]);
|
||||
for(ResourceId id : new_ids) {
|
||||
const std::size_t idx = static_cast<std::size_t>(id);
|
||||
if(idx >= _Reverse[t].size()) {
|
||||
// теоретически не должно случаться (мы пишем reverse до push в log)
|
||||
continue;
|
||||
}
|
||||
|
||||
const auto& e = _Reverse[t][idx];
|
||||
result[t].push_back({e.Domain, e.Key});
|
||||
}
|
||||
|
||||
rlk.unlock();
|
||||
|
||||
// 3) дописать в IdToDK (для новых клиентов)
|
||||
IdToDK[t].append_range(result[t]);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
// id to DK
|
||||
std::optional<BindDomainKeyInfo> getDK(Enum type, ResourceId id) {
|
||||
auto& vec = _Reverse[static_cast<size_t>(type)];
|
||||
auto& mtx = _ReverseMutex[static_cast<size_t>(type)];
|
||||
|
||||
std::unique_lock lk(mtx);
|
||||
if(id >= vec.size())
|
||||
return std::nullopt;
|
||||
|
||||
return vec[id];
|
||||
}
|
||||
|
||||
// Для отправки новым подключенным клиентам
|
||||
const std::array<std::vector<BindDomainKeyInfo>, MAX_ENUM>& idToDK() const {
|
||||
return IdToDK;
|
||||
}
|
||||
|
||||
private:
|
||||
using Map = ankerl::unordered_dense::map<BindDomainKeyInfo, ResourceId, KeyHash, KeyEq>;
|
||||
|
||||
struct Shard {
|
||||
mutable std::shared_mutex mutex;
|
||||
Map map;
|
||||
std::vector<ResourceId> newlyInserted;
|
||||
};
|
||||
|
||||
private:
|
||||
// Кластер таблиц идентификаторов
|
||||
std::array<
|
||||
std::array<Shard, ShardCount>, MAX_ENUM
|
||||
> _Shards;
|
||||
|
||||
// Счётчики идентификаторов
|
||||
std::array<std::atomic<ResourceId>, MAX_ENUM> _NextId;
|
||||
|
||||
// Таблица обратных связок (Id to DK)
|
||||
std::array<std::vector<BindDomainKeyInfo>, MAX_ENUM> _Reverse;
|
||||
mutable std::array<std::shared_mutex, MAX_ENUM> _ReverseMutex;
|
||||
|
||||
#ifndef NDEBUG
|
||||
bool DKToIdInBakingMode = false;
|
||||
#endif
|
||||
|
||||
// stable "full sync" table for new clients:
|
||||
std::array<std::vector<BindDomainKeyInfo>, MAX_ENUM> IdToDK;
|
||||
|
||||
private:
|
||||
Shard& _shardFor(Enum type, const std::string_view domain, const std::string_view key) {
|
||||
const std::size_t idx = KeyHash{}(BindDomainKeyViewInfo{domain, key}) % ShardCount;
|
||||
return _Shards[static_cast<size_t>(type)][idx];
|
||||
}
|
||||
|
||||
const Shard& _shardFor(Enum type, const std::string_view domain, const std::string_view key) const {
|
||||
const std::size_t idx = KeyHash{}(BindDomainKeyViewInfo{domain, key}) % ShardCount;
|
||||
return _Shards[static_cast<size_t>(type)][idx];
|
||||
}
|
||||
|
||||
void _storeReverse(Enum type, ResourceId id, std::string&& domain, std::string&& key) {
|
||||
auto& vec = _Reverse[static_cast<size_t>(type)];
|
||||
auto& mtx = _ReverseMutex[static_cast<size_t>(type)];
|
||||
const std::size_t idx = static_cast<std::size_t>(id);
|
||||
|
||||
std::unique_lock lk(mtx);
|
||||
if(idx >= vec.size())
|
||||
vec.resize(idx + 1);
|
||||
|
||||
vec[idx] = BindDomainKeyInfo{std::move(domain), std::move(key)};
|
||||
}
|
||||
|
||||
void _drainNew(Enum type, std::vector<ResourceId>& out) {
|
||||
out.clear();
|
||||
auto& shards = _Shards[static_cast<size_t>(type)];
|
||||
|
||||
// Можно добавить reserve по эвристике
|
||||
for (auto& sh : shards) {
|
||||
std::unique_lock lk(sh.mutex);
|
||||
if (sh.newlyInserted.empty()) continue;
|
||||
|
||||
const auto old = out.size();
|
||||
out.resize(old + sh.newlyInserted.size());
|
||||
std::copy(sh.newlyInserted.begin(), sh.newlyInserted.end(), out.begin() + old);
|
||||
sh.newlyInserted.clear();
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace LV
|
||||
483
Src/Common/Net2.cpp
Normal file
483
Src/Common/Net2.cpp
Normal file
@@ -0,0 +1,483 @@
|
||||
#include "Net2.hpp"
|
||||
|
||||
#include <boost/asio/buffer.hpp>
|
||||
#include <boost/asio/error.hpp>
|
||||
#include <boost/asio/read.hpp>
|
||||
#include <boost/asio/write.hpp>
|
||||
#include <boost/system/system_error.hpp>
|
||||
#include <algorithm>
|
||||
#include <tuple>
|
||||
|
||||
namespace LV::Net2 {
|
||||
|
||||
using namespace TOS;
|
||||
|
||||
namespace {
|
||||
|
||||
struct HeaderFields {
|
||||
uint32_t size = 0;
|
||||
uint16_t type = 0;
|
||||
Priority priority = Priority::Normal;
|
||||
FrameFlags flags = FrameFlags::None;
|
||||
uint32_t streamId = 0;
|
||||
};
|
||||
|
||||
std::array<std::byte, AsyncSocket::kHeaderSize> encodeHeader(const HeaderFields &h) {
|
||||
std::array<std::byte, AsyncSocket::kHeaderSize> out{};
|
||||
uint32_t sizeNet = detail::toNetwork(h.size);
|
||||
uint16_t typeNet = detail::toNetwork(h.type);
|
||||
uint32_t streamNet = detail::toNetwork(h.streamId);
|
||||
|
||||
std::memcpy(out.data(), &sizeNet, sizeof(sizeNet));
|
||||
std::memcpy(out.data() + 4, &typeNet, sizeof(typeNet));
|
||||
out[6] = std::byte(static_cast<uint8_t>(h.priority));
|
||||
out[7] = std::byte(static_cast<uint8_t>(h.flags));
|
||||
std::memcpy(out.data() + 8, &streamNet, sizeof(streamNet));
|
||||
return out;
|
||||
}
|
||||
|
||||
HeaderFields decodeHeader(const std::array<std::byte, AsyncSocket::kHeaderSize> &in) {
|
||||
HeaderFields h{};
|
||||
std::memcpy(&h.size, in.data(), sizeof(h.size));
|
||||
std::memcpy(&h.type, in.data() + 4, sizeof(h.type));
|
||||
h.priority = static_cast<Priority>(std::to_integer<uint8_t>(in[6]));
|
||||
h.flags = static_cast<FrameFlags>(std::to_integer<uint8_t>(in[7]));
|
||||
std::memcpy(&h.streamId, in.data() + 8, sizeof(h.streamId));
|
||||
|
||||
h.size = detail::fromNetwork(h.size);
|
||||
h.type = detail::fromNetwork(h.type);
|
||||
h.streamId = detail::fromNetwork(h.streamId);
|
||||
return h;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
PacketWriter& PacketWriter::writeBytes(std::span<const std::byte> data) {
|
||||
Buffer.insert(Buffer.end(), data.begin(), data.end());
|
||||
return *this;
|
||||
}
|
||||
|
||||
PacketWriter& PacketWriter::writeString(std::string_view str) {
|
||||
write<uint32_t>(static_cast<uint32_t>(str.size()));
|
||||
auto bytes = std::as_bytes(std::span<const char>(str.data(), str.size()));
|
||||
Buffer.insert(Buffer.end(), bytes.begin(), bytes.end());
|
||||
return *this;
|
||||
}
|
||||
|
||||
std::vector<std::byte> PacketWriter::release() {
|
||||
std::vector<std::byte> out = std::move(Buffer);
|
||||
Buffer.clear();
|
||||
return out;
|
||||
}
|
||||
|
||||
void PacketWriter::clear() {
|
||||
Buffer.clear();
|
||||
}
|
||||
|
||||
PacketReader::PacketReader(std::span<const std::byte> data)
|
||||
: Data(data)
|
||||
{
|
||||
}
|
||||
|
||||
void PacketReader::readBytes(std::span<std::byte> out) {
|
||||
require(out.size());
|
||||
std::memcpy(out.data(), Data.data() + Pos, out.size());
|
||||
Pos += out.size();
|
||||
}
|
||||
|
||||
std::string PacketReader::readString() {
|
||||
uint32_t size = read<uint32_t>();
|
||||
require(size);
|
||||
std::string out(size, '\0');
|
||||
std::memcpy(out.data(), Data.data() + Pos, size);
|
||||
Pos += size;
|
||||
return out;
|
||||
}
|
||||
|
||||
void PacketReader::require(size_t size) {
|
||||
if(Data.size() - Pos < size)
|
||||
MAKE_ERROR("Net2::PacketReader: not enough data");
|
||||
}
|
||||
|
||||
SocketServer::SocketServer(asio::io_context &ioc, std::function<coro<>(tcp::socket)> &&onConnect, uint16_t port)
|
||||
: AsyncObject(ioc), Acceptor(ioc, tcp::endpoint(tcp::v4(), port))
|
||||
{
|
||||
assert(onConnect);
|
||||
co_spawn(run(std::move(onConnect)));
|
||||
}
|
||||
|
||||
bool SocketServer::isStopped() const {
|
||||
return !Acceptor.is_open();
|
||||
}
|
||||
|
||||
uint16_t SocketServer::getPort() const {
|
||||
return Acceptor.local_endpoint().port();
|
||||
}
|
||||
|
||||
coro<void> SocketServer::run(std::function<coro<>(tcp::socket)> onConnect) {
|
||||
while(true) {
|
||||
try {
|
||||
co_spawn(onConnect(co_await Acceptor.async_accept()));
|
||||
} catch(const std::exception &exc) {
|
||||
if(const boost::system::system_error *errc = dynamic_cast<const boost::system::system_error*>(&exc);
|
||||
errc && (errc->code() == asio::error::operation_aborted || errc->code() == asio::error::bad_descriptor))
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
AsyncSocket::SendQueue::SendQueue(asio::io_context &ioc)
|
||||
: semaphore(ioc)
|
||||
{
|
||||
semaphore.expires_at(std::chrono::steady_clock::time_point::max());
|
||||
}
|
||||
|
||||
bool AsyncSocket::SendQueue::empty() const {
|
||||
for(const auto &queue : queues) {
|
||||
if(!queue.empty())
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
AsyncSocket::AsyncSocket(asio::io_context &ioc, tcp::socket &&socket, Limits limits)
|
||||
: AsyncObject(ioc), LimitsCfg(limits), Socket(std::move(socket)), Outgoing(ioc)
|
||||
{
|
||||
Context = std::make_shared<AsyncContext>();
|
||||
|
||||
boost::asio::socket_base::linger optionLinger(true, 4);
|
||||
Socket.set_option(optionLinger);
|
||||
boost::asio::ip::tcp::no_delay optionNoDelay(true);
|
||||
Socket.set_option(optionNoDelay);
|
||||
|
||||
co_spawn(sendLoop());
|
||||
}
|
||||
|
||||
AsyncSocket::~AsyncSocket() {
|
||||
if(Context)
|
||||
Context->needShutdown.store(true);
|
||||
|
||||
{
|
||||
boost::lock_guard lock(Outgoing.mtx);
|
||||
Outgoing.semaphore.cancel();
|
||||
WorkDeadline.cancel();
|
||||
}
|
||||
|
||||
if(Socket.is_open())
|
||||
try { Socket.close(); } catch(...) {}
|
||||
}
|
||||
|
||||
void AsyncSocket::enqueue(OutgoingMessage &&msg) {
|
||||
if(msg.payload.size() > LimitsCfg.maxMessageSize) {
|
||||
setError("Net2::AsyncSocket: message too large");
|
||||
close();
|
||||
return;
|
||||
}
|
||||
|
||||
boost::unique_lock lock(Outgoing.mtx);
|
||||
const size_t msgSize = msg.payload.size();
|
||||
const size_t lowIndex = static_cast<size_t>(Priority::Low);
|
||||
|
||||
if(msg.priority == Priority::Low) {
|
||||
while(Outgoing.bytesInLow + msgSize > LimitsCfg.maxLowPriorityBytes && !Outgoing.queues[lowIndex].empty()) {
|
||||
Outgoing.bytesInQueue -= Outgoing.queues[lowIndex].front().payload.size();
|
||||
Outgoing.bytesInLow -= Outgoing.queues[lowIndex].front().payload.size();
|
||||
Outgoing.queues[lowIndex].pop_front();
|
||||
}
|
||||
if(Outgoing.bytesInLow + msgSize > LimitsCfg.maxLowPriorityBytes) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
if(Outgoing.bytesInQueue + msgSize > LimitsCfg.maxQueueBytes) {
|
||||
dropLow(msgSize);
|
||||
if(Outgoing.bytesInQueue + msgSize > LimitsCfg.maxQueueBytes) {
|
||||
if(msg.dropIfOverloaded)
|
||||
return;
|
||||
setError("Net2::AsyncSocket: send queue overflow");
|
||||
close();
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
const size_t idx = static_cast<size_t>(msg.priority);
|
||||
Outgoing.bytesInQueue += msgSize;
|
||||
if(msg.priority == Priority::Low)
|
||||
Outgoing.bytesInLow += msgSize;
|
||||
Outgoing.queues[idx].push_back(std::move(msg));
|
||||
|
||||
if(Outgoing.waiting) {
|
||||
Outgoing.waiting = false;
|
||||
Outgoing.semaphore.cancel();
|
||||
Outgoing.semaphore.expires_at(std::chrono::steady_clock::time_point::max());
|
||||
}
|
||||
}
|
||||
|
||||
coro<IncomingMessage> AsyncSocket::readMessage() {
|
||||
while(true) {
|
||||
std::array<std::byte, kHeaderSize> headerBytes{};
|
||||
co_await readExact(headerBytes.data(), headerBytes.size());
|
||||
HeaderFields header = decodeHeader(headerBytes);
|
||||
|
||||
if(header.size > LimitsCfg.maxFrameSize)
|
||||
MAKE_ERROR("Net2::AsyncSocket: frame too large");
|
||||
|
||||
std::vector<std::byte> chunk(header.size);
|
||||
if(header.size)
|
||||
co_await readExact(chunk.data(), chunk.size());
|
||||
|
||||
if(header.streamId != 0) {
|
||||
if(Fragments.size() >= LimitsCfg.maxOpenStreams && !Fragments.contains(header.streamId))
|
||||
MAKE_ERROR("Net2::AsyncSocket: too many open streams");
|
||||
|
||||
FragmentState &state = Fragments[header.streamId];
|
||||
if(state.data.empty()) {
|
||||
state.type = header.type;
|
||||
state.priority = header.priority;
|
||||
}
|
||||
|
||||
if(state.data.size() + chunk.size() > LimitsCfg.maxMessageSize)
|
||||
MAKE_ERROR("Net2::AsyncSocket: reassembled message too large");
|
||||
|
||||
state.data.insert(state.data.end(), chunk.begin(), chunk.end());
|
||||
|
||||
if(!hasFlag(header.flags, FrameFlags::HasMore)) {
|
||||
IncomingMessage msg{state.type, state.priority, std::move(state.data)};
|
||||
Fragments.erase(header.streamId);
|
||||
co_return msg;
|
||||
}
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
if(hasFlag(header.flags, FrameFlags::HasMore))
|
||||
MAKE_ERROR("Net2::AsyncSocket: stream id missing for fragmented frame");
|
||||
|
||||
IncomingMessage msg{header.type, header.priority, std::move(chunk)};
|
||||
co_return msg;
|
||||
}
|
||||
}
|
||||
|
||||
coro<> AsyncSocket::readLoop(std::function<coro<>(IncomingMessage&&)> onMessage) {
|
||||
while(isAlive()) {
|
||||
IncomingMessage msg = co_await readMessage();
|
||||
co_await onMessage(std::move(msg));
|
||||
}
|
||||
}
|
||||
|
||||
void AsyncSocket::closeRead() {
|
||||
if(Socket.is_open() && !Context->readClosed.exchange(true)) {
|
||||
try { Socket.shutdown(boost::asio::socket_base::shutdown_receive); } catch(...) {}
|
||||
}
|
||||
}
|
||||
|
||||
void AsyncSocket::close() {
|
||||
if(Context)
|
||||
Context->needShutdown.store(true);
|
||||
if(Socket.is_open())
|
||||
try { Socket.close(); } catch(...) {}
|
||||
}
|
||||
|
||||
bool AsyncSocket::isAlive() const {
|
||||
return Context && !Context->needShutdown.load() && !Context->senderStopped.load() && Socket.is_open();
|
||||
}
|
||||
|
||||
std::string AsyncSocket::getError() const {
|
||||
boost::lock_guard lock(Context->errorMtx);
|
||||
return Context->error;
|
||||
}
|
||||
|
||||
coro<> AsyncSocket::sendLoop() {
|
||||
try {
|
||||
while(!Context->needShutdown.load()) {
|
||||
OutgoingMessage msg;
|
||||
{
|
||||
boost::unique_lock lock(Outgoing.mtx);
|
||||
if(Outgoing.empty()) {
|
||||
Outgoing.waiting = true;
|
||||
auto coroutine = Outgoing.semaphore.async_wait();
|
||||
lock.unlock();
|
||||
try { co_await std::move(coroutine); } catch(...) {}
|
||||
continue;
|
||||
}
|
||||
|
||||
if(!popNext(msg))
|
||||
continue;
|
||||
}
|
||||
|
||||
co_await sendMessage(std::move(msg));
|
||||
}
|
||||
} catch(const std::exception &exc) {
|
||||
setError(exc.what());
|
||||
} catch(...) {
|
||||
setError("Net2::AsyncSocket: send loop stopped");
|
||||
}
|
||||
|
||||
Context->senderStopped.store(true);
|
||||
}
|
||||
|
||||
coro<> AsyncSocket::sendMessage(OutgoingMessage &&msg) {
|
||||
const size_t total = msg.payload.size();
|
||||
if(total <= LimitsCfg.maxFrameSize) {
|
||||
co_await sendFrame(msg.type, msg.priority, FrameFlags::None, 0, msg.payload);
|
||||
co_return;
|
||||
}
|
||||
|
||||
if(!msg.allowFragment) {
|
||||
setError("Net2::AsyncSocket: message requires fragmentation");
|
||||
close();
|
||||
co_return;
|
||||
}
|
||||
|
||||
uint32_t streamId = NextStreamId++;
|
||||
if(streamId == 0)
|
||||
streamId = NextStreamId++;
|
||||
|
||||
size_t offset = 0;
|
||||
while(offset < total) {
|
||||
const size_t chunk = std::min(LimitsCfg.maxFrameSize, total - offset);
|
||||
const bool more = (offset + chunk) < total;
|
||||
FrameFlags flags = more ? FrameFlags::HasMore : FrameFlags::None;
|
||||
std::span<const std::byte> view(msg.payload.data() + offset, chunk);
|
||||
co_await sendFrame(msg.type, msg.priority, flags, streamId, view);
|
||||
offset += chunk;
|
||||
}
|
||||
}
|
||||
|
||||
coro<> AsyncSocket::sendFrame(uint16_t type, Priority priority, FrameFlags flags, uint32_t streamId,
|
||||
std::span<const std::byte> payload) {
|
||||
HeaderFields header{
|
||||
.size = static_cast<uint32_t>(payload.size()),
|
||||
.type = type,
|
||||
.priority = priority,
|
||||
.flags = flags,
|
||||
.streamId = streamId
|
||||
};
|
||||
auto headerBytes = encodeHeader(header);
|
||||
std::array<asio::const_buffer, 2> buffers{
|
||||
asio::buffer(headerBytes),
|
||||
asio::buffer(payload.data(), payload.size())
|
||||
};
|
||||
if(payload.empty())
|
||||
co_await asio::async_write(Socket, asio::buffer(headerBytes));
|
||||
else
|
||||
co_await asio::async_write(Socket, buffers);
|
||||
}
|
||||
|
||||
coro<> AsyncSocket::readExact(std::byte *data, size_t size) {
|
||||
if(size == 0)
|
||||
co_return;
|
||||
co_await asio::async_read(Socket, asio::buffer(data, size));
|
||||
}
|
||||
|
||||
bool AsyncSocket::popNext(OutgoingMessage &out) {
|
||||
static constexpr int kWeights[4] = {8, 4, 2, 1};
|
||||
|
||||
for(int attempt = 0; attempt < 4; ++attempt) {
|
||||
const uint8_t idx = static_cast<uint8_t>((Outgoing.nextIndex + attempt) % 4);
|
||||
auto &queue = Outgoing.queues[idx];
|
||||
if(queue.empty())
|
||||
continue;
|
||||
|
||||
if(Outgoing.credits[idx] <= 0)
|
||||
Outgoing.credits[idx] = kWeights[idx];
|
||||
|
||||
if(Outgoing.credits[idx] <= 0)
|
||||
continue;
|
||||
|
||||
out = std::move(queue.front());
|
||||
queue.pop_front();
|
||||
Outgoing.credits[idx]--;
|
||||
Outgoing.nextIndex = idx;
|
||||
|
||||
const size_t msgSize = out.payload.size();
|
||||
Outgoing.bytesInQueue -= msgSize;
|
||||
if(idx == static_cast<uint8_t>(Priority::Low))
|
||||
Outgoing.bytesInLow -= msgSize;
|
||||
return true;
|
||||
}
|
||||
|
||||
for(int i = 0; i < 4; ++i)
|
||||
Outgoing.credits[i] = kWeights[i];
|
||||
return false;
|
||||
}
|
||||
|
||||
void AsyncSocket::dropLow(size_t needBytes) {
|
||||
const size_t lowIndex = static_cast<size_t>(Priority::Low);
|
||||
while(Outgoing.bytesInQueue + needBytes > LimitsCfg.maxQueueBytes && !Outgoing.queues[lowIndex].empty()) {
|
||||
const size_t size = Outgoing.queues[lowIndex].front().payload.size();
|
||||
Outgoing.bytesInQueue -= size;
|
||||
Outgoing.bytesInLow -= size;
|
||||
Outgoing.queues[lowIndex].pop_front();
|
||||
}
|
||||
}
|
||||
|
||||
void AsyncSocket::setError(const std::string &msg) {
|
||||
if(!Context)
|
||||
return;
|
||||
boost::lock_guard lock(Context->errorMtx);
|
||||
Context->error = msg;
|
||||
}
|
||||
|
||||
coro<tcp::socket> asyncConnectTo(const std::string &address,
|
||||
std::function<void(const std::string&)> onProgress) {
|
||||
std::string progress;
|
||||
auto addLog = [&](const std::string &msg) {
|
||||
progress += '\n';
|
||||
progress += msg;
|
||||
if(onProgress)
|
||||
onProgress('\n' + msg);
|
||||
};
|
||||
|
||||
auto ioc = co_await asio::this_coro::executor;
|
||||
|
||||
addLog("Parsing address " + address);
|
||||
auto re = Str::match(address, "((?:\\[[\\d\\w:]+\\])|(?:[\\d\\.]+))(?:\\:(\\d+))?");
|
||||
|
||||
std::vector<std::tuple<tcp::endpoint, std::string>> eps;
|
||||
|
||||
if(!re) {
|
||||
re = Str::match(address, "([-_\\.\\w\\d]+)(?:\\:(\\d+))?");
|
||||
if(!re)
|
||||
MAKE_ERROR("Failed to parse address");
|
||||
|
||||
tcp::resolver resv{ioc};
|
||||
tcp::resolver::results_type result;
|
||||
|
||||
addLog("Resolving name...");
|
||||
result = co_await resv.async_resolve(*re->at(1), re->at(2) ? *re->at(2) : "7890");
|
||||
|
||||
addLog("Got " + std::to_string(result.size()) + " endpoints");
|
||||
for(auto iter : result) {
|
||||
std::string addr = iter.endpoint().address().to_string() + ':' + std::to_string(iter.endpoint().port());
|
||||
std::string hostname = iter.host_name();
|
||||
if(hostname == addr)
|
||||
addLog("ep: " + addr);
|
||||
else
|
||||
addLog("ep: " + hostname + " (" + addr + ')');
|
||||
|
||||
eps.emplace_back(iter.endpoint(), iter.host_name());
|
||||
}
|
||||
} else {
|
||||
eps.emplace_back(tcp::endpoint{asio::ip::make_address(*re->at(1)),
|
||||
static_cast<uint16_t>(re->at(2) ? Str::toVal<int>(*re->at(2)) : 7890)},
|
||||
*re->at(1));
|
||||
}
|
||||
|
||||
for(auto [ep, hostname] : eps) {
|
||||
addLog("Connecting to " + hostname + " (" + ep.address().to_string() + ':'
|
||||
+ std::to_string(ep.port()) + ")");
|
||||
try {
|
||||
tcp::socket sock{ioc};
|
||||
co_await sock.async_connect(ep);
|
||||
addLog("Connected");
|
||||
co_return sock;
|
||||
} catch(const std::exception &exc) {
|
||||
addLog(std::string("Connect failed: ") + exc.what());
|
||||
}
|
||||
}
|
||||
|
||||
MAKE_ERROR("Unable to connect to server");
|
||||
}
|
||||
|
||||
} // namespace LV::Net2
|
||||
227
Src/Common/Net2.hpp
Normal file
227
Src/Common/Net2.hpp
Normal file
@@ -0,0 +1,227 @@
|
||||
#pragma once
|
||||
|
||||
#include "Async.hpp"
|
||||
#include "TOSLib.hpp"
|
||||
|
||||
#include <boost/asio.hpp>
|
||||
#include <boost/thread.hpp>
|
||||
#include <array>
|
||||
#include <bit>
|
||||
#include <atomic>
|
||||
#include <cassert>
|
||||
#include <chrono>
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
#include <cstring>
|
||||
#include <deque>
|
||||
#include <memory>
|
||||
#include <optional>
|
||||
#include <span>
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
#include <type_traits>
|
||||
#include <unordered_map>
|
||||
#include <vector>
|
||||
|
||||
namespace LV::Net2 {
|
||||
|
||||
namespace detail {
|
||||
|
||||
constexpr bool kLittleEndian = (std::endian::native == std::endian::little);
|
||||
|
||||
template<typename T>
|
||||
requires std::is_integral_v<T>
|
||||
inline T toNetwork(T value) {
|
||||
if constexpr (kLittleEndian && sizeof(T) > 1)
|
||||
return std::byteswap(value);
|
||||
return value;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
requires std::is_floating_point_v<T>
|
||||
inline T toNetwork(T value) {
|
||||
using U = std::conditional_t<sizeof(T) == 4, uint32_t, uint64_t>;
|
||||
U u = std::bit_cast<U>(value);
|
||||
u = toNetwork(u);
|
||||
return std::bit_cast<T>(u);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
inline T fromNetwork(T value) {
|
||||
return toNetwork(value);
|
||||
}
|
||||
|
||||
} // namespace detail
|
||||
|
||||
enum class Priority : uint8_t {
|
||||
Realtime = 0,
|
||||
High = 1,
|
||||
Normal = 2,
|
||||
Low = 3
|
||||
};
|
||||
|
||||
enum class FrameFlags : uint8_t {
|
||||
None = 0,
|
||||
HasMore = 1
|
||||
};
|
||||
|
||||
inline FrameFlags operator|(FrameFlags a, FrameFlags b) {
|
||||
return static_cast<FrameFlags>(static_cast<uint8_t>(a) | static_cast<uint8_t>(b));
|
||||
}
|
||||
|
||||
inline bool hasFlag(FrameFlags value, FrameFlags flag) {
|
||||
return (static_cast<uint8_t>(value) & static_cast<uint8_t>(flag)) != 0;
|
||||
}
|
||||
|
||||
struct Limits {
|
||||
size_t maxFrameSize = 1 << 24;
|
||||
size_t maxMessageSize = 1 << 26;
|
||||
size_t maxQueueBytes = 1 << 27;
|
||||
size_t maxLowPriorityBytes = 1 << 26;
|
||||
size_t maxOpenStreams = 64;
|
||||
};
|
||||
|
||||
struct OutgoingMessage {
|
||||
uint16_t type = 0;
|
||||
Priority priority = Priority::Normal;
|
||||
bool dropIfOverloaded = false;
|
||||
bool allowFragment = true;
|
||||
std::vector<std::byte> payload;
|
||||
};
|
||||
|
||||
struct IncomingMessage {
|
||||
uint16_t type = 0;
|
||||
Priority priority = Priority::Normal;
|
||||
std::vector<std::byte> payload;
|
||||
};
|
||||
|
||||
class PacketWriter {
|
||||
public:
|
||||
PacketWriter& writeBytes(std::span<const std::byte> data);
|
||||
|
||||
template<typename T>
|
||||
requires (std::is_integral_v<T> || std::is_floating_point_v<T>)
|
||||
PacketWriter& write(T value) {
|
||||
T net = detail::toNetwork(value);
|
||||
std::array<std::byte, sizeof(T)> bytes{};
|
||||
std::memcpy(bytes.data(), &net, sizeof(T));
|
||||
Buffer.insert(Buffer.end(), bytes.begin(), bytes.end());
|
||||
return *this;
|
||||
}
|
||||
|
||||
PacketWriter& writeString(std::string_view str);
|
||||
|
||||
const std::vector<std::byte>& data() const { return Buffer; }
|
||||
std::vector<std::byte> release();
|
||||
void clear();
|
||||
|
||||
private:
|
||||
std::vector<std::byte> Buffer;
|
||||
};
|
||||
|
||||
class PacketReader {
|
||||
public:
|
||||
explicit PacketReader(std::span<const std::byte> data);
|
||||
|
||||
template<typename T>
|
||||
requires (std::is_integral_v<T> || std::is_floating_point_v<T>)
|
||||
T read() {
|
||||
require(sizeof(T));
|
||||
T net{};
|
||||
std::memcpy(&net, Data.data() + Pos, sizeof(T));
|
||||
Pos += sizeof(T);
|
||||
return detail::fromNetwork(net);
|
||||
}
|
||||
|
||||
void readBytes(std::span<std::byte> out);
|
||||
std::string readString();
|
||||
bool empty() const { return Pos >= Data.size(); }
|
||||
size_t remaining() const { return Data.size() - Pos; }
|
||||
|
||||
private:
|
||||
void require(size_t size);
|
||||
|
||||
size_t Pos = 0;
|
||||
std::span<const std::byte> Data;
|
||||
};
|
||||
|
||||
class SocketServer : public AsyncObject {
|
||||
public:
|
||||
SocketServer(asio::io_context &ioc, std::function<coro<>(tcp::socket)> &&onConnect, uint16_t port = 0);
|
||||
bool isStopped() const;
|
||||
uint16_t getPort() const;
|
||||
|
||||
private:
|
||||
coro<void> run(std::function<coro<>(tcp::socket)> onConnect);
|
||||
|
||||
tcp::acceptor Acceptor;
|
||||
};
|
||||
|
||||
class AsyncSocket : public AsyncObject {
|
||||
public:
|
||||
static constexpr size_t kHeaderSize = 12;
|
||||
|
||||
AsyncSocket(asio::io_context &ioc, tcp::socket &&socket, Limits limits = {});
|
||||
~AsyncSocket();
|
||||
|
||||
void enqueue(OutgoingMessage &&msg);
|
||||
coro<IncomingMessage> readMessage();
|
||||
coro<> readLoop(std::function<coro<>(IncomingMessage&&)> onMessage);
|
||||
|
||||
void closeRead();
|
||||
void close();
|
||||
bool isAlive() const;
|
||||
std::string getError() const;
|
||||
|
||||
private:
|
||||
struct FragmentState {
|
||||
uint16_t type = 0;
|
||||
Priority priority = Priority::Normal;
|
||||
std::vector<std::byte> data;
|
||||
};
|
||||
|
||||
struct AsyncContext {
|
||||
std::atomic_bool needShutdown{false};
|
||||
std::atomic_bool senderStopped{false};
|
||||
std::atomic_bool readClosed{false};
|
||||
boost::mutex errorMtx;
|
||||
std::string error;
|
||||
};
|
||||
|
||||
struct SendQueue {
|
||||
boost::mutex mtx;
|
||||
bool waiting = false;
|
||||
asio::steady_timer semaphore;
|
||||
std::deque<OutgoingMessage> queues[4];
|
||||
size_t bytesInQueue = 0;
|
||||
size_t bytesInLow = 0;
|
||||
uint8_t nextIndex = 0;
|
||||
int credits[4] = {8, 4, 2, 1};
|
||||
|
||||
explicit SendQueue(asio::io_context &ioc);
|
||||
bool empty() const;
|
||||
};
|
||||
|
||||
coro<> sendLoop();
|
||||
coro<> sendMessage(OutgoingMessage &&msg);
|
||||
coro<> sendFrame(uint16_t type, Priority priority, FrameFlags flags, uint32_t streamId,
|
||||
std::span<const std::byte> payload);
|
||||
|
||||
coro<> readExact(std::byte *data, size_t size);
|
||||
|
||||
bool popNext(OutgoingMessage &out);
|
||||
void dropLow(size_t needBytes);
|
||||
void setError(const std::string &msg);
|
||||
|
||||
Limits LimitsCfg;
|
||||
tcp::socket Socket;
|
||||
SendQueue Outgoing;
|
||||
std::shared_ptr<AsyncContext> Context;
|
||||
std::unordered_map<uint32_t, FragmentState> Fragments;
|
||||
uint32_t NextStreamId = 1;
|
||||
};
|
||||
|
||||
coro<tcp::socket> asyncConnectTo(const std::string &address,
|
||||
std::function<void(const std::string&)> onProgress = nullptr);
|
||||
|
||||
} // namespace LV::Net2
|
||||
@@ -92,6 +92,7 @@ enum struct ToClient : uint8_t {
|
||||
AssetsInitSend, // Начало отправки запрошенного клиентом ресурса
|
||||
AssetsNextSend, // Продолжение отправки ресурса
|
||||
|
||||
DefinitionsFull, // Полная информация о профилях контента
|
||||
DefinitionsUpdate, // Обновление и потеря профилей контента (воксели, ноды, сущности, миры, ...)
|
||||
|
||||
ChunkVoxels, // Обновление вокселей чанка
|
||||
|
||||
1845
Src/Common/TexturePipelineProgram.cpp
Normal file
1845
Src/Common/TexturePipelineProgram.cpp
Normal file
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
@@ -34,24 +34,6 @@ using PlayerId_t = ResourceId;
|
||||
using DefGeneratorId_t = ResourceId;
|
||||
|
||||
|
||||
/*
|
||||
Сервер загружает информацию о локальных текстурах
|
||||
Пересмотр списка текстур?
|
||||
Динамичные текстуры?
|
||||
|
||||
*/
|
||||
|
||||
struct ResourceFile {
|
||||
using Hash_t = sha2::sha256_hash; // boost::uuids::detail::sha1::digest_type;
|
||||
|
||||
Hash_t Hash;
|
||||
std::vector<std::byte> Data;
|
||||
|
||||
void calcHash() {
|
||||
Hash = sha2::sha256((const uint8_t*) Data.data(), Data.size());
|
||||
}
|
||||
};
|
||||
|
||||
struct ServerTime {
|
||||
uint32_t Seconds : 24, Sub : 8;
|
||||
};
|
||||
@@ -236,6 +218,7 @@ public:
|
||||
}
|
||||
|
||||
DefEntityId getDefId() const { return DefId; }
|
||||
void setDefId(DefEntityId defId) { DefId = defId; }
|
||||
};
|
||||
|
||||
template<typename Vec>
|
||||
|
||||
129
Src/Server/AssetsManager.hpp
Normal file
129
Src/Server/AssetsManager.hpp
Normal file
@@ -0,0 +1,129 @@
|
||||
#pragma once
|
||||
|
||||
#include "Common/Abstract.hpp"
|
||||
#include "Common/IdProvider.hpp"
|
||||
#include "Common/AssetsPreloader.hpp"
|
||||
#include <unordered_map>
|
||||
|
||||
namespace LV::Server {
|
||||
|
||||
class AssetsManager : public IdProvider<EnumAssets>, protected AssetsPreloader {
|
||||
public:
|
||||
using BindHashHeaderInfo = AssetsManager::BindHashHeaderInfo;
|
||||
|
||||
struct Out_checkAndPrepareResourcesUpdate : public AssetsPreloader::Out_checkAndPrepareResourcesUpdate {
|
||||
Out_checkAndPrepareResourcesUpdate(AssetsPreloader::Out_checkAndPrepareResourcesUpdate&& obj)
|
||||
: AssetsPreloader::Out_checkAndPrepareResourcesUpdate(std::move(obj))
|
||||
{}
|
||||
|
||||
std::unordered_map<ResourceFile::Hash_t, std::u8string> NewHeadless;
|
||||
};
|
||||
|
||||
Out_checkAndPrepareResourcesUpdate checkAndPrepareResourcesUpdate(
|
||||
const AssetsRegister& instances,
|
||||
ReloadStatus* status = nullptr
|
||||
) {
|
||||
std::unordered_map<ResourceFile::Hash_t, std::u8string> newHeadless;
|
||||
|
||||
Out_checkAndPrepareResourcesUpdate result = AssetsPreloader::checkAndPrepareResourcesUpdate(
|
||||
instances,
|
||||
[&](EnumAssets type, std::string_view domain, std::string_view key) { return getId(type, domain, key); },
|
||||
[&](std::u8string&& resource, ResourceFile::Hash_t hash, fs::path resPath) { newHeadless.emplace(hash, std::move(resource)); },
|
||||
status
|
||||
);
|
||||
|
||||
result.NewHeadless = std::move(newHeadless);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
struct Out_applyResourcesUpdate : public AssetsPreloader::Out_applyResourcesUpdate {
|
||||
Out_applyResourcesUpdate(AssetsPreloader::Out_applyResourcesUpdate&& obj)
|
||||
: AssetsPreloader::Out_applyResourcesUpdate(std::move(obj))
|
||||
{}
|
||||
};
|
||||
|
||||
Out_applyResourcesUpdate applyResourcesUpdate(Out_checkAndPrepareResourcesUpdate& orr) {
|
||||
Out_applyResourcesUpdate result = AssetsPreloader::applyResourcesUpdate(orr);
|
||||
|
||||
{
|
||||
static TOS::Logger LOG = "Server>AssetsManager";
|
||||
|
||||
for(size_t type = 0; type < static_cast<size_t>(EnumAssets::MAX_ENUM); ++type) {
|
||||
if(result.NewOrUpdates[type].empty())
|
||||
continue;
|
||||
|
||||
EnumAssets typeEnum = static_cast<EnumAssets>(type);
|
||||
const char* typeName = ::EnumAssetsToDirectory(typeEnum);
|
||||
|
||||
for(const auto& bind : result.NewOrUpdates[type]) {
|
||||
auto dk = getDK(typeEnum, bind.Id);
|
||||
if(!dk)
|
||||
continue;
|
||||
|
||||
LOG.debug()
|
||||
<< typeName << ": "
|
||||
<< dk->Domain << '+' << dk->Key << " -> " << bind.Id;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for(auto& [hash, data] : orr.NewHeadless) {
|
||||
Resources.emplace(hash, ResourceHashData{0, std::make_shared<std::u8string>(std::move(data))});
|
||||
}
|
||||
|
||||
for(auto& [hash, pathes] : orr.HashToPathNew) {
|
||||
auto iter = Resources.find(hash);
|
||||
assert(iter != Resources.end());
|
||||
iter->second.RefCount += pathes.size();
|
||||
}
|
||||
|
||||
for(auto& [hash, pathes] : orr.HashToPathLost) {
|
||||
auto iter = Resources.find(hash);
|
||||
assert(iter != Resources.end());
|
||||
iter->second.RefCount -= pathes.size();
|
||||
|
||||
if(iter->second.RefCount == 0)
|
||||
Resources.erase(iter);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
std::vector<std::tuple<ResourceFile::Hash_t, std::shared_ptr<const std::u8string>>>
|
||||
getResources(const std::vector<ResourceFile::Hash_t>& hashes) const
|
||||
{
|
||||
std::vector<std::tuple<ResourceFile::Hash_t, std::shared_ptr<const std::u8string>>> result;
|
||||
result.reserve(hashes.size());
|
||||
|
||||
for(const auto& hash : hashes) {
|
||||
auto iter = Resources.find(hash);
|
||||
if(iter == Resources.end())
|
||||
continue;
|
||||
|
||||
result.emplace_back(hash, iter->second.Data);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
std::array<
|
||||
std::vector<BindHashHeaderInfo>,
|
||||
static_cast<size_t>(EnumAssets::MAX_ENUM)
|
||||
> collectHashBindings() const {
|
||||
return AssetsPreloader::collectHashBindings();
|
||||
}
|
||||
|
||||
private:
|
||||
struct ResourceHashData {
|
||||
size_t RefCount;
|
||||
std::shared_ptr<std::u8string> Data;
|
||||
};
|
||||
|
||||
std::unordered_map<
|
||||
ResourceFile::Hash_t,
|
||||
ResourceHashData
|
||||
> Resources;
|
||||
};
|
||||
|
||||
}
|
||||
@@ -1,25 +1,35 @@
|
||||
#include "ContentManager.hpp"
|
||||
#include "Common/Abstract.hpp"
|
||||
#include <algorithm>
|
||||
#include <optional>
|
||||
#include <utility>
|
||||
|
||||
namespace LV::Server {
|
||||
|
||||
ContentManager::ContentManager(AssetsPreloader& am)
|
||||
ContentManager::ContentManager(AssetsManager& am)
|
||||
: AM(am)
|
||||
{
|
||||
std::fill(std::begin(NextId), std::end(NextId), 1);
|
||||
}
|
||||
|
||||
ContentManager::~ContentManager() = default;
|
||||
|
||||
void ContentManager::registerBase_Node(ResourceId id, const std::string& domain, const std::string& key, const sol::table& profile) {
|
||||
std::optional<DefNode>& node = getEntry_Node(id);
|
||||
if(!node)
|
||||
node.emplace();
|
||||
std::optional<DefNode_Base>* basePtr;
|
||||
|
||||
DefNode& def = *node;
|
||||
def.Domain = domain;
|
||||
def.Key = key;
|
||||
{
|
||||
size_t entryIndex = id / TableEntry<DefNode_Base>::ChunkSize;
|
||||
size_t entryId = id % TableEntry<DefNode_Base>::ChunkSize;
|
||||
|
||||
size_t need = entryIndex+1-Profiles_Base_Node.size();
|
||||
for(size_t iter = 0; iter < need; iter++) {
|
||||
Profiles_Base_Node.emplace_back(std::make_unique<TableEntry<DefNode_Base>>());
|
||||
}
|
||||
|
||||
basePtr = &Profiles_Base_Node[entryIndex]->Entries[entryId];
|
||||
*basePtr = DefNode_Base();
|
||||
}
|
||||
|
||||
DefNode_Base& def = **basePtr;
|
||||
|
||||
{
|
||||
std::optional<std::variant<std::string, sol::table>> parent = profile.get<std::optional<std::variant<std::string, sol::table>>>("parent");
|
||||
@@ -93,7 +103,7 @@ void ContentManager::registerBase_Entity(ResourceId id, const std::string& domai
|
||||
void ContentManager::registerBase(EnumDefContent type, const std::string& domain, const std::string& key, const sol::table& profile)
|
||||
{
|
||||
ResourceId id = getId(type, domain, key);
|
||||
ProfileChanges[(int) type].push_back(id);
|
||||
ProfileChanges[static_cast<size_t>(type)].push_back(id);
|
||||
|
||||
if(type == EnumDefContent::Node)
|
||||
registerBase_Node(id, domain, key, profile);
|
||||
@@ -123,56 +133,203 @@ void ContentManager::unRegisterModifier(EnumDefContent type, const std::string&
|
||||
ProfileChanges[(int) type].push_back(id);
|
||||
}
|
||||
|
||||
void ContentManager::markAllProfilesDirty(EnumDefContent type) {
|
||||
const auto &table = ContentKeyToId[(int) type];
|
||||
for(const auto& domainPair : table) {
|
||||
for(const auto& keyPair : domainPair.second) {
|
||||
ProfileChanges[(int) type].push_back(keyPair.second);
|
||||
}
|
||||
}
|
||||
}
|
||||
// void ContentManager::markAllProfilesDirty(EnumDefContent type) {
|
||||
// const auto &table = this->idToDK()[(int) type];
|
||||
// size_t counter = 0;
|
||||
// for(const auto& [domain, key] : table) {
|
||||
// ProfileChanges[static_cast<size_t>(type)].push_back(counter++);
|
||||
// }
|
||||
// }
|
||||
|
||||
std::vector<ResourceId> ContentManager::collectProfileIds(EnumDefContent type) const {
|
||||
std::vector<ResourceId> ids;
|
||||
const auto &table = ContentKeyToId[(int) type];
|
||||
template<class type, class modType>
|
||||
void ContentManager::buildEndProfilesByType(auto& profiles, auto enumType, auto& base, auto& keys, auto& result, auto& modsTable) {
|
||||
// Расширяем таблицу итоговых профилей до нужного количества
|
||||
if(!keys.empty()) {
|
||||
size_t need = keys.back() / TableEntry<type>::ChunkSize;
|
||||
if(need >= profiles.size()) {
|
||||
profiles.reserve(need);
|
||||
|
||||
for(const auto& domainPair : table) {
|
||||
for(const auto& keyPair : domainPair.second) {
|
||||
ids.push_back(keyPair.second);
|
||||
for(size_t iter = 0; iter <= need-profiles.size(); ++iter)
|
||||
profiles.emplace_back(std::make_unique<TableEntry<type>>());
|
||||
}
|
||||
}
|
||||
|
||||
std::sort(ids.begin(), ids.end());
|
||||
auto last = std::unique(ids.begin(), ids.end());
|
||||
ids.erase(last, ids.end());
|
||||
return ids;
|
||||
TOS::Logger("CM").debug() << "type: " << static_cast<size_t>(enumType);
|
||||
|
||||
// Пересчитываем профили
|
||||
for(size_t id : keys) {
|
||||
size_t entryIndex = id / TableEntry<type>::ChunkSize;
|
||||
size_t subIndex = id % TableEntry<type>::ChunkSize;
|
||||
|
||||
if(
|
||||
entryIndex >= base.size()
|
||||
|| !base[entryIndex]->Entries[subIndex]
|
||||
) {
|
||||
// Базовый профиль не существует
|
||||
profiles[entryIndex]->Entries[subIndex] = std::nullopt;
|
||||
// Уведомляем о потере профиля
|
||||
result.LostProfiles[static_cast<size_t>(enumType)].push_back(id);
|
||||
} else {
|
||||
// Собираем конечный профиль
|
||||
std::vector<std::tuple<std::string, modType>> mods_default, *mods = &mods_default;
|
||||
auto iter = modsTable.find(id);
|
||||
if(iter != modsTable.end())
|
||||
mods = &iter->second;
|
||||
|
||||
std::optional<BindDomainKeyInfo> dk = getDK(enumType, id);
|
||||
assert(dk);
|
||||
TOS::Logger("CM").debug() << "\t" << dk->Domain << ":" << dk->Key << " -> " << id;
|
||||
profiles[entryIndex]->Entries[subIndex] = base[entryIndex]->Entries[subIndex]->compile(AM, *this, dk->Domain, dk->Key, *mods);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
ContentManager::Out_buildEndProfiles ContentManager::buildEndProfiles() {
|
||||
Out_buildEndProfiles result;
|
||||
|
||||
for(int type = 0; type < (int) EnumDefContent::MAX_ENUM; type++) {
|
||||
std::shared_lock lock(Profiles_Mtx[type]);
|
||||
auto& keys = ProfileChanges[type];
|
||||
std::sort(keys.begin(), keys.end());
|
||||
auto iterErase = std::unique(keys.begin(), keys.end());
|
||||
keys.erase(iterErase, keys.end());
|
||||
}
|
||||
|
||||
for(ResourceId id : ProfileChanges[(int) EnumDefContent::Node]) {
|
||||
std::optional<DefNode>& node = getEntry_Node(id);
|
||||
if(!node) {
|
||||
continue;
|
||||
switch(type) {
|
||||
case 0: buildEndProfilesByType<DefVoxel, DefVoxel_Mod> (Profiles_Voxel, EnumDefContent::Voxel, Profiles_Base_Voxel, keys, result, Profiles_Mod_Voxel); break;
|
||||
case 1: buildEndProfilesByType<DefNode, DefNode_Mod> (Profiles_Node, EnumDefContent::Node, Profiles_Base_Node, keys, result, Profiles_Mod_Node); break;
|
||||
case 2: buildEndProfilesByType<DefWorld, DefWorld_Mod> (Profiles_World, EnumDefContent::World, Profiles_Base_World, keys, result, Profiles_Mod_World); break;
|
||||
case 3: buildEndProfilesByType<DefPortal, DefPortal_Mod> (Profiles_Portal, EnumDefContent::Portal, Profiles_Base_Portal, keys, result, Profiles_Mod_Portal); break;
|
||||
case 4: buildEndProfilesByType<DefEntity, DefEntity_Mod> (Profiles_Entity, EnumDefContent::Entity, Profiles_Base_Entity, keys, result, Profiles_Mod_Entity); break;
|
||||
case 5: buildEndProfilesByType<DefItem, DefItem_Mod> (Profiles_Item, EnumDefContent::Item, Profiles_Base_Item, keys, result, Profiles_Mod_Item); break;
|
||||
default: std::unreachable();
|
||||
}
|
||||
|
||||
auto [nodestateId, assetsModel, assetsTexture]
|
||||
= AM.getNodeDependency(node->Domain, node->Key);
|
||||
|
||||
node->NodestateId = nodestateId;
|
||||
node->ModelDeps = std::move(assetsModel);
|
||||
node->TextureDeps = std::move(assetsTexture);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
ContentManager::Out_getAllProfiles ContentManager::getAllProfiles() {
|
||||
Out_getAllProfiles result;
|
||||
|
||||
size_t counter;
|
||||
|
||||
{
|
||||
std::shared_lock lock(Profiles_Mtx[static_cast<size_t>(EnumDefContent::Voxel)]);
|
||||
result.ProfilesIds_Voxel.reserve(Profiles_Voxel.size()*TableEntry<DefVoxel>::ChunkSize);
|
||||
counter = 0;
|
||||
for(const auto& entry : Profiles_Voxel)
|
||||
for(const auto& item : entry->Entries) {
|
||||
size_t id = counter++;
|
||||
if(item)
|
||||
result.ProfilesIds_Voxel.push_back(id);
|
||||
}
|
||||
|
||||
result.ProfilesIds_Voxel.shrink_to_fit();
|
||||
result.Profiles_Voxel.reserve(result.ProfilesIds_Voxel.size());
|
||||
for(const auto& entry : Profiles_Voxel)
|
||||
for(const auto& item : entry->Entries)
|
||||
if(item)
|
||||
result.Profiles_Voxel.push_back(&item.value());
|
||||
}
|
||||
|
||||
{
|
||||
std::shared_lock lock(Profiles_Mtx[static_cast<size_t>(EnumDefContent::Node)]);
|
||||
result.ProfilesIds_Node.reserve(Profiles_Node.size()*TableEntry<DefNode>::ChunkSize);
|
||||
counter = 0;
|
||||
for(const auto& entry : Profiles_Node)
|
||||
for(const auto& item : entry->Entries) {
|
||||
size_t id = counter++;
|
||||
if(item)
|
||||
result.ProfilesIds_Node.push_back(id);
|
||||
}
|
||||
|
||||
result.ProfilesIds_Node.shrink_to_fit();
|
||||
result.Profiles_Node.reserve(result.ProfilesIds_Node.size());
|
||||
for(const auto& entry : Profiles_Node)
|
||||
for(const auto& item : entry->Entries)
|
||||
if(item)
|
||||
result.Profiles_Node.push_back(&item.value());
|
||||
}
|
||||
|
||||
{
|
||||
std::shared_lock lock(Profiles_Mtx[static_cast<size_t>(EnumDefContent::World)]);
|
||||
result.ProfilesIds_World.reserve(Profiles_World.size()*TableEntry<DefWorld>::ChunkSize);
|
||||
counter = 0;
|
||||
for(const auto& entry : Profiles_World)
|
||||
for(const auto& item : entry->Entries) {
|
||||
size_t id = counter++;
|
||||
if(item)
|
||||
result.ProfilesIds_World.push_back(id);
|
||||
}
|
||||
|
||||
result.ProfilesIds_World.shrink_to_fit();
|
||||
result.Profiles_World.reserve(result.ProfilesIds_World.size());
|
||||
for(const auto& entry : Profiles_World)
|
||||
for(const auto& item : entry->Entries)
|
||||
if(item)
|
||||
result.Profiles_World.push_back(&item.value());
|
||||
}
|
||||
|
||||
{
|
||||
std::shared_lock lock(Profiles_Mtx[static_cast<size_t>(EnumDefContent::Portal)]);
|
||||
result.ProfilesIds_Portal.reserve(Profiles_Portal.size()*TableEntry<DefPortal>::ChunkSize);
|
||||
counter = 0;
|
||||
for(const auto& entry : Profiles_Portal)
|
||||
for(const auto& item : entry->Entries) {
|
||||
size_t id = counter++;
|
||||
if(item)
|
||||
result.ProfilesIds_Portal.push_back(id);
|
||||
}
|
||||
|
||||
result.ProfilesIds_Portal.shrink_to_fit();
|
||||
result.Profiles_Portal.reserve(result.ProfilesIds_Portal.size());
|
||||
for(const auto& entry : Profiles_Portal)
|
||||
for(const auto& item : entry->Entries)
|
||||
if(item)
|
||||
result.Profiles_Portal.push_back(&item.value());
|
||||
}
|
||||
|
||||
{
|
||||
std::shared_lock lock(Profiles_Mtx[static_cast<size_t>(EnumDefContent::Entity)]);
|
||||
result.ProfilesIds_Entity.reserve(Profiles_Entity.size()*TableEntry<DefEntity>::ChunkSize);
|
||||
counter = 0;
|
||||
for(const auto& entry : Profiles_Entity)
|
||||
for(const auto& item : entry->Entries) {
|
||||
size_t id = counter++;
|
||||
if(item)
|
||||
result.ProfilesIds_Entity.push_back(id);
|
||||
}
|
||||
|
||||
result.ProfilesIds_Entity.shrink_to_fit();
|
||||
result.Profiles_Entity.reserve(result.ProfilesIds_Entity.size());
|
||||
for(const auto& entry : Profiles_Entity)
|
||||
for(const auto& item : entry->Entries)
|
||||
if(item)
|
||||
result.Profiles_Entity.push_back(&item.value());
|
||||
}
|
||||
|
||||
{
|
||||
std::shared_lock lock(Profiles_Mtx[static_cast<size_t>(EnumDefContent::Item)]);
|
||||
result.ProfilesIds_Item.reserve(Profiles_Item.size()*TableEntry<DefItem>::ChunkSize);
|
||||
counter = 0;
|
||||
for(const auto& entry : Profiles_Item)
|
||||
for(const auto& item : entry->Entries) {
|
||||
size_t id = counter++;
|
||||
if(item)
|
||||
result.ProfilesIds_Item.push_back(id);
|
||||
}
|
||||
|
||||
result.ProfilesIds_Item.shrink_to_fit();
|
||||
result.Profiles_Item.reserve(result.ProfilesIds_Item.size());
|
||||
for(const auto& entry : Profiles_Item)
|
||||
for(const auto& item : entry->Entries)
|
||||
if(item)
|
||||
result.Profiles_Item.push_back(&item.value());
|
||||
}
|
||||
|
||||
result.IdToDK = idToDK();
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,21 +1,64 @@
|
||||
#pragma once
|
||||
|
||||
#include "Common/Abstract.hpp"
|
||||
#include "Server/Abstract.hpp"
|
||||
#include "Common/AssetsPreloader.hpp"
|
||||
#include "AssetsManager.hpp"
|
||||
#include "Common/IdProvider.hpp"
|
||||
#include "Common/Net.hpp"
|
||||
#include "TOSLib.hpp"
|
||||
#include <array>
|
||||
#include <mutex>
|
||||
#include <sol/table.hpp>
|
||||
#include <string_view>
|
||||
#include <unordered_map>
|
||||
#include <unordered_set>
|
||||
|
||||
|
||||
namespace LV::Server {
|
||||
|
||||
struct DefVoxel_Base { };
|
||||
struct DefNode_Base { };
|
||||
struct DefWorld_Base { };
|
||||
struct DefPortal_Base { };
|
||||
struct DefEntity_Base { };
|
||||
struct DefItem_Base { };
|
||||
struct ResourceBase {
|
||||
std::string Domain, Key;
|
||||
};
|
||||
|
||||
class ContentManager;
|
||||
|
||||
struct DefVoxel : public ResourceBase {
|
||||
std::u8string dumpToClient() const {
|
||||
return {};
|
||||
}
|
||||
};
|
||||
|
||||
struct DefNode : public ResourceBase {
|
||||
AssetsNodestate NodestateId;
|
||||
|
||||
std::u8string dumpToClient() const {
|
||||
auto wr = TOS::ByteBuffer::Writer();
|
||||
wr << uint32_t(NodestateId);
|
||||
auto buff = wr.complite();
|
||||
return (std::u8string) std::u8string_view((const char8_t*) buff.data(), buff.size());
|
||||
}
|
||||
};
|
||||
struct DefWorld : public ResourceBase {
|
||||
std::u8string dumpToClient() const {
|
||||
return {};
|
||||
}
|
||||
};
|
||||
|
||||
struct DefPortal : public ResourceBase {
|
||||
std::u8string dumpToClient() const {
|
||||
return {};
|
||||
}
|
||||
};
|
||||
|
||||
struct DefEntity : public ResourceBase {
|
||||
std::u8string dumpToClient() const {
|
||||
return {};
|
||||
}
|
||||
};
|
||||
|
||||
struct DefItem : public ResourceBase {
|
||||
std::u8string dumpToClient() const {
|
||||
return {};
|
||||
}
|
||||
};
|
||||
|
||||
struct DefVoxel_Mod { };
|
||||
struct DefNode_Mod { };
|
||||
@@ -24,41 +67,370 @@ struct DefPortal_Mod { };
|
||||
struct DefEntity_Mod { };
|
||||
struct DefItem_Mod { };
|
||||
|
||||
struct ResourceBase {
|
||||
std::string Domain, Key;
|
||||
struct DefVoxel_Base {
|
||||
private:
|
||||
friend ContentManager;
|
||||
DefVoxel compile(AssetsManager& am, ContentManager& cm, const std::string_view domain, const std::string_view key, const std::vector<std::tuple<std::string, DefVoxel_Mod>>& mods) const {
|
||||
return DefVoxel();
|
||||
}
|
||||
};
|
||||
|
||||
struct DefVoxel : public ResourceBase { };
|
||||
struct DefNode : public ResourceBase {
|
||||
AssetsNodestate NodestateId;
|
||||
std::vector<AssetsModel> ModelDeps;
|
||||
std::vector<AssetsTexture> TextureDeps;
|
||||
struct DefNode_Base {
|
||||
private:
|
||||
friend ContentManager;
|
||||
DefNode compile(AssetsManager& am, ContentManager& cm, const std::string_view domain, const std::string_view key, const std::vector<std::tuple<std::string, DefNode_Mod>>& mods) const {
|
||||
DefNode profile;
|
||||
std::string jsonKey = std::string(key)+".json";
|
||||
profile.NodestateId = am.getId(EnumAssets::Nodestate, domain, jsonKey);
|
||||
TOS::Logger("Compile").info() << domain << ' ' << key << " -> " << profile.NodestateId;
|
||||
return profile;
|
||||
}
|
||||
};
|
||||
struct DefWorld : public ResourceBase { };
|
||||
struct DefPortal : public ResourceBase { };
|
||||
struct DefEntity : public ResourceBase { };
|
||||
struct DefItem : public ResourceBase { };
|
||||
|
||||
class ContentManager {
|
||||
struct DefWorld_Base {
|
||||
private:
|
||||
friend ContentManager;
|
||||
DefWorld compile(AssetsManager& am, ContentManager& cm, const std::string_view domain, const std::string_view key, const std::vector<std::tuple<std::string, DefWorld_Mod>>& mods) const {
|
||||
return DefWorld();
|
||||
}
|
||||
};
|
||||
|
||||
struct DefPortal_Base {
|
||||
private:
|
||||
friend ContentManager;
|
||||
DefPortal compile(AssetsManager& am, ContentManager& cm, const std::string_view domain, const std::string_view key, const std::vector<std::tuple<std::string, DefPortal_Mod>>& mods) const {
|
||||
return DefPortal();
|
||||
}
|
||||
};
|
||||
|
||||
struct DefEntity_Base {
|
||||
private:
|
||||
friend ContentManager;
|
||||
DefEntity compile(AssetsManager& am, ContentManager& cm, const std::string_view domain, const std::string_view key, const std::vector<std::tuple<std::string, DefEntity_Mod>>& mods) const {
|
||||
return DefEntity();
|
||||
}
|
||||
};
|
||||
|
||||
struct DefItem_Base {
|
||||
private:
|
||||
friend ContentManager;
|
||||
DefItem compile(AssetsManager& am, ContentManager& cm, const std::string_view domain, const std::string_view key, const std::vector<std::tuple<std::string, DefItem_Mod>>& mods) const {
|
||||
return DefItem();
|
||||
}
|
||||
};
|
||||
|
||||
/*
|
||||
DK to id
|
||||
id to profile
|
||||
*/
|
||||
|
||||
class ContentManager : public IdProvider<EnumDefContent> {
|
||||
public:
|
||||
class LRU {
|
||||
public:
|
||||
LRU(ContentManager& cm)
|
||||
: CM(&cm)
|
||||
{
|
||||
}
|
||||
|
||||
LRU(const LRU&) = default;
|
||||
LRU(LRU&&) = default;
|
||||
LRU& operator=(const LRU&) = default;
|
||||
LRU& operator=(LRU&&) = default;
|
||||
|
||||
ResourceId getId(EnumDefContent type, const std::string_view domain, const std::string_view key) {
|
||||
auto iter = DKToId[static_cast<size_t>(type)].find(BindDomainKeyViewInfo(domain, key));
|
||||
if(iter == DKToId[static_cast<size_t>(type)].end()) {
|
||||
ResourceId id = CM->getId(type, domain, key);
|
||||
DKToId[static_cast<size_t>(type)].emplace_hint(iter, BindDomainKeyInfo((std::string) domain, (std::string) key), id);
|
||||
return id;
|
||||
}
|
||||
|
||||
return iter->second;
|
||||
|
||||
// switch(type) {
|
||||
// case EnumDefContent::Voxel:
|
||||
|
||||
// case EnumDefContent::Node:
|
||||
// case EnumDefContent::World:
|
||||
// case EnumDefContent::Portal:
|
||||
// case EnumDefContent::Entity:
|
||||
// case EnumDefContent::Item:
|
||||
// default:
|
||||
// std::unreachable();
|
||||
// }
|
||||
}
|
||||
|
||||
ResourceId getIdVoxel(const std::string_view domain, const std::string_view key) {
|
||||
return getId(EnumDefContent::Voxel, domain, key);
|
||||
}
|
||||
|
||||
ResourceId getIdNode(const std::string_view domain, const std::string_view key) {
|
||||
return getId(EnumDefContent::Node, domain, key);
|
||||
}
|
||||
|
||||
ResourceId getIdWorld(const std::string_view domain, const std::string_view key) {
|
||||
return getId(EnumDefContent::World, domain, key);
|
||||
}
|
||||
|
||||
ResourceId getIdPortal(const std::string_view domain, const std::string_view key) {
|
||||
return getId(EnumDefContent::Portal, domain, key);
|
||||
}
|
||||
|
||||
ResourceId getIdEntity(const std::string_view domain, const std::string_view key) {
|
||||
return getId(EnumDefContent::Entity, domain, key);
|
||||
}
|
||||
|
||||
ResourceId getIdItem(const std::string_view domain, const std::string_view key) {
|
||||
return getId(EnumDefContent::Item, domain, key);
|
||||
}
|
||||
|
||||
private:
|
||||
ContentManager* CM;
|
||||
|
||||
std::array<
|
||||
ankerl::unordered_dense::map<BindDomainKeyInfo, ResourceId, KeyHash, KeyEq>,
|
||||
MAX_ENUM
|
||||
> DKToId;
|
||||
|
||||
std::unordered_map<DefVoxelId, std::optional<DefVoxel>*> Profiles_Voxel;
|
||||
std::unordered_map<DefNodeId, std::optional<DefNode>*> Profiles_Node;
|
||||
std::unordered_map<DefWorldId, std::optional<DefWorld>*> Profiles_World;
|
||||
std::unordered_map<DefPortalId, std::optional<DefPortal>*> Profiles_Portal;
|
||||
std::unordered_map<DefEntityId, std::optional<DefEntity>*> Profiles_Entity;
|
||||
std::unordered_map<DefItemId, std::optional<DefItem>*> Profiles_Item;
|
||||
};
|
||||
|
||||
public:
|
||||
ContentManager(AssetsManager &am);
|
||||
~ContentManager();
|
||||
|
||||
// Регистрирует определение контента
|
||||
void registerBase(EnumDefContent type, const std::string& domain, const std::string& key, const sol::table& profile);
|
||||
void unRegisterBase(EnumDefContent type, const std::string& domain, const std::string& key);
|
||||
// Регистрация модификатора предмета модом
|
||||
void registerModifier(EnumDefContent type, const std::string& mod, const std::string& domain, const std::string& key, const sol::table& profile);
|
||||
void unRegisterModifier(EnumDefContent type, const std::string& mod, const std::string& domain, const std::string& key);
|
||||
// Пометить все профили типа как изменённые (например, после перезагрузки ассетов)
|
||||
// void markAllProfilesDirty(EnumDefContent type);
|
||||
// Список всех зарегистрированных профилей выбранного типа
|
||||
std::vector<ResourceId> collectProfileIds(EnumDefContent type) const;
|
||||
// Компилирует изменённые профили
|
||||
struct Out_buildEndProfiles {
|
||||
std::vector<
|
||||
std::tuple<DefVoxelId, const DefVoxel*>
|
||||
> ChangedProfiles_Voxel;
|
||||
|
||||
std::vector<
|
||||
std::tuple<DefNodeId, const DefNode*>
|
||||
> ChangedProfiles_Node;
|
||||
|
||||
std::vector<
|
||||
std::tuple<DefWorldId, const DefWorld*>
|
||||
> ChangedProfiles_World;
|
||||
|
||||
std::vector<
|
||||
std::tuple<DefPortalId, const DefPortal*>
|
||||
> ChangedProfiles_Portal;
|
||||
|
||||
std::vector<
|
||||
std::tuple<DefEntityId, const DefEntity*>
|
||||
> ChangedProfiles_Entity;
|
||||
|
||||
std::vector<
|
||||
std::tuple<DefItemId, const DefItem*>
|
||||
> ChangedProfiles_Item;
|
||||
|
||||
std::array<
|
||||
std::vector<ResourceId>,
|
||||
MAX_ENUM
|
||||
> LostProfiles;
|
||||
|
||||
std::array<
|
||||
std::vector<BindDomainKeyInfo>,
|
||||
static_cast<size_t>(EnumDefContent::MAX_ENUM)
|
||||
> IdToDK;
|
||||
};
|
||||
|
||||
// Компилирует конечные профили по базе и модификаторам (предоставляет клиентам изменённые и потерянные)
|
||||
Out_buildEndProfiles buildEndProfiles();
|
||||
|
||||
struct Out_getAllProfiles {
|
||||
std::vector<DefVoxelId> ProfilesIds_Voxel;
|
||||
std::vector<const DefVoxel*> Profiles_Voxel;
|
||||
|
||||
std::vector<DefNodeId> ProfilesIds_Node;
|
||||
std::vector<const DefNode*> Profiles_Node;
|
||||
|
||||
std::vector<DefWorldId> ProfilesIds_World;
|
||||
std::vector<const DefWorld*> Profiles_World;
|
||||
|
||||
std::vector<DefPortalId> ProfilesIds_Portal;
|
||||
std::vector<const DefPortal*> Profiles_Portal;
|
||||
|
||||
std::vector<DefEntityId> ProfilesIds_Entity;
|
||||
std::vector<const DefEntity*> Profiles_Entity;
|
||||
|
||||
std::vector<DefItemId> ProfilesIds_Item;
|
||||
std::vector<const DefItem*> Profiles_Item;
|
||||
|
||||
std::array<
|
||||
std::vector<BindDomainKeyInfo>,
|
||||
static_cast<size_t>(EnumDefContent::MAX_ENUM)
|
||||
> IdToDK;
|
||||
};
|
||||
|
||||
// Выдаёт все профили (для новых клиентов)
|
||||
Out_getAllProfiles getAllProfiles();
|
||||
|
||||
|
||||
std::optional<DefVoxel>& getEntry_Voxel(ResourceId resId) {
|
||||
std::shared_lock mtx(Profiles_Mtx[static_cast<size_t>(EnumDefContent::Voxel)]);
|
||||
|
||||
assert(resId / TableEntry<DefVoxel>::ChunkSize <= Profiles_Voxel.size());
|
||||
return Profiles_Voxel[resId / TableEntry<DefVoxel>::ChunkSize]->Entries[resId % TableEntry<DefVoxel>::ChunkSize];
|
||||
}
|
||||
|
||||
std::optional<DefVoxel>& getEntry_Voxel(const std::string_view domain, const std::string_view key) {
|
||||
return getEntry_Voxel(getId(EnumDefContent::Voxel, domain, key));
|
||||
}
|
||||
|
||||
std::optional<DefNode>& getEntry_Node(ResourceId resId) {
|
||||
std::shared_lock mtx(Profiles_Mtx[static_cast<size_t>(EnumDefContent::Node)]);
|
||||
|
||||
assert(resId / TableEntry<DefNode>::ChunkSize < Profiles_Node.size());
|
||||
return Profiles_Node[resId / TableEntry<DefVoxel>::ChunkSize]->Entries[resId % TableEntry<DefVoxel>::ChunkSize];
|
||||
}
|
||||
|
||||
std::optional<DefNode>& getEntry_Node(const std::string_view domain, const std::string_view key) {
|
||||
return getEntry_Node(getId(EnumDefContent::Node, domain, key));
|
||||
}
|
||||
|
||||
std::optional<DefWorld>& getEntry_World(ResourceId resId) {
|
||||
std::shared_lock mtx(Profiles_Mtx[static_cast<size_t>(EnumDefContent::World)]);
|
||||
|
||||
assert(resId / TableEntry<DefWorld>::ChunkSize < Profiles_World.size());
|
||||
return Profiles_World[resId / TableEntry<DefVoxel>::ChunkSize]->Entries[resId % TableEntry<DefVoxel>::ChunkSize];
|
||||
}
|
||||
|
||||
std::optional<DefWorld>& getEntry_World(const std::string_view domain, const std::string_view key) {
|
||||
return getEntry_World(getId(EnumDefContent::World, domain, key));
|
||||
}
|
||||
|
||||
std::optional<DefPortal>& getEntry_Portal(ResourceId resId) {
|
||||
std::shared_lock mtx(Profiles_Mtx[static_cast<size_t>(EnumDefContent::Portal)]);
|
||||
|
||||
assert(resId / TableEntry<DefPortal>::ChunkSize < Profiles_Portal.size());
|
||||
return Profiles_Portal[resId / TableEntry<DefVoxel>::ChunkSize]->Entries[resId % TableEntry<DefVoxel>::ChunkSize];
|
||||
}
|
||||
|
||||
std::optional<DefPortal>& getEntry_Portal(const std::string_view domain, const std::string_view key) {
|
||||
return getEntry_Portal(getId(EnumDefContent::Portal, domain, key));
|
||||
}
|
||||
|
||||
std::optional<DefEntity>& getEntry_Entity(ResourceId resId) {
|
||||
std::shared_lock mtx(Profiles_Mtx[static_cast<size_t>(EnumDefContent::Entity)]);
|
||||
|
||||
assert(resId / TableEntry<DefEntity>::ChunkSize < Profiles_Entity.size());
|
||||
return Profiles_Entity[resId / TableEntry<DefVoxel>::ChunkSize]->Entries[resId % TableEntry<DefVoxel>::ChunkSize];
|
||||
}
|
||||
|
||||
std::optional<DefEntity>& getEntry_Entity(const std::string_view domain, const std::string_view key) {
|
||||
return getEntry_Entity(getId(EnumDefContent::Entity, domain, key));
|
||||
}
|
||||
|
||||
std::optional<DefItem>& getEntry_Item(ResourceId resId) {
|
||||
std::shared_lock mtx(Profiles_Mtx[static_cast<size_t>(EnumDefContent::Item)]);
|
||||
|
||||
assert(resId / TableEntry<DefItem>::ChunkSize < Profiles_Item.size());
|
||||
return Profiles_Item[resId / TableEntry<DefVoxel>::ChunkSize]->Entries[resId % TableEntry<DefVoxel>::ChunkSize];
|
||||
}
|
||||
|
||||
std::optional<DefItem>& getEntry_Item(const std::string_view domain, const std::string_view key) {
|
||||
return getEntry_Item(getId(EnumDefContent::Item, domain, key));
|
||||
}
|
||||
|
||||
ResourceId getId(EnumDefContent type, const std::string_view domain, const std::string_view key) {
|
||||
ResourceId resId = IdProvider::getId(type, domain, key);
|
||||
|
||||
switch(type) {
|
||||
case EnumDefContent::Voxel:
|
||||
if(resId >= Profiles_Voxel.size()*TableEntry<DefVoxel>::ChunkSize) {
|
||||
std::unique_lock mtx(Profiles_Mtx[static_cast<size_t>(EnumDefContent::Voxel)]);
|
||||
if(resId >= Profiles_Voxel.size()*TableEntry<DefVoxel>::ChunkSize)
|
||||
Profiles_Voxel.push_back(std::make_unique<TableEntry<DefVoxel>>());
|
||||
}
|
||||
break;
|
||||
case EnumDefContent::Node:
|
||||
if(resId >= Profiles_Node.size()*TableEntry<DefNode>::ChunkSize) {
|
||||
std::unique_lock mtx(Profiles_Mtx[static_cast<size_t>(EnumDefContent::Node)]);
|
||||
if(resId >= Profiles_Node.size()*TableEntry<DefNode>::ChunkSize)
|
||||
Profiles_Node.push_back(std::make_unique<TableEntry<DefNode>>());
|
||||
}
|
||||
break;
|
||||
case EnumDefContent::World:
|
||||
if(resId >= Profiles_World.size()*TableEntry<DefWorld>::ChunkSize) {
|
||||
std::unique_lock mtx(Profiles_Mtx[static_cast<size_t>(EnumDefContent::World)]);
|
||||
if(resId >= Profiles_World.size()*TableEntry<DefWorld>::ChunkSize)
|
||||
Profiles_World.push_back(std::make_unique<TableEntry<DefWorld>>());
|
||||
}
|
||||
break;
|
||||
case EnumDefContent::Portal:
|
||||
if(resId >= Profiles_Portal.size()*TableEntry<DefPortal>::ChunkSize) {
|
||||
std::unique_lock mtx(Profiles_Mtx[static_cast<size_t>(EnumDefContent::Portal)]);
|
||||
if(resId >= Profiles_Portal.size()*TableEntry<DefPortal>::ChunkSize)
|
||||
Profiles_Portal.push_back(std::make_unique<TableEntry<DefPortal>>());
|
||||
}
|
||||
break;
|
||||
case EnumDefContent::Entity:
|
||||
if(resId >= Profiles_Entity.size()*TableEntry<DefEntity>::ChunkSize) {
|
||||
std::unique_lock mtx(Profiles_Mtx[static_cast<size_t>(EnumDefContent::Entity)]);
|
||||
if(resId >= Profiles_Entity.size()*TableEntry<DefEntity>::ChunkSize)
|
||||
Profiles_Entity.push_back(std::make_unique<TableEntry<DefEntity>>());
|
||||
}
|
||||
break;
|
||||
case EnumDefContent::Item:
|
||||
if(resId >= Profiles_Item.size()*TableEntry<DefItem>::ChunkSize) {
|
||||
std::unique_lock mtx(Profiles_Mtx[static_cast<size_t>(EnumDefContent::Item)]);
|
||||
if(resId >= Profiles_Item.size()*TableEntry<DefItem>::ChunkSize)
|
||||
Profiles_Item.push_back(std::make_unique<TableEntry<DefItem>>());
|
||||
}
|
||||
break;
|
||||
default:
|
||||
std::unreachable();
|
||||
}
|
||||
|
||||
return resId;
|
||||
}
|
||||
|
||||
LRU createLRU() {
|
||||
return {*this};
|
||||
}
|
||||
|
||||
private:
|
||||
template<typename T>
|
||||
struct TableEntry {
|
||||
static constexpr size_t ChunkSize = 4096;
|
||||
std::array<std::optional<T>, ChunkSize> Entries;
|
||||
};
|
||||
|
||||
void registerBase_Node(ResourceId id, const std::string& domain, const std::string& key, const sol::table& profile);
|
||||
void registerBase_World(ResourceId id, const std::string& domain, const std::string& key, const sol::table& profile);
|
||||
void registerBase_Entity(ResourceId id, const std::string& domain, const std::string& key, const sol::table& profile);
|
||||
|
||||
// Следующие идентификаторы регистрации контента
|
||||
ResourceId NextId[(int) EnumDefContent::MAX_ENUM] = {};
|
||||
// Домен -> {ключ -> идентификатор}
|
||||
std::unordered_map<std::string, std::unordered_map<std::string, ResourceId>> ContentKeyToId[(int) EnumDefContent::MAX_ENUM];
|
||||
template<class type, class modType>
|
||||
void buildEndProfilesByType(auto& profiles, auto enumType, auto& base, auto& keys, auto& result, auto& mods);
|
||||
|
||||
TOS::Logger LOG = "Server>ContentManager";
|
||||
AssetsManager& AM;
|
||||
|
||||
// Профили зарегистрированные модами
|
||||
std::vector<std::unique_ptr<TableEntry<DefVoxel>>> Profiles_Base_Voxel;
|
||||
std::vector<std::unique_ptr<TableEntry<DefNode>>> Profiles_Base_Node;
|
||||
std::vector<std::unique_ptr<TableEntry<DefWorld>>> Profiles_Base_World;
|
||||
std::vector<std::unique_ptr<TableEntry<DefPortal>>> Profiles_Base_Portal;
|
||||
std::vector<std::unique_ptr<TableEntry<DefEntity>>> Profiles_Base_Entity;
|
||||
std::vector<std::unique_ptr<TableEntry<DefItem>>> Profiles_Base_Item;
|
||||
std::vector<std::unique_ptr<TableEntry<DefVoxel_Base>>> Profiles_Base_Voxel;
|
||||
std::vector<std::unique_ptr<TableEntry<DefNode_Base>>> Profiles_Base_Node;
|
||||
std::vector<std::unique_ptr<TableEntry<DefWorld_Base>>> Profiles_Base_World;
|
||||
std::vector<std::unique_ptr<TableEntry<DefPortal_Base>>> Profiles_Base_Portal;
|
||||
std::vector<std::unique_ptr<TableEntry<DefEntity_Base>>> Profiles_Base_Entity;
|
||||
std::vector<std::unique_ptr<TableEntry<DefItem_Base>>> Profiles_Base_Item;
|
||||
|
||||
// Изменения, накладываемые на профили
|
||||
// Идентификатор [домен мода модификатора, модификатор]
|
||||
@@ -71,151 +443,16 @@ class ContentManager {
|
||||
|
||||
// Затронутые профили в процессе регистраций
|
||||
// По ним будут пересобраны профили
|
||||
std::vector<ResourceId> ProfileChanges[(int) EnumDefContent::MAX_ENUM];
|
||||
std::vector<ResourceId> ProfileChanges[MAX_ENUM];
|
||||
|
||||
// Конечные профили контента
|
||||
std::array<std::shared_mutex, MAX_ENUM> Profiles_Mtx;
|
||||
std::vector<std::unique_ptr<TableEntry<DefVoxel>>> Profiles_Voxel;
|
||||
std::vector<std::unique_ptr<TableEntry<DefNode>>> Profiles_Node;
|
||||
std::vector<std::unique_ptr<TableEntry<DefWorld>>> Profiles_World;
|
||||
std::vector<std::unique_ptr<TableEntry<DefPortal>>> Profiles_Portal;
|
||||
std::vector<std::unique_ptr<TableEntry<DefEntity>>> Profiles_Entity;
|
||||
std::vector<std::unique_ptr<TableEntry<DefItem>>> Profiles_Item;
|
||||
|
||||
std::optional<DefVoxel>& getEntry_Voxel(ResourceId resId) { return Profiles_Voxel[resId / TableEntry<DefVoxel>::ChunkSize]->Entries[resId % TableEntry<DefVoxel>::ChunkSize];}
|
||||
std::optional<DefNode>& getEntry_Node(ResourceId resId) { return Profiles_Node[resId / TableEntry<DefVoxel>::ChunkSize]->Entries[resId % TableEntry<DefVoxel>::ChunkSize];}
|
||||
std::optional<DefWorld>& getEntry_World(ResourceId resId) { return Profiles_World[resId / TableEntry<DefVoxel>::ChunkSize]->Entries[resId % TableEntry<DefVoxel>::ChunkSize];}
|
||||
std::optional<DefPortal>& getEntry_Portal(ResourceId resId) { return Profiles_Portal[resId / TableEntry<DefVoxel>::ChunkSize]->Entries[resId % TableEntry<DefVoxel>::ChunkSize];}
|
||||
std::optional<DefEntity>& getEntry_Entity(ResourceId resId) { return Profiles_Entity[resId / TableEntry<DefVoxel>::ChunkSize]->Entries[resId % TableEntry<DefVoxel>::ChunkSize];}
|
||||
std::optional<DefItem>& getEntry_Item(ResourceId resId) { return Profiles_Item[resId / TableEntry<DefVoxel>::ChunkSize]->Entries[resId % TableEntry<DefVoxel>::ChunkSize];}
|
||||
|
||||
ResourceId getId(EnumDefContent type, const std::string& domain, const std::string& key) {
|
||||
if(auto iterCKTI = ContentKeyToId[(int) type].find(domain); iterCKTI != ContentKeyToId[(int) type].end()) {
|
||||
if(auto iterKey = iterCKTI->second.find(key); iterKey != iterCKTI->second.end()) {
|
||||
return iterKey->second;
|
||||
}
|
||||
}
|
||||
|
||||
ResourceId resId = NextId[(int) type]++;
|
||||
ContentKeyToId[(int) type][domain][key] = resId;
|
||||
|
||||
switch(type) {
|
||||
case EnumDefContent::Voxel:
|
||||
if(resId >= Profiles_Voxel.size()*TableEntry<DefVoxel>::ChunkSize)
|
||||
Profiles_Voxel.push_back(std::make_unique<TableEntry<DefVoxel>>());
|
||||
break;
|
||||
case EnumDefContent::Node:
|
||||
if(resId >= Profiles_Node.size()*TableEntry<DefNode>::ChunkSize)
|
||||
Profiles_Node.push_back(std::make_unique<TableEntry<DefNode>>());
|
||||
break;
|
||||
case EnumDefContent::World:
|
||||
if(resId >= Profiles_World.size()*TableEntry<DefWorld>::ChunkSize)
|
||||
Profiles_World.push_back(std::make_unique<TableEntry<DefWorld>>());
|
||||
break;
|
||||
case EnumDefContent::Portal:
|
||||
if(resId >= Profiles_Portal.size()*TableEntry<DefPortal>::ChunkSize)
|
||||
Profiles_Portal.push_back(std::make_unique<TableEntry<DefPortal>>());
|
||||
break;
|
||||
case EnumDefContent::Entity:
|
||||
if(resId >= Profiles_Entity.size()*TableEntry<DefEntity>::ChunkSize)
|
||||
Profiles_Entity.push_back(std::make_unique<TableEntry<DefEntity>>());
|
||||
break;
|
||||
case EnumDefContent::Item:
|
||||
if(resId >= Profiles_Item.size()*TableEntry<DefItem>::ChunkSize)
|
||||
Profiles_Item.push_back(std::make_unique<TableEntry<DefItem>>());
|
||||
break;
|
||||
default:
|
||||
std::unreachable();
|
||||
}
|
||||
|
||||
return resId;
|
||||
}
|
||||
|
||||
void registerBase_Node(ResourceId id, const std::string& domain, const std::string& key, const sol::table& profile);
|
||||
void registerBase_World(ResourceId id, const std::string& domain, const std::string& key, const sol::table& profile);
|
||||
void registerBase_Entity(ResourceId id, const std::string& domain, const std::string& key, const sol::table& profile);
|
||||
|
||||
public:
|
||||
ContentManager(AssetsPreloader &am);
|
||||
~ContentManager();
|
||||
|
||||
// Регистрирует определение контента
|
||||
void registerBase(EnumDefContent type, const std::string& domain, const std::string& key, const sol::table& profile);
|
||||
void unRegisterBase(EnumDefContent type, const std::string& domain, const std::string& key);
|
||||
// Регистрация модификатора предмета модом
|
||||
void registerModifier(EnumDefContent type, const std::string& mod, const std::string& domain, const std::string& key, const sol::table& profile);
|
||||
void unRegisterModifier(EnumDefContent type, const std::string& mod, const std::string& domain, const std::string& key);
|
||||
// Пометить все профили типа как изменённые (например, после перезагрузки ассетов)
|
||||
void markAllProfilesDirty(EnumDefContent type);
|
||||
// Список всех зарегистрированных профилей выбранного типа
|
||||
std::vector<ResourceId> collectProfileIds(EnumDefContent type) const;
|
||||
// Компилирует изменённые профили
|
||||
struct Out_buildEndProfiles {
|
||||
std::vector<ResourceId> ChangedProfiles[(int) EnumDefContent::MAX_ENUM];
|
||||
};
|
||||
|
||||
Out_buildEndProfiles buildEndProfiles();
|
||||
|
||||
std::optional<DefVoxel*> getProfile_Voxel(ResourceId id) {
|
||||
assert(id < Profiles_Voxel.size()*TableEntry<DefVoxel>::ChunkSize);
|
||||
auto& value = Profiles_Voxel[id / TableEntry<DefVoxel>::ChunkSize]->Entries[id % TableEntry<DefVoxel>::ChunkSize];
|
||||
if(value)
|
||||
return {&*value};
|
||||
else
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
std::optional<DefNode*> getProfile_Node(ResourceId id) {
|
||||
assert(id < Profiles_Node.size()*TableEntry<DefNode>::ChunkSize);
|
||||
auto& value = Profiles_Node[id / TableEntry<DefNode>::ChunkSize]->Entries[id % TableEntry<DefNode>::ChunkSize];
|
||||
if(value)
|
||||
return {&*value};
|
||||
else
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
std::optional<DefWorld*> getProfile_World(ResourceId id) {
|
||||
assert(id < Profiles_World.size()*TableEntry<DefWorld>::ChunkSize);
|
||||
auto& value = Profiles_World[id / TableEntry<DefWorld>::ChunkSize]->Entries[id % TableEntry<DefWorld>::ChunkSize];
|
||||
if(value)
|
||||
return {&*value};
|
||||
else
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
std::optional<DefPortal*> getProfile_Portal(ResourceId id) {
|
||||
assert(id < Profiles_Portal.size()*TableEntry<DefPortal>::ChunkSize);
|
||||
auto& value = Profiles_Portal[id / TableEntry<DefPortal>::ChunkSize]->Entries[id % TableEntry<DefPortal>::ChunkSize];
|
||||
if(value)
|
||||
return {&*value};
|
||||
else
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
std::optional<DefEntity*> getProfile_Entity(ResourceId id) {
|
||||
assert(id < Profiles_Entity.size()*TableEntry<DefEntity>::ChunkSize);
|
||||
auto& value = Profiles_Entity[id / TableEntry<DefEntity>::ChunkSize]->Entries[id % TableEntry<DefEntity>::ChunkSize];
|
||||
if(value)
|
||||
return {&*value};
|
||||
else
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
std::optional<DefItem*> getProfile_Item(ResourceId id) {
|
||||
assert(id < Profiles_Item.size()*TableEntry<DefItem>::ChunkSize);
|
||||
auto& value = Profiles_Item[id / TableEntry<DefItem>::ChunkSize]->Entries[id % TableEntry<DefItem>::ChunkSize];
|
||||
if(value)
|
||||
return {&*value};
|
||||
else
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
ResourceId getContentId(EnumDefContent type, const std::string& domain, const std::string& key) {
|
||||
return getId(type, domain, key);
|
||||
}
|
||||
|
||||
private:
|
||||
TOS::Logger LOG = "Server>ContentManager";
|
||||
AssetsPreloader& AM;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
@@ -487,15 +487,14 @@ std::variant<std::vector<ModInfo>, std::vector<std::string>> resolveDepends(cons
|
||||
|
||||
GameServer::GameServer(asio::io_context &ioc, fs::path worldPath)
|
||||
: AsyncObject(ioc),
|
||||
Content(ioc)
|
||||
Content(ioc), BackingAsyncLua(Content.CM)
|
||||
{
|
||||
init(worldPath);
|
||||
}
|
||||
|
||||
GameServer::~GameServer() {
|
||||
shutdown("on ~GameServer");
|
||||
BackingChunkPressure.NeedShutdown = true;
|
||||
BackingChunkPressure.Symaphore.notify_all();
|
||||
BackingChunkPressure.NeedShutdown.store(true, std::memory_order_release);
|
||||
BackingNoiseGenerator.NeedShutdown = true;
|
||||
BackingAsyncLua.NeedShutdown = true;
|
||||
|
||||
@@ -511,27 +510,19 @@ GameServer::~GameServer() {
|
||||
}
|
||||
|
||||
void GameServer::BackingChunkPressure_t::run(int id) {
|
||||
// static thread_local int local_counter = -1;
|
||||
int iteration = 0;
|
||||
LOG.debug() << "Старт потока " << id;
|
||||
|
||||
try {
|
||||
while(true) {
|
||||
// local_counter++;
|
||||
// LOG.debug() << "Ожидаю начала " << id << ' ' << local_counter;
|
||||
{
|
||||
std::unique_lock<std::mutex> lock(Mutex);
|
||||
Symaphore.wait(lock, [&](){ return iteration != Iteration || NeedShutdown; });
|
||||
if(NeedShutdown) {
|
||||
LOG.debug() << "Завершение выполнения потока " << id;
|
||||
break;
|
||||
}
|
||||
|
||||
iteration = Iteration;
|
||||
if(NeedShutdown.load(std::memory_order_acquire)) {
|
||||
CollectStart->arrive_and_drop();
|
||||
LOG.debug() << "Завершение выполнения потока " << id;
|
||||
break;
|
||||
}
|
||||
|
||||
assert(RunCollect > 0);
|
||||
assert(RunCompress > 0);
|
||||
CollectStart->arrive_and_wait();
|
||||
|
||||
bool shutting = NeedShutdown.load(std::memory_order_acquire);
|
||||
|
||||
// Сбор данных
|
||||
size_t pullSize = Threads.size();
|
||||
@@ -546,157 +537,159 @@ void GameServer::BackingChunkPressure_t::run(int id) {
|
||||
|
||||
std::vector<std::pair<WorldId_t, std::vector<std::pair<Pos::GlobalRegion, Dump>>>> dump;
|
||||
|
||||
for(const auto& [worldId, world] : *Worlds) {
|
||||
const auto &worldObj = *world;
|
||||
std::vector<std::pair<Pos::GlobalRegion, Dump>> dumpWorld;
|
||||
if(!shutting) {
|
||||
try {
|
||||
for(const auto& [worldId, world] : *Worlds) {
|
||||
const auto &worldObj = *world;
|
||||
std::vector<std::pair<Pos::GlobalRegion, Dump>> dumpWorld;
|
||||
|
||||
for(const auto& [regionPos, region] : worldObj.Regions) {
|
||||
auto& regionObj = *region;
|
||||
if(counter++ % pullSize != id) {
|
||||
continue;
|
||||
}
|
||||
|
||||
Dump dumpRegion;
|
||||
|
||||
dumpRegion.CECs = regionObj.RMs;
|
||||
dumpRegion.IsChunkChanged_Voxels = regionObj.IsChunkChanged_Voxels;
|
||||
regionObj.IsChunkChanged_Voxels = 0;
|
||||
dumpRegion.IsChunkChanged_Nodes = regionObj.IsChunkChanged_Nodes;
|
||||
regionObj.IsChunkChanged_Nodes = 0;
|
||||
|
||||
if(!regionObj.NewRMs.empty()) {
|
||||
dumpRegion.NewCECs = std::move(regionObj.NewRMs);
|
||||
dumpRegion.Voxels = regionObj.Voxels;
|
||||
|
||||
for(int z = 0; z < 4; z++)
|
||||
for(int y = 0; y < 4; y++)
|
||||
for(int x = 0; x < 4; x++)
|
||||
{
|
||||
auto &toPtr = dumpRegion.Nodes[Pos::bvec4u(x, y, z)];
|
||||
const Node *fromPtr = regionObj.Nodes[Pos::bvec4u(x, y, z).pack()].data();
|
||||
std::copy(fromPtr, fromPtr+16*16*16, toPtr.data());
|
||||
for(const auto& [regionPos, region] : worldObj.Regions) {
|
||||
auto& regionObj = *region;
|
||||
if(counter++ % pullSize != id) {
|
||||
continue;
|
||||
}
|
||||
} else {
|
||||
if(dumpRegion.IsChunkChanged_Voxels) {
|
||||
for(int index = 0; index < 64; index++) {
|
||||
if(((dumpRegion.IsChunkChanged_Voxels >> index) & 0x1) == 0)
|
||||
continue;
|
||||
|
||||
Pos::bvec4u chunkPos;
|
||||
chunkPos.unpack(index);
|
||||
Dump dumpRegion;
|
||||
|
||||
auto voxelIter = regionObj.Voxels.find(chunkPos);
|
||||
if(voxelIter != regionObj.Voxels.end()) {
|
||||
dumpRegion.Voxels[chunkPos] = voxelIter->second;
|
||||
} else {
|
||||
dumpRegion.Voxels[chunkPos] = {};
|
||||
dumpRegion.CECs = regionObj.RMs;
|
||||
dumpRegion.IsChunkChanged_Voxels = regionObj.IsChunkChanged_Voxels;
|
||||
regionObj.IsChunkChanged_Voxels = 0;
|
||||
dumpRegion.IsChunkChanged_Nodes = regionObj.IsChunkChanged_Nodes;
|
||||
regionObj.IsChunkChanged_Nodes = 0;
|
||||
|
||||
if(!regionObj.NewRMs.empty()) {
|
||||
dumpRegion.NewCECs = std::move(regionObj.NewRMs);
|
||||
dumpRegion.Voxels = regionObj.Voxels;
|
||||
|
||||
for(int z = 0; z < 4; z++)
|
||||
for(int y = 0; y < 4; y++)
|
||||
for(int x = 0; x < 4; x++)
|
||||
{
|
||||
auto &toPtr = dumpRegion.Nodes[Pos::bvec4u(x, y, z)];
|
||||
const Node *fromPtr = regionObj.Nodes[Pos::bvec4u(x, y, z).pack()].data();
|
||||
std::copy(fromPtr, fromPtr+16*16*16, toPtr.data());
|
||||
}
|
||||
} else {
|
||||
if(dumpRegion.IsChunkChanged_Voxels) {
|
||||
for(int index = 0; index < 64; index++) {
|
||||
if(((dumpRegion.IsChunkChanged_Voxels >> index) & 0x1) == 0)
|
||||
continue;
|
||||
|
||||
Pos::bvec4u chunkPos;
|
||||
chunkPos.unpack(index);
|
||||
|
||||
auto voxelIter = regionObj.Voxels.find(chunkPos);
|
||||
if(voxelIter != regionObj.Voxels.end()) {
|
||||
dumpRegion.Voxels[chunkPos] = voxelIter->second;
|
||||
} else {
|
||||
dumpRegion.Voxels[chunkPos] = {};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if(dumpRegion.IsChunkChanged_Nodes) {
|
||||
for(int index = 0; index < 64; index++) {
|
||||
if(((dumpRegion.IsChunkChanged_Nodes >> index) & 0x1) == 0)
|
||||
continue;
|
||||
|
||||
Pos::bvec4u chunkPos;
|
||||
chunkPos.unpack(index);
|
||||
|
||||
auto &toPtr = dumpRegion.Nodes[chunkPos];
|
||||
const Node *fromPtr = regionObj.Nodes[chunkPos.pack()].data();
|
||||
std::copy(fromPtr, fromPtr+16*16*16, toPtr.data());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if(dumpRegion.IsChunkChanged_Nodes) {
|
||||
for(int index = 0; index < 64; index++) {
|
||||
if(((dumpRegion.IsChunkChanged_Nodes >> index) & 0x1) == 0)
|
||||
continue;
|
||||
|
||||
Pos::bvec4u chunkPos;
|
||||
chunkPos.unpack(index);
|
||||
|
||||
auto &toPtr = dumpRegion.Nodes[chunkPos];
|
||||
const Node *fromPtr = regionObj.Nodes[chunkPos.pack()].data();
|
||||
std::copy(fromPtr, fromPtr+16*16*16, toPtr.data());
|
||||
if(!dumpRegion.CECs.empty()) {
|
||||
dumpWorld.push_back({regionPos, std::move(dumpRegion)});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if(!dumpRegion.CECs.empty()) {
|
||||
dumpWorld.push_back({regionPos, std::move(dumpRegion)});
|
||||
if(!dumpWorld.empty()) {
|
||||
dump.push_back({worldId, std::move(dumpWorld)});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if(!dumpWorld.empty()) {
|
||||
dump.push_back({worldId, std::move(dumpWorld)});
|
||||
} catch(const std::exception&) {
|
||||
NeedShutdown.store(true, std::memory_order_release);
|
||||
shutting = true;
|
||||
}
|
||||
}
|
||||
|
||||
// Синхронизация
|
||||
// LOG.debug() << "Синхронизирую " << id << ' ' << local_counter;
|
||||
{
|
||||
std::unique_lock<std::mutex> lock(Mutex);
|
||||
RunCollect -= 1;
|
||||
Symaphore.notify_all();
|
||||
}
|
||||
CollectEnd->arrive_and_wait();
|
||||
|
||||
// Сжатие и отправка игрокам
|
||||
for(auto& [worldId, world] : dump) {
|
||||
for(auto& [regionPos, region] : world) {
|
||||
for(auto& [chunkPos, chunk] : region.Voxels) {
|
||||
std::u8string cmp = compressVoxels(chunk);
|
||||
if(!shutting) {
|
||||
try {
|
||||
for(auto& [worldId, world] : dump) {
|
||||
for(auto& [regionPos, region] : world) {
|
||||
for(auto& [chunkPos, chunk] : region.Voxels) {
|
||||
std::u8string cmp = compressVoxels(chunk);
|
||||
|
||||
for(auto& ptr : region.NewCECs) {
|
||||
ptr->prepareChunkUpdate_Voxels(worldId, regionPos, chunkPos, cmp);
|
||||
}
|
||||
|
||||
if((region.IsChunkChanged_Voxels >> chunkPos.pack()) & 0x1) {
|
||||
for(auto& ptr : region.CECs) {
|
||||
bool skip = false;
|
||||
for(auto& ptr2 : region.NewCECs) {
|
||||
if(ptr == ptr2) {
|
||||
skip = true;
|
||||
break;
|
||||
}
|
||||
for(auto& ptr : region.NewCECs) {
|
||||
ptr->prepareChunkUpdate_Voxels(worldId, regionPos, chunkPos, cmp);
|
||||
}
|
||||
|
||||
if(skip)
|
||||
continue;
|
||||
if((region.IsChunkChanged_Voxels >> chunkPos.pack()) & 0x1) {
|
||||
for(auto& ptr : region.CECs) {
|
||||
bool skip = false;
|
||||
for(auto& ptr2 : region.NewCECs) {
|
||||
if(ptr == ptr2) {
|
||||
skip = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
ptr->prepareChunkUpdate_Voxels(worldId, regionPos, chunkPos, cmp);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for(auto& [chunkPos, chunk] : region.Nodes) {
|
||||
std::u8string cmp = compressNodes(chunk.data());
|
||||
|
||||
for(auto& ptr : region.NewCECs) {
|
||||
ptr->prepareChunkUpdate_Nodes(worldId, regionPos, chunkPos, cmp);
|
||||
}
|
||||
|
||||
if((region.IsChunkChanged_Nodes >> chunkPos.pack()) & 0x1) {
|
||||
for(auto& ptr : region.CECs) {
|
||||
bool skip = false;
|
||||
for(auto& ptr2 : region.NewCECs) {
|
||||
if(ptr == ptr2) {
|
||||
skip = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if(skip)
|
||||
continue;
|
||||
|
||||
ptr->prepareChunkUpdate_Nodes(worldId, regionPos, chunkPos, cmp);
|
||||
if(skip)
|
||||
continue;
|
||||
|
||||
ptr->prepareChunkUpdate_Voxels(worldId, regionPos, chunkPos, cmp);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for(auto& [chunkPos, chunk] : region.Nodes) {
|
||||
std::u8string cmp = compressNodes(chunk.data());
|
||||
|
||||
for(auto& ptr : region.NewCECs) {
|
||||
ptr->prepareChunkUpdate_Nodes(worldId, regionPos, chunkPos, cmp);
|
||||
}
|
||||
|
||||
if((region.IsChunkChanged_Nodes >> chunkPos.pack()) & 0x1) {
|
||||
for(auto& ptr : region.CECs) {
|
||||
bool skip = false;
|
||||
for(auto& ptr2 : region.NewCECs) {
|
||||
if(ptr == ptr2) {
|
||||
skip = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if(skip)
|
||||
continue;
|
||||
|
||||
ptr->prepareChunkUpdate_Nodes(worldId, regionPos, chunkPos, cmp);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch(const std::exception&) {
|
||||
NeedShutdown.store(true, std::memory_order_release);
|
||||
shutting = true;
|
||||
}
|
||||
}
|
||||
|
||||
// Синхронизация
|
||||
// LOG.debug() << "Конец " << id << ' ' << local_counter;
|
||||
{
|
||||
std::unique_lock<std::mutex> lock(Mutex);
|
||||
RunCompress -= 1;
|
||||
Symaphore.notify_all();
|
||||
}
|
||||
CompressEnd->arrive_and_wait();
|
||||
|
||||
if(shutting)
|
||||
continue;
|
||||
}
|
||||
} catch(const std::exception& exc) {
|
||||
std::unique_lock<std::mutex> lock(Mutex);
|
||||
NeedShutdown = true;
|
||||
NeedShutdown.store(true, std::memory_order_release);
|
||||
LOG.error() << "Ошибка выполнения потока " << id << ":\n" << exc.what();
|
||||
}
|
||||
|
||||
Symaphore.notify_all();
|
||||
}
|
||||
|
||||
void GameServer::BackingNoiseGenerator_t::run(int id) {
|
||||
@@ -778,17 +771,19 @@ void GameServer::BackingAsyncLua_t::run(int id) {
|
||||
out.Voxels.clear();
|
||||
out.Entityes.clear();
|
||||
|
||||
auto lru = CM.createLRU();
|
||||
|
||||
{
|
||||
constexpr DefNodeId kNodeAir = 0;
|
||||
constexpr DefNodeId kNodeGrass = 2;
|
||||
constexpr uint8_t kMetaGrass = 1;
|
||||
constexpr DefNodeId kNodeDirt = 3;
|
||||
constexpr DefNodeId kNodeStone = 4;
|
||||
constexpr DefNodeId kNodeWood = 1;
|
||||
constexpr DefNodeId kNodeLeaves = 5;
|
||||
constexpr DefNodeId kNodeLava = 7;
|
||||
constexpr DefNodeId kNodeWater = 8;
|
||||
constexpr DefNodeId kNodeFire = 9;
|
||||
DefNodeId kNodeAir = 0;
|
||||
DefNodeId kNodeGrass = lru.getIdNode("test", "grass");
|
||||
uint8_t kMetaGrass = 1;
|
||||
DefNodeId kNodeDirt = lru.getIdNode("test", "dirt");
|
||||
DefNodeId kNodeStone = lru.getIdNode("test", "stone");
|
||||
DefNodeId kNodeWood = lru.getIdNode("test", "log");
|
||||
DefNodeId kNodeLeaves = lru.getIdNode("test", "leaves");
|
||||
DefNodeId kNodeLava = lru.getIdNode("test", "lava");
|
||||
DefNodeId kNodeWater = lru.getIdNode("test", "water");
|
||||
DefNodeId kNodeFire = lru.getIdNode("test", "fire");
|
||||
|
||||
auto hash32 = [](uint32_t x) {
|
||||
x ^= x >> 16;
|
||||
@@ -923,9 +918,9 @@ void GameServer::BackingAsyncLua_t::run(int id) {
|
||||
constexpr int kTestGlobalY = 64;
|
||||
if(regionBase.y <= kTestGlobalY && (regionBase.y + 63) >= kTestGlobalY) {
|
||||
int localY = kTestGlobalY - regionBase.y;
|
||||
setNode(2, localY, 2, kNodeLava, 0, false);
|
||||
setNode(4, localY, 2, kNodeWater, 0, false);
|
||||
setNode(6, localY, 2, kNodeFire, 0, false);
|
||||
setNode(7, localY, 2, kNodeLava, 0, false);
|
||||
setNode(8, localY, 2, kNodeWater, 0, false);
|
||||
setNode(9, localY, 2, kNodeFire, 0, false);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1345,7 +1340,8 @@ void GameServer::init(fs::path worldPath) {
|
||||
AssetsInit.Assets.push_back(mlt.LoadChain[index].Path / "assets");
|
||||
}
|
||||
|
||||
Content.AM.applyResourceChange(Content.AM.reloadResources(AssetsInit));
|
||||
auto capru = Content.AM.checkAndPrepareResourcesUpdate(AssetsInit);
|
||||
Content.AM.applyResourcesUpdate(capru);
|
||||
|
||||
LOG.info() << "Пре Инициализация";
|
||||
|
||||
@@ -1354,7 +1350,7 @@ void GameServer::init(fs::path worldPath) {
|
||||
// Content.CM.registerBase(EnumDefContent::Node, "core", "none", t);
|
||||
Content.CM.registerBase(EnumDefContent::World, "test", "devel_world", t);
|
||||
Content.CM.registerBase(EnumDefContent::Entity, "core", "player", t);
|
||||
PlayerEntityDefId = Content.CM.getContentId(EnumDefContent::Entity, "core", "player");
|
||||
// PlayerEntityDefId = Content.CM.getContentId(EnumDefContent::Entity, "core", "player");
|
||||
}
|
||||
|
||||
initLuaPre();
|
||||
@@ -1367,7 +1363,6 @@ void GameServer::init(fs::path worldPath) {
|
||||
|
||||
Content.CM.buildEndProfiles();
|
||||
|
||||
|
||||
LOG.info() << "Инициализация";
|
||||
initLua();
|
||||
pushEvent("init");
|
||||
@@ -1388,6 +1383,7 @@ void GameServer::init(fs::path worldPath) {
|
||||
LOG.info() << "Загрузка существующих миров...";
|
||||
BackingChunkPressure.Threads.resize(4);
|
||||
BackingChunkPressure.Worlds = &Expanse.Worlds;
|
||||
BackingChunkPressure.init(BackingChunkPressure.Threads.size());
|
||||
for(size_t iter = 0; iter < BackingChunkPressure.Threads.size(); iter++) {
|
||||
BackingChunkPressure.Threads[iter] = std::thread(&BackingChunkPressure_t::run, &BackingChunkPressure, iter);
|
||||
}
|
||||
@@ -1410,7 +1406,10 @@ void GameServer::prerun() {
|
||||
auto useLock = UseLock.lock();
|
||||
run();
|
||||
|
||||
} catch(const std::exception& exc) {
|
||||
LOG.error() << "Исключение в GameServer::run: " << exc.what();
|
||||
} catch(...) {
|
||||
LOG.error() << "Неизвестное исключение в GameServer::run";
|
||||
}
|
||||
|
||||
IsAlive = false;
|
||||
@@ -1429,6 +1428,7 @@ void GameServer::run() {
|
||||
|
||||
while(true) {
|
||||
((uint32_t&) Game.AfterStartTime) += (uint32_t) (CurrentTickDuration*256);
|
||||
Game.Tick++;
|
||||
|
||||
std::chrono::steady_clock::time_point atTickStart = std::chrono::steady_clock::now();
|
||||
|
||||
@@ -1460,7 +1460,14 @@ void GameServer::run() {
|
||||
|
||||
stepConnections();
|
||||
stepModInitializations();
|
||||
IWorldSaveBackend::TickSyncInfo_Out dat1 = stepDatabaseSync();
|
||||
IWorldSaveBackend::TickSyncInfo_Out dat1;
|
||||
try {
|
||||
dat1 = stepDatabaseSync();
|
||||
} catch(const std::exception& exc) {
|
||||
LOG.error() << "Ошибка stepDatabaseSync: " << exc.what();
|
||||
} catch(...) {
|
||||
LOG.error() << "Неизвестная ошибка stepDatabaseSync";
|
||||
}
|
||||
stepGeneratorAndLuaAsync(std::move(dat1));
|
||||
stepPlayerProceed();
|
||||
stepWorldPhysic();
|
||||
@@ -1606,12 +1613,12 @@ void GameServer::stepConnections() {
|
||||
}
|
||||
|
||||
if(!newClients.empty()) {
|
||||
AssetsPreloader::Out_fullSync fullSync = Content.AM.collectFullSync();
|
||||
std::array<std::vector<ResourceId>, static_cast<size_t>(EnumAssets::MAX_ENUM)> lost{};
|
||||
|
||||
std::vector<Net::Packet> packets;
|
||||
packets.push_back(RemoteClient::makePacket_informateAssets_DK(fullSync.IdToDK));
|
||||
packets.push_back(RemoteClient::makePacket_informateAssets_HH(fullSync.HashHeaders, lost));
|
||||
packets.push_back(RemoteClient::makePacket_informateAssets_DK(Content.AM.idToDK()));
|
||||
packets.push_back(RemoteClient::makePacket_informateAssets_HH(Content.AM.collectHashBindings(), lost));
|
||||
packets.append_range(RemoteClient::makePackets_informateDefContent_Full(Content.CM.getAllProfiles()));
|
||||
|
||||
for(const std::shared_ptr<RemoteClient>& client : newClients) {
|
||||
if(!packets.empty()) {
|
||||
@@ -1682,29 +1689,34 @@ void GameServer::reloadMods() {
|
||||
{
|
||||
// TODO: перезагрузка модов
|
||||
|
||||
Content.CM.buildEndProfiles();
|
||||
ContentManager::Out_buildEndProfiles out = Content.CM.buildEndProfiles();
|
||||
packetsToSend.append_range(RemoteClient::makePackets_informateDefContentUpdate(out));
|
||||
}
|
||||
|
||||
LOG.info() << "Перезагрузка ассетов";
|
||||
{
|
||||
{
|
||||
AssetsPreloader::Out_applyResourceChange applied
|
||||
= Content.AM.applyResourceChange(Content.AM.reloadResources(AssetsInit));
|
||||
AssetsManager::Out_checkAndPrepareResourcesUpdate capru = Content.AM.checkAndPrepareResourcesUpdate(AssetsInit);
|
||||
AssetsManager::Out_applyResourcesUpdate aru = Content.AM.applyResourcesUpdate(capru);
|
||||
|
||||
if(!applied.NewOrChange.empty() || !applied.Lost.empty())
|
||||
if(!capru.ResourceUpdates.empty() || !capru.LostLinks.empty())
|
||||
packetsToSend.push_back(
|
||||
RemoteClient::makePacket_informateAssets_HH(
|
||||
applied.NewOrChange,
|
||||
applied.Lost
|
||||
aru.NewOrUpdates,
|
||||
capru.LostLinks
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
{
|
||||
AssetsPreloader::Out_bakeId baked = Content.AM.bakeIdTables();
|
||||
if(hasAnyBindings(baked.IdToDK)) {
|
||||
packetsToSend.push_back(RemoteClient::makePacket_informateAssets_DK(baked.IdToDK));
|
||||
std::array<
|
||||
std::vector<AssetsManager::BindDomainKeyInfo>,
|
||||
static_cast<size_t>(EnumAssets::MAX_ENUM)
|
||||
> baked = Content.AM.bake();
|
||||
|
||||
if(hasAnyBindings(baked)) {
|
||||
packetsToSend.push_back(RemoteClient::makePacket_informateAssets_DK(baked));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1719,73 +1731,202 @@ void GameServer::reloadMods() {
|
||||
IWorldSaveBackend::TickSyncInfo_Out GameServer::stepDatabaseSync() {
|
||||
IWorldSaveBackend::TickSyncInfo_In toDB;
|
||||
|
||||
for(std::shared_ptr<RemoteClient>& remoteClient : Game.RemoteClients) {
|
||||
assert(remoteClient);
|
||||
// Пересчитать зоны наблюдения
|
||||
if(remoteClient->CrossedRegion) {
|
||||
remoteClient->CrossedRegion = false;
|
||||
constexpr uint32_t kRegionUnloadDelayTicks = 300;
|
||||
constexpr uint8_t kUnloadHysteresisExtraRegions = 1;
|
||||
const uint32_t nowTick = Game.Tick;
|
||||
|
||||
// Пересчёт зон наблюдения
|
||||
std::vector<ContentViewCircle> newCVCs;
|
||||
for(std::shared_ptr<RemoteClient>& remoteClient : Game.RemoteClients) {
|
||||
assert(remoteClient);
|
||||
|
||||
{
|
||||
std::vector<std::tuple<WorldId_t, Pos::Object, uint8_t>> points = remoteClient->getViewPoints();
|
||||
for(auto& [wId, pos, radius] : points) {
|
||||
assert(radius < 5);
|
||||
// 1) Если игрок пересёк границу региона — пересчитываем области наблюдения.
|
||||
// Вводим гистерезис: загрузка по "внутренней" границе, выгрузка по "внешней" (+1 регион).
|
||||
if(remoteClient->CrossedRegion) {
|
||||
remoteClient->CrossedRegion = false;
|
||||
|
||||
std::vector<ContentViewCircle> innerCVCs;
|
||||
std::vector<ContentViewCircle> outerCVCs;
|
||||
|
||||
{
|
||||
std::vector<std::tuple<WorldId_t, Pos::Object, uint8_t>> points = remoteClient->getViewPoints();
|
||||
for(auto& [wId, pos, radius] : points) {
|
||||
assert(radius < 5);
|
||||
|
||||
// Внутренняя область (на загрузку)
|
||||
{
|
||||
ContentViewCircle cvc;
|
||||
cvc.WorldId = wId;
|
||||
cvc.Pos = Pos::Object_t::asRegionsPos(pos);
|
||||
cvc.Range = radius*radius;
|
||||
cvc.Range = int16_t(radius * radius);
|
||||
|
||||
std::vector<ContentViewCircle> list = Expanse.accumulateContentViewCircles(cvc);
|
||||
newCVCs.insert(newCVCs.end(), list.begin(), list.end());
|
||||
innerCVCs.insert(innerCVCs.end(), list.begin(), list.end());
|
||||
}
|
||||
|
||||
// Внешняя область (на удержание/выгрузку) = внутренняя + 1 регион
|
||||
{
|
||||
uint8_t outerRadius = radius + kUnloadHysteresisExtraRegions;
|
||||
if(outerRadius > 5) outerRadius = 5;
|
||||
|
||||
ContentViewCircle cvc;
|
||||
cvc.WorldId = wId;
|
||||
cvc.Pos = Pos::Object_t::asRegionsPos(pos);
|
||||
cvc.Range = int16_t(outerRadius * outerRadius);
|
||||
|
||||
std::vector<ContentViewCircle> list = Expanse.accumulateContentViewCircles(cvc);
|
||||
outerCVCs.insert(outerCVCs.end(), list.begin(), list.end());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
ContentViewInfo newCbg = Expanse_t::makeContentViewInfo(newCVCs);
|
||||
ContentViewInfo viewInner = Expanse_t::makeContentViewInfo(innerCVCs);
|
||||
ContentViewInfo viewOuter = Expanse_t::makeContentViewInfo(outerCVCs);
|
||||
|
||||
ContentViewInfo_Diff diff = newCbg.diffWith(remoteClient->ContentViewState);
|
||||
if(!diff.WorldsNew.empty()) {
|
||||
// Сообщить о новых мирах
|
||||
for(const WorldId_t id : diff.WorldsNew) {
|
||||
auto iter = Expanse.Worlds.find(id);
|
||||
assert(iter != Expanse.Worlds.end());
|
||||
// Отменяем отложенную выгрузку для регионов, которые снова попали во внешнюю область
|
||||
for(const auto& [worldId, regions] : viewOuter.Regions) {
|
||||
auto itWorld = remoteClient->PendingRegionUnload.find(worldId);
|
||||
if(itWorld == remoteClient->PendingRegionUnload.end())
|
||||
continue;
|
||||
|
||||
remoteClient->prepareWorldUpdate(id, iter->second.get());
|
||||
}
|
||||
for(const Pos::GlobalRegion& pos : regions) {
|
||||
itWorld->second.erase(pos);
|
||||
}
|
||||
|
||||
remoteClient->ContentViewState = newCbg;
|
||||
// Вычистка не наблюдаемых регионов
|
||||
for(const auto& [worldId, regions] : diff.RegionsLost)
|
||||
remoteClient->prepareRegionsRemove(worldId, regions);
|
||||
// и миров
|
||||
for(const WorldId_t worldId : diff.WorldsLost)
|
||||
remoteClient->prepareWorldRemove(worldId);
|
||||
if(itWorld->second.empty())
|
||||
remoteClient->PendingRegionUnload.erase(itWorld);
|
||||
}
|
||||
|
||||
// Подписываем игрока на наблюдение за регионами
|
||||
for(const auto& [worldId, regions] : diff.RegionsNew) {
|
||||
auto iterWorld = Expanse.Worlds.find(worldId);
|
||||
assert(iterWorld != Expanse.Worlds.end());
|
||||
// Загрузка: только по внутренней границе
|
||||
ContentViewInfo_Diff diffInner = viewInner.diffWith(remoteClient->ContentViewState);
|
||||
|
||||
std::vector<Pos::GlobalRegion> notLoaded = iterWorld->second->onRemoteClient_RegionsEnter(worldId, remoteClient, regions);
|
||||
if(!notLoaded.empty()) {
|
||||
// Добавляем к списку на загрузку
|
||||
std::vector<Pos::GlobalRegion> &tl = toDB.Load[worldId];
|
||||
tl.insert(tl.end(), notLoaded.begin(), notLoaded.end());
|
||||
}
|
||||
if(!diffInner.WorldsNew.empty()) {
|
||||
// Сообщить о новых мирах
|
||||
for(const WorldId_t id : diffInner.WorldsNew) {
|
||||
auto iter = Expanse.Worlds.find(id);
|
||||
assert(iter != Expanse.Worlds.end());
|
||||
|
||||
remoteClient->prepareWorldUpdate(id, iter->second.get());
|
||||
}
|
||||
}
|
||||
|
||||
// Подписываем игрока на наблюдение за регионами (внутренняя область)
|
||||
for(const auto& [worldId, regions] : diffInner.RegionsNew) {
|
||||
// Добавляем в состояние клиента (слиянием, т.к. там могут быть регионы на удержании)
|
||||
{
|
||||
auto& cur = remoteClient->ContentViewState.Regions[worldId];
|
||||
std::vector<Pos::GlobalRegion> merged;
|
||||
merged.reserve(cur.size() + regions.size());
|
||||
std::merge(cur.begin(), cur.end(), regions.begin(), regions.end(), std::back_inserter(merged));
|
||||
merged.erase(std::unique(merged.begin(), merged.end()), merged.end());
|
||||
cur = std::move(merged);
|
||||
}
|
||||
|
||||
// Отписываем то, что игрок больше не наблюдает
|
||||
for(const auto& [worldId, regions] : diff.RegionsLost) {
|
||||
auto iterWorld = Expanse.Worlds.find(worldId);
|
||||
assert(iterWorld != Expanse.Worlds.end());
|
||||
auto iterWorld = Expanse.Worlds.find(worldId);
|
||||
assert(iterWorld != Expanse.Worlds.end());
|
||||
|
||||
iterWorld->second->onRemoteClient_RegionsLost(worldId, remoteClient, regions);
|
||||
std::vector<Pos::GlobalRegion> notLoaded = iterWorld->second->onRemoteClient_RegionsEnter(worldId, remoteClient, regions);
|
||||
if(!notLoaded.empty()) {
|
||||
// Добавляем к списку на загрузку
|
||||
std::vector<Pos::GlobalRegion> &tl = toDB.Load[worldId];
|
||||
tl.insert(tl.end(), notLoaded.begin(), notLoaded.end());
|
||||
}
|
||||
}
|
||||
|
||||
// Кандидаты на выгрузку: то, что есть у клиента, но не попадает во внешнюю область (гистерезис)
|
||||
for(const auto& [worldId, curRegions] : remoteClient->ContentViewState.Regions) {
|
||||
std::vector<Pos::GlobalRegion> outer;
|
||||
auto itOuter = viewOuter.Regions.find(worldId);
|
||||
if(itOuter != viewOuter.Regions.end())
|
||||
outer = itOuter->second;
|
||||
|
||||
std::vector<Pos::GlobalRegion> toDelay;
|
||||
toDelay.reserve(curRegions.size());
|
||||
|
||||
if(outer.empty()) {
|
||||
toDelay = curRegions;
|
||||
} else {
|
||||
std::set_difference(
|
||||
curRegions.begin(), curRegions.end(),
|
||||
outer.begin(), outer.end(),
|
||||
std::back_inserter(toDelay)
|
||||
);
|
||||
}
|
||||
|
||||
if(!toDelay.empty()) {
|
||||
auto& pending = remoteClient->PendingRegionUnload[worldId];
|
||||
for(const Pos::GlobalRegion& pos : toDelay) {
|
||||
// если уже ждёт выгрузки — не трогаем
|
||||
if(pending.find(pos) == pending.end())
|
||||
pending[pos] = nowTick + kRegionUnloadDelayTicks;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 2) Отложенная выгрузка: если истекла задержка — реально удаляем регион из зоны видимости
|
||||
if(!remoteClient->PendingRegionUnload.empty()) {
|
||||
std::unordered_map<WorldId_t, std::vector<Pos::GlobalRegion>> expiredByWorld;
|
||||
|
||||
for(auto itWorld = remoteClient->PendingRegionUnload.begin(); itWorld != remoteClient->PendingRegionUnload.end(); ) {
|
||||
const WorldId_t worldId = itWorld->first;
|
||||
auto& regMap = itWorld->second;
|
||||
|
||||
std::vector<Pos::GlobalRegion> expired;
|
||||
for(auto itReg = regMap.begin(); itReg != regMap.end(); ) {
|
||||
if(itReg->second <= nowTick) {
|
||||
expired.push_back(itReg->first);
|
||||
itReg = regMap.erase(itReg);
|
||||
} else {
|
||||
++itReg;
|
||||
}
|
||||
}
|
||||
|
||||
if(!expired.empty()) {
|
||||
std::sort(expired.begin(), expired.end());
|
||||
expiredByWorld[worldId] = std::move(expired);
|
||||
}
|
||||
|
||||
if(regMap.empty())
|
||||
itWorld = remoteClient->PendingRegionUnload.erase(itWorld);
|
||||
else
|
||||
++itWorld;
|
||||
}
|
||||
|
||||
// Применяем выгрузку: отписка + сообщение клиенту + актуализация ContentViewState
|
||||
for(auto& [worldId, expired] : expiredByWorld) {
|
||||
// Удаляем регионы из состояния клиента
|
||||
auto itCur = remoteClient->ContentViewState.Regions.find(worldId);
|
||||
if(itCur != remoteClient->ContentViewState.Regions.end()) {
|
||||
std::vector<Pos::GlobalRegion> kept;
|
||||
kept.reserve(itCur->second.size());
|
||||
|
||||
std::set_difference(
|
||||
itCur->second.begin(), itCur->second.end(),
|
||||
expired.begin(), expired.end(),
|
||||
std::back_inserter(kept)
|
||||
);
|
||||
|
||||
itCur->second = std::move(kept);
|
||||
}
|
||||
|
||||
// Сообщаем клиенту и мирам
|
||||
remoteClient->prepareRegionsRemove(worldId, expired);
|
||||
|
||||
auto iterWorld = Expanse.Worlds.find(worldId);
|
||||
if(iterWorld != Expanse.Worlds.end()) {
|
||||
iterWorld->second->onRemoteClient_RegionsLost(worldId, remoteClient, expired);
|
||||
}
|
||||
|
||||
// Если в мире больше нет наблюдаемых регионов — удалить мир у клиента
|
||||
auto itStateWorld = remoteClient->ContentViewState.Regions.find(worldId);
|
||||
if(itStateWorld != remoteClient->ContentViewState.Regions.end() && itStateWorld->second.empty()) {
|
||||
remoteClient->ContentViewState.Regions.erase(itStateWorld);
|
||||
remoteClient->prepareWorldRemove(worldId);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
for(auto& [worldId, regions] : toDB.Load) {
|
||||
std::sort(regions.begin(), regions.end());
|
||||
auto eraseIter = std::unique(regions.begin(), regions.end());
|
||||
@@ -1795,7 +1936,7 @@ IWorldSaveBackend::TickSyncInfo_Out GameServer::stepDatabaseSync() {
|
||||
// Обзавелись списком на прогрузку регионов
|
||||
// Теперь узнаем что нужно сохранить и что из регионов было выгружено
|
||||
for(auto& [worldId, world] : Expanse.Worlds) {
|
||||
World::SaveUnloadInfo info = world->onStepDatabaseSync();
|
||||
World::SaveUnloadInfo info = world->onStepDatabaseSync(Content.CM, CurrentTickDuration);
|
||||
|
||||
if(!info.ToSave.empty()) {
|
||||
auto &obj = toDB.ToSave[worldId];
|
||||
@@ -1809,7 +1950,18 @@ IWorldSaveBackend::TickSyncInfo_Out GameServer::stepDatabaseSync() {
|
||||
}
|
||||
|
||||
// Синхронизируемся с базой
|
||||
return SaveBackend.World->tickSync(std::move(toDB));
|
||||
const auto loadFallback = toDB.Load;
|
||||
try {
|
||||
return SaveBackend.World->tickSync(std::move(toDB));
|
||||
} catch(const std::exception& exc) {
|
||||
LOG.error() << "Ошибка tickSync: " << exc.what();
|
||||
} catch(...) {
|
||||
LOG.error() << "Неизвестная ошибка tickSync";
|
||||
}
|
||||
|
||||
IWorldSaveBackend::TickSyncInfo_Out out;
|
||||
out.NotExisten = loadFallback;
|
||||
return out;
|
||||
}
|
||||
|
||||
void GameServer::stepGeneratorAndLuaAsync(IWorldSaveBackend::TickSyncInfo_Out db) {
|
||||
@@ -1849,10 +2001,62 @@ void GameServer::stepGeneratorAndLuaAsync(IWorldSaveBackend::TickSyncInfo_Out db
|
||||
// Обработка идентификаторов на стороне луа
|
||||
|
||||
// Трансформация полученных ключей в профили сервера
|
||||
auto buildRemap = [&](EnumDefContent type, const std::vector<std::string>& idToKey) {
|
||||
std::vector<ResourceId> remap;
|
||||
remap.resize(idToKey.size());
|
||||
for(size_t i = 0; i < idToKey.size(); ++i) {
|
||||
if(idToKey[i].empty()) {
|
||||
remap[i] = static_cast<ResourceId>(i);
|
||||
continue;
|
||||
}
|
||||
auto [domain, key] = parseDomainKey(std::string_view(idToKey[i]));
|
||||
remap[i] = Content.CM.getId(type, domain, key);
|
||||
}
|
||||
return remap;
|
||||
};
|
||||
|
||||
auto remapRegion = [&](DB_Region_Out& region) {
|
||||
std::vector<ResourceId> voxelRemap;
|
||||
std::vector<ResourceId> nodeRemap;
|
||||
std::vector<ResourceId> entityRemap;
|
||||
|
||||
if(!region.VoxelIdToKey.empty())
|
||||
voxelRemap = buildRemap(EnumDefContent::Voxel, region.VoxelIdToKey);
|
||||
if(!region.NodeIdToKey.empty())
|
||||
nodeRemap = buildRemap(EnumDefContent::Node, region.NodeIdToKey);
|
||||
if(!region.EntityToKey.empty())
|
||||
entityRemap = buildRemap(EnumDefContent::Entity, region.EntityToKey);
|
||||
|
||||
if(!voxelRemap.empty()) {
|
||||
for(auto& voxel : region.Voxels) {
|
||||
if(voxel.VoxelId < voxelRemap.size())
|
||||
voxel.VoxelId = voxelRemap[voxel.VoxelId];
|
||||
}
|
||||
}
|
||||
|
||||
if(!nodeRemap.empty()) {
|
||||
for(auto& chunk : region.Nodes) {
|
||||
for(auto& node : chunk) {
|
||||
if(node.NodeId < nodeRemap.size())
|
||||
node.NodeId = nodeRemap[node.NodeId];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if(!entityRemap.empty()) {
|
||||
for(auto& entity : region.Entityes) {
|
||||
auto id = entity.getDefId();
|
||||
if(id < entityRemap.size())
|
||||
entity.setDefId(entityRemap[id]);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
for(auto& [WorldId_t, regions] : db.LoadedRegions) {
|
||||
auto &list = toLoadRegions[WorldId_t];
|
||||
|
||||
for(auto& [pos, region] : regions) {
|
||||
remapRegion(region);
|
||||
auto &obj = list.emplace_back(pos, World::RegionIn()).second;
|
||||
convertRegionVoxelsToChunks(region.Voxels, obj.Voxels);
|
||||
obj.Nodes = std::move(region.Nodes);
|
||||
@@ -2463,6 +2667,7 @@ void GameServer::stepSyncContent() {
|
||||
n.NodeId = 4;
|
||||
n.Meta = uint8_t((int(nPos.x) + int(nPos.y) + int(nPos.z)) & 0x3);
|
||||
region->second->IsChunkChanged_Nodes |= 1ull << cPos.pack();
|
||||
region->second->IsChanged = true;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2480,6 +2685,7 @@ void GameServer::stepSyncContent() {
|
||||
n.NodeId = 0;
|
||||
n.Meta = 0;
|
||||
region->second->IsChunkChanged_Nodes |= 1ull << cPos.pack();
|
||||
region->second->IsChanged = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -2495,9 +2701,13 @@ void GameServer::stepSyncContent() {
|
||||
|
||||
std::vector<Net::Packet> packetsToAll;
|
||||
{
|
||||
AssetsPreloader::Out_bakeId baked = Content.AM.bakeIdTables();
|
||||
if(hasAnyBindings(baked.IdToDK)) {
|
||||
packetsToAll.push_back(RemoteClient::makePacket_informateAssets_DK(baked.IdToDK));
|
||||
std::array<
|
||||
std::vector<AssetsManager::BindDomainKeyInfo>,
|
||||
static_cast<size_t>(EnumAssets::MAX_ENUM)
|
||||
> baked = Content.AM.bake();
|
||||
|
||||
if(hasAnyBindings(baked)) {
|
||||
packetsToAll.push_back(RemoteClient::makePacket_informateAssets_DK(baked));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2515,30 +2725,8 @@ void GameServer::stepSyncContent() {
|
||||
}
|
||||
};
|
||||
|
||||
std::vector<AssetBinaryInfo> binaryResources;
|
||||
for(const Hash_t& hash : full.Hashes) {
|
||||
std::optional<
|
||||
std::tuple<EnumAssets, uint32_t, const AssetsPreloader::MediaResource*>
|
||||
> result = Content.AM.getResource(hash);
|
||||
|
||||
if(!result)
|
||||
continue;
|
||||
|
||||
auto& [type, id, media] = *result;
|
||||
LOG.debug() << "Server sending type=" << assetTypeName(type)
|
||||
<< " id=" << id
|
||||
<< " key=" << media->Domain << ':' << media->Key
|
||||
<< " hash=" << int(media->Hash[0]) << '.'
|
||||
<< int(media->Hash[1]) << '.'
|
||||
<< int(media->Hash[2]) << '.'
|
||||
<< int(media->Hash[3])
|
||||
<< " size=" << media->Resource->size();
|
||||
Resource resource(*media->Resource);
|
||||
binaryResources.push_back(AssetBinaryInfo{
|
||||
.Data = std::move(resource),
|
||||
.Hash = media->Hash
|
||||
});
|
||||
}
|
||||
std::vector<std::tuple<ResourceFile::Hash_t, std::shared_ptr<const std::u8string>>> binaryResources
|
||||
= Content.AM.getResources(full.Hashes);
|
||||
|
||||
for(std::shared_ptr<RemoteClient>& remoteClient : Game.RemoteClients) {
|
||||
if(!binaryResources.empty())
|
||||
|
||||
@@ -6,15 +6,14 @@
|
||||
#include <Common/Net.hpp>
|
||||
#include <Common/Lockable.hpp>
|
||||
#include <atomic>
|
||||
#include <barrier>
|
||||
#include <boost/asio/any_io_executor.hpp>
|
||||
#include <boost/asio/io_context.hpp>
|
||||
#include <condition_variable>
|
||||
#include <filesystem>
|
||||
#include "Common/Abstract.hpp"
|
||||
#include "RemoteClient.hpp"
|
||||
#include "Server/Abstract.hpp"
|
||||
#include <TOSLib.hpp>
|
||||
#include <functional>
|
||||
#include <memory>
|
||||
#include <queue>
|
||||
#include <set>
|
||||
@@ -25,6 +24,7 @@
|
||||
|
||||
#include "WorldDefManager.hpp"
|
||||
#include "ContentManager.hpp"
|
||||
#include "AssetsManager.hpp"
|
||||
#include "World.hpp"
|
||||
|
||||
#include "SaveBackend.hpp"
|
||||
@@ -73,7 +73,7 @@ class GameServer : public AsyncObject {
|
||||
|
||||
struct ContentObj {
|
||||
public:
|
||||
AssetsPreloader AM;
|
||||
AssetsManager AM;
|
||||
ContentManager CM;
|
||||
|
||||
// Если контент был перерегистрирован (исключая двоичные ресурсы), то профили будут повторно разосланы
|
||||
@@ -88,6 +88,8 @@ class GameServer : public AsyncObject {
|
||||
struct {
|
||||
std::vector<std::shared_ptr<RemoteClient>> RemoteClients;
|
||||
ServerTime AfterStartTime = {0, 0};
|
||||
// Счётчик тактов (увеличивается на 1 каждый тик в GameServer::run)
|
||||
uint32_t Tick = 0;
|
||||
|
||||
} Game;
|
||||
|
||||
@@ -156,38 +158,56 @@ class GameServer : public AsyncObject {
|
||||
*/
|
||||
struct BackingChunkPressure_t {
|
||||
TOS::Logger LOG = "BackingChunkPressure";
|
||||
volatile bool NeedShutdown = false;
|
||||
std::atomic<bool> NeedShutdown = false;
|
||||
std::vector<std::thread> Threads;
|
||||
std::mutex Mutex;
|
||||
volatile int RunCollect = 0, RunCompress = 0, Iteration = 0;
|
||||
std::condition_variable Symaphore;
|
||||
std::unique_ptr<std::barrier<>> CollectStart;
|
||||
std::unique_ptr<std::barrier<>> CollectEnd;
|
||||
std::unique_ptr<std::barrier<>> CompressEnd;
|
||||
std::unordered_map<WorldId_t, std::unique_ptr<World>> *Worlds;
|
||||
bool HasStarted = false;
|
||||
|
||||
void init(size_t threadCount) {
|
||||
if(threadCount == 0)
|
||||
return;
|
||||
|
||||
const ptrdiff_t participants = static_cast<ptrdiff_t>(threadCount + 1);
|
||||
CollectStart = std::make_unique<std::barrier<>>(participants);
|
||||
CollectEnd = std::make_unique<std::barrier<>>(participants);
|
||||
CompressEnd = std::make_unique<std::barrier<>>(participants);
|
||||
}
|
||||
|
||||
void startCollectChanges() {
|
||||
std::lock_guard<std::mutex> lock(Mutex);
|
||||
RunCollect = Threads.size();
|
||||
RunCompress = Threads.size();
|
||||
Iteration += 1;
|
||||
assert(RunCollect != 0);
|
||||
Symaphore.notify_all();
|
||||
if(!CollectStart)
|
||||
return;
|
||||
HasStarted = true;
|
||||
CollectStart->arrive_and_wait();
|
||||
}
|
||||
|
||||
void endCollectChanges() {
|
||||
std::unique_lock<std::mutex> lock(Mutex);
|
||||
Symaphore.wait(lock, [&](){ return RunCollect == 0 || NeedShutdown; });
|
||||
if(!CollectEnd)
|
||||
return;
|
||||
if(!HasStarted)
|
||||
return;
|
||||
CollectEnd->arrive_and_wait();
|
||||
}
|
||||
|
||||
void endWithResults() {
|
||||
std::unique_lock<std::mutex> lock(Mutex);
|
||||
Symaphore.wait(lock, [&](){ return RunCompress == 0 || NeedShutdown; });
|
||||
if(!CompressEnd)
|
||||
return;
|
||||
if(!HasStarted)
|
||||
return;
|
||||
CompressEnd->arrive_and_wait();
|
||||
}
|
||||
|
||||
void stop() {
|
||||
{
|
||||
std::unique_lock<std::mutex> lock(Mutex);
|
||||
NeedShutdown = true;
|
||||
Symaphore.notify_all();
|
||||
}
|
||||
NeedShutdown.store(true, std::memory_order_release);
|
||||
|
||||
if(CollectStart)
|
||||
CollectStart->arrive_and_drop();
|
||||
if(CollectEnd)
|
||||
CollectEnd->arrive_and_drop();
|
||||
if(CompressEnd)
|
||||
CompressEnd->arrive_and_drop();
|
||||
|
||||
for(std::thread& thread : Threads)
|
||||
thread.join();
|
||||
@@ -234,7 +254,7 @@ class GameServer : public AsyncObject {
|
||||
|
||||
auto lock = Output.lock();
|
||||
std::vector<std::pair<NoiseKey, std::array<float, 64*64*64>>> out = std::move(*lock);
|
||||
lock->reserve(8000);
|
||||
lock->reserve(25);
|
||||
|
||||
return std::move(out);
|
||||
}
|
||||
@@ -249,6 +269,13 @@ class GameServer : public AsyncObject {
|
||||
std::vector<std::thread> Threads;
|
||||
TOS::SpinlockObject<std::queue<std::pair<BackingNoiseGenerator_t::NoiseKey, std::array<float, 64*64*64>>>> NoiseIn;
|
||||
TOS::SpinlockObject<std::vector<std::pair<BackingNoiseGenerator_t::NoiseKey, World::RegionIn>>> RegionOut;
|
||||
ContentManager &CM;
|
||||
|
||||
BackingAsyncLua_t(ContentManager& cm)
|
||||
: CM(cm)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
void stop() {
|
||||
NeedShutdown = true;
|
||||
|
||||
@@ -11,13 +11,14 @@
|
||||
#include <boost/system/system_error.hpp>
|
||||
#include <exception>
|
||||
#include <Common/Packets.hpp>
|
||||
#include <unordered_set>
|
||||
|
||||
|
||||
namespace LV::Server {
|
||||
|
||||
Net::Packet RemoteClient::makePacket_informateAssets_DK(
|
||||
const std::array<
|
||||
std::vector<AssetsPreloader::BindDomainKeyInfo>,
|
||||
std::vector<AssetsManager::BindDomainKeyInfo>,
|
||||
static_cast<size_t>(EnumAssets::MAX_ENUM)
|
||||
>& dkVector
|
||||
) {
|
||||
@@ -46,7 +47,7 @@ Net::Packet RemoteClient::makePacket_informateAssets_DK(
|
||||
|
||||
// Запись связок домен+ключ
|
||||
for(size_t type = 0; type < static_cast<size_t>(EnumAssets::MAX_ENUM); type++) {
|
||||
const std::vector<AssetsPreloader::BindDomainKeyInfo>& binds = dkVector[type];
|
||||
const std::vector<AssetsManager::BindDomainKeyInfo>& binds = dkVector[type];
|
||||
pack << uint32_t(binds.size());
|
||||
|
||||
for(const auto& bind : binds) {
|
||||
@@ -67,7 +68,7 @@ Net::Packet RemoteClient::makePacket_informateAssets_DK(
|
||||
|
||||
Net::Packet RemoteClient::makePacket_informateAssets_HH(
|
||||
const std::array<
|
||||
std::vector<AssetsPreloader::BindHashHeaderInfo>,
|
||||
std::vector<AssetsManager::BindHashHeaderInfo>,
|
||||
static_cast<size_t>(EnumAssets::MAX_ENUM)
|
||||
>& hhVector,
|
||||
const std::array<
|
||||
@@ -93,24 +94,122 @@ Net::Packet RemoteClient::makePacket_informateAssets_HH(
|
||||
return pack;
|
||||
}
|
||||
|
||||
std::vector<Net::Packet> RemoteClient::makePackets_sendDefContentUpdate(
|
||||
std::array<
|
||||
std::vector<
|
||||
std::pair<
|
||||
ResourceId, // Идентификатор профиля
|
||||
std::u8string // Двоичный формат профиля
|
||||
>
|
||||
>,
|
||||
static_cast<size_t>(EnumDefContent::MAX_ENUM)
|
||||
> newOrUpdate, // Новые или изменённые
|
||||
std::array<
|
||||
std::vector<ResourceId>,
|
||||
static_cast<size_t>(EnumDefContent::MAX_ENUM)
|
||||
> lost, // Потерянные профили
|
||||
std::array<
|
||||
std::vector<std::pair<std::string, std::string>>,
|
||||
static_cast<size_t>(EnumDefContent::MAX_ENUM)
|
||||
> idToDK // Новые привязки
|
||||
std::vector<Net::Packet> RemoteClient::makePackets_informateDefContent_Full(
|
||||
const ContentManager::Out_getAllProfiles& profiles
|
||||
) {
|
||||
std::vector<Net::Packet> packets;
|
||||
Net::Packet pack;
|
||||
|
||||
auto check = [&](size_t needSize) {
|
||||
if(pack.size()+needSize > 65500) {
|
||||
packets.emplace_back(std::move(pack));
|
||||
pack.clear();
|
||||
}
|
||||
};
|
||||
|
||||
pack << (uint8_t) ToClient::DefinitionsFull;
|
||||
|
||||
{
|
||||
pack << (uint32_t) profiles.ProfilesIds_Voxel.size();
|
||||
for(uint32_t id : profiles.ProfilesIds_Voxel) {
|
||||
check(4);
|
||||
pack << id;
|
||||
}
|
||||
|
||||
for(const auto& profile : profiles.Profiles_Voxel) {
|
||||
std::u8string data = profile->dumpToClient();
|
||||
check(data.size());
|
||||
pack << std::string_view((const char*) data.data(), data.size());
|
||||
}
|
||||
}
|
||||
|
||||
{
|
||||
pack << (uint32_t) profiles.ProfilesIds_Node.size();
|
||||
for(uint32_t id : profiles.ProfilesIds_Node) {
|
||||
check(4);
|
||||
pack << id;
|
||||
}
|
||||
|
||||
for(const auto& profile : profiles.Profiles_Node) {
|
||||
std::u8string data = profile->dumpToClient();
|
||||
check(data.size());
|
||||
pack << std::string_view((const char*) data.data(), data.size());
|
||||
}
|
||||
}
|
||||
|
||||
{
|
||||
pack << (uint32_t) profiles.ProfilesIds_World.size();
|
||||
for(uint32_t id : profiles.ProfilesIds_World) {
|
||||
check(4);
|
||||
pack << id;
|
||||
}
|
||||
|
||||
for(const auto& profile : profiles.Profiles_World) {
|
||||
std::u8string data = profile->dumpToClient();
|
||||
check(data.size());
|
||||
pack << std::string_view((const char*) data.data(), data.size());
|
||||
}
|
||||
}
|
||||
|
||||
{
|
||||
pack << (uint32_t) profiles.ProfilesIds_Portal.size();
|
||||
for(uint32_t id : profiles.ProfilesIds_Portal) {
|
||||
check(4);
|
||||
pack << id;
|
||||
}
|
||||
|
||||
for(const auto& profile : profiles.Profiles_Portal) {
|
||||
std::u8string data = profile->dumpToClient();
|
||||
check(data.size());
|
||||
pack << std::string_view((const char*) data.data(), data.size());
|
||||
}
|
||||
}
|
||||
|
||||
{
|
||||
pack << (uint32_t) profiles.ProfilesIds_Entity.size();
|
||||
for(uint32_t id : profiles.ProfilesIds_Entity) {
|
||||
check(4);
|
||||
pack << id;
|
||||
}
|
||||
|
||||
for(const auto& profile : profiles.Profiles_Entity) {
|
||||
std::u8string data = profile->dumpToClient();
|
||||
check(data.size());
|
||||
pack << std::string_view((const char*) data.data(), data.size());
|
||||
}
|
||||
}
|
||||
|
||||
{
|
||||
pack << (uint32_t) profiles.ProfilesIds_Item.size();
|
||||
for(uint32_t id : profiles.ProfilesIds_Item) {
|
||||
check(4);
|
||||
pack << id;
|
||||
}
|
||||
|
||||
for(const auto& profile : profiles.Profiles_Item) {
|
||||
std::u8string data = profile->dumpToClient();
|
||||
check(data.size());
|
||||
pack << std::string_view((const char*) data.data(), data.size());
|
||||
}
|
||||
}
|
||||
|
||||
for(size_t type = 0; type < static_cast<size_t>(EnumDefContent::MAX_ENUM); type++) {
|
||||
pack << uint32_t(profiles.IdToDK[type].size());
|
||||
|
||||
for(const auto& [domain, key] : profiles.IdToDK[type]) {
|
||||
check(domain.size() + key.size() + 8);
|
||||
pack << domain << key;
|
||||
}
|
||||
}
|
||||
|
||||
if(pack.size())
|
||||
packets.emplace_back(std::move(pack));
|
||||
|
||||
return packets;
|
||||
}
|
||||
|
||||
std::vector<Net::Packet> RemoteClient::makePackets_informateDefContentUpdate(
|
||||
const ContentManager::Out_buildEndProfiles& profiles
|
||||
) {
|
||||
std::vector<Net::Packet> packets;
|
||||
Net::Packet pack;
|
||||
@@ -123,33 +222,86 @@ std::vector<Net::Packet> RemoteClient::makePackets_sendDefContentUpdate(
|
||||
};
|
||||
|
||||
pack << (uint8_t) ToClient::DefinitionsUpdate;
|
||||
pack << uint32_t(newOrUpdate.size());
|
||||
for(size_t type = 0; type < static_cast<size_t>(EnumDefContent::MAX_ENUM); type++) {
|
||||
pack << uint32_t(newOrUpdate[type].size());
|
||||
|
||||
for(const auto& [id, data] : newOrUpdate[type]) {
|
||||
check(data.size());
|
||||
pack << id << (const std::string&) data;
|
||||
}
|
||||
}
|
||||
|
||||
pack << uint32_t(lost.size());
|
||||
for(size_t type = 0; type < static_cast<size_t>(EnumDefContent::MAX_ENUM); type++) {
|
||||
pack << uint32_t(lost[type].size());
|
||||
|
||||
for(ResourceId id : lost[type]) {
|
||||
check(4);
|
||||
{
|
||||
pack << uint32_t(profiles.ChangedProfiles_Voxel.size());
|
||||
for(const auto& [id, profile] : profiles.ChangedProfiles_Voxel) {
|
||||
pack << id;
|
||||
std::u8string data = profile->dumpToClient();
|
||||
check(data.size());
|
||||
pack << std::string_view((const char*) data.data(), data.size());
|
||||
}
|
||||
}
|
||||
|
||||
pack << uint32_t(idToDK.size());
|
||||
for(size_t type = 0; type < static_cast<size_t>(EnumDefContent::MAX_ENUM); type++) {
|
||||
pack << uint32_t(idToDK[type].size());
|
||||
{
|
||||
pack << uint32_t(profiles.ChangedProfiles_Node.size());
|
||||
for(const auto& [id, profile] : profiles.ChangedProfiles_Node) {
|
||||
pack << id;
|
||||
std::u8string data = profile->dumpToClient();
|
||||
check(data.size());
|
||||
pack << std::string_view((const char*) data.data(), data.size());
|
||||
}
|
||||
}
|
||||
|
||||
for(const auto& [domain, key] : idToDK[type]) {
|
||||
check(domain.size() + key.size() + 8);
|
||||
pack << key << domain;
|
||||
{
|
||||
pack << uint32_t(profiles.ChangedProfiles_World.size());
|
||||
for(const auto& [id, profile] : profiles.ChangedProfiles_World) {
|
||||
pack << id;
|
||||
std::u8string data = profile->dumpToClient();
|
||||
check(data.size());
|
||||
pack << std::string_view((const char*) data.data(), data.size());
|
||||
}
|
||||
}
|
||||
|
||||
{
|
||||
pack << uint32_t(profiles.ChangedProfiles_Portal.size());
|
||||
for(const auto& [id, profile] : profiles.ChangedProfiles_Portal) {
|
||||
pack << id;
|
||||
std::u8string data = profile->dumpToClient();
|
||||
check(data.size());
|
||||
pack << std::string_view((const char*) data.data(), data.size());
|
||||
}
|
||||
}
|
||||
|
||||
{
|
||||
pack << uint32_t(profiles.ChangedProfiles_Entity.size());
|
||||
for(const auto& [id, profile] : profiles.ChangedProfiles_Entity) {
|
||||
pack << id;
|
||||
std::u8string data = profile->dumpToClient();
|
||||
check(data.size());
|
||||
pack << std::string_view((const char*) data.data(), data.size());
|
||||
}
|
||||
}
|
||||
|
||||
{
|
||||
pack << uint32_t(profiles.ChangedProfiles_Item.size());
|
||||
for(const auto& [id, profile] : profiles.ChangedProfiles_Item) {
|
||||
pack << id;
|
||||
std::u8string data = profile->dumpToClient();
|
||||
check(data.size());
|
||||
pack << std::string_view((const char*) data.data(), data.size());
|
||||
}
|
||||
}
|
||||
|
||||
{
|
||||
for(size_t type = 0; type < static_cast<size_t>(EnumDefContent::MAX_ENUM); type++) {
|
||||
pack << uint32_t(profiles.LostProfiles[type].size());
|
||||
|
||||
for(const ResourceId id : profiles.LostProfiles[type]) {
|
||||
check(sizeof(ResourceId));
|
||||
pack << id;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
{
|
||||
for(size_t type = 0; type < static_cast<size_t>(EnumDefContent::MAX_ENUM); type++) {
|
||||
pack << uint32_t(profiles.IdToDK[type].size());
|
||||
|
||||
for(const auto& [domain, key] : profiles.IdToDK[type]) {
|
||||
check(domain.size() + key.size() + 8);
|
||||
pack << key << domain;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -430,21 +582,21 @@ ResourceRequest RemoteClient::pushPreparedPackets() {
|
||||
return std::move(nextRequest);
|
||||
}
|
||||
|
||||
void RemoteClient::informateBinaryAssets(const std::vector<AssetBinaryInfo>& resources)
|
||||
void RemoteClient::informateBinaryAssets(const std::vector<std::tuple<ResourceFile::Hash_t, std::shared_ptr<const std::u8string>>>& resources)
|
||||
{
|
||||
for(const AssetBinaryInfo& resource : resources) {
|
||||
for(const auto& [hash, resource] : resources) {
|
||||
auto lock = NetworkAndResource.lock();
|
||||
auto iter = std::find(lock->ClientRequested.begin(), lock->ClientRequested.end(), resource.Hash);
|
||||
auto iter = std::find(lock->ClientRequested.begin(), lock->ClientRequested.end(), hash);
|
||||
if(iter == lock->ClientRequested.end())
|
||||
continue;
|
||||
|
||||
lock->ClientRequested.erase(iter);
|
||||
lock.unlock();
|
||||
|
||||
auto it = std::lower_bound(AssetsInWork.OnClient.begin(), AssetsInWork.OnClient.end(), resource.Hash);
|
||||
if(it == AssetsInWork.OnClient.end() || *it != resource.Hash) {
|
||||
AssetsInWork.OnClient.insert(it, resource.Hash);
|
||||
AssetsInWork.ToSend.emplace_back(resource.Data, 0);
|
||||
auto it = std::lower_bound(AssetsInWork.OnClient.begin(), AssetsInWork.OnClient.end(), hash);
|
||||
if(it == AssetsInWork.OnClient.end() || *it != hash) {
|
||||
AssetsInWork.OnClient.insert(it, hash);
|
||||
AssetsInWork.ToSend.emplace_back(hash, resource, 0);
|
||||
} else {
|
||||
LOG.warn() << "Клиент повторно запросил имеющийся у него ресурс";
|
||||
}
|
||||
@@ -611,36 +763,36 @@ void RemoteClient::onUpdate() {
|
||||
|
||||
bool hasFullSended = false;
|
||||
|
||||
for(auto& [res, sended] : toSend) {
|
||||
for(auto& [hash, res, sended] : toSend) {
|
||||
if(sended == 0) {
|
||||
// Оповещаем о начале отправки ресурса
|
||||
const size_t initSize = 1 + 1 + 4 + 32 + 4 + 1;
|
||||
if(p.size() + initSize > kMaxAssetPacketSize)
|
||||
flushAssetsPacket();
|
||||
p << (uint8_t) ToClient::AssetsInitSend
|
||||
<< uint32_t(res.size());
|
||||
p.write((const std::byte*) res.hash().data(), 32);
|
||||
<< uint32_t(res->size());
|
||||
p.write((const std::byte*) hash.data(), 32);
|
||||
}
|
||||
|
||||
// Отправляем чанк
|
||||
size_t willSend = std::min(chunkSize, res.size()-sended);
|
||||
size_t willSend = std::min(chunkSize, res->size()-sended);
|
||||
const size_t chunkMsgSize = 1 + 1 + 32 + 4 + willSend;
|
||||
if(p.size() + chunkMsgSize > kMaxAssetPacketSize)
|
||||
flushAssetsPacket();
|
||||
p << (uint8_t) ToClient::AssetsNextSend;
|
||||
p.write((const std::byte*) res.hash().data(), 32);
|
||||
p.write((const std::byte*) hash.data(), 32);
|
||||
p << uint32_t(willSend);
|
||||
p.write(res.data() + sended, willSend);
|
||||
p.write((const std::byte*) res->data() + sended, willSend);
|
||||
sended += willSend;
|
||||
|
||||
if(sended == res.size()) {
|
||||
if(sended == res->size()) {
|
||||
hasFullSended = true;
|
||||
}
|
||||
}
|
||||
|
||||
if(hasFullSended) {
|
||||
for(ssize_t iter = toSend.size()-1; iter >= 0; iter--) {
|
||||
if(std::get<0>(toSend[iter]).size() == std::get<1>(toSend[iter])) {
|
||||
if(std::get<1>(toSend[iter])->size() == std::get<2>(toSend[iter])) {
|
||||
toSend.erase(toSend.begin()+iter);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,6 +5,7 @@
|
||||
#include <Common/Net.hpp>
|
||||
#include "Abstract.hpp"
|
||||
#include "Common/Packets.hpp"
|
||||
#include "Server/AssetsManager.hpp"
|
||||
#include "Server/ContentManager.hpp"
|
||||
#include <Common/Abstract.hpp>
|
||||
#include <bitset>
|
||||
@@ -256,7 +257,7 @@ class RemoteClient {
|
||||
std::vector<Hash_t> OnClient;
|
||||
// Отправляемые на клиент ресурсы
|
||||
// Ресурс, количество отправленных байт
|
||||
std::vector<std::tuple<Resource, size_t>> ToSend;
|
||||
std::vector<std::tuple<ResourceFile::Hash_t, std::shared_ptr<const std::u8string>, size_t>> ToSend;
|
||||
// Пакет с ресурсами
|
||||
std::vector<Net::Packet> AssetsPackets;
|
||||
Net::Packet AssetsPacket;
|
||||
@@ -276,6 +277,10 @@ public:
|
||||
ContentViewInfo ContentViewState;
|
||||
// Если игрок пересекал границы региона (для перерасчёта ContentViewState)
|
||||
bool CrossedRegion = true;
|
||||
|
||||
// Отложенная выгрузка регионов (гистерезис + задержка)
|
||||
// worldId -> (regionPos -> tick_deadline)
|
||||
std::unordered_map<WorldId_t, std::unordered_map<Pos::GlobalRegion, uint32_t>> PendingRegionUnload;
|
||||
std::queue<Pos::GlobalNode> Build, Break;
|
||||
std::optional<ServerEntityId_t> PlayerEntity;
|
||||
|
||||
@@ -361,7 +366,7 @@ public:
|
||||
// Создаёт пакет для всех игроков с оповещением о новых идентификаторах (id -> domain+key)
|
||||
static Net::Packet makePacket_informateAssets_DK(
|
||||
const std::array<
|
||||
std::vector<AssetsPreloader::BindDomainKeyInfo>,
|
||||
std::vector<AssetsManager::BindDomainKeyInfo>,
|
||||
static_cast<size_t>(EnumAssets::MAX_ENUM)
|
||||
>& dkVector
|
||||
);
|
||||
@@ -369,7 +374,7 @@ public:
|
||||
// Создаёт пакет для всех игроков с оповещением об изменении файлов ресурсов (id -> hash+header)
|
||||
static Net::Packet makePacket_informateAssets_HH(
|
||||
const std::array<
|
||||
std::vector<AssetsPreloader::BindHashHeaderInfo>,
|
||||
std::vector<AssetsManager::BindHashHeaderInfo>,
|
||||
static_cast<size_t>(EnumAssets::MAX_ENUM)
|
||||
>& hhVector,
|
||||
const std::array<
|
||||
@@ -380,28 +385,18 @@ public:
|
||||
|
||||
// Оповещение о двоичных ресурсах (стриминг по запросу)
|
||||
void informateBinaryAssets(
|
||||
const std::vector<AssetBinaryInfo>& resources
|
||||
const std::vector<std::tuple<ResourceFile::Hash_t, std::shared_ptr<const std::u8string>>>& resources
|
||||
);
|
||||
|
||||
|
||||
// Создаёт пакет со всеми данными об игровых профилях
|
||||
static std::vector<Net::Packet> makePackets_informateDefContent_Full(
|
||||
const ContentManager::Out_getAllProfiles& profiles
|
||||
);
|
||||
|
||||
// Создаёт пакет об обновлении игровых профилей
|
||||
static std::vector<Net::Packet> makePackets_sendDefContentUpdate(
|
||||
std::array<
|
||||
std::vector<
|
||||
std::pair<
|
||||
ResourceId, // Идентификатор профиля
|
||||
std::u8string // Двоичный формат профиля
|
||||
>
|
||||
>,
|
||||
static_cast<size_t>(EnumDefContent::MAX_ENUM)
|
||||
> newOrUpdate, // Новые или изменённые
|
||||
std::array<
|
||||
std::vector<ResourceId>,
|
||||
static_cast<size_t>(EnumDefContent::MAX_ENUM)
|
||||
> lost, // Потерянные профили
|
||||
std::array<
|
||||
std::vector<std::pair<std::string, std::string>>,
|
||||
static_cast<size_t>(EnumDefContent::MAX_ENUM)
|
||||
> idToDK // Новые привязки
|
||||
static std::vector<Net::Packet> makePackets_informateDefContentUpdate(
|
||||
const ContentManager::Out_buildEndProfiles& profiles
|
||||
);
|
||||
|
||||
void onUpdate();
|
||||
|
||||
@@ -16,7 +16,7 @@ namespace LV::Server {
|
||||
*/
|
||||
struct SB_Region_In {
|
||||
// Список вокселей всех чанков
|
||||
std::unordered_map<Pos::bvec4u, VoxelCube> Voxels;
|
||||
std::unordered_map<Pos::bvec4u, std::vector<VoxelCube>> Voxels;
|
||||
// Привязка вокселей к ключу профиля
|
||||
std::vector<std::pair<DefVoxelId, std::string>> VoxelsMap;
|
||||
// Ноды всех чанков
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
#include "Filesystem.hpp"
|
||||
#include "Server/Abstract.hpp"
|
||||
#include "Server/SaveBackend.hpp"
|
||||
#include "TOSLib.hpp"
|
||||
#include <boost/json/array.hpp>
|
||||
#include <boost/json/object.hpp>
|
||||
#include <boost/json/parse.hpp>
|
||||
@@ -11,12 +12,277 @@
|
||||
#include <filesystem>
|
||||
#include <fstream>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <algorithm>
|
||||
#include <cstring>
|
||||
|
||||
namespace LV::Server::SaveBackends {
|
||||
|
||||
namespace fs = std::filesystem;
|
||||
namespace js = boost::json;
|
||||
|
||||
namespace {
|
||||
|
||||
constexpr uint32_t kRegionVersion = 1;
|
||||
constexpr size_t kRegionNodeCount = 4 * 4 * 4 * 16 * 16 * 16;
|
||||
|
||||
template<typename T>
|
||||
js::object packIdMap(const std::vector<std::pair<T, std::string>>& map) {
|
||||
js::object out;
|
||||
for(const auto& [id, key] : map) {
|
||||
out[std::to_string(id)] = key;
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
void unpackIdMap(const js::object& obj, std::vector<std::string>& out) {
|
||||
size_t maxId = 0;
|
||||
for(const auto& kvp : obj) {
|
||||
try {
|
||||
maxId = std::max(maxId, static_cast<size_t>(std::stoul(kvp.key())));
|
||||
} catch(...) {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
out.assign(maxId + 1, {});
|
||||
|
||||
for(const auto& kvp : obj) {
|
||||
try {
|
||||
size_t id = std::stoul(kvp.key());
|
||||
out[id] = std::string(kvp.value().as_string());
|
||||
} catch(...) {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
std::string encodeCompressed(const uint8_t* data, size_t size) {
|
||||
std::u8string compressed = compressLinear(std::u8string_view(reinterpret_cast<const char8_t*>(data), size));
|
||||
return TOS::Enc::toBase64(reinterpret_cast<const uint8_t*>(compressed.data()), compressed.size());
|
||||
}
|
||||
|
||||
std::u8string decodeCompressed(const std::string& base64) {
|
||||
if(base64.empty())
|
||||
return {};
|
||||
|
||||
TOS::ByteBuffer buffer = TOS::Enc::fromBase64(base64);
|
||||
return unCompressLinear(std::u8string_view(reinterpret_cast<const char8_t*>(buffer.data()), buffer.size()));
|
||||
}
|
||||
|
||||
bool writeRegionFile(const fs::path& path, const SB_Region_In& data) {
|
||||
js::object jobj;
|
||||
jobj["version"] = kRegionVersion;
|
||||
|
||||
{
|
||||
std::vector<VoxelCube_Region> voxels;
|
||||
convertChunkVoxelsToRegion(data.Voxels, voxels);
|
||||
|
||||
js::object jvoxels;
|
||||
jvoxels["count"] = static_cast<uint64_t>(voxels.size());
|
||||
if(!voxels.empty()) {
|
||||
const uint8_t* raw = reinterpret_cast<const uint8_t*>(voxels.data());
|
||||
size_t rawSize = sizeof(VoxelCube_Region) * voxels.size();
|
||||
jvoxels["data"] = encodeCompressed(raw, rawSize);
|
||||
} else {
|
||||
jvoxels["data"] = "";
|
||||
}
|
||||
|
||||
jobj["voxels"] = std::move(jvoxels);
|
||||
jobj["voxels_map"] = packIdMap(data.VoxelsMap);
|
||||
}
|
||||
|
||||
{
|
||||
js::object jnodes;
|
||||
const Node* nodePtr = data.Nodes[0].data();
|
||||
const uint8_t* raw = reinterpret_cast<const uint8_t*>(nodePtr);
|
||||
size_t rawSize = sizeof(Node) * kRegionNodeCount;
|
||||
jnodes["data"] = encodeCompressed(raw, rawSize);
|
||||
jobj["nodes"] = std::move(jnodes);
|
||||
jobj["nodes_map"] = packIdMap(data.NodeMap);
|
||||
}
|
||||
|
||||
{
|
||||
js::array ents;
|
||||
for(const Entity& entity : data.Entityes) {
|
||||
js::object je;
|
||||
je["def"] = static_cast<uint64_t>(entity.getDefId());
|
||||
je["world"] = static_cast<uint64_t>(entity.WorldId);
|
||||
je["pos"] = js::array{entity.Pos.x, entity.Pos.y, entity.Pos.z};
|
||||
je["speed"] = js::array{entity.Speed.x, entity.Speed.y, entity.Speed.z};
|
||||
je["accel"] = js::array{entity.Acceleration.x, entity.Acceleration.y, entity.Acceleration.z};
|
||||
je["quat"] = js::array{entity.Quat.x, entity.Quat.y, entity.Quat.z, entity.Quat.w};
|
||||
je["hp"] = static_cast<uint64_t>(entity.HP);
|
||||
je["abbox"] = js::array{entity.ABBOX.x, entity.ABBOX.y, entity.ABBOX.z};
|
||||
je["in_region"] = js::array{entity.InRegionPos.x, entity.InRegionPos.y, entity.InRegionPos.z};
|
||||
|
||||
js::object tags;
|
||||
for(const auto& [key, value] : entity.Tags) {
|
||||
tags[key] = value;
|
||||
}
|
||||
je["tags"] = std::move(tags);
|
||||
|
||||
ents.push_back(std::move(je));
|
||||
}
|
||||
|
||||
jobj["entities"] = std::move(ents);
|
||||
jobj["entities_map"] = packIdMap(data.EntityMap);
|
||||
}
|
||||
|
||||
fs::create_directories(path.parent_path());
|
||||
std::ofstream fd(path, std::ios::binary);
|
||||
if(!fd)
|
||||
return false;
|
||||
|
||||
fd << js::serialize(jobj);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool readRegionFile(const fs::path& path, DB_Region_Out& out) {
|
||||
try {
|
||||
std::ifstream fd(path, std::ios::binary);
|
||||
if(!fd)
|
||||
return false;
|
||||
|
||||
out = {};
|
||||
|
||||
js::object jobj = js::parse(fd).as_object();
|
||||
|
||||
if(auto it = jobj.find("voxels"); it != jobj.end()) {
|
||||
const js::object& jvoxels = it->value().as_object();
|
||||
size_t count = 0;
|
||||
if(auto itCount = jvoxels.find("count"); itCount != jvoxels.end())
|
||||
count = static_cast<size_t>(itCount->value().to_number<uint64_t>());
|
||||
|
||||
std::string base64;
|
||||
if(auto itData = jvoxels.find("data"); itData != jvoxels.end())
|
||||
base64 = std::string(itData->value().as_string());
|
||||
|
||||
if(count > 0 && !base64.empty()) {
|
||||
std::u8string raw = decodeCompressed(base64);
|
||||
if(raw.size() != sizeof(VoxelCube_Region) * count)
|
||||
return false;
|
||||
|
||||
out.Voxels.resize(count);
|
||||
std::memcpy(out.Voxels.data(), raw.data(), raw.size());
|
||||
}
|
||||
}
|
||||
|
||||
if(auto it = jobj.find("voxels_map"); it != jobj.end()) {
|
||||
unpackIdMap(it->value().as_object(), out.VoxelIdToKey);
|
||||
}
|
||||
|
||||
if(auto it = jobj.find("nodes"); it != jobj.end()) {
|
||||
const js::object& jnodes = it->value().as_object();
|
||||
std::string base64;
|
||||
if(auto itData = jnodes.find("data"); itData != jnodes.end())
|
||||
base64 = std::string(itData->value().as_string());
|
||||
|
||||
if(!base64.empty()) {
|
||||
std::u8string raw = decodeCompressed(base64);
|
||||
if(raw.size() != sizeof(Node) * kRegionNodeCount)
|
||||
return false;
|
||||
|
||||
std::memcpy(out.Nodes[0].data(), raw.data(), raw.size());
|
||||
}
|
||||
}
|
||||
|
||||
if(auto it = jobj.find("nodes_map"); it != jobj.end()) {
|
||||
unpackIdMap(it->value().as_object(), out.NodeIdToKey);
|
||||
}
|
||||
|
||||
if(auto it = jobj.find("entities"); it != jobj.end()) {
|
||||
const js::array& ents = it->value().as_array();
|
||||
out.Entityes.reserve(ents.size());
|
||||
|
||||
for(const js::value& val : ents) {
|
||||
const js::object& je = val.as_object();
|
||||
DefEntityId defId = static_cast<DefEntityId>(je.at("def").to_number<uint64_t>());
|
||||
Entity entity(defId);
|
||||
|
||||
if(auto itWorld = je.find("world"); itWorld != je.end())
|
||||
entity.WorldId = static_cast<DefWorldId>(itWorld->value().to_number<uint64_t>());
|
||||
|
||||
if(auto itPos = je.find("pos"); itPos != je.end()) {
|
||||
const js::array& arr = itPos->value().as_array();
|
||||
entity.Pos = Pos::Object(
|
||||
static_cast<int32_t>(arr.at(0).to_number<int64_t>()),
|
||||
static_cast<int32_t>(arr.at(1).to_number<int64_t>()),
|
||||
static_cast<int32_t>(arr.at(2).to_number<int64_t>())
|
||||
);
|
||||
}
|
||||
|
||||
if(auto itSpeed = je.find("speed"); itSpeed != je.end()) {
|
||||
const js::array& arr = itSpeed->value().as_array();
|
||||
entity.Speed = Pos::Object(
|
||||
static_cast<int32_t>(arr.at(0).to_number<int64_t>()),
|
||||
static_cast<int32_t>(arr.at(1).to_number<int64_t>()),
|
||||
static_cast<int32_t>(arr.at(2).to_number<int64_t>())
|
||||
);
|
||||
}
|
||||
|
||||
if(auto itAccel = je.find("accel"); itAccel != je.end()) {
|
||||
const js::array& arr = itAccel->value().as_array();
|
||||
entity.Acceleration = Pos::Object(
|
||||
static_cast<int32_t>(arr.at(0).to_number<int64_t>()),
|
||||
static_cast<int32_t>(arr.at(1).to_number<int64_t>()),
|
||||
static_cast<int32_t>(arr.at(2).to_number<int64_t>())
|
||||
);
|
||||
}
|
||||
|
||||
if(auto itQuat = je.find("quat"); itQuat != je.end()) {
|
||||
const js::array& arr = itQuat->value().as_array();
|
||||
entity.Quat = glm::quat(
|
||||
static_cast<float>(arr.at(3).to_number<double>()),
|
||||
static_cast<float>(arr.at(0).to_number<double>()),
|
||||
static_cast<float>(arr.at(1).to_number<double>()),
|
||||
static_cast<float>(arr.at(2).to_number<double>())
|
||||
);
|
||||
}
|
||||
|
||||
if(auto itHp = je.find("hp"); itHp != je.end())
|
||||
entity.HP = static_cast<uint32_t>(itHp->value().to_number<uint64_t>());
|
||||
|
||||
if(auto itAabb = je.find("abbox"); itAabb != je.end()) {
|
||||
const js::array& arr = itAabb->value().as_array();
|
||||
entity.ABBOX.x = static_cast<uint64_t>(arr.at(0).to_number<uint64_t>());
|
||||
entity.ABBOX.y = static_cast<uint64_t>(arr.at(1).to_number<uint64_t>());
|
||||
entity.ABBOX.z = static_cast<uint64_t>(arr.at(2).to_number<uint64_t>());
|
||||
}
|
||||
|
||||
if(auto itRegion = je.find("in_region"); itRegion != je.end()) {
|
||||
const js::array& arr = itRegion->value().as_array();
|
||||
entity.InRegionPos = Pos::GlobalRegion(
|
||||
static_cast<int16_t>(arr.at(0).to_number<int64_t>()),
|
||||
static_cast<int16_t>(arr.at(1).to_number<int64_t>()),
|
||||
static_cast<int16_t>(arr.at(2).to_number<int64_t>())
|
||||
);
|
||||
}
|
||||
|
||||
if(auto itTags = je.find("tags"); itTags != je.end()) {
|
||||
const js::object& tags = itTags->value().as_object();
|
||||
for(const auto& kvp : tags) {
|
||||
entity.Tags[std::string(kvp.key())] = static_cast<float>(kvp.value().to_number<double>());
|
||||
}
|
||||
}
|
||||
|
||||
out.Entityes.push_back(std::move(entity));
|
||||
}
|
||||
}
|
||||
|
||||
if(auto it = jobj.find("entities_map"); it != jobj.end()) {
|
||||
unpackIdMap(it->value().as_object(), out.EntityToKey);
|
||||
}
|
||||
|
||||
return true;
|
||||
} catch(const std::exception& exc) {
|
||||
TOS::Logger("RegionLoader::Filesystem").warn() << "Не удалось загрузить регион " << path << "\n\t" << exc.what();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
class WSB_Filesystem : public IWorldSaveBackend {
|
||||
fs::path Dir;
|
||||
|
||||
@@ -35,7 +301,32 @@ public:
|
||||
|
||||
virtual TickSyncInfo_Out tickSync(TickSyncInfo_In &&data) override {
|
||||
TickSyncInfo_Out out;
|
||||
out.NotExisten = std::move(data.Load);
|
||||
// Сохранение регионов
|
||||
for(auto& [worldId, regions] : data.ToSave) {
|
||||
for(auto& [regionPos, region] : regions) {
|
||||
writeRegionFile(getPath(std::to_string(worldId), regionPos), region);
|
||||
}
|
||||
}
|
||||
|
||||
// Загрузка регионов
|
||||
for(auto& [worldId, regions] : data.Load) {
|
||||
for(const Pos::GlobalRegion& regionPos : regions) {
|
||||
const fs::path path = getPath(std::to_string(worldId), regionPos);
|
||||
if(!fs::exists(path)) {
|
||||
out.NotExisten[worldId].push_back(regionPos);
|
||||
continue;
|
||||
}
|
||||
|
||||
DB_Region_Out regionOut;
|
||||
if(!readRegionFile(path, regionOut)) {
|
||||
out.NotExisten[worldId].push_back(regionPos);
|
||||
continue;
|
||||
}
|
||||
|
||||
out.LoadedRegions[worldId].push_back({regionPos, std::move(regionOut)});
|
||||
}
|
||||
}
|
||||
|
||||
return out;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
#include "World.hpp"
|
||||
#include "ContentManager.hpp"
|
||||
#include "TOSLib.hpp"
|
||||
#include <memory>
|
||||
#include <unordered_set>
|
||||
|
||||
|
||||
namespace LV::Server {
|
||||
@@ -96,8 +98,94 @@ void World::onRemoteClient_RegionsLost(WorldId_t worldId, std::shared_ptr<Remote
|
||||
}
|
||||
}
|
||||
|
||||
World::SaveUnloadInfo World::onStepDatabaseSync() {
|
||||
return {};
|
||||
World::SaveUnloadInfo World::onStepDatabaseSync(ContentManager& cm, float dtime) {
|
||||
SaveUnloadInfo out;
|
||||
|
||||
constexpr float kSaveDelay = 15.0f;
|
||||
constexpr float kUnloadDelay = 15.0f;
|
||||
|
||||
std::vector<Pos::GlobalRegion> toErase;
|
||||
toErase.reserve(16);
|
||||
|
||||
for(auto& [pos, regionPtr] : Regions) {
|
||||
Region& region = *regionPtr;
|
||||
|
||||
region.LastSaveTime += dtime;
|
||||
|
||||
const bool hasChanges = region.IsChanged || region.IsChunkChanged_Voxels || region.IsChunkChanged_Nodes;
|
||||
const bool needToSave = hasChanges && region.LastSaveTime > kSaveDelay;
|
||||
const bool needToUnload = region.RMs.empty() && region.LastSaveTime > kUnloadDelay;
|
||||
|
||||
if(needToSave || needToUnload) {
|
||||
SB_Region_In data;
|
||||
data.Voxels = region.Voxels;
|
||||
data.Nodes = region.Nodes;
|
||||
|
||||
data.Entityes.reserve(region.Entityes.size());
|
||||
for(const Entity& entity : region.Entityes) {
|
||||
if(entity.IsRemoved || entity.NeedRemove)
|
||||
continue;
|
||||
data.Entityes.push_back(entity);
|
||||
}
|
||||
|
||||
std::unordered_set<DefVoxelId> voxelIds;
|
||||
for(const auto& [chunkPos, voxels] : region.Voxels) {
|
||||
(void) chunkPos;
|
||||
for(const VoxelCube& cube : voxels)
|
||||
voxelIds.insert(cube.VoxelId);
|
||||
}
|
||||
|
||||
std::unordered_set<DefNodeId> nodeIds;
|
||||
for(const auto& chunk : region.Nodes) {
|
||||
for(const Node& node : chunk)
|
||||
nodeIds.insert(node.NodeId);
|
||||
}
|
||||
|
||||
std::unordered_set<DefEntityId> entityIds;
|
||||
for(const Entity& entity : data.Entityes)
|
||||
entityIds.insert(entity.getDefId());
|
||||
|
||||
data.VoxelsMap.reserve(voxelIds.size());
|
||||
for(DefVoxelId id : voxelIds) {
|
||||
auto dk = cm.getDK(EnumDefContent::Voxel, id);
|
||||
if(!dk)
|
||||
continue;
|
||||
data.VoxelsMap.emplace_back(id, dk->Domain + ":" + dk->Key);
|
||||
}
|
||||
|
||||
data.NodeMap.reserve(nodeIds.size());
|
||||
for(DefNodeId id : nodeIds) {
|
||||
auto dk = cm.getDK(EnumDefContent::Node, id);
|
||||
if(!dk)
|
||||
continue;
|
||||
data.NodeMap.emplace_back(id, dk->Domain + ":" + dk->Key);
|
||||
}
|
||||
|
||||
data.EntityMap.reserve(entityIds.size());
|
||||
for(DefEntityId id : entityIds) {
|
||||
auto dk = cm.getDK(EnumDefContent::Entity, id);
|
||||
if(!dk)
|
||||
continue;
|
||||
data.EntityMap.emplace_back(id, dk->Domain + ":" + dk->Key);
|
||||
}
|
||||
|
||||
out.ToSave.push_back({pos, std::move(data)});
|
||||
|
||||
region.LastSaveTime = 0.0f;
|
||||
region.IsChanged = false;
|
||||
}
|
||||
|
||||
if(needToUnload) {
|
||||
out.ToUnload.push_back(pos);
|
||||
toErase.push_back(pos);
|
||||
}
|
||||
}
|
||||
|
||||
for(const Pos::GlobalRegion& pos : toErase) {
|
||||
Regions.erase(pos);
|
||||
}
|
||||
|
||||
return out;
|
||||
}
|
||||
|
||||
void World::pushRegions(std::vector<std::pair<Pos::GlobalRegion, RegionIn>> regions) {
|
||||
|
||||
@@ -12,6 +12,7 @@
|
||||
namespace LV::Server {
|
||||
|
||||
class GameServer;
|
||||
class ContentManager;
|
||||
|
||||
class Region {
|
||||
public:
|
||||
@@ -152,7 +153,7 @@ public:
|
||||
std::vector<Pos::GlobalRegion> ToUnload;
|
||||
std::vector<std::pair<Pos::GlobalRegion, SB_Region_In>> ToSave;
|
||||
};
|
||||
SaveUnloadInfo onStepDatabaseSync();
|
||||
SaveUnloadInfo onStepDatabaseSync(ContentManager& cm, float dtime);
|
||||
|
||||
struct RegionIn {
|
||||
std::unordered_map<Pos::bvec4u, std::vector<VoxelCube>> Voxels;
|
||||
|
||||
@@ -338,9 +338,10 @@ class ByteBuffer : public std::vector<uint8_t> {
|
||||
if(Index + sizeof(T) > Obj->size())
|
||||
throw std::runtime_error("Вышли за пределы буфера");
|
||||
|
||||
const uint8_t *ptr = Obj->data()+Index;
|
||||
T value{};
|
||||
std::memcpy(&value, Obj->data() + Index, sizeof(T));
|
||||
Index += sizeof(T);
|
||||
return swapEndian(*(const T*) ptr);
|
||||
return swapEndian(value);
|
||||
}
|
||||
|
||||
public:
|
||||
@@ -362,8 +363,8 @@ class ByteBuffer : public std::vector<uint8_t> {
|
||||
inline Reader& operator>>(int64_t &value) { value = readOffset<int64_t>(); return *this; }
|
||||
inline Reader& operator>>(uint64_t &value) { value = readOffset<uint64_t>(); return *this; }
|
||||
inline Reader& operator>>(bool &value) { value = readOffset<uint8_t>(); return *this; }
|
||||
inline Reader& operator>>(float &value) { return operator>>(*(uint32_t*) &value); }
|
||||
inline Reader& operator>>(double &value) { return operator>>(*(uint64_t*) &value); }
|
||||
inline Reader& operator>>(float &value) { uint32_t raw = readOffset<uint32_t>(); std::memcpy(&value, &raw, sizeof(raw)); return *this; }
|
||||
inline Reader& operator>>(double &value) { uint64_t raw = readOffset<uint64_t>(); std::memcpy(&value, &raw, sizeof(raw)); return *this; }
|
||||
|
||||
inline int8_t readInt8() { int8_t value; this->operator>>(value); return value; }
|
||||
inline uint8_t readUInt8() { uint8_t value; this->operator>>(value); return value; }
|
||||
@@ -449,6 +450,17 @@ class ByteBuffer : public std::vector<uint8_t> {
|
||||
size_t Index = 0;
|
||||
uint16_t BlockSize = 256;
|
||||
|
||||
template<typename T> inline void writeRaw(const T &value)
|
||||
{
|
||||
uint8_t *ptr = checkBorder(sizeof(T));
|
||||
std::memcpy(ptr, &value, sizeof(T));
|
||||
}
|
||||
|
||||
template<typename T> inline void writeSwapped(const T &value)
|
||||
{
|
||||
T temp = swapEndian(value);
|
||||
writeRaw(temp);
|
||||
}
|
||||
|
||||
inline uint8_t* checkBorder(size_t count)
|
||||
{
|
||||
@@ -469,17 +481,17 @@ class ByteBuffer : public std::vector<uint8_t> {
|
||||
Writer& operator=(const Writer&) = default;
|
||||
Writer& operator=(Writer&&) = default;
|
||||
|
||||
inline Writer& operator<<(const int8_t &value) { *(int8_t*) checkBorder(sizeof(value)) = value; return *this; }
|
||||
inline Writer& operator<<(const uint8_t &value) { *(uint8_t*) checkBorder(sizeof(value)) = value; return *this; }
|
||||
inline Writer& operator<<(const int16_t &value) { *(int16_t*) checkBorder(sizeof(value)) = swapEndian(value); return *this; }
|
||||
inline Writer& operator<<(const uint16_t &value) { *(uint16_t*) checkBorder(sizeof(value)) = swapEndian(value); return *this; }
|
||||
inline Writer& operator<<(const int32_t &value) { *(int32_t*) checkBorder(sizeof(value)) = swapEndian(value); return *this; }
|
||||
inline Writer& operator<<(const uint32_t &value) { *(uint32_t*) checkBorder(sizeof(value)) = swapEndian(value); return *this; }
|
||||
inline Writer& operator<<(const int64_t &value) { *(int64_t*) checkBorder(sizeof(value)) = swapEndian(value); return *this; }
|
||||
inline Writer& operator<<(const uint64_t &value) { *(uint64_t*) checkBorder(sizeof(value)) = swapEndian(value); return *this; }
|
||||
inline Writer& operator<<(const bool &value) { *(uint8_t*) checkBorder(sizeof(value)) = uint8_t(value ? 1 : 0); return *this; }
|
||||
inline Writer& operator<<(const float &value) { *(uint32_t*) checkBorder(sizeof(value)) = swapEndian(*(uint32_t*) &value); return *this; }
|
||||
inline Writer& operator<<(const double &value) { *(uint64_t*) checkBorder(sizeof(value)) = swapEndian(*(uint64_t*) &value); return *this; }
|
||||
inline Writer& operator<<(const int8_t &value) { writeRaw(value); return *this; }
|
||||
inline Writer& operator<<(const uint8_t &value) { writeRaw(value); return *this; }
|
||||
inline Writer& operator<<(const int16_t &value) { writeSwapped(value); return *this; }
|
||||
inline Writer& operator<<(const uint16_t &value) { writeSwapped(value); return *this; }
|
||||
inline Writer& operator<<(const int32_t &value) { writeSwapped(value); return *this; }
|
||||
inline Writer& operator<<(const uint32_t &value) { writeSwapped(value); return *this; }
|
||||
inline Writer& operator<<(const int64_t &value) { writeSwapped(value); return *this; }
|
||||
inline Writer& operator<<(const uint64_t &value) { writeSwapped(value); return *this; }
|
||||
inline Writer& operator<<(const bool &value) { uint8_t temp = value ? 1 : 0; writeRaw(temp); return *this; }
|
||||
inline Writer& operator<<(const float &value) { uint32_t raw; std::memcpy(&raw, &value, sizeof(raw)); writeSwapped(raw); return *this; }
|
||||
inline Writer& operator<<(const double &value) { uint64_t raw; std::memcpy(&raw, &value, sizeof(raw)); writeSwapped(raw); return *this; }
|
||||
|
||||
inline void writeInt8(const int8_t &value) { this->operator<<(value); }
|
||||
inline void writeUInt8(const uint8_t &value) { this->operator<<(value); }
|
||||
|
||||
105
docs/texture_pipeline.md
Normal file
105
docs/texture_pipeline.md
Normal file
@@ -0,0 +1,105 @@
|
||||
# Текстурные программы (TexturePipelineProgram)
|
||||
|
||||
Текстурная программа — это строка, описывающая источник текстуры и цепочку операций
|
||||
над ней. Такие строки используются в `textures` моделей и компилируются
|
||||
`TexturePipelineProgram`.
|
||||
|
||||
## Общая форма
|
||||
```
|
||||
[tex] <base> [|> op(...)]*
|
||||
```
|
||||
`tex` в начале необязателен.
|
||||
|
||||
## Базовые выражения
|
||||
- `name` или `"name.png"` — ссылка на текстуру из assets. Расширение .png/.jpg/.jpeg допустимо.
|
||||
- `anim(...)` — анимация из спрайт-листа (см. ниже).
|
||||
- `<w>x<h> <#RRGGBB|#RRGGBBAA>` — заливка цветом.
|
||||
|
||||
Примеры:
|
||||
```
|
||||
stone
|
||||
tex "core:stone.png"
|
||||
32x32 "#FF00FF"
|
||||
```
|
||||
|
||||
## Аргументы операций
|
||||
- Позиционные: `op(1, 2, "str")`
|
||||
- Именованные: `op(w=16, h=16)`
|
||||
- Значения: числа (uint32), строки в кавычках, либо идентификаторы.
|
||||
|
||||
Цвета задаются `#RRGGBB` или `#RRGGBBAA`.
|
||||
|
||||
## Операции пайплайна
|
||||
|
||||
Операции без аргументов можно писать без `()`: `brighten` и т.п.
|
||||
В подвыражениях текстур (см. ниже) операции без аргументов нужно писать со скобками:
|
||||
`brighten()`.
|
||||
|
||||
### Операции, принимающие текстуру
|
||||
- `overlay(tex)` — наложение с альфой.
|
||||
- `mask(tex)` — применение альфа-маски.
|
||||
- `lowpart(percent, tex)` — смешивание нижней части (percent 1..100).
|
||||
|
||||
`tex` может быть:
|
||||
- именем текстуры: `overlay("core:stone")`
|
||||
- именованным аргументом: `overlay(tex="core:stone")`
|
||||
- вложенной программой: `overlay( tex stone |> invert("rgb") )`
|
||||
|
||||
### Геометрия и альфа
|
||||
- `resize(w, h)` — ресайз до размеров.
|
||||
- `transform(t)` — трансформация (значение 0..7).
|
||||
- `opacity(a)` — прозрачность 0..255.
|
||||
- `remove_alpha` или `noalpha` — убрать альфа-канал.
|
||||
- `make_alpha(color)` — сделать альфу по цвету (цвет в `#RRGGBB`).
|
||||
|
||||
### Цвет и яркость
|
||||
- `invert(channels="rgb")` — инверсия каналов (`r`, `g`, `b`, `a`).
|
||||
- `brighten()` — лёгкое осветление.
|
||||
- `contrast(value, brightness)` — контраст и яркость (-127..127).
|
||||
- `multiply(color)` — умножение на цвет.
|
||||
- `screen(color)` — экранный режим.
|
||||
- `colorize(color, ratio=255)` — тонирование цветом.
|
||||
|
||||
### Анимация
|
||||
`anim` можно использовать в базе (с указанием текстуры) или в пайплайне
|
||||
над текущим изображением.
|
||||
|
||||
База:
|
||||
```
|
||||
anim(tex, frame_w, frame_h, frames, fps, smooth, axis)
|
||||
```
|
||||
|
||||
Пайплайн:
|
||||
```
|
||||
... |> anim(frame_w, frame_h, frames, fps, smooth, axis)
|
||||
```
|
||||
|
||||
Именованные аргументы:
|
||||
- `tex` — имя текстуры (только для базового `anim`).
|
||||
- `frame_w` или `w`
|
||||
- `frame_h` или `h`
|
||||
- `frames` или `count`
|
||||
- `fps`
|
||||
- `smooth` (0/1)
|
||||
- `axis` — режим нарезки:
|
||||
- `g` или пусто: по сетке (слева направо, сверху вниз)
|
||||
- `x`/`h`: по горизонтали
|
||||
- `y`/`v`: по вертикали
|
||||
|
||||
Если `frames` не задан, количество кадров вычисляется автоматически:
|
||||
- сетка: `(sheet.W / frame_w) * (sheet.H / frame_h)`
|
||||
- ось X/Y: `sheet.W / frame_w` или `sheet.H / frame_h`
|
||||
|
||||
Примеры:
|
||||
```
|
||||
anim("core:sheet", 16, 16, fps=8) # сетка по умолчанию
|
||||
anim("core:sheet", 16, 16, axis="x") # по горизонтали
|
||||
stone |> anim(16, 16, fps=10, smooth=1) # анимировать текущую текстуру
|
||||
```
|
||||
|
||||
## Вложенные текстурные выражения
|
||||
Некоторые операции принимают текстуру в аргументах. Чтобы передать не только имя,
|
||||
но и полноценную программу, используйте префикс `tex`:
|
||||
```
|
||||
overlay( tex "core:stone" |> resize(16,16) |> brighten() )
|
||||
```
|
||||
Reference in New Issue
Block a user