/*
 *  This file is part of CounterStrikeSharp.
 *  CounterStrikeSharp is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  CounterStrikeSharp is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with CounterStrikeSharp.  If not, see <https://www.gnu.org/licenses/>. *
 */

#include <ios>
#include <sstream>

#include "core/log.h"
#include "protobuf/generated/cstrike15_usermessages.pb.h"
#include <igameeventsystem.h>
#include <networksystem/inetworkserializer.h>
#include <networksystem/inetworkmessages.h>
#include "scripting//callback_manager.h"
#include "scripting/autonative.h"
#include "scripting/script_engine.h"
#include "natives_protobufs.h"
#include "google/protobuf/message.h"
#include "google/protobuf/message_lite.h"
#include <google/protobuf/dynamic_message.h>
#include <google/protobuf/compiler/importer.h>


namespace counterstrikesharp {

enum Protobuf_t {
    cstrike15_usermessages,
    netmessages,
};

void SendProtobuf(ScriptContext &script_context)
{
    auto client = script_context.GetArgument<int>(0);
    auto is_filter = script_context.GetArgument<bool>(1);
    auto msg = script_context.GetArgument<const char*>(2);
    auto data = script_context.GetArgument<const char*>(3);
    auto protobuf_enum = script_context.GetArgument<Protobuf_t>(4);

    CSSHARP_CORE_INFO("{0} | {1} | {2} | {3} | {4}", client, is_filter, msg, data, protobuf_enum);

    CRecipientFilter filter;
    if(is_filter)
    {
        CPlayerSlot PlayerSlot = CPlayerSlot(client);
        filter.AddRecipient(PlayerSlot);
    }

    std::string protobuf_file_name;
    switch (protobuf_enum) {
        case cstrike15_usermessages:
            protobuf_file_name = "cstrike15_usermessages";
            break;
        case netmessages:
            protobuf_file_name = "netmessages";
            break;
    }

    INetworkSerializable* message_type = globals::networkMessages->FindNetworkMessagePartial(msg);
    ::google::protobuf::compiler::DiskSourceTree source_tree;
    source_tree.MapPath("", ".");

    ::google::protobuf::compiler::Importer importer(&source_tree, nullptr);

    char import_path[100];
    snprintf(import_path, sizeof(import_path), "src/protobuf/%s.proto", protobuf_file_name.c_str());
    importer.Import(import_path);

    const ::google::protobuf::Descriptor* descriptor = importer.pool()->FindMessageTypeByName(msg);
    if (descriptor)
    {
        ::google::protobuf::DynamicMessageFactory factory;
        std::unique_ptr<::google::protobuf::Message> message(factory.GetPrototype(descriptor)->New());

        if (message->ParseFromArray(data, sizeof(data)))
        {
            globals::gameEventSystem->PostEventAbstract(0, false, &filter, message_type, message.get(), 0);
            CSSHARP_CORE_INFO("Successfully parsed message of type");
        }
        else
        {
            CSSHARP_CORE_INFO("Failed to parse message of type");
        }
    }
    else
    {
        CSSHARP_CORE_INFO("Unknown message type");
    }

}

REGISTER_NATIVES(protobufs, {
    ScriptEngine::RegisterNativeHandler("SEND_PROTOBUF", SendProtobuf);
})
}  // namespace counterstrikesharp


фикс