Decompiled source of Multiplayer v0.2.7
LiteNetLib.dll
Decompiled 4 days ago
The result has been truncated due to the large size, download it to view full contents!
using System; using System.Collections; using System.Collections.Concurrent; using System.Collections.Generic; using System.Diagnostics; using System.Net; using System.Net.NetworkInformation; using System.Net.Sockets; using System.Reflection; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; using System.Runtime.Serialization; using System.Runtime.Versioning; using System.Security; using System.Security.Permissions; using System.Text; using System.Threading; using LiteNetLib.Layers; using LiteNetLib.Utils; using Microsoft.CodeAnalysis; [assembly: CompilationRelaxations(8)] [assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)] [assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)] [assembly: TargetFramework(".NETStandard,Version=v2.1", FrameworkDisplayName = ".NET Standard 2.1")] [assembly: AssemblyCompany("Ruslan Pyrch")] [assembly: AssemblyConfiguration("Release")] [assembly: AssemblyCopyright("Copyright 2023 Ruslan Pyrch")] [assembly: AssemblyDescription("Lite reliable UDP library for .NET, Mono, and .NET Core")] [assembly: AssemblyFileVersion("1.2.0")] [assembly: AssemblyInformationalVersion("1.1.0+07c089a22639ba64a414eb5816e4bd805cc5a684")] [assembly: AssemblyProduct("LiteNetLib")] [assembly: AssemblyTitle("LiteNetLib")] [assembly: AssemblyMetadata("RepositoryUrl", "https://github.com/RevenantX/LiteNetLib")] [assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)] [assembly: AssemblyVersion("1.2.0.0")] [module: UnverifiableCode] namespace Microsoft.CodeAnalysis { [CompilerGenerated] [Microsoft.CodeAnalysis.Embedded] internal sealed class EmbeddedAttribute : Attribute { } } namespace System.Runtime.CompilerServices { [CompilerGenerated] [Microsoft.CodeAnalysis.Embedded] internal sealed class IsUnmanagedAttribute : Attribute { } } namespace LiteNetLib { internal abstract class BaseChannel { protected readonly NetPeer Peer; protected readonly Queue<NetPacket> OutgoingQueue = new Queue<NetPacket>(64); private int _isAddedToPeerChannelSendQueue; public int PacketsInQueue => OutgoingQueue.Count; protected BaseChannel(NetPeer peer) { Peer = peer; } public void AddToQueue(NetPacket packet) { lock (OutgoingQueue) { OutgoingQueue.Enqueue(packet); } AddToPeerChannelSendQueue(); } protected void AddToPeerChannelSendQueue() { if (Interlocked.CompareExchange(ref _isAddedToPeerChannelSendQueue, 1, 0) == 0) { Peer.AddToReliableChannelSendQueue(this); } } public bool SendAndCheckQueue() { bool num = SendNextPackets(); if (!num) { Interlocked.Exchange(ref _isAddedToPeerChannelSendQueue, 0); } return num; } protected abstract bool SendNextPackets(); public abstract bool ProcessPacket(NetPacket packet); } internal enum ConnectionRequestResult { None, Accept, Reject, RejectForce } public class ConnectionRequest { private readonly NetManager _listener; private int _used; internal NetConnectRequestPacket InternalPacket; public readonly IPEndPoint RemoteEndPoint; public NetDataReader Data => InternalPacket.Data; internal ConnectionRequestResult Result { get; private set; } internal void UpdateRequest(NetConnectRequestPacket connectRequest) { if (connectRequest.ConnectionTime >= InternalPacket.ConnectionTime && (connectRequest.ConnectionTime != InternalPacket.ConnectionTime || connectRequest.ConnectionNumber != InternalPacket.ConnectionNumber)) { InternalPacket = connectRequest; } } private bool TryActivate() { return Interlocked.CompareExchange(ref _used, 1, 0) == 0; } internal ConnectionRequest(IPEndPoint remoteEndPoint, NetConnectRequestPacket requestPacket, NetManager listener) { InternalPacket = requestPacket; RemoteEndPoint = remoteEndPoint; _listener = listener; } public NetPeer AcceptIfKey(string key) { if (!TryActivate()) { return null; } try { if (Data.GetString() == key) { Result = ConnectionRequestResult.Accept; } } catch { NetDebug.WriteError("[AC] Invalid incoming data"); } if (Result == ConnectionRequestResult.Accept) { return _listener.OnConnectionSolved(this, null, 0, 0); } Result = ConnectionRequestResult.Reject; _listener.OnConnectionSolved(this, null, 0, 0); return null; } public NetPeer Accept() { if (!TryActivate()) { return null; } Result = ConnectionRequestResult.Accept; return _listener.OnConnectionSolved(this, null, 0, 0); } public void Reject(byte[] rejectData, int start, int length, bool force) { if (TryActivate()) { Result = (force ? ConnectionRequestResult.RejectForce : ConnectionRequestResult.Reject); _listener.OnConnectionSolved(this, rejectData, start, length); } } public void Reject(byte[] rejectData, int start, int length) { Reject(rejectData, start, length, force: false); } public void RejectForce(byte[] rejectData, int start, int length) { Reject(rejectData, start, length, force: true); } public void RejectForce() { Reject(null, 0, 0, force: true); } public void RejectForce(byte[] rejectData) { Reject(rejectData, 0, rejectData.Length, force: true); } public void RejectForce(NetDataWriter rejectData) { Reject(rejectData.Data, 0, rejectData.Length, force: true); } public void Reject() { Reject(null, 0, 0, force: false); } public void Reject(byte[] rejectData) { Reject(rejectData, 0, rejectData.Length, force: false); } public void Reject(NetDataWriter rejectData) { Reject(rejectData.Data, 0, rejectData.Length, force: false); } } public enum UnconnectedMessageType { BasicMessage, Broadcast } public enum DisconnectReason { ConnectionFailed, Timeout, HostUnreachable, NetworkUnreachable, RemoteConnectionClose, DisconnectPeerCalled, ConnectionRejected, InvalidProtocol, UnknownHost, Reconnect, PeerToPeerConnection, PeerNotFound } public struct DisconnectInfo { public DisconnectReason Reason; public SocketError SocketErrorCode; public NetPacketReader AdditionalData; } public interface INetEventListener { void OnPeerConnected(NetPeer peer); void OnPeerDisconnected(NetPeer peer, DisconnectInfo disconnectInfo); void OnNetworkError(IPEndPoint endPoint, SocketError socketError); void OnNetworkReceive(NetPeer peer, NetPacketReader reader, byte channelNumber, DeliveryMethod deliveryMethod); void OnNetworkReceiveUnconnected(IPEndPoint remoteEndPoint, NetPacketReader reader, UnconnectedMessageType messageType); void OnNetworkLatencyUpdate(NetPeer peer, int latency); void OnConnectionRequest(ConnectionRequest request); } public interface IDeliveryEventListener { void OnMessageDelivered(NetPeer peer, object userData); } public interface INtpEventListener { void OnNtpResponse(NtpPacket packet); } public interface IPeerAddressChangedListener { void OnPeerAddressChanged(NetPeer peer, IPEndPoint previousAddress); } public class EventBasedNetListener : INetEventListener, IDeliveryEventListener, INtpEventListener, IPeerAddressChangedListener { public delegate void OnPeerConnected(NetPeer peer); public delegate void OnPeerDisconnected(NetPeer peer, DisconnectInfo disconnectInfo); public delegate void OnNetworkError(IPEndPoint endPoint, SocketError socketError); public delegate void OnNetworkReceive(NetPeer peer, NetPacketReader reader, byte channel, DeliveryMethod deliveryMethod); public delegate void OnNetworkReceiveUnconnected(IPEndPoint remoteEndPoint, NetPacketReader reader, UnconnectedMessageType messageType); public delegate void OnNetworkLatencyUpdate(NetPeer peer, int latency); public delegate void OnConnectionRequest(ConnectionRequest request); public delegate void OnDeliveryEvent(NetPeer peer, object userData); public delegate void OnNtpResponseEvent(NtpPacket packet); public delegate void OnPeerAddressChangedEvent(NetPeer peer, IPEndPoint previousAddress); public event OnPeerConnected PeerConnectedEvent; public event OnPeerDisconnected PeerDisconnectedEvent; public event OnNetworkError NetworkErrorEvent; public event OnNetworkReceive NetworkReceiveEvent; public event OnNetworkReceiveUnconnected NetworkReceiveUnconnectedEvent; public event OnNetworkLatencyUpdate NetworkLatencyUpdateEvent; public event OnConnectionRequest ConnectionRequestEvent; public event OnDeliveryEvent DeliveryEvent; public event OnNtpResponseEvent NtpResponseEvent; public event OnPeerAddressChangedEvent PeerAddressChangedEvent; public void ClearPeerConnectedEvent() { this.PeerConnectedEvent = null; } public void ClearPeerDisconnectedEvent() { this.PeerDisconnectedEvent = null; } public void ClearNetworkErrorEvent() { this.NetworkErrorEvent = null; } public void ClearNetworkReceiveEvent() { this.NetworkReceiveEvent = null; } public void ClearNetworkReceiveUnconnectedEvent() { this.NetworkReceiveUnconnectedEvent = null; } public void ClearNetworkLatencyUpdateEvent() { this.NetworkLatencyUpdateEvent = null; } public void ClearConnectionRequestEvent() { this.ConnectionRequestEvent = null; } public void ClearDeliveryEvent() { this.DeliveryEvent = null; } public void ClearNtpResponseEvent() { this.NtpResponseEvent = null; } public void ClearPeerAddressChangedEvent() { this.PeerAddressChangedEvent = null; } void INetEventListener.OnPeerConnected(NetPeer peer) { if (this.PeerConnectedEvent != null) { this.PeerConnectedEvent(peer); } } void INetEventListener.OnPeerDisconnected(NetPeer peer, DisconnectInfo disconnectInfo) { if (this.PeerDisconnectedEvent != null) { this.PeerDisconnectedEvent(peer, disconnectInfo); } } void INetEventListener.OnNetworkError(IPEndPoint endPoint, SocketError socketErrorCode) { if (this.NetworkErrorEvent != null) { this.NetworkErrorEvent(endPoint, socketErrorCode); } } void INetEventListener.OnNetworkReceive(NetPeer peer, NetPacketReader reader, byte channelNumber, DeliveryMethod deliveryMethod) { if (this.NetworkReceiveEvent != null) { this.NetworkReceiveEvent(peer, reader, channelNumber, deliveryMethod); } } void INetEventListener.OnNetworkReceiveUnconnected(IPEndPoint remoteEndPoint, NetPacketReader reader, UnconnectedMessageType messageType) { if (this.NetworkReceiveUnconnectedEvent != null) { this.NetworkReceiveUnconnectedEvent(remoteEndPoint, reader, messageType); } } void INetEventListener.OnNetworkLatencyUpdate(NetPeer peer, int latency) { if (this.NetworkLatencyUpdateEvent != null) { this.NetworkLatencyUpdateEvent(peer, latency); } } void INetEventListener.OnConnectionRequest(ConnectionRequest request) { if (this.ConnectionRequestEvent != null) { this.ConnectionRequestEvent(request); } } void IDeliveryEventListener.OnMessageDelivered(NetPeer peer, object userData) { if (this.DeliveryEvent != null) { this.DeliveryEvent(peer, userData); } } void INtpEventListener.OnNtpResponse(NtpPacket packet) { if (this.NtpResponseEvent != null) { this.NtpResponseEvent(packet); } } void IPeerAddressChangedListener.OnPeerAddressChanged(NetPeer peer, IPEndPoint previousAddress) { if (this.PeerAddressChangedEvent != null) { this.PeerAddressChangedEvent(peer, previousAddress); } } } internal sealed class NetConnectRequestPacket { public const int HeaderSize = 18; public readonly long ConnectionTime; public byte ConnectionNumber; public readonly byte[] TargetAddress; public readonly NetDataReader Data; public readonly int PeerId; private NetConnectRequestPacket(long connectionTime, byte connectionNumber, int localId, byte[] targetAddress, NetDataReader data) { ConnectionTime = connectionTime; ConnectionNumber = connectionNumber; TargetAddress = targetAddress; Data = data; PeerId = localId; } public static int GetProtocolId(NetPacket packet) { return BitConverter.ToInt32(packet.RawData, 1); } public static NetConnectRequestPacket FromData(NetPacket packet) { if (packet.ConnectionNumber >= 4) { return null; } long connectionTime = BitConverter.ToInt64(packet.RawData, 5); int localId = BitConverter.ToInt32(packet.RawData, 13); int num = packet.RawData[17]; if (num != 16 && num != 28) { return null; } byte[] array = new byte[num]; Buffer.BlockCopy(packet.RawData, 18, array, 0, num); NetDataReader netDataReader = new NetDataReader(null, 0, 0); if (packet.Size > 18 + num) { netDataReader.SetSource(packet.RawData, 18 + num, packet.Size); } return new NetConnectRequestPacket(connectionTime, packet.ConnectionNumber, localId, array, netDataReader); } public static NetPacket Make(NetDataWriter connectData, SocketAddress addressBytes, long connectTime, int localId) { NetPacket netPacket = new NetPacket(PacketProperty.ConnectRequest, connectData.Length + addressBytes.Size); FastBitConverter.GetBytes(netPacket.RawData, 1, 13); FastBitConverter.GetBytes(netPacket.RawData, 5, connectTime); FastBitConverter.GetBytes(netPacket.RawData, 13, localId); netPacket.RawData[17] = (byte)addressBytes.Size; for (int i = 0; i < addressBytes.Size; i++) { netPacket.RawData[18 + i] = addressBytes[i]; } Buffer.BlockCopy(connectData.Data, 0, netPacket.RawData, 18 + addressBytes.Size, connectData.Length); return netPacket; } } internal sealed class NetConnectAcceptPacket { public const int Size = 15; public readonly long ConnectionTime; public readonly byte ConnectionNumber; public readonly int PeerId; public readonly bool PeerNetworkChanged; private NetConnectAcceptPacket(long connectionTime, byte connectionNumber, int peerId, bool peerNetworkChanged) { ConnectionTime = connectionTime; ConnectionNumber = connectionNumber; PeerId = peerId; PeerNetworkChanged = peerNetworkChanged; } public static NetConnectAcceptPacket FromData(NetPacket packet) { if (packet.Size != 15) { return null; } long connectionTime = BitConverter.ToInt64(packet.RawData, 1); byte b = packet.RawData[9]; if (b >= 4) { return null; } byte b2 = packet.RawData[10]; if (b2 > 1) { return null; } int num = BitConverter.ToInt32(packet.RawData, 11); if (num < 0) { return null; } return new NetConnectAcceptPacket(connectionTime, b, num, b2 == 1); } public static NetPacket Make(long connectTime, byte connectNum, int localPeerId) { NetPacket netPacket = new NetPacket(PacketProperty.ConnectAccept, 0); FastBitConverter.GetBytes(netPacket.RawData, 1, connectTime); netPacket.RawData[9] = connectNum; FastBitConverter.GetBytes(netPacket.RawData, 11, localPeerId); return netPacket; } public static NetPacket MakeNetworkChanged(NetPeer peer) { NetPacket netPacket = new NetPacket(PacketProperty.PeerNotFound, 14); FastBitConverter.GetBytes(netPacket.RawData, 1, peer.ConnectTime); netPacket.RawData[9] = peer.ConnectionNum; netPacket.RawData[10] = 1; FastBitConverter.GetBytes(netPacket.RawData, 11, peer.RemoteId); return netPacket; } } internal static class NativeSocket { private static class WinSock { private const string LibName = "ws2_32.dll"; [DllImport("ws2_32.dll", SetLastError = true)] public static extern int recvfrom(IntPtr socketHandle, [In][Out] byte[] pinnedBuffer, [In] int len, [In] SocketFlags socketFlags, [Out] byte[] socketAddress, [In][Out] ref int socketAddressSize); [DllImport("ws2_32.dll", SetLastError = true)] internal unsafe static extern int sendto(IntPtr socketHandle, byte* pinnedBuffer, [In] int len, [In] SocketFlags socketFlags, [In] byte[] socketAddress, [In] int socketAddressSize); } private static class UnixSock { private const string LibName = "libc"; [DllImport("libc", SetLastError = true)] public static extern int recvfrom(IntPtr socketHandle, [In][Out] byte[] pinnedBuffer, [In] int len, [In] SocketFlags socketFlags, [Out] byte[] socketAddress, [In][Out] ref int socketAddressSize); [DllImport("libc", SetLastError = true)] internal unsafe static extern int sendto(IntPtr socketHandle, byte* pinnedBuffer, [In] int len, [In] SocketFlags socketFlags, [In] byte[] socketAddress, [In] int socketAddressSize); } public static readonly bool IsSupported; public static readonly bool UnixMode; public const int IPv4AddrSize = 16; public const int IPv6AddrSize = 28; public const int AF_INET = 2; public const int AF_INET6 = 10; private static readonly Dictionary<int, SocketError> NativeErrorToSocketError; static NativeSocket() { IsSupported = false; UnixMode = false; NativeErrorToSocketError = new Dictionary<int, SocketError> { { 13, SocketError.AccessDenied }, { 98, SocketError.AddressAlreadyInUse }, { 99, SocketError.AddressNotAvailable }, { 97, SocketError.AddressFamilyNotSupported }, { 11, SocketError.WouldBlock }, { 114, SocketError.AlreadyInProgress }, { 9, SocketError.OperationAborted }, { 125, SocketError.OperationAborted }, { 103, SocketError.ConnectionAborted }, { 111, SocketError.ConnectionRefused }, { 104, SocketError.ConnectionReset }, { 89, SocketError.DestinationAddressRequired }, { 14, SocketError.Fault }, { 112, SocketError.HostDown }, { 6, SocketError.HostNotFound }, { 113, SocketError.HostUnreachable }, { 115, SocketError.InProgress }, { 4, SocketError.Interrupted }, { 22, SocketError.InvalidArgument }, { 106, SocketError.IsConnected }, { 24, SocketError.TooManyOpenSockets }, { 90, SocketError.MessageSize }, { 100, SocketError.NetworkDown }, { 102, SocketError.NetworkReset }, { 101, SocketError.NetworkUnreachable }, { 23, SocketError.TooManyOpenSockets }, { 105, SocketError.NoBufferSpaceAvailable }, { 61, SocketError.NoData }, { 2, SocketError.AddressNotAvailable }, { 92, SocketError.ProtocolOption }, { 107, SocketError.NotConnected }, { 88, SocketError.NotSocket }, { 3440, SocketError.OperationNotSupported }, { 1, SocketError.AccessDenied }, { 32, SocketError.Shutdown }, { 96, SocketError.ProtocolFamilyNotSupported }, { 93, SocketError.ProtocolNotSupported }, { 91, SocketError.ProtocolType }, { 94, SocketError.SocketNotSupported }, { 108, SocketError.Disconnecting }, { 110, SocketError.TimedOut }, { 0, SocketError.Success } }; if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) { IsSupported = true; UnixMode = true; } else if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) { IsSupported = true; } } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static int RecvFrom(IntPtr socketHandle, byte[] pinnedBuffer, int len, byte[] socketAddress, ref int socketAddressSize) { if (!UnixMode) { return WinSock.recvfrom(socketHandle, pinnedBuffer, len, SocketFlags.None, socketAddress, ref socketAddressSize); } return UnixSock.recvfrom(socketHandle, pinnedBuffer, len, SocketFlags.None, socketAddress, ref socketAddressSize); } [MethodImpl(MethodImplOptions.AggressiveInlining)] public unsafe static int SendTo(IntPtr socketHandle, byte* pinnedBuffer, int len, byte[] socketAddress, int socketAddressSize) { if (!UnixMode) { return WinSock.sendto(socketHandle, pinnedBuffer, len, SocketFlags.None, socketAddress, socketAddressSize); } return UnixSock.sendto(socketHandle, pinnedBuffer, len, SocketFlags.None, socketAddress, socketAddressSize); } public static SocketError GetSocketError() { int lastWin32Error = Marshal.GetLastWin32Error(); if (UnixMode) { if (!NativeErrorToSocketError.TryGetValue(lastWin32Error, out var value)) { return SocketError.SocketError; } return value; } return (SocketError)lastWin32Error; } public static SocketException GetSocketException() { int lastWin32Error = Marshal.GetLastWin32Error(); if (UnixMode) { if (!NativeErrorToSocketError.TryGetValue(lastWin32Error, out var value)) { return new SocketException(-1); } return new SocketException((int)value); } return new SocketException(lastWin32Error); } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static short GetNativeAddressFamily(IPEndPoint remoteEndPoint) { if (!UnixMode) { return (short)remoteEndPoint.AddressFamily; } return (short)((remoteEndPoint.AddressFamily == AddressFamily.InterNetwork) ? 2 : 10); } } public enum NatAddressType { Internal, External } public interface INatPunchListener { void OnNatIntroductionRequest(IPEndPoint localEndPoint, IPEndPoint remoteEndPoint, string token); void OnNatIntroductionSuccess(IPEndPoint targetEndPoint, NatAddressType type, string token); } public class EventBasedNatPunchListener : INatPunchListener { public delegate void OnNatIntroductionRequest(IPEndPoint localEndPoint, IPEndPoint remoteEndPoint, string token); public delegate void OnNatIntroductionSuccess(IPEndPoint targetEndPoint, NatAddressType type, string token); public event OnNatIntroductionRequest NatIntroductionRequest; public event OnNatIntroductionSuccess NatIntroductionSuccess; void INatPunchListener.OnNatIntroductionRequest(IPEndPoint localEndPoint, IPEndPoint remoteEndPoint, string token) { if (this.NatIntroductionRequest != null) { this.NatIntroductionRequest(localEndPoint, remoteEndPoint, token); } } void INatPunchListener.OnNatIntroductionSuccess(IPEndPoint targetEndPoint, NatAddressType type, string token) { if (this.NatIntroductionSuccess != null) { this.NatIntroductionSuccess(targetEndPoint, type, token); } } } public sealed class NatPunchModule { private struct RequestEventData { public IPEndPoint LocalEndPoint; public IPEndPoint RemoteEndPoint; public string Token; } private struct SuccessEventData { public IPEndPoint TargetEndPoint; public NatAddressType Type; public string Token; } private class NatIntroduceRequestPacket { public IPEndPoint Internal { [Preserve] get; [Preserve] set; } public string Token { [Preserve] get; [Preserve] set; } } private class NatIntroduceResponsePacket { public IPEndPoint Internal { [Preserve] get; [Preserve] set; } public IPEndPoint External { [Preserve] get; [Preserve] set; } public string Token { [Preserve] get; [Preserve] set; } } private class NatPunchPacket { public string Token { [Preserve] get; [Preserve] set; } public bool IsExternal { [Preserve] get; [Preserve] set; } } private readonly NetManager _socket; private readonly ConcurrentQueue<RequestEventData> _requestEvents = new ConcurrentQueue<RequestEventData>(); private readonly ConcurrentQueue<SuccessEventData> _successEvents = new ConcurrentQueue<SuccessEventData>(); private readonly NetDataReader _cacheReader = new NetDataReader(); private readonly NetDataWriter _cacheWriter = new NetDataWriter(); private readonly NetPacketProcessor _netPacketProcessor = new NetPacketProcessor(256); private INatPunchListener _natPunchListener; public const int MaxTokenLength = 256; public bool UnsyncedEvents; internal NatPunchModule(NetManager socket) { _socket = socket; _netPacketProcessor.SubscribeReusable<NatIntroduceResponsePacket>(OnNatIntroductionResponse); _netPacketProcessor.SubscribeReusable<NatIntroduceRequestPacket, IPEndPoint>(OnNatIntroductionRequest); _netPacketProcessor.SubscribeReusable<NatPunchPacket, IPEndPoint>(OnNatPunch); } internal void ProcessMessage(IPEndPoint senderEndPoint, NetPacket packet) { lock (_cacheReader) { _cacheReader.SetSource(packet.RawData, 1, packet.Size); _netPacketProcessor.ReadAllPackets(_cacheReader, senderEndPoint); } } public void Init(INatPunchListener listener) { _natPunchListener = listener; } private void Send<T>(T packet, IPEndPoint target) where T : class, new() { _cacheWriter.Reset(); _cacheWriter.Put((byte)16); _netPacketProcessor.Write(_cacheWriter, packet); _socket.SendRaw(_cacheWriter.Data, 0, _cacheWriter.Length, target); } public void NatIntroduce(IPEndPoint hostInternal, IPEndPoint hostExternal, IPEndPoint clientInternal, IPEndPoint clientExternal, string additionalInfo) { NatIntroduceResponsePacket natIntroduceResponsePacket = new NatIntroduceResponsePacket { Token = additionalInfo }; natIntroduceResponsePacket.Internal = hostInternal; natIntroduceResponsePacket.External = hostExternal; Send(natIntroduceResponsePacket, clientExternal); natIntroduceResponsePacket.Internal = clientInternal; natIntroduceResponsePacket.External = clientExternal; Send(natIntroduceResponsePacket, hostExternal); } public void PollEvents() { if (!UnsyncedEvents && _natPunchListener != null && (!_successEvents.IsEmpty || !_requestEvents.IsEmpty)) { SuccessEventData result; while (_successEvents.TryDequeue(out result)) { _natPunchListener.OnNatIntroductionSuccess(result.TargetEndPoint, result.Type, result.Token); } RequestEventData result2; while (_requestEvents.TryDequeue(out result2)) { _natPunchListener.OnNatIntroductionRequest(result2.LocalEndPoint, result2.RemoteEndPoint, result2.Token); } } } public void SendNatIntroduceRequest(string host, int port, string additionalInfo) { SendNatIntroduceRequest(NetUtils.MakeEndPoint(host, port), additionalInfo); } public void SendNatIntroduceRequest(IPEndPoint masterServerEndPoint, string additionalInfo) { string localIp = NetUtils.GetLocalIp(LocalAddrType.IPv4); if (string.IsNullOrEmpty(localIp) || masterServerEndPoint.AddressFamily == AddressFamily.InterNetworkV6) { localIp = NetUtils.GetLocalIp(LocalAddrType.IPv6); } Send(new NatIntroduceRequestPacket { Internal = NetUtils.MakeEndPoint(localIp, _socket.LocalPort), Token = additionalInfo }, masterServerEndPoint); } private void OnNatIntroductionRequest(NatIntroduceRequestPacket req, IPEndPoint senderEndPoint) { if (UnsyncedEvents) { _natPunchListener.OnNatIntroductionRequest(req.Internal, senderEndPoint, req.Token); return; } _requestEvents.Enqueue(new RequestEventData { LocalEndPoint = req.Internal, RemoteEndPoint = senderEndPoint, Token = req.Token }); } private void OnNatIntroductionResponse(NatIntroduceResponsePacket req) { NatPunchPacket natPunchPacket = new NatPunchPacket { Token = req.Token }; Send(natPunchPacket, req.Internal); _socket.Ttl = 2; _socket.SendRaw(new byte[1] { 17 }, 0, 1, req.External); _socket.Ttl = 255; natPunchPacket.IsExternal = true; Send(natPunchPacket, req.External); } private void OnNatPunch(NatPunchPacket req, IPEndPoint senderEndPoint) { if (UnsyncedEvents) { _natPunchListener.OnNatIntroductionSuccess(senderEndPoint, req.IsExternal ? NatAddressType.External : NatAddressType.Internal, req.Token); return; } _successEvents.Enqueue(new SuccessEventData { TargetEndPoint = senderEndPoint, Type = (req.IsExternal ? NatAddressType.External : NatAddressType.Internal), Token = req.Token }); } } public enum DeliveryMethod : byte { Unreliable = 4, ReliableUnordered = 0, Sequenced = 1, ReliableOrdered = 2, ReliableSequenced = 3 } public static class NetConstants { public const int DefaultWindowSize = 64; public const int SocketBufferSize = 1048576; public const int SocketTTL = 255; public const int HeaderSize = 1; public const int ChanneledHeaderSize = 4; public const int FragmentHeaderSize = 6; public const int FragmentedHeaderTotalSize = 10; public const ushort MaxSequence = 32768; public const ushort HalfMaxSequence = 16384; internal const int ProtocolId = 13; internal const int MaxUdpHeaderSize = 68; internal const int ChannelTypeCount = 4; internal static readonly int[] PossibleMtu = new int[7] { 508, 1024, 1164, 1392, 1404, 1424, 1432 }; public static readonly int MaxPacketSize = PossibleMtu[PossibleMtu.Length - 1]; public static readonly int MaxUnreliableDataSize = MaxPacketSize - 1; public const byte MaxConnectionNumber = 4; } public class InvalidPacketException : ArgumentException { public InvalidPacketException(string message) : base(message) { } } public class TooBigPacketException : InvalidPacketException { public TooBigPacketException(string message) : base(message) { } } public enum NetLogLevel { Warning, Error, Trace, Info } public interface INetLogger { void WriteNet(NetLogLevel level, string str, params object[] args); } public static class NetDebug { public static INetLogger Logger = null; private static readonly object DebugLogLock = new object(); private static void WriteLogic(NetLogLevel logLevel, string str, params object[] args) { lock (DebugLogLock) { if (Logger == null) { Console.WriteLine(str, args); } else { Logger.WriteNet(logLevel, str, args); } } } [Conditional("DEBUG_MESSAGES")] internal static void Write(string str) { WriteLogic(NetLogLevel.Trace, str); } [Conditional("DEBUG_MESSAGES")] internal static void Write(NetLogLevel level, string str) { WriteLogic(level, str); } [Conditional("DEBUG_MESSAGES")] [Conditional("DEBUG")] internal static void WriteForce(string str) { WriteLogic(NetLogLevel.Trace, str); } [Conditional("DEBUG_MESSAGES")] [Conditional("DEBUG")] internal static void WriteForce(NetLogLevel level, string str) { WriteLogic(level, str); } internal static void WriteError(string str) { WriteLogic(NetLogLevel.Error, str); } } public sealed class NetPacketReader : NetDataReader { private NetPacket _packet; private readonly NetManager _manager; private readonly NetEvent _evt; internal NetPacketReader(NetManager manager, NetEvent evt) { _manager = manager; _evt = evt; } internal void SetSource(NetPacket packet, int headerSize) { if (packet != null) { _packet = packet; SetSource(packet.RawData, headerSize, packet.Size); } } internal void RecycleInternal() { Clear(); if (_packet != null) { _manager.PoolRecycle(_packet); } _packet = null; _manager.RecycleEvent(_evt); } public void Recycle() { if (!_manager.AutoRecycle) { RecycleInternal(); } } } internal sealed class NetEvent { public enum EType { Connect, Disconnect, Receive, ReceiveUnconnected, Error, ConnectionLatencyUpdated, Broadcast, ConnectionRequest, MessageDelivered, PeerAddressChanged } public NetEvent Next; public EType Type; public NetPeer Peer; public IPEndPoint RemoteEndPoint; public object UserData; public int Latency; public SocketError ErrorCode; public DisconnectReason DisconnectReason; public ConnectionRequest ConnectionRequest; public DeliveryMethod DeliveryMethod; public byte ChannelNumber; public readonly NetPacketReader DataReader; public NetEvent(NetManager manager) { DataReader = new NetPacketReader(manager, this); } } public class NetManager : IEnumerable<NetPeer>, IEnumerable { public struct NetPeerEnumerator : IEnumerator<NetPeer>, IEnumerator, IDisposable { private readonly NetPeer _initialPeer; private NetPeer _p; public NetPeer Current => _p; object IEnumerator.Current => _p; public NetPeerEnumerator(NetPeer p) { _initialPeer = p; _p = null; } public void Dispose() { } public bool MoveNext() { _p = ((_p == null) ? _initialPeer : _p.NextPeer); return _p != null; } public void Reset() { throw new NotSupportedException(); } } private struct Slot { internal int HashCode; internal int Next; internal NetPeer Value; } private Thread _logicThread; private bool _manualMode; private readonly AutoResetEvent _updateTriggerEvent = new AutoResetEvent(initialState: true); private NetEvent _pendingEventHead; private NetEvent _pendingEventTail; private NetEvent _netEventPoolHead; private readonly INetEventListener _netEventListener; private readonly IDeliveryEventListener _deliveryEventListener; private readonly INtpEventListener _ntpEventListener; private readonly IPeerAddressChangedListener _peerAddressChangedListener; private readonly Dictionary<IPEndPoint, ConnectionRequest> _requestsDict = new Dictionary<IPEndPoint, ConnectionRequest>(); private readonly ConcurrentDictionary<IPEndPoint, NtpRequest> _ntpRequests = new ConcurrentDictionary<IPEndPoint, NtpRequest>(); private int _connectedPeersCount; private readonly List<NetPeer> _connectedPeerListCache = new List<NetPeer>(); private readonly PacketLayerBase _extraPacketLayer; private int _lastPeerId; private ConcurrentQueue<int> _peerIds = new ConcurrentQueue<int>(); private byte _channelsCount = 1; private readonly object _eventLock = new object(); public bool UnconnectedMessagesEnabled; public bool NatPunchEnabled; public int UpdateTime = 15; public int PingInterval = 1000; public int DisconnectTimeout = 5000; public bool SimulatePacketLoss; public bool SimulateLatency; public int SimulationPacketLossChance = 10; public int SimulationMinLatency = 30; public int SimulationMaxLatency = 100; public bool UnsyncedEvents; public bool UnsyncedReceiveEvent; public bool UnsyncedDeliveryEvent; public bool BroadcastReceiveEnabled; public int ReconnectDelay = 500; public int MaxConnectAttempts = 10; public bool ReuseAddress; public bool DontRoute; public readonly NetStatistics Statistics = new NetStatistics(); public bool EnableStatistics; public readonly NatPunchModule NatPunchModule; public bool AutoRecycle; public bool IPv6Enabled = true; public int MtuOverride; public bool UseSafeMtu; public bool UseNativeSockets; public bool DisconnectOnUnreachable; public bool AllowPeerAddressChange; private const int MaxPrimeArrayLength = 2147483587; private const int HashPrime = 101; private const int Lower31BitMask = int.MaxValue; private static readonly int[] Primes; private int[] _buckets; private Slot[] _slots; private int _count; private int _lastIndex; private int _freeList = -1; private NetPeer[] _peersArray = new NetPeer[32]; private readonly ReaderWriterLockSlim _peersLock = new ReaderWriterLockSlim(LockRecursionPolicy.NoRecursion); private volatile NetPeer _headPeer; private NetPacket _poolHead; private int _poolCount; private readonly object _poolLock = new object(); public int PacketPoolSize = 1000; private const int ReceivePollingTime = 500000; private Socket _udpSocketv4; private Socket _udpSocketv6; private Thread _receiveThread; private IPEndPoint _bufferEndPointv4; private IPEndPoint _bufferEndPointv6; private const int SioUdpConnreset = -1744830452; private static readonly IPAddress MulticastAddressV6; public static readonly bool IPv6Support; public int MaxPacketsReceivePerUpdate; internal bool NotConnected; public bool IsRunning { get; private set; } public int LocalPort { get; private set; } public NetPeer FirstPeer => _headPeer; public byte ChannelsCount { get { return _channelsCount; } set { if (value < 1 || value > 64) { throw new ArgumentException("Channels count must be between 1 and 64"); } _channelsCount = value; } } public List<NetPeer> ConnectedPeerList { get { GetPeersNonAlloc(_connectedPeerListCache, ConnectionState.Connected); return _connectedPeerListCache; } } public int ConnectedPeersCount => Interlocked.CompareExchange(ref _connectedPeersCount, 0, 0); public int ExtraPacketSizeForLayer => _extraPacketLayer?.ExtraPacketSizeForLayer ?? 0; public int PoolCount => _poolCount; public short Ttl { get { return _udpSocketv4.Ttl; } internal set { _udpSocketv4.Ttl = value; } } public NetManager(INetEventListener listener, PacketLayerBase extraPacketLayer = null) { _netEventListener = listener; _deliveryEventListener = listener as IDeliveryEventListener; _ntpEventListener = listener as INtpEventListener; _peerAddressChangedListener = listener as IPeerAddressChangedListener; NatPunchModule = new NatPunchModule(this); _extraPacketLayer = extraPacketLayer; } internal void ConnectionLatencyUpdated(NetPeer fromPeer, int latency) { CreateEvent(NetEvent.EType.ConnectionLatencyUpdated, fromPeer, null, SocketError.Success, latency, DisconnectReason.ConnectionFailed, null, DeliveryMethod.Unreliable, 0); } internal void MessageDelivered(NetPeer fromPeer, object userData) { if (_deliveryEventListener != null) { CreateEvent(NetEvent.EType.MessageDelivered, fromPeer, null, SocketError.Success, 0, DisconnectReason.ConnectionFailed, null, DeliveryMethod.Unreliable, 0, null, userData); } } internal void DisconnectPeerForce(NetPeer peer, DisconnectReason reason, SocketError socketErrorCode, NetPacket eventData) { DisconnectPeer(peer, reason, socketErrorCode, force: true, null, 0, 0, eventData); } private void DisconnectPeer(NetPeer peer, DisconnectReason reason, SocketError socketErrorCode, bool force, byte[] data, int start, int count, NetPacket eventData) { switch (peer.Shutdown(data, start, count, force)) { case ShutdownResult.None: return; case ShutdownResult.WasConnected: Interlocked.Decrement(ref _connectedPeersCount); break; } CreateEvent(NetEvent.EType.Disconnect, peer, null, socketErrorCode, 0, reason, null, DeliveryMethod.Unreliable, 0, eventData); } private void CreateEvent(NetEvent.EType type, NetPeer peer = null, IPEndPoint remoteEndPoint = null, SocketError errorCode = SocketError.Success, int latency = 0, DisconnectReason disconnectReason = DisconnectReason.ConnectionFailed, ConnectionRequest connectionRequest = null, DeliveryMethod deliveryMethod = DeliveryMethod.Unreliable, byte channelNumber = 0, NetPacket readerSource = null, object userData = null) { bool flag = UnsyncedEvents; switch (type) { case NetEvent.EType.Connect: Interlocked.Increment(ref _connectedPeersCount); break; case NetEvent.EType.MessageDelivered: flag = UnsyncedDeliveryEvent; break; } NetEvent netEvent; lock (_eventLock) { netEvent = _netEventPoolHead; if (netEvent == null) { netEvent = new NetEvent(this); } else { _netEventPoolHead = netEvent.Next; } } netEvent.Next = null; netEvent.Type = type; netEvent.DataReader.SetSource(readerSource, readerSource?.GetHeaderSize() ?? 0); netEvent.Peer = peer; netEvent.RemoteEndPoint = remoteEndPoint; netEvent.Latency = latency; netEvent.ErrorCode = errorCode; netEvent.DisconnectReason = disconnectReason; netEvent.ConnectionRequest = connectionRequest; netEvent.DeliveryMethod = deliveryMethod; netEvent.ChannelNumber = channelNumber; netEvent.UserData = userData; if (flag || _manualMode) { ProcessEvent(netEvent); return; } lock (_eventLock) { if (_pendingEventTail == null) { _pendingEventHead = netEvent; } else { _pendingEventTail.Next = netEvent; } _pendingEventTail = netEvent; } } private void ProcessEvent(NetEvent evt) { bool isNull = evt.DataReader.IsNull; switch (evt.Type) { case NetEvent.EType.Connect: _netEventListener.OnPeerConnected(evt.Peer); break; case NetEvent.EType.Disconnect: { DisconnectInfo disconnectInfo = default(DisconnectInfo); disconnectInfo.Reason = evt.DisconnectReason; disconnectInfo.AdditionalData = evt.DataReader; disconnectInfo.SocketErrorCode = evt.ErrorCode; DisconnectInfo disconnectInfo2 = disconnectInfo; _netEventListener.OnPeerDisconnected(evt.Peer, disconnectInfo2); break; } case NetEvent.EType.Receive: _netEventListener.OnNetworkReceive(evt.Peer, evt.DataReader, evt.ChannelNumber, evt.DeliveryMethod); break; case NetEvent.EType.ReceiveUnconnected: _netEventListener.OnNetworkReceiveUnconnected(evt.RemoteEndPoint, evt.DataReader, UnconnectedMessageType.BasicMessage); break; case NetEvent.EType.Broadcast: _netEventListener.OnNetworkReceiveUnconnected(evt.RemoteEndPoint, evt.DataReader, UnconnectedMessageType.Broadcast); break; case NetEvent.EType.Error: _netEventListener.OnNetworkError(evt.RemoteEndPoint, evt.ErrorCode); break; case NetEvent.EType.ConnectionLatencyUpdated: _netEventListener.OnNetworkLatencyUpdate(evt.Peer, evt.Latency); break; case NetEvent.EType.ConnectionRequest: _netEventListener.OnConnectionRequest(evt.ConnectionRequest); break; case NetEvent.EType.MessageDelivered: _deliveryEventListener.OnMessageDelivered(evt.Peer, evt.UserData); break; case NetEvent.EType.PeerAddressChanged: { _peersLock.EnterUpgradeableReadLock(); IPEndPoint iPEndPoint = null; if (ContainsPeer(evt.Peer)) { _peersLock.EnterWriteLock(); RemovePeerFromSet(evt.Peer); iPEndPoint = new IPEndPoint(evt.Peer.Address, evt.Peer.Port); evt.Peer.FinishEndPointChange(evt.RemoteEndPoint); AddPeerToSet(evt.Peer); _peersLock.ExitWriteLock(); } _peersLock.ExitUpgradeableReadLock(); if (iPEndPoint != null && _peerAddressChangedListener != null) { _peerAddressChangedListener.OnPeerAddressChanged(evt.Peer, iPEndPoint); } break; } } if (isNull) { RecycleEvent(evt); } else if (AutoRecycle) { evt.DataReader.RecycleInternal(); } } internal void RecycleEvent(NetEvent evt) { evt.Peer = null; evt.ErrorCode = SocketError.Success; evt.RemoteEndPoint = null; evt.ConnectionRequest = null; lock (_eventLock) { evt.Next = _netEventPoolHead; _netEventPoolHead = evt; } } private void UpdateLogic() { List<NetPeer> list = new List<NetPeer>(); Stopwatch stopwatch = new Stopwatch(); stopwatch.Start(); while (IsRunning) { try { int num = (int)stopwatch.ElapsedMilliseconds; num = ((num <= 0) ? 1 : num); stopwatch.Restart(); for (NetPeer netPeer = _headPeer; netPeer != null; netPeer = netPeer.NextPeer) { if (netPeer.ConnectionState == ConnectionState.Disconnected && netPeer.TimeSinceLastPacket > DisconnectTimeout) { list.Add(netPeer); } else { netPeer.Update(num); } } if (list.Count > 0) { _peersLock.EnterWriteLock(); for (int i = 0; i < list.Count; i++) { RemovePeerInternal(list[i]); } _peersLock.ExitWriteLock(); list.Clear(); } ProcessNtpRequests(num); int num2 = UpdateTime - (int)stopwatch.ElapsedMilliseconds; if (num2 > 0) { _updateTriggerEvent.WaitOne(num2); } } catch (ThreadAbortException) { return; } catch (Exception ex2) { NetDebug.WriteError("[NM] LogicThread error: " + ex2); } } stopwatch.Stop(); } [Conditional("DEBUG")] private void ProcessDelayedPackets() { } private void ProcessNtpRequests(int elapsedMilliseconds) { List<IPEndPoint> list = null; foreach (KeyValuePair<IPEndPoint, NtpRequest> ntpRequest in _ntpRequests) { ntpRequest.Value.Send(_udpSocketv4, elapsedMilliseconds); if (ntpRequest.Value.NeedToKill) { if (list == null) { list = new List<IPEndPoint>(); } list.Add(ntpRequest.Key); } } if (list == null) { return; } foreach (IPEndPoint item in list) { _ntpRequests.TryRemove(item, out var _); } } public void ManualUpdate(int elapsedMilliseconds) { if (!_manualMode) { return; } for (NetPeer netPeer = _headPeer; netPeer != null; netPeer = netPeer.NextPeer) { if (netPeer.ConnectionState == ConnectionState.Disconnected && netPeer.TimeSinceLastPacket > DisconnectTimeout) { RemovePeerInternal(netPeer); } else { netPeer.Update(elapsedMilliseconds); } } ProcessNtpRequests(elapsedMilliseconds); } internal NetPeer OnConnectionSolved(ConnectionRequest request, byte[] rejectData, int start, int length) { NetPeer actualValue = null; if (request.Result == ConnectionRequestResult.RejectForce) { if (rejectData != null && length > 0) { NetPacket netPacket = PoolGetWithProperty(PacketProperty.Disconnect, length); netPacket.ConnectionNumber = request.InternalPacket.ConnectionNumber; FastBitConverter.GetBytes(netPacket.RawData, 1, request.InternalPacket.ConnectionTime); if (netPacket.Size >= NetConstants.PossibleMtu[0]) { NetDebug.WriteError("[Peer] Disconnect additional data size more than MTU!"); } else { Buffer.BlockCopy(rejectData, start, netPacket.RawData, 9, length); } SendRawAndRecycle(netPacket, request.RemoteEndPoint); } lock (_requestsDict) { _requestsDict.Remove(request.RemoteEndPoint); } } else { lock (_requestsDict) { if (!TryGetPeer(request.RemoteEndPoint, out actualValue)) { if (request.Result == ConnectionRequestResult.Reject) { actualValue = new NetPeer(this, request.RemoteEndPoint, GetNextPeerId()); actualValue.Reject(request.InternalPacket, rejectData, start, length); AddPeer(actualValue); } else { actualValue = new NetPeer(this, request, GetNextPeerId()); AddPeer(actualValue); CreateEvent(NetEvent.EType.Connect, actualValue, null, SocketError.Success, 0, DisconnectReason.ConnectionFailed, null, DeliveryMethod.Unreliable, 0); } } _requestsDict.Remove(request.RemoteEndPoint); } } return actualValue; } private int GetNextPeerId() { if (!_peerIds.TryDequeue(out var result)) { return _lastPeerId++; } return result; } private void ProcessConnectRequest(IPEndPoint remoteEndPoint, NetPeer netPeer, NetConnectRequestPacket connRequest) { if (netPeer != null) { ConnectRequestResult connectRequestResult = netPeer.ProcessConnectRequest(connRequest); switch (connectRequestResult) { default: return; case ConnectRequestResult.Reconnection: DisconnectPeerForce(netPeer, DisconnectReason.Reconnect, SocketError.Success, null); RemovePeer(netPeer); break; case ConnectRequestResult.NewConnection: RemovePeer(netPeer); break; case ConnectRequestResult.P2PLose: DisconnectPeerForce(netPeer, DisconnectReason.PeerToPeerConnection, SocketError.Success, null); RemovePeer(netPeer); break; } if (connectRequestResult != ConnectRequestResult.P2PLose) { connRequest.ConnectionNumber = (byte)((netPeer.ConnectionNum + 1) % 4); } } ConnectionRequest value; lock (_requestsDict) { if (_requestsDict.TryGetValue(remoteEndPoint, out value)) { value.UpdateRequest(connRequest); return; } value = new ConnectionRequest(remoteEndPoint, connRequest, this); _requestsDict.Add(remoteEndPoint, value); } CreateEvent(NetEvent.EType.ConnectionRequest, null, null, SocketError.Success, 0, DisconnectReason.ConnectionFailed, value, DeliveryMethod.Unreliable, 0); } private void OnMessageReceived(NetPacket packet, IPEndPoint remoteEndPoint) { if (packet.Size == 0) { PoolRecycle(packet); return; } int size = packet.Size; if (EnableStatistics) { Statistics.IncrementPacketsReceived(); Statistics.AddBytesReceived(size); } if (_ntpRequests.Count > 0 && _ntpRequests.TryGetValue(remoteEndPoint, out var _)) { if (packet.Size >= 48) { byte[] array = new byte[packet.Size]; Buffer.BlockCopy(packet.RawData, 0, array, 0, packet.Size); NtpPacket ntpPacket = NtpPacket.FromServerResponse(array, DateTime.UtcNow); try { ntpPacket.ValidateReply(); } catch (InvalidOperationException) { ntpPacket = null; } if (ntpPacket != null) { _ntpRequests.TryRemove(remoteEndPoint, out var _); _ntpEventListener?.OnNtpResponse(ntpPacket); } } return; } if (_extraPacketLayer != null) { _extraPacketLayer.ProcessInboundPacket(ref remoteEndPoint, ref packet.RawData, ref packet.Size); if (packet.Size == 0) { return; } } if (!packet.Verify()) { NetDebug.WriteError("[NM] DataReceived: bad!"); PoolRecycle(packet); return; } switch (packet.Property) { case PacketProperty.ConnectRequest: if (NetConnectRequestPacket.GetProtocolId(packet) != 13) { SendRawAndRecycle(PoolGetWithProperty(PacketProperty.InvalidProtocol), remoteEndPoint); return; } break; case PacketProperty.Broadcast: if (BroadcastReceiveEnabled) { CreateEvent(NetEvent.EType.Broadcast, null, remoteEndPoint, SocketError.Success, 0, DisconnectReason.ConnectionFailed, null, DeliveryMethod.Unreliable, 0, packet); } return; case PacketProperty.UnconnectedMessage: if (UnconnectedMessagesEnabled) { CreateEvent(NetEvent.EType.ReceiveUnconnected, null, remoteEndPoint, SocketError.Success, 0, DisconnectReason.ConnectionFailed, null, DeliveryMethod.Unreliable, 0, packet); } return; case PacketProperty.NatMessage: if (NatPunchEnabled) { NatPunchModule.ProcessMessage(remoteEndPoint, packet); } return; } NetPeer actualValue = remoteEndPoint as NetPeer; bool flag = actualValue != null || TryGetPeer(remoteEndPoint, out actualValue); if (flag && EnableStatistics) { actualValue.Statistics.IncrementPacketsReceived(); actualValue.Statistics.AddBytesReceived(size); } switch (packet.Property) { case PacketProperty.ConnectRequest: { NetConnectRequestPacket netConnectRequestPacket = NetConnectRequestPacket.FromData(packet); if (netConnectRequestPacket != null) { ProcessConnectRequest(remoteEndPoint, actualValue, netConnectRequestPacket); } break; } case PacketProperty.PeerNotFound: if (flag) { if (actualValue.ConnectionState == ConnectionState.Connected) { if (packet.Size == 1) { actualValue.ResetMtu(); SendRaw(NetConnectAcceptPacket.MakeNetworkChanged(actualValue), remoteEndPoint); } else if (packet.Size == 2 && packet.RawData[1] == 1) { DisconnectPeerForce(actualValue, DisconnectReason.PeerNotFound, SocketError.Success, null); } } } else { if (packet.Size <= 1) { break; } bool flag2 = false; if (AllowPeerAddressChange) { NetConnectAcceptPacket netConnectAcceptPacket = NetConnectAcceptPacket.FromData(packet); if (netConnectAcceptPacket != null && netConnectAcceptPacket.PeerNetworkChanged && netConnectAcceptPacket.PeerId < _peersArray.Length) { _peersLock.EnterUpgradeableReadLock(); NetPeer netPeer = _peersArray[netConnectAcceptPacket.PeerId]; _peersLock.ExitUpgradeableReadLock(); if (netPeer != null && netPeer.ConnectTime == netConnectAcceptPacket.ConnectionTime && netPeer.ConnectionNum == netConnectAcceptPacket.ConnectionNumber) { if (netPeer.ConnectionState == ConnectionState.Connected) { netPeer.InitiateEndPointChange(); CreateEvent(NetEvent.EType.PeerAddressChanged, netPeer, remoteEndPoint, SocketError.Success, 0, DisconnectReason.ConnectionFailed, null, DeliveryMethod.Unreliable, 0); } flag2 = true; } } } PoolRecycle(packet); if (!flag2) { NetPacket netPacket = PoolGetWithProperty(PacketProperty.PeerNotFound, 1); netPacket.RawData[1] = 1; SendRawAndRecycle(netPacket, remoteEndPoint); } } break; case PacketProperty.InvalidProtocol: if (flag && actualValue.ConnectionState == ConnectionState.Outgoing) { DisconnectPeerForce(actualValue, DisconnectReason.InvalidProtocol, SocketError.Success, null); } break; case PacketProperty.Disconnect: if (flag) { DisconnectResult disconnectResult = actualValue.ProcessDisconnect(packet); if (disconnectResult == DisconnectResult.None) { PoolRecycle(packet); break; } DisconnectPeerForce(actualValue, (disconnectResult == DisconnectResult.Disconnect) ? DisconnectReason.RemoteConnectionClose : DisconnectReason.ConnectionRejected, SocketError.Success, packet); } else { PoolRecycle(packet); } SendRawAndRecycle(PoolGetWithProperty(PacketProperty.ShutdownOk), remoteEndPoint); break; case PacketProperty.ConnectAccept: if (flag) { NetConnectAcceptPacket netConnectAcceptPacket2 = NetConnectAcceptPacket.FromData(packet); if (netConnectAcceptPacket2 != null && actualValue.ProcessConnectAccept(netConnectAcceptPacket2)) { CreateEvent(NetEvent.EType.Connect, actualValue, null, SocketError.Success, 0, DisconnectReason.ConnectionFailed, null, DeliveryMethod.Unreliable, 0); } } break; default: if (flag) { actualValue.ProcessPacket(packet); } else { SendRawAndRecycle(PoolGetWithProperty(PacketProperty.PeerNotFound), remoteEndPoint); } break; } } internal void CreateReceiveEvent(NetPacket packet, DeliveryMethod method, byte channelNumber, int headerSize, NetPeer fromPeer) { if (UnsyncedEvents || UnsyncedReceiveEvent || _manualMode) { NetEvent netEvent; lock (_eventLock) { netEvent = _netEventPoolHead; if (netEvent == null) { netEvent = new NetEvent(this); } else { _netEventPoolHead = netEvent.Next; } } netEvent.Next = null; netEvent.Type = NetEvent.EType.Receive; netEvent.DataReader.SetSource(packet, headerSize); netEvent.Peer = fromPeer; netEvent.DeliveryMethod = method; netEvent.ChannelNumber = channelNumber; ProcessEvent(netEvent); return; } lock (_eventLock) { NetEvent netEvent = _netEventPoolHead; if (netEvent == null) { netEvent = new NetEvent(this); } else { _netEventPoolHead = netEvent.Next; } netEvent.Next = null; netEvent.Type = NetEvent.EType.Receive; netEvent.DataReader.SetSource(packet, headerSize); netEvent.Peer = fromPeer; netEvent.DeliveryMethod = method; netEvent.ChannelNumber = channelNumber; if (_pendingEventTail == null) { _pendingEventHead = netEvent; } else { _pendingEventTail.Next = netEvent; } _pendingEventTail = netEvent; } } public void SendToAll(NetDataWriter writer, DeliveryMethod options) { SendToAll(writer.Data, 0, writer.Length, options); } public void SendToAll(byte[] data, DeliveryMethod options) { SendToAll(data, 0, data.Length, options); } public void SendToAll(byte[] data, int start, int length, DeliveryMethod options) { SendToAll(data, start, length, 0, options); } public void SendToAll(NetDataWriter writer, byte channelNumber, DeliveryMethod options) { SendToAll(writer.Data, 0, writer.Length, channelNumber, options); } public void SendToAll(byte[] data, byte channelNumber, DeliveryMethod options) { SendToAll(data, 0, data.Length, channelNumber, options); } public void SendToAll(byte[] data, int start, int length, byte channelNumber, DeliveryMethod options) { try { _peersLock.EnterReadLock(); for (NetPeer netPeer = _headPeer; netPeer != null; netPeer = netPeer.NextPeer) { netPeer.Send(data, start, length, channelNumber, options); } } finally { _peersLock.ExitReadLock(); } } public void SendToAll(NetDataWriter writer, DeliveryMethod options, NetPeer excludePeer) { SendToAll(writer.Data, 0, writer.Length, 0, options, excludePeer); } public void SendToAll(byte[] data, DeliveryMethod options, NetPeer excludePeer) { SendToAll(data, 0, data.Length, 0, options, excludePeer); } public void SendToAll(byte[] data, int start, int length, DeliveryMethod options, NetPeer excludePeer) { SendToAll(data, start, length, 0, options, excludePeer); } public void SendToAll(NetDataWriter writer, byte channelNumber, DeliveryMethod options, NetPeer excludePeer) { SendToAll(writer.Data, 0, writer.Length, channelNumber, options, excludePeer); } public void SendToAll(byte[] data, byte channelNumber, DeliveryMethod options, NetPeer excludePeer) { SendToAll(data, 0, data.Length, channelNumber, options, excludePeer); } public void SendToAll(byte[] data, int start, int length, byte channelNumber, DeliveryMethod options, NetPeer excludePeer) { try { _peersLock.EnterReadLock(); for (NetPeer netPeer = _headPeer; netPeer != null; netPeer = netPeer.NextPeer) { if (netPeer != excludePeer) { netPeer.Send(data, start, length, channelNumber, options); } } } finally { _peersLock.ExitReadLock(); } } public void SendToAll(ReadOnlySpan<byte> data, DeliveryMethod options) { SendToAll(data, 0, options, null); } public void SendToAll(ReadOnlySpan<byte> data, DeliveryMethod options, NetPeer excludePeer) { SendToAll(data, 0, options, excludePeer); } public void SendToAll(ReadOnlySpan<byte> data, byte channelNumber, DeliveryMethod options, NetPeer excludePeer) { try { _peersLock.EnterReadLock(); for (NetPeer netPeer = _headPeer; netPeer != null; netPeer = netPeer.NextPeer) { if (netPeer != excludePeer) { netPeer.Send(data, channelNumber, options); } } } finally { _peersLock.ExitReadLock(); } } public bool Start() { return Start(0); } public bool Start(IPAddress addressIPv4, IPAddress addressIPv6, int port) { return Start(addressIPv4, addressIPv6, port, manualMode: false); } public bool Start(string addressIPv4, string addressIPv6, int port) { IPAddress addressIPv7 = NetUtils.ResolveAddress(addressIPv4); IPAddress addressIPv8 = NetUtils.ResolveAddress(addressIPv6); return Start(addressIPv7, addressIPv8, port); } public bool Start(int port) { return Start(IPAddress.Any, IPAddress.IPv6Any, port); } public bool StartInManualMode(IPAddress addressIPv4, IPAddress addressIPv6, int port) { return Start(addressIPv4, addressIPv6, port, manualMode: true); } public bool StartInManualMode(string addressIPv4, string addressIPv6, int port) { IPAddress addressIPv7 = NetUtils.ResolveAddress(addressIPv4); IPAddress addressIPv8 = NetUtils.ResolveAddress(addressIPv6); return StartInManualMode(addressIPv7, addressIPv8, port); } public bool StartInManualMode(int port) { return StartInManualMode(IPAddress.Any, IPAddress.IPv6Any, port); } public bool SendUnconnectedMessage(byte[] message, IPEndPoint remoteEndPoint) { return SendUnconnectedMessage(message, 0, message.Length, remoteEndPoint); } public bool SendUnconnectedMessage(NetDataWriter writer, string address, int port) { IPEndPoint remoteEndPoint = NetUtils.MakeEndPoint(address, port); return SendUnconnectedMessage(writer.Data, 0, writer.Length, remoteEndPoint); } public bool SendUnconnectedMessage(NetDataWriter writer, IPEndPoint remoteEndPoint) { return SendUnconnectedMessage(writer.Data, 0, writer.Length, remoteEndPoint); } public bool SendUnconnectedMessage(byte[] message, int start, int length, IPEndPoint remoteEndPoint) { NetPacket packet = PoolGetWithData(PacketProperty.UnconnectedMessage, message, start, length); return SendRawAndRecycle(packet, remoteEndPoint) > 0; } public void TriggerUpdate() { _updateTriggerEvent.Set(); } public void PollEvents() { if (_manualMode) { if (_udpSocketv4 != null) { ManualReceive(_udpSocketv4, _bufferEndPointv4); } if (_udpSocketv6 != null && _udpSocketv6 != _udpSocketv4) { ManualReceive(_udpSocketv6, _bufferEndPointv6); } } else if (!UnsyncedEvents) { NetEvent netEvent; lock (_eventLock) { netEvent = _pendingEventHead; _pendingEventHead = null; _pendingEventTail = null; } while (netEvent != null) { NetEvent next = netEvent.Next; ProcessEvent(netEvent); netEvent = next; } } } public NetPeer Connect(string address, int port, string key) { return Connect(address, port, NetDataWriter.FromString(key)); } public NetPeer Connect(string address, int port, NetDataWriter connectionData) { IPEndPoint target; try { target = NetUtils.MakeEndPoint(address, port); } catch { CreateEvent(NetEvent.EType.Disconnect, null, null, SocketError.Success, 0, DisconnectReason.UnknownHost, null, DeliveryMethod.Unreliable, 0); return null; } return Connect(target, connectionData); } public NetPeer Connect(IPEndPoint target, string key) { return Connect(target, NetDataWriter.FromString(key)); } public NetPeer Connect(IPEndPoint target, NetDataWriter connectionData) { if (!IsRunning) { throw new InvalidOperationException("Client is not running"); } lock (_requestsDict) { if (_requestsDict.ContainsKey(target)) { return null; } byte connectNum = 0; if (TryGetPeer(target, out var actualValue)) { ConnectionState connectionState = actualValue.ConnectionState; if (connectionState == ConnectionState.Outgoing || connectionState == ConnectionState.Connected) { return actualValue; } connectNum = (byte)((actualValue.ConnectionNum + 1) % 4); RemovePeer(actualValue); } actualValue = new NetPeer(this, target, GetNextPeerId(), connectNum, connectionData); AddPeer(actualValue); return actualValue; } } public void Stop() { Stop(sendDisconnectMessages: true); } public void Stop(bool sendDisconnectMessages) { if (IsRunning) { for (NetPeer netPeer = _headPeer; netPeer != null; netPeer = netPeer.NextPeer) { netPeer.Shutdown(null, 0, 0, !sendDisconnectMessages); } CloseSocket(); _updateTriggerEvent.Set(); if (!_manualMode) { _logicThread.Join(); _logicThread = null; } ClearPeerSet(); _peerIds = new ConcurrentQueue<int>(); _lastPeerId = 0; _connectedPeersCount = 0; _pendingEventHead = null; _pendingEventTail = null; } } public int GetPeersCount(ConnectionState peerState) { int num = 0; _peersLock.EnterReadLock(); for (NetPeer netPeer = _headPeer; netPeer != null; netPeer = netPeer.NextPeer) { if ((netPeer.ConnectionState & peerState) != 0) { num++; } } _peersLock.ExitReadLock(); return num; } public void GetPeersNonAlloc(List<NetPeer> peers, ConnectionState peerState) { peers.Clear(); _peersLock.EnterReadLock(); for (NetPeer netPeer = _headPeer; netPeer != null; netPeer = netPeer.NextPeer) { if ((netPeer.ConnectionState & peerState) != 0) { peers.Add(netPeer); } } _peersLock.ExitReadLock(); } public void DisconnectAll() { DisconnectAll(null, 0, 0); } public void DisconnectAll(byte[] data, int start, int count) { _peersLock.EnterReadLock(); for (NetPeer netPeer = _headPeer; netPeer != null; netPeer = netPeer.NextPeer) { DisconnectPeer(netPeer, DisconnectReason.DisconnectPeerCalled, SocketError.Success, force: false, data, start, count, null); } _peersLock.ExitReadLock(); } public void DisconnectPeerForce(NetPeer peer) { DisconnectPeerForce(peer, DisconnectReason.DisconnectPeerCalled, SocketError.Success, null); } public void DisconnectPeer(NetPeer peer) { DisconnectPeer(peer, null, 0, 0); } public void DisconnectPeer(NetPeer peer, byte[] data) { DisconnectPeer(peer, data, 0, data.Length); } public void DisconnectPeer(NetPeer peer, NetDataWriter writer) { DisconnectPeer(peer, writer.Data, 0, writer.Length); } public void DisconnectPeer(NetPeer peer, byte[] data, int start, int count) { DisconnectPeer(peer, DisconnectReason.DisconnectPeerCalled, SocketError.Success, force: false, data, start, count, null); } public void CreateNtpRequest(IPEndPoint endPoint) { _ntpRequests.TryAdd(endPoint, new NtpRequest(endPoint)); } public void CreateNtpRequest(string ntpServerAddress, int port) { IPEndPoint iPEndPoint = NetUtils.MakeEndPoint(ntpServerAddress, port); _ntpRequests.TryAdd(iPEndPoint, new NtpRequest(iPEndPoint)); } public void CreateNtpRequest(string ntpServerAddress) { IPEndPoint iPEndPoint = NetUtils.MakeEndPoint(ntpServerAddress, 123); _ntpRequests.TryAdd(iPEndPoint, new NtpRequest(iPEndPoint)); } public NetPeerEnumerator GetEnumerator() { return new NetPeerEnumerator(_headPeer); } IEnumerator<NetPeer> IEnumerable<NetPeer>.GetEnumerator() { return new NetPeerEnumerator(_headPeer); } IEnumerator IEnumerable.GetEnumerator() { return new NetPeerEnumerator(_headPeer); } private static int HashSetGetPrime(int min) { int[] primes = Primes; foreach (int num in primes) { if (num >= min) { return num; } } for (int j = min | 1; j < int.MaxValue; j += 2) { if (IsPrime(j) && (j - 1) % 101 != 0) { return j; } } return min; static bool IsPrime(int candidate) { if (((uint)candidate & (true ? 1u : 0u)) != 0) { int num2 = (int)Math.Sqrt(candidate); for (int k = 3; k <= num2; k += 2) { if (candidate % k == 0) { return false; } } return true; } return candidate == 2; } } private void ClearPeerSet() { _peersLock.EnterWriteLock(); _headPeer = null; if (_lastIndex > 0) { Array.Clear(_slots, 0, _lastIndex); Array.Clear(_buckets, 0, _buckets.Length); _lastIndex = 0; _count = 0; _freeList = -1; } _peersArray = new NetPeer[32]; _peersLock.ExitWriteLock(); } private bool ContainsPeer(NetPeer item) { if (_buckets != null) { int num = item.GetHashCode() & 0x7FFFFFFF; for (int num2 = _buckets[num % _buckets.Length] - 1; num2 >= 0; num2 = _slots[num2].Next) { if (_slots[num2].HashCode == num && _slots[num2].Value.Equals(item)) { return true; } } } return false; } public NetPeer GetPeerById(int id) { if (id < 0 || id >= _peersArray.Length) { return null; } return _peersArray[id]; } public bool TryGetPeerById(int id, out NetPeer peer) { peer = GetPeerById(id); return peer != null; } private void AddPeer(NetPeer peer) { _peersLock.EnterWriteLock(); if (_headPeer != null) { peer.NextPeer = _headPeer; _headPeer.PrevPeer = peer; } _headPeer = peer; AddPeerToSet(peer); if (peer.Id >= _peersArray.Length) { int num = _peersArray.Length * 2; while (peer.Id >= num) { num *= 2; } Array.Resize(ref _peersArray, num); } _peersArray[peer.Id] = peer; _peersLock.ExitWriteLock(); } private void RemovePeer(NetPeer peer) { _peersLock.EnterWriteLock(); RemovePeerInternal(peer); _peersLock.ExitWriteLock(); } private void RemovePeerInternal(NetPeer peer) { if (RemovePeerFromSet(peer)) { if (peer == _headPeer) { _headPeer = peer.NextPeer; } if (peer.PrevPeer != null) { peer.PrevPeer.NextPeer = peer.NextPeer; } if (peer.NextPeer != null) { peer.NextPeer.PrevPeer = peer.PrevPeer; } peer.PrevPeer = null; _peersArray[peer.Id] = null; _peerIds.Enqueue(peer.Id); } } private bool RemovePeerFromSet(NetPeer peer) { if (_buckets == null) { return false; } int num = peer.GetHashCode() & 0x7FFFFFFF; int num2 = num % _buckets.Length; int num3 = -1; for (int num4 = _buckets[num2] - 1; num4 >= 0; num4 = _slots[num4].Next) { if (_slots[num4].HashCode == num && _slots[num4].Value.Equals(peer)) { if (num3 < 0) { _buckets[num2] = _slots[num4].Next + 1; } else { _slots[num3].Next = _slots[num4].Next; } _slots[num4].HashCode = -1; _slots[num4].Value = null; _slots[num4].Next = _freeList; _count--; if (_count == 0) { _lastIndex = 0; _freeList = -1; } else { _freeList = num4; } return true; } num3 = num4; } return false; } private bool TryGetPeer(IPEndPoint endPoint, out NetPeer actualValue) { if (_buckets != null) { int num = endPoint.GetHashCode() & 0x7FFFFFFF; _peersLock.EnterReadLock(); for (int num2 = _buckets[num % _buckets.Length] - 1; num2 >= 0; num2 = _slots[num2].Next) { if (_slots[num2].HashCode == num && _slots[num2].Value.Equals(endPoint)) { actualValue = _slots[num2].Value; _peersLock.ExitReadLock(); return true; } } _peersLock.ExitReadLock(); } actualValue = null; return false; } private bool TryGetPeer(SocketAddress saddr, out NetPeer actualValue) { if (_buckets != null) { int num = saddr.GetHashCode() & 0x7FFFFFFF; _peersLock.EnterReadLock(); for (int num2 = _buckets[num % _buckets.Length] - 1; num2 >= 0; num2 = _slots[num2].Next) { if (_slots[num2].HashCode == num && _slots[num2].Value.Serialize().Equals(saddr)) { actualValue = _slots[num2].Value; _peersLock.ExitReadLock(); return true; } } _peersLock.ExitReadLock(); } actualValue = null; return false; } private bool AddPeerToSet(NetPeer value) { if (_buckets == null) { int num = HashSetGetPrime(0); _buckets = new int[num]; _slots = new Slot[num]; } int num2 = value.GetHashCode() & 0x7FFFFFFF; int num3 = num2 % _buckets.Length; for (int num4 = _buckets[num2 % _buckets.Length] - 1; num4 >= 0; num4 = _slots[num4].Next) { if (_slots[num4].HashCode == num2 && _slots[num4].Value.Equals(value)) { return false; } } int num5; if (_freeList >= 0) { num5 = _freeList; _freeList = _slots[num5].Next; } else { if (_lastIndex == _slots.Length) { int num6 = 2 * _count; num6 = (((uint)num6 > 2147483587u && 2147483587 > _count) ? 2147483587 : HashSetGetPrime(num6)); Slot[] array = new Slot[num6]; Array.Copy(_slots, 0, array, 0, _lastIndex); _buckets = new int[num6]; for (int i = 0; i < _lastIndex; i++) { int num7 = array[i].HashCode % num6; array[i].Next = _buckets[num7] - 1; _buckets[num7] = i + 1; } _slots = array; num3 = num2 % _buckets.Length; } num5 = _lastIndex; _lastIndex++; } _slots[num5].HashCode = num2; _slots[num5].Value = value; _slots[num5].Next = _buckets[num3] - 1; _buckets[num3] = num5 + 1; _count++; return true; } private NetPacket PoolGetWithData(PacketProperty property, byte[] data, int start, int length) { int headerSize = NetPacket.GetHeaderSize(property); NetPacket netPacket = PoolGetPacket(length + headerSize); netPacket.Property = property; Buffer.BlockCopy(data, start, netPacket.RawData, headerSize, length); return netPacket; } private NetPacket PoolGetWithProperty(PacketProperty property, int size) { NetPacket netPacket = PoolGetPacket(size + NetPacket.GetHeaderSize(property)); netPacket.Property = property; return netPacket; } private NetPacket PoolGetWithProperty(PacketProperty property) { NetPacket netPacket = PoolGetPacket(NetPacket.GetHeaderSize(property)); netPacket.Property = property; return netPacket; } internal NetPacket PoolGetPacket(int size) { if (size > NetConstants.MaxPacketSize) { return new NetPacket(size); } NetPacket poolHead; lock (_poolLock) { poolHead = _poolHead; if (poolHead == null) { return new NetPacket(size); } _poolHead = _poolHead.Next; _poolCount--; } poolHead.Size = size; if (poolHead.RawData.Length < size) { poolHead.RawData = new byte[size]; } return poolHead; } internal void PoolRecycle(NetPacket packet) { if (packet.RawData.Length > NetConstants.MaxPacketSize || _poolCount >= PacketPoolSize) { return; } packet.RawData[0] = 0; lock (_poolLock) { packet.Next = _poolHead; _poolHead = packet; _poolCount++; } } static NetManager() { Primes = new int[72] { 3, 7, 11, 17, 23, 29, 37, 47, 59, 71, 89, 107, 131, 163, 197, 239, 293, 353, 431, 521, 631, 761, 919, 1103, 1327, 1597, 1931, 2333, 2801, 3371, 4049, 4861, 5839, 7013, 8419, 10103, 12143, 14591, 17519, 21023, 25229, 30293, 36353, 43627, 52361, 62851, 75431, 90523, 108631, 130363, 156437, 187751, 225307, 270371, 324449, 389357, 467237, 560689, 672827, 807403, 968897, 1162687, 1395263, 1674319, 2009191, 2411033, 2893249, 3471899, 4166287, 4999559, 5999471, 7199369 }; MulticastAddressV6 = IPAddress.Parse("ff02::1"); IPv6Support = Socket.OSSupportsIPv6; } private bool ProcessError(SocketException ex) { switch (ex.SocketErrorCode) { case SocketError.NotConnected: NotConnected = true; return true; case SocketError.OperationAborted: case SocketError.Interrupted: case SocketError.NotSocket: return true; default: NetDebug.WriteError($"[R]Error code: {(int)ex.SocketErrorCode} - {ex}"); CreateEvent(NetEvent.EType.Error, null, null, ex.SocketErrorCode, 0, DisconnectReason.ConnectionFailed, null, DeliveryMethod.Unreliable, 0); break; case SocketError.WouldBlock: case SocketError.MessageSize: case SocketError.NetworkReset: case SocketError.ConnectionReset: case SocketError.TimedOut: break; } return false; } private void ManualReceive(Socket socket, EndPoint bufferEndPoint) { try { int num = 0; while (socket.Available > 0) { ReceiveFrom(socket, ref bufferEndPoint); num++; if (num == MaxPacketsReceivePerUpdate) { break; } } } catch (SocketException ex) { ProcessError(ex); } catch (ObjectDisposedException) { } catch (Exception ex3) { NetDebug.WriteError("[NM] SocketReceiveThread error: " + ex3); } } private void NativeReceiveLogic() { IntPtr handle = _udpSocketv4.Handle; IntPtr s2 = _udpSocketv6?.Handle ?? IntPtr.Zero; byte[] address2 = new byte[16]; byte[] address3 = new byte[28]; IPEndPoint tempEndPoint = new IPEndPoint(IPAddress.Any, 0); List<Socket> list = new List<Socket>(2); Socket udpSocketv = _udpSocketv4; Socket udpSocketv2 = _udpSocketv6; NetPacket packet = PoolGetPacket(NetConstants.MaxPacketSize); while (IsRunning) { if (udpSocketv2 == null) { if (!NativeReceiveFrom(handle, address2)) { break; } continue; } bool flag = false; if (udpSocketv.Available != 0 || list.Contains(udpSocketv)) { if (!NativeReceiveFrom(handle, address2)) { break; } flag = true; } if (udpSocketv2.Available != 0 || list.Contains(udpSocketv2)) { if (!NativeReceiveFrom(s2, address3)) { break; } flag = true; } list.Clear(); if (flag) { continue; } list.Add(udpSocketv); list.Add(udpSocketv2); try { Socket.Select(list, null, null, 500000); } catch (SocketException ex) { if (ProcessError(ex)) { break; } } catch (ObjectDisposedException) { break; } catch (ThreadAbortException) { break; } catch (Exception ex4) { NetDebug.WriteError("[NM] SocketReceiveThread error: " + ex4); } } bool NativeReceiveFrom(IntPtr s, byte[] address) { int socketAddressSize = address.Length; packet.Size = NativeSocket.RecvFrom(s, packet.RawData, NetConstants.MaxPacketSize, address, ref socketAddressSize); if (packet.Size == 0) { return true; } if (packet.Size == -1) { return !ProcessError(new SocketException((int)NativeSocket.GetSocketError())); } short num = (short)((address[1] << 8) | address[0]); tempEndPoint.Port = (ushort)((address[2] << 8) | address[3]); if ((NativeSocket.UnixMode && num == 10) || (!NativeSocket.UnixMode && num == 23)) { uint num2 = (uint)((address[27] << 24) + (address[26] << 16) + (address[25] << 8) + address[24]); tempEndPoint.Address = new IPAddress(new ReadOnlySpan<byte>(address, 8, 16), num2); } else { long newAddress = (uint)((address[4] & 0xFF) | ((address[5] << 8) & 0xFF00) | ((address[6] << 16) & 0xFF0000) | (address[7] << 24)); tempEndPoint.Address = new IPAddress(newAddress); } if (TryGetPeer(tempEndPoint, out var actualValue)) { OnMessageReceived(packet, actualValue); } else { OnMessageReceived(packet, tempEndPoint); tempEndPoint = new IPEndPoint(IPAddress.Any, 0); } packet = PoolGetPacket(NetConstants.MaxPacketSize); return true; } } private void ReceiveFrom(Socket s, ref EndPoint bufferEndPoint) { NetPacket netPacket = PoolGetPacket(NetConstants.MaxPacketSize); netPacket.Size = s.ReceiveFrom(netPacket.RawData, 0, NetConstants.MaxPacketSize, SocketFlags.None, ref bufferEndPoint); OnMessageReceived(netPacket, (IPEndPoint)bufferEndPoint); } private void ReceiveLogic() { EndPoint bufferEndPoint = new IPEndPoint(IPAddress.Any, 0); EndPoint bufferEndPoint2 = new IPEndPoint(IPAddress.IPv6Any, 0); List<Socket> list = new List<Socket>(2); Socket udpSocketv = _udpSocketv4; Socket udpSocketv2 = _udpSocketv6; while (IsRunning) { try { if (udpSocketv2 == null) { if (udpSocketv.Available != 0 || udpSocketv.Poll(500000, SelectMode.SelectRead)) { ReceiveFrom(udpSocketv, ref bufferEndPoint); } continue; } bool flag = false; if (udpSocketv.Available != 0 || list.Contains(udpSocketv)) { ReceiveFrom(udpSocketv, ref bufferEndPoint); flag = true; } if (udpSocketv2.Available != 0 || list.Contains(udpSocketv2)) { ReceiveFrom(udpSocketv2, ref bufferEndPoint2); flag = true; } list.Clear(); if (!flag) { list.Add(udpSocketv); list.Add(udpSocketv2); Socket.Select(list, null, null, 500000); } } catch (SocketException ex) { if (ProcessError(ex)) { break; } } catch (ObjectDisposedException) { break; } catch (ThreadAbortException) { break; } catch (Exception ex4) { NetDebug.WriteError("[NM] SocketReceiveThread error: " + ex4); } } } public bool Start(IPAddress addressIPv4, IPAddress addressIPv6, int port, bool manualMode) { if (IsRunning && !NotConnected) { return false; } NotConnected = false; _manualMode = manualMode; UseNativeSockets = UseNativeSockets && NativeSocket.IsSupported; _udpSocketv4 = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); if (!BindSocket(_udpSocketv4, new IPEndPoint(addressIPv4, port))) { return false; } LocalPort = ((IPEndPoint)_udpSocketv4.LocalEndPoint).Port; IsRunning = true; if (_manualMode) { _bufferEndPointv4 = new IPEndPoint(IPAddress.Any, 0); } if (IPv6Support && IPv6Enabled) { _udpSocketv6 = new Socket(AddressFamily.InterNetworkV6, SocketType.Dgram, ProtocolType.Udp); if (BindSocket(_udpSocketv6, new IPEndPoint(addressIPv6, LocalPort))) { if (_manualMode) { _bufferEndPointv6 = new IPEndPoint(IPAddress.IPv6Any, 0); } } else { _udpSocketv6 = null; } } if (!manualMode) { ThreadStart start = ReceiveLogic; if (UseNativeSockets) { start = NativeReceiveLogic; } _receiveThread = new Thread(start) { Name = $"ReceiveThread({LocalPort})", IsBackground = true }; _receiveThread.Start(); if (_logicThread == null) { _logicThread = new Thread(UpdateLogic) { Name = "LogicThread", IsBackground = true }; _logicThread.Start(); } } return true; } private bool BindSocket(Socket socket, IPEndPoint ep) { socket.ReceiveTimeout = 500; socket.SendTimeout = 500; socket.ReceiveBufferSize = 1048576; socket.SendBufferSize = 1048576; socket.Blocking = true; if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) { try { socket.IOControl(-1744830452, new byte[1], null); } catch { } } try { socket.ExclusiveAddressUse = !ReuseAddress; socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, ReuseAddress); socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.DontRoute, DontRoute); } catch { } if (ep.AddressFamily == AddressFamily.InterNetwork) { Ttl = 255; try { socket.EnableBroadcast = true; } catch (SocketException ex) { NetDebug.WriteError($"[B]Broadcast error: {ex.SocketErrorCode}"); } if (!RuntimeInformation.IsOSPlatform(OSPlatform.OSX)) { try { socket.DontFragment = true; } catch (SocketException ex2) { NetDebug.WriteError($"[B]DontFragment error: {ex2.SocketErrorCode}"); } } } try { socket.Bind(ep); if (ep.AddressFamily == AddressFamily.InterNetworkV6) { try { socket.SetSocketOption(SocketOptionLevel.IPv6, SocketOptionName.AddMembership, new IPv6MulticastOption(MulticastAddressV6)); } catch (Exception) { } } } catch (SocketException ex4) { switch (ex4.SocketErrorCode) { case SocketError.AddressAlreadyInUse: if (socket.AddressFamily == AddressFamily.InterNetworkV6) { try { socket.DualMode = false; socket.Bind(ep); } catch (SocketException ex5) { NetDebug.WriteError($"[B]Bind exception: {ex5}, errorCode: {ex5.SocketErrorCode}"); return false; } return true; } break; case SocketError.AddressFamilyNotSupported: return true; } NetDebug.WriteError($"[B]Bind exception: {ex4}, errorCode: {ex4.SocketErrorCode}"); return false; } return true; } internal int SendRawAndRecycle(NetPacket packet, IPEndPoint remoteEndPoint) { int result = SendRaw(packet.RawData, 0, packet.Size, remoteEndPoint); PoolRecycle(packet); return result; } internal int SendRaw(NetPacket packet, IPEndPoint remoteEndPoint) { return SendRaw(packet.RawData, 0, packet.Size, remoteEndPoint); } internal unsafe int SendRaw(byte[] message, int start, int length, IPEndPoint remoteEndPoint) { if (!IsRunning) { return 0; } NetPacket netPacket = null; if (_extraPacketLayer != null) { netPacket = PoolGetPacket(length + _extraPacketLayer.ExtraPacketSizeForLayer); Buffer.BlockCopy(message, start, netPacket.RawData, 0, length); start = 0; _extraPacketLayer.ProcessOutBoundPacket(ref remoteEndPoint, ref netPacket.RawData, ref start, ref length); message = netPacket.RawData; } Socket socket = _udpSocketv4; if (remoteEndPoint.AddressFamily == AddressFamily.InterNetworkV6 && IPv6Support) { socket = _udpSocketv6; if (socket == null) { return 0; } } int num; try { if (UseNativeSockets && remoteEndPoint is NetPeer netPeer) { fixed (byte* pinnedBuffer = &message[start]) { num = NativeSocket.SendTo(socket.Handle, pinnedBuffer, length, netPeer.NativeAddress, netPeer.NativeAddress.Length); } if (num == -1) { throw NativeSocket.GetSocketException(); } } else { num = socket.SendTo(message, start, length, SocketFlags.None, remoteEndPoint); } } catch (SocketException ex) { switch (ex.SocketErrorCode) { case SocketError.Interrupted: case SocketError.NoBufferSpaceAvailable: return 0; case SocketError.MessageSize: return 0; case SocketError.NetworkUnreachable: case SocketError.HostUnreachable: if (DisconnectOnUnreachable && remoteEndPoint is NetPeer peer) { DisconnectPeerForce(peer, (ex.SocketErrorCode == SocketError.HostUnreachable) ? DisconnectReason.HostUnreachable : DisconnectReason.NetworkUnreachable, ex.SocketErrorCode, null); } CreateEvent(NetEvent.EType.Error, null, remoteEndPoint, ex.SocketErrorCode, 0, DisconnectReason.ConnectionFailed, null, DeliveryMethod.Unreliable, 0); return -1; case SocketError.Shutdown: CreateEvent(NetEvent.EType.Error, null, remoteEndPoint, ex.SocketErrorCode, 0, DisconnectReason.ConnectionFailed, null, DeliveryMethod.Unreliable, 0); return -1; default: NetDebug.WriteError($"[S] {ex}"); return -1; } } catch (Exception arg) { NetDebug.WriteError($"[S] {arg}"); return 0; } finally { if (netPacket != null) { PoolRecycle(netPacket); } } if (num <= 0) { return 0; } if (EnableStatistics) { Statistics.IncrementPacketsSent(); Statistics.AddBytesSent(length); } return num; } public bool SendBroadcast(NetDataWriter writer, int port) { return SendBroadcast(writer.Data, 0, writer.Length, port); } public bool SendBroadcast(byte[] data, int port) { return SendBroadcast(data, 0, data.Length, port); } public bool SendBroadcast(byte[] data, int start, int length, int port) { if (!IsRunning) { return false; } NetPacket netPacket; if (_extraPacketLayer != null) { int headerSize = NetPacket.GetHeaderSize(PacketProperty.Broadcast); netPacket = PoolGetPacket(headerSize + length + _extraPacketLayer.ExtraPacketSizeForLayer); netPacket.Property = PacketProperty.Broadcast; Buffer.BlockCopy(data, start, netPacket.RawData, headerSize, length); int offset = 0; int length2 = length + headerSize; IPEndPoint endPoint = null; _extraPacketLayer.ProcessOutBoundPacket(ref endPoint, ref netPacket.RawData, ref offset, ref length2); } else { netPacket = PoolGetWithData(PacketProperty.Broadcast, data, start, length); } bool flag = false; bool flag2 = false; try { flag = _udpSocketv4.SendTo(netPacket.RawData, 0, netPacket.Size, SocketFlags.None, new IPEndPoint(IPAddress.Broadcast, port)) > 0; if (_udpSocketv6 != null) { flag2 = _udpSocketv6.SendTo(netPacket.RawData, 0, netPacket.Size, SocketFlags.None, new IPEndPoint(MulticastAddressV6, port)) > 0; } } catch (SocketException ex) { if (ex.SocketErrorCode == SocketError.HostUnreachable) { return flag; } NetDebug.WriteError($"[S][MCAST] {ex}"); return flag; } catch (Exception arg) { NetDebug.WriteError($"[S][MCAST] {arg}"); return flag; } finally { PoolRecycle(netPacket); } return flag || flag2; } private void CloseSocket() { IsRunning = false; _udpSocketv4?.Close(); _udpSocketv6?.Close(); _udpSocketv4 = null; _udpSocketv6 = null; if (_receiveThread != null && _receiveThread != Thread.CurrentThread) { _receiveThread.Join(); } _receiveThread = null; } } internal enum PacketProperty : byte { Unreliable, Channeled, Ack, Ping, Pong, ConnectRequest, ConnectAccept, Disconnect, UnconnectedMessage, MtuCheck, MtuOk, Broadcast, Merged, ShutdownOk, PeerNotFound, InvalidProtocol, NatMessage, Empty } internal sealed class NetPacket { private static readonly int PropertiesCount; private static readonly int[] HeaderSizes; public byte[] RawData; public int Size; public object UserData; public NetPacket Next; public PacketProperty Property { get { return (PacketProperty)(RawData[0] & 0x1Fu); } set { RawData[0] = (byte)((RawData[0] & 0xE0u) | (uint)value); } } public byte ConnectionNumber { get { return (byte)((RawData[0] & 0x60) >> 5); } set { RawData[0] = (byte)((RawData[0] & 0x9Fu) | (uint)(value << 5)); } } public ushort Sequence { get { return BitConverter.ToUInt16(RawData, 1); } set { FastBitConverter.GetBytes(RawData, 1, value); } } public bool IsFragmented => (RawData[0] & 0x80) != 0; public byte ChannelId { get { return RawData[3]; } set { RawData[3] = value; } } public ushort FragmentId { get { return BitConverter.ToUInt16(RawData, 4); } set { FastBitConverter.GetBytes(RawData, 4, value); } } public ushort FragmentPart { get { return BitConverter.ToUInt16(RawData, 6); } set { FastBitConverter.GetBytes(RawData, 6, value); } } public ushort FragmentsTotal { get { return BitConverter.ToUInt16(RawData, 8); } set { FastBitConverter.GetBytes(RawData, 8, value); } } static NetPacket() { PropertiesCount = Enum.GetValues(typeof(PacketProperty)).Length; HeaderSizes = NetUtils.AllocatePinnedUninitializedArray<int>(PropertiesCount); for (int i = 0; i < HeaderSizes.Length; i++) { switch ((PacketProperty)(byte)i) { case PacketProperty.Channeled: case PacketProperty.Ack: HeaderSizes[i] = 4; break; case PacketProperty.Ping: HeaderSizes[i] = 3; break; case PacketProperty.ConnectRequest: HeaderSizes[i] = 18; break; case PacketProperty.ConnectAccept: HeaderSizes[i] = 15; break; case PacketProperty.Disconnect: HeaderSizes[i] = 9; break; case PacketProperty.Pong: HeaderSizes[i] = 11; break; default: HeaderSizes[i] = 1; break; } } } public void MarkFragmented() { RawData[0] |= 128; } public NetPacket(int size) { RawData = new byte[size]; Size = size; } public NetPacket(PacketProperty property, int size) { size += GetHeaderSize(property); RawData = new byte[size]; Property = property; Size = size; } public static int GetHeaderSize(PacketProperty property) { return HeaderSizes[(uint)property]; } public int GetHeaderSize() { return HeaderSizes[RawData[0] & 0x1F]; } public bool Verify() { byte b = (byte)(RawData[0] & 0x1Fu); if (b >= PropertiesCount) { return false; } int num = HeaderSizes[b]; bool flag = (RawData[0] & 0x80) != 0; if (Size >= num) { if (flag) { return Size >= num + 6; } return true; } return false; } public static implicit operator Span<byte>(NetPacket p) { return new Span<byte>(p.RawData, 0, p.Size); } } [Flags] public enum ConnectionState : byte { Outgoing = 2, Connected = 4, ShutdownRequested = 8, Disconnected = 0x10, EndPointChange = 0x20, Any = 0x2E } internal enum ConnectRequestResult { None, P2PLose, Reconnection, NewConnection } internal enum DisconnectResult { None, Reject, Disconnect } internal enum ShutdownResult { None, Success, WasConnected } public class NetPeer : IPEndPoint { private class IncomingFragments { public NetPacket[] Fragments; public int ReceivedCount; public int TotalSize; public byte ChannelId; } private int _rtt; private int _avgRtt; private int _rttCount; private double _resendDelay = 27.0; private int _pingSendTimer; private int _rttResetTimer; private readonly Stopwatch _pingTimer = new Stopwatch(); private int _timeSinceLastPacket; private long _remoteDelta; private readonly object _shutdownLock = new object(); internal volatile NetPeer NextPeer; internal NetPeer PrevPeer; private readonly Queue<NetPacket> _unreliableChannel; private readonly ConcurrentQueue<BaseChannel> _channelSendQueue; private readonly BaseChannel[] _channels; private int _mtu; private int _mtuIdx; private bool _finishMtu; private int _mtuCheckTimer; private int _mtuCheckAttempts; private const int MtuCheckDelay = 1000; private const int MaxMtuCheckAttempts = 4; private readonly object _mtuMutex = new object(); private int _fragmentId; private readonly Dictionary<ushort, IncomingFragments> _holdedFragments; private readonly Dictionary<ushort, ushort> _deliveredFragments; private readonly NetPacket _mergeData; private int _mergePos; private int _mergeCount; private int _connectAttempts; private int _connectTimer; private long _connectTime; private byte _connectNum; private ConnectionState _connectionState; private NetPacket _shutdownPacket; private const int ShutdownDelay = 300; private int _shutdownTimer; private readonly NetPacket _pingPacket; private readonly NetPacket _pongPacket; private readonly NetPacket _connectRequestPacket; private readonly NetPacket _connectAcceptPacket; public readonly NetManager NetManager; public readonly int Id; public object Tag; public readonly NetStatistics Statistics; private SocketAddress _cachedSocketAddr; private int _cachedHashCode; internal byte[] NativeAddress; internal byte ConnectionNum { get { return _connectNum; } private set { _connectNum = value; _mergeData.ConnectionNumber = value; _pingPacket.ConnectionNumber = value; _pongPacket.ConnectionNumber = value; } } public ConnectionState ConnectionState => _connectionState; internal long ConnectTime => _connectTime; public int RemoteId { get; private set; } public int Ping => _avgRtt / 2; public int RoundTripTime => _avgRtt; public int Mtu => _mtu; public long RemoteTimeDelta => _remoteDelta; public DateTime RemoteUtcTime => new DateTime(DateTime.UtcNow.Ticks + _remoteDelta); public int TimeSinceLastPacket => _timeSinceLastPacket; internal double ResendDelay => _resendDelay; public override SocketAddress Serialize() { return _cachedSocketAddr; } public override int GetHashCode() { return _cachedHashCode; } internal NetPeer(NetManager netManager, IPEndPoint remoteEndPoint, int id) : base(remoteEndPoint.Address, remoteEndPoint.Port) { Id = id; Statistics = new NetStatistics(); NetManager = netManager; _cachedSocketAddr = base.Serialize(); if (NetManager.UseNativeSockets) { NativeAddress = new byte[_cachedSocketAddr.Size]; for (int i = 0; i < _cachedSocketAddr.Size; i++) { NativeAddress[i] = _cachedSocketAddr[i]; } } _cachedHashCode = base.GetHashCode(); ResetMtu(); _connectionState = ConnectionState.Connected; _mergeData = new NetPacket(PacketProperty.Merged, NetConstants.MaxPacketSize); _pongPacket = new NetPacket(PacketProperty.Pong, 0); _pingPacket = new NetPacket(PacketProperty.Ping, 0) { Sequence = 1 }; _unreliableChannel = new Queue<NetPacket>(); _holdedFragments = new Dictionary<ushort, IncomingFragments>(); _deliveredFragments = new Dictionary<ushort, ushort>(); _channels = new BaseChannel[netManager.ChannelsCount * 4]; _channelSendQueue = new ConcurrentQueue<BaseChannel>(); } internal void InitiateEndPointChange() { ResetMtu(); _connectionState = ConnectionState.EndPointChange; } internal void FinishEndPointChange(IPEndPoint newEndPoint) { if (_connectionState != ConnectionState.EndPointChange) { return; } _connectionState = ConnectionState.Connected; base.Address = newEndPoint.Address; base.Port = newEndPoint.Port; if (NetManager.UseNativeSockets) { NativeAddress = new byte[_cachedSocketAddr.Size]; for (int i = 0; i < _cachedSocketAddr.Size; i++) { NativeAddress[i] = _cachedSocketAddr[i]; } } _cachedSocketAddr = base.Serialize(); _cachedHashCode = base.GetHashCode(); } internal void ResetMtu() { _finishMtu = false; if (NetManager.MtuOverride > 0) { OverrideMtu(NetManager.MtuOverride); } else if (NetManager.UseSafeMtu) { SetMtu(0); } else { SetMtu(1); } } private void SetMtu(int mtuIdx) { _mtuIdx = mtuIdx; _mtu = NetConstants.PossibleMtu[mtuIdx] - NetManager.ExtraPacketSizeForLayer; } private void OverrideMtu(int mtuValue) { _mtu = mtuValue; _finishMtu = true; } public int GetPacketsCountInReliableQueue(byte channelNumber, bool ordered) { int num = channelNumber * 4 + (ordered ? 2 : 0); BaseChannel baseChannel = _channels[num]; if (baseChannel == null) { return 0; } return ((ReliableChannel)baseChannel).PacketsInQueue; } public PooledPacket CreatePacketFromPool(DeliveryMethod deliveryMethod, byte channelNumber) { int mtu = _mtu; NetPacket netPacket = NetManager.PoolGetPacket(mtu); if (deliveryMethod == DeliveryMethod.Unreliable) { netPacket.Property = PacketProperty.Unreliable; return new PooledPacket(netPacket, mtu, 0); } netPacket.Property = PacketProperty.Channeled; return new PooledPacket(netPacket, mtu, (byte)((uint)(channelNumber * 4) + (uint)deliveryMethod)); } public void SendPooledPacket(PooledPacket packet, int userDataSize) { if (_connectionState != ConnectionState.Connected) { return; } packet._packet.Size = packet.UserDataOffset + userDataSize; if (packet._packet.Property == PacketProperty.Channeled) { CreateChannel(packet._channelNumber).AddToQueue(packet._packet); return; } lock (_unreliableChannel) { _unreliableChannel.Enqueue(packet._packet); } } private BaseChannel CreateChannel(byte idx) { BaseChannel baseChannel = _channels[idx]; if (baseChannel != null) { return baseChannel; } switch ((DeliveryMethod)(byte)(idx % 4)) { case DeliveryMethod.ReliableUnordered: baseChannel = new ReliableChannel(this, ordered: false, idx); break; case DeliveryMethod.Sequenced: baseChannel = new SequencedChannel(this, reliable: false, idx); break; case DeliveryMethod.ReliableOrdered: baseChannel = new ReliableChannel(this, ordered: true, idx); break; case DeliveryMethod.ReliableSequenced: baseChannel = new SequencedChannel(this, reliable: true, idx); break; } BaseChannel baseChannel2 = Interlocked.CompareExchange(ref _channels[idx], baseChannel, null); if (baseChannel2 != null) { return baseChannel2; } return baseChannel; } internal NetPeer(NetManager netManager, IPEndPoint remoteEndPoint, int id, byte connectNum, NetDataWriter connectData) : this(netManager, remoteEndPoint, id) { _connectTime = DateTime.UtcNow.Ticks; _connectionState = ConnectionState.Outgoing; ConnectionNum = connectNum; _connectRequestPacket = NetConnectRequestPacket.Make(connectData, remoteEndPoint.Serialize(), _connectTime, id); _connectRequestPacket.ConnectionNumber = connectNum; NetManager.SendRaw(_connectRequestPacket, this); } internal NetPeer(NetManager netManager, ConnectionRequest request, int id) : this(netManager, request.RemoteEndPoint, id) { _connectTime = request.InternalPacket.ConnectionTime; ConnectionNum = request.InternalPacket.ConnectionNumber; RemoteId = request.InternalPacket.PeerId; _connectAcceptPacket = NetConnectAcceptPacket.Make(_connectTime, ConnectionNum, id); _connectionState = ConnectionState.Connected; NetManager.SendRaw(_connectAcceptPacket, this); } internal void Reject(NetConnectRequestPacket requestData, byte[] data, int start, int length) { _connectTime = requestData.ConnectionTime; _connectNum = requestData.ConnectionNumber; Shutdown(data, start, length, force: false); } internal bool ProcessConnectAccept(NetConnectAcceptPacket packet) { if (_connectionState != ConnectionState.Outgoing) { return false; } if (packet.ConnectionTime != _connectTime) { return false; } ConnectionNum = packet.ConnectionNumber; RemoteId = packet.PeerId; Interlocked.Exchange(ref _timeSinceLastPacket, 0); _connectionState = ConnectionState.Connected; return true; } public int GetMaxSinglePacketSize(DeliveryMethod options) { return _mtu - NetPacket.GetHeaderSize((options != DeliveryMethod.Unreliable) ? PacketProperty.Channeled : PacketProperty.Unreliable); } public void SendWithDeliveryEvent(byte[] data, byte channelNumber, DeliveryMethod deliveryMethod, object userData) { if (deliveryMethod != DeliveryMethod.ReliableOrdered && deliveryMethod != 0) { throw
Multiplayer.dll
Decompiled 4 days agousing System; using System.Collections; using System.Collections.Generic; using System.Diagnostics; using System.Reflection; using System.Runtime.CompilerServices; using System.Runtime.Versioning; using System.Threading.Tasks; using BepInEx; using BepInEx.Bootstrap; using BepInEx.Configuration; using BepInEx.Logging; using HarmonyLib; using I2.Loc; using InControl; using InputExtension; using LiteNetLib; using LiteNetLib.Layers; using LiteNetLib.Utils; using Microsoft.CodeAnalysis; using NineSolsAPI; using NineSolsAPI.Utils; using TMPro; using UnityEngine; using UnityEngine.Events; using UnityEngine.SceneManagement; using UnityEngine.UI; [assembly: CompilationRelaxations(8)] [assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)] [assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)] [assembly: TargetFramework(".NETStandard,Version=v2.1", FrameworkDisplayName = ".NET Standard 2.1")] [assembly: AssemblyCompany("Multiplayer")] [assembly: AssemblyConfiguration("Release")] [assembly: AssemblyDescription("Multiplayer")] [assembly: AssemblyFileVersion("1.0.0.0")] [assembly: AssemblyInformationalVersion("1.0.0")] [assembly: AssemblyProduct("Multiplayer")] [assembly: AssemblyTitle("Multiplayer")] [assembly: AssemblyVersion("1.0.0.0")] [module: RefSafetyRules(11)] namespace Microsoft.CodeAnalysis { [CompilerGenerated] [Microsoft.CodeAnalysis.Embedded] internal sealed class EmbeddedAttribute : Attribute { } } namespace System.Runtime.CompilerServices { [CompilerGenerated] [Microsoft.CodeAnalysis.Embedded] [AttributeUsage(AttributeTargets.Class | AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Event | AttributeTargets.Parameter | AttributeTargets.ReturnValue | AttributeTargets.GenericParameter, AllowMultiple = false, Inherited = false)] internal sealed class NullableAttribute : Attribute { public readonly byte[] NullableFlags; public NullableAttribute(byte P_0) { NullableFlags = new byte[1] { P_0 }; } public NullableAttribute(byte[] P_0) { NullableFlags = P_0; } } [CompilerGenerated] [Microsoft.CodeAnalysis.Embedded] [AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Method | AttributeTargets.Interface | AttributeTargets.Delegate, AllowMultiple = false, Inherited = false)] internal sealed class NullableContextAttribute : Attribute { public readonly byte Flag; public NullableContextAttribute(byte P_0) { Flag = P_0; } } [CompilerGenerated] [Microsoft.CodeAnalysis.Embedded] [AttributeUsage(AttributeTargets.Module, AllowMultiple = false, Inherited = false)] internal sealed class RefSafetyRulesAttribute : Attribute { public readonly int Version; public RefSafetyRulesAttribute(int P_0) { Version = P_0; } } } public class EnemyData { public GameObject EnemyObject { get; set; } public string guid { get; set; } public EnemyData(GameObject obj, string guid) { EnemyObject = obj; this.guid = guid; } } public class PlayerData { public GameObject nameObject; public GameObject PlayerObject { get; set; } public Vector3 Position { get; set; } public string AnimationState { get; set; } public bool isFacingRight { get; set; } public int id { get; set; } public string name { get; set; } public PlayerData(GameObject obj, Vector3 pos, int id, GameObject nameObject) { //IL_000e: Unknown result type (might be due to invalid IL or missing references) PlayerObject = obj; Position = pos; this.id = id; this.nameObject = nameObject; } public PlayerData(int id, string name) { this.id = id; this.name = name; } public PlayerData() { } } namespace NineSolsAPI.Menu { internal class TitlescreenModifications { private UIControlGroup? group; private UIControlButton? button; private const bool Enable = true; public void Load() { //IL_0001: Unknown result type (might be due to invalid IL or missing references) MaybeExtendMainMenu(SceneManager.GetActiveScene()); } public void Unload() { if ((Object)(object)button != (Object)null) { Object.Destroy((Object)(object)((Component)button).gameObject); } if ((Object)(object)group != (Object)null) { Object.Destroy((Object)(object)((Component)group).gameObject); } } public void MaybeExtendMainMenu(Scene scene) { if (!(((Scene)(ref scene)).name != "TitleScreenMenu") && !Object.op_Implicit((Object)(object)button)) { group = CreateUiControlGroup(); button = CreateOptionsButton(); button.clickToShowGroup = group; } } private UIControlButton CreateOptionsButton() { GameObject val = GameObject.Find("MainMenuButton_Option"); GameObject obj = ObjectUtils.InstantiateAutoReference(val, val.transform.parent, false); Object.Destroy((Object)(object)obj.GetComponentInChildren<Localize>()); obj.GetComponentInChildren<TMP_Text>().text = "MultPlayer"; obj.gameObject.transform.SetSiblingIndex(val.transform.GetSiblingIndex() + 1); return obj.GetComponentInChildren<UIControlButton>(); } private UIControlGroup CreateUiControlGroup() { //IL_0015: Unknown result type (might be due to invalid IL or missing references) //IL_001a: Unknown result type (might be due to invalid IL or missing references) //IL_002c: Unknown result type (might be due to invalid IL or missing references) //IL_0033: Unknown result type (might be due to invalid IL or missing references) //IL_003a: Unknown result type (might be due to invalid IL or missing references) //IL_0041: Unknown result type (might be due to invalid IL or missing references) //IL_0048: Unknown result type (might be due to invalid IL or missing references) //IL_004f: Unknown result type (might be due to invalid IL or missing references) //IL_005b: Expected O, but got Unknown //IL_005c: Unknown result type (might be due to invalid IL or missing references) //IL_0067: Unknown result type (might be due to invalid IL or missing references) //IL_0072: Unknown result type (might be due to invalid IL or missing references) //IL_007c: Expected O, but got Unknown //IL_007d: Unknown result type (might be due to invalid IL or missing references) //IL_0087: Expected O, but got Unknown //IL_0088: Unknown result type (might be due to invalid IL or missing references) //IL_0092: Expected O, but got Unknown //IL_0093: Unknown result type (might be due to invalid IL or missing references) //IL_009d: Expected O, but got Unknown //IL_009d: Unknown result type (might be due to invalid IL or missing references) //IL_00a4: Expected O, but got Unknown //IL_00b6: Unknown result type (might be due to invalid IL or missing references) //IL_00cb: Unknown result type (might be due to invalid IL or missing references) //IL_00df: Unknown result type (might be due to invalid IL or missing references) //IL_010d: Unknown result type (might be due to invalid IL or missing references) //IL_0112: Unknown result type (might be due to invalid IL or missing references) //IL_0122: Unknown result type (might be due to invalid IL or missing references) //IL_013b: Unknown result type (might be due to invalid IL or missing references) //IL_0140: Unknown result type (might be due to invalid IL or missing references) //IL_0147: Unknown result type (might be due to invalid IL or missing references) //IL_014e: Unknown result type (might be due to invalid IL or missing references) //IL_01d3: Unknown result type (might be due to invalid IL or missing references) //IL_01e1: Unknown result type (might be due to invalid IL or missing references) //IL_01eb: Expected O, but got Unknown //IL_01eb: Unknown result type (might be due to invalid IL or missing references) //IL_01f3: Expected O, but got Unknown //IL_01fd: Expected O, but got Unknown UICursorProvider componentInChildren = ((Component)SingletonBehaviour<StartMenuLogic>.Instance).gameObject.GetComponentInChildren<UICursorProvider>(); GameObject val = new GameObject("ModOptions Panel"); val.transform.SetParent(((Component)componentInChildren).transform, false); RectTransform val2 = val.AddComponent<RectTransform>(); UIControlGroup val3 = val.AddComponent<UIControlGroup>(); RCGUIPanel component = val.GetComponent<RCGUIPanel>(); val.AddComponent<CanvasRenderer>(); val.AddComponent<SelectableNavigationProvider>(); val.AddComponent<Animator>(); AutoAttributeManager.AutoReference(val); val2.anchorMin = Vector2.zero; val2.anchorMax = Vector2.one; component.OnShowInit = new UnityEvent(); component.OnHideInit = new UnityEvent(); component.OnShowComplete = new UnityEvent(); component.OnHideComplete = new UnityEvent(); GameObject val4 = new GameObject(); RectTransform obj = val4.AddComponent<RectTransform>(); obj.anchorMin = new Vector2(0.5f, 0.5f); obj.anchorMax = new Vector2(0.5f, 0.5f); obj.sizeDelta = new Vector2(600f, 800f); ((HorizontalOrVerticalLayoutGroup)val4.AddComponent<VerticalLayoutGroup>()).spacing = 20f; val4.transform.SetParent(((Component)val3).transform, false); GameObject val5 = new GameObject(); ((TMP_Text)val5.AddComponent<TextMeshProUGUI>()).text = "Mod options"; val5.AddComponent<LayoutElement>(); val5.transform.SetParent(val4.transform, false); GameObject val6 = new GameObject(); val6.AddComponent<CanvasRenderer>(); val6.AddComponent<RectTransform>(); val6.AddComponent<LayoutElement>().minHeight = 0f; val6.transform.SetParent(val4.transform, false); Button val7 = ObjectUtils.FindDisabledByName<Button>("Show HUD"); foreach (KeyValuePair<string, PluginInfo> pluginInfo in Chainloader.PluginInfos) { ObjectUtils.InstantiateAutoReference(((Component)val7).gameObject, val4.transform, true).GetComponentInChildren<TMP_Text>().text = $"{pluginInfo.Key} {pluginInfo.Value.Metadata.Version}"; FlagFieldEntryInt val8 = new FlagFieldEntryInt(); GameFlagInt val9 = ScriptableObject.CreateInstance<GameFlagInt>(); ((AbstractScriptableData<FlagFieldInt, int>)(object)val9).field = new FlagFieldInt(); ((FlagFieldEntry<int>)val8).flagBase = (GameFlagBase)(object)val9; ((FlagFieldEntry<int>)val8).fieldName = "field"; } val3.defaultSelectable = val4.GetComponentInChildren<Selectable>(); return val3; } } } namespace Multiplayer { internal static class Log { private static ManualLogSource? logSource; internal static void Init(ManualLogSource logSource) { Log.logSource = logSource; } internal static void Debug(object data) { ManualLogSource? obj = logSource; if (obj != null) { obj.LogDebug(data); } } internal static void Error(object data) { ManualLogSource? obj = logSource; if (obj != null) { obj.LogError(data); } } internal static void Fatal(object data) { ManualLogSource? obj = logSource; if (obj != null) { obj.LogFatal(data); } } internal static void Info(object data) { ManualLogSource? obj = logSource; if (obj != null) { obj.LogInfo(data); } } internal static void Message(object data) { ManualLogSource? obj = logSource; if (obj != null) { obj.LogMessage(data); } } internal static void Warning(object data) { ManualLogSource? obj = logSource; if (obj != null) { obj.LogWarning(data); } } } [BepInDependency(/*Could not decode attribute arguments.*/)] [BepInPlugin("Multiplayer", "Multiplayer", "1.0.0")] public class Multiplayer : BaseUnityPlugin { private Harmony _harmony; private NetManager _client; private NetDataWriter _dataWriter; private EventBasedNetListener _listener; private ConfigEntry<string> ip; private ConfigEntry<int> port; private ConfigEntry<bool> join; private ConfigEntry<bool> leave; private ConfigEntry<string> pvp; private ConfigEntry<string> playerName; private ConfigEntry<bool> displayPlayerName; private ConfigEntry<int> playerNameSize; public bool isPVP; private float timeStarted; private float timeLimit = 0.1f; private bool isTimerRunning; public bool isTexting; private bool testbool; private GameObject minionPrefab; public readonly Dictionary<int, PlayerData> _playerObjects = new Dictionary<int, PlayerData>(); private Dictionary<string, EnemyData> enemyDict = new Dictionary<string, EnemyData>(); private int _localPlayerId = -1; private const float SendInterval = 0.02f; private float _sendTimer; private string? currentAnimationState = string.Empty; public string? localAnimationState = ""; public string? enemyAnimationState = ""; private GameObject chatCanvas; private GameObject inputField; private GameObject chatLog; private GameObject scrollView; private Coroutine disableScrollCoroutine; private Coroutine clearAllEnemiesCoroutine; public static Multiplayer Instance { get; private set; } private void Awake() { Instance = this; Log.Init(((BaseUnityPlugin)this).Logger); try { RCGLifeCycle.DontDestroyForever(((Component)this).gameObject); _harmony = Harmony.CreateAndPatchAll(typeof(Multiplayer).Assembly, (string)null); InitializeNetworking(); ip = ((BaseUnityPlugin)this).Config.Bind<string>("", "Server Ip", "yukikaco.ddns.net", ""); port = ((BaseUnityPlugin)this).Config.Bind<int>("", "Server Port", 9050, ""); join = ((BaseUnityPlugin)this).Config.Bind<bool>("", "Join Server Button", false, ""); leave = ((BaseUnityPlugin)this).Config.Bind<bool>("", "Leave Server Button", false, ""); pvp = ((BaseUnityPlugin)this).Config.Bind<string>("", "Server PVP State", "", ""); playerName = ((BaseUnityPlugin)this).Config.Bind<string>("Name", "Your Player Name", "", ""); displayPlayerName = ((BaseUnityPlugin)this).Config.Bind<bool>("Name", "Is Display Player Name", true, ""); playerNameSize = ((BaseUnityPlugin)this).Config.Bind<int>("Name", "Player Name Size", 200, ""); join.SettingChanged += delegate { if (join.Value) { ConnectToServer(); } join.Value = false; }; leave.SettingChanged += delegate { if (leave.Value) { DisconnectFromServer(); } leave.Value = false; }; displayPlayerName.SettingChanged += delegate { SetPlayerNameVisible(); }; playerNameSize.SettingChanged += delegate { SetPlayerNameSize(); }; SceneManager.sceneLoaded += OnSceneLoaded; } catch (Exception arg) { Log.Error($"Failed to initialized modding API: {arg}"); } Log.Info("Multiplayer plugin initialized."); } private void CreateChatCanvas() { //IL_0006: Unknown result type (might be due to invalid IL or missing references) //IL_0010: Expected O, but got Unknown //IL_0054: Unknown result type (might be due to invalid IL or missing references) //IL_0069: Unknown result type (might be due to invalid IL or missing references) //IL_007e: Unknown result type (might be due to invalid IL or missing references) //IL_0093: Unknown result type (might be due to invalid IL or missing references) //IL_00a7: Unknown result type (might be due to invalid IL or missing references) chatCanvas = new GameObject("ChatCanvas"); chatCanvas.AddComponent<Canvas>().renderMode = (RenderMode)0; chatCanvas.AddComponent<CanvasScaler>().uiScaleMode = (ScaleMode)1; chatCanvas.AddComponent<GraphicRaycaster>(); RectTransform component = chatCanvas.GetComponent<RectTransform>(); component.sizeDelta = new Vector2(400f, 300f); component.anchorMin = new Vector2(0f, 0f); component.anchorMax = new Vector2(0f, 0f); component.pivot = new Vector2(0f, 0f); component.anchoredPosition = new Vector2(0f, 0f); RCGLifeCycle.DontDestroyForever(chatCanvas); } private void SetPlayerNameSize() { float fontSize = playerNameSize.Value; foreach (PlayerData value in _playerObjects.Values) { Transform val = value.PlayerObject.transform.Find("PlayerName"); if ((Object)(object)val != (Object)null) { TextMeshPro component = ((Component)val).GetComponent<TextMeshPro>(); if ((Object)(object)component != (Object)null) { ((TMP_Text)component).fontSize = fontSize; } else { Log.Warning("TextMeshPro component not found in " + ((Object)value.PlayerObject).name + "'s PlayerName."); } } else { Log.Warning("PlayerName GameObject not found in " + ((Object)value.PlayerObject).name); } } } private void SetPlayerNameVisible() { bool value = displayPlayerName.Value; foreach (PlayerData value2 in _playerObjects.Values) { Transform obj = value2.PlayerObject.transform.Find("PlayerName"); GameObject val = ((obj != null) ? ((Component)obj).gameObject : null); if ((Object)(object)val != (Object)null) { val.SetActive(value); } } } private void InitializeNetworking() { //IL_0001: Unknown result type (might be due to invalid IL or missing references) //IL_000b: Expected O, but got Unknown //IL_0013: Unknown result type (might be due to invalid IL or missing references) //IL_0018: Unknown result type (might be due to invalid IL or missing references) //IL_0024: Expected O, but got Unknown //IL_0025: Unknown result type (might be due to invalid IL or missing references) //IL_002f: Expected O, but got Unknown //IL_003c: Unknown result type (might be due to invalid IL or missing references) //IL_0046: Expected O, but got Unknown //IL_0053: Unknown result type (might be due to invalid IL or missing references) //IL_005d: Expected O, but got Unknown _listener = new EventBasedNetListener(); _client = new NetManager((INetEventListener)(object)_listener, (PacketLayerBase)null) { AutoRecycle = true }; _dataWriter = new NetDataWriter(); _listener.NetworkReceiveEvent += new OnNetworkReceive(OnNetworkReceiveEvent); _listener.PeerDisconnectedEvent += new OnPeerDisconnected(OnPeerDisconnectedEvent); } private void OnSceneLoaded(Scene scene, LoadSceneMode mode) { //IL_003c: Unknown result type (might be due to invalid IL or missing references) //IL_0041: Unknown result type (might be due to invalid IL or missing references) //IL_000f: Unknown result type (might be due to invalid IL or missing references) if (_client.FirstPeer != null) { ((MonoBehaviour)this).StartCoroutine(WaitAndClearPlayerObjects(scene)); } _dataWriter.Reset(); _dataWriter.Put("Scene"); NetDataWriter dataWriter = _dataWriter; Scene activeScene = SceneManager.GetActiveScene(); dataWriter.Put(((Scene)(ref activeScene)).name); _client.FirstPeer.Send(_dataWriter, (DeliveryMethod)2); } private IEnumerator WaitAndClearPlayerObjects(Scene scene) { while ((int)SingletonBehaviour<GameCore>.Instance.currentCoreState != 1) { yield return (object)new WaitForSeconds(0.3f); } while ((int)Player.i.playerInput.currentStateType != 0) { yield return (object)new WaitForSeconds(0.3f); } ClearPlayerObjects(); } private void ConnectToServer() { if (_client.IsRunning) { return; } if ((Object)(object)Player.i == (Object)null) { ToastManager.Toast((object)"Yi haven't create. Enter game try join server again"); return; } ToastManager.Toast((object)"Connecting to server..."); _client.Start(); _client.Connect(ip.Value, port.Value, "SomeConnectionKey"); _localPlayerId = -1; ClearPlayerObjects(); ((MonoBehaviour)this).StartCoroutine(CheckConnectionStatus(OnConnectionStatusChecked)); if ((Object)(object)chatCanvas == (Object)null) { Canvas[] array = Resources.FindObjectsOfTypeAll<Canvas>(); foreach (Canvas val in array) { if (((Object)val).name == "ChatCanvas (RCGLifeCycle)") { chatCanvas = ((Component)val).gameObject; } } if ((Object)(object)chatCanvas == (Object)null) { CreateChatCanvas(); } } if ((Object)(object)scrollView == (Object)null) { ScrollRect[] array2 = Resources.FindObjectsOfTypeAll<ScrollRect>(); foreach (ScrollRect val2 in array2) { if (((Object)((Component)val2).transform.parent).name == "ChatCanvas (RCGLifeCycle)") { scrollView = ((Component)val2).gameObject; ToastManager.Toast((object)((Object)val2).name); } } if ((Object)(object)scrollView == (Object)null) { CreateChatLog(); } } if ((Object)(object)inputField == (Object)null) { InputField[] array3 = Resources.FindObjectsOfTypeAll<InputField>(); foreach (InputField val3 in array3) { if (((Object)((Component)val3).transform.parent).name == "ChatCanvas (RCGLifeCycle)") { inputField = ((Component)val3).gameObject; ToastManager.Toast((object)((Object)val3).name); } } if ((Object)(object)inputField == (Object)null) { CreateInputField(); } } clearAllEnemiesCoroutine = ((MonoBehaviour)this).StartCoroutine(ClearAllEnemies()); } private void OnConnectionStatusChecked(bool success) { if (!success) { ToastManager.Toast((object)"Connection failed."); } else if (!((Object)(object)Player.i == (Object)null)) { LoadMinionPrefab(); } } private void ConfigurePlayerEffectReceiver() { //IL_0030: Unknown result type (might be due to invalid IL or missing references) Transform obj = ((Component)Player.i).transform.Find("RotateProxy/SpriteHolder/Health(Don'tKey)/DamageReceiver"); EffectReceiver val = ((obj != null) ? ((Component)obj).GetComponent<EffectReceiver>() : null); if ((Object)(object)val != (Object)null) { val.effectType = (EffectType)10369; } } private void LoadMinionPrefab() { MonsterBase[] array = Resources.FindObjectsOfTypeAll<MonsterBase>(); foreach (MonsterBase val in array) { if (!(((Object)val).name == "StealthGameMonster_Minion_prefab")) { continue; } minionPrefab = ((Component)val).gameObject; AutoAttributeManager.AutoReference(minionPrefab); AutoAttributeManager.AutoReferenceAllChildren(minionPrefab); ILevelAwake[] componentsInChildren = minionPrefab.GetComponentsInChildren<ILevelAwake>(true); for (int num = componentsInChildren.Length - 1; num >= 0; num--) { ILevelAwake val2 = componentsInChildren[num]; try { val2.EnterLevelAwake(); } catch (Exception ex) { Log.Error(ex.StackTrace); } } return; } if ((Object)(object)minionPrefab == (Object)null) { ((MonoBehaviour)this).StartCoroutine(PreloadMinionPrefab()); } } private IEnumerator CheckConnectionStatus(Action<bool> callback) { float timeout = 2f; float elapsedTime = 0f; while (true) { NetPeer firstPeer = _client.FirstPeer; if ((firstPeer != null && (int)firstPeer.ConnectionState == 4) || !(elapsedTime < timeout)) { break; } elapsedTime += Time.deltaTime; yield return null; } NetPeer firstPeer2 = _client.FirstPeer; bool flag = firstPeer2 != null && (int)firstPeer2.ConnectionState == 4; callback(flag); if (!flag) { _client.Stop(); } } private IEnumerator PreloadMinionPrefab() { yield return ((MonoBehaviour)this).StartCoroutine(LoadUnloadScene()); MonsterBase[] array = Resources.FindObjectsOfTypeAll<MonsterBase>(); foreach (MonsterBase val in array) { if (((Object)val).name == "StealthGameMonster_Minion_prefab") { minionPrefab = ((Component)val).gameObject; AutoAttributeManager.AutoReference(minionPrefab); AutoAttributeManager.AutoReferenceAllChildren(minionPrefab); break; } } if ((Object)(object)minionPrefab != (Object)null) { Log.Info("Object '" + ((Object)minionPrefab).name + "' found."); } else { Log.Warning("Object 'StealthGameMonster_Minion_prefab' not found."); } } private IEnumerator LoadUnloadScene() { string sceneName = "A1_S2_ConnectionToElevator_Final"; AsyncOperation asyncLoad = SceneManager.LoadSceneAsync(sceneName, (LoadSceneMode)1); while (!asyncLoad.isDone) { yield return null; } Log.Info("Scene " + sceneName + " loaded."); Scene sceneByName = SceneManager.GetSceneByName(sceneName); if (!((Scene)(ref sceneByName)).IsValid()) { Log.Error("Scene " + sceneName + " is not valid after loading."); yield break; } AsyncOperation asyncUnload = SceneManager.UnloadSceneAsync(sceneName); while (!asyncUnload.isDone) { yield return null; } Log.Info("Scene " + sceneName + " unloaded."); Log.Info("Waiting for 3 seconds before reloading the active scene..."); yield return WaitForPrefabAndGameCoreState("StealthGameMonster_Minion_prefab"); Scene activeScene = SceneManager.GetActiveScene(); SceneManager.LoadScene(((Scene)(ref activeScene)).name); Log.Info("Active scene reloaded."); } private IEnumerator WaitForPrefabAndGameCoreState(string prefabName, float timeoutSeconds = 2f) { float elapsedTime = 0f; while ((Object)(object)minionPrefab == (Object)null && elapsedTime < timeoutSeconds) { MonsterBase[] array = Resources.FindObjectsOfTypeAll<MonsterBase>(); foreach (MonsterBase val in array) { if (((Object)val).name == prefabName) { minionPrefab = ((Component)val).gameObject; break; } } if ((Object)(object)minionPrefab == (Object)null) { Log.Info("Waiting for '" + prefabName + "' to be found..."); } yield return null; elapsedTime += Time.deltaTime; } if ((Object)(object)minionPrefab == (Object)null) { Log.Warning($"Timeout: Prefab '{prefabName}' not found within {timeoutSeconds} seconds."); } else { Log.Info($"Prefab '{((Object)minionPrefab).name}' found within {elapsedTime:F2} seconds."); } } private async void DisconnectFromServer() { if (_client.IsRunning) { _dataWriter.Reset(); _dataWriter.Put("Leave"); _dataWriter.Put(playerName.Value); _client.FirstPeer.Send(_dataWriter, (DeliveryMethod)2); int elapsedTime = 0; while ((int)_client.FirstPeer.ConnectionState != 16 && elapsedTime < 500) { await Task.Delay(50); elapsedTime += 50; } _client.DisconnectAll(); _client.Stop(); _localPlayerId = -1; ClearPlayerObjects(); ClearEnemyObjects(); StopClearingEnemies(); ToastManager.Toast((object)"Disconnected from server."); } } private void ClearEnemyObjects() { foreach (EnemyData value in enemyDict.Values) { Object.Destroy((Object)(object)value.EnemyObject); } enemyDict.Clear(); } private void ClearPlayerObjects() { foreach (PlayerData value in _playerObjects.Values) { Object.Destroy((Object)(object)value.PlayerObject); } _playerObjects.Clear(); } public void SendEnemy(string uniqueID, float health, Vector3 pos, bool isFacingRight) { //IL_0022: Unknown result type (might be due to invalid IL or missing references) //IL_0028: Invalid comparison between Unknown and I4 //IL_008a: Unknown result type (might be due to invalid IL or missing references) //IL_009b: Unknown result type (might be due to invalid IL or missing references) //IL_00ac: Unknown result type (might be due to invalid IL or missing references) if (_client.IsRunning) { NetPeer firstPeer = _client.FirstPeer; if (firstPeer != null && (int)firstPeer.ConnectionState == 4) { _dataWriter.Reset(); _dataWriter.Put("Enemy"); _dataWriter.Put(uniqueID); _dataWriter.Put(_localPlayerId); _dataWriter.Put(health); _dataWriter.Put(enemyAnimationState); _dataWriter.Put(pos.x); _dataWriter.Put(pos.y); _dataWriter.Put(pos.z); _dataWriter.Put(isFacingRight); _client.FirstPeer.Send(_dataWriter, (DeliveryMethod)2); } } } private void HandleEnemyUpdate(NetDataReader reader) { //IL_00b7: Unknown result type (might be due to invalid IL or missing references) //IL_00b9: Unknown result type (might be due to invalid IL or missing references) //IL_0304: Unknown result type (might be due to invalid IL or missing references) //IL_0317: Unknown result type (might be due to invalid IL or missing references) //IL_031c: Unknown result type (might be due to invalid IL or missing references) //IL_0320: Unknown result type (might be due to invalid IL or missing references) //IL_0280: Unknown result type (might be due to invalid IL or missing references) //IL_0282: Unknown result type (might be due to invalid IL or missing references) //IL_0113: Unknown result type (might be due to invalid IL or missing references) //IL_0118: Unknown result type (might be due to invalid IL or missing references) //IL_0126: Unknown result type (might be due to invalid IL or missing references) //IL_0351: Unknown result type (might be due to invalid IL or missing references) //IL_02b2: Unknown result type (might be due to invalid IL or missing references) //IL_02b7: Unknown result type (might be due to invalid IL or missing references) //IL_02c5: Unknown result type (might be due to invalid IL or missing references) //IL_0382: Unknown result type (might be due to invalid IL or missing references) //IL_0387: Unknown result type (might be due to invalid IL or missing references) string @string = reader.GetString(); int @int = reader.GetInt(); float @float = reader.GetFloat(); string string2 = reader.GetString(); float float2 = reader.GetFloat(); float float3 = reader.GetFloat(); float float4 = reader.GetFloat(); bool @bool = reader.GetBool(); Vector3 val = default(Vector3); foreach (MonsterBase value2 in SingletonBehaviour<MonsterManager>.Instance.monsterDict.Values) { if (!(((Actor)value2).ActorID == @string)) { continue; } GameObject gameObject = ((Component)value2).gameObject; ((Vector3)(ref val))..ctor(float2, float3, float4); if (@int == -1) { break; } if (!enemyDict.TryGetValue(@int.ToString(), out EnemyData value)) { if (@float > 0f) { value = new EnemyData(Object.Instantiate<GameObject>(gameObject, val, Quaternion.identity), @string); enemyDict[@int.ToString()] = value; if (!value.EnemyObject.activeSelf) { value.EnemyObject.SetActive(true); } SpriteRenderer[] componentsInChildren = value.EnemyObject.GetComponentsInChildren<SpriteRenderer>(); foreach (SpriteRenderer obj in componentsInChildren) { Color color = obj.color; color.a = 0.4f; obj.color = color; } MultiSpriteEffect[] componentsInChildren2 = value.EnemyObject.GetComponentsInChildren<MultiSpriteEffect>(); for (int i = 0; i < componentsInChildren2.Length; i++) { Object.Destroy((Object)(object)((Component)componentsInChildren2[i]).gameObject); } WeaknessEffectManager[] componentsInChildren3 = value.EnemyObject.GetComponentsInChildren<WeaknessEffectManager>(); for (int i = 0; i < componentsInChildren3.Length; i++) { Object.Destroy((Object)(object)((Component)componentsInChildren3[i]).gameObject); } Log.Info($"Created new enemy for Player {@int}: Enemy ID: {@string}"); } continue; } if (@float <= 0f) { Log.Info($"Enemy {value.guid} for Player {@int} has died. Removing."); Object.Destroy((Object)(object)value.EnemyObject); enemyDict.Remove(@int.ToString()); continue; } if (!((Component)value.EnemyObject.transform.Find("MonsterCore")).gameObject.activeSelf) { ((Component)value.EnemyObject.transform.Find("MonsterCore")).gameObject.SetActive(true); } if (value.guid != @string) { Log.Info($"Replacing enemy for Player {@int}: Old GUID: {value.guid}, New GUID: {@string}"); Object.Destroy((Object)(object)value.EnemyObject); value.EnemyObject = Object.Instantiate<GameObject>(gameObject, val, Quaternion.identity); value.guid = @string; SpriteRenderer[] componentsInChildren = value.EnemyObject.GetComponentsInChildren<SpriteRenderer>(); foreach (SpriteRenderer obj2 in componentsInChildren) { Color color2 = obj2.color; color2.a = 0.4f; obj2.color = color2; } enemyDict[@int.ToString()] = value; } value.EnemyObject.transform.Find("MonsterCore/Animator(Proxy)/Animator").position = val; Vector3 localScale = value.EnemyObject.transform.localScale; localScale.x *= (float)(@bool ? 1 : (-1)); ((Component)value.EnemyObject.transform.Find("MonsterCore/Animator(Proxy)/Animator")).transform.localScale = localScale; Animator component = ((Component)value.EnemyObject.transform.Find("MonsterCore/Animator(Proxy)/Animator")).GetComponent<Animator>(); if ((Object)(object)component != (Object)null) { AnimatorStateInfo currentAnimatorStateInfo = component.GetCurrentAnimatorStateInfo(0); if (!((AnimatorStateInfo)(ref currentAnimatorStateInfo)).IsName(string2)) { Log.Info($"Playing animation '{string2}' for Enemy {@string} of Player {@int}"); component.Play(string2, 0, 0f); } } } } private void StopClearingEnemies() { if (clearAllEnemiesCoroutine != null) { ((MonoBehaviour)this).StopCoroutine(clearAllEnemiesCoroutine); clearAllEnemiesCoroutine = null; Log.Info("Clearing enemies coroutine has been stopped."); } } private IEnumerator ClearAllEnemies() { while (true) { yield return (object)new WaitForSeconds(5f); foreach (KeyValuePair<string, EnemyData> item in enemyDict) { EnemyData value = item.Value; if ((Object)(object)value.EnemyObject != (Object)null) { Object.Destroy((Object)(object)value.EnemyObject); Log.Info("Destroyed enemy " + value.guid + " due to periodic cleanup."); } } enemyDict.Clear(); Log.Info("Cleared all enemies."); } } private void Update() { //IL_0022: Unknown result type (might be due to invalid IL or missing references) //IL_0028: Invalid comparison between Unknown and I4 //IL_0078: Unknown result type (might be due to invalid IL or missing references) //IL_007d: Unknown result type (might be due to invalid IL or missing references) //IL_0090: Unknown result type (might be due to invalid IL or missing references) //IL_0092: Unknown result type (might be due to invalid IL or missing references) //IL_0097: Unknown result type (might be due to invalid IL or missing references) if (_client.IsRunning) { NetPeer firstPeer = _client.FirstPeer; if (firstPeer != null && (int)firstPeer.ConnectionState == 4) { _sendTimer += Time.deltaTime; if (_sendTimer >= 0.02f) { SendPosition(); MonsterBase val = SingletonBehaviour<MonsterManager>.Instance.FindClosestMonster(); if ((Object)(object)val != (Object)null) { Vector3 position = ((Component)val).transform.Find("MonsterCore/Animator(Proxy)/Animator").position; string actorID = ((Actor)val).ActorID; float currentHealthValue = val.postureSystem.CurrentHealthValue; Facings facing = ((Actor)val).Facing; SendEnemy(actorID, currentHealthValue, position, ((object)(Facings)(ref facing)).ToString() == "Right"); } _sendTimer = 0f; } } } _client.PollEvents(); if (!((Object)(object)inputField != (Object)null)) { return; } InputField component = inputField.GetComponent<InputField>(); if (Input.GetKeyDown((KeyCode)13)) { isTexting = true; Cursor.lockState = (CursorLockMode)0; Cursor.visible = true; if (disableScrollCoroutine != null) { ((MonoBehaviour)this).StopCoroutine(disableScrollCoroutine); disableScrollCoroutine = null; } if ((Object)(object)scrollView != (Object)null && (Object)(object)inputField != (Object)null && !component.isFocused) { scrollView.SetActive(true); inputField.SetActive(true); component.ActivateInputField(); } if (!isTimerRunning) { timeStarted = Time.time; isTimerRunning = true; } string text = component.text.Trim(); if (!string.IsNullOrWhiteSpace(text)) { SendMessageToChat(text); component.text = string.Empty; if ((Object)(object)inputField != (Object)null) { inputField.SetActive(false); } if (disableScrollCoroutine != null) { ((MonoBehaviour)this).StopCoroutine(disableScrollCoroutine); } disableScrollCoroutine = ((MonoBehaviour)this).StartCoroutine(DisableScrollViewAfterDelay(3f)); isTexting = false; } if ((Object)(object)inputField != (Object)null) { component.ActivateInputField(); } } if (isTimerRunning && Time.time - timeStarted >= timeLimit && Input.GetKeyDown((KeyCode)13)) { if ((Object)(object)inputField != (Object)null) { inputField.SetActive(false); } isTexting = false; if (disableScrollCoroutine != null) { ((MonoBehaviour)this).StopCoroutine(disableScrollCoroutine); disableScrollCoroutine = null; } disableScrollCoroutine = ((MonoBehaviour)this).StartCoroutine(DisableScrollViewAfterDelay(3f)); isTimerRunning = false; } if (Input.GetKeyDown((KeyCode)27)) { if ((Object)(object)inputField != (Object)null) { component.text = string.Empty; inputField.SetActive(false); } isTexting = false; if (disableScrollCoroutine != null) { ((MonoBehaviour)this).StopCoroutine(disableScrollCoroutine); disableScrollCoroutine = null; } disableScrollCoroutine = ((MonoBehaviour)this).StartCoroutine(DisableScrollViewAfterDelay(3f)); } } private IEnumerator DisableScrollViewAfterDelay(float delay) { yield return (object)new WaitForSeconds(delay); scrollView.SetActive(false); disableScrollCoroutine = null; } private void CreateInputField() { //IL_0006: Unknown result type (might be due to invalid IL or missing references) //IL_0010: Expected O, but got Unknown //IL_0042: Unknown result type (might be due to invalid IL or missing references) //IL_0057: Unknown result type (might be due to invalid IL or missing references) //IL_006c: Unknown result type (might be due to invalid IL or missing references) //IL_0081: Unknown result type (might be due to invalid IL or missing references) //IL_0095: Unknown result type (might be due to invalid IL or missing references) //IL_00aa: Unknown result type (might be due to invalid IL or missing references) //IL_00c4: Unknown result type (might be due to invalid IL or missing references) //IL_00c9: Unknown result type (might be due to invalid IL or missing references) //IL_00e0: Unknown result type (might be due to invalid IL or missing references) //IL_00f1: Unknown result type (might be due to invalid IL or missing references) //IL_00fb: Unknown result type (might be due to invalid IL or missing references) //IL_0124: Unknown result type (might be due to invalid IL or missing references) //IL_0141: Unknown result type (might be due to invalid IL or missing references) //IL_0146: Unknown result type (might be due to invalid IL or missing references) //IL_015d: Unknown result type (might be due to invalid IL or missing references) //IL_016e: Unknown result type (might be due to invalid IL or missing references) //IL_0178: Unknown result type (might be due to invalid IL or missing references) //IL_01b5: Unknown result type (might be due to invalid IL or missing references) inputField = new GameObject("InputField"); inputField.transform.SetParent(chatCanvas.transform, false); RectTransform obj = inputField.AddComponent<RectTransform>(); obj.sizeDelta = new Vector2(380f, 30f); obj.anchorMin = new Vector2(0f, 0f); obj.anchorMax = new Vector2(0f, 0f); obj.pivot = new Vector2(0f, 0f); obj.anchoredPosition = new Vector2(20f, 20f); ((Graphic)inputField.AddComponent<Image>()).color = Color.gray; InputField obj2 = inputField.AddComponent<InputField>(); GameObject val = new GameObject("Text"); val.transform.SetParent(inputField.transform, false); RectTransform obj3 = val.AddComponent<RectTransform>(); obj3.sizeDelta = new Vector2(360f, 25f); obj3.anchoredPosition = Vector2.zero; Text val2 = val.AddComponent<Text>(); val2.font = Resources.GetBuiltinResource<Font>("Arial.ttf"); val2.fontSize = 12; ((Graphic)val2).color = Color.black; val2.alignment = (TextAnchor)3; obj2.textComponent = val2; GameObject val3 = new GameObject("Placeholder"); val3.transform.SetParent(inputField.transform, false); RectTransform obj4 = val3.AddComponent<RectTransform>(); obj4.sizeDelta = new Vector2(360f, 25f); obj4.anchoredPosition = Vector2.zero; Text val4 = val3.AddComponent<Text>(); val4.font = Resources.GetBuiltinResource<Font>("Arial.ttf"); val4.fontSize = 12; ((Graphic)val4).color = new Color(0.7f, 0.7f, 0.7f, 1f); val4.text = "Enter message..."; val4.alignment = (TextAnchor)3; obj2.placeholder = (Graphic)(object)val4; inputField.SetActive(false); } private void CreateChatLog() { //IL_0006: Unknown result type (might be due to invalid IL or missing references) //IL_0010: Expected O, but got Unknown //IL_001b: Unknown result type (might be due to invalid IL or missing references) //IL_006f: Unknown result type (might be due to invalid IL or missing references) //IL_0084: Unknown result type (might be due to invalid IL or missing references) //IL_0099: Unknown result type (might be due to invalid IL or missing references) //IL_00ae: Unknown result type (might be due to invalid IL or missing references) //IL_00c2: Unknown result type (might be due to invalid IL or missing references) //IL_00f7: Unknown result type (might be due to invalid IL or missing references) //IL_0106: Unknown result type (might be due to invalid IL or missing references) //IL_010c: Expected O, but got Unknown //IL_0134: Unknown result type (might be due to invalid IL or missing references) //IL_0149: Unknown result type (might be due to invalid IL or missing references) scrollView = new GameObject("ChatLogScrollView"); scrollView.transform.localPosition = Vector3.zero; scrollView.transform.SetParent(chatCanvas.transform); ScrollRect obj = scrollView.AddComponent<ScrollRect>(); obj.vertical = true; obj.horizontal = false; RectTransform component = scrollView.GetComponent<RectTransform>(); component.sizeDelta = new Vector2(380f, 130f); component.anchorMin = new Vector2(0f, 0f); component.anchorMax = new Vector2(0f, 0f); component.anchoredPosition = new Vector2(20f, 50f); component.pivot = new Vector2(0f, 0f); scrollView.AddComponent<Mask>(); ((Graphic)scrollView.AddComponent<Image>()).color = new Color(0f, 0f, 0f, 0.3f); GameObject val = new GameObject("Content"); val.transform.SetParent(scrollView.transform); RectTransform val2 = val.AddComponent<RectTransform>(); val2.sizeDelta = new Vector2(360f, 0f); val2.anchoredPosition = new Vector2(10f, 0f); VerticalLayoutGroup obj2 = val.AddComponent<VerticalLayoutGroup>(); ((LayoutGroup)obj2).childAlignment = (TextAnchor)0; ((HorizontalOrVerticalLayoutGroup)obj2).childForceExpandWidth = true; ((HorizontalOrVerticalLayoutGroup)obj2).childForceExpandHeight = false; ((HorizontalOrVerticalLayoutGroup)obj2).spacing = 2f; val.AddComponent<ContentSizeFitter>().verticalFit = (FitMode)2; obj.content = val2; chatLog = val; scrollView.SetActive(false); } private void ReceiveMessageToChat(string message) { //IL_0035: Unknown result type (might be due to invalid IL or missing references) //IL_003a: Unknown result type (might be due to invalid IL or missing references) //IL_0051: Unknown result type (might be due to invalid IL or missing references) //IL_0061: Unknown result type (might be due to invalid IL or missing references) //IL_006b: Unknown result type (might be due to invalid IL or missing references) //IL_008a: Unknown result type (might be due to invalid IL or missing references) if (!string.IsNullOrWhiteSpace(message)) { scrollView.SetActive(true); if (disableScrollCoroutine != null) { ((MonoBehaviour)this).StopCoroutine(disableScrollCoroutine); disableScrollCoroutine = null; } GameObject val = new GameObject("ChatMessage"); val.transform.SetParent(chatLog.transform, false); val.AddComponent<RectTransform>().sizeDelta = new Vector2(0f, 0f); Text obj = val.AddComponent<Text>(); obj.font = Resources.GetBuiltinResource<Font>("Arial.ttf"); obj.fontSize = 12; ((Graphic)obj).color = Color.white; obj.text = message; obj.horizontalOverflow = (HorizontalWrapMode)0; obj.verticalOverflow = (VerticalWrapMode)1; obj.alignment = (TextAnchor)0; LayoutElement obj2 = val.AddComponent<LayoutElement>(); obj2.preferredWidth = 380f; obj2.flexibleHeight = 0f; obj2.minHeight = 5f; Canvas.ForceUpdateCanvases(); ScrollRect componentInParent = chatLog.GetComponentInParent<ScrollRect>(); if ((Object)(object)componentInParent != (Object)null) { Canvas.ForceUpdateCanvases(); componentInParent.verticalNormalizedPosition = 0f; } if (!isTexting) { disableScrollCoroutine = ((MonoBehaviour)this).StartCoroutine(DisableScrollViewAfterDelay(3f)); } } } private void SendMessageToChat(string message) { //IL_000e: Unknown result type (might be due to invalid IL or missing references) //IL_0013: Unknown result type (might be due to invalid IL or missing references) //IL_002a: Unknown result type (might be due to invalid IL or missing references) //IL_003a: Unknown result type (might be due to invalid IL or missing references) //IL_0044: Unknown result type (might be due to invalid IL or missing references) //IL_0063: Unknown result type (might be due to invalid IL or missing references) if (!string.IsNullOrWhiteSpace(message)) { GameObject val = new GameObject("ChatMessage"); val.transform.SetParent(chatLog.transform, false); val.AddComponent<RectTransform>().sizeDelta = new Vector2(0f, 0f); Text obj = val.AddComponent<Text>(); obj.font = Resources.GetBuiltinResource<Font>("Arial.ttf"); obj.fontSize = 12; ((Graphic)obj).color = Color.white; obj.text = playerName.Value + ": " + message; obj.horizontalOverflow = (HorizontalWrapMode)0; obj.verticalOverflow = (VerticalWrapMode)1; obj.alignment = (TextAnchor)0; LayoutElement obj2 = val.AddComponent<LayoutElement>(); obj2.preferredWidth = 380f; obj2.flexibleHeight = 0f; obj2.minHeight = 5f; Canvas.ForceUpdateCanvases(); ScrollRect componentInParent = chatLog.GetComponentInParent<ScrollRect>(); if ((Object)(object)componentInParent != (Object)null) { Canvas.ForceUpdateCanvases(); componentInParent.verticalNormalizedPosition = 0f; } if (_client.FirstPeer == null) { ToastManager.Toast((object)"Not connected to server."); return; } _dataWriter.Reset(); _dataWriter.Put("Chat"); _dataWriter.Put(playerName.Value + ": " + message); _client.FirstPeer.Send(_dataWriter, (DeliveryMethod)4); } } public void SendDecreaseHealth(int playerId, float value) { if (_client.FirstPeer == null) { ToastManager.Toast((object)"Not connected to server."); return; } _dataWriter.Reset(); _dataWriter.Put("DecreaseHealth"); _dataWriter.Put(playerId); _dataWriter.Put(value); _client.FirstPeer.Send(_dataWriter, (DeliveryMethod)4); } public void SendRecoverableDamage(int playerId, float value) { if (_client.FirstPeer == null) { ToastManager.Toast((object)"Not connected to server."); return; } _dataWriter.Reset(); _dataWriter.Put("RecoverableDamage"); _dataWriter.Put(playerId); _dataWriter.Put(value); _client.FirstPeer.Send(_dataWriter, (DeliveryMethod)4); } private void SendPosition() { //IL_003c: Unknown result type (might be due to invalid IL or missing references) //IL_0041: Unknown result type (might be due to invalid IL or missing references) //IL_0048: Unknown result type (might be due to invalid IL or missing references) //IL_0059: Unknown result type (might be due to invalid IL or missing references) //IL_0070: Unknown result type (might be due to invalid IL or missing references) //IL_0097: Unknown result type (might be due to invalid IL or missing references) //IL_009c: Unknown result type (might be due to invalid IL or missing references) if (_localPlayerId != -1 && !((Object)(object)Player.i == (Object)null)) { _dataWriter.Reset(); _dataWriter.Put("Position"); Vector3 position = ((Component)Player.i).transform.position; _dataWriter.Put(position.x); _dataWriter.Put(position.y + 6.5f); _dataWriter.Put(position.z); _dataWriter.Put(localAnimationState); NetDataWriter dataWriter = _dataWriter; Facings facing = ((Actor)Player.i).Facing; dataWriter.Put(((object)(Facings)(ref facing)).ToString() == "Right"); _client.FirstPeer.Send(_dataWriter, (DeliveryMethod)4); } } private void OnNetworkReceiveEvent(NetPeer peer, NetPacketReader reader, byte channel, DeliveryMethod deliveryMethod) { HandleReceivedDataAsync((NetDataReader)(object)reader); reader.Recycle(); } private void OnPeerDisconnectedEvent(NetPeer peer, DisconnectInfo disconnectInfo) { ClearPlayerObjects(); } private async Task HandleReceivedDataAsync(NetDataReader reader) { string @string = reader.GetString(); Scene activeScene; switch (@string) { case "Position": HandlePositionMessage(reader); break; case "localPlayerId": { _localPlayerId = reader.GetInt(); _dataWriter.Reset(); _dataWriter.Put("Join"); _dataWriter.Put(playerName.Value); _client.FirstPeer.Send(_dataWriter, (DeliveryMethod)4); _dataWriter.Reset(); _dataWriter.Put("Scene"); NetDataWriter dataWriter = _dataWriter; activeScene = SceneManager.GetActiveScene(); dataWriter.Put(((Scene)(ref activeScene)).name); _client.FirstPeer.Send(_dataWriter, (DeliveryMethod)2); break; } case "DecreaseHealth": HandleDecreaseHealth(reader); break; case "RecoverableDamage": HandleRecoverableDamage(reader); break; case "DestroyDisconnectObject": HandleDisconnectObject(reader); break; case "PvPEnabled": EnablePVP(reader); break; case "GetName": { int @int = reader.GetInt(); string string3 = reader.GetString(); _playerObjects[@int].name = string3; ((TMP_Text)((Component)_playerObjects[@int].PlayerObject.transform.Find("PlayerName")).GetComponent<TextMeshPro>()).text = string3; break; } case "tp": { string tpSceneName = reader.GetString(); ToastManager.Toast((object)("Server Teleported All Players to " + tpSceneName)); activeScene = SceneManager.GetActiveScene(); if (!(((Scene)(ref activeScene)).name == tpSceneName)) { GameCore instance = SingletonBehaviour<GameCore>.Instance; if (instance != null) { instance.GoToScene(tpSceneName); } await WaitPlaying(); TeleportPointData reviveSavePoint = new TeleportPointData { sceneName = tpSceneName, TeleportPosition = ((Component)Player.i).transform.position }; GameCore instance2 = SingletonBehaviour<GameCore>.Instance; if (instance2 != null) { instance2.SetReviveSavePoint(reviveSavePoint); } } break; } case "stop": ToastManager.Toast((object)"Server Owner Stop Server"); DisconnectFromServer(); break; case "Chat": { string string2 = reader.GetString(); ReceiveMessageToChat(string2); break; } case "Enemy": HandleEnemyUpdate(reader); break; default: ToastManager.Toast((object)@string); break; } } private async Task WaitPlaying() { while ((int)SingletonBehaviour<GameCore>.Instance.currentCoreState != 1 || (int)Player.i.playerInput.currentStateType == 3) { await Task.Delay(300); } } private void EnablePVP(NetDataReader reader) { //IL_0101: Unknown result type (might be due to invalid IL or missing references) //IL_010b: Unknown result type (might be due to invalid IL or missing references) //IL_010c: Unknown result type (might be due to invalid IL or missing references) //IL_0085: Unknown result type (might be due to invalid IL or missing references) //IL_008f: Unknown result type (might be due to invalid IL or missing references) //IL_0090: Unknown result type (might be due to invalid IL or missing references) //IL_014f: Unknown result type (might be due to invalid IL or missing references) //IL_0159: Unknown result type (might be due to invalid IL or missing references) //IL_015a: Unknown result type (might be due to invalid IL or missing references) //IL_00d3: Unknown result type (might be due to invalid IL or missing references) //IL_00dd: Unknown result type (might be due to invalid IL or missing references) //IL_00de: Unknown result type (might be due to invalid IL or missing references) isPVP = reader.GetBool(); pvp.Value = (isPVP ? "PVP Enabled" : "PVP Disabled"); ToastManager.Toast((object)("PvP " + (isPVP ? "Enabled" : "Disabled"))); Transform obj = ((Component)Player.i).transform.Find("RotateProxy/SpriteHolder/Health(Don'tKey)/DamageReceiver"); EffectReceiver val = ((obj != null) ? ((Component)obj).GetComponent<EffectReceiver>() : null); if (!((Object)(object)val != (Object)null)) { return; } if (isPVP) { val.effectType = (EffectType)(val.effectType | 0x2881); { foreach (KeyValuePair<int, PlayerData> playerObject in _playerObjects) { Transform obj2 = playerObject.Value.PlayerObject.transform.Find("RotateProxy/SpriteHolder/Health(Don'tKey)/DamageReceiver"); EffectReceiver obj3 = ((obj2 != null) ? ((Component)obj2).GetComponent<EffectReceiver>() : null); obj3.effectType = (EffectType)(obj3.effectType | 0x2881); } return; } } val.effectType = (EffectType)(val.effectType & -10369); foreach (KeyValuePair<int, PlayerData> playerObject2 in _playerObjects) { Transform obj4 = playerObject2.Value.PlayerObject.transform.Find("RotateProxy/SpriteHolder/Health(Don'tKey)/DamageReceiver"); EffectReceiver obj5 = ((obj4 != null) ? ((Component)obj4).GetComponent<EffectReceiver>() : null); obj5.effectType = (EffectType)(obj5.effectType & -10369); } } private void HandlePositionMessage(NetDataReader reader) { //IL_0042: Unknown result type (might be due to invalid IL or missing references) //IL_0047: Unknown result type (might be due to invalid IL or missing references) //IL_0104: Unknown result type (might be due to invalid IL or missing references) //IL_00eb: Unknown result type (might be due to invalid IL or missing references) int @int = reader.GetInt(); Vector3 position = default(Vector3); ((Vector3)(ref position))..ctor(reader.GetFloat(), reader.GetFloat(), reader.GetFloat()); string @string = reader.GetString(); bool @bool = reader.GetBool(); string string2 = reader.GetString(); if (_localPlayerId == @int) { return; } Scene activeScene = SceneManager.GetActiveScene(); if (string2 != ((Scene)(ref activeScene)).name) { if (_playerObjects.TryGetValue(@int, out PlayerData value)) { Object.Destroy((Object)(object)value.PlayerObject); _playerObjects.Remove(@int); } return; } if (!_playerObjects.TryGetValue(@int, out PlayerData value2)) { if ((Object)(object)minionPrefab == (Object)null) { return; } _dataWriter.Reset(); _dataWriter.Put("GetName"); _dataWriter.Put(@int); _client.FirstPeer.Send(_dataWriter, (DeliveryMethod)2); ToastManager.Toast((object)@int); value2 = CreatePlayerObject(@int, position); _playerObjects[@int] = value2; } UpdatePlayerObject(value2, position, @string, @bool); } private void HandleDecreaseHealth(NetDataReader reader) { ToastManager.Toast((object)"HandleDecreaseHealth"); int @int = reader.GetInt(); float @float = reader.GetFloat(); if (@int == _localPlayerId && (Object)(object)Player.i != (Object)null) { Player.i.health.ReceiveDOT_Damage(@float); Player.i.ChangeState((PlayerStateType)26, true); } } private void HandleRecoverableDamage(NetDataReader reader) { //IL_007a: Unknown result type (might be due to invalid IL or missing references) int @int = reader.GetInt(); float @float = reader.GetFloat(); ToastManager.Toast((object)$"HandleRecoverableDamage: {@int} {_localPlayerId}"); if (@int == _localPlayerId && (Object)(object)Player.i != (Object)null) { Player.i.health.ReceiveRecoverableDamage(@float); Player.i.ChangeState((PlayerStateType)33, true); ((Actor)Player.i).velocityModifierManager.attackKnockbackModifier.ApplyVelocity(400f * (0f - Player.i.towardDir.x), 0f); } } private void HandleDisconnectObject(NetDataReader reader) { int @int = reader.GetInt(); if (_playerObjects.TryGetValue(@int, out PlayerData value)) { Object.Destroy((Object)(object)value.PlayerObject); _playerObjects.Remove(@int); } if (enemyDict.TryGetValue(@int.ToString(), out EnemyData value2)) { Object.Destroy((Object)(object)value2.EnemyObject); enemyDict.Remove(@int.ToString()); } } private string GetGameObjectPath(GameObject obj) { string text = ((Object)obj).name; Transform val = obj.transform; while ((Object)(object)val.parent != (Object)null) { val = val.parent; text = ((Object)val).name + "/" + text; } return text; } private void MakeDamage(GameObject playerObject, Player dp) { //IL_0083: Unknown result type (might be due to invalid IL or missing references) Transform obj = playerObject.transform.Find("RotateProxy/SpriteHolder/HitBoxManager"); Transform val = ((obj != null) ? ((Component)obj).transform : null); if ((Object)(object)val == (Object)null) { return; } MonsterBase component = minionPrefab.GetComponent<MonsterBase>(); Transform obj2 = minionPrefab.transform.Find("MonsterCore/Animator(Proxy)/Animator/LogicRoot/SwordSlashEffect/DamageArea"); ParriableAttackEffect val2 = ((obj2 == null) ? null : ((Component)obj2).GetComponent<DamageDealer>()?.bindingParry); if ((Object)(object)component == (Object)null || (Object)(object)val2 == (Object)null) { return; } foreach (Transform item in val) { EffectDealer[] componentsInChildren = ((Component)item).GetComponentsInChildren<EffectDealer>(); foreach (EffectDealer val3 in componentsInChildren) { string gameObjectPath = GetGameObjectPath(((Component)val3).gameObject); DamageDealer val4 = AddDamageDealer(playerObject, gameObjectPath, val2, component, val3.FinalValue); if ((Object)(object)val4 != (Object)null) { ConfigureEffectDealer(val3, dp, val4); ConfigureEffectReceiver(playerObject, dp); } } } } private DamageDealer AddDamageDealer(GameObject playerObject, string hitBoxPath, ParriableAttackEffect bindingParry, MonsterBase monster, float damageAmount) { //IL_0018: Unknown result type (might be due to invalid IL or missing references) //IL_0025: Unknown result type (might be due to invalid IL or missing references) //IL_002f: Expected O, but got Unknown DamageDealer val = GameObject.Find(hitBoxPath).AddComponent<DamageDealer>(); if ((Object)(object)val != (Object)null) { val.type = (DamageType)16; val.bindingParry = bindingParry; val.attacker = new Health(); val.damageAmount = damageAmount; Traverse.Create((object)val).Field("_parriableOwner").SetValue((object)monster); Traverse.Create((object)val).Field("owner").SetValue((object)monster); } return val; } private void ConfigureEffectDealer(EffectDealer effectDealer, Player dp, DamageDealer damageDealer) { Traverse.Create((object)effectDealer).Field("valueProvider").SetValue((object)damageDealer); Traverse.Create((object)effectDealer).Field("fxTimingOverrider").SetValue((object)damageDealer); effectDealer.owner = (Actor)(object)dp; effectDealer.DealerEffectOwner = (IEffectOwner)(object)dp; List<DamageDealer> list = new List<DamageDealer> { damageDealer }; Traverse.Create((object)effectDealer).Field("customDealers").SetValue((object)list.ToArray()); } private void ConfigureEffectReceiver(GameObject playerObject, Player dp) { //IL_0044: Unknown result type (might be due to invalid IL or missing references) //IL_004e: Unknown result type (might be due to invalid IL or missing references) //IL_004f: Unknown result type (might be due to invalid IL or missing references) //IL_003c: Unknown result type (might be due to invalid IL or missing references) Transform obj = playerObject.transform.Find("RotateProxy/SpriteHolder/Health(Don'tKey)/DamageReceiver"); EffectReceiver val = ((obj != null) ? ((Component)obj).GetComponent<EffectReceiver>() : null); if (!((Object)(object)val == (Object)null)) { val.Owner = (IEffectOwner)(object)dp; if (isPVP) { val.effectType = (EffectType)10368; } else { val.effectType = (EffectType)(val.effectType & -10369); } } } private void DestroyChildObjects(GameObject parent, params string[] paths) { foreach (string text in paths) { Transform val = parent.transform.Find(text); if ((Object)(object)val != (Object)null) { Object.Destroy((Object)(object)((Component)val).gameObject); } } } private PlayerData CreatePlayerObject(int playerId, Vector3 position) { //IL_000a: Unknown result type (might be due to invalid IL or missing references) //IL_000b: Unknown result type (might be due to invalid IL or missing references) //IL_001b: Unknown result type (might be due to invalid IL or missing references) //IL_0021: Expected O, but got Unknown //IL_0069: Unknown result type (might be due to invalid IL or missing references) //IL_006e: Unknown result type (might be due to invalid IL or missing references) //IL_0075: Unknown result type (might be due to invalid IL or missing references) //IL_007b: Unknown result type (might be due to invalid IL or missing references) //IL_0087: Unknown result type (might be due to invalid IL or missing references) //IL_008d: Unknown result type (might be due to invalid IL or missing references) //IL_00ae: Unknown result type (might be due to invalid IL or missing references) //IL_00c7: Unknown result type (might be due to invalid IL or missing references) //IL_0133: Unknown result type (might be due to invalid IL or missing references) GameObject val = Object.Instantiate<GameObject>(((Component)Player.i).gameObject, position, Quaternion.identity); GameObject val2 = new GameObject("PlayerName"); if (!displayPlayerName.Value) { val2.SetActive(false); } TextMeshPro obj = val2.AddComponent<TextMeshPro>(); ((TMP_Text)obj).text = "Yuki"; ((TMP_Text)obj).fontSize = playerNameSize.Value; ((TMP_Text)obj).alignment = (TextAlignmentOptions)514; Vector3 position2 = val.transform.position; obj.transform.position = new Vector3(position2.x, position2.y + 50f, position2.z); val2.transform.SetParent(val.transform); obj.transform.rotation = Quaternion.identity; ((TMP_Text)obj).rectTransform.sizeDelta = new Vector2(2000f, 50f); Object.Destroy((Object)(object)val.GetComponent<Player>()); Player dp = val.AddComponent<Player>(); DestroyChildObjects(val, "RotateProxy/SpriteHolder/HitBoxManager/Foo", "RotateProxy/SpriteHolder/HitBoxManager/FooInit", "RotateProxy/SpriteHolder/HitBoxManager/FooExplode"); AutoAttributeManager.AutoReference(val); AutoAttributeManager.AutoReferenceAllChildren(val); ((Object)val).name = $"PlayerObject_{playerId}"; MakeDamage(val, dp); return new PlayerData(val, position, playerId, val2); } private void UpdatePlayerObject(PlayerData playerData, Vector3 position, string animationState, bool isFacingRight) { //IL_0030: Unknown result type (might be due to invalid IL or missing references) //IL_0036: Unknown result type (might be due to invalid IL or missing references) //IL_0037: Unknown result type (might be due to invalid IL or missing references) //IL_0053: Unknown result type (might be due to invalid IL or missing references) //IL_0099: Unknown result type (might be due to invalid IL or missing references) //IL_009e: Unknown result type (might be due to invalid IL or missing references) //IL_00d4: Unknown result type (might be due to invalid IL or missing references) //IL_00d9: Unknown result type (might be due to invalid IL or missing references) //IL_00dc: Unknown result type (might be due to invalid IL or missing references) //IL_00ac: Unknown result type (might be due to invalid IL or missing references) //IL_00b1: Unknown result type (might be due to invalid IL or missing references) //IL_00fc: Unknown result type (might be due to invalid IL or missing references) Transform val = playerData.PlayerObject.transform.Find("RotateProxy/SpriteHolder"); if ((Object)(object)val == (Object)null) { Log.Error("Player object not found!"); return; } ((Component)val).transform.position = position; Vector3 position2 = position; position2.y += 50f; playerData.nameObject.transform.position = position2; Animator component = ((Component)val).GetComponent<Animator>(); if ((Object)(object)component == (Object)null) { Log.Error("Animator not found on player object!"); return; } if (animationState != currentAnimationState) { currentAnimationState = animationState; component.CrossFade(animationState, 0.1f); } else { AnimatorStateInfo currentAnimatorStateInfo = component.GetCurrentAnimatorStateInfo(0); if (((AnimatorStateInfo)(ref currentAnimatorStateInfo)).IsName(animationState)) { currentAnimatorStateInfo = component.GetCurrentAnimatorStateInfo(0); if (((AnimatorStateInfo)(ref currentAnimatorStateInfo)).normalizedTime >= 1f) { component.PlayInFixedTime(animationState, 0, 0f); } } } Vector3 localScale = ((Component)val).transform.localScale; localScale.x = Mathf.Abs(localScale.x) * (float)(isFacingRight ? 1 : (-1)); ((Component)val).transform.localScale = localScale; } private void OnDestroy() { _harmony.UnpatchSelf(); SceneManager.sceneLoaded -= OnSceneLoaded; DisconnectFromServer(); } } [HarmonyPatch] public class Patches { [HarmonyPatch(typeof(Animator), "Play", new Type[] { typeof(string), typeof(int), typeof(float) })] [HarmonyPrefix] public static bool Prefix(Animator __instance, string stateName, int layer, float normalizedTime) { if (((Object)__instance).name == "SpriteHolder" && Multiplayer.Instance.localAnimationState != stateName) { Multiplayer.Instance.localAnimationState = stateName; } MonsterManager instance = SingletonBehaviour<MonsterManager>.Instance; if ((Object)(object)((instance != null) ? instance.FindClosestMonster() : null) != (Object)null) { MonsterBase val = SingletonBehaviour<MonsterManager>.Instance.FindClosestMonster(); if ((Object)(object)val != (Object)null) { object obj; if (__instance == null) { obj = null; } else { Transform transform = ((Component)__instance).transform; if (transform == null) { obj = null; } else { Transform parent = transform.parent; if (parent == null) { obj = null; } else { Transform parent2 = parent.parent; if (parent2 == null) { obj = null; } else { Transform parent3 = parent2.parent; obj = ((parent3 != null) ? ((Object)parent3).name : null); } } } } if ((string?)obj == ((Object)val).name) { Multiplayer.Instance.enemyAnimationState = stateName; } } } return true; } [HarmonyPatch(typeof(InControlManager), "Update")] [HarmonyPrefix] public static bool HookUpdate(InControlManager __instance) { if (Multiplayer.Instance.isTexting) { return false; } return true; } [HarmonyPatch(typeof(RCGInput), "SetCursorVisible")] [HarmonyPrefix] public static bool HookSetCursorVisible(ref bool visible) { if (Multiplayer.Instance.isTexting) { return false; } return true; } } public static class PluginInfo { public const string PLUGIN_GUID = "Multiplayer"; public const string PLUGIN_NAME = "Multiplayer"; public const string PLUGIN_VERSION = "1.0.0"; } }