diff --git a/ResoniteIPv6Mod/ResoniteIPv6Mod.cs b/ResoniteIPv6Mod/ResoniteIPv6Mod.cs index 2897125..67584ff 100644 --- a/ResoniteIPv6Mod/ResoniteIPv6Mod.cs +++ b/ResoniteIPv6Mod/ResoniteIPv6Mod.cs @@ -24,7 +24,7 @@ public static class BuildInfo { public const string Name = "ResoniteIPv6Mod"; public const string Author = "Rucio"; - public const string Version = "3.1.1"; + public const string Version = "4.0.0"; public const string Link = "https://github.com/bontebok/ResoniteIPv6Mod"; public const string GUID = "com.ruciomods.resoniteipv6mod"; } @@ -60,6 +60,8 @@ public override void OnEngineInit() } } + // LNL NativeSockets is not used, commenting out for now unless this becomes a problem. + /* [HarmonyPatch(typeof(NetManager))] public class NetManagerPatch { @@ -76,6 +78,7 @@ public static bool Start(ref bool ___UseNativeSockets) return true; } } + */ [HarmonyPatch(typeof(LNL_Listener))] public class ResoniteIPv6ModPatch @@ -132,8 +135,7 @@ public static bool GlobalAnnounceRefresh(LNL_Listener __instance, ref bool ___in private static readonly PropertyInfo Peer = AccessTools.Property(typeof(LNL_Connection), "Peer"); private static readonly PropertyInfo FailReason = AccessTools.Property(typeof(LNL_Connection), "FailReason"); - private static readonly MethodInfo ConnectToRelay = AccessTools.Method(typeof(LNL_Connection), "ConnectToRelay"); - private static bool? ForceRelay; + private static readonly MethodInfo ConnectToRelay = AccessTools.Method(typeof(LNL_Connection), "ConnectToRelay", new Type[] { typeof(RelaySettings) }); [HarmonyPrefix] [HarmonyPatch(typeof(LNL_Connection), "PunchthroughConnect")] @@ -153,42 +155,30 @@ public static bool PunchthroughConnect(LNL_Connection __instance, Action { + RelaySettings settings = await Settings.GetActiveSettingAsync(); await new ToBackground(); - if (!ForceRelay.HasValue) - { - ForceRelay = false; - foreach (string commandLineArg in Environment.GetCommandLineArgs()) - { - if (commandLineArg.ToLower().EndsWith("forcerelay")) - { - ForceRelay = true; - break; - } - } - } - List nodes = new List(); if (nodeId != null) { NetworkNodeInfo networkNodeInfo = await cloud.NetworkNodes.TryGetNodeWithRefetch(nodeId).ConfigureAwait(false); if (networkNodeInfo == null) - Msg($"Cannot find NAT Punchthrough node: {nodeId}"); + Msg($"Cannot find NAT Punchthrough node for connecting to {__instance.Address}: {nodeId}"); else nodes.Add(networkNodeInfo); } else { - foreach (NetworkNodeInfo node in cloud.NetworkNodes.GetNodes(NetworkNodeType.LNL_NAT, 0)) + foreach (NetworkNodeInfo node in cloud.NetworkNodes.GetNodes(NetworkNodeType.LNL_NAT, 2, new NetworkNodePreference?(Engine.Config.NodePreference), Engine.Config.UniverseId)) nodes.Add(node); } - if (!ForceRelay.Value) + if (!settings.AlwaysUseRelay.Value) { for (int i = 0; i < 5; ++i) // IPv6 first { statusCallback("World.Connection.LNL.NATPunchthrough".AsLocaleKey(null, "n", $"IPv6 {i}")); - Msg($"IPv6 Punchthrough attempt: {i.ToString()}"); + Msg($"IPv6 Punchthrough attempt for {__instance.Address}: {i.ToString()}"); client.NatPunchModule.SendNatIntroduceRequest(MatchMakerEPv6, "C;" + connectionToken); await Task.Delay(TimeSpan.FromSeconds(1.0)); @@ -197,18 +187,17 @@ public static bool PunchthroughConnect(LNL_Connection __instance, Action(ConnectToRelay, __instance).Invoke(); - ConnectToRelay.Invoke(__instance, new object[] { }); + Msg($"IPv4 Punchthrough failed for {__instance.Address}, Connecting to Relay"); + ConnectToRelay.Invoke(__instance, new object[] { settings }); } else { - // Exausted all options, fail FailReason.SetValue(__instance, "World.Error.FailedToConnect"); - ConnectionFailed?.Invoke(__instance); } return; - }); return false; }