2025-12-06 14:16:50 +03:00
|
|
|
#pragma once
|
|
|
|
|
#include <compare>
|
|
|
|
|
#include <memory>
|
|
|
|
|
#include <unordered_map>
|
|
|
|
|
#include <functional>
|
|
|
|
|
#include <cereal/archives/portable_binary.hpp>
|
|
|
|
|
#include <chrono>
|
|
|
|
|
#include <vector>
|
|
|
|
|
#include "../Networking/NetClient.hpp"
|
|
|
|
|
#include "../Networking/NetServer.hpp"
|
|
|
|
|
#include "../Networking/NetLibrary.hpp"
|
|
|
|
|
#include "NetObjIDGenerator.hpp"
|
|
|
|
|
#include "NetObjManagerTypeList.hpp"
|
2025-12-07 17:35:27 +03:00
|
|
|
#include "NetObjPtr.decl.hpp"
|
|
|
|
|
#include "../Networking/NetServer.hpp"
|
2025-12-06 14:16:50 +03:00
|
|
|
|
|
|
|
|
namespace NetworkingObjects {
|
|
|
|
|
class NetObjManager {
|
|
|
|
|
public:
|
2025-12-07 17:35:27 +03:00
|
|
|
NetObjManager(std::shared_ptr<NetObjIDGenerator> initIDGen, std::shared_ptr<NetObjManagerTypeList> initTypeList, bool initIsServer);
|
|
|
|
|
void read_update_message(cereal::PortableBinaryInputArchive& a, const std::shared_ptr<NetServer::ClientData>& clientReceivedFrom);
|
2025-12-06 14:16:50 +03:00
|
|
|
void set_client(std::shared_ptr<NetClient> initClient, MessageCommandType initUpdateCommandID);
|
|
|
|
|
void set_server(std::shared_ptr<NetServer> initServer, MessageCommandType initUpdateCommandID);
|
|
|
|
|
|
2025-12-07 17:35:27 +03:00
|
|
|
template <typename T> NetObjPtr<T> read_create_message(cereal::PortableBinaryInputArchive& a, const std::shared_ptr<NetServer::ClientData>& clientReceivedFrom) {
|
2025-12-06 14:16:50 +03:00
|
|
|
NetObjID id;
|
|
|
|
|
NetTypeIDType typeID;
|
|
|
|
|
a(id, typeID);
|
|
|
|
|
auto& netTypeIDData = typeList->netTypeIDData[typeID];
|
2025-12-07 17:35:27 +03:00
|
|
|
std::shared_ptr<void> sharedPtr = isServer ? netTypeIDData.allocatorFuncServer() : netTypeIDData.allocatorFuncClient();
|
|
|
|
|
auto [it, placed] = objectData.emplace(id, SingleObjectData{.netTypeID = typeID, .p = sharedPtr});
|
2025-12-06 14:16:50 +03:00
|
|
|
if(!placed)
|
|
|
|
|
throw std::runtime_error("[NetObjManager::read_create_message] ID Collision on object creation");
|
2025-12-07 17:35:27 +03:00
|
|
|
if(isServer)
|
|
|
|
|
netTypeIDData.readConstructorFuncServer(NetObjPtr<void>(this, id, sharedPtr), a, clientReceivedFrom);
|
|
|
|
|
else
|
|
|
|
|
netTypeIDData.readConstructorFuncClient(NetObjPtr<void>(this, id, sharedPtr), a, clientReceivedFrom);
|
|
|
|
|
return NetObjPtr<T>(this, id, std::static_pointer_cast<T>(sharedPtr));
|
2025-12-06 14:16:50 +03:00
|
|
|
}
|
|
|
|
|
template <typename T> NetObjPtr<T> read_get_obj_from_message(cereal::PortableBinaryInputArchive& a) {
|
|
|
|
|
NetObjID id;
|
|
|
|
|
a(id);
|
|
|
|
|
auto it = objectData.find(id);
|
|
|
|
|
if(it == objectData.end())
|
|
|
|
|
return NetObjPtr<T>();
|
2025-12-07 17:35:27 +03:00
|
|
|
return NetObjPtr<T>(this, id, std::static_pointer_cast<T>(it->second.p));
|
2025-12-06 14:16:50 +03:00
|
|
|
}
|
|
|
|
|
template <typename T, typename... Args> NetObjPtr<T> make_obj(Args&&... items) {
|
|
|
|
|
auto sharedPtr = std::make_shared<T>(items...);
|
|
|
|
|
NetObjID newID = idGen->gen();
|
2025-12-07 17:35:27 +03:00
|
|
|
if(!objectData.emplace(newID, SingleObjectData{.netTypeID = isServer ? typeList->typeIndexDataServer[std::type_index(typeid(T))].netTypeID : typeList->typeIndexDataClient[std::type_index(typeid(T))].netTypeID, .p = std::static_pointer_cast<void>(sharedPtr)}).second)
|
2025-12-06 14:16:50 +03:00
|
|
|
throw std::runtime_error("[NetObjManager::make_obj] ID Collision");
|
2025-12-07 17:35:27 +03:00
|
|
|
return NetObjPtr<T>(this, newID, sharedPtr);
|
2025-12-06 14:16:50 +03:00
|
|
|
}
|
|
|
|
|
template <typename T> NetObjPtr<T> obj_from_ptr(T* p) {
|
|
|
|
|
std::shared_ptr<T> sharedPtr(p);
|
|
|
|
|
NetObjID newID = idGen->gen();
|
2025-12-07 17:35:27 +03:00
|
|
|
if(!objectData.emplace(newID, SingleObjectData{.netTypeID = isServer ? typeList->typeIndexDataServer[std::type_index(typeid(T))].netTypeID : typeList->typeIndexDataClient[std::type_index(typeid(T))].netTypeID, .p = std::static_pointer_cast<void>(sharedPtr)}).second)
|
2025-12-06 14:16:50 +03:00
|
|
|
throw std::runtime_error("[NetObjManager::obj_from_ptr] ID Collision");
|
2025-12-07 17:35:27 +03:00
|
|
|
return NetObjPtr<T>(this, newID, sharedPtr);
|
2025-12-06 14:16:50 +03:00
|
|
|
}
|
|
|
|
|
template <typename T> NetObjPtr<T> get_obj_from_id(NetObjID id) {
|
|
|
|
|
auto it = objectData.find(id);
|
|
|
|
|
if(it == objectData.end())
|
|
|
|
|
throw std::runtime_error("[NetObjManager::get_obj_from_id] ID doesn't exist");
|
2025-12-07 17:35:27 +03:00
|
|
|
return NetObjPtr<T>(this, id, std::static_pointer_cast<T>(it->second.p));
|
2025-12-06 14:16:50 +03:00
|
|
|
}
|
|
|
|
|
private:
|
|
|
|
|
template <typename T> friend class NetObjPtr;
|
|
|
|
|
|
|
|
|
|
struct SingleObjectData {
|
|
|
|
|
NetTypeIDType netTypeID;
|
|
|
|
|
std::shared_ptr<void> p;
|
|
|
|
|
};
|
2025-12-07 17:35:27 +03:00
|
|
|
bool isServer;
|
2025-12-06 14:16:50 +03:00
|
|
|
std::shared_ptr<NetClient> client;
|
|
|
|
|
std::shared_ptr<NetServer> server;
|
|
|
|
|
MessageCommandType updateCommandID;
|
|
|
|
|
std::unordered_map<NetObjID, SingleObjectData> objectData;
|
|
|
|
|
std::shared_ptr<NetObjIDGenerator> idGen;
|
|
|
|
|
std::shared_ptr<NetObjManagerTypeList> typeList;
|
|
|
|
|
NetTypeIDType nextTypeID;
|
|
|
|
|
};
|
|
|
|
|
}
|