// Copyright (c) 2012-2022 Wojciech Figat. All rights reserved.
#pragma once
#include "Types.h"
#include "Engine/Scripting/ScriptingObject.h"
#include "Engine/Scripting/ScriptingType.h"
///
/// High-level networking replication system for game objects.
///
API_CLASS(static, Namespace = "FlaxEngine.Networking") class FLAXENGINE_API NetworkReplicator
{
DECLARE_SCRIPTING_TYPE_MINIMAL(NetworkReplicator);
friend class NetworkReplicatorInternal;
typedef void (*SerializeFunc)(void* instance, NetworkStream* stream, void* tag);
public:
///
/// Adds the network replication serializer for a given type.
///
/// The scripting type to serialize.
/// Serialization callback method.
/// Deserialization callback method.
/// Serialization callback method tag value.
/// Deserialization callback method tag value.
static void AddSerializer(const ScriptingTypeHandle& typeHandle, SerializeFunc serialize, SerializeFunc deserialize, void* serializeTag = nullptr, void* deserializeTag = nullptr);
///
/// Invokes the network replication serializer for a given type.
///
/// The scripting type to serialize.
/// The value instance to serialize.
/// The input/output stream to use for serialization.
/// True if serialize, otherwise deserialize mode.
/// True if failed, otherwise false.
API_FUNCTION(NoProxy) static bool InvokeSerializer(const ScriptingTypeHandle& typeHandle, void* instance, NetworkStream* stream, bool serialize);
///
/// Adds the object to the network replication system.
///
/// Does nothing if network is offline.
/// The object to replicate.
/// The owner of the object (eg. player that spawned it).
API_FUNCTION() static void AddObject(ScriptingObject* obj, ScriptingObject* owner);
private:
#if !COMPILE_WITHOUT_CSHARP
API_FUNCTION(NoProxy) static void AddSerializer(const ScriptingTypeHandle& type, const Function& serialize, const Function& deserialize);
#endif
};