Skip to content

Commit

Permalink
LAN Fixes (#76)
Browse files Browse the repository at this point in the history
* Fixed the client's mod list not showing on LAN

* Fixed additional LAN exception

* Removed lobby list filter on lan as well since its not used
  • Loading branch information
1A3Dev authored Nov 6, 2024
1 parent a2bd4d2 commit 45d55a1
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 10 deletions.
3 changes: 2 additions & 1 deletion LobbyCompatibility/Behaviours/ModdedLobbySlot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ internal void Setup(LobbySlot lobbySlot)
_lobbySlot = lobbySlot;

// Get the "diff" of the lobby
_lobbyDiff = LobbyHelper.GetLobbyDiff(_lobbySlot.thisLobby);
if (_lobbyDiff == null || !GameNetworkManager.Instance || !GameNetworkManager.Instance.disableSteam)
_lobbyDiff = LobbyHelper.GetLobbyDiff(_lobbySlot.thisLobby);

// Find player count text (could be moved/removed in a future update, but unlikely)
var playerCount = _lobbySlot.playerCount;
Expand Down
21 changes: 13 additions & 8 deletions LobbyCompatibility/Features/LobbyHelper.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
Expand Down Expand Up @@ -27,24 +28,24 @@ public static class LobbyHelper
/// </summary>
/// <param name="lobby"> The lobby to get the diff from. </param>
/// <returns> The <see cref="LobbyDiff" /> from the <see cref="Lobby" />. </returns>
public static LobbyDiff GetLobbyDiff(Lobby lobby) => GetLobbyDiff(lobby, null);
public static LobbyDiff GetLobbyDiff(Lobby? lobby) => GetLobbyDiff(lobby, null);

/// <summary>
/// Get a <see cref="LobbyDiff" /> from a <see cref="Lobby" /> or <see cref="IEnumerable{String}" />.
/// </summary>
/// <param name="lobby"> The lobby to cache the diff to and/or get the diff from. </param>
/// <param name="lobbyPluginString"> The json string to parse. </param>
/// <returns> The <see cref="LobbyDiff" />. </returns>
internal static LobbyDiff GetLobbyDiff(Lobby lobby, string? lobbyPluginString)
internal static LobbyDiff GetLobbyDiff(Lobby? lobby, string? lobbyPluginString)
{
if (LobbyDiffCache.TryGetValue(lobby.Id, out var cachedLobbyDiff))
if (lobby.HasValue && LobbyDiffCache.TryGetValue(lobby.Value.Id, out var cachedLobbyDiff))
{
LatestLobbyDiff = cachedLobbyDiff;
return cachedLobbyDiff;
}

var lobbyPlugins = PluginHelper
.ParseLobbyPluginsMetadata(lobbyPluginString ?? GetLobbyPlugins(lobby)).ToList();
.ParseLobbyPluginsMetadata(lobbyPluginString ?? (lobby.HasValue ? GetLobbyPlugins(lobby.Value) : string.Empty)).ToList();
_clientPlugins ??= PluginHelper.GetAllPluginInfo().ToList();

var pluginDiffs = new List<PluginDiff>();
Expand Down Expand Up @@ -119,7 +120,8 @@ internal static LobbyDiff GetLobbyDiff(Lobby lobby, string? lobbyPluginString)
LatestLobbyDiff = new LobbyDiff(pluginDiffs, lobbyCompatibilityPresent);

// Add to cache to avoid making multiple unnecessary GetData() calls
LobbyDiffCache.Add(lobby.Id, LatestLobbyDiff);
if (lobby.HasValue)
LobbyDiffCache.Add(lobby.Value.Id, LatestLobbyDiff);

return LatestLobbyDiff;
}
Expand All @@ -132,10 +134,13 @@ internal static LobbyDiff GetLobbyDiff(Lobby lobby, string? lobbyPluginString)
internal static string GetLobbyPlugins(Lobby lobby)
{
var lobbyPluginStrings = new List<string>();
var i = 0;

do lobbyPluginStrings.Insert(i, lobby.GetData($"{LobbyMetadata.Plugins}{i}"));
while (lobbyPluginStrings[i++].Contains("@"));
if (GameNetworkManager.Instance && !GameNetworkManager.Instance.disableSteam)
{
var i = 0;
do lobbyPluginStrings.Insert(i, lobby.GetData($"{LobbyMetadata.Plugins}{i}"));
while (lobbyPluginStrings[i++].Contains("@"));
}

return lobbyPluginStrings
.Join(delimiter: string.Empty)
Expand Down
3 changes: 2 additions & 1 deletion LobbyCompatibility/Patches/MenuManagerPostfix.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ private static void Postfix(MenuManager __instance)
UIHelper.ReskinRefreshButton(refreshButton);

// Add a custom "Mods" filtering type, and shift all other filtering UI elements to the left
UIHelper.AddCustomFilterToLobbyList(listPanel);
if (!GameNetworkManager.Instance || !GameNetworkManager.Instance.disableSteam)
UIHelper.AddCustomFilterToLobbyList(listPanel);
}
}
22 changes: 22 additions & 0 deletions LobbyCompatibility/Patches/StartAClientPostfix.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using HarmonyLib;
using LobbyCompatibility.Features;

namespace LobbyCompatibility.Patches;

/// <summary>
/// Patches <see cref="MenuManager.StartAClient" />.
/// Checks if required plugins are present in the lobby metadata and are the same version as the client.
/// </summary>
/// <seealso cref="MenuManager.StartAClient" />
[HarmonyPatch(typeof(MenuManager), nameof(MenuManager.StartAClient))]
[HarmonyPriority(Priority.Last)]
[HarmonyWrapSafe]
internal static class StartAClientPostfix
{
[HarmonyPrefix]
private static void Prefix()
{
// Create lobby diff so LatestLobbyDiff is set
LobbyHelper.GetLobbyDiff(null);
}
}

0 comments on commit 45d55a1

Please sign in to comment.