Commit 33bae45a authored by Filip Hauzvic's avatar Filip Hauzvic
Browse files

Register property for replication

parent b20f6824
Loading
Loading
Loading
Loading
+14 −3
Original line number Diff line number Diff line
#pragma once
#include <com/library.hpp>
#include <com/runner.hpp>
#include <net/types.hpp>

namespace net {
    struct ReplicationSystem final : com::Library
    struct ReplicationSystem final : com::Runner
    {
        static inline std::string self_name() { return "replication_system.lib"; }
        static inline std::string self_name() { return "replication_system"; }

        ReplicationSystem();
        ~ReplicationSystem() override;

        void next_round() override;

        void register_property(
            com::ContextItem* owner,
            const std::string& getter_name,
            const std::string& setter_name);

    private:
        std::vector<ReplicatedProperty> replicated_properties;
    };
}
+17 −4
Original line number Diff line number Diff line
#pragma once
#include <cstdint>
#include <com/reflection.hpp>

namespace net
{
namespace net {
    using ConnectionId = uint32_t;
    constexpr ConnectionId SERVER_ID = 0;
    constexpr ConnectionId BROADCAST_ID = static_cast<ConnectionId>(-1);
@@ -14,7 +14,10 @@ namespace net
        double rtt = 0.0;

        Connection() = default;
        explicit Connection(const ConnectionId id) : id(id) {}

        explicit Connection(const ConnectionId id) : id(id)
        {
        }
    };

    struct NetworkMessage
@@ -24,6 +27,16 @@ namespace net
        int channel;
    };

    struct ReplicatedProperty
    {
        com::ContextItem* owner;
        std::string getter_name;
        std::string setter_name;
        com::Reflection::ValuePtr last_value;

        std::function<void(std::vector<com::Reflection::ValuePtr>& params)> getter_func;
    };

    enum class PacketType : uint8_t
    {
        RPC
+48 −3
Original line number Diff line number Diff line
#include <net/index.hpp>
#include <net/replication_system.hpp>

namespace net {
    void ReplicationSystem::next_round()
    {

    }


    void ReplicationSystem::register_property(ContextItem* owner, const std::string& getter_name,
                                              const std::string& setter_name)
    {
        if (driver()->mode() != SocketMode::SERVER)
        {
            LOG(LSL_ERROR, "[ReplicationSystem] Only the server can register replicated properties");
            return;
        }
        if (!owner || !owner->reflection())
        {
            LOG(LSL_ERROR, "[ReplicationSystem] Invalid owner for replicated property");
            return;
        }
        if (!owner->reflection()->functions().contains(getter_name))
        {
            LOG(LSL_ERROR, "[ReplicationSystem] Getter function " << getter_name << " not found in owner " << owner->name());
            return;
        }
        if (!owner->reflection()->functions().contains(setter_name))
        {
            LOG(LSL_ERROR, "[ReplicationSystem] Setter function " << setter_name << " not found in owner " << owner->name());
            return;
        }

        ReplicatedProperty property;
        property.owner = owner;
        property.getter_name = getter_name;
        property.setter_name = setter_name;

    ReplicationSystem::ReplicationSystem() : com::Library(self_name())
        const auto& getter_function = owner->reflection()->functions().at(getter_name);
        property.getter_func = getter_function.code;
        property.last_value = com::make_default_value(getter_function.param_types[0]);

        std::vector<com::Reflection::ValuePtr> params;
        params.push_back(property.last_value);
        property.getter_func(params);

        replicated_properties.push_back(property);
    }

    ReplicationSystem::ReplicationSystem() : com::Runner(self_name())
    {
    }

    ReplicationSystem::~ReplicationSystem() = default;

}