Skip to content

Commit

Permalink
Updated to work with Resonite 2024.8.5.1341
Browse files Browse the repository at this point in the history
Also works with .NET 8 headless.
  • Loading branch information
bontebok committed Aug 25, 2024
1 parent 5444875 commit f5b6654
Showing 1 changed file with 16 additions and 30 deletions.
46 changes: 16 additions & 30 deletions ResoniteIPv6Mod/ResoniteIPv6Mod.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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";
}
Expand Down Expand Up @@ -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
{
Expand All @@ -76,6 +78,7 @@ public static bool Start(ref bool ___UseNativeSockets)
return true;
}
}
*/

[HarmonyPatch(typeof(LNL_Listener))]
public class ResoniteIPv6ModPatch
Expand Down Expand Up @@ -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")]
Expand All @@ -153,42 +155,30 @@ public static bool PunchthroughConnect(LNL_Connection __instance, Action<LocaleS

__result = Task.Run(async () =>
{
RelaySettings settings = await Settings.GetActiveSettingAsync<RelaySettings>();
await new ToBackground();

if (!ForceRelay.HasValue)
{
ForceRelay = false;
foreach (string commandLineArg in Environment.GetCommandLineArgs())
{
if (commandLineArg.ToLower().EndsWith("forcerelay"))
{
ForceRelay = true;
break;
}
}
}

List<NetworkNodeInfo> nodes = new List<NetworkNodeInfo>();
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));

Expand All @@ -197,18 +187,17 @@ public static bool PunchthroughConnect(LNL_Connection __instance, Action<LocaleS
}
if (ipv6only)
{
Msg($"IPv6 Punchthrough failed");
Msg($"IPv6 Punchthrough failed. IPv4 fallback not enabled");
}
else
{
Msg($"IPv6 Punchthrough failed, falling back to IPv4");

for (int i = 0; i < 5; ++i)
for (int i = 0; i < 5; ++i) // IPv4 next
{
statusCallback("World.Connection.LNL.NATPunchthrough".AsLocaleKey(null, "n", $"IPv4 {i}"));
Msg($"IPv4 Punchthrough attempt: {i.ToString()}");
Msg($"IPv4 Punchthrough attempt for {__instance.Address}: {i.ToString()}");

// new NetworkNode implementation for IPv4
foreach (NetworkNodeInfo networkNodeInfo in nodes)
client.NatPunchModule.SendNatIntroduceRequest(networkNodeInfo.Address, networkNodeInfo.Port, "C;" + connectionToken);

Expand All @@ -222,21 +211,18 @@ public static bool PunchthroughConnect(LNL_Connection __instance, Action<LocaleS
}
}
}

if (!ipv6only)
{
statusCallback("World.Connection.LNL.Relay".AsLocaleKey(null, true, null));
Msg("IPv4 Punchthrough failed, Connecting to Relay");
//AccessTools.MethodDelegate<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;
}
Expand Down

0 comments on commit f5b6654

Please sign in to comment.