Commit 0fa25178 authored by Filip Hauzvic's avatar Filip Hauzvic
Browse files

Register replication into reflection

parent 9c84ffcc
Loading
Loading
Loading
Loading
+5 −0
Original line number Diff line number Diff line
@@ -17,6 +17,10 @@ namespace net {
            com::ContextItem* owner,
            const std::string& getter_name,
            const std::string& setter_name);
        void register_property(
            const std::vector<std::string>& owner_path,
            const std::string& getter_name,
            const std::string& setter_name);
        void handle_packet(ConnectionId from, utils::Deserializer& deserializer);

    private:
@@ -26,6 +30,7 @@ namespace net {
                         const com::Reflection::ValuePtr& value);

        void initialize() override;
        void register_functions() override;

        const size_t INITIAL_BUFFER_SIZE = 512;
        std::vector<ReplicatedProperty> replicated_properties;
+3 −0
Original line number Diff line number Diff line
@@ -93,6 +93,9 @@ namespace net
            } else if constexpr (std::is_same_v<T, vec2i>)
            {
                return com::make_value<com::Reflection::ValueVEC2I>(val);
            } else if constexpr (std::is_same_v<T, std::vector<std::string>>)
            {
                return com::make_value<com::Reflection::ValuePATH>(val);
            } else
            {
                throw std::runtime_error("Unsupported type for reflection value creation");
+1 −1
Original line number Diff line number Diff line
@@ -39,7 +39,7 @@ namespace net {

    enum class PacketType : uint8_t
    {
        RPC,
        RPC = 1,
        REPLICATION
    };

+37 −3
Original line number Diff line number Diff line
@@ -69,6 +69,21 @@ namespace net {
        replicated_properties.push_back(property);
    }

    void ReplicationSystem::register_property(const std::vector<std::string>& owner_path,
                                              const std::string& getter_name,
                                              const std::string& setter_name)
    {
        auto owner = com::Folder::root()->locate<com::ContextItem>(owner_path);
        if (!owner)
        {
            LOG(LSL_ERROR, "[ReplicationSystem] Owner not found at path " << owner_path);
            return;
        }

        register_property(owner, getter_name, setter_name);
    }


    void ReplicationSystem::handle_packet(ConnectionId from, utils::Deserializer& deserializer)
    {
        std::vector<std::string> context_item_path;
@@ -87,7 +102,8 @@ namespace net {
        call_setter(context_item_path, setter_name, value);
    }

    void ReplicationSystem::call_setter(const std::vector<std::string>& context_item_path, const std::string& setter_name, const com::Reflection::ValuePtr& value)
    void ReplicationSystem::call_setter(const std::vector<std::string>& context_item_path, const std::string& setter_name,
                                        const com::Reflection::ValuePtr& value)
    {
        const auto context_item = com::Folder::root()->locate<com::ContextItem>(context_item_path);
        if (!context_item)
@@ -124,8 +140,7 @@ namespace net {
    }


    utils::Buffer ReplicationSystem::create_packet(const ReplicatedProperty& property, const com::Reflection::ValuePtr& value,
                                                   int& written_bytes) const
    utils::Buffer ReplicationSystem::create_packet(const ReplicatedProperty& property, const com::Reflection::ValuePtr& value, int& written_bytes) const
    {
        utils::Buffer buffer(INITIAL_BUFFER_SIZE);
        auto serializer = utils::create_serializer(buffer);
@@ -146,6 +161,25 @@ namespace net {
        osi::updaters()->find<com::Folder>("net")->push_back<com::Link>(self_name() + ".link", this);
    }

    void ReplicationSystem::register_functions()
    {
        COM_REFLECTION_USINGS();
        com::Runner::register_functions();

        com::Reflection::register_function(this, {
                                               "register_property",
                                               {TypeID::PATH, TypeID::STRING, TypeID::STRING},
                                               false,
                                               [this](std::vector<ValuePtr>& params) -> void
                                               {
                                                   auto owner_path = com::as<ValuePATH>(params[0])->value;
                                                   const std::string& getter_name = com::as<ValueSTRING>(params[1])->value;
                                                   const std::string& setter_name = com::as<ValueSTRING>(params[2])->value;
                                                   register_property(owner_path, getter_name, setter_name);
                                               }
                                           });
    }


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