]> git.tdb.fi Git - libs/game.git/commitdiff
Allow a custom protocol to be set for Replicator
authorMikko Rasa <tdb@tdb.fi>
Thu, 11 Jan 2024 22:01:56 +0000 (00:01 +0200)
committerMikko Rasa <tdb@tdb.fi>
Thu, 11 Jan 2024 22:04:04 +0000 (00:04 +0200)
It needs to derive from StageProtocol, which is now exported from the
library.

source/game/protocol.h
source/game/replicator.cpp
source/game/replicator.h

index 72f63c3fc6f01750d727b13931d54d1af6e9b0a2..209435fb9e78059bd0a5b98e9978e3ff7f014237 100644 (file)
@@ -2,6 +2,7 @@
 #define MSP_GAME_PROTOCOL_H_
 
 #include <msp/net/protocol.h>
+#include "mspgame_api.h"
 
 namespace Msp::Game {
 
@@ -12,7 +13,7 @@ public:
 };
 
 
-class StageProtocol: public Net::Protocol
+class MSPGAME_API StageProtocol: public Net::Protocol
 {
 public:
        StageProtocol();
index acdc6d0e2f3c5897a44db120991351c5aadaf85d..aba64eb438175b5867021abd8c44c41ede147c45 100644 (file)
@@ -13,13 +13,9 @@ Replicator::Replicator(Stage &s, Networking &n):
        System(s),
        event_source(stage.get_event_bus()),
        observer(stage.get_event_bus()),
-       networking(n),
-       protocol(networking.set_receiver<StageProtocol>(&dispatcher))
+       networking(n)
 {
        observer.observe<Events::ComponentCreated>([this](const auto &e){ component_created(e); });
-
-       add_receiver<Messages::SpawnEntity>(*this);
-       add_receiver<Messages::GrantPossession>(*this);
 }
 
 void Replicator::add_spawner(Spawner &spawner, const string &type)
@@ -45,6 +41,16 @@ void Replicator::add_target_player(uint32_t id)
                send_spawn(e, id);
 }
 
+void Replicator::set_protocol(const Net::Protocol &p)
+{
+       if(protocol)
+               throw already_called("Replicator::set_protocol");
+
+       protocol = &p;
+       add_receiver<Messages::SpawnEntity>(*this);
+       add_receiver<Messages::GrantPossession>(*this);
+}
+
 Handle<Entity> Replicator::find_entity(uint32_t id) const
 {
        auto i = find_if(entities, [id](const ReplicatedEntity &e){ return e.zygote->get_entity_id()==id; });
index c9c918f9b07c5e70c088f997309f08fd91a40f1b..069429a2f043b54923f6d8f69051d549f35ac6fb 100644 (file)
@@ -36,7 +36,7 @@ private:
        EventObserver observer;
        Networking &networking;
        Net::DynamicDispatcher dispatcher;
-       const Net::Protocol &protocol;
+       const Net::Protocol *protocol = nullptr;
        std::vector<Spawner *> spawners;
        std::vector<ReplicatedEntity> entities;
        std::vector<std::uint32_t> players;
@@ -50,6 +50,14 @@ public:
        void add_target_player(std::uint32_t);
        bool is_server() const { return networking.is_server(); }
 
+       template<typename T>
+               requires std::is_base_of_v<StageProtocol, T>
+       void set_protocol();
+
+private:
+       void set_protocol(const Net::Protocol &);
+
+public:
        template<typename P>
        void add_receiver(Net::PacketReceiver<P> &);
 
@@ -76,10 +84,19 @@ private:
        void receive(const Messages::GrantPossession &) override;
 };
 
+template<typename T>
+       requires std::is_base_of_v<StageProtocol, T>
+void Replicator::set_protocol()
+{
+       set_protocol(networking.set_receiver<T>(&dispatcher));
+}
+
 template<typename P>
 void Replicator::add_receiver(Net::PacketReceiver<P> &recv)
 {
-       dispatcher.add_receiver<P>(protocol.get_packet_id<P>(), recv);
+       if(!protocol)
+               throw invalid_state("Replicator::add_receiver");
+       dispatcher.add_receiver<P>(protocol->get_packet_id<P>(), recv);
 }
 
 template<typename P>