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>
|
2025-12-08 14:28:01 +03:00
|
|
|
#include <Helpers/Random.hpp>
|
2025-12-06 14:16:50 +03:00
|
|
|
#include "../Networking/NetClient.hpp"
|
|
|
|
|
#include "../Networking/NetServer.hpp"
|
|
|
|
|
#include "../Networking/NetLibrary.hpp"
|
2025-12-08 14:28:01 +03:00
|
|
|
#include "Helpers/NetworkingObjects/NetObjID.hpp"
|
2025-12-06 14:16:50 +03:00
|
|
|
#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-08 14:28:01 +03:00
|
|
|
NetObjManager(std::shared_ptr<NetObjManagerTypeList> initTypeList, bool initIsServer);
|
2025-12-07 17:35:27 +03:00
|
|
|
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 23:09:17 +03:00
|
|
|
bool is_server() const;
|
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-08 22:21:07 +03:00
|
|
|
NetObjPtr<T> newPtr = make_obj_with_id<T>(id);
|
2025-12-07 17:35:27 +03:00
|
|
|
if(isServer)
|
2025-12-08 22:21:07 +03:00
|
|
|
netTypeIDData.readConstructorFuncServer(newPtr.template cast<void>(), a, clientReceivedFrom);
|
2025-12-07 17:35:27 +03:00
|
|
|
else
|
2025-12-08 22:21:07 +03:00
|
|
|
netTypeIDData.readConstructorFuncClient(newPtr.template cast<void>(), a, clientReceivedFrom);
|
|
|
|
|
return newPtr;
|
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
|
|
|
}
|
2025-12-08 20:51:11 +03:00
|
|
|
template <typename T> NetObjPtr<T> make_obj() {
|
|
|
|
|
auto sharedPtr = std::static_pointer_cast<T>(isServer ? typeList->typeIndexDataServer[std::type_index(typeid(T*))].allocatorFunc() : typeList->typeIndexDataClient[std::type_index(typeid(T*))].allocatorFunc());
|
|
|
|
|
NetObjID newID = NetObjID::random_gen();
|
|
|
|
|
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)
|
|
|
|
|
throw std::runtime_error("[NetObjManager::make_obj] ID Collision");
|
|
|
|
|
return NetObjPtr<T>(this, newID, sharedPtr);
|
|
|
|
|
}
|
|
|
|
|
// Don't use this function unless you're sure that class T isn't a base class
|
|
|
|
|
template <typename T, typename... Args> NetObjPtr<T> make_obj_direct(Args&&... items) {
|
2025-12-06 14:16:50 +03:00
|
|
|
auto sharedPtr = std::make_shared<T>(items...);
|
2025-12-08 14:28:01 +03:00
|
|
|
NetObjID newID = NetObjID::random_gen();
|
2025-12-07 23:09:17 +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);
|
2025-12-08 14:28:01 +03:00
|
|
|
NetObjID newID = NetObjID::random_gen();
|
2025-12-07 23:09:17 +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:
|
2025-12-08 22:21:07 +03:00
|
|
|
template <typename T> NetObjPtr<T> make_obj_with_id(NetObjID id) {
|
|
|
|
|
auto sharedPtr = std::static_pointer_cast<T>(isServer ? typeList->typeIndexDataServer[std::type_index(typeid(T*))].allocatorFunc() : typeList->typeIndexDataClient[std::type_index(typeid(T*))].allocatorFunc());
|
|
|
|
|
if(!objectData.emplace(id, 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)
|
|
|
|
|
throw std::runtime_error("[NetObjManager::make_obj] ID Collision");
|
|
|
|
|
return NetObjPtr<T>(this, id, sharedPtr);
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-06 14:16:50 +03:00
|
|
|
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<NetObjManagerTypeList> typeList;
|
|
|
|
|
NetTypeIDType nextTypeID;
|
|
|
|
|
};
|
|
|
|
|
}
|