]> git.tdb.fi Git - libs/game.git/commitdiff
Require entity packets to have an entity_id field of correct type
authorMikko Rasa <tdb@tdb.fi>
Sat, 13 Jan 2024 10:11:43 +0000 (12:11 +0200)
committerMikko Rasa <tdb@tdb.fi>
Sat, 13 Jan 2024 10:11:43 +0000 (12:11 +0200)
source/game/remotecall.h

index e5e0fb768979f8fe6d3afff405c4be88a0d26aab..b9c2ac61474a1f44ec7645518cf3e09e73e27cef 100644 (file)
@@ -1,6 +1,7 @@
 #ifndef MSP_GAME_REMOTECALL_H_
 #define MSP_GAME_REMOTECALL_H_
 
+#include <concepts>
 #include <functional>
 #include <stdexcept>
 #include <msp/net/receiver.h>
@@ -10,6 +11,9 @@
 
 namespace Msp::Game {
 
+template<typename P>
+concept HasEntityIdField = requires(P p) { { (p.entity_id) } -> std::same_as<std::uint32_t &>; };
+
 template<typename P, typename F = void(const P &)>
 class RemoteCallBase: protected Net::PacketReceiver<P>
 {
@@ -24,7 +28,7 @@ protected:
 
 
 template<typename P, typename E = Entity>
-       requires std::is_base_of_v<Entity, E>
+       requires std::is_base_of_v<Entity, E> && HasEntityIdField<P>
 class RemoteEntityCallBase: protected RemoteCallBase<P, void(Handle<E>, const P &)>
 {
 protected:
@@ -98,7 +102,7 @@ RemoteCallBase<P, F>::RemoteCallBase(Replicator *r, Function f):
 
 
 template<typename P, typename E>
-       requires std::is_base_of_v<Entity, E>
+       requires std::is_base_of_v<Entity, E> && HasEntityIdField<P>
 void RemoteEntityCallBase<P, E>::prepare(Handle<E> entity, P &packet)
 {
        Handle<Zygote> zygote = entity->template get_component<Zygote>();
@@ -113,7 +117,7 @@ void RemoteEntityCallBase<P, E>::prepare(Handle<E> entity, P &packet)
 }
 
 template<typename P, typename E>
-       requires std::is_base_of_v<Entity, E>
+       requires std::is_base_of_v<Entity, E> && HasEntityIdField<P>
 void RemoteEntityCallBase<P, E>::call(const P &packet)
 {
        Handle<Entity> entity = this->replicator->find_entity(packet.entity_id);