Skip to content

Commit

Permalink
Modded Leaderboard Patch (#7)
Browse files Browse the repository at this point in the history
* Change to .user File

* I hate patching async methods

this took me 2 hours

* Change async method finder location; added more transpiler patches

* Attempted patch - commit for history

* Changed to much simpler & compact method

kept async method finder function for future usage

* Changed to Lord's GameLibs; Updated Leaderboard Patch Respectively

* remove file left from user stuff

* Added older sdk support

* Lowered patch priorities

Allows other mods to override the prefix patch before it is ran - as these patches only change the parameter based on specific circumstances that would be changed by another mod

* Shortened Leaderboard Patchfile

Done so by publicizing game assets from GameLibs

* Change Binding Flags

* Fix incorrect patch

* Minor Refactoring & Reorganization
  • Loading branch information
Ryan Gregory authored Feb 1, 2024
1 parent f82a7fe commit 85df43f
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 43 deletions.
30 changes: 30 additions & 0 deletions LobbyCompatibility/Features/HarmonyHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using System;
using System.Reflection;
using System.Runtime.CompilerServices;
using HarmonyLib;

namespace LobbyCompatibility.Features;

/// <summary>
/// Helper class related to Harmony Patching.
/// </summary>
internal class HarmonyHelper
{
/// <summary>
/// Retrieves the MethodInfo of the compiler-generated async method.
/// Async method content is not actually in the <c>async Method()</c>, but instead is in a separate <c>struct</c> under the method "MoveNext";
/// this function retrieves that method info.
/// </summary>
/// <param name="type">(<see cref="Type"/>) The type of the class housing the method.</param>
/// <param name="method">(<see cref="string"/>) The name of the method being patched.</param>
/// <returns>(<see cref="MethodInfo"/>) The info of the async "MoveNext" method.</returns>
public static MethodInfo? GetAsyncInfo(Type type, string method)
{
// Get the Method Info of the target Async Method
return AccessTools.Method(type, method)
// Find the AsyncStateMachine class from target method
.GetCustomAttribute<AsyncStateMachineAttribute>()
// Get the struct type (random compiler junk)
.StateMachineType.GetMethod("MoveNext", BindingFlags.Instance | BindingFlags.NonPublic);
}
}
5 changes: 4 additions & 1 deletion LobbyCompatibility/LobbyCompatibilityPlugin.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
using BepInEx;
using System;
using System.Reflection;
using System.Runtime.CompilerServices;
using BepInEx;
using BepInEx.Logging;
using HarmonyLib;
using LobbyCompatibility.Attributes;
Expand Down
16 changes: 0 additions & 16 deletions LobbyCompatibility/Patches/ChallengeSaveFilePatch.cs

This file was deleted.

21 changes: 21 additions & 0 deletions LobbyCompatibility/Patches/ES3SettingsConstructorPrefix.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using System;
using HarmonyLib;

namespace LobbyCompatibility.Patches;

/// <summary>
/// Patches <see cref="ES3Settings(string, ES3Settings)" />.
/// Creates a separate modded challenge save file.
/// </summary>
/// <seealso cref="ES3Settings(string, ES3Settings)" />
[HarmonyPatch(typeof(ES3Settings), MethodType.Constructor, new Type[] { typeof(string), typeof(ES3Settings) })]
[HarmonyPriority(Priority.Last)]
[HarmonyWrapSafe]
internal class ES3SettingsConstructorPrefix
{
[HarmonyPrefix]
private static void FileSettings(ref string path)
{
path = path == "LCChallengeFile" ? "LCModdedChallengeFile" : path;
}
}
21 changes: 21 additions & 0 deletions LobbyCompatibility/Patches/FindOrCreateLeaderboardPrefix.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using HarmonyLib;
using Steamworks;

namespace LobbyCompatibility.Patches;

/// <summary>
/// Patches <see cref="ISteamUserStats.FindOrCreateLeaderboard" />.
/// Changes the target leaderboard to a modded one.
/// </summary>
/// <seealso cref="ISteamUserStats.FindOrCreateLeaderboard" />
[HarmonyPatch(typeof(ISteamUserStats), nameof(ISteamUserStats.FindOrCreateLeaderboard))]
[HarmonyPriority(Priority.Last)]
[HarmonyWrapSafe]
internal class FindOrCreateLeaderboardPrefix
{
[HarmonyPrefix]
private static void Prefix(ref string pchLeaderboardName)
{
pchLeaderboardName = (pchLeaderboardName.StartsWith("challenge")) ? $"modded_{pchLeaderboardName}" : pchLeaderboardName;
}
}
26 changes: 0 additions & 26 deletions LobbyCompatibility/Patches/LeaderboardNamePatch.cs

This file was deleted.

0 comments on commit 85df43f

Please sign in to comment.