Skip to content

Commit

Permalink
fix merge conflict
Browse files Browse the repository at this point in the history
  • Loading branch information
legoandmars committed Feb 18, 2024
2 parents 382169c + b30c7fc commit 1682ea0
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 11 deletions.
33 changes: 30 additions & 3 deletions LobbyCompatibility/Features/LobbyHelper.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Collections.Generic;
using System.Linq;
using HarmonyLib;
using LobbyCompatibility.Enums;
using LobbyCompatibility.Models;
using Steamworks;
Expand All @@ -23,13 +24,21 @@ internal 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)
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)
{
if (LobbyDiffCache.TryGetValue(lobby.Id, out var cachedLobbyDiff))
return cachedLobbyDiff;

var lobbyPlugins = PluginHelper
.ParseLobbyPluginsMetadata(lobby.GetData(LobbyMetadata.Plugins)).ToList();
.ParseLobbyPluginsMetadata(lobbyPluginString ?? GetLobbyPlugins(lobby)).ToList();
_clientPlugins ??= PluginHelper.GetAllPluginInfo().ToList();

var pluginDiffs = new List<PluginDiff>();
Expand Down Expand Up @@ -105,6 +114,24 @@ public static LobbyDiff GetLobbyDiff(Lobby lobby)

return LatestLobbyDiff;
}

/// <summary>
/// Get the plugins json from a <see cref="Lobby" />.
/// </summary>
/// <param name="lobby"> The <see cref="Lobby" /> to get the json string from. </param>
/// <returns> A json <see cref="string" /> from the <see cref="Lobby" />. </returns>
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("@"));

return lobbyPluginStrings
.Join(delimiter: string.Empty)
.Replace("@", string.Empty);
}

public static string GetCompatibilityHeader(PluginDiffResult pluginDiffResult)
{
Expand Down
20 changes: 16 additions & 4 deletions LobbyCompatibility/Features/PluginHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -103,12 +103,24 @@ internal static IEnumerable<PluginInfoRecord> GetAllPluginInfo()
}

/// <summary>
/// Creates a json string containing the metadata of all plugins, to add to the lobby.
/// Creates a list of json strings containing the metadata of all plugins, to add to the lobby.
/// </summary>
/// <returns> A json string containing the metadata of all plugins. </returns>
internal static string GetLobbyPluginsMetadata()
/// <returns> A list of json strings containing the metadata of all plugins. </returns>
internal static IEnumerable<string> GetLobbyPluginsMetadata()
{
return JsonConvert.SerializeObject(GetAllPluginInfo().ToList(), new VersionConverter());
var json = JsonConvert.SerializeObject(GetAllPluginInfo().ToList(), new VersionConverter());

// The maximum string size for steam lobby metadata is 8192 (2^13).
// We want one less than the maximum to allow space for a delimiter
var maxChunkLength = 8191;

for (var i = 0; i < json.Length; i += maxChunkLength)
{
if (maxChunkLength + i > json.Length)
maxChunkLength = json.Length - i;

yield return json.Substring(i, maxChunkLength);
}
}

/// <summary>
Expand Down
7 changes: 7 additions & 0 deletions LobbyCompatibility/Models/PluginInfoRecord.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,16 @@ namespace LobbyCompatibility.Models;
/// <param name="VersionStrictness"> The version strictness of the plugin. </param>
[Serializable]
public record PluginInfoRecord(
[property:JsonProperty("i")]
string GUID,

[property:JsonProperty("v")]
[property: JsonConverter(typeof(VersionConverter))]
Version Version,

[property:JsonProperty("c")]
CompatibilityLevel? CompatibilityLevel,

[property:JsonProperty("s")]
VersionStrictness? VersionStrictness
);
4 changes: 2 additions & 2 deletions LobbyCompatibility/Patches/LobbyDataIsJoinablePostfix.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@ private static bool Postfix(bool isJoinable, ref Lobby lobby)
return PluginHelper.CanJoinVanillaLobbies() && isJoinable;
}

var lobbyPluginString = lobby.GetData(LobbyMetadata.Plugins);
var lobbyPluginString = LobbyHelper.GetLobbyPlugins(lobby).Join(delimiter: string.Empty);

// Create lobby diff so LatestLobbyDiff is set
LobbyHelper.GetLobbyDiff(lobby);
LobbyHelper.GetLobbyDiff(lobby, lobbyPluginString);

// If the lobby does not have any plugin information, return original result (since we can't check anything)
if (string.IsNullOrEmpty(lobbyPluginString))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,11 @@ private static void Postfix(Result result, ref Lobby lobby)
// Modded is flagged as true, since we're using mods
lobby.SetData(LobbyMetadata.Modded, "true");

// Add plugin metadata to the lobby so clients can check if they have the required plugins
lobby.SetData(LobbyMetadata.Plugins, PluginHelper.GetLobbyPluginsMetadata());
// Add paginated plugin metadata to the lobby so clients can check if they have the required plugins
var plugins = PluginHelper.GetLobbyPluginsMetadata().ToArray();
// Add each page - with a delimiter if there's another page
for (var i = 0; i < plugins.Length; i++)
lobby.SetData($"{LobbyMetadata.Plugins}{i}", $"{plugins[i]}{(i < plugins.Length - 1 ? "@" : string.Empty)}");

// Set the joinable modded metadata to the same value as the original joinable metadata, in case it wasn't originally joinable
lobby.SetData(LobbyMetadata.JoinableModded, lobby.GetData(LobbyMetadata.Joinable));
Expand Down

0 comments on commit 1682ea0

Please sign in to comment.