using System;
using System.IO;
using System.Linq;
using System.Reflection;
using UnityEngine;
namespace VelNet
{
public abstract class NetworkComponent : MonoBehaviour
{
public NetworkObject networkObject;
protected bool IsMine => networkObject != null && networkObject.owner != null && networkObject.owner.isLocal;
protected VelNetPlayer Owner => networkObject != null ? networkObject.owner : null;
///
/// call this in child classes to send a message to other people
///
protected void SendBytes(byte[] message, bool reliable = true)
{
networkObject.SendBytes(this, false, message, reliable);
}
///
/// call this in child classes to send a message to other people
///
protected void SendBytesToGroup(string group, byte[] message, bool reliable = true)
{
networkObject.SendBytesToGroup(this, false, group, message, reliable);
}
///
/// This is called by when messages are received for this component
///
public abstract void ReceiveBytes(byte[] message);
public void ReceiveRPC(byte[] message)
{
using MemoryStream mem = new MemoryStream(message);
using BinaryReader reader = new BinaryReader(mem);
byte methodIndex = reader.ReadByte();
int length = reader.ReadInt32();
byte[] parameterData = reader.ReadBytes(length);
MethodInfo[] mInfos = GetType().GetMethods(BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.Instance | BindingFlags.NonPublic);
Array.Sort(mInfos, (m1, m2) => string.Compare(m1.Name, m2.Name, StringComparison.Ordinal));
try
{
mInfos[methodIndex].Invoke(this, length > 0 ? new object[] { parameterData } : Array.Empty