diff --git a/Fika.Core/Coop/Matchmaker/MatchmakerAccept/MatchmakerAcceptPatches.cs b/Fika.Core/Coop/Matchmaker/MatchmakerAccept/MatchmakerAcceptPatches.cs index 48dc355d..bae39e6a 100644 --- a/Fika.Core/Coop/Matchmaker/MatchmakerAccept/MatchmakerAcceptPatches.cs +++ b/Fika.Core/Coop/Matchmaker/MatchmakerAccept/MatchmakerAcceptPatches.cs @@ -30,6 +30,8 @@ public static class MatchmakerAcceptPatches public static MatchMakerGroupPreview MatchMakerGroupPreview { get; set; } public static int HostExpectedNumberOfPlayers { get; set; } = 1; public static WeatherClass[] Nodes { get; set; } = null; + public static string RemoteIp; + public static int RemotePort; private static string groupId; private static long timestamp; #endregion diff --git a/Fika.Core/Networking/FikaClient.cs b/Fika.Core/Networking/FikaClient.cs index 00faa324..4b129c87 100644 --- a/Fika.Core/Networking/FikaClient.cs +++ b/Fika.Core/Networking/FikaClient.cs @@ -50,8 +50,8 @@ public NetManager NetClient } } public NetPeer ServerConnection { get; private set; } - public string IP { get; private set; } - public int Port { get; private set; } + /*public string IP { get; private set; } + public int Port { get; private set; }*/ public bool SpawnPointsReceived { get; private set; } = false; private readonly ManualLogSource clientLogger = BepInEx.Logging.Logger.CreateLogSource("Fika.Client"); @@ -94,19 +94,22 @@ public void Init() _netClient.Start(); - GetHostRequest body = new(MatchmakerAcceptPatches.GetGroupId()); + /*GetHostRequest body = new(MatchmakerAcceptPatches.GetGroupId()); GetHostResponse result = FikaRequestHandler.GetHost(body); IP = result.Ip; - Port = result.Port; + Port = result.Port;*/ - if (string.IsNullOrEmpty(IP)) + string ip = MatchmakerAcceptPatches.RemoteIp; + int port = MatchmakerAcceptPatches.RemotePort; + + if (string.IsNullOrEmpty(ip)) { Singleton.Instance.ShowErrorScreen("Network Error", "Unable to connect to the raid server. IP and/or Port was empty when requesting data!"); } else { - ServerConnection = _netClient.Connect(IP, Port, "fika.core"); + ServerConnection = _netClient.Connect(ip, port, "fika.core"); }; FikaEventDispatcher.DispatchEvent(new FikaClientCreatedEvent(this)); diff --git a/Fika.Core/Networking/FikaPingingClient.cs b/Fika.Core/Networking/FikaPingingClient.cs index ef77fa1a..e61f5865 100644 --- a/Fika.Core/Networking/FikaPingingClient.cs +++ b/Fika.Core/Networking/FikaPingingClient.cs @@ -1,4 +1,6 @@ using BepInEx.Logging; +using EFT.UI; +using Fika.Core.Coop.Matchmaker; using Fika.Core.Networking.Http; using Fika.Core.Networking.Http.Models; using LiteNetLib; @@ -14,6 +16,7 @@ internal class FikaPingingClient(string serverId) : INetEventListener private readonly ManualLogSource _logger = Logger.CreateLogSource("Fika.PingingClient"); private readonly string serverId = serverId; private IPEndPoint remoteEndPoint; + private IPEndPoint localEndPoint; public bool Received = false; public bool Init() @@ -26,7 +29,12 @@ public bool Init() GetHostRequest body = new(serverId); GetHostResponse result = FikaRequestHandler.GetHost(body); - string ip = result.Ip; + string ip = result.Ips[0]; + string localIp = null; + if (result.Ips.Length > 1) + { + localIp = result.Ips[1]; + } int port = result.Port; if (string.IsNullOrEmpty(ip)) @@ -42,23 +50,31 @@ public bool Init() } remoteEndPoint = new(IPAddress.Parse(ip), port); + if (!string.IsNullOrEmpty(localIp)) + { + localEndPoint = new(IPAddress.Parse(localIp), port); + } NetClient.Start(); return true; } - public bool PingEndPoint() + public void PingEndPoint() { if (Received) { - return true; + return; } NetDataWriter writer = new(); writer.Put("fika.hello"); - return NetClient.SendUnconnectedMessage(writer, remoteEndPoint); + NetClient.SendUnconnectedMessage(writer, remoteEndPoint); + if (localEndPoint != null) + { + NetClient.SendUnconnectedMessage(writer, localEndPoint); + } } public void OnConnectionRequest(ConnectionRequest request) @@ -94,6 +110,8 @@ public void OnNetworkReceiveUnconnected(IPEndPoint remoteEndPoint, NetPacketRead if (result == "fika.hello") { Received = true; + MatchmakerAcceptPatches.RemoteIp = remoteEndPoint.Address.ToString(); + MatchmakerAcceptPatches.RemotePort = remoteEndPoint.Port; } else { diff --git a/Fika.Core/Networking/FikaServer.cs b/Fika.Core/Networking/FikaServer.cs index 98a92f47..7c6e957b 100644 --- a/Fika.Core/Networking/FikaServer.cs +++ b/Fika.Core/Networking/FikaServer.cs @@ -152,7 +152,24 @@ public async Task Init() NotificationManagerClass.DisplayMessageNotification($"Server started on port {_netServer.LocalPort}.", EFT.Communications.ENotificationDurationType.Default, EFT.Communications.ENotificationIconType.EntryPoint); - SetHostRequest body = new(MyExternalIP, Port); + string[] Ips = []; + + foreach (string ip in FikaPlugin.Instance.LocalIPs) + { + if (ip.StartsWith("192.168")) // need to add more cases here later, for now only check normal range... + { + Ips = [MyExternalIP, ip]; + } + } + + if (Ips.Length < 1) + { + Ips = [MyExternalIP, ""]; + NotificationManagerClass.DisplayMessageNotification("Could not find a valid local IP!", + iconType: EFT.Communications.ENotificationIconType.Alert); + } + + SetHostRequest body = new(Ips, Port); FikaRequestHandler.UpdateSetHost(body); FikaEventDispatcher.DispatchEvent(new FikaServerCreatedEvent(this)); diff --git a/Fika.Core/Networking/Models/GetHostResponse.cs b/Fika.Core/Networking/Models/GetHostResponse.cs index 11f82482..9ed9c3b3 100644 --- a/Fika.Core/Networking/Models/GetHostResponse.cs +++ b/Fika.Core/Networking/Models/GetHostResponse.cs @@ -1,3 +1,4 @@ +using System.Collections.Generic; using System.Runtime.Serialization; namespace Fika.Core.Networking.Http.Models @@ -5,15 +6,15 @@ namespace Fika.Core.Networking.Http.Models [DataContract] public struct GetHostResponse { - [DataMember(Name = "ip")] - public string Ip; + [DataMember(Name = "ips")] + public string[] Ips; [DataMember(Name = "port")] public int Port; - public GetHostResponse(string ip, int port) + public GetHostResponse(string[] ips, int port) { - Ip = ip; + Ips = ips; Port = port; } } diff --git a/Fika.Core/Networking/Models/SetHostRequest.cs b/Fika.Core/Networking/Models/SetHostRequest.cs index aa539a09..34d05f43 100644 --- a/Fika.Core/Networking/Models/SetHostRequest.cs +++ b/Fika.Core/Networking/Models/SetHostRequest.cs @@ -1,4 +1,5 @@ using Fika.Core.Coop.Components; +using System.Collections.Generic; using System.Runtime.Serialization; namespace Fika.Core.Networking.Http.Models @@ -9,16 +10,16 @@ public struct SetHostRequest [DataMember(Name = "serverId")] public string ServerId; - [DataMember(Name = "ip")] - public string Ip; + [DataMember(Name = "ips")] + public string[] Ips; [DataMember(Name = "port")] public int Port; - public SetHostRequest(string ip, int port) + public SetHostRequest(string[] ips, int port) { ServerId = CoopHandler.GetServerId(); - Ip = ip; + Ips = ips; Port = port; } }